Visualize Your Ideas With Code
feature: persistent node ids
aarthificial0fac3d8d5807
diff --git a/src/Scene.ts b/src/Scene.tsindex 92ef8ab8..5583988d 100644--- a/src/Scene.ts+++ b/src/Scene.ts@@ -56,7 +56,6 @@ export class Scene extends Group {return this.timeEventsChanged.asEvent();}- private readonly debugNode: Debug;private readonly storageKey: string;private readonly timeEventsChanged = new SimpleEventDispatcher<TimeEvent[]>();private timeEventLookup: Record<string, TimeEvent> = {};@@ -66,6 +65,7 @@ export class Scene extends Group {private state: SceneState = SceneState.Initial;private cached = false;private preserveEvents = false;+ private counters: Record<string, number> = {};public constructor(public readonly project: Project,@@ -76,9 +76,6 @@ export class Scene extends Group {name: runnerFactory.name,...config,});- this.debugNode = new Debug();- this.add(this.debugNode);- this.debugNode.hide();decorate(runnerFactory, threadable());this.storageKey = `scene-${this.name()}`;@@ -107,7 +104,6 @@ export class Scene extends Group {if (runnerFactory) {this.runnerFactory = runnerFactory;}- this.debug(null);this.cached = false;this.storedEventLookup = {...this.storedEventLookup,@@ -119,9 +115,8 @@ export class Scene extends Group {public async reset(previousScene: Scene = null) {this.x(0).y(0);- this.debugNode.remove();+ this.counters = {};this.destroyChildren();- this.add(this.debugNode);this.previousScene = previousScene;this.runner = threads(() => this.runnerFactory(this, this.project),@@ -214,14 +209,19 @@ export class Scene extends Group {public add(...children: (Shape | Group)[]): this {super.add(...children.flat());- this.debugNode.moveToTop();this.updateLayout();return this;}- public debug(node: Node) {- this.debugNode.target(node);- this.debugNode.visible(node !== null);+ public generateNodeId(type: string): string {+ let id = 0;+ if (type in this.counters) {+ id = ++this.counters[type];+ } else {+ this.counters[type] = id;+ }++ return `${this.name()}.${type}.${id}`;}public getFrameEvent(name: string): number {diff --git a/src/patches/Node.ts b/src/patches/Node.tsindex b76c6e88..7c536c3c 100644--- a/src/patches/Node.ts+++ b/src/patches/Node.ts@@ -13,6 +13,8 @@ import {Rect} from 'konva/lib/shapes/Rect';import {Container} from 'konva/lib/Container';import {Transform} from 'konva/lib/Util';import {Konva} from 'konva/lib/Global';+import {NODE_ID} from '../symbols';+import {useScene} from '../utils';declare module 'konva/lib/Node' {export interface Node {@@ -225,7 +227,7 @@ Node.prototype.setHeight = function (this: Node, value: number) {};const super__getTransform = Node.prototype._getTransform;-Node.prototype._getTransform = function () {+Node.prototype._getTransform = function (this: Node) {const m = super__getTransform.call(this);const offset = this.getOriginOffset();if (offset.x !== 0 || offset.y !== 0) {@@ -235,6 +237,18 @@ Node.prototype._getTransform = function () {return m;};+const super_setAttrs = Node.prototype.setAttrs;+Node.prototype.setAttrs = function (this: Node, config: any) {+ if (!(NODE_ID in this.attrs)) {+ const scene = useScene();+ if (scene) {+ const type = this.className;+ this.attrs[NODE_ID] = scene.generateNodeId(type);+ }+ }+ return super_setAttrs.call(this, config);+};+Factory.addGetterSetter(Node, 'padd', new Spacing());Factory.addGetterSetter(Node, 'margin', new Spacing());Factory.addGetterSetter(Node, 'origin', Origin.Middle);diff --git a/src/player/Player.ts b/src/player/Player.tsindex 05c086ff..12b14294 100644--- a/src/player/Player.ts+++ b/src/player/Player.ts@@ -54,10 +54,15 @@ export class Player {return this.renderChanged.asEvent();}+ public get Reloaded() {+ return this.reloaded.asEvent();+ }+private readonly stateChanged = new SimpleEventDispatcher<PlayerState>();private readonly timeChanged = new SimpleEventDispatcher<PlayerTime>();private readonly renderChanged =new PromiseSimpleEventDispatcher<PlayerRenderEvent>();+ private readonly reloaded = new SimpleEventDispatcher<number>();private readonly audioElement: HTMLAudioElement = null;private startTime: number;@@ -217,7 +222,7 @@ export class Player {// Recalculateif (commands.recalculate) {- console.time('reload time');+ const startTime = performance.now();await this.project.recalculate();const duration = this.project.frame;const finished = await this.project.seek(this.frame);@@ -234,7 +239,7 @@ export class Player {this.updateFrame(this.project.frame);}this.request();- console.timeEnd('reload time');+ this.reloaded.dispatch(performance.now() - startTime);return;}diff --git a/src/symbols.ts b/src/symbols.tsindex 1bf3218c..59730bae 100644--- a/src/symbols.ts+++ b/src/symbols.ts@@ -1,3 +1,4 @@export const ANIMATE = Symbol('ANIMATE');export const PROJECT = Symbol('PROJECT');export const SCENE = Symbol('SCENE');+export const NODE_ID = Symbol.for('NODE_ID');\ No newline at end of file