repo_name
string
dataset
string
owner
string
lang
string
func_name
string
code
string
docstring
string
url
string
sha
string
obsidian-beautitab
github_2023
andrewmcgivery
typescript
Observable.setValue
setValue(value: any) { this.value = value; this.subscribers.forEach((callback) => callback({ ...this.value })); }
/** * Set the value * @param value */
https://github.com/andrewmcgivery/obsidian-beautitab/blob/8b796a66bd05e1aaab61b09f48b8a346d323dea7/src/Utils/Observable.ts#L16-L19
8b796a66bd05e1aaab61b09f48b8a346d323dea7
obsidian-beautitab
github_2023
andrewmcgivery
typescript
Observable.getValue
getValue() { return this.value; }
/** * Get the current value */
https://github.com/andrewmcgivery/obsidian-beautitab/blob/8b796a66bd05e1aaab61b09f48b8a346d323dea7/src/Utils/Observable.ts#L24-L26
8b796a66bd05e1aaab61b09f48b8a346d323dea7
obsidian-beautitab
github_2023
andrewmcgivery
typescript
Observable.onChange
onChange(callback: Function) { this.subscribers.push(callback); return () => { this.subscribers = this.subscribers.filter( (value) => value === callback ); }; }
/** * Subscribe to changes in the value. Function returns a "unsubscribe" function to clean up as nessessary. * @param callback */
https://github.com/andrewmcgivery/obsidian-beautitab/blob/8b796a66bd05e1aaab61b09f48b8a346d323dea7/src/Utils/Observable.ts#L32-L40
8b796a66bd05e1aaab61b09f48b8a346d323dea7
obsidian-beautitab
github_2023
andrewmcgivery
typescript
capitalizeFirstLetter
const capitalizeFirstLetter = (string: string) => string.charAt(0).toUpperCase() + string.slice(1);
/** * Capitlizes the firt letter of a string * @param string */
https://github.com/andrewmcgivery/obsidian-beautitab/blob/8b796a66bd05e1aaab61b09f48b8a346d323dea7/src/Utils/capitalizeFirstLetter.ts#L5-L6
8b796a66bd05e1aaab61b09f48b8a346d323dea7
starpls
github_2023
withered-magic
typescript
Context.start
async start() { // Establish connection to the language server. console.log('context: connecting to the language server'); const client = await this.getOrCreateClient(); await client.start(); // Register commands with the VSCode API. console.log('context: registering commands'); this.regist...
/** * Initializes the context and establishes */
https://github.com/withered-magic/starpls/blob/75ea08c66670a6fd9ba8ee321795bda391d958cd/editors/code/src/context.ts#L34-L43
75ea08c66670a6fd9ba8ee321795bda391d958cd
ollama-autocoder
github_2023
10Nates
typescript
messageHeaderSub
function messageHeaderSub(document: vscode.TextDocument) { const sub = apiMessageHeader .replace("{LANG}", document.languageId) .replace("{FILE_NAME}", document.fileName) .replace("{PROJECT_NAME}", vscode.workspace.name || "Untitled"); return sub; }
// Give model additional information
https://github.com/10Nates/ollama-autocoder/blob/84b0e99deddafd024f2b5460a7c063cff726a9af/src/extension.ts#L40-L46
84b0e99deddafd024f2b5460a7c063cff726a9af
ollama-autocoder
github_2023
10Nates
typescript
autocompleteCommand
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) { const document = textEditor.document; const position = textEditor.selection.active; // Get the current prompt let prompt = document.getText(new vscode.Range(document.lineAt(0).range.start, position)); ...
// internal function for autocomplete, not directly exposed
https://github.com/10Nates/ollama-autocoder/blob/84b0e99deddafd024f2b5460a7c063cff726a9af/src/extension.ts#L65-L182
84b0e99deddafd024f2b5460a7c063cff726a9af
ollama-autocoder
github_2023
10Nates
typescript
provideCompletionItems
async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) { // Create a completion item const item = new vscode.CompletionItem("Autocomplete with Ollama"); // Set the insert text to a placeholder item.insertText = new vscode.Snippe...
// Completion item provider callback for activate
https://github.com/10Nates/ollama-autocoder/blob/84b0e99deddafd024f2b5460a7c063cff726a9af/src/extension.ts#L185-L246
84b0e99deddafd024f2b5460a7c063cff726a9af
ollama-autocoder
github_2023
10Nates
typescript
activate
function activate(context: vscode.ExtensionContext) { // Register a completion provider for JavaScript files const completionProvider = vscode.languages.registerCompletionItemProvider("*", { provideCompletionItems }, ...completionKeys.split("") ); // Register a command for getting a completion from Ollama thr...
// This method is called when extension is activated
https://github.com/10Nates/ollama-autocoder/blob/84b0e99deddafd024f2b5460a7c063cff726a9af/src/extension.ts#L249-L274
84b0e99deddafd024f2b5460a7c063cff726a9af
ollama-autocoder
github_2023
10Nates
typescript
deactivate
function deactivate() { }
// This method is called when extension is deactivated
https://github.com/10Nates/ollama-autocoder/blob/84b0e99deddafd024f2b5460a7c063cff726a9af/src/extension.ts#L277-L277
84b0e99deddafd024f2b5460a7c063cff726a9af
cv
github_2023
BartoszJarocki
typescript
getCommandMenuLinks
function getCommandMenuLinks() { const links = []; if (RESUME_DATA.personalWebsiteUrl) { links.push({ url: RESUME_DATA.personalWebsiteUrl, title: "Personal Website", }); } return [ ...links, ...RESUME_DATA.contact.social.map((socialMediaLink) => ({ url: socialMediaLink.url, ...
/** * Transform social links for command menu */
https://github.com/BartoszJarocki/cv/blob/4bb244f5ffaca27a246c6ec07e76513858b1aaca/src/app/page.tsx#L39-L56
4bb244f5ffaca27a246c6ec07e76513858b1aaca
ha-fusion
github_2023
matt8707
typescript
handleError
function handleError(_error: unknown) { switch (_error) { case ERR_INVALID_AUTH: console.error('ERR_INVALID_AUTH'); options.clearTokens(); break; case ERR_CANNOT_CONNECT: console.error('ERR_CANNOT_CONNECT'); break; case ERR_CONNECTION_LOST: console.error('ERR_CONNECTION_LOST'); break; case...
// error string instead of code
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Socket.ts#L158-L180
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
random
function random(list: HassEntity[] | any[]) { if (!list || list.length === 0) return undefined; return list[Math.floor(Math.random() * list.length)]; }
/** * Returns a random entity_id from provided array */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/getRandomEntity.ts#L6-L9
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
handleDeviceTracker
function handleDeviceTracker(stateObj: HassEntity) { const { state, attributes } = stateObj; if (attributes?.source_type === 'router') { return state === 'home' ? 'lan-connect' : 'lan-disconnect'; } if (['bluetooth', 'bluetooth_le'].includes(attributes?.source_type)) { return state === 'home' ? 'bluetooth-conn...
// https://github.com/home-assistant/frontend/blob/dev/src/common/entity/device_tracker_icon.ts
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/computeIcon.ts#L144-L154
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
handleInputDatetime
function handleInputDatetime(stateObj: HassEntity) { const { attributes } = stateObj; if (!attributes?.has_date) { return 'clock'; } if (!attributes?.has_time) { return 'calendar'; } return FIXED_DOMAIN_ICONS['input_datetime']; }
// https://github.com/home-assistant/frontend/blob/dev/src/common/entity/state_icon.ts
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/computeIcon.ts#L157-L167
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
handleAlarmControlPanel
function handleAlarmControlPanel(stateObj: HassEntity) { const { state } = stateObj; switch (state) { case 'armed_away': return 'shield-lock'; case 'armed_custom_bypass': return 'security'; case 'armed_home': return 'shield-home'; case 'armed_night': return 'shield-moon'; case 'armed_vacation':...
// https://github.com/home-assistant/frontend/blob/dev/src/fake_data/entity_component_icons.ts
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/computeIcon.ts#L185-L208
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
batteryIcon
function batteryIcon(state: number | string, attributes: any) { const BATTERY_ICONS: Record<number, string> = { 10: 'battery-10', 20: 'battery-20', 30: 'battery-30', 40: 'battery-40', 50: 'battery-50', 60: 'battery-60', 70: 'battery-70', 80: 'battery-80', 90: 'battery-90', 100: 'battery' }; cons...
// https://github.com/home-assistant/frontend/blob/dev/src/common/entity/battery_icon.ts
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/computeIcon.ts#L368-L417
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.updateCallback
protected updateCallback() { // this.update(); }
/** * Can be overridden in `KonvaEditor` */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L53-L55
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.isGuide
protected isGuide(node: Konva.Node): boolean { return node?.attrs?.type?.includes('-guide'); }
/** * Helper to check for `Konva.Line` guides */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L60-L62
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.updateStateIcon
protected async updateStateIcon(node: Konva.Image, $states: HassEntities | undefined) { const entity_id = node.getAttr('entity_id'); if (!entity_id) { node.setAttr('icon', icons['state-icon']); node.setAttr('state_color', undefined); this.updateIcon(node); return; } if (!$states) $states = get(sta...
/** * Handles updating `state-icon` * - sets default icon if `entity_id` is missing * - updates `icon` if it doesn't match previous icon * - updates icon `state_color` if icon matches but not color */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L70-L111
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.updateStateLabel
protected updateStateLabel(node: Konva.Text, $states: HassEntities | undefined) { const unknownState = 'unknown'; const entity_id = node.getAttr('entity_id'); if (!entity_id || entity_id?.trim() === '') { if (node.text() !== unknownState) { node.text(unknownState); } return; } if (!$states) $st...
/** * Handles updating `state-label` * - Updates `text` if it doesn't match previous text * - Sets text to 'unknown' if it can't update */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L118-L139
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.updateImage
protected async updateImage(node: Konva.Image, src: string, fitImage: boolean) { return new Promise<void>((resolve) => { const image = new Image(); image.onload = () => { node.image(image); node.width(image.naturalWidth); node.height(image.naturalHeight); if (fitImage) this.fitImage(node); ...
/** * Update image * - Sets gray placeholder on error */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L145-L177
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.fitImage
protected fitImage(node: Konva.Image) { const canvasLayer = this.stage.findOne('Layer') as Konva.Layer; const canvas = canvasLayer.findOne('Rect') as Konva.Rect; const padding = 15; const centerX = canvas.x() + canvas.width() / 2; const centerY = canvas.y() + canvas.height() / 2; const maxWidth = canvas....
/** * Fits image to canvas * - canvas reference is defined in editor */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L183-L207
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.loadImage
protected async loadImage(src: string): Promise<HTMLImageElement> { return new Promise((resolve, reject) => { const image = new Image(); image.onload = () => resolve(image); image.onerror = () => reject(new Error(`Failed to load image: ${src}`)); image.src = src; }); }
/** * Helper method to load image */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L212-L219
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.updateImageCache
protected updateImageCache(id: string, image: HTMLImageElement) { konvaImageCache.update((cache) => { if (!cache[this.selId]) cache[this.selId] = {}; cache[this.selId][id] = image; return cache; }); }
/** * Handles $konvaImageCache store * * Stores images by id so it can be passed * to KonvaEditor without refetching images */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L227-L233
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.updateIcon
protected async updateIcon(node: Konva.Image) { if (!node.getAttr('icon')) return; const result = await this.handleSvg(node, undefined); if (!result) return; const { width, height, url } = result; return new Promise<void>((resolve, reject) => { const image = new Image(); image.onload = () => { no...
/** * Updates icon * - Loads new icon from `handleSvg` data * - Adds it to `$konvaImageCache` * - Update callback for `KonvaEditor` */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L241-L264
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.updateIconColor
protected updateIconColor(node: Konva.Image, color: string) { const image = node.getAttr('image') as HTMLImageElement; if (!image) return; const parts = image.src.split(','); const decoded = decodeURIComponent(parts[1]); const parsed = this.parser.parseFromString(decoded, 'image/svg+xml'); const svg = pars...
/** * Sets icon color by decoding svg * uri without fetching icon again */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L270-L285
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.handleSvg
protected async handleSvg( node: Konva.Image | undefined, icon: string | undefined, color?: string ): Promise<{ url: string; width: number; height: number; color: string; } | void> { icon = icon || node?.getAttr('icon'); if (!icon) { console.error('no icon specified'); return; } let data...
/** * Loads iconify data and returns an svg encoded uri * - handles either a `node` or string like 'mdi:dog' * - fetches new icon using iconify api * - handles aspect ratio for non-uniform icons like 'fa6-solid:arrow-down-long' */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L293-L360
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.getShapeAttrs
protected getShapeAttrs(node: Konva.Node) { const type = node?.attrs?.type; let attrs: Record<string, any> = { id: node.id(), type: node?.attrs?.type, name: node.name(), x: node.x(), y: node.y(), width: node.width(), height: node.height(), scaleX: node.scaleX(), scaleY: node.scaleY(), ...
/** * Explicitly get shape attributes, because * Konva doesn't include default values with `toJSON()` ... */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L366-L460
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.debounce
private debounce(func: (event: Event) => void, wait: number) { let timeout: ReturnType<typeof setTimeout>; return (event: Event) => { clearTimeout(timeout); timeout = setTimeout(() => func(event), wait); }; }
/** * Debounce method for `handleScaleChange()` */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L465-L471
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.handleScaleChange
private handleScaleChange = (event: Event) => { const visualViewport = event.target as VisualViewport; const current = visualViewport?.scale; if (this.visualViewportScale !== current) { this.stage.getLayers().forEach((layer) => { if (this.devicePixelRatio && current) { layer.getCanvas().setPixelRatio(...
/** * Handles canvas pixel ratio on viewport scale (smart zoom) * by multiplying devicePixelRatio with visualViewport scale */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaBase.destroyBase
protected destroyBase() { if (visualViewport) { visualViewport.removeEventListener('resize', this.debounceFunction); } window.removeEventListener('resize', this.handleZoomChange); this.stage.destroy(); }
/** * Destroy konva */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaBase.ts#L509-L515
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.update
private update() { konvaStore.set({ children: this.layer.toObject()?.children || [], selectedShapes: this.selectedShapes.map((shape) => this.getNodeData(shape)), mode: this.mode, undoStack: this.undoStack, redoStack: this.redoStack }); }
/** * Update $konvaStore to render ui updates */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L85-L93
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.updateCallback
protected updateCallback() { this.update(); }
/** * Runs update if called from `KonvaBase` */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L98-L100
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.handleMount
private async handleMount() { this.layer.find('Text').forEach((node) => { if (node?.attrs?.type === 'state-label' && node instanceof Konva.Text) { this.updateStateLabel(node, undefined); } }); const images = this.layer.find('Image'); const imageCache = get(konvaImageCache)?.[this.selId] || {}; awa...
/** * Handles mount * - handles state-label with no entity_id * - loads images from $konvaImageCache */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L107-L138
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.setupCanvas
private setupCanvas() { this.stage.add(this.canvasLayer); this.canvasLayer.moveToBottom(); this.canvasLayer.listening(false); this.canvas.setAttrs({ x: (this.stage.width() - this.canvasWidth) / 2, y: (this.stage.height() - this.canvasHeight) / 2, width: this.canvasWidth, height: this.canvasHeight, ...
/** * Setup canvas layer */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L143-L165
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.setupTransformer
private setupTransformer() { this.stage.add(this.selectionLayer); this.selectionLayer.add(this.transformer); this.transformer.setAttrs({ keepRatio: true, rotationSnapTolerance: 15 / 2 }); this.transformer.on('transformstart transform transformend', (event) => { const activeAnchor = this.transformer...
/** * Setup selection layer * - `rotationSnapTolerance` is half of a `rotationSnap` * - handle dragging corner anchors when shift is pressed * - limit `boundBox` if text node has custom `box` attribute * - add any text `box` event listeners * - add selection rectangle (mouse drag) */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L175-L217
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.setupEvents
private setupEvents() { this.stage.on('click tap dragstart', (event) => this.handleClick(event)); this.stage.on('add remove dragmove dragend', (event) => { const { type, target } = event; if (type === 'dragmove' && this.panning) { this.updateGuidePos(); } // prevent `record` when panning with gui...
/** * Adds event listeners * - handle click event * - record history and update on drag event * - handle mousedown/mouseup event * - handle mousemove event * - adds events guides * - add events for alt-duplicate * - adds guide snapping events */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L229-L260
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.handleShiftDragMove
public updateGuidePos = () => { const nodes = this.layer.find('Line').filter((node): node is Konva.Line => this.isGuide(node)); if (!nodes?.length) { this.guides = []; return; } const stagePos = this.stage.position(); const scaleX = this.stage.scaleX(); const scaleY = this.stage.scaleY(); this.gu...
/** * Updates `v-guide | h-guide` position * * Guides are drawn to fit stage dimensions, so * by zooming or panning they need to be updated */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.handleClick
private async handleClick(event: Konva.KonvaEventObject<MouseEvent | TouchEvent>) { const activeElement = document?.activeElement; if (activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement) { activeElement.blur(); await tick(); } if (this.mode === 'pan' || this.spaceKe...
/** * Handle click event * - blurs any active input field so data can be saved * - handle pan and zoom clicks * - handle stage click (no nodes) * - get node parent if group * - handle mutiselect cmd click */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L310-L386
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.handleMousedown
private handleMousedown(event: Konva.KonvaEventObject<MouseEvent | TouchEvent>) { if (this.mode === 'pan' || this.panning) return; const draggableNode = (node: Konva.Node): boolean => { if (node.draggable()) return true; const parent = node.getParent(); return parent && parent !== this.stage ? draggableNo...
/** * Handle mousedown event * - handles panning * - checks if target is `stage` or non-draggable node * - starts selection rectangle */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L394-L433
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.setupAltDuplicateEvents
private setupAltDuplicateEvents() { let nodes: (Konva.Shape | Konva.Group)[] = []; let clones: (Konva.Shape | Konva.Group)[] = []; let duplicating = false; this.stage.on('dragstart', (event) => { if (!this.altKey || duplicating || event?.target instanceof Konva.Line) return; duplicating = true; const...
/** * Handles alt-duplicate * - original nodes are already being dragged, so move them to top * - keep clones in place, so it looks like the original node is the clone * - keeps the original layer children node order when cloning */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L587-L644
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.setupGuideSnap
private setupGuideSnap() { this.layer.on('dragmove', (event) => { if (this.isGuide(event.target)) return; const nodes = this.selectedShapes.filter( (node): node is Konva.Shape | Konva.Group => !this.isGuide(node) ); const box = this.getSnapBoundingBox(nodes); const points = this.getSnapPoints(box...
/** * Sets up event listener for snap functionality * for when dragging shapes in close proximity to guides */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L650-L702
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.getSnapBoundingBox
private getSnapBoundingBox(nodes: (Konva.Shape | Konva.Group)[]) { const boxes = nodes.map((node) => node.getClientRect({ relativeTo: this.layer })); const x = Math.min(...boxes.map((box) => box.x)); const y = Math.min(...boxes.map((box) => box.y)); const width = Math.max(...boxes.map((box) => box.x + box.widt...
/** * Calculates bounding box that encloses selected nodes */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L707-L721
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.subscribeStates
subscribeStates() { this.unsubscribe = states.subscribe(($states) => { if (this.layer && $states) { const shapes = this.layer.getChildren( (node) => node.getAttr('entity_id') && node?.attrs?.type?.startsWith('state-') ); shapes.forEach((node) => { if (!this.transformer.nodes().includes(node))...
/** * Subscribes to $states store * and updates 'state-' nodes */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L727-L744
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.record
private record() { if (this.applyingState) return; const currentState: KonvaHistory = { elements: this.layer.children.map((shape) => this.getNodeData(shape)), selectedShapes: this.selectedShapes.map((shape) => shape.id()) }; const previousState = this.undoStack[this.undoStack.length - 1]; if (!this.s...
/** * Saves history state */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L749-L770
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.undo
public undo() { if (this.undoStack.length > 1) { const currentState = this.undoStack.pop(); if (currentState) this.redoStack.push(currentState); const previousState = this.undoStack[this.undoStack.length - 1]; this.applyState(previousState); if (previousState.selectedShapes.length === 0) { this.r...
/** * Handle undo */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L775-L789
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.redo
public redo() { if (this.redoStack.length > 0) { const previousState = this.redoStack.pop(); if (previousState) { this.undoStack.push(previousState); this.applyState(previousState); if (previousState.selectedShapes.length === 0) { this.restoreSelection(this.selectedShapes.map((shape) => shape...
/** * Handle redo */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L794-L809
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.applyState
private applyState(state: KonvaHistory) { this.applyingState = true; /** * Handles history state * - remove, updates or add nodes */ const localHandleState = (layer: Konva.Container, nodes: Konva.Node[]) => { layer.children.slice().forEach((node) => { const exists = nodes.some((item) => item?.att...
/** * Sets state from that point in history */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L814-L969
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
localHandleState
const localHandleState = (layer: Konva.Container, nodes: Konva.Node[]) => { layer.children.slice().forEach((node) => { const exists = nodes.some((item) => item?.attrs?.id === node.id()); if (!exists) node.destroy(); }); nodes.forEach(async (item, index) => { if (!item.attrs?.id) return; let n...
/** * Handles history state * - remove, updates or add nodes */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L821-L850
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
localUpdateNode
const localUpdateNode = async (node: Konva.Shape | Konva.Group, attrs: ShapeConfig) => { const onclick = attrs?.onclick; node.setAttr('onclick', onclick ? JSON.parse(JSON.stringify(onclick)) : undefined); const type = node?.attrs?.type; if (type === 'state-label') { node.setAttrs({ ...attrs, text: nod...
/** * Update node */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L855-L886
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
localAddNode
const localAddNode = async (nodeData: Konva.Node) => { let node: any; const attrs = nodeData?.attrs; const type = attrs?.type; switch (type) { case 'state-label': node = new Konva.Text(attrs); this.updateStateLabel(node, undefined); break; case 'text': node = new Konva.Text(attr...
/** * Add node */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L891-L958
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.restoreSelection
private restoreSelection(selectedShapes: string[]) { this.selectedShapes = selectedShapes .map((id) => this.layer.findOne(`#${id}`)) .filter( (shape): shape is Konva.Shape | Konva.Group => shape instanceof Konva.Shape || shape instanceof Konva.Group ); this.transformer.nodes(this.selectedShapes);...
/** * Restores transformer nodes on redo/undo */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L974-L983
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.stateEqual
private stateEqual(state1: KonvaHistory, state2: KonvaHistory) { if (!state1) return false; if (!this.arraysEqual(state1.selectedShapes, state2.selectedShapes)) return false; if (state1.elements.length !== state2.elements.length) return false; return state1.elements.every((node1: Konva.Node, index: number) =>...
/** * Helper method to check history state equality * Prevents duplicate entries if mutiple events are fired */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L989-L999
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.duplicateSelected
public duplicateSelected() { if (this.selectedShapes.length === 0) return; const clones: (Konva.Shape | Konva.Group)[] = []; const layerChildren = this.layer.getChildren(); const layerOrder = this.selectedShapes.sort( (a, b) => layerChildren.indexOf(a) - layerChildren.indexOf(b) ); layerOrder.forEach(...
/** * Duplicate selected shape (with Ctrl+J) */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1049-L1087
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.getSnapPoints
private getSnapPoints(box: { x: number; y: number; width: number; height: number }) { const points: { pos: number; diff: number; snap: string; orientation: string }[] = []; const pos = (start: number, size: number) => [ { guide: Math.round(start), snap: 'start' }, { guide: Math.round(start + size / 2), snap:...
/** * Determines snapping points for the bounding box relative to guide lines */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1092-L1131
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.setupGuideEvents
private setupGuideEvents(node: Konva.Line) { if (!this.isGuide(node)) return; node.on('mouseover dragstart', (event) => { if (node.visible() && node.draggable() && !this.selectionRect.isVisible()) { this.updateCursor(event); } }); node.on('dragmove', () => { if (node.attrs.type === 'v-guide') { ...
/** * Setup special guide events for `v-guide | h-guide` * - handle cursor update * - limit guide x/y position */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1138-L1161
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.updateCursor
public updateCursor(event?: Konva.KonvaEventObject<MouseEvent>) { const container = this.stage.container(); if (event && this.isGuide(event.target)) { const type = event.target?.attrs?.type; container.style.cursor = type === 'v-guide' ? 'col-resize' : 'row-resize'; return; } if (this.mode === 'pan' |...
/** * Updates mouse cursor * - guide * - pan * - zoom * - default */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1170-L1186
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.toggleTextBox
public toggleTextBox(id: string) { const node = this.layer.findOne(`#${id}`); if (node instanceof Konva.Text) { if (node.getAttr('box')) { node.setAttrs({ box: false, width: undefined, height: undefined, scaleX: 1, scaleY: 1 }); node.off('transform'); this.transformer.s...
/** * Toggles text box attribute * - adds/removes event listener */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1192-L1221
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.addTextBoxEventListener
private addTextBoxEventListener(node: Konva.Text) { node.off('transform'); node.on('transform', () => { node.setAttrs({ width: node.width() * node.scaleX(), height: node.height() * node.scaleY(), scaleX: 1, scaleY: 1 }); }); }
/** * Adds shape event listener to set fixed scale on text */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1226-L1237
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.fitCanvas
public fitCanvas() { const padding = 15; const stageWidth = this.stage.width() - padding * 2; const stageHeight = this.stage.height() - padding * 2; const scale = Math.min(stageWidth / this.canvasWidth, stageHeight / this.canvasHeight); const x = (this.stage.width() - this.canvasWidth * scale) / 2 - this.ca...
/** * Fits canvas to stage (double-clicking pan tool) */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1242-L1268
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.moveSelectedShapes
public moveSelectedShapes(x: number, y: number) { if (this.mode !== 'default' || this.selectedShapes.length === 0) return; this.selectedShapes.forEach((node) => { if (node.draggable()) { node.position({ x: node.x() + x, y: node.y() + y }); } }); this.update(); }
/** * Move nodes (arrow keys) * - don't record */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1274-L1287
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.updateAttr
public async updateAttr(id: string, key: string, value: any, oninput = false) { const node = this.layer.findOne(`#${id}`); if (!node) return; const attrs = node.attrs; const type = attrs?.type; switch (true) { case key === 'src' && node instanceof Konva.Image: await this.updateImage(node, value, true...
/** * Helper method for `setAttr` */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1292-L1368
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.toggleVisibility
public toggleVisibility(id?: string) { const nodes = id ? [this.layer.findOne(`#${id}`)] : this.selectedShapes; nodes.forEach((node) => { if (node) node.visible(!node.visible()); }); this.transformer.nodes( this.selectedShapes.filter( (node) => node.visible() && node.draggable() && !this.isGuide(nod...
/** * Handles node visibility */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1373-L1388
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.toggleDraggable
public toggleDraggable(id?: string) { const nodes = id ? [this.layer.findOne(`#${id}`)] : this.selectedShapes; const validNodes = nodes.filter( (shape): shape is Konva.Shape | Konva.Group => shape instanceof Konva.Shape || shape instanceof Konva.Group ); if (validNodes.length) { const state = !valid...
/** * Toggles node lock */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1393-L1414
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.toggleKeepRatio
public toggleKeepRatio() { if (this.transformer) { this.transformer.keepRatio(!this.transformer.keepRatio()); this.update(); } }
/** * Toggles shape scale ratio lock */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1419-L1425
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.handleElementClick
public handleElementClick(event: MouseEvent, id: string) { const { shiftKey, ctrlKey, metaKey } = event; if (shiftKey && this.selectedShapes.length > 0) { const start = this.layer.findOne(`#${this.selectedShapes[0].id()}`) as Konva.Shape; const end = this.layer.findOne(`#${id}`) as Konva.Shape; if (!start...
/** * Handle node select in elements panel (outside of canvas) * - handles range select (shift) * - handles multi select (cmd) * - handles default click */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1433-L1470
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.selectShapesById
public selectShapesById(ids: string[]) { this.selectedShapes = ids .map((id) => this.layer.findOne(`#${id}`)) .filter((node) => node instanceof Konva.Shape || node instanceof Konva.Group); this.transformer.nodes( this.selectedShapes.filter( (node) => node.visible() && node.draggable() && !this.isGuide...
/** * Selects nodes by ids */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1475-L1487
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.selectAll
public selectAll() { this.selectedShapes = this.layer.getChildren( (node) => node instanceof Konva.Shape || node instanceof Konva.Group ); this.transformer.nodes( this.selectedShapes.filter( (node) => node.visible() && node.draggable() && !this.isGuide(node) ) ); this.update(); }
/** * Selects all nodes (Ctrl+A) */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1492-L1504
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.deselectAll
public deselectAll() { this.selectedShapes = []; this.transformer.nodes([]); }
/** * Deselects all nodes */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1509-L1512
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.deleteSelected
public deleteSelected() { this.selectedShapes.forEach((node) => { node.destroy(); }); this.deselectAll(); this.record(); this.update(); }
/** * Delete selected nodes */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1517-L1525
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.reorderElements
public async reorderElements(shapes: string[]) { shapes.forEach((id, index) => { const node = this.layer.findOne(`#${id}`); if (node) node.zIndex(this.layer.children.length - 1 - index); }); await tick(); this.record(); this.update(); }
/** * Reorders nodes with drag and drop */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1530-L1540
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.updateRotationSnaps
public updateRotationSnaps() { const snapPoints = [ 0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345 ]; this.transformer.rotationSnaps(this.shiftPressed ? snapPoints : []); }
/** * Handles rotation snapping */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1545-L1552
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.handleGroup
public handleGroup() { if (this.selectedShapes.length > 1) { this.group(); } else if (this.selectedShapes.length === 1 && this.selectedShapes[0] instanceof Konva.Group) { this.ungroup(); } }
/** * Handles group and ungrouping */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1557-L1563
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.group
private group() { const nodes = this.selectedShapes .filter((node) => !this.isGuide(node)) .sort((a, b) => { return this.layer.children.indexOf(a) - this.layer.children.indexOf(b); }); if (nodes.length < 2) return; const group = new Konva.Group({ id: this.generateUniqueId('group'), type: 'gro...
/** * Groups nodes */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1568-L1602
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.ungroup
private ungroup() { if (this.selectedShapes.length !== 1 && !(this.selectedShapes[0] instanceof Konva.Group)) return; const selectedShapes: (Konva.Shape | Konva.Group)[] = []; const group = this.selectedShapes[0] as Konva.Group; const groupIndex = this.layer.children.indexOf(group); group .getChildre...
/** * Ungroups nodes */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1607-L1639
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.setMode
public setMode(mode: KonvaMode) { this.mode = mode; if (mode === 'pan') { this.startPan(); } else { this.stopPan(); } this.update(); }
/** * Set tool mode */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1644-L1654
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.startPan
public startPan() { this.panning = true; this.stage.draggable(true); this.layer.setAttr('listening', false); this.selectionLayer.setAttr('listening', false); this.updateCursor(); }
/** * Starts pan mode (space) */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1659-L1667
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.stopPan
public stopPan() { this.panning = false; this.stage.draggable(false); this.layer.setAttr('listening', this.mode === 'default'); this.selectionLayer.setAttr('listening', this.mode === 'default'); this.updateCursor(); }
/** * Stops pan mode (space release) */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1672-L1680
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.setZoom
public setZoom(type: 'in' | 'out' | 'reset', point: { x: number; y: number }) { if (this.currentTween) { this.currentTween.finish(); this.currentTween = undefined; } let scale; if (type === 'reset') { scale = 1; } else { const duration = 0.4; const zoomFactor = this.zoomFactor * Math.exp(dura...
/** * Handle zoom * - logarithmic scale otherwise it takes forever to zoom with extreme values */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1686-L1720
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.zoomToArea
private zoomToArea(box: { x: number; y: number; width: number; height: number }) { if (this.altKey) return; box = { x: (box.x - this.stage.x()) / this.zoomFactor, y: (box.y - this.stage.y()) / this.zoomFactor, width: box.width / this.zoomFactor, height: box.height / this.zoomFactor }; const center...
/** * Zooms to area within selection rectangle */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1725-L1758
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.addStateLabel
public addStateLabel() { const node = new Konva.Text({ type: 'state-label', name: 'State Label', entity_id: '', fontSize: 60, fill: '#ffffff', fontStyle: 'normal', fontFamily: 'Inter Variable', draggable: true }); this.updateStateLabel(node, undefined); this.handleAddNode(node); }
/** * Add state-label */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1763-L1778
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.addStateIcon
public async addStateIcon() { try { const icon = icons['state-icon']; const color = '#d5d5d5'; const result = await this.handleSvg(undefined, icon, color); if (!result) return; const { width, height, url } = result; const image = await this.loadImage(url); const node = new Konva.Image({ ...
/** * Adds state-icon */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1783-L1812
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.addText
public addText() { const node = new Konva.Text({ type: 'text', name: 'Text', text: 'Text', fontSize: 60, fill: '#ffffff', fontFamily: 'Inter Variable', draggable: true, align: 'left' }); this.handleAddNode(node); }
/** * Add text */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1817-L1830
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.addIcon
public async addIcon() { try { const icon = icons['icon']; const color = '#d5d5d5'; const result = await this.handleSvg(undefined, icon, color); if (!result) return; const { width, height, url } = result; const image = await this.loadImage(url); const node = new Konva.Image({ type: 'icon...
/** * Add icon */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1835-L1863
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.addImage
public async addImage() { const src = 'https://demo.home-assistant.io/stub_config/t-shirt-promo.png'; try { const image = await this.loadImage(src); const node = new Konva.Image({ type: 'image', name: 'Image', image: image, src: src, width: image.naturalWidth || 64, height: image.nat...
/** * Add image */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1868-L1904
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.addRectangle
public addRectangle() { const node = new Konva.Rect({ type: 'rectangle', name: 'Rectangle', width: 150, height: 80, fill: '#ffffff', draggable: true }); this.handleAddNode(node); }
/** * Add rectangle */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1909-L1920
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.addCircle
public addCircle() { const node = new Konva.Circle({ type: 'circle', name: 'Circle', radius: 50, fill: '#ffffff', draggable: true }); this.handleAddNode(node); }
/** * Add circle */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1925-L1935
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.addVerticalGuide
public addVerticalGuide() { const stageHeight = this.stage.height(); const centerX = this.canvasWidth / 2; const topY = -(stageHeight - this.canvasHeight) / 2; const bottomY = stageHeight / 2 + this.canvasHeight / 2; const node = new Konva.Line({ type: 'v-guide', points: [centerX, topY, centerX, bottom...
/** * Add v-guide */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1940-L1958
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.addHorizontalGuide
public addHorizontalGuide() { const stageWidth = this.stage.width(); const centerY = this.canvasHeight / 2; const leftX = -(stageWidth - this.canvasWidth) / 2; const rightX = stageWidth / 2 + this.canvasWidth / 2; const node = new Konva.Line({ type: 'h-guide', points: [leftX, centerY, rightX, centerY],...
/** * Add h-guide */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1963-L1981
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.handleAddNode
private handleAddNode(node: Konva.Shape) { const type = node?.attrs?.type; node.setAttrs({ id: this.generateUniqueId(type) }); if (node instanceof Konva.Circle) node.position({ x: this.canvasWidth / 2, y: this.canvasHeight / 2 }); else if (!this.isGuide(node)) { node.position({ x: (t...
/** * Handle add node */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L1986-L2021
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.getElementsData
public getElementsData() { return this.layer.getChildren().map((node) => this.getNodeData(node)); }
/** * onDestroy helper to record data */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L2026-L2028
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.getNodeData
private getNodeData(node: Konva.Node) { const attrs = this.getShapeAttrs(node); delete attrs.image; if (attrs.type === 'state-icon') delete attrs.state_color; const data: any = { attrs, className: node.getClassName() }; if (node instanceof Konva.Group) { data.children = node.getChildren().map((c...
/** * Get node * - removes image attribute because it can't be serialized * - removes state-icon color * - handles nested group nodes */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L2036-L2052
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaEditor.destroyEditor
public destroyEditor() { window.removeEventListener('mousemove', this.handleMouseMove); window.removeEventListener('mouseup', this.handleMouseup); this.unsubscribe?.(); super.destroyBase(); }
/** * Destroy editor */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaEditor.ts#L2063-L2068
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaViewer.handleMount
private async handleMount() { this.layer .getChildren((node) => node instanceof Konva.Shape || node instanceof Konva.Group) .forEach((node) => { if (!node.isVisible() || this.isGuide(node)) { node.remove(); } else { this.handleNodeClick(node); if (node?.attrs?.type === 'state-label' && n...
/** * Handles mount * - removes guides and hidden shapes * - disables `draggable` attribute * - handles state-label with no entity_id * - loads images and icons * - updates $konvaImageCache */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaViewer.ts#L27-L73
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaViewer.subscribeStates
private subscribeStates() { this.unsubscribe = states.subscribe(($states) => { if (this.layer && $states) { const nodes = this.layer.getChildren( (node) => node.getAttr('entity_id') && node?.attrs?.type?.startsWith('state-') ); nodes.forEach((node) => { if (node instanceof Konva.Image) { ...
/** * Subscribes to $states store * and updates 'state-' shapes */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaViewer.ts#L79-L94
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaViewer.handleNodeClick
private handleNodeClick(node: Konva.Node) { const onclick = node.getAttr('onclick'); node.listening(!!onclick); node.draggable(false); node.off('mouseenter mouseleave click tap'); if (onclick && typeof onclick === 'object') { const setCursor = (cursor: string) => { this.stage.container().style.cursor...
/** * Handle click events */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaViewer.ts#L99-L140
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaViewer.updateLayerChildren
public async updateLayerChildren(data: any[]) { this.layer .getChildren((node) => !data.some((nodeData) => nodeData?.attrs?.id === node.getAttr('id'))) .forEach((node) => node.destroy()); await Promise.all( data .filter((nodeData) => !this.isGuide(nodeData)) .map(async (nodeData, index) => { ...
/** * Updates layer children * - removes nonexistent nodes * - updates or creates nodes * - updates $konvaImageCache */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaViewer.ts#L148-L176
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaViewer.updateNode
private async updateNode(node: Konva.Node, nodeData: Konva.Node) { const onclick = nodeData?.attrs?.onclick; node.setAttr('onclick', onclick ? JSON.parse(JSON.stringify(onclick)) : undefined); const type = node?.attrs?.type; if (type === 'state-label' && node instanceof Konva.Text) { node.setAttrs({ ......
/** * Handle node updates * - updates `onclick` attribute * - updates node attributes * - loads any icons/images * * does not update reactive attributes: * - state-label `text` * - state-icon `image`, `icon` and `state_color` */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaViewer.ts#L188-L222
25c374d31ccf1ea05aaf324d473ff975181c0308
ha-fusion
github_2023
matt8707
typescript
KonvaViewer.createNode
private async createNode(nodeData: Konva.Node) { let node; const attrs = nodeData?.attrs; const type = attrs?.type; switch (type) { case 'state-label': node = new Konva.Text(attrs); this.updateStateLabel(node, undefined); break; case 'state-icon': node = new Konva.Image(attrs); this.u...
/** * Handle node creation */
https://github.com/matt8707/ha-fusion/blob/25c374d31ccf1ea05aaf324d473ff975181c0308/src/lib/Modal/PictureElements/konvaViewer.ts#L227-L276
25c374d31ccf1ea05aaf324d473ff975181c0308