js-draw 0.12.0 → 0.13.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.
Files changed (73) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/bundle.js +1 -1
  3. package/dist/src/Color4.d.ts +12 -0
  4. package/dist/src/Color4.js +16 -0
  5. package/dist/src/Editor.d.ts +33 -18
  6. package/dist/src/Editor.js +22 -19
  7. package/dist/src/EditorImage.d.ts +12 -0
  8. package/dist/src/EditorImage.js +12 -0
  9. package/dist/src/Pointer.d.ts +1 -0
  10. package/dist/src/Pointer.js +8 -0
  11. package/dist/src/SVGLoader.d.ts +5 -0
  12. package/dist/src/SVGLoader.js +6 -1
  13. package/dist/src/Viewport.d.ts +30 -1
  14. package/dist/src/Viewport.js +39 -9
  15. package/dist/src/commands/invertCommand.js +1 -1
  16. package/dist/src/components/AbstractComponent.d.ts +19 -0
  17. package/dist/src/components/AbstractComponent.js +17 -2
  18. package/dist/src/lib.d.ts +6 -3
  19. package/dist/src/lib.js +4 -1
  20. package/dist/src/math/Mat33.d.ts +1 -1
  21. package/dist/src/math/Mat33.js +1 -1
  22. package/dist/src/rendering/Display.d.ts +9 -11
  23. package/dist/src/rendering/Display.js +12 -14
  24. package/dist/src/rendering/lib.d.ts +3 -0
  25. package/dist/src/rendering/lib.js +3 -0
  26. package/dist/src/rendering/renderers/DummyRenderer.js +2 -2
  27. package/dist/src/rendering/renderers/SVGRenderer.js +4 -0
  28. package/dist/src/toolbar/IconProvider.d.ts +1 -1
  29. package/dist/src/toolbar/IconProvider.js +90 -29
  30. package/dist/src/toolbar/makeColorInput.js +8 -1
  31. package/dist/src/tools/PanZoom.d.ts +3 -1
  32. package/dist/src/tools/PanZoom.js +31 -6
  33. package/dist/src/tools/PasteHandler.d.ts +11 -4
  34. package/dist/src/tools/PasteHandler.js +12 -5
  35. package/dist/src/tools/Pen.d.ts +7 -2
  36. package/dist/src/tools/Pen.js +39 -6
  37. package/dist/src/tools/SelectionTool/SelectionHandle.d.ts +3 -0
  38. package/dist/src/tools/SelectionTool/SelectionHandle.js +6 -0
  39. package/dist/src/tools/SelectionTool/SelectionTool.d.ts +3 -1
  40. package/dist/src/tools/SelectionTool/SelectionTool.js +53 -15
  41. package/dist/src/tools/ToolSwitcherShortcut.d.ts +8 -0
  42. package/dist/src/tools/ToolSwitcherShortcut.js +9 -3
  43. package/dist/src/tools/UndoRedoShortcut.js +2 -4
  44. package/package.json +2 -2
  45. package/src/Color4.test.ts +11 -0
  46. package/src/Color4.ts +22 -0
  47. package/src/Editor.ts +36 -22
  48. package/src/EditorImage.ts +12 -0
  49. package/src/Pointer.ts +19 -0
  50. package/src/SVGLoader.ts +6 -1
  51. package/src/Viewport.ts +50 -11
  52. package/src/commands/invertCommand.ts +1 -1
  53. package/src/components/AbstractComponent.ts +33 -2
  54. package/src/lib.ts +6 -3
  55. package/src/math/Mat33.ts +1 -1
  56. package/src/rendering/Display.ts +12 -15
  57. package/src/rendering/RenderingStyle.ts +1 -1
  58. package/src/rendering/lib.ts +4 -0
  59. package/src/rendering/renderers/DummyRenderer.ts +2 -3
  60. package/src/rendering/renderers/SVGRenderer.ts +4 -0
  61. package/src/rendering/renderers/TextOnlyRenderer.ts +0 -1
  62. package/src/toolbar/HTMLToolbar.ts +1 -1
  63. package/src/toolbar/IconProvider.ts +98 -31
  64. package/src/toolbar/makeColorInput.ts +9 -1
  65. package/src/tools/PanZoom.ts +37 -7
  66. package/src/tools/PasteHandler.ts +12 -6
  67. package/src/tools/Pen.test.ts +44 -1
  68. package/src/tools/Pen.ts +53 -8
  69. package/src/tools/SelectionTool/SelectionHandle.ts +9 -0
  70. package/src/tools/SelectionTool/SelectionTool.ts +67 -15
  71. package/src/tools/ToolSwitcherShortcut.ts +10 -5
  72. package/src/tools/UndoRedoShortcut.ts +2 -5
  73. package/typedoc.json +2 -2
@@ -1,7 +1,3 @@
1
- // Allows users to select/transform portions of the `EditorImage`.
2
- // With respect to `extend`ing, `SelectionTool` is not stable.
3
- // @packageDocumentation
4
-
5
1
  import AbstractComponent from '../../components/AbstractComponent';
6
2
  import Editor from '../../Editor';
7
3
  import Mat33 from '../../math/Mat33';
@@ -16,7 +12,8 @@ import TextComponent from '../../components/TextComponent';
16
12
 
17
13
  export const cssPrefix = 'selection-tool-';
18
14
 
19
- // {@inheritDoc SelectionTool!}
15
+ // Allows users to select/transform portions of the `EditorImage`.
16
+ // With respect to `extend`ing, `SelectionTool` is not stable.
20
17
  export default class SelectionTool extends BaseTool {
21
18
  private handleOverlay: HTMLElement;
22
19
  private prevSelectionBox: Selection|null;
@@ -25,6 +22,7 @@ export default class SelectionTool extends BaseTool {
25
22
 
26
23
  private expandingSelectionBox: boolean = false;
27
24
  private shiftKeyPressed: boolean = false;
25
+ private ctrlKeyPressed: boolean = false;
28
26
 
29
27
  public constructor(private editor: Editor, description: string) {
30
28
  super(editor.notifier, description);
@@ -61,17 +59,47 @@ export default class SelectionTool extends BaseTool {
61
59
  this.selectionBox.addTo(this.handleOverlay);
62
60
  }
63
61
 
62
+ private snapSelectionToGrid() {
63
+ if (!this.selectionBox) throw new Error('No selection to snap!');
64
+
65
+ const topLeftOfBBox = this.selectionBox.region.topLeft;
66
+ const snapDistance =
67
+ this.editor.viewport.snapToGrid(topLeftOfBBox).minus(topLeftOfBBox);
68
+
69
+ const oldTransform = this.selectionBox.getTransform();
70
+ this.selectionBox.setTransform(oldTransform.rightMul(Mat33.translation(snapDistance)));
71
+ this.selectionBox.finalizeTransform();
72
+ }
73
+
64
74
  private selectionBoxHandlingEvt: boolean = false;
65
- public onPointerDown(event: PointerEvt): boolean {
66
- if (event.allPointers.length === 1 && event.current.isPrimary) {
67
- if (this.lastEvtTarget && this.selectionBox?.onDragStart(event.current, this.lastEvtTarget)) {
68
- this.selectionBoxHandlingEvt = true;
69
- this.expandingSelectionBox = false;
75
+ public onPointerDown({ allPointers, current }: PointerEvt): boolean {
76
+ const snapToGrid = this.ctrlKeyPressed;
77
+ if (snapToGrid) {
78
+ current = current.snappedToGrid(this.editor.viewport);
79
+ }
80
+
81
+ if (allPointers.length === 1 && current.isPrimary) {
82
+ let transforming = false;
83
+
84
+ if (this.lastEvtTarget && this.selectionBox) {
85
+ if (snapToGrid) {
86
+ this.snapSelectionToGrid();
87
+ }
88
+
89
+ const dragStartResult = this.selectionBox.onDragStart(current, this.lastEvtTarget);
90
+
91
+ if (dragStartResult) {
92
+ transforming = true;
93
+
94
+ this.selectionBoxHandlingEvt = true;
95
+ this.expandingSelectionBox = false;
96
+ }
70
97
  }
71
- else {
98
+
99
+ if (!transforming) {
72
100
  // Shift key: Combine the new and old selection boxes at the end of the gesture.
73
101
  this.expandingSelectionBox = this.shiftKeyPressed;
74
- this.makeSelectionBox(event.current.canvasPos);
102
+ this.makeSelectionBox(current.canvasPos);
75
103
  }
76
104
 
77
105
  return true;
@@ -82,10 +110,15 @@ export default class SelectionTool extends BaseTool {
82
110
  public onPointerMove(event: PointerEvt): void {
83
111
  if (!this.selectionBox) return;
84
112
 
113
+ let currentPointer = event.current;
114
+ if (this.ctrlKeyPressed) {
115
+ currentPointer = currentPointer.snappedToGrid(this.editor.viewport);
116
+ }
117
+
85
118
  if (this.selectionBoxHandlingEvt) {
86
- this.selectionBox.onDragUpdate(event.current);
119
+ this.selectionBox.onDragUpdate(currentPointer);
87
120
  } else {
88
- this.selectionBox!.setToPoint(event.current.canvasPos);
121
+ this.selectionBox!.setToPoint(currentPointer.canvasPos);
89
122
  }
90
123
  }
91
124
 
@@ -137,7 +170,12 @@ export default class SelectionTool extends BaseTool {
137
170
  public onPointerUp(event: PointerEvt): void {
138
171
  if (!this.selectionBox) return;
139
172
 
140
- this.selectionBox.setToPoint(event.current.canvasPos);
173
+ let currentPointer = event.current;
174
+ if (this.ctrlKeyPressed) {
175
+ currentPointer = currentPointer.snappedToGrid(this.editor.viewport);
176
+ }
177
+
178
+ this.selectionBox.setToPoint(currentPointer.canvasPos);
141
179
 
142
180
  // Were we expanding the previous selection?
143
181
  if (this.expandingSelectionBox && this.prevSelectionBox) {
@@ -173,8 +211,14 @@ export default class SelectionTool extends BaseTool {
173
211
  'e', 'j', 'ArrowDown',
174
212
  'r', 'R',
175
213
  'i', 'I', 'o', 'O',
214
+ 'Control',
176
215
  ];
177
216
  public onKeyPress(event: KeyPressEvent): boolean {
217
+ if (event.key === 'Control') {
218
+ this.ctrlKeyPressed = true;
219
+ return true;
220
+ }
221
+
178
222
  if (this.selectionBox && event.ctrlKey && event.key === 'd') {
179
223
  // Handle duplication on key up — we don't want to accidentally duplicate
180
224
  // many times.
@@ -290,6 +334,11 @@ export default class SelectionTool extends BaseTool {
290
334
  }
291
335
 
292
336
  public onKeyUp(evt: KeyUpEvent) {
337
+ if (evt.key === 'Control') {
338
+ this.ctrlKeyPressed = false;
339
+ return true;
340
+ }
341
+
293
342
  if (evt.key === 'Shift') {
294
343
  this.shiftKeyPressed = false;
295
344
  return true;
@@ -358,6 +407,9 @@ export default class SelectionTool extends BaseTool {
358
407
  this.handleOverlay.replaceChildren();
359
408
  this.selectionBox = null;
360
409
 
410
+ this.shiftKeyPressed = false;
411
+ this.ctrlKeyPressed = false;
412
+
361
413
  this.handleOverlay.style.display = enabled ? 'block' : 'none';
362
414
 
363
415
  if (enabled) {
@@ -1,16 +1,21 @@
1
- // Handles ctrl+1, ctrl+2, ctrl+3, ..., shortcuts for switching tools.
2
- // @packageDocumentation
3
-
4
1
  import Editor from '../Editor';
5
2
  import { KeyPressEvent } from '../types';
6
3
  import BaseTool from './BaseTool';
7
4
 
8
- // {@inheritDoc ToolSwitcherShortcut!}
5
+ /**
6
+ * Handles keyboard events used, by default, to select tools. By default,
7
+ * 1 maps to the first primary tool, 2 to the second primary tool, ... .
8
+ *
9
+ * This is in the default set of {@link ToolController} tools.
10
+ *
11
+ * @deprecated This may be replaced in the future.
12
+ */
9
13
  export default class ToolSwitcherShortcut extends BaseTool {
10
14
  public constructor(private editor: Editor) {
11
15
  super(editor.notifier, editor.localization.changeTool);
12
16
  }
13
-
17
+
18
+ // @internal
14
19
  public onKeyPress({ key }: KeyPressEvent): boolean {
15
20
  const toolController = this.editor.toolController;
16
21
  const primaryTools = toolController.getPrimaryTools();
@@ -1,17 +1,14 @@
1
- // Handles ctrl+Z, ctrl+Shift+Z keyboard shortcuts.
2
- // @packageDocumentation
3
-
4
1
  import Editor from '../Editor';
5
2
  import { KeyPressEvent } from '../types';
6
3
  import BaseTool from './BaseTool';
7
4
 
8
- // {@inheritDoc UndoRedoShortcut!}
5
+ // Handles ctrl+Z, ctrl+Shift+Z keyboard shortcuts.
9
6
  export default class UndoRedoShortcut extends BaseTool {
10
7
  public constructor(private editor: Editor) {
11
8
  super(editor.notifier, editor.localization.undoRedoTool);
12
9
  }
13
10
 
14
- // Activate undo/redo
11
+ // @internal
15
12
  public onKeyPress({ key, ctrlKey }: KeyPressEvent): boolean {
16
13
  if (ctrlKey) {
17
14
  if (key === 'z') {
package/typedoc.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "entryPoints": [
3
- "./src/"
3
+ "./src/lib.ts", "./src/bundle/bundled.ts"
4
4
  ],
5
5
  "exclude": [
6
6
  "**/*.test.ts",
@@ -21,4 +21,4 @@
21
21
  "invalidLink": true,
22
22
  "notDocumented": false
23
23
  }
24
- }
24
+ }