js-draw 1.21.2 → 1.21.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (183) hide show
  1. package/dist/bundle.js +1 -1
  2. package/dist/cjs/tools/BaseTool.d.ts +61 -0
  3. package/dist/cjs/tools/BaseTool.js +179 -0
  4. package/dist/cjs/tools/Eraser.d.ts +60 -0
  5. package/dist/cjs/tools/Eraser.js +299 -0
  6. package/dist/cjs/tools/Eraser.test.d.ts +1 -0
  7. package/dist/cjs/tools/FindTool.d.ts +21 -0
  8. package/dist/cjs/tools/FindTool.js +137 -0
  9. package/dist/cjs/tools/FindTool.test.d.ts +1 -0
  10. package/dist/cjs/tools/InputFilter/ContextMenuRecognizer.d.ts +17 -0
  11. package/dist/cjs/tools/InputFilter/ContextMenuRecognizer.js +105 -0
  12. package/dist/cjs/tools/InputFilter/ContextMenuRecognizer.test.d.ts +1 -0
  13. package/dist/cjs/tools/InputFilter/FunctionMapper.d.ts +12 -0
  14. package/dist/cjs/tools/InputFilter/FunctionMapper.js +21 -0
  15. package/dist/cjs/tools/InputFilter/InputMapper.d.ts +23 -0
  16. package/dist/cjs/tools/InputFilter/InputMapper.js +38 -0
  17. package/dist/cjs/tools/InputFilter/InputPipeline.d.ts +15 -0
  18. package/dist/cjs/tools/InputFilter/InputPipeline.js +54 -0
  19. package/dist/cjs/tools/InputFilter/InputPipeline.test.d.ts +1 -0
  20. package/dist/cjs/tools/InputFilter/InputStabilizer.d.ts +29 -0
  21. package/dist/cjs/tools/InputFilter/InputStabilizer.js +181 -0
  22. package/dist/cjs/tools/InputFilter/StrokeKeyboardControl.d.ts +21 -0
  23. package/dist/cjs/tools/InputFilter/StrokeKeyboardControl.js +84 -0
  24. package/dist/cjs/tools/PanZoom.d.ts +125 -0
  25. package/dist/cjs/tools/PanZoom.js +517 -0
  26. package/dist/cjs/tools/PanZoom.test.d.ts +1 -0
  27. package/dist/cjs/tools/PasteHandler.d.ts +23 -0
  28. package/dist/cjs/tools/PasteHandler.js +109 -0
  29. package/dist/cjs/tools/Pen.d.ts +54 -0
  30. package/dist/cjs/tools/Pen.js +335 -0
  31. package/dist/cjs/tools/Pen.test.d.ts +1 -0
  32. package/dist/cjs/tools/PipetteTool.d.ts +28 -0
  33. package/dist/cjs/tools/PipetteTool.js +69 -0
  34. package/dist/cjs/tools/ScrollbarTool.d.ts +18 -0
  35. package/dist/cjs/tools/ScrollbarTool.js +85 -0
  36. package/dist/cjs/tools/SelectionTool/SelectAllShortcutHandler.d.ts +9 -0
  37. package/dist/cjs/tools/SelectionTool/SelectAllShortcutHandler.js +32 -0
  38. package/dist/cjs/tools/SelectionTool/Selection.d.ts +72 -0
  39. package/dist/cjs/tools/SelectionTool/Selection.js +634 -0
  40. package/dist/cjs/tools/SelectionTool/SelectionHandle.d.ts +62 -0
  41. package/dist/cjs/tools/SelectionTool/SelectionHandle.js +141 -0
  42. package/dist/cjs/tools/SelectionTool/SelectionMenuShortcut.d.ts +32 -0
  43. package/dist/cjs/tools/SelectionTool/SelectionMenuShortcut.js +86 -0
  44. package/dist/cjs/tools/SelectionTool/SelectionTool.d.ts +42 -0
  45. package/dist/cjs/tools/SelectionTool/SelectionTool.js +500 -0
  46. package/dist/cjs/tools/SelectionTool/SelectionTool.selecting.test.d.ts +1 -0
  47. package/dist/cjs/tools/SelectionTool/SelectionTool.test.d.ts +1 -0
  48. package/dist/cjs/tools/SelectionTool/ToPointerAutoscroller.d.ts +23 -0
  49. package/dist/cjs/tools/SelectionTool/ToPointerAutoscroller.js +83 -0
  50. package/dist/cjs/tools/SelectionTool/TransformMode.d.ts +42 -0
  51. package/dist/cjs/tools/SelectionTool/TransformMode.js +155 -0
  52. package/dist/cjs/tools/SelectionTool/keybindings.d.ts +15 -0
  53. package/dist/cjs/tools/SelectionTool/keybindings.js +38 -0
  54. package/dist/cjs/tools/SelectionTool/types.d.ts +35 -0
  55. package/dist/cjs/tools/SelectionTool/types.js +14 -0
  56. package/dist/cjs/tools/SelectionTool/util/makeClipboardErrorHandlers.d.ts +6 -0
  57. package/dist/cjs/tools/SelectionTool/util/makeClipboardErrorHandlers.js +50 -0
  58. package/dist/cjs/tools/SelectionTool/util/showSelectionContextMenu.d.ts +5 -0
  59. package/dist/cjs/tools/SelectionTool/util/showSelectionContextMenu.js +43 -0
  60. package/dist/cjs/tools/SoundUITool.d.ts +26 -0
  61. package/dist/cjs/tools/SoundUITool.js +171 -0
  62. package/dist/cjs/tools/TextTool.d.ts +36 -0
  63. package/dist/cjs/tools/TextTool.js +285 -0
  64. package/dist/cjs/tools/TextTool.test.d.ts +1 -0
  65. package/dist/cjs/tools/ToolController.d.ts +73 -0
  66. package/dist/cjs/tools/ToolController.js +304 -0
  67. package/dist/cjs/tools/ToolController.test.d.ts +1 -0
  68. package/dist/cjs/tools/ToolEnabledGroup.d.ts +6 -0
  69. package/dist/cjs/tools/ToolEnabledGroup.js +13 -0
  70. package/dist/cjs/tools/ToolSwitcherShortcut.d.ts +16 -0
  71. package/dist/cjs/tools/ToolSwitcherShortcut.js +40 -0
  72. package/dist/cjs/tools/ToolbarShortcutHandler.d.ts +12 -0
  73. package/dist/cjs/tools/ToolbarShortcutHandler.js +34 -0
  74. package/dist/cjs/tools/UndoRedoShortcut.d.ts +8 -0
  75. package/dist/cjs/tools/UndoRedoShortcut.js +27 -0
  76. package/dist/cjs/tools/UndoRedoShortcut.test.d.ts +1 -0
  77. package/dist/cjs/tools/keybindings.d.ts +16 -0
  78. package/dist/cjs/tools/keybindings.js +58 -0
  79. package/dist/cjs/tools/lib.d.ts +14 -0
  80. package/dist/cjs/tools/lib.js +36 -0
  81. package/dist/cjs/tools/localization.d.ts +43 -0
  82. package/dist/cjs/tools/localization.js +45 -0
  83. package/dist/cjs/tools/util/StationaryPenDetector.d.ts +25 -0
  84. package/dist/cjs/tools/util/StationaryPenDetector.js +107 -0
  85. package/dist/cjs/tools/util/createMenuOverlay.d.ts +10 -0
  86. package/dist/cjs/tools/util/createMenuOverlay.js +126 -0
  87. package/dist/cjs/tools/util/createMenuOverlay.test.d.ts +1 -0
  88. package/dist/cjs/version.js +1 -1
  89. package/dist/mjs/tools/BaseTool.d.ts +61 -0
  90. package/dist/mjs/tools/BaseTool.mjs +177 -0
  91. package/dist/mjs/tools/Eraser.d.ts +60 -0
  92. package/dist/mjs/tools/Eraser.mjs +292 -0
  93. package/dist/mjs/tools/Eraser.test.d.ts +1 -0
  94. package/dist/mjs/tools/FindTool.d.ts +21 -0
  95. package/dist/mjs/tools/FindTool.mjs +131 -0
  96. package/dist/mjs/tools/FindTool.test.d.ts +1 -0
  97. package/dist/mjs/tools/InputFilter/ContextMenuRecognizer.d.ts +17 -0
  98. package/dist/mjs/tools/InputFilter/ContextMenuRecognizer.mjs +76 -0
  99. package/dist/mjs/tools/InputFilter/ContextMenuRecognizer.test.d.ts +1 -0
  100. package/dist/mjs/tools/InputFilter/FunctionMapper.d.ts +12 -0
  101. package/dist/mjs/tools/InputFilter/FunctionMapper.mjs +15 -0
  102. package/dist/mjs/tools/InputFilter/InputMapper.d.ts +23 -0
  103. package/dist/mjs/tools/InputFilter/InputMapper.mjs +36 -0
  104. package/dist/mjs/tools/InputFilter/InputPipeline.d.ts +15 -0
  105. package/dist/mjs/tools/InputFilter/InputPipeline.mjs +49 -0
  106. package/dist/mjs/tools/InputFilter/InputPipeline.test.d.ts +1 -0
  107. package/dist/mjs/tools/InputFilter/InputStabilizer.d.ts +29 -0
  108. package/dist/mjs/tools/InputFilter/InputStabilizer.mjs +175 -0
  109. package/dist/mjs/tools/InputFilter/StrokeKeyboardControl.d.ts +21 -0
  110. package/dist/mjs/tools/InputFilter/StrokeKeyboardControl.mjs +78 -0
  111. package/dist/mjs/tools/PanZoom.d.ts +125 -0
  112. package/dist/mjs/tools/PanZoom.mjs +510 -0
  113. package/dist/mjs/tools/PanZoom.test.d.ts +1 -0
  114. package/dist/mjs/tools/PasteHandler.d.ts +23 -0
  115. package/dist/mjs/tools/PasteHandler.mjs +103 -0
  116. package/dist/mjs/tools/Pen.d.ts +54 -0
  117. package/dist/mjs/tools/Pen.mjs +306 -0
  118. package/dist/mjs/tools/Pen.test.d.ts +1 -0
  119. package/dist/mjs/tools/PipetteTool.d.ts +28 -0
  120. package/dist/mjs/tools/PipetteTool.mjs +63 -0
  121. package/dist/mjs/tools/ScrollbarTool.d.ts +18 -0
  122. package/dist/mjs/tools/ScrollbarTool.mjs +79 -0
  123. package/dist/mjs/tools/SelectionTool/SelectAllShortcutHandler.d.ts +9 -0
  124. package/dist/mjs/tools/SelectionTool/SelectAllShortcutHandler.mjs +26 -0
  125. package/dist/mjs/tools/SelectionTool/Selection.d.ts +72 -0
  126. package/dist/mjs/tools/SelectionTool/Selection.mjs +606 -0
  127. package/dist/mjs/tools/SelectionTool/SelectionHandle.d.ts +62 -0
  128. package/dist/mjs/tools/SelectionTool/SelectionHandle.mjs +137 -0
  129. package/dist/mjs/tools/SelectionTool/SelectionMenuShortcut.d.ts +32 -0
  130. package/dist/mjs/tools/SelectionTool/SelectionMenuShortcut.mjs +83 -0
  131. package/dist/mjs/tools/SelectionTool/SelectionTool.d.ts +42 -0
  132. package/dist/mjs/tools/SelectionTool/SelectionTool.mjs +493 -0
  133. package/dist/mjs/tools/SelectionTool/SelectionTool.selecting.test.d.ts +1 -0
  134. package/dist/mjs/tools/SelectionTool/SelectionTool.test.d.ts +1 -0
  135. package/dist/mjs/tools/SelectionTool/ToPointerAutoscroller.d.ts +23 -0
  136. package/dist/mjs/tools/SelectionTool/ToPointerAutoscroller.mjs +77 -0
  137. package/dist/mjs/tools/SelectionTool/TransformMode.d.ts +42 -0
  138. package/dist/mjs/tools/SelectionTool/TransformMode.mjs +146 -0
  139. package/dist/mjs/tools/SelectionTool/keybindings.d.ts +15 -0
  140. package/dist/mjs/tools/SelectionTool/keybindings.mjs +32 -0
  141. package/dist/mjs/tools/SelectionTool/types.d.ts +35 -0
  142. package/dist/mjs/tools/SelectionTool/types.mjs +11 -0
  143. package/dist/mjs/tools/SelectionTool/util/makeClipboardErrorHandlers.d.ts +6 -0
  144. package/dist/mjs/tools/SelectionTool/util/makeClipboardErrorHandlers.mjs +45 -0
  145. package/dist/mjs/tools/SelectionTool/util/showSelectionContextMenu.d.ts +5 -0
  146. package/dist/mjs/tools/SelectionTool/util/showSelectionContextMenu.mjs +38 -0
  147. package/dist/mjs/tools/SoundUITool.d.ts +26 -0
  148. package/dist/mjs/tools/SoundUITool.mjs +165 -0
  149. package/dist/mjs/tools/TextTool.d.ts +36 -0
  150. package/dist/mjs/tools/TextTool.mjs +279 -0
  151. package/dist/mjs/tools/TextTool.test.d.ts +1 -0
  152. package/dist/mjs/tools/ToolController.d.ts +73 -0
  153. package/dist/mjs/tools/ToolController.mjs +275 -0
  154. package/dist/mjs/tools/ToolController.test.d.ts +1 -0
  155. package/dist/mjs/tools/ToolEnabledGroup.d.ts +6 -0
  156. package/dist/mjs/tools/ToolEnabledGroup.mjs +10 -0
  157. package/dist/mjs/tools/ToolSwitcherShortcut.d.ts +16 -0
  158. package/dist/mjs/tools/ToolSwitcherShortcut.mjs +34 -0
  159. package/dist/mjs/tools/ToolbarShortcutHandler.d.ts +12 -0
  160. package/dist/mjs/tools/ToolbarShortcutHandler.mjs +28 -0
  161. package/dist/mjs/tools/UndoRedoShortcut.d.ts +8 -0
  162. package/dist/mjs/tools/UndoRedoShortcut.mjs +21 -0
  163. package/dist/mjs/tools/UndoRedoShortcut.test.d.ts +1 -0
  164. package/dist/mjs/tools/keybindings.d.ts +16 -0
  165. package/dist/mjs/tools/keybindings.mjs +38 -0
  166. package/dist/mjs/tools/lib.d.ts +14 -0
  167. package/dist/mjs/tools/lib.mjs +14 -0
  168. package/dist/mjs/tools/localization.d.ts +43 -0
  169. package/dist/mjs/tools/localization.mjs +42 -0
  170. package/dist/mjs/tools/util/StationaryPenDetector.d.ts +25 -0
  171. package/dist/mjs/tools/util/StationaryPenDetector.mjs +103 -0
  172. package/dist/mjs/tools/util/createMenuOverlay.d.ts +10 -0
  173. package/dist/mjs/tools/util/createMenuOverlay.mjs +121 -0
  174. package/dist/mjs/tools/util/createMenuOverlay.test.d.ts +1 -0
  175. package/dist/mjs/version.mjs +1 -1
  176. package/package.json +4 -4
  177. package/src/tools/FindTool.css +7 -0
  178. package/src/tools/ScrollbarTool.scss +57 -0
  179. package/src/tools/SelectionTool/SelectionTool.scss +165 -0
  180. package/src/tools/SelectionTool/util/makeClipboardErrorHandlers.scss +15 -0
  181. package/src/tools/SoundUITool.scss +22 -0
  182. package/src/tools/tools.scss +6 -0
  183. package/src/tools/util/createMenuOverlay.scss +67 -0
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ // Displays a find dialog that allows the user to search for and focus text.
3
+ //
4
+ // @packageDocumentation
5
+ var __importDefault = (this && this.__importDefault) || function (mod) {
6
+ return (mod && mod.__esModule) ? mod : { "default": mod };
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ const TextComponent_1 = __importDefault(require("../components/TextComponent"));
10
+ const ImageComponent_1 = __importDefault(require("../components/ImageComponent"));
11
+ const BaseTool_1 = __importDefault(require("./BaseTool"));
12
+ const keybindings_1 = require("./keybindings");
13
+ const cssPrefix = 'find-tool';
14
+ class FindTool extends BaseTool_1.default {
15
+ constructor(editor) {
16
+ super(editor.notifier, editor.localization.findLabel);
17
+ this.editor = editor;
18
+ this.currentMatchIdx = 0;
19
+ this.overlay = document.createElement('div');
20
+ this.fillOverlay();
21
+ editor.createHTMLOverlay(this.overlay);
22
+ this.overlay.style.display = 'none';
23
+ this.overlay.classList.add(`${cssPrefix}-overlay`);
24
+ }
25
+ canReceiveInputInReadOnlyEditor() {
26
+ return true;
27
+ }
28
+ getMatches(searchFor) {
29
+ const lowerSearchFor = searchFor.toLocaleLowerCase();
30
+ const matchingComponents = this.editor.image.getAllElements().filter(component => {
31
+ let text = '';
32
+ if (component instanceof TextComponent_1.default) {
33
+ text = component.getText();
34
+ }
35
+ else if (component instanceof ImageComponent_1.default) {
36
+ text = component.getAltText() ?? '';
37
+ }
38
+ else {
39
+ return false;
40
+ }
41
+ const hasLowercaseMatch = text.toLocaleLowerCase().indexOf(lowerSearchFor) !== -1;
42
+ const hasSameCaseMatch = text.indexOf(searchFor) !== -1;
43
+ return hasLowercaseMatch || hasSameCaseMatch;
44
+ });
45
+ return matchingComponents.map(match => match.getBBox());
46
+ }
47
+ focusCurrentMatch() {
48
+ const matches = this.getMatches(this.searchInput.value);
49
+ let matchIdx = this.currentMatchIdx % matches.length;
50
+ if (matchIdx < 0) {
51
+ matchIdx = matches.length + matchIdx;
52
+ }
53
+ if (matchIdx < matches.length) {
54
+ const undoable = false;
55
+ void this.editor.dispatch(this.editor.viewport.zoomTo(matches[matchIdx], true, true), undoable);
56
+ this.editor.announceForAccessibility(this.editor.localization.focusedFoundText(matchIdx + 1, matches.length));
57
+ }
58
+ }
59
+ toNextMatch() {
60
+ this.currentMatchIdx++;
61
+ this.focusCurrentMatch();
62
+ }
63
+ toPrevMatch() {
64
+ this.currentMatchIdx--;
65
+ this.focusCurrentMatch();
66
+ }
67
+ fillOverlay() {
68
+ const label = document.createElement('label');
69
+ this.searchInput = document.createElement('input');
70
+ const nextBtn = document.createElement('button');
71
+ const closeBtn = document.createElement('button');
72
+ // Math.random() ensures that the ID is unique (to allow us to refer to it
73
+ // with an htmlFor).
74
+ this.searchInput.setAttribute('id', `${cssPrefix}-searchInput-${Math.random()}`);
75
+ label.htmlFor = this.searchInput.getAttribute('id');
76
+ label.innerText = this.editor.localization.findLabel;
77
+ nextBtn.innerText = this.editor.localization.toNextMatch;
78
+ closeBtn.innerText = this.editor.localization.closeDialog;
79
+ this.searchInput.onkeydown = (ev) => {
80
+ if (ev.key === 'Enter') {
81
+ if (ev.shiftKey) {
82
+ this.toPrevMatch();
83
+ }
84
+ else {
85
+ this.toNextMatch();
86
+ }
87
+ }
88
+ else if (ev.key === 'Escape') {
89
+ this.setVisible(false);
90
+ }
91
+ else if (this.editor.shortcuts.matchesShortcut(keybindings_1.toggleFindVisibleShortcutId, ev)) {
92
+ ev.preventDefault();
93
+ this.toggleVisible();
94
+ }
95
+ };
96
+ nextBtn.onclick = () => {
97
+ this.toNextMatch();
98
+ };
99
+ closeBtn.onclick = () => {
100
+ this.setVisible(false);
101
+ };
102
+ this.overlay.replaceChildren(label, this.searchInput, nextBtn, closeBtn);
103
+ }
104
+ isVisible() {
105
+ return this.overlay.style.display !== 'none';
106
+ }
107
+ setVisible(visible) {
108
+ if (visible !== this.isVisible()) {
109
+ this.overlay.style.display = visible ? 'block' : 'none';
110
+ if (visible) {
111
+ this.searchInput.focus();
112
+ this.editor.announceForAccessibility(this.editor.localization.findDialogShown);
113
+ }
114
+ else {
115
+ this.editor.focus();
116
+ this.editor.announceForAccessibility(this.editor.localization.findDialogHidden);
117
+ }
118
+ }
119
+ }
120
+ toggleVisible() {
121
+ this.setVisible(!this.isVisible());
122
+ }
123
+ onKeyPress(event) {
124
+ if (this.editor.shortcuts.matchesShortcut(keybindings_1.toggleFindVisibleShortcutId, event)) {
125
+ this.toggleVisible();
126
+ return true;
127
+ }
128
+ return false;
129
+ }
130
+ setEnabled(enabled) {
131
+ super.setEnabled(enabled);
132
+ if (this.isEnabled()) {
133
+ this.setVisible(false);
134
+ }
135
+ }
136
+ }
137
+ exports.default = FindTool;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ import { InputEvt } from '../../inputEvents';
2
+ import InputMapper from './InputMapper';
3
+ export default class ContextMenuRecognizer extends InputMapper {
4
+ private canShowContextMenu;
5
+ private contextMenuTriggerPointer;
6
+ private contextMenuStartPoint;
7
+ private stationaryDetector;
8
+ private clickTolerance;
9
+ constructor();
10
+ /**
11
+ * In general, only certain events (i.e. touchscreens) are expected to be able to
12
+ * create long-press menus. This method checks whether `event` was generated by
13
+ * one such device.
14
+ */
15
+ private canMakeLongPressMenuEvent;
16
+ onEvent(event: InputEvt): boolean;
17
+ }
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const inputEvents_1 = require("../../inputEvents");
30
+ const InputMapper_1 = __importDefault(require("./InputMapper"));
31
+ const StationaryPenDetector_1 = __importStar(require("../util/StationaryPenDetector"));
32
+ const Pointer_1 = require("../../Pointer");
33
+ class ContextMenuRecognizer extends InputMapper_1.default {
34
+ constructor() {
35
+ super();
36
+ this.canShowContextMenu = false;
37
+ this.stationaryDetector = null;
38
+ this.clickTolerance = 12;
39
+ }
40
+ /**
41
+ * In general, only certain events (i.e. touchscreens) are expected to be able to
42
+ * create long-press menus. This method checks whether `event` was generated by
43
+ * one such device.
44
+ */
45
+ canMakeLongPressMenuEvent(event) {
46
+ const allowedDevices = [
47
+ Pointer_1.PointerDevice.Touch,
48
+ ];
49
+ return event.allPointers.length === 1 && allowedDevices.includes(event.current.device);
50
+ }
51
+ onEvent(event) {
52
+ const sendContextMenuEvent = () => {
53
+ if (!(0, inputEvents_1.isPointerEvt)(event))
54
+ return false;
55
+ if (this.canShowContextMenu) {
56
+ const eventHandled = this.emit({
57
+ kind: inputEvents_1.InputEvtType.ContextMenu,
58
+ screenPos: event.current.screenPos,
59
+ canvasPos: event.current.canvasPos,
60
+ });
61
+ if (eventHandled) {
62
+ this.emit({
63
+ kind: inputEvents_1.InputEvtType.GestureCancelEvt,
64
+ });
65
+ return true;
66
+ }
67
+ }
68
+ return false;
69
+ };
70
+ if (event.kind === inputEvents_1.InputEvtType.PointerDownEvt) {
71
+ if (event.allPointers.length === 1) {
72
+ this.canShowContextMenu = true;
73
+ this.contextMenuTriggerPointer = event.current;
74
+ this.contextMenuStartPoint = event.current.screenPos;
75
+ if (this.canMakeLongPressMenuEvent(event)) {
76
+ this.stationaryDetector = new StationaryPenDetector_1.default(event.current, StationaryPenDetector_1.defaultStationaryDetectionConfig, sendContextMenuEvent);
77
+ }
78
+ }
79
+ else {
80
+ this.canShowContextMenu = false;
81
+ }
82
+ }
83
+ else if (event.kind === inputEvents_1.InputEvtType.PointerMoveEvt) {
84
+ if (this.canShowContextMenu) {
85
+ this.stationaryDetector?.onPointerMove(event.current);
86
+ // Only clicks/stationary long presses can create context menu events.
87
+ const deltaPosition = event.current.screenPos.minus(this.contextMenuStartPoint);
88
+ const threshold = this.clickTolerance;
89
+ if (deltaPosition.length() > threshold) {
90
+ this.canShowContextMenu = false;
91
+ }
92
+ }
93
+ }
94
+ else if (event.kind === inputEvents_1.InputEvtType.PointerUpEvt) {
95
+ this.stationaryDetector?.destroy();
96
+ if (this.contextMenuTriggerPointer?.id === event.current.id &&
97
+ this.contextMenuTriggerPointer.device === Pointer_1.PointerDevice.RightButtonMouse &&
98
+ sendContextMenuEvent()) {
99
+ return true;
100
+ }
101
+ }
102
+ return this.emit(event);
103
+ }
104
+ }
105
+ exports.default = ContextMenuRecognizer;
@@ -0,0 +1,12 @@
1
+ import { InputEvt } from '../../inputEvents';
2
+ import InputMapper from './InputMapper';
3
+ /**
4
+ * An `InputMapper` that applies a function to all events it receives.
5
+ *
6
+ * Useful for automated testing.
7
+ */
8
+ export default class FunctionMapper extends InputMapper {
9
+ private fn;
10
+ constructor(fn: (event: InputEvt) => InputEvt);
11
+ onEvent(event: InputEvt): boolean;
12
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const InputMapper_1 = __importDefault(require("./InputMapper"));
7
+ /**
8
+ * An `InputMapper` that applies a function to all events it receives.
9
+ *
10
+ * Useful for automated testing.
11
+ */
12
+ class FunctionMapper extends InputMapper_1.default {
13
+ constructor(fn) {
14
+ super();
15
+ this.fn = fn;
16
+ }
17
+ onEvent(event) {
18
+ return this.emit(this.fn(event));
19
+ }
20
+ }
21
+ exports.default = FunctionMapper;
@@ -0,0 +1,23 @@
1
+ import { InputEvt } from '../../inputEvents';
2
+ type OnEventCallback = (event: InputEvt) => boolean;
3
+ export interface InputEventListener {
4
+ onEvent: OnEventCallback;
5
+ }
6
+ /**
7
+ * Accepts input events and emits input events.
8
+ */
9
+ export default abstract class InputMapper implements InputEventListener {
10
+ #private;
11
+ constructor();
12
+ setEmitListener(listener: InputEventListener | OnEventCallback | null): void;
13
+ protected emit(event: InputEvt): boolean;
14
+ /**
15
+ * @returns true if the given `event` should be considered "handled" by the app and thus not
16
+ * forwarded to other targets. For example, returning "true" for a touchpad pinch event prevents
17
+ * the pinch event from zooming the webpage.
18
+ *
19
+ * Generally, this should return the result of calling `this.emit` with some event.
20
+ */
21
+ abstract onEvent(event: InputEvt): boolean;
22
+ }
23
+ export {};
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _InputMapper_listener;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ /**
16
+ * Accepts input events and emits input events.
17
+ */
18
+ class InputMapper {
19
+ constructor() {
20
+ _InputMapper_listener.set(this, null);
21
+ }
22
+ // @internal
23
+ setEmitListener(listener) {
24
+ if (listener && typeof (listener) === 'object') {
25
+ __classPrivateFieldSet(this, _InputMapper_listener, (event) => {
26
+ return listener.onEvent(event) ?? false;
27
+ }, "f");
28
+ }
29
+ else {
30
+ __classPrivateFieldSet(this, _InputMapper_listener, listener, "f");
31
+ }
32
+ }
33
+ emit(event) {
34
+ return __classPrivateFieldGet(this, _InputMapper_listener, "f")?.call(this, event) ?? false;
35
+ }
36
+ }
37
+ _InputMapper_listener = new WeakMap();
38
+ exports.default = InputMapper;
@@ -0,0 +1,15 @@
1
+ import { InputEvt } from '../../inputEvents';
2
+ import InputMapper from './InputMapper';
3
+ /**
4
+ * The composition of multiple `InputMapper`s.
5
+ */
6
+ export default class InputPipeline extends InputMapper {
7
+ #private;
8
+ onEvent(event: InputEvt): boolean;
9
+ /**
10
+ * Adds a new `InputMapper` to the *tail* of this pipeline.
11
+ * Note that an instance of an `InputMapper` can only be used in a single
12
+ * pipeline.
13
+ */
14
+ addToTail(mapper: InputMapper): void;
15
+ }
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
8
+ if (kind === "m") throw new TypeError("Private method is not writable");
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
11
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ var _InputPipeline_head, _InputPipeline_tail;
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ const InputMapper_1 = __importDefault(require("./InputMapper"));
19
+ /**
20
+ * The composition of multiple `InputMapper`s.
21
+ */
22
+ class InputPipeline extends InputMapper_1.default {
23
+ constructor() {
24
+ super(...arguments);
25
+ _InputPipeline_head.set(this, null);
26
+ _InputPipeline_tail.set(this, null);
27
+ }
28
+ onEvent(event) {
29
+ if (__classPrivateFieldGet(this, _InputPipeline_head, "f") === null) {
30
+ return this.emit(event);
31
+ }
32
+ else {
33
+ return __classPrivateFieldGet(this, _InputPipeline_head, "f").onEvent(event);
34
+ }
35
+ }
36
+ /**
37
+ * Adds a new `InputMapper` to the *tail* of this pipeline.
38
+ * Note that an instance of an `InputMapper` can only be used in a single
39
+ * pipeline.
40
+ */
41
+ addToTail(mapper) {
42
+ if (!__classPrivateFieldGet(this, _InputPipeline_tail, "f")) {
43
+ __classPrivateFieldSet(this, _InputPipeline_head, mapper, "f");
44
+ __classPrivateFieldSet(this, _InputPipeline_tail, __classPrivateFieldGet(this, _InputPipeline_head, "f"), "f");
45
+ }
46
+ else {
47
+ __classPrivateFieldGet(this, _InputPipeline_tail, "f").setEmitListener(mapper);
48
+ __classPrivateFieldSet(this, _InputPipeline_tail, mapper, "f");
49
+ }
50
+ __classPrivateFieldGet(this, _InputPipeline_tail, "f").setEmitListener(event => this.emit(event));
51
+ }
52
+ }
53
+ _InputPipeline_head = new WeakMap(), _InputPipeline_tail = new WeakMap();
54
+ exports.default = InputPipeline;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,29 @@
1
+ import { InputEvt } from '../../inputEvents';
2
+ import InputMapper from './InputMapper';
3
+ import Viewport from '../../Viewport';
4
+ import Editor from '../../Editor';
5
+ declare enum StabilizerType {
6
+ IntertialStabilizer = 0
7
+ }
8
+ interface InputStabilizerOptions {
9
+ kind: StabilizerType.IntertialStabilizer;
10
+ mass: number;
11
+ springConstant: number;
12
+ frictionCoefficient: number;
13
+ maxPointDist: number;
14
+ minSimilarityToFinalize: number;
15
+ inertiaFraction: number;
16
+ velocityDecayFactor: number;
17
+ }
18
+ export default class InputStabilizer extends InputMapper {
19
+ private viewport;
20
+ private readonly options;
21
+ private stabilizer;
22
+ private lastPointerEvent;
23
+ constructor(viewport: Viewport, options?: InputStabilizerOptions);
24
+ private mapPointerEvent;
25
+ private emitPointerMove;
26
+ onEvent(event: InputEvt): boolean;
27
+ static fromEditor(editor: Editor): InputStabilizer;
28
+ }
29
+ export {};
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const inputEvents_1 = require("../../inputEvents");
7
+ const InputMapper_1 = __importDefault(require("./InputMapper"));
8
+ const math_1 = require("@js-draw/math");
9
+ const untilNextAnimationFrame_1 = __importDefault(require("../../util/untilNextAnimationFrame"));
10
+ var StabilizerType;
11
+ (function (StabilizerType) {
12
+ StabilizerType[StabilizerType["IntertialStabilizer"] = 0] = "IntertialStabilizer";
13
+ })(StabilizerType || (StabilizerType = {}));
14
+ const defaultOptions = {
15
+ kind: StabilizerType.IntertialStabilizer,
16
+ mass: 0.4, // kg
17
+ springConstant: 100.0, // N/m
18
+ frictionCoefficient: 0.28,
19
+ maxPointDist: 10, // screen units
20
+ inertiaFraction: 0.75,
21
+ minSimilarityToFinalize: 0.0,
22
+ velocityDecayFactor: 0.1,
23
+ };
24
+ // Stabilizes input for a single cursor
25
+ class StylusInputStabilizer {
26
+ constructor(
27
+ // The initial starting point of the pointer.
28
+ start,
29
+ // Emits a pointer motion event, returns true if the event was handled.
30
+ updatePointer, options) {
31
+ this.updatePointer = updatePointer;
32
+ this.options = options;
33
+ this.runLoop = true;
34
+ this.lastUpdateTime = 0;
35
+ this.velocity = math_1.Vec2.zero;
36
+ this.strokePoint = start;
37
+ this.targetPoint = start;
38
+ this.targetInterval = 10; // ms
39
+ void this.loop();
40
+ }
41
+ async loop() {
42
+ this.lastUpdateTime = performance.now();
43
+ while (this.runLoop) {
44
+ this.update(false);
45
+ await (0, untilNextAnimationFrame_1.default)();
46
+ }
47
+ }
48
+ setTarget(point) {
49
+ this.targetPoint = point;
50
+ }
51
+ getNextVelocity(deltaTimeMs) {
52
+ const toTarget = this.targetPoint.minus(this.strokePoint);
53
+ const springForce = toTarget.times(this.options.springConstant);
54
+ const gravityAccel = 10;
55
+ const normalForceMagnitude = this.options.mass * gravityAccel;
56
+ const frictionForce = this.velocity.normalizedOrZero().times(-this.options.frictionCoefficient * normalForceMagnitude);
57
+ const acceleration = (springForce.plus(frictionForce)).times(1 / this.options.mass);
58
+ const decayFactor = this.options.velocityDecayFactor;
59
+ const springVelocity = this.velocity.times(1 - decayFactor).plus(acceleration.times(deltaTimeMs / 1000));
60
+ // An alternate velocity that goes directly towards the target.
61
+ const toTargetVelocity = toTarget.normalizedOrZero().times(springVelocity.length());
62
+ return toTargetVelocity.lerp(springVelocity, this.options.inertiaFraction);
63
+ }
64
+ update(force) {
65
+ const nowTime = performance.now();
66
+ const deltaTime = nowTime - this.lastUpdateTime;
67
+ const reachedTarget = this.strokePoint.eq(this.targetPoint);
68
+ if (deltaTime > this.targetInterval || force) {
69
+ if (!reachedTarget) {
70
+ let velocity;
71
+ let deltaX;
72
+ let parts = 1;
73
+ do {
74
+ velocity = this.getNextVelocity(deltaTime / parts);
75
+ deltaX = velocity.times(deltaTime / 1000);
76
+ parts++;
77
+ } while (deltaX.magnitude() > this.options.maxPointDist && parts < 10);
78
+ for (let i = 0; i < parts; i++) {
79
+ this.velocity = this.getNextVelocity(deltaTime / parts);
80
+ deltaX = this.velocity.times(deltaTime / 1000);
81
+ this.strokePoint = this.strokePoint.plus(deltaX);
82
+ // Allows the last updatePointer to be returned.
83
+ if (i < parts - 1) {
84
+ this.updatePointer(this.strokePoint, nowTime);
85
+ }
86
+ }
87
+ }
88
+ // Even if we have reached the target, ensure that lastUpdateTime is updated
89
+ // (prevent large deltaTime).
90
+ this.lastUpdateTime = nowTime;
91
+ if (force || !reachedTarget) {
92
+ return this.updatePointer(this.strokePoint, nowTime);
93
+ }
94
+ }
95
+ return false;
96
+ }
97
+ /** Finalizes the current stroke. */
98
+ finish() {
99
+ this.runLoop = false;
100
+ const toTarget = this.targetPoint.minus(this.strokePoint);
101
+ if (this.velocity.dot(toTarget) > this.options.minSimilarityToFinalize) {
102
+ // Connect the stroke to its end point
103
+ this.updatePointer(this.targetPoint, performance.now());
104
+ }
105
+ }
106
+ cancel() {
107
+ this.runLoop = false;
108
+ }
109
+ }
110
+ class InputStabilizer extends InputMapper_1.default {
111
+ constructor(viewport, options = defaultOptions) {
112
+ super();
113
+ this.viewport = viewport;
114
+ this.options = options;
115
+ this.stabilizer = null;
116
+ this.lastPointerEvent = null;
117
+ }
118
+ mapPointerEvent(event) {
119
+ // Don't store the last pointer event for use with pressure/button data --
120
+ // this information can be very different for a pointerup event.
121
+ if ((0, inputEvents_1.isPointerEvt)(event) && event.kind !== inputEvents_1.InputEvtType.PointerUpEvt) {
122
+ this.lastPointerEvent = event;
123
+ }
124
+ // Only apply smoothing if there is a single pointer.
125
+ if (event.kind === inputEvents_1.InputEvtType.GestureCancelEvt || event.allPointers.length > 1 || this.stabilizer === null) {
126
+ return this.emit(event);
127
+ }
128
+ this.stabilizer.setTarget(event.current.screenPos);
129
+ if (event.kind === inputEvents_1.InputEvtType.PointerMoveEvt) {
130
+ return this.stabilizer.update(true);
131
+ }
132
+ else if (event.kind === inputEvents_1.InputEvtType.PointerUpEvt) {
133
+ this.stabilizer.finish();
134
+ return this.emit(event);
135
+ }
136
+ else {
137
+ return this.emit(event);
138
+ }
139
+ }
140
+ // Assumes that there is exactly one pointer that is currently down.
141
+ emitPointerMove(screenPoint, timeStamp) {
142
+ if (!this.lastPointerEvent) {
143
+ return false;
144
+ }
145
+ const pointer = this.lastPointerEvent.current
146
+ .withScreenPosition(screenPoint, this.viewport)
147
+ .withTimestamp(timeStamp);
148
+ const event = {
149
+ kind: inputEvents_1.InputEvtType.PointerMoveEvt,
150
+ current: pointer,
151
+ allPointers: [pointer],
152
+ };
153
+ const handled = this.emit(event);
154
+ return handled;
155
+ }
156
+ onEvent(event) {
157
+ if ((0, inputEvents_1.isPointerEvt)(event) || event.kind === inputEvents_1.InputEvtType.GestureCancelEvt) {
158
+ if (event.kind === inputEvents_1.InputEvtType.PointerDownEvt) {
159
+ if (this.stabilizer === null) {
160
+ this.stabilizer = new StylusInputStabilizer(event.current.screenPos, (screenPoint, timeStamp) => this.emitPointerMove(screenPoint, timeStamp), this.options);
161
+ }
162
+ else if (event.allPointers.length > 1) {
163
+ // Do not attempt to stabilize multiple pointers.
164
+ this.stabilizer.cancel();
165
+ this.stabilizer = null;
166
+ }
167
+ }
168
+ const handled = this.mapPointerEvent(event);
169
+ if (event.kind === inputEvents_1.InputEvtType.PointerUpEvt || event.kind === inputEvents_1.InputEvtType.GestureCancelEvt) {
170
+ this.stabilizer?.cancel();
171
+ this.stabilizer = null;
172
+ }
173
+ return handled;
174
+ }
175
+ return this.emit(event);
176
+ }
177
+ static fromEditor(editor) {
178
+ return new InputStabilizer(editor.viewport);
179
+ }
180
+ }
181
+ exports.default = InputStabilizer;
@@ -0,0 +1,21 @@
1
+ import { InputEvt } from '../../inputEvents';
2
+ import InputMapper from './InputMapper';
3
+ import KeyboardShortcutManager from '../../shortcuts/KeyboardShortcutManager';
4
+ import Viewport from '../../Viewport';
5
+ import Editor from '../../Editor';
6
+ /**
7
+ * Provides keyboard shortcuts that provide some amount of control over
8
+ * drawing (e.g. snap to grid, plane lock).
9
+ */
10
+ export default class StrokeKeyboardControl extends InputMapper {
11
+ private shortcuts;
12
+ private viewport;
13
+ private snapToGridEnabled;
14
+ private angleLockEnabled;
15
+ private startPointCanvas;
16
+ constructor(shortcuts: KeyboardShortcutManager, viewport: Viewport);
17
+ private xyAxesSnap;
18
+ private mapPointerEvent;
19
+ onEvent(event: InputEvt): boolean;
20
+ static fromEditor(editor: Editor): StrokeKeyboardControl;
21
+ }