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 +3 -0
- package/dist/src/EditorImage.d.ts +2 -2
- package/dist/src/UndoRedoHistory.d.ts +2 -0
- package/dist/src/Viewport.d.ts +1 -1
- package/dist/src/htmlUtil.d.ts +1 -0
- package/dist/src/tools/BaseTool.d.ts +4 -4
- package/dist/src/tools/ToolController.d.ts +2 -1
- package/dist/src/tools/UndoRedoShortcut.d.ts +10 -0
- package/dist/src/tools/localization.d.ts +1 -0
- package/dist/src/types.d.ts +1 -0
- package/package.json +1 -1
- package/src/Editor.ts +1 -1
- package/src/EditorImage.test.ts +1 -4
- package/src/UndoRedoHistory.ts +8 -0
- package/src/testing/createEditor.ts +4 -0
- package/src/tools/BaseTool.ts +5 -4
- package/src/tools/ToolController.ts +3 -0
- package/src/tools/UndoRedoShortcut.test.ts +53 -0
- package/src/tools/UndoRedoShortcut.ts +28 -0
- package/src/tools/localization.ts +2 -0
- package/src/types.ts +1 -0
package/CHANGELOG.md
CHANGED
@@ -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 "__#
|
19
|
-
"__#
|
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;
|
package/dist/src/Viewport.d.ts
CHANGED
@@ -10,7 +10,7 @@ export declare class Viewport {
|
|
10
10
|
private notifier;
|
11
11
|
static ViewportTransform: {
|
12
12
|
new (transform: Mat33): {
|
13
|
-
readonly "__#
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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;
|
@@ -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
|
+
}
|
package/dist/src/types.d.ts
CHANGED
package/package.json
CHANGED
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/
|
package/src/EditorImage.test.ts
CHANGED
@@ -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([
|
package/src/UndoRedoHistory.ts
CHANGED
@@ -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;
|
package/src/tools/BaseTool.ts
CHANGED
@@ -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
|
10
|
-
public
|
11
|
-
public
|
12
|
-
public
|
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