js-draw 1.0.1 → 1.1.0
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/LICENSE +21 -0
- package/dist/Editor.css +1 -0
- package/dist/bundle.js +1 -1
- package/dist/bundledStyles.js +1 -1
- package/dist/cjs/toolbar/AbstractToolbar.d.ts +9 -13
- package/dist/cjs/toolbar/AbstractToolbar.js +14 -19
- package/dist/cjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
- package/dist/cjs/toolbar/widgets/SaveActionWidget.js +26 -0
- package/dist/cjs/toolbar/widgets/keybindings.d.ts +1 -0
- package/dist/cjs/toolbar/widgets/keybindings.js +4 -1
- package/dist/cjs/version.js +1 -1
- package/dist/mjs/toolbar/AbstractToolbar.d.ts +9 -13
- package/dist/mjs/toolbar/AbstractToolbar.mjs +14 -19
- package/dist/mjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
- package/dist/mjs/toolbar/widgets/SaveActionWidget.mjs +21 -0
- package/dist/mjs/toolbar/widgets/keybindings.d.ts +1 -0
- package/dist/mjs/toolbar/widgets/keybindings.mjs +3 -0
- package/dist/mjs/version.mjs +1 -1
- package/docs/img/readme-images/js-draw.jpg +0 -0
- package/docs/img/readme-images/unsupported-elements--in-editor.png +0 -0
- package/package.json +5 -4
- package/src/toolbar/EdgeToolbar.scss +1 -0
- package/dist-test/test_imports/package-lock.json +0 -13
- package/dist-test/test_imports/package.json +0 -12
- package/dist-test/test_imports/test-imports.js +0 -11
- package/dist-test/test_imports/test-require.cjs +0 -14
- package/src/Editor.loadFrom.test.ts +0 -24
- package/src/Editor.test.ts +0 -107
- package/src/Editor.toSVG.test.ts +0 -294
- package/src/Editor.ts +0 -1443
- package/src/EditorImage.test.ts +0 -117
- package/src/EditorImage.ts +0 -609
- package/src/EventDispatcher.test.ts +0 -123
- package/src/EventDispatcher.ts +0 -72
- package/src/Pointer.ts +0 -183
- package/src/SVGLoader.test.ts +0 -114
- package/src/SVGLoader.ts +0 -672
- package/src/UndoRedoHistory.test.ts +0 -34
- package/src/UndoRedoHistory.ts +0 -102
- package/src/Viewport.ts +0 -322
- package/src/bundle/bundled.ts +0 -7
- package/src/commands/Command.ts +0 -45
- package/src/commands/Duplicate.ts +0 -75
- package/src/commands/Erase.ts +0 -95
- package/src/commands/SerializableCommand.ts +0 -49
- package/src/commands/UnresolvedCommand.ts +0 -37
- package/src/commands/invertCommand.ts +0 -58
- package/src/commands/lib.ts +0 -16
- package/src/commands/localization.ts +0 -47
- package/src/commands/uniteCommands.test.ts +0 -23
- package/src/commands/uniteCommands.ts +0 -140
- package/src/components/AbstractComponent.transformBy.test.ts +0 -23
- package/src/components/AbstractComponent.ts +0 -383
- package/src/components/BackgroundComponent.test.ts +0 -44
- package/src/components/BackgroundComponent.ts +0 -348
- package/src/components/ImageComponent.ts +0 -176
- package/src/components/RestylableComponent.ts +0 -161
- package/src/components/SVGGlobalAttributesObject.ts +0 -79
- package/src/components/Stroke.test.ts +0 -137
- package/src/components/Stroke.ts +0 -294
- package/src/components/TextComponent.test.ts +0 -202
- package/src/components/TextComponent.ts +0 -429
- package/src/components/UnknownSVGObject.test.ts +0 -10
- package/src/components/UnknownSVGObject.ts +0 -60
- package/src/components/builders/ArrowBuilder.ts +0 -106
- package/src/components/builders/CircleBuilder.ts +0 -100
- package/src/components/builders/FreehandLineBuilder.test.ts +0 -24
- package/src/components/builders/FreehandLineBuilder.ts +0 -210
- package/src/components/builders/LineBuilder.ts +0 -77
- package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -453
- package/src/components/builders/RectangleBuilder.ts +0 -73
- package/src/components/builders/types.ts +0 -15
- package/src/components/lib.ts +0 -31
- package/src/components/localization.ts +0 -24
- package/src/components/util/StrokeSmoother.ts +0 -302
- package/src/components/util/describeComponentList.ts +0 -18
- package/src/dialogs/makeAboutDialog.ts +0 -82
- package/src/inputEvents.ts +0 -143
- package/src/lib.ts +0 -91
- package/src/localization.ts +0 -34
- package/src/localizations/de.ts +0 -146
- package/src/localizations/en.ts +0 -8
- package/src/localizations/es.ts +0 -74
- package/src/localizations/getLocalizationTable.test.ts +0 -27
- package/src/localizations/getLocalizationTable.ts +0 -74
- package/src/rendering/Display.ts +0 -247
- package/src/rendering/RenderablePathSpec.ts +0 -88
- package/src/rendering/RenderingStyle.test.ts +0 -68
- package/src/rendering/RenderingStyle.ts +0 -55
- package/src/rendering/TextRenderingStyle.ts +0 -55
- package/src/rendering/caching/CacheRecord.test.ts +0 -48
- package/src/rendering/caching/CacheRecord.ts +0 -76
- package/src/rendering/caching/CacheRecordManager.ts +0 -71
- package/src/rendering/caching/RenderingCache.test.ts +0 -43
- package/src/rendering/caching/RenderingCache.ts +0 -66
- package/src/rendering/caching/RenderingCacheNode.ts +0 -404
- package/src/rendering/caching/testUtils.ts +0 -35
- package/src/rendering/caching/types.ts +0 -34
- package/src/rendering/lib.ts +0 -8
- package/src/rendering/localization.ts +0 -20
- package/src/rendering/renderers/AbstractRenderer.ts +0 -232
- package/src/rendering/renderers/CanvasRenderer.ts +0 -312
- package/src/rendering/renderers/DummyRenderer.test.ts +0 -41
- package/src/rendering/renderers/DummyRenderer.ts +0 -142
- package/src/rendering/renderers/SVGRenderer.ts +0 -434
- package/src/rendering/renderers/TextOnlyRenderer.test.ts +0 -34
- package/src/rendering/renderers/TextOnlyRenderer.ts +0 -68
- package/src/shortcuts/KeyBinding.test.ts +0 -61
- package/src/shortcuts/KeyBinding.ts +0 -257
- package/src/shortcuts/KeyboardShortcutManager.test.ts +0 -95
- package/src/shortcuts/KeyboardShortcutManager.ts +0 -163
- package/src/shortcuts/lib.ts +0 -3
- package/src/testing/createEditor.ts +0 -11
- package/src/testing/getUniquePointerId.ts +0 -18
- package/src/testing/lib.ts +0 -3
- package/src/testing/sendPenEvent.ts +0 -36
- package/src/testing/sendTouchEvent.ts +0 -71
- package/src/toolbar/AbstractToolbar.ts +0 -542
- package/src/toolbar/DropdownToolbar.ts +0 -220
- package/src/toolbar/EdgeToolbar.test.ts +0 -54
- package/src/toolbar/EdgeToolbar.ts +0 -543
- package/src/toolbar/IconProvider.ts +0 -861
- package/src/toolbar/constants.ts +0 -1
- package/src/toolbar/lib.ts +0 -6
- package/src/toolbar/localization.ts +0 -136
- package/src/toolbar/types.ts +0 -13
- package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
- package/src/toolbar/widgets/BaseToolWidget.ts +0 -81
- package/src/toolbar/widgets/BaseWidget.ts +0 -495
- package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -250
- package/src/toolbar/widgets/EraserToolWidget.ts +0 -84
- package/src/toolbar/widgets/HandToolWidget.ts +0 -239
- package/src/toolbar/widgets/InsertImageWidget.ts +0 -248
- package/src/toolbar/widgets/OverflowWidget.ts +0 -92
- package/src/toolbar/widgets/PenToolWidget.ts +0 -369
- package/src/toolbar/widgets/SelectionToolWidget.ts +0 -195
- package/src/toolbar/widgets/TextToolWidget.ts +0 -149
- package/src/toolbar/widgets/components/makeColorInput.ts +0 -184
- package/src/toolbar/widgets/components/makeFileInput.ts +0 -128
- package/src/toolbar/widgets/components/makeGridSelector.ts +0 -179
- package/src/toolbar/widgets/components/makeSeparator.ts +0 -17
- package/src/toolbar/widgets/components/makeThicknessSlider.ts +0 -62
- package/src/toolbar/widgets/keybindings.ts +0 -19
- package/src/toolbar/widgets/layout/DropdownLayoutManager.ts +0 -262
- package/src/toolbar/widgets/layout/EdgeToolbarLayoutManager.ts +0 -71
- package/src/toolbar/widgets/layout/types.ts +0 -74
- package/src/toolbar/widgets/lib.ts +0 -13
- package/src/tools/BaseTool.ts +0 -169
- package/src/tools/Eraser.test.ts +0 -103
- package/src/tools/Eraser.ts +0 -173
- package/src/tools/FindTool.test.ts +0 -67
- package/src/tools/FindTool.ts +0 -153
- package/src/tools/InputFilter/FunctionMapper.ts +0 -17
- package/src/tools/InputFilter/InputMapper.ts +0 -41
- package/src/tools/InputFilter/InputPipeline.test.ts +0 -41
- package/src/tools/InputFilter/InputPipeline.ts +0 -34
- package/src/tools/InputFilter/InputStabilizer.ts +0 -254
- package/src/tools/InputFilter/StrokeKeyboardControl.ts +0 -104
- package/src/tools/PanZoom.test.ts +0 -339
- package/src/tools/PanZoom.ts +0 -525
- package/src/tools/PasteHandler.ts +0 -94
- package/src/tools/Pen.test.ts +0 -260
- package/src/tools/Pen.ts +0 -284
- package/src/tools/PipetteTool.ts +0 -84
- package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -29
- package/src/tools/SelectionTool/Selection.ts +0 -647
- package/src/tools/SelectionTool/SelectionHandle.ts +0 -142
- package/src/tools/SelectionTool/SelectionTool.test.ts +0 -370
- package/src/tools/SelectionTool/SelectionTool.ts +0 -510
- package/src/tools/SelectionTool/TransformMode.ts +0 -112
- package/src/tools/SelectionTool/types.ts +0 -11
- package/src/tools/SoundUITool.ts +0 -221
- package/src/tools/TextTool.ts +0 -339
- package/src/tools/ToolController.ts +0 -224
- package/src/tools/ToolEnabledGroup.ts +0 -14
- package/src/tools/ToolSwitcherShortcut.ts +0 -39
- package/src/tools/ToolbarShortcutHandler.ts +0 -39
- package/src/tools/UndoRedoShortcut.test.ts +0 -62
- package/src/tools/UndoRedoShortcut.ts +0 -24
- package/src/tools/keybindings.ts +0 -85
- package/src/tools/lib.ts +0 -22
- package/src/tools/localization.ts +0 -76
- package/src/types.ts +0 -151
- package/src/util/ReactiveValue.test.ts +0 -168
- package/src/util/ReactiveValue.ts +0 -241
- package/src/util/assertions.ts +0 -55
- package/src/util/fileToBase64.ts +0 -18
- package/src/util/guessKeyCodeFromKey.ts +0 -36
- package/src/util/listPrefixMatch.ts +0 -19
- package/src/util/stopPropagationOfScrollingWheelEvents.ts +0 -20
- package/src/util/untilNextAnimationFrame.ts +0 -9
- package/src/util/waitForAll.ts +0 -18
- package/src/util/waitForTimeout.ts +0 -9
- package/src/version.test.ts +0 -12
- package/src/version.ts +0 -3
- package/tools/allLocales.js +0 -4
- package/tools/copyREADME.ts +0 -62
package/src/UndoRedoHistory.ts
DELETED
@@ -1,102 +0,0 @@
|
|
1
|
-
import Editor from './Editor';
|
2
|
-
import Command from './commands/Command';
|
3
|
-
import { EditorEventType, UndoEventType } from './types';
|
4
|
-
|
5
|
-
type AnnounceRedoCallback = (command: Command)=>void;
|
6
|
-
type AnnounceUndoCallback = (command: Command)=>void;
|
7
|
-
|
8
|
-
class UndoRedoHistory {
|
9
|
-
#undoStack: Command[];
|
10
|
-
#redoStack: Command[];
|
11
|
-
|
12
|
-
private readonly maxUndoRedoStackSize: number = 700;
|
13
|
-
|
14
|
-
// @internal
|
15
|
-
public constructor(
|
16
|
-
private readonly editor: Editor,
|
17
|
-
private announceRedoCallback: AnnounceRedoCallback,
|
18
|
-
private announceUndoCallback: AnnounceUndoCallback,
|
19
|
-
) {
|
20
|
-
this.#undoStack = [];
|
21
|
-
this.#redoStack = [];
|
22
|
-
}
|
23
|
-
|
24
|
-
private fireUpdateEvent(
|
25
|
-
stackUpdateType: UndoEventType, triggeringCommand: Command
|
26
|
-
) {
|
27
|
-
this.editor.notifier.dispatch(EditorEventType.UndoRedoStackUpdated, {
|
28
|
-
kind: EditorEventType.UndoRedoStackUpdated,
|
29
|
-
undoStackSize: this.#undoStack.length,
|
30
|
-
redoStackSize: this.#redoStack.length,
|
31
|
-
|
32
|
-
command: triggeringCommand,
|
33
|
-
stackUpdateType,
|
34
|
-
});
|
35
|
-
}
|
36
|
-
|
37
|
-
// Adds the given command to this and applies it to the editor.
|
38
|
-
public push(command: Command, apply: boolean = true) {
|
39
|
-
if (apply) {
|
40
|
-
command.apply(this.editor);
|
41
|
-
}
|
42
|
-
this.#undoStack.push(command);
|
43
|
-
|
44
|
-
for (const elem of this.#redoStack) {
|
45
|
-
elem.onDrop(this.editor);
|
46
|
-
}
|
47
|
-
this.#redoStack = [];
|
48
|
-
|
49
|
-
if (this.#undoStack.length > this.maxUndoRedoStackSize) {
|
50
|
-
const removeAtOnceCount = Math.ceil(this.maxUndoRedoStackSize / 100);
|
51
|
-
const removedElements = this.#undoStack.splice(0, removeAtOnceCount);
|
52
|
-
removedElements.forEach(elem => elem.onDrop(this.editor));
|
53
|
-
}
|
54
|
-
|
55
|
-
this.fireUpdateEvent(UndoEventType.CommandDone, command);
|
56
|
-
this.editor.notifier.dispatch(EditorEventType.CommandDone, {
|
57
|
-
kind: EditorEventType.CommandDone,
|
58
|
-
command,
|
59
|
-
});
|
60
|
-
}
|
61
|
-
|
62
|
-
// Remove the last command from this' undo stack and apply it.
|
63
|
-
public undo() {
|
64
|
-
const command = this.#undoStack.pop();
|
65
|
-
if (command) {
|
66
|
-
this.#redoStack.push(command);
|
67
|
-
command.unapply(this.editor);
|
68
|
-
this.announceUndoCallback(command);
|
69
|
-
|
70
|
-
this.fireUpdateEvent(UndoEventType.CommandUndone, command);
|
71
|
-
this.editor.notifier.dispatch(EditorEventType.CommandUndone, {
|
72
|
-
kind: EditorEventType.CommandUndone,
|
73
|
-
command,
|
74
|
-
});
|
75
|
-
}
|
76
|
-
}
|
77
|
-
|
78
|
-
public redo() {
|
79
|
-
const command = this.#redoStack.pop();
|
80
|
-
if (command) {
|
81
|
-
this.#undoStack.push(command);
|
82
|
-
command.apply(this.editor);
|
83
|
-
this.announceRedoCallback(command);
|
84
|
-
|
85
|
-
this.fireUpdateEvent(UndoEventType.CommandRedone, command);
|
86
|
-
this.editor.notifier.dispatch(EditorEventType.CommandDone, {
|
87
|
-
kind: EditorEventType.CommandDone,
|
88
|
-
command,
|
89
|
-
});
|
90
|
-
}
|
91
|
-
}
|
92
|
-
|
93
|
-
public get undoStackSize(): number {
|
94
|
-
return this.#undoStack.length;
|
95
|
-
}
|
96
|
-
|
97
|
-
public get redoStackSize(): number {
|
98
|
-
return this.#redoStack.length;
|
99
|
-
}
|
100
|
-
}
|
101
|
-
|
102
|
-
export default UndoRedoHistory;
|
package/src/Viewport.ts
DELETED
@@ -1,322 +0,0 @@
|
|
1
|
-
import Command from './commands/Command';
|
2
|
-
import { CommandLocalization } from './commands/localization';
|
3
|
-
import Editor from './Editor';
|
4
|
-
import { Mat33, Rect2, Point2, Vec2, Vec3 } from '@js-draw/math';
|
5
|
-
import { StrokeDataPoint } from './types';
|
6
|
-
|
7
|
-
// Returns the base type of some type of point/number
|
8
|
-
type PointDataType<T extends Point2|StrokeDataPoint|number> = T extends Point2 ? Point2 : number;
|
9
|
-
|
10
|
-
export abstract class ViewportTransform extends Command {
|
11
|
-
public abstract readonly transform: Mat33;
|
12
|
-
}
|
13
|
-
|
14
|
-
type TransformChangeCallback = (oldTransform: Mat33, newTransform: Mat33)=> void;
|
15
|
-
|
16
|
-
export class Viewport {
|
17
|
-
// Command that translates/scales the viewport.
|
18
|
-
private static ViewportTransform = class extends ViewportTransform {
|
19
|
-
readonly #inverseTransform: Mat33;
|
20
|
-
|
21
|
-
public constructor(public readonly transform: Mat33) {
|
22
|
-
super();
|
23
|
-
this.#inverseTransform = transform.inverse();
|
24
|
-
}
|
25
|
-
|
26
|
-
public apply(editor: Editor) {
|
27
|
-
const viewport = editor.viewport;
|
28
|
-
viewport.resetTransform(viewport.transform.rightMul(this.transform));
|
29
|
-
editor.queueRerender();
|
30
|
-
}
|
31
|
-
|
32
|
-
public unapply(editor: Editor) {
|
33
|
-
const viewport = editor.viewport;
|
34
|
-
viewport.resetTransform(viewport.transform.rightMul(this.#inverseTransform));
|
35
|
-
editor.queueRerender();
|
36
|
-
}
|
37
|
-
|
38
|
-
public description(editor: Editor, localizationTable: CommandLocalization): string {
|
39
|
-
const result: string[] = [];
|
40
|
-
|
41
|
-
// Describe the transformation's affect on the viewport (note that transformation transforms
|
42
|
-
// the **elements** within the viewport). Assumes the transformation only does rotation/scale/translation.
|
43
|
-
const origVec = editor.viewport.visibleRect.center;
|
44
|
-
const linearTransformedVec = this.transform.transformVec3(Vec2.unitX);
|
45
|
-
const affineTransformedVec = this.transform.transformVec2(origVec);
|
46
|
-
|
47
|
-
const scale = linearTransformedVec.magnitude();
|
48
|
-
const rotation = 180 / Math.PI * linearTransformedVec.angle();
|
49
|
-
const translation = affineTransformedVec.minus(origVec);
|
50
|
-
|
51
|
-
if (scale > 1.2) {
|
52
|
-
result.push(localizationTable.zoomedIn);
|
53
|
-
} else if (scale < 0.8) {
|
54
|
-
result.push(localizationTable.zoomedOut);
|
55
|
-
}
|
56
|
-
|
57
|
-
if (Math.floor(Math.abs(rotation)) > 0) {
|
58
|
-
result.push(localizationTable.rotatedBy(Math.round(rotation)));
|
59
|
-
}
|
60
|
-
|
61
|
-
const minTranslation = 1e-4;
|
62
|
-
if (translation.x > minTranslation) {
|
63
|
-
result.push(localizationTable.movedLeft);
|
64
|
-
} else if (translation.x < -minTranslation) {
|
65
|
-
result.push(localizationTable.movedRight);
|
66
|
-
}
|
67
|
-
|
68
|
-
if (translation.y < -minTranslation) {
|
69
|
-
result.push(localizationTable.movedDown);
|
70
|
-
} else if (translation.y > minTranslation) {
|
71
|
-
result.push(localizationTable.movedUp);
|
72
|
-
}
|
73
|
-
|
74
|
-
return result.join('; ');
|
75
|
-
}
|
76
|
-
};
|
77
|
-
|
78
|
-
private transform: Mat33;
|
79
|
-
private inverseTransform: Mat33;
|
80
|
-
private screenRect: Rect2;
|
81
|
-
|
82
|
-
// @internal
|
83
|
-
public constructor(private onTransformChangeCallback: TransformChangeCallback) {
|
84
|
-
this.resetTransform(Mat33.identity);
|
85
|
-
this.screenRect = Rect2.empty;
|
86
|
-
}
|
87
|
-
|
88
|
-
/**
|
89
|
-
* @returns a temporary copy of `this` that does not notify when modified. This is
|
90
|
-
* useful when rendering with a temporarily different viewport.
|
91
|
-
*/
|
92
|
-
public getTemporaryClone(): Viewport {
|
93
|
-
const result = new Viewport(() => {});
|
94
|
-
result.transform = this.transform;
|
95
|
-
result.inverseTransform = this.inverseTransform;
|
96
|
-
result.screenRect = this.screenRect;
|
97
|
-
|
98
|
-
return result;
|
99
|
-
}
|
100
|
-
|
101
|
-
// @internal
|
102
|
-
public updateScreenSize(screenSize: Vec2) {
|
103
|
-
this.screenRect = this.screenRect.resizedTo(screenSize);
|
104
|
-
}
|
105
|
-
|
106
|
-
/** Get the screen's visible region transformed into canvas space. */
|
107
|
-
public get visibleRect(): Rect2 {
|
108
|
-
return this.screenRect.transformedBoundingBox(this.inverseTransform);
|
109
|
-
}
|
110
|
-
|
111
|
-
/** @returns the given point, but in canvas coordinates */
|
112
|
-
public screenToCanvas(screenPoint: Point2): Point2 {
|
113
|
-
return this.inverseTransform.transformVec2(screenPoint);
|
114
|
-
}
|
115
|
-
|
116
|
-
/** @returns the given point transformed into screen coordinates. */
|
117
|
-
public canvasToScreen(canvasPoint: Point2): Point2 {
|
118
|
-
return this.transform.transformVec2(canvasPoint);
|
119
|
-
}
|
120
|
-
|
121
|
-
/** @returns a command that transforms the canvas by `transform`. */
|
122
|
-
public static transformBy(transform: Mat33): ViewportTransform {
|
123
|
-
return new Viewport.ViewportTransform(transform);
|
124
|
-
}
|
125
|
-
|
126
|
-
/**
|
127
|
-
* Updates the transformation directly. Using `transformBy` is preferred.
|
128
|
-
* @param newTransform - should map from canvas coordinates to screen coordinates.
|
129
|
-
*/
|
130
|
-
public resetTransform(newTransform: Mat33 = Mat33.identity) {
|
131
|
-
const oldTransform = this.transform;
|
132
|
-
this.transform = newTransform;
|
133
|
-
this.inverseTransform = newTransform.inverse();
|
134
|
-
this.onTransformChangeCallback?.(oldTransform, newTransform);
|
135
|
-
}
|
136
|
-
|
137
|
-
public get screenToCanvasTransform(): Mat33 {
|
138
|
-
return this.inverseTransform;
|
139
|
-
}
|
140
|
-
|
141
|
-
public get canvasToScreenTransform(): Mat33 {
|
142
|
-
return this.transform;
|
143
|
-
}
|
144
|
-
|
145
|
-
/** @returns the size of the visible region in pixels. */
|
146
|
-
public getScreenRectSize(): Vec2 {
|
147
|
-
return this.screenRect.size;
|
148
|
-
}
|
149
|
-
|
150
|
-
/** Alias for `getScreenRectSize`. @deprecated */
|
151
|
-
public getResolution() {
|
152
|
-
return this.getScreenRectSize();
|
153
|
-
}
|
154
|
-
|
155
|
-
/** @returns the amount a vector on the canvas is scaled to become a vector on the screen. */
|
156
|
-
public getScaleFactor(): number {
|
157
|
-
// Use transformVec3 to avoid translating the vector
|
158
|
-
return this.transform.transformVec3(Vec3.unitX).magnitude();
|
159
|
-
}
|
160
|
-
|
161
|
-
/**
|
162
|
-
* @returns `getScaleFactor()` rounded to the nearest power of 10.
|
163
|
-
* For example, if `getScaleFactor()` returns 101, `getScaleFactorToNearestPowerOfTen()`
|
164
|
-
* should return `100` because `100` is the nearest power of 10 to 101.
|
165
|
-
*/
|
166
|
-
public getScaleFactorToNearestPowerOfTen() {
|
167
|
-
return this.getScaleFactorToNearestPowerOf(10);
|
168
|
-
}
|
169
|
-
|
170
|
-
private getScaleFactorToNearestPowerOf(powerOf: number) {
|
171
|
-
const scaleFactor = this.getScaleFactor();
|
172
|
-
return Math.pow(powerOf, Math.round(Math.log(scaleFactor) / Math.log(powerOf)));
|
173
|
-
}
|
174
|
-
|
175
|
-
/** Returns the size of a grid cell (in canvas units) as used by {@link snapToGrid}. */
|
176
|
-
public static getGridSize(scaleFactor: number) {
|
177
|
-
return 50 / scaleFactor;
|
178
|
-
}
|
179
|
-
|
180
|
-
public snapToGrid(canvasPos: Point2) {
|
181
|
-
const scaleFactor = this.getScaleFactorToNearestPowerOf(2);
|
182
|
-
|
183
|
-
const snapCoordinate = (coordinate: number) => {
|
184
|
-
const roundFactor = 1 / Viewport.getGridSize(scaleFactor);
|
185
|
-
const snapped = Math.round(coordinate * roundFactor) / roundFactor;
|
186
|
-
|
187
|
-
return snapped;
|
188
|
-
};
|
189
|
-
|
190
|
-
const snappedCanvasPos = Vec2.of(
|
191
|
-
snapCoordinate(canvasPos.x), snapCoordinate(canvasPos.y)
|
192
|
-
);
|
193
|
-
return snappedCanvasPos;
|
194
|
-
}
|
195
|
-
|
196
|
-
/** Returns the size of one screen pixel in canvas units. */
|
197
|
-
public getSizeOfPixelOnCanvas(): number {
|
198
|
-
return 1/this.getScaleFactor();
|
199
|
-
}
|
200
|
-
|
201
|
-
/**
|
202
|
-
* @returns the angle of the canvas in radians.
|
203
|
-
* This is the angle by which the canvas is rotated relative to the screen.
|
204
|
-
*/
|
205
|
-
public getRotationAngle(): number {
|
206
|
-
return this.transform.transformVec3(Vec3.unitX).angle();
|
207
|
-
}
|
208
|
-
|
209
|
-
/**
|
210
|
-
* Rounds the given `point` to a multiple of 10 such that it is within `tolerance` of
|
211
|
-
* its original location. This is useful for preparing data for base-10 conversion.
|
212
|
-
*/
|
213
|
-
public static roundPoint<T extends Point2|number>(
|
214
|
-
point: T, tolerance: number,
|
215
|
-
): PointDataType<T>;
|
216
|
-
|
217
|
-
// The separate function type definition seems necessary here.
|
218
|
-
// See https://stackoverflow.com/a/58163623/17055750.
|
219
|
-
// eslint-disable-next-line no-dupe-class-members
|
220
|
-
public static roundPoint(
|
221
|
-
point: Point2|number, tolerance: number
|
222
|
-
): Point2|number {
|
223
|
-
const scaleFactor = 10 ** Math.floor(Math.log10(tolerance));
|
224
|
-
const roundComponent = (component: number): number => {
|
225
|
-
return Math.round(component / scaleFactor) * scaleFactor;
|
226
|
-
};
|
227
|
-
|
228
|
-
if (typeof point === 'number') {
|
229
|
-
return roundComponent(point);
|
230
|
-
}
|
231
|
-
|
232
|
-
return point.map(roundComponent);
|
233
|
-
}
|
234
|
-
|
235
|
-
// Round a point with a tolerance of ±1 screen unit.
|
236
|
-
public roundPoint(point: Point2): Point2 {
|
237
|
-
return Viewport.roundPoint(point, 1 / this.getScaleFactor());
|
238
|
-
}
|
239
|
-
|
240
|
-
// `roundAmount`: An integer >= 0, larger numbers cause less rounding. Smaller numbers cause more
|
241
|
-
// (as such `roundAmount = 0` does the most rounding).
|
242
|
-
public static roundScaleRatio(scaleRatio: number, roundAmount: number = 1): number {
|
243
|
-
if (Math.abs(scaleRatio) <= 1e-12) {
|
244
|
-
return 0;
|
245
|
-
}
|
246
|
-
|
247
|
-
// Represent as k 10ⁿ for some n, k ∈ ℤ.
|
248
|
-
const decimalComponent = 10 ** Math.floor(Math.log10(Math.abs(scaleRatio)));
|
249
|
-
const roundAmountFactor = 2 ** roundAmount;
|
250
|
-
scaleRatio = Math.round(scaleRatio / decimalComponent * roundAmountFactor) / roundAmountFactor * decimalComponent;
|
251
|
-
|
252
|
-
return scaleRatio;
|
253
|
-
}
|
254
|
-
|
255
|
-
// Computes and returns an affine transformation that makes `toMakeVisible` visible and roughly centered on the screen.
|
256
|
-
public computeZoomToTransform(toMakeVisible: Rect2, allowZoomIn: boolean = true, allowZoomOut: boolean = true): Mat33 {
|
257
|
-
let transform = Mat33.identity;
|
258
|
-
|
259
|
-
if (toMakeVisible.w === 0 || toMakeVisible.h === 0) {
|
260
|
-
throw new Error(`${toMakeVisible.toString()} rectangle is empty! Cannot zoom to!`);
|
261
|
-
}
|
262
|
-
|
263
|
-
if (isNaN(toMakeVisible.size.magnitude())) {
|
264
|
-
throw new Error(`${toMakeVisible.toString()} rectangle has NaN size! Cannot zoom to!`);
|
265
|
-
}
|
266
|
-
|
267
|
-
// Try to move the selection within the center 4/5ths of the viewport.
|
268
|
-
const recomputeTargetRect = () => {
|
269
|
-
// transform transforms objects on the canvas. As such, we need to invert it
|
270
|
-
// to transform the viewport.
|
271
|
-
const visibleRect = this.visibleRect.transformedBoundingBox(transform.inverse());
|
272
|
-
return visibleRect.transformedBoundingBox(Mat33.scaling2D(4/5, visibleRect.center));
|
273
|
-
};
|
274
|
-
|
275
|
-
let targetRect = recomputeTargetRect();
|
276
|
-
const largerThanTarget = targetRect.w < toMakeVisible.w || targetRect.h < toMakeVisible.h;
|
277
|
-
|
278
|
-
// Ensure that toMakeVisible is at least 1/3rd of the visible region.
|
279
|
-
const muchSmallerThanTarget = toMakeVisible.maxDimension / targetRect.maxDimension < 1/3;
|
280
|
-
|
281
|
-
if ((largerThanTarget && allowZoomOut) || (muchSmallerThanTarget && allowZoomIn)) {
|
282
|
-
const multiplier = Math.max(
|
283
|
-
toMakeVisible.w / targetRect.w, toMakeVisible.h / targetRect.h
|
284
|
-
);
|
285
|
-
const visibleRectTransform = Mat33.scaling2D(multiplier, targetRect.topLeft);
|
286
|
-
const viewportContentTransform = visibleRectTransform.inverse();
|
287
|
-
|
288
|
-
transform = transform.rightMul(viewportContentTransform);
|
289
|
-
}
|
290
|
-
|
291
|
-
targetRect = recomputeTargetRect();
|
292
|
-
|
293
|
-
// Ensure that the center of the region is visible
|
294
|
-
if (!targetRect.containsRect(toMakeVisible)) {
|
295
|
-
// target position - current position
|
296
|
-
const translation = toMakeVisible.center.minus(targetRect.center);
|
297
|
-
const visibleRectTransform = Mat33.translation(translation);
|
298
|
-
const viewportContentTransform = visibleRectTransform.inverse();
|
299
|
-
|
300
|
-
transform = transform.rightMul(viewportContentTransform);
|
301
|
-
}
|
302
|
-
|
303
|
-
if (!transform.invertable()) {
|
304
|
-
console.warn('Unable to zoom to ', toMakeVisible, '! Computed transform', transform, 'is singular.');
|
305
|
-
transform = Mat33.identity;
|
306
|
-
}
|
307
|
-
|
308
|
-
return transform;
|
309
|
-
}
|
310
|
-
|
311
|
-
// Returns a Command that transforms the view such that [rect] is visible, and perhaps
|
312
|
-
// centered in the viewport.
|
313
|
-
// Returns null if no transformation is necessary
|
314
|
-
//
|
315
|
-
// @see {@link computeZoomToTransform}
|
316
|
-
public zoomTo(toMakeVisible: Rect2, allowZoomIn: boolean = true, allowZoomOut: boolean = true): Command {
|
317
|
-
const transform = this.computeZoomToTransform(toMakeVisible, allowZoomIn, allowZoomOut);
|
318
|
-
return new Viewport.ViewportTransform(transform);
|
319
|
-
}
|
320
|
-
}
|
321
|
-
|
322
|
-
export default Viewport;
|
package/src/bundle/bundled.ts
DELETED
package/src/commands/Command.ts
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
import Editor from '../Editor';
|
2
|
-
import { EditorLocalization } from '../localization';
|
3
|
-
|
4
|
-
export abstract class Command {
|
5
|
-
public abstract apply(editor: Editor): Promise<void>|void;
|
6
|
-
public abstract unapply(editor: Editor): Promise<void>|void;
|
7
|
-
|
8
|
-
// Called when the command is being deleted
|
9
|
-
public onDrop(_editor: Editor) { }
|
10
|
-
|
11
|
-
public abstract description(editor: Editor, localizationTable: EditorLocalization): string;
|
12
|
-
|
13
|
-
public static union(a: Command, b: Command): Command {
|
14
|
-
return new class extends Command {
|
15
|
-
public apply(editor: Editor) {
|
16
|
-
a.apply(editor);
|
17
|
-
b.apply(editor);
|
18
|
-
}
|
19
|
-
|
20
|
-
public unapply(editor: Editor) {
|
21
|
-
b.unapply(editor);
|
22
|
-
a.unapply(editor);
|
23
|
-
}
|
24
|
-
|
25
|
-
public description(editor: Editor, localizationTable: EditorLocalization) {
|
26
|
-
const aDescription = a.description(editor, localizationTable);
|
27
|
-
const bDescription = b.description(editor, localizationTable);
|
28
|
-
|
29
|
-
if (aDescription === bDescription) {
|
30
|
-
return aDescription;
|
31
|
-
}
|
32
|
-
|
33
|
-
return `${aDescription}, ${bDescription}`;
|
34
|
-
}
|
35
|
-
};
|
36
|
-
}
|
37
|
-
|
38
|
-
public static readonly empty = new class extends Command {
|
39
|
-
public description(_editor: Editor, _localizationTable: EditorLocalization) { return ''; }
|
40
|
-
public apply(_editor: Editor) { }
|
41
|
-
public unapply(_editor: Editor) { }
|
42
|
-
};
|
43
|
-
}
|
44
|
-
|
45
|
-
export default Command;
|
@@ -1,75 +0,0 @@
|
|
1
|
-
import AbstractComponent from '../components/AbstractComponent';
|
2
|
-
import describeComponentList from '../components/util/describeComponentList';
|
3
|
-
import Editor from '../Editor';
|
4
|
-
import { EditorLocalization } from '../localization';
|
5
|
-
import Erase from './Erase';
|
6
|
-
import SerializableCommand from './SerializableCommand';
|
7
|
-
|
8
|
-
/**
|
9
|
-
* A command that duplicates the {@link AbstractComponent}s it's given. This command
|
10
|
-
* is the reverse of an {@link Erase} command.
|
11
|
-
*
|
12
|
-
* @example
|
13
|
-
* ```ts
|
14
|
-
* // Given some editor...
|
15
|
-
*
|
16
|
-
* // Find all elements intersecting the rectangle with top left (0,0) and
|
17
|
-
* // (width,height)=(100,100).
|
18
|
-
* const elems = editor.image.getElementsIntersectingRegion(
|
19
|
-
* new Rect2(0, 0, 100, 100)
|
20
|
-
* );
|
21
|
-
*
|
22
|
-
* // Create a command that, when applied, will duplicate the elements.
|
23
|
-
* const duplicateElems = new Duplicate(elems);
|
24
|
-
*
|
25
|
-
* // Apply the command (and make it undoable)
|
26
|
-
* editor.dispatch(duplicateElems);
|
27
|
-
* ```
|
28
|
-
*
|
29
|
-
* @see {@link Editor.dispatch} {@link EditorImage.getElementsIntersectingRegion}
|
30
|
-
*/
|
31
|
-
export default class Duplicate extends SerializableCommand {
|
32
|
-
private duplicates: AbstractComponent[];
|
33
|
-
private reverse: Erase;
|
34
|
-
|
35
|
-
public constructor(private toDuplicate: AbstractComponent[]) {
|
36
|
-
super('duplicate');
|
37
|
-
|
38
|
-
this.duplicates = toDuplicate.map(elem => elem.clone());
|
39
|
-
this.reverse = new Erase(this.duplicates);
|
40
|
-
}
|
41
|
-
|
42
|
-
public apply(editor: Editor): void {
|
43
|
-
this.reverse.unapply(editor);
|
44
|
-
}
|
45
|
-
|
46
|
-
public unapply(editor: Editor): void {
|
47
|
-
this.reverse.apply(editor);
|
48
|
-
}
|
49
|
-
|
50
|
-
public override onDrop(editor: Editor): void {
|
51
|
-
this.reverse.onDrop(editor);
|
52
|
-
}
|
53
|
-
|
54
|
-
public description(_editor: Editor, localizationTable: EditorLocalization): string {
|
55
|
-
if (this.duplicates.length === 0) {
|
56
|
-
return localizationTable.duplicatedNoElements;
|
57
|
-
}
|
58
|
-
|
59
|
-
return localizationTable.duplicateAction(
|
60
|
-
describeComponentList(localizationTable, this.duplicates) ?? localizationTable.elements,
|
61
|
-
this.duplicates.length
|
62
|
-
);
|
63
|
-
}
|
64
|
-
|
65
|
-
protected serializeToJSON() {
|
66
|
-
return this.toDuplicate.map(elem => elem.getId());
|
67
|
-
}
|
68
|
-
|
69
|
-
static {
|
70
|
-
SerializableCommand.register('duplicate', (json: any, editor: Editor) => {
|
71
|
-
const elems = json.map((id: string) => editor.image.lookupElement(id));
|
72
|
-
return new Duplicate(elems);
|
73
|
-
});
|
74
|
-
}
|
75
|
-
}
|
package/src/commands/Erase.ts
DELETED
@@ -1,95 +0,0 @@
|
|
1
|
-
import AbstractComponent from '../components/AbstractComponent';
|
2
|
-
import describeComponentList from '../components/util/describeComponentList';
|
3
|
-
import Editor from '../Editor';
|
4
|
-
import EditorImage from '../EditorImage';
|
5
|
-
import { EditorLocalization } from '../localization';
|
6
|
-
import SerializableCommand from './SerializableCommand';
|
7
|
-
|
8
|
-
/**
|
9
|
-
* Removes the given {@link AbstractComponent}s from the image.
|
10
|
-
*
|
11
|
-
* @example
|
12
|
-
* ```ts
|
13
|
-
* // Given some editor...
|
14
|
-
*
|
15
|
-
* // Find all elements intersecting the rectangle with top left (-10,-30) and
|
16
|
-
* // (width,height)=(50,100).
|
17
|
-
* const elems = editor.image.getElementsIntersectingRegion(
|
18
|
-
* new Rect2(-10, -30, 50, 100)
|
19
|
-
* );
|
20
|
-
*
|
21
|
-
* // Create a command that erases [elems] when applied
|
22
|
-
* const eraseElemsCmd = new Erase(elems);
|
23
|
-
*
|
24
|
-
* // Apply the command (and make it undoable)
|
25
|
-
* editor.dispatch(eraseElemsCmd);
|
26
|
-
* ```
|
27
|
-
*/
|
28
|
-
export default class Erase extends SerializableCommand {
|
29
|
-
private toRemove: AbstractComponent[];
|
30
|
-
private applied: boolean;
|
31
|
-
|
32
|
-
public constructor(toRemove: AbstractComponent[]) {
|
33
|
-
super('erase');
|
34
|
-
|
35
|
-
// Clone the list
|
36
|
-
this.toRemove = toRemove.map(elem => elem);
|
37
|
-
this.applied = false;
|
38
|
-
}
|
39
|
-
|
40
|
-
public apply(editor: Editor) {
|
41
|
-
for (const part of this.toRemove) {
|
42
|
-
const parent = editor.image.findParent(part);
|
43
|
-
|
44
|
-
if (parent) {
|
45
|
-
editor.image.onDestroyElement(part);
|
46
|
-
parent.remove();
|
47
|
-
}
|
48
|
-
}
|
49
|
-
|
50
|
-
this.applied = true;
|
51
|
-
editor.queueRerender();
|
52
|
-
}
|
53
|
-
|
54
|
-
public unapply(editor: Editor) {
|
55
|
-
for (const part of this.toRemove) {
|
56
|
-
if (!editor.image.findParent(part)) {
|
57
|
-
EditorImage.addElement(part).apply(editor);
|
58
|
-
}
|
59
|
-
}
|
60
|
-
|
61
|
-
this.applied = false;
|
62
|
-
editor.queueRerender();
|
63
|
-
}
|
64
|
-
|
65
|
-
public override onDrop(editor: Editor) {
|
66
|
-
if (this.applied) {
|
67
|
-
for (const part of this.toRemove) {
|
68
|
-
editor.image.onDestroyElement(part);
|
69
|
-
}
|
70
|
-
}
|
71
|
-
}
|
72
|
-
|
73
|
-
public description(_editor: Editor, localizationTable: EditorLocalization): string {
|
74
|
-
if (this.toRemove.length === 0) {
|
75
|
-
return localizationTable.erasedNoElements;
|
76
|
-
}
|
77
|
-
|
78
|
-
const description = describeComponentList(localizationTable, this.toRemove) ?? localizationTable.elements;
|
79
|
-
return localizationTable.eraseAction(description, this.toRemove.length);
|
80
|
-
}
|
81
|
-
|
82
|
-
protected serializeToJSON() {
|
83
|
-
const elemIds = this.toRemove.map(elem => elem.getId());
|
84
|
-
return elemIds;
|
85
|
-
}
|
86
|
-
|
87
|
-
static {
|
88
|
-
SerializableCommand.register('erase', (json: any, editor) => {
|
89
|
-
const elems = json
|
90
|
-
.map((elemId: string) => editor.image.lookupElement(elemId))
|
91
|
-
.filter((elem: AbstractComponent|null) => elem !== null);
|
92
|
-
return new Erase(elems);
|
93
|
-
});
|
94
|
-
}
|
95
|
-
}
|
@@ -1,49 +0,0 @@
|
|
1
|
-
import Editor from '../Editor';
|
2
|
-
import Command from './Command';
|
3
|
-
|
4
|
-
export type DeserializationCallback = (data: Record<string, any>|any[], editor: Editor) => SerializableCommand;
|
5
|
-
|
6
|
-
export default abstract class SerializableCommand extends Command {
|
7
|
-
public constructor(private commandTypeId: string) {
|
8
|
-
super();
|
9
|
-
|
10
|
-
if (!(commandTypeId in SerializableCommand.deserializationCallbacks)) {
|
11
|
-
throw new Error(
|
12
|
-
`Command ${commandTypeId} must have a registered deserialization callback. To do this, call SerializableCommand.register.`
|
13
|
-
);
|
14
|
-
}
|
15
|
-
}
|
16
|
-
|
17
|
-
protected abstract serializeToJSON(): string|Record<string, any>|any[];
|
18
|
-
private static deserializationCallbacks: Record<string, DeserializationCallback> = {};
|
19
|
-
|
20
|
-
// Convert this command to an object that can be passed to `JSON.stringify`.
|
21
|
-
//
|
22
|
-
// Do not rely on the stability of the optupt of this function — it can change
|
23
|
-
// form without a major version increase.
|
24
|
-
public serialize(): Record<string|symbol, any> {
|
25
|
-
return {
|
26
|
-
data: this.serializeToJSON(),
|
27
|
-
commandType: this.commandTypeId,
|
28
|
-
};
|
29
|
-
}
|
30
|
-
|
31
|
-
// Convert a `string` containing JSON data (or the output of `JSON.parse`) into a
|
32
|
-
// `Command`.
|
33
|
-
public static deserialize(data: string|Record<string, any>, editor: Editor): SerializableCommand {
|
34
|
-
const json = typeof data === 'string' ? JSON.parse(data) : data;
|
35
|
-
const commandType = json.commandType as string;
|
36
|
-
|
37
|
-
if (!(commandType in SerializableCommand.deserializationCallbacks)) {
|
38
|
-
throw new Error(`Unrecognised command type ${commandType}!`);
|
39
|
-
}
|
40
|
-
|
41
|
-
return SerializableCommand.deserializationCallbacks[commandType](json.data, editor);
|
42
|
-
}
|
43
|
-
|
44
|
-
// Register a deserialization callback. This must be called at least once for every subclass of
|
45
|
-
// `SerializableCommand`.
|
46
|
-
public static register(commandTypeId: string, deserialize: DeserializationCallback) {
|
47
|
-
SerializableCommand.deserializationCallbacks[commandTypeId] = deserialize;
|
48
|
-
}
|
49
|
-
}
|