js-draw 0.0.7 → 0.0.10

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 (46) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/bundle.js +1 -1
  3. package/dist/src/Display.d.ts +1 -0
  4. package/dist/src/Display.js +3 -0
  5. package/dist/src/Editor.js +3 -1
  6. package/dist/src/UndoRedoHistory.d.ts +2 -0
  7. package/dist/src/UndoRedoHistory.js +6 -0
  8. package/dist/src/rendering/AbstractRenderer.d.ts +1 -0
  9. package/dist/src/rendering/AbstractRenderer.js +1 -0
  10. package/dist/src/rendering/CanvasRenderer.d.ts +4 -0
  11. package/dist/src/rendering/CanvasRenderer.js +22 -6
  12. package/dist/src/testing/createEditor.d.ts +3 -0
  13. package/dist/src/testing/createEditor.js +3 -0
  14. package/dist/src/toolbar/HTMLToolbar.js +10 -1
  15. package/dist/src/toolbar/localization.d.ts +1 -0
  16. package/dist/src/toolbar/localization.js +1 -0
  17. package/dist/src/tools/BaseTool.d.ts +4 -4
  18. package/dist/src/tools/BaseTool.js +4 -0
  19. package/dist/src/tools/PanZoom.js +3 -0
  20. package/dist/src/tools/SelectionTool.d.ts +3 -0
  21. package/dist/src/tools/SelectionTool.js +13 -0
  22. package/dist/src/tools/ToolController.d.ts +2 -1
  23. package/dist/src/tools/ToolController.js +3 -0
  24. package/dist/src/tools/UndoRedoShortcut.d.ts +10 -0
  25. package/dist/src/tools/UndoRedoShortcut.js +23 -0
  26. package/dist/src/tools/localization.d.ts +1 -0
  27. package/dist/src/tools/localization.js +1 -0
  28. package/dist/src/types.d.ts +1 -0
  29. package/package.json +2 -2
  30. package/src/Display.ts +4 -0
  31. package/src/Editor.ts +3 -1
  32. package/src/EditorImage.test.ts +1 -4
  33. package/src/UndoRedoHistory.ts +8 -0
  34. package/src/rendering/AbstractRenderer.ts +2 -0
  35. package/src/rendering/CanvasRenderer.ts +35 -6
  36. package/src/testing/createEditor.ts +4 -0
  37. package/src/toolbar/HTMLToolbar.ts +12 -1
  38. package/src/toolbar/localization.ts +2 -0
  39. package/src/tools/BaseTool.ts +5 -4
  40. package/src/tools/PanZoom.ts +3 -0
  41. package/src/tools/SelectionTool.ts +16 -0
  42. package/src/tools/ToolController.ts +3 -0
  43. package/src/tools/UndoRedoShortcut.test.ts +53 -0
  44. package/src/tools/UndoRedoShortcut.ts +28 -0
  45. package/src/tools/localization.ts +2 -0
  46. package/src/types.ts +1 -0
@@ -14,6 +14,7 @@ export interface ToolbarLocalization {
14
14
  touchDrawing: string;
15
15
  thicknessLabel: string;
16
16
  resizeImageToSelection: string;
17
+ deleteSelection: string;
17
18
  undo: string;
18
19
  redo: string;
19
20
 
@@ -29,6 +30,7 @@ export const defaultToolbarLocalization: ToolbarLocalization = {
29
30
  thicknessLabel: 'Thickness: ',
30
31
  colorLabel: 'Color: ',
31
32
  resizeImageToSelection: 'Resize image to selection',
33
+ deleteSelection: 'Delete selection',
32
34
  undo: 'Undo',
33
35
  redo: 'Redo',
34
36
  selectObjectType: 'Object type: ',
@@ -6,10 +6,11 @@ export default abstract class BaseTool implements PointerEvtListener {
6
6
  private enabled: boolean = true;
7
7
  private group: ToolEnabledGroup|null = null;
8
8
 
9
- public abstract onPointerDown(event: PointerEvt): boolean;
10
- public abstract onPointerMove(event: PointerEvt): void;
11
- public abstract onPointerUp(event: PointerEvt): void;
12
- public abstract onGestureCancel(): void;
9
+ public onPointerDown(_event: PointerEvt): boolean { return false; }
10
+ public onPointerMove(_event: PointerEvt) { }
11
+ public onPointerUp(_event: PointerEvt) { }
12
+ public onGestureCancel() { }
13
+
13
14
  public abstract readonly kind: ToolType;
14
15
 
15
16
  protected constructor(private notifier: EditorNotifier, public readonly description: string) {
@@ -78,6 +78,7 @@ export default class PanZoom extends BaseTool {
78
78
 
79
79
  if (handlingGesture) {
80
80
  this.transform ??= new Viewport.ViewportTransform(Mat33.identity);
81
+ this.editor.display.setDraftMode(true);
81
82
  }
82
83
 
83
84
  return handlingGesture;
@@ -136,11 +137,13 @@ export default class PanZoom extends BaseTool {
136
137
  this.editor.dispatch(this.transform, false);
137
138
  }
138
139
 
140
+ this.editor.display.setDraftMode(false);
139
141
  this.transform = null;
140
142
  }
141
143
 
142
144
  public onGestureCancel(): void {
143
145
  this.transform?.unapply(this.editor);
146
+ this.editor.display.setDraftMode(false);
144
147
  this.transform = null;
145
148
  }
146
149
 
@@ -1,4 +1,5 @@
1
1
  import Command from '../commands/Command';
2
+ import Erase from '../commands/Erase';
2
3
  import AbstractComponent from '../components/AbstractComponent';
3
4
  import Editor from '../Editor';
4
5
  import Mat33 from '../geometry/Mat33';
@@ -421,6 +422,10 @@ class Selection {
421
422
  this.backgroundBox.style.transform = `rotate(${rotationDeg}deg)`;
422
423
  this.rotateCircle.style.transform = `rotate(${-rotationDeg}deg)`;
423
424
  }
425
+
426
+ public deleteSelectedObjects(): Command {
427
+ return new Erase(this.selectedElems);
428
+ }
424
429
  }
425
430
 
426
431
  export default class SelectionTool extends BaseTool {
@@ -542,4 +547,15 @@ export default class SelectionTool extends BaseTool {
542
547
  public getSelection(): Selection|null {
543
548
  return this.selectionBox;
544
549
  }
550
+
551
+ public clearSelection() {
552
+ this.handleOverlay.replaceChildren();
553
+ this.prevSelectionBox = this.selectionBox;
554
+ this.selectionBox = null;
555
+
556
+ this.editor.notifier.dispatch(EditorEventType.ToolUpdated, {
557
+ kind: EditorEventType.ToolUpdated,
558
+ tool: this,
559
+ });
560
+ }
545
561
  }
@@ -8,6 +8,7 @@ import Eraser from './Eraser';
8
8
  import SelectionTool from './SelectionTool';
9
9
  import Color4 from '../Color4';
10
10
  import { ToolLocalization } from './localization';
11
+ import UndoRedoShortcut from './UndoRedoShortcut';
11
12
 
12
13
  export enum ToolType {
13
14
  TouchPanZoom,
@@ -15,6 +16,7 @@ export enum ToolType {
15
16
  Selection,
16
17
  Eraser,
17
18
  PanZoom,
19
+ UndoRedoShortcut,
18
20
  }
19
21
 
20
22
  export default class ToolController {
@@ -40,6 +42,7 @@ export default class ToolController {
40
42
  touchPanZoom,
41
43
  ...primaryTools,
42
44
  new PanZoom(editor, PanZoomMode.TwoFingerGestures | PanZoomMode.AnyDevice, localization.twoFingerPanZoomTool),
45
+ new UndoRedoShortcut(editor),
43
46
  ];
44
47
  primaryTools.forEach(tool => tool.setToolGroup(primaryToolEnabledGroup));
45
48
  touchPanZoom.setEnabled(false);
@@ -0,0 +1,53 @@
1
+ /* @jest-environment jsdom */
2
+
3
+ import Color4 from '../Color4';
4
+ import Stroke from '../components/Stroke';
5
+ import EditorImage from '../EditorImage';
6
+ import Path from '../geometry/Path';
7
+ import createEditor from '../testing/createEditor';
8
+ import { InputEvtType } from '../types';
9
+
10
+ describe('UndoRedoShortcut', () => {
11
+ const testStroke = new Stroke([Path.fromString('M0,0L10,10').toRenderable({ fill: Color4.red })]);
12
+ const addTestStrokeCommand = new EditorImage.AddElementCommand(testStroke);
13
+
14
+ it('ctrl+z should undo', () => {
15
+ const editor = createEditor();
16
+ editor.dispatch(addTestStrokeCommand);
17
+ expect(editor.history.undoStackSize).toBe(1);
18
+
19
+ editor.toolController.dispatchInputEvent({
20
+ kind: InputEvtType.KeyPressEvent,
21
+ ctrlKey: true,
22
+ key: 'z',
23
+ });
24
+
25
+ expect(editor.history.undoStackSize).toBe(0);
26
+ expect(editor.history.redoStackSize).toBe(1);
27
+ });
28
+
29
+ it('ctrl+shift+Z should re-do', () => {
30
+ const editor = createEditor();
31
+ editor.dispatch(addTestStrokeCommand);
32
+ editor.dispatch(addTestStrokeCommand);
33
+ expect(editor.history.undoStackSize).toBe(2);
34
+
35
+ editor.toolController.dispatchInputEvent({
36
+ kind: InputEvtType.KeyPressEvent,
37
+ ctrlKey: true,
38
+ key: 'z',
39
+ });
40
+
41
+ expect(editor.history.undoStackSize).toBe(1);
42
+ expect(editor.history.redoStackSize).toBe(1);
43
+
44
+ editor.toolController.dispatchInputEvent({
45
+ kind: InputEvtType.KeyPressEvent,
46
+ ctrlKey: true,
47
+ key: 'Z',
48
+ });
49
+
50
+ expect(editor.history.undoStackSize).toBe(2);
51
+ expect(editor.history.redoStackSize).toBe(0);
52
+ });
53
+ });
@@ -0,0 +1,28 @@
1
+ import Editor from '../Editor';
2
+ import { KeyPressEvent } from '../types';
3
+ import BaseTool from './BaseTool';
4
+ import { ToolType } from './ToolController';
5
+
6
+
7
+ export default class UndoRedoShortcut extends BaseTool {
8
+ public kind: ToolType.UndoRedoShortcut = ToolType.UndoRedoShortcut;
9
+
10
+ public constructor(private editor: Editor) {
11
+ super(editor.notifier, editor.localization.undoRedoTool);
12
+ }
13
+
14
+ // Activate undo/redo
15
+ public onKeyPress({ key, ctrlKey }: KeyPressEvent): boolean {
16
+ if (ctrlKey) {
17
+ if (key === 'z') {
18
+ this.editor.history.undo();
19
+ return true;
20
+ } else if (key === 'Z') {
21
+ this.editor.history.redo();
22
+ return true;
23
+ }
24
+ }
25
+
26
+ return false;
27
+ }
28
+ }
@@ -5,6 +5,7 @@ export interface ToolLocalization {
5
5
  eraserTool: string;
6
6
  touchPanTool: string;
7
7
  twoFingerPanZoomTool: string;
8
+ undoRedoTool: string;
8
9
 
9
10
  toolEnabledAnnouncement: (toolName: string) => string;
10
11
  toolDisabledAnnouncement: (toolName: string) => string;
@@ -16,6 +17,7 @@ export const defaultToolLocalization: ToolLocalization = {
16
17
  eraserTool: 'Eraser',
17
18
  touchPanTool: 'Touch Panning',
18
19
  twoFingerPanZoomTool: 'Panning and Zooming',
20
+ undoRedoTool: 'Undo/Redo',
19
21
 
20
22
  toolEnabledAnnouncement: (toolName) => `${toolName} enabled`,
21
23
  toolDisabledAnnouncement: (toolName) => `${toolName} disabled`,
package/src/types.ts CHANGED
@@ -46,6 +46,7 @@ export interface WheelEvt {
46
46
  export interface KeyPressEvent {
47
47
  readonly kind: InputEvtType.KeyPressEvent;
48
48
  readonly key: string;
49
+ readonly ctrlKey: boolean;
49
50
  }
50
51
 
51
52
  // Event triggered when pointer capture is taken by a different [PointerEvtListener].