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.
Files changed (197) hide show
  1. package/LICENSE +21 -0
  2. package/dist/Editor.css +1 -0
  3. package/dist/bundle.js +1 -1
  4. package/dist/bundledStyles.js +1 -1
  5. package/dist/cjs/toolbar/AbstractToolbar.d.ts +9 -13
  6. package/dist/cjs/toolbar/AbstractToolbar.js +14 -19
  7. package/dist/cjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
  8. package/dist/cjs/toolbar/widgets/SaveActionWidget.js +26 -0
  9. package/dist/cjs/toolbar/widgets/keybindings.d.ts +1 -0
  10. package/dist/cjs/toolbar/widgets/keybindings.js +4 -1
  11. package/dist/cjs/version.js +1 -1
  12. package/dist/mjs/toolbar/AbstractToolbar.d.ts +9 -13
  13. package/dist/mjs/toolbar/AbstractToolbar.mjs +14 -19
  14. package/dist/mjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
  15. package/dist/mjs/toolbar/widgets/SaveActionWidget.mjs +21 -0
  16. package/dist/mjs/toolbar/widgets/keybindings.d.ts +1 -0
  17. package/dist/mjs/toolbar/widgets/keybindings.mjs +3 -0
  18. package/dist/mjs/version.mjs +1 -1
  19. package/docs/img/readme-images/js-draw.jpg +0 -0
  20. package/docs/img/readme-images/unsupported-elements--in-editor.png +0 -0
  21. package/package.json +5 -4
  22. package/src/toolbar/EdgeToolbar.scss +1 -0
  23. package/dist-test/test_imports/package-lock.json +0 -13
  24. package/dist-test/test_imports/package.json +0 -12
  25. package/dist-test/test_imports/test-imports.js +0 -11
  26. package/dist-test/test_imports/test-require.cjs +0 -14
  27. package/src/Editor.loadFrom.test.ts +0 -24
  28. package/src/Editor.test.ts +0 -107
  29. package/src/Editor.toSVG.test.ts +0 -294
  30. package/src/Editor.ts +0 -1443
  31. package/src/EditorImage.test.ts +0 -117
  32. package/src/EditorImage.ts +0 -609
  33. package/src/EventDispatcher.test.ts +0 -123
  34. package/src/EventDispatcher.ts +0 -72
  35. package/src/Pointer.ts +0 -183
  36. package/src/SVGLoader.test.ts +0 -114
  37. package/src/SVGLoader.ts +0 -672
  38. package/src/UndoRedoHistory.test.ts +0 -34
  39. package/src/UndoRedoHistory.ts +0 -102
  40. package/src/Viewport.ts +0 -322
  41. package/src/bundle/bundled.ts +0 -7
  42. package/src/commands/Command.ts +0 -45
  43. package/src/commands/Duplicate.ts +0 -75
  44. package/src/commands/Erase.ts +0 -95
  45. package/src/commands/SerializableCommand.ts +0 -49
  46. package/src/commands/UnresolvedCommand.ts +0 -37
  47. package/src/commands/invertCommand.ts +0 -58
  48. package/src/commands/lib.ts +0 -16
  49. package/src/commands/localization.ts +0 -47
  50. package/src/commands/uniteCommands.test.ts +0 -23
  51. package/src/commands/uniteCommands.ts +0 -140
  52. package/src/components/AbstractComponent.transformBy.test.ts +0 -23
  53. package/src/components/AbstractComponent.ts +0 -383
  54. package/src/components/BackgroundComponent.test.ts +0 -44
  55. package/src/components/BackgroundComponent.ts +0 -348
  56. package/src/components/ImageComponent.ts +0 -176
  57. package/src/components/RestylableComponent.ts +0 -161
  58. package/src/components/SVGGlobalAttributesObject.ts +0 -79
  59. package/src/components/Stroke.test.ts +0 -137
  60. package/src/components/Stroke.ts +0 -294
  61. package/src/components/TextComponent.test.ts +0 -202
  62. package/src/components/TextComponent.ts +0 -429
  63. package/src/components/UnknownSVGObject.test.ts +0 -10
  64. package/src/components/UnknownSVGObject.ts +0 -60
  65. package/src/components/builders/ArrowBuilder.ts +0 -106
  66. package/src/components/builders/CircleBuilder.ts +0 -100
  67. package/src/components/builders/FreehandLineBuilder.test.ts +0 -24
  68. package/src/components/builders/FreehandLineBuilder.ts +0 -210
  69. package/src/components/builders/LineBuilder.ts +0 -77
  70. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -453
  71. package/src/components/builders/RectangleBuilder.ts +0 -73
  72. package/src/components/builders/types.ts +0 -15
  73. package/src/components/lib.ts +0 -31
  74. package/src/components/localization.ts +0 -24
  75. package/src/components/util/StrokeSmoother.ts +0 -302
  76. package/src/components/util/describeComponentList.ts +0 -18
  77. package/src/dialogs/makeAboutDialog.ts +0 -82
  78. package/src/inputEvents.ts +0 -143
  79. package/src/lib.ts +0 -91
  80. package/src/localization.ts +0 -34
  81. package/src/localizations/de.ts +0 -146
  82. package/src/localizations/en.ts +0 -8
  83. package/src/localizations/es.ts +0 -74
  84. package/src/localizations/getLocalizationTable.test.ts +0 -27
  85. package/src/localizations/getLocalizationTable.ts +0 -74
  86. package/src/rendering/Display.ts +0 -247
  87. package/src/rendering/RenderablePathSpec.ts +0 -88
  88. package/src/rendering/RenderingStyle.test.ts +0 -68
  89. package/src/rendering/RenderingStyle.ts +0 -55
  90. package/src/rendering/TextRenderingStyle.ts +0 -55
  91. package/src/rendering/caching/CacheRecord.test.ts +0 -48
  92. package/src/rendering/caching/CacheRecord.ts +0 -76
  93. package/src/rendering/caching/CacheRecordManager.ts +0 -71
  94. package/src/rendering/caching/RenderingCache.test.ts +0 -43
  95. package/src/rendering/caching/RenderingCache.ts +0 -66
  96. package/src/rendering/caching/RenderingCacheNode.ts +0 -404
  97. package/src/rendering/caching/testUtils.ts +0 -35
  98. package/src/rendering/caching/types.ts +0 -34
  99. package/src/rendering/lib.ts +0 -8
  100. package/src/rendering/localization.ts +0 -20
  101. package/src/rendering/renderers/AbstractRenderer.ts +0 -232
  102. package/src/rendering/renderers/CanvasRenderer.ts +0 -312
  103. package/src/rendering/renderers/DummyRenderer.test.ts +0 -41
  104. package/src/rendering/renderers/DummyRenderer.ts +0 -142
  105. package/src/rendering/renderers/SVGRenderer.ts +0 -434
  106. package/src/rendering/renderers/TextOnlyRenderer.test.ts +0 -34
  107. package/src/rendering/renderers/TextOnlyRenderer.ts +0 -68
  108. package/src/shortcuts/KeyBinding.test.ts +0 -61
  109. package/src/shortcuts/KeyBinding.ts +0 -257
  110. package/src/shortcuts/KeyboardShortcutManager.test.ts +0 -95
  111. package/src/shortcuts/KeyboardShortcutManager.ts +0 -163
  112. package/src/shortcuts/lib.ts +0 -3
  113. package/src/testing/createEditor.ts +0 -11
  114. package/src/testing/getUniquePointerId.ts +0 -18
  115. package/src/testing/lib.ts +0 -3
  116. package/src/testing/sendPenEvent.ts +0 -36
  117. package/src/testing/sendTouchEvent.ts +0 -71
  118. package/src/toolbar/AbstractToolbar.ts +0 -542
  119. package/src/toolbar/DropdownToolbar.ts +0 -220
  120. package/src/toolbar/EdgeToolbar.test.ts +0 -54
  121. package/src/toolbar/EdgeToolbar.ts +0 -543
  122. package/src/toolbar/IconProvider.ts +0 -861
  123. package/src/toolbar/constants.ts +0 -1
  124. package/src/toolbar/lib.ts +0 -6
  125. package/src/toolbar/localization.ts +0 -136
  126. package/src/toolbar/types.ts +0 -13
  127. package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
  128. package/src/toolbar/widgets/BaseToolWidget.ts +0 -81
  129. package/src/toolbar/widgets/BaseWidget.ts +0 -495
  130. package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -250
  131. package/src/toolbar/widgets/EraserToolWidget.ts +0 -84
  132. package/src/toolbar/widgets/HandToolWidget.ts +0 -239
  133. package/src/toolbar/widgets/InsertImageWidget.ts +0 -248
  134. package/src/toolbar/widgets/OverflowWidget.ts +0 -92
  135. package/src/toolbar/widgets/PenToolWidget.ts +0 -369
  136. package/src/toolbar/widgets/SelectionToolWidget.ts +0 -195
  137. package/src/toolbar/widgets/TextToolWidget.ts +0 -149
  138. package/src/toolbar/widgets/components/makeColorInput.ts +0 -184
  139. package/src/toolbar/widgets/components/makeFileInput.ts +0 -128
  140. package/src/toolbar/widgets/components/makeGridSelector.ts +0 -179
  141. package/src/toolbar/widgets/components/makeSeparator.ts +0 -17
  142. package/src/toolbar/widgets/components/makeThicknessSlider.ts +0 -62
  143. package/src/toolbar/widgets/keybindings.ts +0 -19
  144. package/src/toolbar/widgets/layout/DropdownLayoutManager.ts +0 -262
  145. package/src/toolbar/widgets/layout/EdgeToolbarLayoutManager.ts +0 -71
  146. package/src/toolbar/widgets/layout/types.ts +0 -74
  147. package/src/toolbar/widgets/lib.ts +0 -13
  148. package/src/tools/BaseTool.ts +0 -169
  149. package/src/tools/Eraser.test.ts +0 -103
  150. package/src/tools/Eraser.ts +0 -173
  151. package/src/tools/FindTool.test.ts +0 -67
  152. package/src/tools/FindTool.ts +0 -153
  153. package/src/tools/InputFilter/FunctionMapper.ts +0 -17
  154. package/src/tools/InputFilter/InputMapper.ts +0 -41
  155. package/src/tools/InputFilter/InputPipeline.test.ts +0 -41
  156. package/src/tools/InputFilter/InputPipeline.ts +0 -34
  157. package/src/tools/InputFilter/InputStabilizer.ts +0 -254
  158. package/src/tools/InputFilter/StrokeKeyboardControl.ts +0 -104
  159. package/src/tools/PanZoom.test.ts +0 -339
  160. package/src/tools/PanZoom.ts +0 -525
  161. package/src/tools/PasteHandler.ts +0 -94
  162. package/src/tools/Pen.test.ts +0 -260
  163. package/src/tools/Pen.ts +0 -284
  164. package/src/tools/PipetteTool.ts +0 -84
  165. package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -29
  166. package/src/tools/SelectionTool/Selection.ts +0 -647
  167. package/src/tools/SelectionTool/SelectionHandle.ts +0 -142
  168. package/src/tools/SelectionTool/SelectionTool.test.ts +0 -370
  169. package/src/tools/SelectionTool/SelectionTool.ts +0 -510
  170. package/src/tools/SelectionTool/TransformMode.ts +0 -112
  171. package/src/tools/SelectionTool/types.ts +0 -11
  172. package/src/tools/SoundUITool.ts +0 -221
  173. package/src/tools/TextTool.ts +0 -339
  174. package/src/tools/ToolController.ts +0 -224
  175. package/src/tools/ToolEnabledGroup.ts +0 -14
  176. package/src/tools/ToolSwitcherShortcut.ts +0 -39
  177. package/src/tools/ToolbarShortcutHandler.ts +0 -39
  178. package/src/tools/UndoRedoShortcut.test.ts +0 -62
  179. package/src/tools/UndoRedoShortcut.ts +0 -24
  180. package/src/tools/keybindings.ts +0 -85
  181. package/src/tools/lib.ts +0 -22
  182. package/src/tools/localization.ts +0 -76
  183. package/src/types.ts +0 -151
  184. package/src/util/ReactiveValue.test.ts +0 -168
  185. package/src/util/ReactiveValue.ts +0 -241
  186. package/src/util/assertions.ts +0 -55
  187. package/src/util/fileToBase64.ts +0 -18
  188. package/src/util/guessKeyCodeFromKey.ts +0 -36
  189. package/src/util/listPrefixMatch.ts +0 -19
  190. package/src/util/stopPropagationOfScrollingWheelEvents.ts +0 -20
  191. package/src/util/untilNextAnimationFrame.ts +0 -9
  192. package/src/util/waitForAll.ts +0 -18
  193. package/src/util/waitForTimeout.ts +0 -9
  194. package/src/version.test.ts +0 -12
  195. package/src/version.ts +0 -3
  196. package/tools/allLocales.js +0 -4
  197. package/tools/copyREADME.ts +0 -62
@@ -1,41 +0,0 @@
1
- import { InputEvt } from '../../inputEvents';
2
-
3
- type OnEventCallback = (event: InputEvt)=>boolean;
4
-
5
- export interface InputEventListener {
6
- // Returns true if handled
7
- onEvent: OnEventCallback;
8
- }
9
-
10
- /**
11
- * Accepts input events and emits input events.
12
- */
13
- export default abstract class InputMapper implements InputEventListener {
14
- #listener: OnEventCallback|null = null;
15
-
16
- public constructor() { }
17
-
18
- // @internal
19
- public setEmitListener(listener: InputEventListener|OnEventCallback|null) {
20
- if (listener && typeof (listener) === 'object') {
21
- this.#listener = (event) => {
22
- return listener.onEvent(event) ?? false;
23
- };
24
- } else {
25
- this.#listener = listener;
26
- }
27
- }
28
-
29
- protected emit(event: InputEvt): boolean {
30
- return this.#listener?.(event) ?? false;
31
- }
32
-
33
- /**
34
- * @returns true if the given `event` should be considered "handled" by the app and thus not
35
- * forwarded to other targets. For example, returning "true" for a touchpad pinch event prevents
36
- * the pinch event from zooming the webpage.
37
- *
38
- * Generally, this should return the result of calling `this.emit` with some event.
39
- */
40
- public abstract onEvent(event: InputEvt): boolean;
41
- }
@@ -1,41 +0,0 @@
1
- import { CopyEvent, InputEvt, InputEvtType } from '../../inputEvents';
2
- import FunctionMapper from './FunctionMapper';
3
- import InputPipeline from './InputPipeline';
4
-
5
- describe('InputPipeline', () => {
6
- it('empty pipeline should map events to themselves', () => {
7
- const pipeline = new InputPipeline();
8
-
9
- // Shouldn't crash when no emit listener
10
- pipeline.onEvent({ kind: InputEvtType.GestureCancelEvt });
11
-
12
- const mockEmitListener = jest.fn();
13
- pipeline.setEmitListener(mockEmitListener);
14
-
15
- const testCopyEvent: CopyEvent = { kind: InputEvtType.CopyEvent, setData: ()=>{} };
16
- pipeline.onEvent(testCopyEvent);
17
- expect(mockEmitListener).toHaveBeenCalledTimes(1);
18
- expect(mockEmitListener).toHaveBeenCalledWith(testCopyEvent);
19
- });
20
-
21
- it('pipeline with a single mapper should filter events through the mapper', () => {
22
- const pipeline = new InputPipeline();
23
-
24
- const processEvent = jest.fn((event: InputEvt) => event);
25
- pipeline.addToTail(new FunctionMapper(processEvent));
26
-
27
- expect(processEvent).not.toHaveBeenCalled();
28
- pipeline.onEvent({ kind: InputEvtType.GestureCancelEvt });
29
- expect(processEvent).toHaveBeenCalled();
30
-
31
- pipeline.onEvent({ kind: InputEvtType.GestureCancelEvt });
32
- expect(processEvent).toHaveBeenCalledTimes(2);
33
-
34
- const mockEmitListener = jest.fn();
35
- pipeline.setEmitListener(mockEmitListener);
36
-
37
- pipeline.onEvent({ kind: InputEvtType.GestureCancelEvt });
38
- expect(processEvent).toHaveBeenCalledTimes(3);
39
- expect(mockEmitListener).toHaveBeenCalledTimes(1);
40
- });
41
- });
@@ -1,34 +0,0 @@
1
- import { InputEvt } from '../../inputEvents';
2
- import InputMapper from './InputMapper';
3
-
4
- /**
5
- * The composition of multiple `InputMapper`s.
6
- */
7
- export default class InputPipeline extends InputMapper {
8
- #head: InputMapper|null = null;
9
- #tail: InputMapper|null = null;
10
-
11
- public override onEvent(event: InputEvt): boolean {
12
- if (this.#head === null) {
13
- return this.emit(event);
14
- } else {
15
- return this.#head.onEvent(event);
16
- }
17
- }
18
-
19
- /**
20
- * Adds a new `InputMapper` to the *tail* of this pipeline.
21
- * Note that an instance of an `InputMapper` can only be used in a single
22
- * pipeline.
23
- */
24
- public addToTail(mapper: InputMapper) {
25
- if (!this.#tail) {
26
- this.#head = mapper;
27
- this.#tail = this.#head;
28
- } else {
29
- this.#tail.setEmitListener(mapper);
30
- this.#tail = mapper;
31
- }
32
- this.#tail.setEmitListener(event => this.emit(event));
33
- }
34
- }
@@ -1,254 +0,0 @@
1
- import { GestureCancelEvt, InputEvt, InputEvtType, PointerEvt, PointerMoveEvt, isPointerEvt } from '../../inputEvents';
2
- import InputMapper from './InputMapper';
3
- import Viewport from '../../Viewport';
4
- import Editor from '../../Editor';
5
- import { Point2, Vec2 } from '@js-draw/math';
6
- import untilNextAnimationFrame from '../../util/untilNextAnimationFrame';
7
-
8
- enum StabilizerType {
9
- IntertialStabilizer,
10
- }
11
-
12
- interface InputStabilizerOptions {
13
- kind: StabilizerType.IntertialStabilizer,
14
-
15
- mass: number;
16
- springConstant: number;
17
- frictionCoefficient: number;
18
-
19
- maxPointDist: number;
20
-
21
- // Minimum cosine similarity between the velocity vector and the displacement to
22
- // the stroke endpoint to connect them.
23
- minSimilarityToFinalize: number;
24
-
25
- // Fraction of how much should be inertia based and how much should be
26
- // moving in the direction of the pointer.
27
- inertiaFraction: number;
28
-
29
- // In addition to friction, decreases the velocity by this fraction at each
30
- // time step.
31
- velocityDecayFactor: number;
32
- }
33
-
34
- const defaultOptions: InputStabilizerOptions = {
35
- kind: StabilizerType.IntertialStabilizer,
36
-
37
- mass: 0.4, // kg
38
- springConstant: 100.0, // N/m
39
- frictionCoefficient: 0.28,
40
-
41
- maxPointDist: 10, // screen units
42
-
43
- inertiaFraction: 0.75,
44
-
45
- minSimilarityToFinalize: 0.0,
46
- velocityDecayFactor: 0.1,
47
- };
48
-
49
- // Stabilizes input for a single cursor
50
- class StylusInputStabilizer {
51
- private runLoop: boolean = true;
52
- private lastUpdateTime: number = 0;
53
- private targetInterval: number;
54
- private velocity: Vec2 = Vec2.zero;
55
-
56
- private strokePoint: Point2;
57
- private targetPoint: Point2;
58
-
59
- public constructor(
60
- // The initial starting point of the pointer.
61
- start: Point2,
62
-
63
- // Emits a pointer motion event, returns true if the event was handled.
64
- private updatePointer: (screenPoint: Point2, timeStamp: number)=>boolean,
65
-
66
- private readonly options: InputStabilizerOptions
67
- ) {
68
- this.strokePoint = start;
69
- this.targetPoint = start;
70
- this.targetInterval = 10; // ms
71
- void this.loop();
72
- }
73
-
74
- private async loop() {
75
- this.lastUpdateTime = performance.now();
76
- while (this.runLoop) {
77
- this.update(false);
78
- await untilNextAnimationFrame();
79
- }
80
- }
81
-
82
- public setTarget(point: Point2) {
83
- this.targetPoint = point;
84
- }
85
-
86
- private getNextVelocity(deltaTimeMs: number) {
87
- const toTarget = this.targetPoint.minus(this.strokePoint);
88
-
89
- const springForce = toTarget.times(this.options.springConstant);
90
-
91
- const gravityAccel = 10;
92
- const normalForceMagnitude = this.options.mass * gravityAccel;
93
- const frictionForce = this.velocity.normalizedOrZero().times(
94
- -this.options.frictionCoefficient * normalForceMagnitude
95
- );
96
- const acceleration = (springForce.plus(frictionForce)).times(1/this.options.mass);
97
-
98
- const decayFactor = this.options.velocityDecayFactor;
99
- const springVelocity = this.velocity.times(1 - decayFactor).plus(acceleration.times(deltaTimeMs / 1000));
100
-
101
- // An alternate velocity that goes directly towards the target.
102
- const toTargetVelocity = toTarget.normalizedOrZero().times(springVelocity.length());
103
-
104
- return toTargetVelocity.lerp(springVelocity, this.options.inertiaFraction);
105
- }
106
-
107
- public update(force: boolean): boolean {
108
- const nowTime = performance.now();
109
- const deltaTime = nowTime - this.lastUpdateTime;
110
-
111
- const reachedTarget = this.strokePoint.eq(this.targetPoint);
112
-
113
- if (deltaTime > this.targetInterval || force) {
114
- if (!reachedTarget) {
115
- let velocity: Vec2;
116
- let deltaX: Vec2;
117
- let parts = 1;
118
-
119
- do {
120
- velocity = this.getNextVelocity(deltaTime / parts);
121
- deltaX = velocity.times(deltaTime / 1000);
122
-
123
- parts ++;
124
- } while (deltaX.magnitude() > this.options.maxPointDist && parts < 10);
125
-
126
- for (let i = 0; i < parts; i++) {
127
- this.velocity = this.getNextVelocity(deltaTime / parts);
128
- deltaX = this.velocity.times(deltaTime / 1000);
129
- this.strokePoint = this.strokePoint.plus(deltaX);
130
-
131
- // Allows the last updatePointer to be returned.
132
- if (i < parts - 1) {
133
- this.updatePointer(this.strokePoint, nowTime);
134
- }
135
- }
136
- }
137
-
138
- // Even if we have reached the target, ensure that lastUpdateTime is updated
139
- // (prevent large deltaTime).
140
- this.lastUpdateTime = nowTime;
141
-
142
- if (force || !reachedTarget) {
143
- return this.updatePointer(this.strokePoint, nowTime);
144
- }
145
- }
146
-
147
- return false;
148
- }
149
-
150
- /** Finalizes the current stroke. */
151
- public finish() {
152
- this.runLoop = false;
153
-
154
- const toTarget = this.targetPoint.minus(this.strokePoint);
155
- if (this.velocity.dot(toTarget) > this.options.minSimilarityToFinalize) {
156
- // Connect the stroke to its end point
157
- this.updatePointer(this.targetPoint, performance.now());
158
- }
159
- }
160
-
161
- public cancel() {
162
- this.runLoop = false;
163
- }
164
- }
165
-
166
- export default class InputStabilizer extends InputMapper {
167
- private stabilizer: StylusInputStabilizer|null = null;
168
- private lastPointerEvent: PointerEvt|null = null;
169
-
170
- public constructor(
171
- private viewport: Viewport,
172
- private readonly options: InputStabilizerOptions = defaultOptions,
173
- ) {
174
- super();
175
- }
176
-
177
- private mapPointerEvent(event: PointerEvt|GestureCancelEvt) {
178
- // Don't store the last pointer event for use with pressure/button data --
179
- // this information can be very different for a pointerup event.
180
- if (isPointerEvt(event) && event.kind !== InputEvtType.PointerUpEvt) {
181
- this.lastPointerEvent = event;
182
- }
183
-
184
- // Only apply smoothing if there is a single pointer.
185
- if (event.kind === InputEvtType.GestureCancelEvt || event.allPointers.length > 1 || this.stabilizer === null) {
186
- return this.emit(event);
187
- }
188
-
189
- this.stabilizer.setTarget(event.current.screenPos);
190
-
191
- if (event.kind === InputEvtType.PointerMoveEvt) {
192
- return this.stabilizer.update(true);
193
- } else if (event.kind === InputEvtType.PointerUpEvt) {
194
- this.stabilizer.finish();
195
- return this.emit(event);
196
- } else {
197
- return this.emit(event);
198
- }
199
- }
200
-
201
- // Assumes that there is exactly one pointer that is currently down.
202
- private emitPointerMove(screenPoint: Point2, timeStamp: number) {
203
- if (!this.lastPointerEvent) {
204
- return false;
205
- }
206
-
207
- const pointer = this.lastPointerEvent.current
208
- .withScreenPosition(screenPoint, this.viewport)
209
- .withTimestamp(timeStamp);
210
-
211
- const event: PointerMoveEvt = {
212
- kind: InputEvtType.PointerMoveEvt,
213
- current: pointer,
214
- allPointers: [ pointer ],
215
- };
216
-
217
- const handled = this.emit(event);
218
- return handled;
219
- }
220
-
221
- public override onEvent(event: InputEvt): boolean {
222
- if (isPointerEvt(event) || event.kind === InputEvtType.GestureCancelEvt
223
- ) {
224
- if (event.kind === InputEvtType.PointerDownEvt) {
225
- if (this.stabilizer === null) {
226
- this.stabilizer = new StylusInputStabilizer(
227
- event.current.screenPos,
228
- (screenPoint, timeStamp) => this.emitPointerMove(screenPoint, timeStamp),
229
- this.options,
230
- );
231
- } else if (event.allPointers.length > 1) {
232
- // Do not attempt to stabilize multiple pointers.
233
- this.stabilizer.cancel();
234
- this.stabilizer = null;
235
- }
236
- }
237
-
238
- const handled = this.mapPointerEvent(event);
239
-
240
- if (event.kind === InputEvtType.PointerUpEvt || event.kind === InputEvtType.GestureCancelEvt) {
241
- this.stabilizer?.cancel();
242
- this.stabilizer = null;
243
- }
244
-
245
- return handled;
246
- }
247
-
248
- return this.emit(event);
249
- }
250
-
251
- public static fromEditor(editor: Editor) {
252
- return new InputStabilizer(editor.viewport);
253
- }
254
- }
@@ -1,104 +0,0 @@
1
- import { InputEvt, InputEvtType, PointerEvt } from '../../inputEvents';
2
- import InputMapper from './InputMapper';
3
- import KeyboardShortcutManager from '../../shortcuts/KeyboardShortcutManager';
4
- import { lineLockKeyboardShortcutId, snapToGridKeyboardShortcutId } from '../keybindings';
5
- import Viewport from '../../Viewport';
6
- import Pointer from '../../Pointer';
7
- import { Point2 } from '@js-draw/math';
8
- import Editor from '../../Editor';
9
-
10
- /**
11
- * Provides keyboard shortcuts that provide some amount of control over
12
- * drawing (e.g. snap to grid, plane lock).
13
- */
14
- export default class StrokeKeyboardControl extends InputMapper {
15
- private snapToGridEnabled: boolean = false;
16
- private angleLockEnabled: boolean = false;
17
-
18
- // The point at which the last pointerDown event happened (or null if
19
- // no such event has occurred).
20
- private startPointCanvas: Point2|null = null;
21
-
22
- public constructor(
23
- private shortcuts: KeyboardShortcutManager, private viewport: Viewport
24
- ) {
25
- super();
26
- }
27
-
28
- // Snap the given pointer to the nearer of the x/y axes.
29
- private xyAxesSnap(pointer: Pointer) {
30
- if (!this.startPointCanvas) {
31
- return pointer;
32
- }
33
-
34
- // Convert this.startPointCanvas here because the viewport might change
35
- // while drawing a stroke.
36
- const screenPos = this.viewport.canvasToScreen(this.startPointCanvas);
37
- return pointer.lockedToXYAxesScreen(screenPos, this.viewport);
38
- }
39
-
40
- private mapPointerEvent(event: PointerEvt): PointerEvt {
41
- const mapPointer = (pointer: Pointer) => {
42
- // Only map if there's exactly one pointer.
43
- if (event.allPointers.length > 1) {
44
- return pointer;
45
- }
46
-
47
- if (this.snapToGridEnabled) {
48
- return pointer.snappedToGrid(this.viewport);
49
- }
50
-
51
- if (this.angleLockEnabled && this.startPointCanvas) {
52
- return this.xyAxesSnap(pointer);
53
- }
54
-
55
- return pointer;
56
- };
57
-
58
- return {
59
- kind: event.kind,
60
- current: mapPointer(event.current),
61
- allPointers: event.allPointers.map(mapPointer),
62
- };
63
- }
64
-
65
- public override onEvent(event: InputEvt): boolean {
66
- const shortcuts = this.shortcuts;
67
-
68
- if (event.kind === InputEvtType.PointerDownEvt || event.kind === InputEvtType.PointerMoveEvt || event.kind === InputEvtType.PointerUpEvt) {
69
- if (event.kind === InputEvtType.PointerDownEvt) {
70
- this.startPointCanvas = event.current.canvasPos;
71
- }
72
-
73
- event = this.mapPointerEvent(event);
74
- }
75
-
76
- let handled = this.emit(event);
77
-
78
- if (// Always check keyUpEvents (in case we handled the corresponding keyDown event)
79
- event.kind === InputEvtType.KeyUpEvent
80
-
81
- // Only handle key press events if another tool isn't handling it. We don't want
82
- // snap to grid/angle lock to conflict with selection/another tool's shortcuts.
83
- || (!handled && event.kind === InputEvtType.KeyPressEvent)
84
- ) {
85
- const isKeyPress = event.kind === InputEvtType.KeyPressEvent;
86
-
87
- if (shortcuts.matchesShortcut(snapToGridKeyboardShortcutId, event)) {
88
- this.snapToGridEnabled = isKeyPress;
89
- handled = true;
90
- }
91
-
92
- if (shortcuts.matchesShortcut(lineLockKeyboardShortcutId, event)) {
93
- this.angleLockEnabled = isKeyPress;
94
- handled = true;
95
- }
96
- }
97
-
98
- return handled;
99
- }
100
-
101
- public static fromEditor(editor: Editor) {
102
- return new StrokeKeyboardControl(editor.shortcuts, editor.viewport);
103
- }
104
- }