js-draw 0.0.7 → 0.0.8

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/CHANGELOG.md CHANGED
@@ -1,4 +1,7 @@
1
1
 
2
+ # 0.0.8
3
+ * Map `ctrl+z` to undo, `ctrl+shift+Z` to redo.
4
+
2
5
  # 0.0.7
3
6
  * Preserve SVG global attributes when loading/saving images.
4
7
  * This fixes a bug where lost information (e.g. a missing namespace) broke SVGs on export.
@@ -15,8 +15,8 @@ export default class EditorImage {
15
15
  getElementsIntersectingRegion(region: Rect2): AbstractComponent[];
16
16
  static AddElementCommand: {
17
17
  new (element: AbstractComponent, applyByFlattening?: boolean): {
18
- readonly "__#679@#element": AbstractComponent;
19
- "__#679@#applyByFlattening": boolean;
18
+ readonly "__#2@#element": AbstractComponent;
19
+ "__#2@#applyByFlattening": boolean;
20
20
  apply(editor: Editor): void;
21
21
  unapply(editor: Editor): void;
22
22
  description(localization: EditorLocalization): string;
@@ -13,5 +13,7 @@ declare class UndoRedoHistory {
13
13
  push(command: Command, apply?: boolean): void;
14
14
  undo(): void;
15
15
  redo(): void;
16
+ get undoStackSize(): number;
17
+ get redoStackSize(): number;
16
18
  }
17
19
  export default UndoRedoHistory;
@@ -10,7 +10,7 @@ export declare class Viewport {
10
10
  private notifier;
11
11
  static ViewportTransform: {
12
12
  new (transform: Mat33): {
13
- readonly "__#678@#inverseTransform": Mat33;
13
+ readonly "__#1@#inverseTransform": Mat33;
14
14
  readonly transform: Mat33;
15
15
  apply(editor: Editor): void;
16
16
  unapply(editor: Editor): void;
@@ -0,0 +1 @@
1
+ export declare const escapeHtml: (html: string) => string;
@@ -6,10 +6,10 @@ export default abstract class BaseTool implements PointerEvtListener {
6
6
  readonly description: string;
7
7
  private enabled;
8
8
  private group;
9
- abstract onPointerDown(event: PointerEvt): boolean;
10
- abstract onPointerMove(event: PointerEvt): void;
11
- abstract onPointerUp(event: PointerEvt): void;
12
- abstract onGestureCancel(): void;
9
+ onPointerDown(_event: PointerEvt): boolean;
10
+ onPointerMove(_event: PointerEvt): void;
11
+ onPointerUp(_event: PointerEvt): void;
12
+ onGestureCancel(): void;
13
13
  abstract readonly kind: ToolType;
14
14
  protected constructor(notifier: EditorNotifier, description: string);
15
15
  onWheel(_event: WheelEvt): boolean;
@@ -7,7 +7,8 @@ export declare enum ToolType {
7
7
  Pen = 1,
8
8
  Selection = 2,
9
9
  Eraser = 3,
10
- PanZoom = 4
10
+ PanZoom = 4,
11
+ UndoRedoShortcut = 5
11
12
  }
12
13
  export default class ToolController {
13
14
  private tools;
@@ -0,0 +1,10 @@
1
+ import Editor from '../Editor';
2
+ import { KeyPressEvent } from '../types';
3
+ import BaseTool from './BaseTool';
4
+ import { ToolType } from './ToolController';
5
+ export default class UndoRedoShortcut extends BaseTool {
6
+ private editor;
7
+ kind: ToolType.UndoRedoShortcut;
8
+ constructor(editor: Editor);
9
+ onKeyPress({ key, ctrlKey }: KeyPressEvent): boolean;
10
+ }
@@ -4,6 +4,7 @@ export interface ToolLocalization {
4
4
  eraserTool: string;
5
5
  touchPanTool: string;
6
6
  twoFingerPanZoomTool: string;
7
+ undoRedoTool: string;
7
8
  toolEnabledAnnouncement: (toolName: string) => string;
8
9
  toolDisabledAnnouncement: (toolName: string) => string;
9
10
  }
@@ -29,6 +29,7 @@ export interface WheelEvt {
29
29
  export interface KeyPressEvent {
30
30
  readonly kind: InputEvtType.KeyPressEvent;
31
31
  readonly key: string;
32
+ readonly ctrlKey: boolean;
32
33
  }
33
34
  export interface GestureCancelEvt {
34
35
  readonly kind: InputEvtType.GestureCancelEvt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-draw",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "Draw pictures using a pen, touchscreen, or mouse! JS-draw is a drawing library for JavaScript and TypeScript. ",
5
5
  "main": "dist/src/Editor.js",
6
6
  "types": "dist/src/Editor.d.ts",
package/src/Editor.ts CHANGED
@@ -238,6 +238,7 @@ export class Editor {
238
238
  if (this.toolController.dispatchInputEvent({
239
239
  kind: InputEvtType.KeyPressEvent,
240
240
  key: evt.key,
241
+ ctrlKey: evt.ctrlKey,
241
242
  })) {
242
243
  evt.preventDefault();
243
244
  }
@@ -447,7 +448,6 @@ export class Editor {
447
448
  result.setAttribute('viewBox', `${rect.x} ${rect.y} ${rect.w} ${rect.h}`);
448
449
  result.setAttribute('width', `${rect.w}`);
449
450
  result.setAttribute('height', `${rect.h}`);
450
- console.log('res', result);
451
451
 
452
452
  // Ensure the image can be identified as an SVG if downloaded.
453
453
  // See https://jwatt.org/svg/authoring/
@@ -5,12 +5,9 @@ import Stroke from './components/Stroke';
5
5
  import { Vec2 } from './geometry/Vec2';
6
6
  import Path, { PathCommandType } from './geometry/Path';
7
7
  import Color4 from './Color4';
8
- import Editor from './Editor';
9
- import { RenderingMode } from './Display';
10
8
  import DummyRenderer from './rendering/DummyRenderer';
11
9
  import { RenderingStyle } from './rendering/AbstractRenderer';
12
-
13
- const createEditor = () => new Editor(document.body, { renderingMode: RenderingMode.DummyRenderer });
10
+ import createEditor from './testing/createEditor';
14
11
 
15
12
  describe('EditorImage', () => {
16
13
  const testStroke = new Stroke([
@@ -56,6 +56,14 @@ class UndoRedoHistory {
56
56
  }
57
57
  this.fireUpdateEvent();
58
58
  }
59
+
60
+ public get undoStackSize(): number {
61
+ return this.undoStack.length;
62
+ }
63
+
64
+ public get redoStackSize(): number {
65
+ return this.redoStack.length;
66
+ }
59
67
  }
60
68
 
61
69
  export default UndoRedoHistory;
@@ -0,0 +1,4 @@
1
+ import { RenderingMode } from '../Display';
2
+ import Editor from '../Editor';
3
+
4
+ export default () => new Editor(document.body, { renderingMode: RenderingMode.DummyRenderer });
@@ -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) {
@@ -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].