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/tools/PanZoom.ts
DELETED
@@ -1,525 +0,0 @@
|
|
1
|
-
|
2
|
-
import { Editor } from '../Editor';
|
3
|
-
import { Mat33, Vec3, Point2, Vec2 } from '@js-draw/math';
|
4
|
-
import Pointer, { PointerDevice } from '../Pointer';
|
5
|
-
import { EditorEventType } from '../types';
|
6
|
-
import { KeyPressEvent, PointerEvt, WheelEvt } from '../inputEvents';
|
7
|
-
import untilNextAnimationFrame from '../util/untilNextAnimationFrame';
|
8
|
-
import { Viewport, ViewportTransform } from '../Viewport';
|
9
|
-
import BaseTool from './BaseTool';
|
10
|
-
import { moveDownKeyboardShortcutId, moveLeftKeyboardShortcutId, moveRightKeyboardShortcutId, moveUpKeyboardShortcutId, rotateClockwiseKeyboardShortcutId, rotateCounterClockwiseKeyboardShortcutId, zoomInKeyboardShortcutId, zoomOutKeyboardShortcutId } from './keybindings';
|
11
|
-
|
12
|
-
interface PinchData {
|
13
|
-
canvasCenter: Point2;
|
14
|
-
screenCenter: Point2;
|
15
|
-
angle: number;
|
16
|
-
dist: number;
|
17
|
-
}
|
18
|
-
|
19
|
-
export enum PanZoomMode {
|
20
|
-
OneFingerTouchGestures = 0x1,
|
21
|
-
TwoFingerTouchGestures = 0x1 << 1,
|
22
|
-
RightClickDrags = 0x1 << 2,
|
23
|
-
SinglePointerGestures = 0x1 << 3,
|
24
|
-
Keyboard = 0x1 << 4,
|
25
|
-
|
26
|
-
RotationLocked = 0x1 << 5,
|
27
|
-
}
|
28
|
-
|
29
|
-
type ScrollByCallback = (delta: Vec2) => void;
|
30
|
-
|
31
|
-
class InertialScroller {
|
32
|
-
private running: boolean = false;
|
33
|
-
private currentVelocity: Vec2;
|
34
|
-
|
35
|
-
public constructor(
|
36
|
-
private initialVelocity: Vec2,
|
37
|
-
private scrollBy: ScrollByCallback,
|
38
|
-
private onComplete: ()=> void
|
39
|
-
) {
|
40
|
-
this.start();
|
41
|
-
}
|
42
|
-
|
43
|
-
private async start() {
|
44
|
-
if (this.running) {
|
45
|
-
return;
|
46
|
-
}
|
47
|
-
|
48
|
-
this.currentVelocity = this.initialVelocity;
|
49
|
-
let lastTime = performance.now();
|
50
|
-
this.running = true;
|
51
|
-
|
52
|
-
const maxSpeed = 5000; // units/s
|
53
|
-
const minSpeed = 200; // units/s
|
54
|
-
if (this.currentVelocity.magnitude() > maxSpeed) {
|
55
|
-
this.currentVelocity = this.currentVelocity.normalized().times(maxSpeed);
|
56
|
-
}
|
57
|
-
|
58
|
-
while (this.running && this.currentVelocity.magnitude() > minSpeed) {
|
59
|
-
const nowTime = performance.now();
|
60
|
-
const dt = (nowTime - lastTime) / 1000;
|
61
|
-
|
62
|
-
this.currentVelocity = this.currentVelocity.times(Math.pow(1/8, dt));
|
63
|
-
this.scrollBy(this.currentVelocity.times(dt));
|
64
|
-
|
65
|
-
await untilNextAnimationFrame();
|
66
|
-
lastTime = nowTime;
|
67
|
-
}
|
68
|
-
|
69
|
-
if (this.running) {
|
70
|
-
this.stop();
|
71
|
-
}
|
72
|
-
}
|
73
|
-
|
74
|
-
public getCurrentVelocity(): Vec2|null {
|
75
|
-
if (!this.running) {
|
76
|
-
return null;
|
77
|
-
}
|
78
|
-
|
79
|
-
return this.currentVelocity;
|
80
|
-
}
|
81
|
-
|
82
|
-
public stop(): void {
|
83
|
-
if (this.running) {
|
84
|
-
this.running = false;
|
85
|
-
this.onComplete();
|
86
|
-
}
|
87
|
-
}
|
88
|
-
}
|
89
|
-
|
90
|
-
export default class PanZoom extends BaseTool {
|
91
|
-
private transform: ViewportTransform|null = null;
|
92
|
-
|
93
|
-
// Distance between two touch points at the **start** of a gesture.
|
94
|
-
private startDist: number;
|
95
|
-
|
96
|
-
// Distance between two touch points the last time input data was received.
|
97
|
-
private lastDist: number;
|
98
|
-
private lastScreenCenter: Point2;
|
99
|
-
private lastTimestamp: number;
|
100
|
-
private lastPointerDownTimestamp: number = 0;
|
101
|
-
private initialTouchAngle: number = 0;
|
102
|
-
private initialViewportRotation: number = 0;
|
103
|
-
|
104
|
-
// Set to `true` only when scaling has started (if two fingers are down and have moved
|
105
|
-
// far enough).
|
106
|
-
private isScaling: boolean = false;
|
107
|
-
|
108
|
-
private inertialScroller: InertialScroller|null = null;
|
109
|
-
private velocity: Vec2|null = null;
|
110
|
-
|
111
|
-
public constructor(private editor: Editor, private mode: PanZoomMode, description: string) {
|
112
|
-
super(editor.notifier, description);
|
113
|
-
}
|
114
|
-
|
115
|
-
// Returns information about the pointers in a gesture
|
116
|
-
public computePinchData(p1: Pointer, p2: Pointer): PinchData {
|
117
|
-
// Swap the pointers to ensure consistent ordering.
|
118
|
-
if (p1.id < p2.id) {
|
119
|
-
const tmp = p1;
|
120
|
-
p1 = p2;
|
121
|
-
p2 = tmp;
|
122
|
-
}
|
123
|
-
|
124
|
-
const screenBetween = p2.screenPos.minus(p1.screenPos);
|
125
|
-
const angle = screenBetween.angle();
|
126
|
-
const dist = screenBetween.magnitude();
|
127
|
-
const canvasCenter = p2.canvasPos.plus(p1.canvasPos).times(0.5);
|
128
|
-
const screenCenter = p2.screenPos.plus(p1.screenPos).times(0.5);
|
129
|
-
|
130
|
-
return { canvasCenter, screenCenter, angle, dist };
|
131
|
-
}
|
132
|
-
|
133
|
-
private allPointersAreOfType(pointers: Pointer[], kind: PointerDevice) {
|
134
|
-
return pointers.every(pointer => pointer.device === kind);
|
135
|
-
}
|
136
|
-
|
137
|
-
public override onPointerDown({ allPointers: pointers, current: currentPointer }: PointerEvt): boolean {
|
138
|
-
let handlingGesture = false;
|
139
|
-
|
140
|
-
const inertialScrollerVelocity = this.inertialScroller?.getCurrentVelocity() ?? Vec2.zero;
|
141
|
-
this.inertialScroller?.stop();
|
142
|
-
this.velocity = inertialScrollerVelocity;
|
143
|
-
|
144
|
-
this.lastPointerDownTimestamp = currentPointer.timeStamp;
|
145
|
-
|
146
|
-
const allAreTouch = this.allPointersAreOfType(pointers, PointerDevice.Touch);
|
147
|
-
const isRightClick = this.allPointersAreOfType(pointers, PointerDevice.RightButtonMouse);
|
148
|
-
|
149
|
-
if (allAreTouch && pointers.length === 2 && this.mode & PanZoomMode.TwoFingerTouchGestures) {
|
150
|
-
const { screenCenter, angle, dist } = this.computePinchData(pointers[0], pointers[1]);
|
151
|
-
this.lastDist = dist;
|
152
|
-
this.startDist = dist;
|
153
|
-
this.lastScreenCenter = screenCenter;
|
154
|
-
this.initialTouchAngle = angle;
|
155
|
-
this.initialViewportRotation = this.editor.viewport.getRotationAngle();
|
156
|
-
this.isScaling = false;
|
157
|
-
|
158
|
-
handlingGesture = true;
|
159
|
-
} else if (pointers.length === 1 && (
|
160
|
-
(this.mode & PanZoomMode.OneFingerTouchGestures && allAreTouch)
|
161
|
-
|| (isRightClick && this.mode & PanZoomMode.RightClickDrags)
|
162
|
-
|| (this.mode & PanZoomMode.SinglePointerGestures)
|
163
|
-
)) {
|
164
|
-
this.lastScreenCenter = pointers[0].screenPos;
|
165
|
-
this.isScaling = false;
|
166
|
-
handlingGesture = true;
|
167
|
-
}
|
168
|
-
|
169
|
-
if (handlingGesture) {
|
170
|
-
this.lastTimestamp = performance.now();
|
171
|
-
this.transform ??= Viewport.transformBy(Mat33.identity);
|
172
|
-
this.editor.display.setDraftMode(true);
|
173
|
-
}
|
174
|
-
|
175
|
-
return handlingGesture;
|
176
|
-
}
|
177
|
-
|
178
|
-
private updateVelocity(currentCenter: Point2) {
|
179
|
-
const deltaPos = currentCenter.minus(this.lastScreenCenter);
|
180
|
-
let deltaTime = (performance.now() - this.lastTimestamp) / 1000;
|
181
|
-
|
182
|
-
// Ignore duplicate events, unless there has been enough time between them.
|
183
|
-
if (deltaPos.magnitude() === 0 && deltaTime < 0.1) {
|
184
|
-
return;
|
185
|
-
}
|
186
|
-
// We divide by deltaTime. Don't divide by zero.
|
187
|
-
if (deltaTime === 0) {
|
188
|
-
return;
|
189
|
-
}
|
190
|
-
|
191
|
-
// Don't divide by almost zero, either
|
192
|
-
deltaTime = Math.max(deltaTime, 0.01);
|
193
|
-
|
194
|
-
const currentVelocity = deltaPos.times(1 / deltaTime);
|
195
|
-
let smoothedVelocity = currentVelocity;
|
196
|
-
|
197
|
-
if (this.velocity) {
|
198
|
-
smoothedVelocity = this.velocity.lerp(currentVelocity, 0.5);
|
199
|
-
}
|
200
|
-
|
201
|
-
this.velocity = smoothedVelocity;
|
202
|
-
}
|
203
|
-
|
204
|
-
// Returns the change in position of the center of the given group of pointers.
|
205
|
-
// Assumes this.lastScreenCenter has been set appropriately.
|
206
|
-
private getCenterDelta(screenCenter: Point2): Vec2 {
|
207
|
-
// Use transformVec3 to avoid translating the delta
|
208
|
-
const delta = this.editor.viewport.screenToCanvasTransform.transformVec3(screenCenter.minus(this.lastScreenCenter));
|
209
|
-
return delta;
|
210
|
-
}
|
211
|
-
|
212
|
-
// Snaps `angle` to common desired rotations. For example, if `touchAngle` corresponds
|
213
|
-
// to a viewport rotation of 90.1 degrees, this function returns a rotation delta that,
|
214
|
-
// when applied to the viewport, rotates the viewport to 90.0 degrees.
|
215
|
-
//
|
216
|
-
// Returns a snapped rotation delta that, when applied to the viewport, rotates the viewport,
|
217
|
-
// from its position on the last touchDown event, by `touchAngle - initialTouchAngle`.
|
218
|
-
private toSnappedRotationDelta(touchAngle: number) {
|
219
|
-
const deltaAngle = touchAngle - this.initialTouchAngle;
|
220
|
-
let fullRotation = deltaAngle + this.initialViewportRotation;
|
221
|
-
|
222
|
-
const snapToMultipleOf = Math.PI / 2;
|
223
|
-
const roundedFullRotation = Math.round(fullRotation / snapToMultipleOf) * snapToMultipleOf;
|
224
|
-
|
225
|
-
// The maximum angle for which we snap the given angle to a multiple of
|
226
|
-
// `snapToMultipleOf`.
|
227
|
-
const maxSnapAngle = 0.07;
|
228
|
-
|
229
|
-
// Snap the rotation
|
230
|
-
if (Math.abs(fullRotation - roundedFullRotation) < maxSnapAngle) {
|
231
|
-
fullRotation = roundedFullRotation;
|
232
|
-
|
233
|
-
// Work around a rotation/matrix multiply bug.
|
234
|
-
// (See commit after 4abe27ff8e7913155828f98dee77b09c57c51d30).
|
235
|
-
// TODO: Fix the underlying issue and remove this.
|
236
|
-
if (fullRotation !== 0) {
|
237
|
-
fullRotation += 0.0001;
|
238
|
-
}
|
239
|
-
}
|
240
|
-
|
241
|
-
return fullRotation - this.editor.viewport.getRotationAngle();
|
242
|
-
}
|
243
|
-
|
244
|
-
private handleTwoFingerMove(allPointers: Pointer[]) {
|
245
|
-
const { screenCenter, canvasCenter, angle, dist } = this.computePinchData(allPointers[0], allPointers[1]);
|
246
|
-
|
247
|
-
const delta = this.getCenterDelta(screenCenter);
|
248
|
-
let deltaRotation;
|
249
|
-
|
250
|
-
if (this.isRotationLocked()) {
|
251
|
-
deltaRotation = 0;
|
252
|
-
} else {
|
253
|
-
deltaRotation = this.toSnappedRotationDelta(angle);
|
254
|
-
}
|
255
|
-
|
256
|
-
this.updateVelocity(screenCenter);
|
257
|
-
|
258
|
-
let scaleFactor = 1;
|
259
|
-
if (this.isScaling) {
|
260
|
-
scaleFactor = dist / this.lastDist;
|
261
|
-
} else {
|
262
|
-
const initialScaleFactor = dist / this.startDist;
|
263
|
-
|
264
|
-
// Only start scaling if scaling done so far exceeds some threshold.
|
265
|
-
if (initialScaleFactor > 1.05 || initialScaleFactor < 0.95) {
|
266
|
-
scaleFactor = initialScaleFactor;
|
267
|
-
this.isScaling = true;
|
268
|
-
}
|
269
|
-
}
|
270
|
-
|
271
|
-
const transformUpdate = Mat33.translation(delta)
|
272
|
-
.rightMul(Mat33.scaling2D(scaleFactor, canvasCenter))
|
273
|
-
.rightMul(Mat33.zRotation(deltaRotation, canvasCenter));
|
274
|
-
|
275
|
-
this.lastScreenCenter = screenCenter;
|
276
|
-
this.lastDist = dist;
|
277
|
-
this.transform = Viewport.transformBy(
|
278
|
-
this.transform!.transform.rightMul(transformUpdate)
|
279
|
-
);
|
280
|
-
}
|
281
|
-
|
282
|
-
private handleOneFingerMove(pointer: Pointer) {
|
283
|
-
const delta = this.getCenterDelta(pointer.screenPos);
|
284
|
-
this.transform = Viewport.transformBy(
|
285
|
-
this.transform!.transform.rightMul(
|
286
|
-
Mat33.translation(delta)
|
287
|
-
)
|
288
|
-
);
|
289
|
-
this.updateVelocity(pointer.screenPos);
|
290
|
-
this.lastScreenCenter = pointer.screenPos;
|
291
|
-
}
|
292
|
-
|
293
|
-
public override onPointerMove({ allPointers }: PointerEvt): void {
|
294
|
-
this.transform ??= Viewport.transformBy(Mat33.identity);
|
295
|
-
|
296
|
-
const lastTransform = this.transform;
|
297
|
-
if (allPointers.length === 2) {
|
298
|
-
this.handleTwoFingerMove(allPointers);
|
299
|
-
} else if (allPointers.length === 1) {
|
300
|
-
this.handleOneFingerMove(allPointers[0]);
|
301
|
-
}
|
302
|
-
lastTransform.unapply(this.editor);
|
303
|
-
this.transform.apply(this.editor);
|
304
|
-
|
305
|
-
this.lastTimestamp = performance.now();
|
306
|
-
}
|
307
|
-
|
308
|
-
public override onPointerUp(event: PointerEvt): void {
|
309
|
-
const onComplete = () => {
|
310
|
-
if (this.transform) {
|
311
|
-
this.transform.unapply(this.editor);
|
312
|
-
this.editor.dispatch(this.transform, false);
|
313
|
-
}
|
314
|
-
|
315
|
-
this.editor.display.setDraftMode(false);
|
316
|
-
this.transform = null;
|
317
|
-
this.velocity = Vec2.zero;
|
318
|
-
};
|
319
|
-
|
320
|
-
const minInertialScrollDt = 30;
|
321
|
-
const shouldInertialScroll =
|
322
|
-
event.current.device === PointerDevice.Touch
|
323
|
-
&& event.allPointers.length === 1
|
324
|
-
&& this.velocity !== null
|
325
|
-
&& event.current.timeStamp - this.lastPointerDownTimestamp > minInertialScrollDt;
|
326
|
-
|
327
|
-
if (shouldInertialScroll && this.velocity !== null) {
|
328
|
-
const oldVelocity = this.velocity;
|
329
|
-
|
330
|
-
// If the user drags the screen, then stops, then lifts the pointer,
|
331
|
-
// we want the final velocity to reflect the stop at the end (so the velocity
|
332
|
-
// should be near zero). Handle this:
|
333
|
-
this.updateVelocity(event.current.screenPos);
|
334
|
-
|
335
|
-
// Work around an input issue. Some devices that disable the touchscreen when a stylus
|
336
|
-
// comes near the screen fire a touch-end event at the position of the stylus when a
|
337
|
-
// touch gesture is canceled. Because the stylus is often far away from the last touch,
|
338
|
-
// this causes a great displacement between the second-to-last (from the touchscreen) and
|
339
|
-
// last (from the pen that is now near the screen) events. Only allow velocity to decrease
|
340
|
-
// to work around this:
|
341
|
-
if (oldVelocity.magnitude() < this.velocity.magnitude()) {
|
342
|
-
this.velocity = oldVelocity;
|
343
|
-
}
|
344
|
-
|
345
|
-
// Cancel any ongoing inertial scrolling.
|
346
|
-
this.inertialScroller?.stop();
|
347
|
-
|
348
|
-
this.inertialScroller = new InertialScroller(this.velocity, (scrollDelta: Vec2) => {
|
349
|
-
if (!this.transform) {
|
350
|
-
return;
|
351
|
-
}
|
352
|
-
|
353
|
-
const canvasDelta = this.editor.viewport.screenToCanvasTransform.transformVec3(scrollDelta);
|
354
|
-
|
355
|
-
// Scroll by scrollDelta
|
356
|
-
this.transform.unapply(this.editor);
|
357
|
-
this.transform = Viewport.transformBy(
|
358
|
-
this.transform.transform.rightMul(
|
359
|
-
Mat33.translation(canvasDelta)
|
360
|
-
)
|
361
|
-
);
|
362
|
-
this.transform.apply(this.editor);
|
363
|
-
}, onComplete);
|
364
|
-
} else {
|
365
|
-
onComplete();
|
366
|
-
}
|
367
|
-
}
|
368
|
-
|
369
|
-
public override onGestureCancel(): void {
|
370
|
-
this.inertialScroller?.stop();
|
371
|
-
this.velocity = Vec2.zero;
|
372
|
-
this.transform?.unapply(this.editor);
|
373
|
-
this.editor.display.setDraftMode(false);
|
374
|
-
this.transform = null;
|
375
|
-
}
|
376
|
-
|
377
|
-
// Applies [transformUpdate] to the editor. This stacks on top of the
|
378
|
-
// current transformation, if it exists.
|
379
|
-
private updateTransform(transformUpdate: Mat33, announce: boolean = false) {
|
380
|
-
let newTransform = transformUpdate;
|
381
|
-
if (this.transform) {
|
382
|
-
newTransform = this.transform.transform.rightMul(transformUpdate);
|
383
|
-
}
|
384
|
-
|
385
|
-
this.transform?.unapply(this.editor);
|
386
|
-
this.transform = Viewport.transformBy(newTransform);
|
387
|
-
this.transform.apply(this.editor);
|
388
|
-
|
389
|
-
if (announce) {
|
390
|
-
this.editor.announceForAccessibility(this.transform.description(this.editor, this.editor.localization));
|
391
|
-
}
|
392
|
-
}
|
393
|
-
|
394
|
-
public override onWheel({ delta, screenPos }: WheelEvt): boolean {
|
395
|
-
this.inertialScroller?.stop();
|
396
|
-
|
397
|
-
// Reset the transformation -- wheel events are individual events, so we don't
|
398
|
-
// need to unapply/reapply.
|
399
|
-
this.transform = Viewport.transformBy(Mat33.identity);
|
400
|
-
|
401
|
-
const canvasPos = this.editor.viewport.screenToCanvas(screenPos);
|
402
|
-
const toCanvas = this.editor.viewport.screenToCanvasTransform;
|
403
|
-
|
404
|
-
// Transform without including translation
|
405
|
-
const translation =
|
406
|
-
toCanvas.transformVec3(
|
407
|
-
Vec3.of(-delta.x, -delta.y, 0)
|
408
|
-
);
|
409
|
-
const pinchZoomScaleFactor = 1.03;
|
410
|
-
const transformUpdate = Mat33.scaling2D(
|
411
|
-
Math.max(0.25, Math.min(Math.pow(pinchZoomScaleFactor, -delta.z), 4)), canvasPos
|
412
|
-
).rightMul(
|
413
|
-
Mat33.translation(translation)
|
414
|
-
);
|
415
|
-
this.updateTransform(transformUpdate, true);
|
416
|
-
|
417
|
-
return true;
|
418
|
-
}
|
419
|
-
|
420
|
-
public override onKeyPress(event: KeyPressEvent): boolean {
|
421
|
-
this.inertialScroller?.stop();
|
422
|
-
|
423
|
-
if (!(this.mode & PanZoomMode.Keyboard)) {
|
424
|
-
return false;
|
425
|
-
}
|
426
|
-
|
427
|
-
// No need to keep the same the transform for keyboard events.
|
428
|
-
this.transform = Viewport.transformBy(Mat33.identity);
|
429
|
-
|
430
|
-
let translation = Vec2.zero;
|
431
|
-
let scale = 1;
|
432
|
-
let rotation = 0;
|
433
|
-
|
434
|
-
// Keyboard shortcut handling
|
435
|
-
const shortcucts = this.editor.shortcuts;
|
436
|
-
if (shortcucts.matchesShortcut(moveLeftKeyboardShortcutId, event)) {
|
437
|
-
translation = Vec2.of(-1, 0);
|
438
|
-
} else if (shortcucts.matchesShortcut(moveRightKeyboardShortcutId, event)) {
|
439
|
-
translation = Vec2.of(1, 0);
|
440
|
-
} else if (shortcucts.matchesShortcut(moveUpKeyboardShortcutId, event)) {
|
441
|
-
translation = Vec2.of(0, -1);
|
442
|
-
} else if (shortcucts.matchesShortcut(moveDownKeyboardShortcutId, event)) {
|
443
|
-
translation = Vec2.of(0, 1);
|
444
|
-
} else if (shortcucts.matchesShortcut(zoomInKeyboardShortcutId, event)) {
|
445
|
-
scale = 1/2;
|
446
|
-
} else if (shortcucts.matchesShortcut(zoomOutKeyboardShortcutId, event)) {
|
447
|
-
scale = 2;
|
448
|
-
} else if (shortcucts.matchesShortcut(rotateClockwiseKeyboardShortcutId, event)) {
|
449
|
-
rotation = 1;
|
450
|
-
} else if (shortcucts.matchesShortcut(rotateCounterClockwiseKeyboardShortcutId, event)) {
|
451
|
-
rotation = -1;
|
452
|
-
} else {
|
453
|
-
return false;
|
454
|
-
}
|
455
|
-
|
456
|
-
// For each keypress,
|
457
|
-
translation = translation.times(30); // Move at most 30 units
|
458
|
-
rotation *= Math.PI / 8; // Rotate at least a sixteenth of a rotation
|
459
|
-
|
460
|
-
// Transform the canvas, not the viewport:
|
461
|
-
translation = translation.times(-1);
|
462
|
-
rotation = rotation * -1;
|
463
|
-
scale = 1 / scale;
|
464
|
-
|
465
|
-
// Work around an issue that seems to be related to rotation matricies losing precision on inversion.
|
466
|
-
// TODO: Figure out why and implement a better solution.
|
467
|
-
if (rotation !== 0) {
|
468
|
-
rotation += 0.0001;
|
469
|
-
}
|
470
|
-
|
471
|
-
if (this.isRotationLocked()) {
|
472
|
-
rotation = 0;
|
473
|
-
}
|
474
|
-
|
475
|
-
const toCanvas = this.editor.viewport.screenToCanvasTransform;
|
476
|
-
|
477
|
-
// Transform without translating (treat toCanvas as a linear instead of
|
478
|
-
// an affine transformation).
|
479
|
-
translation = toCanvas.transformVec3(translation);
|
480
|
-
|
481
|
-
// Rotate/scale about the center of the canvas
|
482
|
-
const transformCenter = this.editor.viewport.visibleRect.center;
|
483
|
-
const transformUpdate = Mat33.scaling2D(
|
484
|
-
scale, transformCenter
|
485
|
-
).rightMul(Mat33.zRotation(
|
486
|
-
rotation, transformCenter
|
487
|
-
)).rightMul(Mat33.translation(
|
488
|
-
translation
|
489
|
-
));
|
490
|
-
this.updateTransform(transformUpdate, true);
|
491
|
-
|
492
|
-
return true;
|
493
|
-
}
|
494
|
-
|
495
|
-
private isRotationLocked(): boolean {
|
496
|
-
return !!(this.mode & PanZoomMode.RotationLocked);
|
497
|
-
}
|
498
|
-
|
499
|
-
// Sets whether the given `mode` is enabled. `mode` should be a single
|
500
|
-
// mode from the `PanZoomMode` enum.
|
501
|
-
public setModeEnabled(mode: PanZoomMode, enabled: boolean) {
|
502
|
-
let newMode = this.mode;
|
503
|
-
if (enabled) {
|
504
|
-
newMode |= mode;
|
505
|
-
} else {
|
506
|
-
newMode &= ~mode;
|
507
|
-
}
|
508
|
-
this.setMode(newMode);
|
509
|
-
}
|
510
|
-
|
511
|
-
public setMode(mode: PanZoomMode) {
|
512
|
-
if (mode !== this.mode) {
|
513
|
-
this.mode = mode;
|
514
|
-
|
515
|
-
this.editor.notifier.dispatch(EditorEventType.ToolUpdated, {
|
516
|
-
kind: EditorEventType.ToolUpdated,
|
517
|
-
tool: this,
|
518
|
-
});
|
519
|
-
}
|
520
|
-
}
|
521
|
-
|
522
|
-
public getMode(): PanZoomMode {
|
523
|
-
return this.mode;
|
524
|
-
}
|
525
|
-
}
|
@@ -1,94 +0,0 @@
|
|
1
|
-
import Editor from '../Editor';
|
2
|
-
import AbstractComponent from '../components/AbstractComponent';
|
3
|
-
import TextComponent from '../components/TextComponent';
|
4
|
-
import SVGLoader from '../SVGLoader';
|
5
|
-
import { PasteEvent } from '../inputEvents';
|
6
|
-
import { Mat33, Color4 } from '@js-draw/math';
|
7
|
-
import BaseTool from './BaseTool';
|
8
|
-
import TextTool from './TextTool';
|
9
|
-
import ImageComponent from '../components/ImageComponent';
|
10
|
-
import TextRenderingStyle from '../rendering/TextRenderingStyle';
|
11
|
-
|
12
|
-
/**
|
13
|
-
* A tool that handles paste events (e.g. as triggered by ctrl+V).
|
14
|
-
*
|
15
|
-
* @example
|
16
|
-
* While `ToolController` has a `PasteHandler` in its default list of tools,
|
17
|
-
* if a non-default set is being used, `PasteHandler` can be added as follows:
|
18
|
-
* ```ts
|
19
|
-
* const toolController = editor.toolController;
|
20
|
-
* toolController.addTool(new PasteHandler(editor));
|
21
|
-
* ```
|
22
|
-
*/
|
23
|
-
export default class PasteHandler extends BaseTool {
|
24
|
-
public constructor(private editor: Editor) {
|
25
|
-
super(editor.notifier, editor.localization.pasteHandler);
|
26
|
-
}
|
27
|
-
|
28
|
-
// @internal
|
29
|
-
public override onPaste(event: PasteEvent): boolean {
|
30
|
-
const mime = event.mime.toLowerCase();
|
31
|
-
|
32
|
-
if (mime === 'image/svg+xml') {
|
33
|
-
void this.doSVGPaste(event.data);
|
34
|
-
return true;
|
35
|
-
}
|
36
|
-
else if (mime === 'text/plain') {
|
37
|
-
void this.doTextPaste(event.data);
|
38
|
-
return true;
|
39
|
-
}
|
40
|
-
else if (mime === 'image/png' || mime === 'image/jpeg') {
|
41
|
-
void this.doImagePaste(event.data);
|
42
|
-
return true;
|
43
|
-
}
|
44
|
-
|
45
|
-
return false;
|
46
|
-
}
|
47
|
-
|
48
|
-
private async addComponentsFromPaste(components: AbstractComponent[]) {
|
49
|
-
await this.editor.addAndCenterComponents(components);
|
50
|
-
}
|
51
|
-
|
52
|
-
private async doSVGPaste(data: string) {
|
53
|
-
const sanitize = true;
|
54
|
-
const loader = SVGLoader.fromString(data, sanitize);
|
55
|
-
|
56
|
-
const components: AbstractComponent[] = [];
|
57
|
-
|
58
|
-
await loader.start((component) => {
|
59
|
-
components.push(component);
|
60
|
-
},
|
61
|
-
(_countProcessed: number, _totalToProcess: number) => null);
|
62
|
-
|
63
|
-
await this.addComponentsFromPaste(components);
|
64
|
-
}
|
65
|
-
|
66
|
-
private async doTextPaste(text: string) {
|
67
|
-
const textTools = this.editor.toolController.getMatchingTools(TextTool);
|
68
|
-
|
69
|
-
textTools.sort((a, b) => {
|
70
|
-
if (!a.isEnabled() && b.isEnabled()) {
|
71
|
-
return -1;
|
72
|
-
}
|
73
|
-
|
74
|
-
if (!b.isEnabled() && a.isEnabled()) {
|
75
|
-
return 1;
|
76
|
-
}
|
77
|
-
|
78
|
-
return 0;
|
79
|
-
});
|
80
|
-
|
81
|
-
const defaultTextStyle: TextRenderingStyle = { size: 12, fontFamily: 'sans', renderingStyle: { fill: Color4.red } };
|
82
|
-
const pastedTextStyle: TextRenderingStyle = textTools[0]?.getTextStyle() ?? defaultTextStyle;
|
83
|
-
|
84
|
-
const lines = text.split('\n');
|
85
|
-
await this.addComponentsFromPaste([ TextComponent.fromLines(lines, Mat33.identity, pastedTextStyle) ]);
|
86
|
-
}
|
87
|
-
|
88
|
-
private async doImagePaste(dataURL: string) {
|
89
|
-
const image = new Image();
|
90
|
-
image.src = dataURL;
|
91
|
-
const component = await ImageComponent.fromImage(image, Mat33.identity);
|
92
|
-
await this.addComponentsFromPaste([ component ]);
|
93
|
-
}
|
94
|
-
}
|