@fieldnotes/core 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -30,6 +30,7 @@ interface BaseElement {
30
30
  position: Point;
31
31
  zIndex: number;
32
32
  locked: boolean;
33
+ layerId: string;
33
34
  }
34
35
  interface StrokeElement extends BaseElement {
35
36
  type: 'stroke';
@@ -87,6 +88,15 @@ interface ShapeElement extends BaseElement {
87
88
  type CanvasElement = StrokeElement | NoteElement | ArrowElement | ImageElement | HtmlElement | TextElement | ShapeElement;
88
89
  type ElementType = CanvasElement['type'];
89
90
 
91
+ interface Layer {
92
+ id: string;
93
+ name: string;
94
+ visible: boolean;
95
+ locked: boolean;
96
+ order: number;
97
+ opacity: number;
98
+ }
99
+
90
100
  interface CanvasState {
91
101
  version: number;
92
102
  camera: {
@@ -94,11 +104,12 @@ interface CanvasState {
94
104
  zoom: number;
95
105
  };
96
106
  elements: CanvasElement[];
107
+ layers?: Layer[];
97
108
  }
98
109
  declare function exportState(elements: CanvasElement[], camera: {
99
110
  position: Point;
100
111
  zoom: number;
101
- }): CanvasState;
112
+ }, layers?: Layer[]): CanvasState;
102
113
  declare function parseState(json: string): CanvasState;
103
114
 
104
115
  declare function snapPoint(point: Point, gridSize: number): Point;
@@ -116,7 +127,9 @@ interface ElementStoreEvents {
116
127
  declare class ElementStore {
117
128
  private elements;
118
129
  private bus;
130
+ private layerOrderMap;
119
131
  get count(): number;
132
+ setLayerOrder(order: Map<string, number>): void;
120
133
  getAll(): CanvasElement[];
121
134
  getById(id: string): CanvasElement | undefined;
122
135
  getElementsByType<T extends ElementType>(type: T): Extract<CanvasElement, {
@@ -156,15 +169,47 @@ declare class Camera {
156
169
  private notifyChange;
157
170
  }
158
171
 
172
+ declare class LayerManager {
173
+ private readonly store;
174
+ private layers;
175
+ private _activeLayerId;
176
+ private bus;
177
+ constructor(store: ElementStore);
178
+ get activeLayer(): Layer;
179
+ get activeLayerId(): string;
180
+ getLayers(): Layer[];
181
+ getLayer(id: string): Layer | undefined;
182
+ isLayerVisible(id: string): boolean;
183
+ isLayerLocked(id: string): boolean;
184
+ createLayer(name?: string): Layer;
185
+ removeLayer(id: string): void;
186
+ renameLayer(id: string, name: string): void;
187
+ reorderLayer(id: string, newOrder: number): void;
188
+ setLayerVisible(id: string, visible: boolean): boolean;
189
+ setLayerLocked(id: string, locked: boolean): boolean;
190
+ setActiveLayer(id: string): void;
191
+ moveElementToLayer(elementId: string, layerId: string): void;
192
+ snapshot(): Layer[];
193
+ loadSnapshot(layers: Layer[]): void;
194
+ on(event: 'change', callback: () => void): () => void;
195
+ addLayerDirect(layer: Layer): void;
196
+ removeLayerDirect(id: string): void;
197
+ updateLayerDirect(id: string, props: Omit<Partial<Layer>, 'id'>): void;
198
+ private syncLayerOrder;
199
+ private findFallbackLayer;
200
+ }
201
+
159
202
  interface AutoSaveOptions {
160
203
  key?: string;
161
204
  debounceMs?: number;
205
+ layerManager?: LayerManager;
162
206
  }
163
207
  declare class AutoSave {
164
208
  private readonly store;
165
209
  private readonly camera;
166
210
  private readonly key;
167
211
  private readonly debounceMs;
212
+ private readonly layerManager?;
168
213
  private timerId;
169
214
  private unsubscribers;
170
215
  constructor(store: ElementStore, camera: Camera, options?: AutoSaveOptions);
@@ -207,6 +252,9 @@ interface ToolContext {
207
252
  setCursor?: (cursor: string) => void;
208
253
  snapToGrid?: boolean;
209
254
  gridSize?: number;
255
+ activeLayerId?: string;
256
+ isLayerVisible?: (layerId: string) => boolean;
257
+ isLayerLocked?: (layerId: string) => boolean;
210
258
  }
211
259
  interface PointerState {
212
260
  x: number;
@@ -342,6 +390,7 @@ declare class Viewport {
342
390
  private readonly container;
343
391
  readonly camera: Camera;
344
392
  readonly store: ElementStore;
393
+ readonly layerManager: LayerManager;
345
394
  readonly toolManager: ToolManager;
346
395
  readonly history: HistoryStack;
347
396
  readonly domLayer: HTMLDivElement;
@@ -405,6 +454,7 @@ declare class Viewport {
405
454
  private syncDomNode;
406
455
  private renderDomContent;
407
456
  private unbindArrowsFrom;
457
+ private hideDomNode;
408
458
  private removeDomNode;
409
459
  private clearDomNodes;
410
460
  private createWrapper;
@@ -417,7 +467,10 @@ declare class Viewport {
417
467
 
418
468
  declare class ElementRenderer {
419
469
  private store;
470
+ private imageCache;
471
+ private onImageLoad;
420
472
  setStore(store: ElementStore): void;
473
+ setOnImageLoad(callback: () => void): void;
421
474
  isDomElement(element: CanvasElement): boolean;
422
475
  renderCanvasElement(ctx: CanvasRenderingContext2D, element: CanvasElement): void;
423
476
  private renderStroke;
@@ -427,6 +480,8 @@ declare class ElementRenderer {
427
480
  private renderShape;
428
481
  private fillShapePath;
429
482
  private strokeShapePath;
483
+ private renderImage;
484
+ private getImage;
430
485
  }
431
486
 
432
487
  declare class NoteEditor {
@@ -452,6 +507,7 @@ interface BaseDefaults {
452
507
  position?: Point;
453
508
  zIndex?: number;
454
509
  locked?: boolean;
510
+ layerId?: string;
455
511
  }
456
512
  interface StrokeInput extends BaseDefaults {
457
513
  points: StrokePoint[];
@@ -525,7 +581,7 @@ declare function isBindable(element: CanvasElement): boolean;
525
581
  declare function getElementCenter(element: CanvasElement): Point;
526
582
  declare function getElementBounds(element: CanvasElement): Rect | null;
527
583
  declare function getEdgeIntersection(bounds: Rect, outsidePoint: Point): Point;
528
- declare function findBindTarget(point: Point, store: ElementStore, threshold: number, excludeId?: string): CanvasElement | null;
584
+ declare function findBindTarget(point: Point, store: ElementStore, threshold: number, excludeId?: string, filter?: (el: CanvasElement) => boolean): CanvasElement | null;
529
585
  declare function findBoundArrows(elementId: string, store: ElementStore): ArrowElement[];
530
586
  declare function updateBoundArrow(arrow: ArrowElement, store: ElementStore): Partial<ArrowElement> | null;
531
587
  declare function clearStaleBindings(arrow: ArrowElement, store: ElementStore): Partial<ArrowElement> | null;
@@ -657,6 +713,7 @@ declare class ArrowTool implements Tool {
657
713
  private toTarget;
658
714
  constructor(options?: ArrowToolOptions);
659
715
  setOptions(options: ArrowToolOptions): void;
716
+ private layerFilter;
660
717
  onPointerDown(state: PointerState, ctx: ToolContext): void;
661
718
  onPointerMove(state: PointerState, ctx: ToolContext): void;
662
719
  onPointerUp(_state: PointerState, ctx: ToolContext): void;
@@ -743,6 +800,30 @@ declare class ShapeTool implements Tool {
743
800
  private onKeyUp;
744
801
  }
745
802
 
746
- declare const VERSION = "0.5.0";
803
+ declare class CreateLayerCommand implements Command {
804
+ private readonly manager;
805
+ private readonly layer;
806
+ constructor(manager: LayerManager, layer: Layer);
807
+ execute(_store: ElementStore): void;
808
+ undo(_store: ElementStore): void;
809
+ }
810
+ declare class RemoveLayerCommand implements Command {
811
+ private readonly manager;
812
+ private readonly layer;
813
+ constructor(manager: LayerManager, layer: Layer);
814
+ execute(_store: ElementStore): void;
815
+ undo(_store: ElementStore): void;
816
+ }
817
+ declare class UpdateLayerCommand implements Command {
818
+ private readonly manager;
819
+ private readonly layerId;
820
+ private readonly previous;
821
+ private readonly current;
822
+ constructor(manager: LayerManager, layerId: string, previous: Layer, current: Layer);
823
+ execute(_store: ElementStore): void;
824
+ undo(_store: ElementStore): void;
825
+ }
826
+
827
+ declare const VERSION = "0.6.1";
747
828
 
748
- export { AddElementCommand, type ArrowElement, ArrowTool, type ArrowToolOptions, AutoSave, type AutoSaveOptions, Background, type BackgroundOptions, type BackgroundPattern, BatchCommand, type Binding, type Bounds, Camera, type CameraOptions, type CanvasElement, type CanvasState, type Command, ElementRenderer, ElementStore, type ElementType, type ElementUpdateEvent, EraserTool, type EraserToolOptions, EventBus, HandTool, HistoryRecorder, HistoryStack, type HistoryStackOptions, type HtmlElement, type ImageElement, ImageTool, type ImageToolOptions, InputHandler, NoteEditor, type NoteElement, NoteTool, type NoteToolOptions, PencilTool, type PencilToolOptions, type Point, type PointerState, RemoveElementCommand, SelectTool, type ShapeElement, type ShapeKind, ShapeTool, type ShapeToolOptions, type Size, type StrokeElement, type StrokePoint, type TextElement, TextTool, type TextToolOptions, type Tool, type ToolContext, ToolManager, type ToolName, UpdateElementCommand, VERSION, Viewport, type ViewportOptions, clearStaleBindings, createArrow, createHtmlElement, createId, createImage, createNote, createShape, createStroke, createText, exportState, findBindTarget, findBoundArrows, getArrowBounds, getArrowControlPoint, getArrowMidpoint, getArrowTangentAngle, getBendFromPoint, getEdgeIntersection, getElementBounds, getElementCenter, isBindable, isNearBezier, parseState, snapPoint, unbindArrow, updateBoundArrow };
829
+ export { AddElementCommand, type ArrowElement, ArrowTool, type ArrowToolOptions, AutoSave, type AutoSaveOptions, Background, type BackgroundOptions, type BackgroundPattern, BatchCommand, type Binding, type Bounds, Camera, type CameraOptions, type CanvasElement, type CanvasState, type Command, CreateLayerCommand, ElementRenderer, ElementStore, type ElementType, type ElementUpdateEvent, EraserTool, type EraserToolOptions, EventBus, HandTool, HistoryRecorder, HistoryStack, type HistoryStackOptions, type HtmlElement, type ImageElement, ImageTool, type ImageToolOptions, InputHandler, type Layer, LayerManager, NoteEditor, type NoteElement, NoteTool, type NoteToolOptions, PencilTool, type PencilToolOptions, type Point, type PointerState, RemoveElementCommand, RemoveLayerCommand, SelectTool, type ShapeElement, type ShapeKind, ShapeTool, type ShapeToolOptions, type Size, type StrokeElement, type StrokePoint, type TextElement, TextTool, type TextToolOptions, type Tool, type ToolContext, ToolManager, type ToolName, UpdateElementCommand, UpdateLayerCommand, VERSION, Viewport, type ViewportOptions, clearStaleBindings, createArrow, createHtmlElement, createId, createImage, createNote, createShape, createStroke, createText, exportState, findBindTarget, findBoundArrows, getArrowBounds, getArrowControlPoint, getArrowMidpoint, getArrowTangentAngle, getBendFromPoint, getEdgeIntersection, getElementBounds, getElementCenter, isBindable, isNearBezier, parseState, snapPoint, unbindArrow, updateBoundArrow };
package/dist/index.d.ts CHANGED
@@ -30,6 +30,7 @@ interface BaseElement {
30
30
  position: Point;
31
31
  zIndex: number;
32
32
  locked: boolean;
33
+ layerId: string;
33
34
  }
34
35
  interface StrokeElement extends BaseElement {
35
36
  type: 'stroke';
@@ -87,6 +88,15 @@ interface ShapeElement extends BaseElement {
87
88
  type CanvasElement = StrokeElement | NoteElement | ArrowElement | ImageElement | HtmlElement | TextElement | ShapeElement;
88
89
  type ElementType = CanvasElement['type'];
89
90
 
91
+ interface Layer {
92
+ id: string;
93
+ name: string;
94
+ visible: boolean;
95
+ locked: boolean;
96
+ order: number;
97
+ opacity: number;
98
+ }
99
+
90
100
  interface CanvasState {
91
101
  version: number;
92
102
  camera: {
@@ -94,11 +104,12 @@ interface CanvasState {
94
104
  zoom: number;
95
105
  };
96
106
  elements: CanvasElement[];
107
+ layers?: Layer[];
97
108
  }
98
109
  declare function exportState(elements: CanvasElement[], camera: {
99
110
  position: Point;
100
111
  zoom: number;
101
- }): CanvasState;
112
+ }, layers?: Layer[]): CanvasState;
102
113
  declare function parseState(json: string): CanvasState;
103
114
 
104
115
  declare function snapPoint(point: Point, gridSize: number): Point;
@@ -116,7 +127,9 @@ interface ElementStoreEvents {
116
127
  declare class ElementStore {
117
128
  private elements;
118
129
  private bus;
130
+ private layerOrderMap;
119
131
  get count(): number;
132
+ setLayerOrder(order: Map<string, number>): void;
120
133
  getAll(): CanvasElement[];
121
134
  getById(id: string): CanvasElement | undefined;
122
135
  getElementsByType<T extends ElementType>(type: T): Extract<CanvasElement, {
@@ -156,15 +169,47 @@ declare class Camera {
156
169
  private notifyChange;
157
170
  }
158
171
 
172
+ declare class LayerManager {
173
+ private readonly store;
174
+ private layers;
175
+ private _activeLayerId;
176
+ private bus;
177
+ constructor(store: ElementStore);
178
+ get activeLayer(): Layer;
179
+ get activeLayerId(): string;
180
+ getLayers(): Layer[];
181
+ getLayer(id: string): Layer | undefined;
182
+ isLayerVisible(id: string): boolean;
183
+ isLayerLocked(id: string): boolean;
184
+ createLayer(name?: string): Layer;
185
+ removeLayer(id: string): void;
186
+ renameLayer(id: string, name: string): void;
187
+ reorderLayer(id: string, newOrder: number): void;
188
+ setLayerVisible(id: string, visible: boolean): boolean;
189
+ setLayerLocked(id: string, locked: boolean): boolean;
190
+ setActiveLayer(id: string): void;
191
+ moveElementToLayer(elementId: string, layerId: string): void;
192
+ snapshot(): Layer[];
193
+ loadSnapshot(layers: Layer[]): void;
194
+ on(event: 'change', callback: () => void): () => void;
195
+ addLayerDirect(layer: Layer): void;
196
+ removeLayerDirect(id: string): void;
197
+ updateLayerDirect(id: string, props: Omit<Partial<Layer>, 'id'>): void;
198
+ private syncLayerOrder;
199
+ private findFallbackLayer;
200
+ }
201
+
159
202
  interface AutoSaveOptions {
160
203
  key?: string;
161
204
  debounceMs?: number;
205
+ layerManager?: LayerManager;
162
206
  }
163
207
  declare class AutoSave {
164
208
  private readonly store;
165
209
  private readonly camera;
166
210
  private readonly key;
167
211
  private readonly debounceMs;
212
+ private readonly layerManager?;
168
213
  private timerId;
169
214
  private unsubscribers;
170
215
  constructor(store: ElementStore, camera: Camera, options?: AutoSaveOptions);
@@ -207,6 +252,9 @@ interface ToolContext {
207
252
  setCursor?: (cursor: string) => void;
208
253
  snapToGrid?: boolean;
209
254
  gridSize?: number;
255
+ activeLayerId?: string;
256
+ isLayerVisible?: (layerId: string) => boolean;
257
+ isLayerLocked?: (layerId: string) => boolean;
210
258
  }
211
259
  interface PointerState {
212
260
  x: number;
@@ -342,6 +390,7 @@ declare class Viewport {
342
390
  private readonly container;
343
391
  readonly camera: Camera;
344
392
  readonly store: ElementStore;
393
+ readonly layerManager: LayerManager;
345
394
  readonly toolManager: ToolManager;
346
395
  readonly history: HistoryStack;
347
396
  readonly domLayer: HTMLDivElement;
@@ -405,6 +454,7 @@ declare class Viewport {
405
454
  private syncDomNode;
406
455
  private renderDomContent;
407
456
  private unbindArrowsFrom;
457
+ private hideDomNode;
408
458
  private removeDomNode;
409
459
  private clearDomNodes;
410
460
  private createWrapper;
@@ -417,7 +467,10 @@ declare class Viewport {
417
467
 
418
468
  declare class ElementRenderer {
419
469
  private store;
470
+ private imageCache;
471
+ private onImageLoad;
420
472
  setStore(store: ElementStore): void;
473
+ setOnImageLoad(callback: () => void): void;
421
474
  isDomElement(element: CanvasElement): boolean;
422
475
  renderCanvasElement(ctx: CanvasRenderingContext2D, element: CanvasElement): void;
423
476
  private renderStroke;
@@ -427,6 +480,8 @@ declare class ElementRenderer {
427
480
  private renderShape;
428
481
  private fillShapePath;
429
482
  private strokeShapePath;
483
+ private renderImage;
484
+ private getImage;
430
485
  }
431
486
 
432
487
  declare class NoteEditor {
@@ -452,6 +507,7 @@ interface BaseDefaults {
452
507
  position?: Point;
453
508
  zIndex?: number;
454
509
  locked?: boolean;
510
+ layerId?: string;
455
511
  }
456
512
  interface StrokeInput extends BaseDefaults {
457
513
  points: StrokePoint[];
@@ -525,7 +581,7 @@ declare function isBindable(element: CanvasElement): boolean;
525
581
  declare function getElementCenter(element: CanvasElement): Point;
526
582
  declare function getElementBounds(element: CanvasElement): Rect | null;
527
583
  declare function getEdgeIntersection(bounds: Rect, outsidePoint: Point): Point;
528
- declare function findBindTarget(point: Point, store: ElementStore, threshold: number, excludeId?: string): CanvasElement | null;
584
+ declare function findBindTarget(point: Point, store: ElementStore, threshold: number, excludeId?: string, filter?: (el: CanvasElement) => boolean): CanvasElement | null;
529
585
  declare function findBoundArrows(elementId: string, store: ElementStore): ArrowElement[];
530
586
  declare function updateBoundArrow(arrow: ArrowElement, store: ElementStore): Partial<ArrowElement> | null;
531
587
  declare function clearStaleBindings(arrow: ArrowElement, store: ElementStore): Partial<ArrowElement> | null;
@@ -657,6 +713,7 @@ declare class ArrowTool implements Tool {
657
713
  private toTarget;
658
714
  constructor(options?: ArrowToolOptions);
659
715
  setOptions(options: ArrowToolOptions): void;
716
+ private layerFilter;
660
717
  onPointerDown(state: PointerState, ctx: ToolContext): void;
661
718
  onPointerMove(state: PointerState, ctx: ToolContext): void;
662
719
  onPointerUp(_state: PointerState, ctx: ToolContext): void;
@@ -743,6 +800,30 @@ declare class ShapeTool implements Tool {
743
800
  private onKeyUp;
744
801
  }
745
802
 
746
- declare const VERSION = "0.5.0";
803
+ declare class CreateLayerCommand implements Command {
804
+ private readonly manager;
805
+ private readonly layer;
806
+ constructor(manager: LayerManager, layer: Layer);
807
+ execute(_store: ElementStore): void;
808
+ undo(_store: ElementStore): void;
809
+ }
810
+ declare class RemoveLayerCommand implements Command {
811
+ private readonly manager;
812
+ private readonly layer;
813
+ constructor(manager: LayerManager, layer: Layer);
814
+ execute(_store: ElementStore): void;
815
+ undo(_store: ElementStore): void;
816
+ }
817
+ declare class UpdateLayerCommand implements Command {
818
+ private readonly manager;
819
+ private readonly layerId;
820
+ private readonly previous;
821
+ private readonly current;
822
+ constructor(manager: LayerManager, layerId: string, previous: Layer, current: Layer);
823
+ execute(_store: ElementStore): void;
824
+ undo(_store: ElementStore): void;
825
+ }
826
+
827
+ declare const VERSION = "0.6.1";
747
828
 
748
- export { AddElementCommand, type ArrowElement, ArrowTool, type ArrowToolOptions, AutoSave, type AutoSaveOptions, Background, type BackgroundOptions, type BackgroundPattern, BatchCommand, type Binding, type Bounds, Camera, type CameraOptions, type CanvasElement, type CanvasState, type Command, ElementRenderer, ElementStore, type ElementType, type ElementUpdateEvent, EraserTool, type EraserToolOptions, EventBus, HandTool, HistoryRecorder, HistoryStack, type HistoryStackOptions, type HtmlElement, type ImageElement, ImageTool, type ImageToolOptions, InputHandler, NoteEditor, type NoteElement, NoteTool, type NoteToolOptions, PencilTool, type PencilToolOptions, type Point, type PointerState, RemoveElementCommand, SelectTool, type ShapeElement, type ShapeKind, ShapeTool, type ShapeToolOptions, type Size, type StrokeElement, type StrokePoint, type TextElement, TextTool, type TextToolOptions, type Tool, type ToolContext, ToolManager, type ToolName, UpdateElementCommand, VERSION, Viewport, type ViewportOptions, clearStaleBindings, createArrow, createHtmlElement, createId, createImage, createNote, createShape, createStroke, createText, exportState, findBindTarget, findBoundArrows, getArrowBounds, getArrowControlPoint, getArrowMidpoint, getArrowTangentAngle, getBendFromPoint, getEdgeIntersection, getElementBounds, getElementCenter, isBindable, isNearBezier, parseState, snapPoint, unbindArrow, updateBoundArrow };
829
+ export { AddElementCommand, type ArrowElement, ArrowTool, type ArrowToolOptions, AutoSave, type AutoSaveOptions, Background, type BackgroundOptions, type BackgroundPattern, BatchCommand, type Binding, type Bounds, Camera, type CameraOptions, type CanvasElement, type CanvasState, type Command, CreateLayerCommand, ElementRenderer, ElementStore, type ElementType, type ElementUpdateEvent, EraserTool, type EraserToolOptions, EventBus, HandTool, HistoryRecorder, HistoryStack, type HistoryStackOptions, type HtmlElement, type ImageElement, ImageTool, type ImageToolOptions, InputHandler, type Layer, LayerManager, NoteEditor, type NoteElement, NoteTool, type NoteToolOptions, PencilTool, type PencilToolOptions, type Point, type PointerState, RemoveElementCommand, RemoveLayerCommand, SelectTool, type ShapeElement, type ShapeKind, ShapeTool, type ShapeToolOptions, type Size, type StrokeElement, type StrokePoint, type TextElement, TextTool, type TextToolOptions, type Tool, type ToolContext, ToolManager, type ToolName, UpdateElementCommand, UpdateLayerCommand, VERSION, Viewport, type ViewportOptions, clearStaleBindings, createArrow, createHtmlElement, createId, createImage, createNote, createShape, createStroke, createText, exportState, findBindTarget, findBoundArrows, getArrowBounds, getArrowControlPoint, getArrowMidpoint, getArrowTangentAngle, getBendFromPoint, getEdgeIntersection, getElementBounds, getElementCenter, isBindable, isNearBezier, parseState, snapPoint, unbindArrow, updateBoundArrow };