@fieldnotes/core 0.1.2 → 0.2.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
@@ -13,6 +13,11 @@ interface Point {
13
13
  x: number;
14
14
  y: number;
15
15
  }
16
+ interface StrokePoint {
17
+ x: number;
18
+ y: number;
19
+ pressure: number;
20
+ }
16
21
  interface Size {
17
22
  w: number;
18
23
  h: number;
@@ -28,7 +33,7 @@ interface BaseElement {
28
33
  }
29
34
  interface StrokeElement extends BaseElement {
30
35
  type: 'stroke';
31
- points: Point[];
36
+ points: StrokePoint[];
32
37
  color: string;
33
38
  width: number;
34
39
  opacity: number;
@@ -73,6 +78,34 @@ declare function exportState(elements: CanvasElement[], camera: {
73
78
  }): CanvasState;
74
79
  declare function parseState(json: string): CanvasState;
75
80
 
81
+ interface ElementUpdateEvent {
82
+ previous: CanvasElement;
83
+ current: CanvasElement;
84
+ }
85
+ interface ElementStoreEvents {
86
+ add: CanvasElement;
87
+ remove: CanvasElement;
88
+ update: ElementUpdateEvent;
89
+ clear: null;
90
+ }
91
+ declare class ElementStore {
92
+ private elements;
93
+ private bus;
94
+ get count(): number;
95
+ getAll(): CanvasElement[];
96
+ getById(id: string): CanvasElement | undefined;
97
+ getElementsByType<T extends ElementType>(type: T): Extract<CanvasElement, {
98
+ type: T;
99
+ }>[];
100
+ add(element: CanvasElement): void;
101
+ update(id: string, partial: Partial<CanvasElement>): void;
102
+ remove(id: string): void;
103
+ clear(): void;
104
+ snapshot(): CanvasElement[];
105
+ loadSnapshot(elements: CanvasElement[]): void;
106
+ on<K extends keyof ElementStoreEvents>(event: K, listener: (data: ElementStoreEvents[K]) => void): () => void;
107
+ }
108
+
76
109
  interface CameraOptions {
77
110
  minZoom?: number;
78
111
  maxZoom?: number;
@@ -98,6 +131,27 @@ declare class Camera {
98
131
  private notifyChange;
99
132
  }
100
133
 
134
+ interface AutoSaveOptions {
135
+ key?: string;
136
+ debounceMs?: number;
137
+ }
138
+ declare class AutoSave {
139
+ private readonly store;
140
+ private readonly camera;
141
+ private readonly key;
142
+ private readonly debounceMs;
143
+ private timerId;
144
+ private unsubscribers;
145
+ constructor(store: ElementStore, camera: Camera, options?: AutoSaveOptions);
146
+ start(): void;
147
+ stop(): void;
148
+ load(): CanvasState | null;
149
+ clear(): void;
150
+ private scheduleSave;
151
+ private cancelPending;
152
+ private save;
153
+ }
154
+
101
155
  type BackgroundPattern = 'dots' | 'grid' | 'none';
102
156
  interface BackgroundOptions {
103
157
  pattern?: BackgroundPattern;
@@ -119,34 +173,6 @@ declare class Background {
119
173
  private renderGrid;
120
174
  }
121
175
 
122
- interface ElementUpdateEvent {
123
- previous: CanvasElement;
124
- current: CanvasElement;
125
- }
126
- interface ElementStoreEvents {
127
- add: CanvasElement;
128
- remove: CanvasElement;
129
- update: ElementUpdateEvent;
130
- clear: null;
131
- }
132
- declare class ElementStore {
133
- private elements;
134
- private bus;
135
- get count(): number;
136
- getAll(): CanvasElement[];
137
- getById(id: string): CanvasElement | undefined;
138
- getElementsByType<T extends ElementType>(type: T): Extract<CanvasElement, {
139
- type: T;
140
- }>[];
141
- add(element: CanvasElement): void;
142
- update(id: string, partial: Partial<CanvasElement>): void;
143
- remove(id: string): void;
144
- clear(): void;
145
- snapshot(): CanvasElement[];
146
- loadSnapshot(elements: CanvasElement[]): void;
147
- on<K extends keyof ElementStoreEvents>(event: K, listener: (data: ElementStoreEvents[K]) => void): () => void;
148
- }
149
-
150
176
  interface ToolContext {
151
177
  camera: Camera;
152
178
  store: ElementStore;
@@ -179,6 +205,7 @@ declare class ToolManager {
179
205
  get activeTool(): Tool | null;
180
206
  get toolNames(): string[];
181
207
  register(tool: Tool): void;
208
+ getTool<T extends Tool = Tool>(name: string): T | undefined;
182
209
  setTool(name: string, ctx: ToolContext): void;
183
210
  handlePointerDown(state: PointerState, ctx: ToolContext): void;
184
211
  handlePointerMove(state: PointerState, ctx: ToolContext): void;
@@ -259,7 +286,6 @@ declare class InputHandler {
259
286
  destroy(): void;
260
287
  private bind;
261
288
  private onWheel;
262
- private isInteractiveHtmlContent;
263
289
  private onPointerDown;
264
290
  private onPointerMove;
265
291
  private onPointerUp;
@@ -307,6 +333,7 @@ declare class Viewport {
307
333
  private needsRender;
308
334
  private domNodes;
309
335
  private htmlContent;
336
+ private interactingElementId;
310
337
  constructor(container: HTMLElement, options?: ViewportOptions);
311
338
  get ctx(): CanvasRenderingContext2D | null;
312
339
  requestRender(): void;
@@ -322,7 +349,7 @@ declare class Viewport {
322
349
  }, size?: {
323
350
  w: number;
324
351
  h: number;
325
- }): void;
352
+ }): string;
326
353
  addHtmlElement(dom: HTMLElement, position: {
327
354
  x: number;
328
355
  y: number;
@@ -335,6 +362,11 @@ declare class Viewport {
335
362
  private render;
336
363
  private startEditingNote;
337
364
  private onDblClick;
365
+ private hitTestWorld;
366
+ private startInteracting;
367
+ stopInteracting(): void;
368
+ private onInteractKeyDown;
369
+ private onInteractPointerDown;
338
370
  private onDragOver;
339
371
  private onDrop;
340
372
  private syncDomNode;
@@ -380,7 +412,7 @@ interface BaseDefaults {
380
412
  locked?: boolean;
381
413
  }
382
414
  interface StrokeInput extends BaseDefaults {
383
- points: Point[];
415
+ points: StrokePoint[];
384
416
  color?: string;
385
417
  width?: number;
386
418
  opacity?: number;
@@ -467,6 +499,7 @@ declare class HandTool implements Tool {
467
499
  interface PencilToolOptions {
468
500
  color?: string;
469
501
  width?: number;
502
+ smoothing?: number;
470
503
  }
471
504
  declare class PencilTool implements Tool {
472
505
  readonly name = "pencil";
@@ -474,6 +507,7 @@ declare class PencilTool implements Tool {
474
507
  private points;
475
508
  private color;
476
509
  private width;
510
+ private smoothing;
477
511
  constructor(options?: PencilToolOptions);
478
512
  onActivate(ctx: ToolContext): void;
479
513
  onDeactivate(ctx: ToolContext): void;
@@ -544,6 +578,7 @@ declare class ArrowTool implements Tool {
544
578
  private color;
545
579
  private width;
546
580
  constructor(options?: ArrowToolOptions);
581
+ setOptions(options: ArrowToolOptions): void;
547
582
  onPointerDown(state: PointerState, ctx: ToolContext): void;
548
583
  onPointerMove(state: PointerState, ctx: ToolContext): void;
549
584
  onPointerUp(_state: PointerState, ctx: ToolContext): void;
@@ -559,6 +594,7 @@ declare class NoteTool implements Tool {
559
594
  private backgroundColor;
560
595
  private size;
561
596
  constructor(options?: NoteToolOptions);
597
+ setOptions(options: NoteToolOptions): void;
562
598
  onPointerDown(_state: PointerState, _ctx: ToolContext): void;
563
599
  onPointerMove(_state: PointerState, _ctx: ToolContext): void;
564
600
  onPointerUp(state: PointerState, ctx: ToolContext): void;
@@ -578,6 +614,6 @@ declare class ImageTool implements Tool {
578
614
  onPointerUp(state: PointerState, ctx: ToolContext): void;
579
615
  }
580
616
 
581
- declare const VERSION = "0.1.2";
617
+ declare const VERSION = "0.2.1";
582
618
 
583
- export { AddElementCommand, type ArrowElement, ArrowTool, type ArrowToolOptions, Background, type BackgroundOptions, type BackgroundPattern, BatchCommand, 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 Size, type StrokeElement, type Tool, type ToolContext, ToolManager, type ToolName, UpdateElementCommand, VERSION, Viewport, type ViewportOptions, createArrow, createHtmlElement, createId, createImage, createNote, createStroke, exportState, getArrowBounds, getArrowControlPoint, getArrowMidpoint, getArrowTangentAngle, getBendFromPoint, isNearBezier, parseState };
619
+ export { AddElementCommand, type ArrowElement, ArrowTool, type ArrowToolOptions, AutoSave, type AutoSaveOptions, Background, type BackgroundOptions, type BackgroundPattern, BatchCommand, 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 Size, type StrokeElement, type StrokePoint, type Tool, type ToolContext, ToolManager, type ToolName, UpdateElementCommand, VERSION, Viewport, type ViewportOptions, createArrow, createHtmlElement, createId, createImage, createNote, createStroke, exportState, getArrowBounds, getArrowControlPoint, getArrowMidpoint, getArrowTangentAngle, getBendFromPoint, isNearBezier, parseState };
package/dist/index.d.ts CHANGED
@@ -13,6 +13,11 @@ interface Point {
13
13
  x: number;
14
14
  y: number;
15
15
  }
16
+ interface StrokePoint {
17
+ x: number;
18
+ y: number;
19
+ pressure: number;
20
+ }
16
21
  interface Size {
17
22
  w: number;
18
23
  h: number;
@@ -28,7 +33,7 @@ interface BaseElement {
28
33
  }
29
34
  interface StrokeElement extends BaseElement {
30
35
  type: 'stroke';
31
- points: Point[];
36
+ points: StrokePoint[];
32
37
  color: string;
33
38
  width: number;
34
39
  opacity: number;
@@ -73,6 +78,34 @@ declare function exportState(elements: CanvasElement[], camera: {
73
78
  }): CanvasState;
74
79
  declare function parseState(json: string): CanvasState;
75
80
 
81
+ interface ElementUpdateEvent {
82
+ previous: CanvasElement;
83
+ current: CanvasElement;
84
+ }
85
+ interface ElementStoreEvents {
86
+ add: CanvasElement;
87
+ remove: CanvasElement;
88
+ update: ElementUpdateEvent;
89
+ clear: null;
90
+ }
91
+ declare class ElementStore {
92
+ private elements;
93
+ private bus;
94
+ get count(): number;
95
+ getAll(): CanvasElement[];
96
+ getById(id: string): CanvasElement | undefined;
97
+ getElementsByType<T extends ElementType>(type: T): Extract<CanvasElement, {
98
+ type: T;
99
+ }>[];
100
+ add(element: CanvasElement): void;
101
+ update(id: string, partial: Partial<CanvasElement>): void;
102
+ remove(id: string): void;
103
+ clear(): void;
104
+ snapshot(): CanvasElement[];
105
+ loadSnapshot(elements: CanvasElement[]): void;
106
+ on<K extends keyof ElementStoreEvents>(event: K, listener: (data: ElementStoreEvents[K]) => void): () => void;
107
+ }
108
+
76
109
  interface CameraOptions {
77
110
  minZoom?: number;
78
111
  maxZoom?: number;
@@ -98,6 +131,27 @@ declare class Camera {
98
131
  private notifyChange;
99
132
  }
100
133
 
134
+ interface AutoSaveOptions {
135
+ key?: string;
136
+ debounceMs?: number;
137
+ }
138
+ declare class AutoSave {
139
+ private readonly store;
140
+ private readonly camera;
141
+ private readonly key;
142
+ private readonly debounceMs;
143
+ private timerId;
144
+ private unsubscribers;
145
+ constructor(store: ElementStore, camera: Camera, options?: AutoSaveOptions);
146
+ start(): void;
147
+ stop(): void;
148
+ load(): CanvasState | null;
149
+ clear(): void;
150
+ private scheduleSave;
151
+ private cancelPending;
152
+ private save;
153
+ }
154
+
101
155
  type BackgroundPattern = 'dots' | 'grid' | 'none';
102
156
  interface BackgroundOptions {
103
157
  pattern?: BackgroundPattern;
@@ -119,34 +173,6 @@ declare class Background {
119
173
  private renderGrid;
120
174
  }
121
175
 
122
- interface ElementUpdateEvent {
123
- previous: CanvasElement;
124
- current: CanvasElement;
125
- }
126
- interface ElementStoreEvents {
127
- add: CanvasElement;
128
- remove: CanvasElement;
129
- update: ElementUpdateEvent;
130
- clear: null;
131
- }
132
- declare class ElementStore {
133
- private elements;
134
- private bus;
135
- get count(): number;
136
- getAll(): CanvasElement[];
137
- getById(id: string): CanvasElement | undefined;
138
- getElementsByType<T extends ElementType>(type: T): Extract<CanvasElement, {
139
- type: T;
140
- }>[];
141
- add(element: CanvasElement): void;
142
- update(id: string, partial: Partial<CanvasElement>): void;
143
- remove(id: string): void;
144
- clear(): void;
145
- snapshot(): CanvasElement[];
146
- loadSnapshot(elements: CanvasElement[]): void;
147
- on<K extends keyof ElementStoreEvents>(event: K, listener: (data: ElementStoreEvents[K]) => void): () => void;
148
- }
149
-
150
176
  interface ToolContext {
151
177
  camera: Camera;
152
178
  store: ElementStore;
@@ -179,6 +205,7 @@ declare class ToolManager {
179
205
  get activeTool(): Tool | null;
180
206
  get toolNames(): string[];
181
207
  register(tool: Tool): void;
208
+ getTool<T extends Tool = Tool>(name: string): T | undefined;
182
209
  setTool(name: string, ctx: ToolContext): void;
183
210
  handlePointerDown(state: PointerState, ctx: ToolContext): void;
184
211
  handlePointerMove(state: PointerState, ctx: ToolContext): void;
@@ -259,7 +286,6 @@ declare class InputHandler {
259
286
  destroy(): void;
260
287
  private bind;
261
288
  private onWheel;
262
- private isInteractiveHtmlContent;
263
289
  private onPointerDown;
264
290
  private onPointerMove;
265
291
  private onPointerUp;
@@ -307,6 +333,7 @@ declare class Viewport {
307
333
  private needsRender;
308
334
  private domNodes;
309
335
  private htmlContent;
336
+ private interactingElementId;
310
337
  constructor(container: HTMLElement, options?: ViewportOptions);
311
338
  get ctx(): CanvasRenderingContext2D | null;
312
339
  requestRender(): void;
@@ -322,7 +349,7 @@ declare class Viewport {
322
349
  }, size?: {
323
350
  w: number;
324
351
  h: number;
325
- }): void;
352
+ }): string;
326
353
  addHtmlElement(dom: HTMLElement, position: {
327
354
  x: number;
328
355
  y: number;
@@ -335,6 +362,11 @@ declare class Viewport {
335
362
  private render;
336
363
  private startEditingNote;
337
364
  private onDblClick;
365
+ private hitTestWorld;
366
+ private startInteracting;
367
+ stopInteracting(): void;
368
+ private onInteractKeyDown;
369
+ private onInteractPointerDown;
338
370
  private onDragOver;
339
371
  private onDrop;
340
372
  private syncDomNode;
@@ -380,7 +412,7 @@ interface BaseDefaults {
380
412
  locked?: boolean;
381
413
  }
382
414
  interface StrokeInput extends BaseDefaults {
383
- points: Point[];
415
+ points: StrokePoint[];
384
416
  color?: string;
385
417
  width?: number;
386
418
  opacity?: number;
@@ -467,6 +499,7 @@ declare class HandTool implements Tool {
467
499
  interface PencilToolOptions {
468
500
  color?: string;
469
501
  width?: number;
502
+ smoothing?: number;
470
503
  }
471
504
  declare class PencilTool implements Tool {
472
505
  readonly name = "pencil";
@@ -474,6 +507,7 @@ declare class PencilTool implements Tool {
474
507
  private points;
475
508
  private color;
476
509
  private width;
510
+ private smoothing;
477
511
  constructor(options?: PencilToolOptions);
478
512
  onActivate(ctx: ToolContext): void;
479
513
  onDeactivate(ctx: ToolContext): void;
@@ -544,6 +578,7 @@ declare class ArrowTool implements Tool {
544
578
  private color;
545
579
  private width;
546
580
  constructor(options?: ArrowToolOptions);
581
+ setOptions(options: ArrowToolOptions): void;
547
582
  onPointerDown(state: PointerState, ctx: ToolContext): void;
548
583
  onPointerMove(state: PointerState, ctx: ToolContext): void;
549
584
  onPointerUp(_state: PointerState, ctx: ToolContext): void;
@@ -559,6 +594,7 @@ declare class NoteTool implements Tool {
559
594
  private backgroundColor;
560
595
  private size;
561
596
  constructor(options?: NoteToolOptions);
597
+ setOptions(options: NoteToolOptions): void;
562
598
  onPointerDown(_state: PointerState, _ctx: ToolContext): void;
563
599
  onPointerMove(_state: PointerState, _ctx: ToolContext): void;
564
600
  onPointerUp(state: PointerState, ctx: ToolContext): void;
@@ -578,6 +614,6 @@ declare class ImageTool implements Tool {
578
614
  onPointerUp(state: PointerState, ctx: ToolContext): void;
579
615
  }
580
616
 
581
- declare const VERSION = "0.1.2";
617
+ declare const VERSION = "0.2.1";
582
618
 
583
- export { AddElementCommand, type ArrowElement, ArrowTool, type ArrowToolOptions, Background, type BackgroundOptions, type BackgroundPattern, BatchCommand, 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 Size, type StrokeElement, type Tool, type ToolContext, ToolManager, type ToolName, UpdateElementCommand, VERSION, Viewport, type ViewportOptions, createArrow, createHtmlElement, createId, createImage, createNote, createStroke, exportState, getArrowBounds, getArrowControlPoint, getArrowMidpoint, getArrowTangentAngle, getBendFromPoint, isNearBezier, parseState };
619
+ export { AddElementCommand, type ArrowElement, ArrowTool, type ArrowToolOptions, AutoSave, type AutoSaveOptions, Background, type BackgroundOptions, type BackgroundPattern, BatchCommand, 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 Size, type StrokeElement, type StrokePoint, type Tool, type ToolContext, ToolManager, type ToolName, UpdateElementCommand, VERSION, Viewport, type ViewportOptions, createArrow, createHtmlElement, createId, createImage, createNote, createStroke, exportState, getArrowBounds, getArrowControlPoint, getArrowMidpoint, getArrowTangentAngle, getBendFromPoint, isNearBezier, parseState };