js-draw 1.14.0 → 1.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. package/dist/Editor.css +285 -1
  2. package/dist/bundle.js +2 -2
  3. package/dist/bundledStyles.js +1 -1
  4. package/dist/cjs/Editor.js +5 -0
  5. package/dist/cjs/components/util/StrokeSmoother.js +11 -4
  6. package/dist/cjs/rendering/caching/CacheRecordManager.js +1 -1
  7. package/dist/cjs/testing/sendHtmlSwipe.d.ts +4 -0
  8. package/dist/cjs/testing/sendHtmlSwipe.js +14 -0
  9. package/dist/cjs/toolbar/EdgeToolbar.d.ts +1 -0
  10. package/dist/cjs/toolbar/EdgeToolbar.js +30 -110
  11. package/dist/cjs/toolbar/IconProvider.d.ts +1 -0
  12. package/dist/cjs/toolbar/IconProvider.js +27 -0
  13. package/dist/cjs/toolbar/localization.d.ts +28 -1
  14. package/dist/cjs/toolbar/localization.js +30 -1
  15. package/dist/cjs/toolbar/utils/HelpDisplay.d.ts +37 -0
  16. package/dist/cjs/toolbar/utils/HelpDisplay.js +442 -0
  17. package/dist/cjs/toolbar/utils/HelpDisplay.test.d.ts +1 -0
  18. package/dist/cjs/toolbar/utils/localization.d.ts +9 -0
  19. package/dist/cjs/toolbar/utils/localization.js +11 -0
  20. package/dist/cjs/toolbar/utils/makeDraggable.d.ts +16 -0
  21. package/dist/cjs/toolbar/utils/makeDraggable.js +130 -0
  22. package/dist/cjs/toolbar/widgets/ActionButtonWidget.d.ts +7 -0
  23. package/dist/cjs/toolbar/widgets/ActionButtonWidget.js +14 -2
  24. package/dist/cjs/toolbar/widgets/BaseWidget.d.ts +8 -1
  25. package/dist/cjs/toolbar/widgets/BaseWidget.js +25 -3
  26. package/dist/cjs/toolbar/widgets/DocumentPropertiesWidget.d.ts +3 -1
  27. package/dist/cjs/toolbar/widgets/DocumentPropertiesWidget.js +19 -4
  28. package/dist/cjs/toolbar/widgets/HandToolWidget.d.ts +3 -1
  29. package/dist/cjs/toolbar/widgets/HandToolWidget.js +19 -7
  30. package/dist/cjs/toolbar/widgets/InsertImageWidget.js +1 -0
  31. package/dist/cjs/toolbar/widgets/PenToolWidget.d.ts +4 -2
  32. package/dist/cjs/toolbar/widgets/PenToolWidget.js +27 -8
  33. package/dist/cjs/toolbar/widgets/SelectionToolWidget.d.ts +3 -1
  34. package/dist/cjs/toolbar/widgets/SelectionToolWidget.js +19 -5
  35. package/dist/cjs/toolbar/widgets/components/makeColorInput.d.ts +2 -0
  36. package/dist/cjs/toolbar/widgets/components/makeColorInput.js +17 -7
  37. package/dist/cjs/toolbar/widgets/components/makeGridSelector.d.ts +6 -0
  38. package/dist/cjs/toolbar/widgets/components/makeGridSelector.js +3 -0
  39. package/dist/cjs/tools/FindTool.js +18 -5
  40. package/dist/cjs/util/addLongPressOrHoverCssClasses.d.ts +3 -1
  41. package/dist/cjs/util/addLongPressOrHoverCssClasses.js +2 -1
  42. package/dist/cjs/util/cloneElementWithStyles.d.ts +6 -0
  43. package/dist/cjs/util/cloneElementWithStyles.js +32 -0
  44. package/dist/cjs/version.js +1 -1
  45. package/dist/mjs/Editor.mjs +5 -0
  46. package/dist/mjs/components/util/StrokeSmoother.mjs +11 -4
  47. package/dist/mjs/rendering/caching/CacheRecordManager.mjs +1 -1
  48. package/dist/mjs/testing/sendHtmlSwipe.d.ts +4 -0
  49. package/dist/mjs/testing/sendHtmlSwipe.mjs +12 -0
  50. package/dist/mjs/toolbar/EdgeToolbar.d.ts +1 -0
  51. package/dist/mjs/toolbar/EdgeToolbar.mjs +30 -110
  52. package/dist/mjs/toolbar/IconProvider.d.ts +1 -0
  53. package/dist/mjs/toolbar/IconProvider.mjs +27 -0
  54. package/dist/mjs/toolbar/localization.d.ts +28 -1
  55. package/dist/mjs/toolbar/localization.mjs +30 -1
  56. package/dist/mjs/toolbar/utils/HelpDisplay.d.ts +37 -0
  57. package/dist/mjs/toolbar/utils/HelpDisplay.mjs +437 -0
  58. package/dist/mjs/toolbar/utils/HelpDisplay.test.d.ts +1 -0
  59. package/dist/mjs/toolbar/utils/localization.d.ts +9 -0
  60. package/dist/mjs/toolbar/utils/localization.mjs +8 -0
  61. package/dist/mjs/toolbar/utils/makeDraggable.d.ts +16 -0
  62. package/dist/mjs/toolbar/utils/makeDraggable.mjs +128 -0
  63. package/dist/mjs/toolbar/widgets/ActionButtonWidget.d.ts +7 -0
  64. package/dist/mjs/toolbar/widgets/ActionButtonWidget.mjs +14 -2
  65. package/dist/mjs/toolbar/widgets/BaseWidget.d.ts +8 -1
  66. package/dist/mjs/toolbar/widgets/BaseWidget.mjs +25 -3
  67. package/dist/mjs/toolbar/widgets/DocumentPropertiesWidget.d.ts +3 -1
  68. package/dist/mjs/toolbar/widgets/DocumentPropertiesWidget.mjs +19 -4
  69. package/dist/mjs/toolbar/widgets/HandToolWidget.d.ts +3 -1
  70. package/dist/mjs/toolbar/widgets/HandToolWidget.mjs +19 -7
  71. package/dist/mjs/toolbar/widgets/InsertImageWidget.mjs +1 -0
  72. package/dist/mjs/toolbar/widgets/PenToolWidget.d.ts +4 -2
  73. package/dist/mjs/toolbar/widgets/PenToolWidget.mjs +27 -8
  74. package/dist/mjs/toolbar/widgets/SelectionToolWidget.d.ts +3 -1
  75. package/dist/mjs/toolbar/widgets/SelectionToolWidget.mjs +19 -5
  76. package/dist/mjs/toolbar/widgets/components/makeColorInput.d.ts +2 -0
  77. package/dist/mjs/toolbar/widgets/components/makeColorInput.mjs +17 -7
  78. package/dist/mjs/toolbar/widgets/components/makeGridSelector.d.ts +6 -0
  79. package/dist/mjs/toolbar/widgets/components/makeGridSelector.mjs +3 -0
  80. package/dist/mjs/tools/FindTool.mjs +18 -5
  81. package/dist/mjs/util/addLongPressOrHoverCssClasses.d.ts +3 -1
  82. package/dist/mjs/util/addLongPressOrHoverCssClasses.mjs +2 -1
  83. package/dist/mjs/util/cloneElementWithStyles.d.ts +6 -0
  84. package/dist/mjs/util/cloneElementWithStyles.mjs +30 -0
  85. package/dist/mjs/version.mjs +1 -1
  86. package/package.json +2 -2
  87. package/src/toolbar/EdgeToolbar.scss +23 -2
  88. package/src/toolbar/toolbar.scss +2 -0
  89. package/src/toolbar/utils/HelpDisplay.scss +315 -0
  90. package/src/toolbar/widgets/components/makeColorInput.scss +7 -0
@@ -18,7 +18,7 @@ const makeFormatMenu = (editor, selectionTool, localizationTable) => {
18
18
  container.classList.add('selection-format-menu', `${constants_1.toolbarCSSPrefix}spacedList`, `${constants_1.toolbarCSSPrefix}indentedList`);
19
19
  const colorRow = document.createElement('div');
20
20
  const colorLabel = document.createElement('label');
21
- const { input: colorInput, container: colorInputContainer, setValue: setColorInputValue } = (0, makeColorInput_1.default)(editor, color => {
21
+ const colorInputControl = (0, makeColorInput_1.default)(editor, color => {
22
22
  const selection = selectionTool.getSelection();
23
23
  if (selection) {
24
24
  const updateStyleCommands = [];
@@ -31,6 +31,7 @@ const makeFormatMenu = (editor, selectionTool, localizationTable) => {
31
31
  editor.dispatch(unitedCommand);
32
32
  }
33
33
  });
34
+ const { input: colorInput, container: colorInputContainer } = colorInputControl;
34
35
  colorLabel.innerText = localizationTable.colorLabel;
35
36
  const update = () => {
36
37
  const selection = selectionTool.getSelection();
@@ -46,12 +47,12 @@ const makeFormatMenu = (editor, selectionTool, localizationTable) => {
46
47
  }
47
48
  }
48
49
  }
49
- setColorInputValue(math_1.Color4.average(colors));
50
+ colorInputControl.setValue(math_1.Color4.average(colors));
50
51
  }
51
52
  else {
52
53
  colorInput.disabled = true;
53
54
  container.classList.add('disabled');
54
- setColorInputValue(math_1.Color4.transparent);
55
+ colorInputControl.setValue(math_1.Color4.transparent);
55
56
  }
56
57
  };
57
58
  colorRow.replaceChildren(colorLabel, colorInputContainer);
@@ -61,6 +62,10 @@ const makeFormatMenu = (editor, selectionTool, localizationTable) => {
61
62
  parent.appendChild(container);
62
63
  },
63
64
  update,
65
+ registerHelpText: (helpDisplay) => {
66
+ helpDisplay.registerTextHelpForElement(colorRow, localizationTable.selectionDropdown__changeColorHelpText);
67
+ colorInputControl.registerWithHelpTextDisplay(helpDisplay);
68
+ },
64
69
  };
65
70
  };
66
71
  class SelectionToolWidget extends BaseToolWidget_1.default {
@@ -71,16 +76,19 @@ class SelectionToolWidget extends BaseToolWidget_1.default {
71
76
  const resizeButton = new ActionButtonWidget_1.default(editor, 'resize-btn', () => editor.icons.makeResizeImageToSelectionIcon(), this.localizationTable.resizeImageToSelection, () => {
72
77
  this.resizeImageToSelection();
73
78
  }, localization);
79
+ resizeButton.setHelpText(this.localizationTable.selectionDropdown__resizeToHelpText);
74
80
  const deleteButton = new ActionButtonWidget_1.default(editor, 'delete-btn', () => editor.icons.makeDeleteSelectionIcon(), this.localizationTable.deleteSelection, () => {
75
81
  const selection = this.tool.getSelection();
76
82
  this.editor.dispatch(selection.deleteSelectedObjects());
77
83
  this.tool.clearSelection();
78
84
  }, localization);
85
+ deleteButton.setHelpText(this.localizationTable.selectionDropdown__deleteHelpText);
79
86
  const duplicateButton = new ActionButtonWidget_1.default(editor, 'duplicate-btn', () => editor.icons.makeDuplicateSelectionIcon(), this.localizationTable.duplicateSelection, async () => {
80
87
  const selection = this.tool.getSelection();
81
88
  this.editor.dispatch(await selection.duplicateSelectedObjects());
82
89
  this.setDropdownVisible(false);
83
90
  }, localization);
91
+ duplicateButton.setHelpText(this.localizationTable.selectionDropdown__duplicateHelpText);
84
92
  this.addSubWidget(resizeButton);
85
93
  this.addSubWidget(deleteButton);
86
94
  this.addSubWidget(duplicateButton);
@@ -129,8 +137,11 @@ class SelectionToolWidget extends BaseToolWidget_1.default {
129
137
  createIcon() {
130
138
  return this.editor.icons.makeSelectionIcon();
131
139
  }
132
- fillDropdown(dropdown) {
133
- super.fillDropdown(dropdown);
140
+ getHelpText() {
141
+ return this.localizationTable.selectionDropdown__baseHelpText;
142
+ }
143
+ fillDropdown(dropdown, helpDisplay) {
144
+ super.fillDropdown(dropdown, helpDisplay);
134
145
  const controlsContainer = document.createElement('div');
135
146
  controlsContainer.classList.add(`${constants_1.toolbarCSSPrefix}nonbutton-controls-main-list`);
136
147
  dropdown.appendChild(controlsContainer);
@@ -138,6 +149,9 @@ class SelectionToolWidget extends BaseToolWidget_1.default {
138
149
  const formatMenu = makeFormatMenu(this.editor, this.tool, this.localizationTable);
139
150
  formatMenu.addTo(controlsContainer);
140
151
  this.updateFormatMenu = () => formatMenu.update();
152
+ if (helpDisplay) {
153
+ formatMenu.registerHelpText(helpDisplay);
154
+ }
141
155
  formatMenu.update();
142
156
  return true;
143
157
  }
@@ -1,10 +1,12 @@
1
1
  import { Color4 } from '@js-draw/math';
2
2
  import Editor from '../../../Editor';
3
+ import type HelpDisplay from '../../utils/HelpDisplay';
3
4
  type OnColorChangeListener = (color: Color4) => void;
4
5
  export declare const makeColorInput: (editor: Editor, onColorChange: OnColorChangeListener) => {
5
6
  input: HTMLInputElement;
6
7
  container: HTMLSpanElement;
7
8
  setValue: (color: Color4 | string) => void;
8
9
  closePicker: () => void;
10
+ registerWithHelpTextDisplay: (helpDisplay: HelpDisplay) => void;
9
11
  };
10
12
  export default makeColorInput;
@@ -9,13 +9,16 @@ const PipetteTool_1 = __importDefault(require("../../../tools/PipetteTool"));
9
9
  const types_1 = require("../../../types");
10
10
  // Returns [ color input, input container, callback to change the color value ].
11
11
  const makeColorInput = (editor, onColorChange) => {
12
- const colorInputContainer = document.createElement('span');
12
+ const container = document.createElement('span');
13
+ const inputWrapper = document.createElement('span');
13
14
  const colorInput = document.createElement('input');
14
15
  colorInput.type = 'button';
15
16
  colorInput.classList.add('coloris_input');
16
- colorInputContainer.classList.add('color-input-container');
17
- colorInputContainer.appendChild(colorInput);
18
- const pipetteController = addPipetteTool(editor, colorInputContainer, (color) => {
17
+ container.classList.add('color-input-container');
18
+ inputWrapper.classList.add('color-input-wrapper');
19
+ inputWrapper.appendChild(colorInput);
20
+ container.appendChild(inputWrapper);
21
+ const pipetteController = addPipetteTool(editor, container, (color) => {
19
22
  colorInput.value = color.toHexString();
20
23
  onInputEnd();
21
24
  // Update the color preview, if it exists (may be managed by Coloris).
@@ -50,7 +53,7 @@ const makeColorInput = (editor, onColorChange) => {
50
53
  open: true,
51
54
  });
52
55
  pipetteController.cancel();
53
- colorInputContainer.classList.add('picker-open');
56
+ container.classList.add('picker-open');
54
57
  // Focus the Coloris color picker, if it exists.
55
58
  // Don't focus the text input within the color picker, however,
56
59
  // as this displays a keyboard on mobile devices.
@@ -66,7 +69,7 @@ const makeColorInput = (editor, onColorChange) => {
66
69
  onInputEnd();
67
70
  // Restore focus to the input that opened the color picker
68
71
  colorInput.focus();
69
- colorInputContainer.classList.remove('picker-open');
72
+ container.classList.remove('picker-open');
70
73
  };
71
74
  colorInput.addEventListener('close', () => {
72
75
  onClose();
@@ -82,13 +85,17 @@ const makeColorInput = (editor, onColorChange) => {
82
85
  };
83
86
  return {
84
87
  input: colorInput,
85
- container: colorInputContainer,
88
+ container,
86
89
  setValue: setColorInputValue,
87
90
  closePicker: () => {
88
91
  if (isOpen) {
89
92
  onInputEnd();
90
93
  }
91
94
  },
95
+ registerWithHelpTextDisplay: (helpDisplay) => {
96
+ helpDisplay.registerTextHelpForElement(inputWrapper, editor.localization.colorPickerToggleHelpText);
97
+ pipetteController.registerWithHelpTextDisplay(helpDisplay);
98
+ },
92
99
  };
93
100
  };
94
101
  exports.makeColorInput = makeColorInput;
@@ -143,6 +150,9 @@ const addPipetteTool = (editor, container, onColorChange) => {
143
150
  cancel: () => {
144
151
  endColorSelectMode();
145
152
  },
153
+ registerWithHelpTextDisplay: (helpDisplay) => {
154
+ helpDisplay.registerTextHelpForElement(pipetteButton, editor.localization.colorPickerPipetteHelpText);
155
+ },
146
156
  };
147
157
  };
148
158
  exports.default = exports.makeColorInput;
@@ -7,8 +7,14 @@ interface GridSelectChoice<ChoiceIdType> {
7
7
  }
8
8
  interface GridSelector<ChoiceIdType> {
9
9
  value: MutableReactiveValue<ChoiceIdType>;
10
+ /**
11
+ * Connects this grid selector with `other` such that only one item in
12
+ * either this or `other` can be selected at a time.
13
+ */
10
14
  linkWith: (other: GridSelector<ChoiceIdType>) => void;
15
+ /** Re-builds the icons shown in the grid selector. */
11
16
  updateIcons: () => void;
17
+ getRootElement: () => HTMLElement;
12
18
  addTo: (parent: HTMLElement) => void;
13
19
  /** Used internally @internal */
14
20
  _radiogroupName: string;
@@ -136,6 +136,9 @@ labelText, defaultId, choices) => {
136
136
  updateIcons: () => {
137
137
  buttons.forEach(button => button.updateIcon());
138
138
  },
139
+ getRootElement() {
140
+ return outerContainer;
141
+ },
139
142
  addTo: (parent) => {
140
143
  parent.appendChild(outerContainer);
141
144
  },
@@ -7,6 +7,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
7
7
  };
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  const TextComponent_1 = __importDefault(require("../components/TextComponent"));
10
+ const ImageComponent_1 = __importDefault(require("../components/ImageComponent"));
10
11
  const BaseTool_1 = __importDefault(require("./BaseTool"));
11
12
  const keybindings_1 = require("./keybindings");
12
13
  const cssPrefix = 'find-tool';
@@ -25,11 +26,23 @@ class FindTool extends BaseTool_1.default {
25
26
  return true;
26
27
  }
27
28
  getMatches(searchFor) {
28
- searchFor = searchFor.toLocaleLowerCase();
29
- const allTextComponents = this.editor.image.getAllElements()
30
- .filter(elem => elem instanceof TextComponent_1.default);
31
- const matches = allTextComponents.filter(text => text.getText().toLocaleLowerCase().indexOf(searchFor) !== -1);
32
- return matches.map(match => match.getBBox());
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());
33
46
  }
34
47
  focusCurrentMatch() {
35
48
  const matches = this.getMatches(this.searchInput.value);
@@ -4,7 +4,9 @@
4
4
  *
5
5
  * When no pointers are inside `element`, adds the CSS class `no-long-press-or-hover`.
6
6
  */
7
- declare const addLongPressOrHoverCssClasses: (element: HTMLElement) => {
7
+ declare const addLongPressOrHoverCssClasses: (element: HTMLElement, options?: {
8
+ timeout: number;
9
+ }) => {
8
10
  removeEventListeners: () => void;
9
11
  };
10
12
  export default addLongPressOrHoverCssClasses;
@@ -10,7 +10,7 @@ const listenForLongPressOrHover_1 = __importDefault(require("./listenForLongPres
10
10
  *
11
11
  * When no pointers are inside `element`, adds the CSS class `no-long-press-or-hover`.
12
12
  */
13
- const addLongPressOrHoverCssClasses = (element) => {
13
+ const addLongPressOrHoverCssClasses = (element, options) => {
14
14
  const hasLongPressClass = 'has-long-press-or-hover';
15
15
  const noLongPressClass = 'no-long-press-or-hover';
16
16
  element.classList.add('no-long-press-or-hover');
@@ -23,6 +23,7 @@ const addLongPressOrHoverCssClasses = (element) => {
23
23
  element.classList.add(noLongPressClass);
24
24
  element.classList.remove(hasLongPressClass);
25
25
  },
26
+ longPressTimeout: options?.timeout,
26
27
  });
27
28
  return {
28
29
  removeEventListeners: () => {
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Makes a clone of `element` and recursively applies styles from the original to the
3
+ * clone's children.
4
+ */
5
+ declare const cloneElementWithStyles: (element: HTMLElement) => HTMLElement;
6
+ export default cloneElementWithStyles;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Makes a clone of `element` and recursively applies styles from the original to the
5
+ * clone's children.
6
+ */
7
+ const cloneElementWithStyles = (element) => {
8
+ const restyle = (originalElement, clonedElement) => {
9
+ const originalComputedStyle = getComputedStyle(originalElement);
10
+ // jsdom doesn't support iterators in CSSStyleDeclarations. Iterate with
11
+ // an index.
12
+ for (let index = 0; index < originalComputedStyle.length; index++) {
13
+ const propertyName = originalComputedStyle.item(index);
14
+ const propertyValue = originalComputedStyle.getPropertyValue(propertyName);
15
+ clonedElement.style.setProperty(propertyName, propertyValue);
16
+ }
17
+ for (let i = 0; i < originalElement.children.length; i++) {
18
+ const originalChild = originalElement.children.item(i);
19
+ const clonedChild = clonedElement.children.item(i);
20
+ if (originalChild && clonedChild) {
21
+ restyle(originalChild, clonedChild);
22
+ }
23
+ else {
24
+ console.warn('CloneElement: Missing child');
25
+ }
26
+ }
27
+ };
28
+ const elementClone = element.cloneNode(true);
29
+ restyle(element, elementClone);
30
+ return elementClone;
31
+ };
32
+ exports.default = cloneElementWithStyles;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = {
4
- number: '1.14.0',
4
+ number: '1.15.0',
5
5
  };
@@ -1198,6 +1198,11 @@ export class Editor {
1198
1198
  '',
1199
1199
  '== Bezier.js ==',
1200
1200
  mitLicenseAttribution('2023 Mike "Pomax" Kamermans'),
1201
+ '',
1202
+ '',
1203
+ '== js-draw ==',
1204
+ mitLicenseAttribution('2023 Henry Heino'),
1205
+ '',
1201
1206
  ].join('\n'),
1202
1207
  minimized: true,
1203
1208
  });
@@ -144,7 +144,7 @@ export class StrokeSmoother {
144
144
  }
145
145
  let exitingVec = this.computeExitingVec();
146
146
  // Find the intersection between the entering vector and the exiting vector
147
- const maxRelativeLength = 2.4;
147
+ const maxRelativeLength = 1.7;
148
148
  const segmentStart = this.buffer[0];
149
149
  const segmentEnd = newPoint.pos;
150
150
  const startEndDist = segmentEnd.minus(segmentStart).magnitude();
@@ -165,11 +165,18 @@ export class StrokeSmoother {
165
165
  if (intersection) {
166
166
  controlPoint = intersection.point;
167
167
  }
168
- // No intersection or the intersection is one of the end points?
169
- if (!controlPoint || segmentStart.eq(controlPoint) || segmentEnd.eq(controlPoint)) {
168
+ // No intersection?
169
+ if (!controlPoint) {
170
+ // Estimate the control point position based on the entering tangent line
171
+ controlPoint = segmentStart
172
+ .lerp(segmentEnd, 0.5)
173
+ .lerp(segmentStart.plus(enteringVec.times(startEndDist)), 0.1);
174
+ }
175
+ // Equal to an endpoint?
176
+ if (segmentStart.eq(controlPoint) || segmentEnd.eq(controlPoint)) {
170
177
  // Position the control point closer to the first -- the connecting
171
178
  // segment will be roughly a line.
172
- controlPoint = segmentStart.plus(enteringVec.times(startEndDist / 4));
179
+ controlPoint = segmentStart.plus(enteringVec.times(startEndDist / 5));
173
180
  }
174
181
  console.assert(!segmentStart.eq(controlPoint, 1e-11), 'Start and control points are equal!');
175
182
  console.assert(!controlPoint.eq(segmentEnd, 1e-11), 'Control and end points are equal!');
@@ -49,7 +49,7 @@ export class CacheRecordManager {
49
49
  numberAllocd++;
50
50
  }
51
51
  }
52
- averageReassignedCount /= Math.max(this.cacheRecords.length, 0);
52
+ averageReassignedCount /= Math.max(this.cacheRecords.length, 1);
53
53
  const debugInfo = [
54
54
  `${this.cacheRecords.length} cache records (max ${this.maxCanvases})`,
55
55
  `${numberAllocd} assigned to screen regions`,
@@ -0,0 +1,4 @@
1
+ import { Point2 } from '@js-draw/math';
2
+ /** Swipes `element` using HTML pointer events. */
3
+ declare const sendHtmlSwipe: (element: HTMLElement, start: Point2, end: Point2, timeMs?: number) => Promise<void>;
4
+ export default sendHtmlSwipe;
@@ -0,0 +1,12 @@
1
+ /** Swipes `element` using HTML pointer events. */
2
+ const sendHtmlSwipe = async (element, start, end, timeMs = 300) => {
3
+ element.dispatchEvent(new PointerEvent('pointerdown', { isPrimary: true, clientX: start.x, clientY: start.y, }));
4
+ const step = 0.1;
5
+ for (let i = 0; i < 1; i += step) {
6
+ await jest.advanceTimersByTimeAsync(timeMs * step);
7
+ const currentPoint = start.lerp(end, i);
8
+ element.dispatchEvent(new PointerEvent('pointermove', { isPrimary: true, clientX: currentPoint.x, clientY: currentPoint.y, }));
9
+ }
10
+ element.dispatchEvent(new PointerEvent('pointerup', { isPrimary: true, clientX: end.x, clientY: end.y, }));
11
+ };
12
+ export default sendHtmlSwipe;
@@ -44,6 +44,7 @@ export default class EdgeToolbar extends AbstractToolbar {
44
44
  private sidebarVisible;
45
45
  private sidebarY;
46
46
  private sidebarTitle;
47
+ private clearDragListeners;
47
48
  /** @internal */
48
49
  constructor(editor: Editor, parent: HTMLElement, localizationTable: ToolbarLocalization);
49
50
  private listenForVisibilityChanges;
@@ -4,6 +4,7 @@ import EdgeToolbarLayoutManager from './widgets/layout/EdgeToolbarLayoutManage
4
4
  import { MutableReactiveValue, ReactiveValue } from '../util/ReactiveValue.mjs';
5
5
  import AbstractToolbar from './AbstractToolbar.mjs';
6
6
  import stopPropagationOfScrollingWheelEvents from '../util/stopPropagationOfScrollingWheelEvents.mjs';
7
+ import makeDraggable from './utils/makeDraggable.mjs';
7
8
  /**
8
9
  * Creates an `EdgeToolbar`.
9
10
  *
@@ -39,6 +40,7 @@ export default class EdgeToolbar extends AbstractToolbar {
39
40
  /** @internal */
40
41
  constructor(editor, parent, localizationTable) {
41
42
  super(editor, localizationTable);
43
+ this.clearDragListeners = null;
42
44
  this.toolbarContainer = document.createElement('div');
43
45
  this.toolbarContainer.classList.add(`${toolbarCSSPrefix}root`);
44
46
  this.toolbarContainer.classList.add(`${toolbarCSSPrefix}element`);
@@ -124,8 +126,11 @@ export default class EdgeToolbar extends AbstractToolbar {
124
126
  // to animate it to 1.
125
127
  this.menuContainer.style.opacity = '0';
126
128
  }
129
+ const prefersReduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)') ?? '';
127
130
  this.sidebarVisible.onUpdate(visible => {
128
131
  const animationProperties = `${animationDuration}ms ease`;
132
+ // We need to use different animations when reducing motion.
133
+ const reduceMotion = prefersReduceMotion.matches ? '-reduce-motion' : '';
129
134
  if (visible) {
130
135
  this.sidebarY.set(this.snappedSidebarY());
131
136
  if (animationTimeout) {
@@ -133,8 +138,10 @@ export default class EdgeToolbar extends AbstractToolbar {
133
138
  animationTimeout = null;
134
139
  }
135
140
  this.menuContainer.style.display = '';
136
- this.sidebarContainer.style.animation = `${animationProperties} ${toolbarCSSPrefix}-edgemenu-transition-in`;
137
- this.menuContainer.style.animation = `${animationProperties} ${toolbarCSSPrefix}-edgemenu-container-transition-in`;
141
+ this.sidebarContainer.style.animation =
142
+ `${animationProperties} ${toolbarCSSPrefix}-edgemenu-transition-in${reduceMotion}`;
143
+ this.menuContainer.style.animation =
144
+ `${animationProperties} ${toolbarCSSPrefix}-edgemenu-container-transition-in${reduceMotion}`;
138
145
  this.menuContainer.style.opacity = '1';
139
146
  // Focus the close button when first shown, but prevent scroll because the button
140
147
  // is likely at the bottom of the screen (and we want the full sidebar to remain
@@ -144,8 +151,10 @@ export default class EdgeToolbar extends AbstractToolbar {
144
151
  else {
145
152
  this.closeColorPickers();
146
153
  if (animationTimeout === null) {
147
- this.sidebarContainer.style.animation = ` ${animationProperties} ${toolbarCSSPrefix}-edgemenu-transition-out`;
148
- this.menuContainer.style.animation = `${animationProperties} ${toolbarCSSPrefix}-edgemenu-container-transition-out`;
154
+ this.sidebarContainer.style.animation =
155
+ `${animationProperties} ${toolbarCSSPrefix}-edgemenu-transition-out${reduceMotion}`;
156
+ this.menuContainer.style.animation =
157
+ `${animationProperties} ${toolbarCSSPrefix}-edgemenu-container-transition-out${reduceMotion}`;
149
158
  // Manually set the container's opacity to prevent flickering when closing
150
159
  // the toolbar.
151
160
  this.menuContainer.style.opacity = '0';
@@ -254,15 +263,12 @@ export default class EdgeToolbar extends AbstractToolbar {
254
263
  this.toolbarContainer.remove();
255
264
  this.menuContainer.remove();
256
265
  this.toolRowResizeObserver.disconnect();
266
+ this.clearDragListeners?.();
257
267
  }
258
268
  initDragListeners() {
259
- let lastX = 0;
260
- let lastY = 0;
261
- let startX = 0;
262
- let startY = 0;
263
- let pointerDown = false;
264
- let capturedPointerId = null;
265
269
  const dragElements = [this.closeButton, this.sidebarContainer, this.sidebarContent];
270
+ // Forward longer touch events from the menu background to the
271
+ // editor (and close the sidebar).
266
272
  this.manageListener(this.editor.handlePointerEventsExceptClicksFrom(this.menuContainer, (eventName, event) => {
267
273
  if (event.target === this.menuContainer) {
268
274
  if (eventName === 'pointerdown') {
@@ -281,113 +287,27 @@ export default class EdgeToolbar extends AbstractToolbar {
281
287
  }, (_eventName, event) => {
282
288
  return event.target === this.menuContainer;
283
289
  }));
284
- const isDraggableElement = (element) => {
285
- if (!element) {
286
- return false;
287
- }
288
- if (dragElements.includes(element)) {
289
- return true;
290
- }
291
- // Some inputs handle dragging themselves. Don't also interpret such gestures
292
- // as dragging the dropdown.
293
- const undraggableElementTypes = ['INPUT', 'SELECT', 'IMG'];
294
- let hasSuitableAncestors = false;
295
- let ancestor = element.parentElement;
296
- while (ancestor) {
297
- if (undraggableElementTypes.includes(ancestor.tagName)) {
298
- break;
299
- }
300
- if (ancestor === this.sidebarContainer) {
301
- hasSuitableAncestors = true;
302
- break;
303
- }
304
- ancestor = ancestor.parentElement;
305
- }
306
- return !undraggableElementTypes.includes(element.tagName) && hasSuitableAncestors;
307
- };
308
- const clickThreshold = 5;
309
- // Returns whether the current (or if no current, **the last**) gesture is roughly a click.
310
- // Because this can be called **after** a gesture has just ended, it should not require
311
- // the gesture to be in progress.
312
- const isRoughlyClick = () => {
313
- return Math.hypot(lastX - startX, lastY - startY) < clickThreshold;
314
- };
315
- let startedDragging = false;
316
- this.sidebarContainer.addEventListener('pointerdown', event => {
317
- if (event.defaultPrevented || !isDraggableElement(event.target)) {
318
- return;
319
- }
320
- if (event.isPrimary) {
321
- startedDragging = false;
322
- lastX = event.clientX;
323
- lastY = event.clientY;
324
- startX = event.clientX;
325
- startY = event.clientY;
326
- capturedPointerId = null;
327
- pointerDown = true;
328
- }
329
- }, { passive: true });
290
+ // Set lastGestureWasRoughlyClick to `true` initially because on page load
291
+ // performance.now() is zero.
292
+ let lastGestureWasRoughlyClick = true;
330
293
  let gestureEndTimestamp = 0;
331
- const onGestureEnd = (_event) => {
332
- // If the pointerup/pointercancel event was for a pointer not being tracked,
333
- if (!pointerDown) {
334
- return;
335
- }
336
- gestureEndTimestamp = performance.now();
337
- if (capturedPointerId !== null) {
338
- this.sidebarContainer.releasePointerCapture(capturedPointerId);
339
- capturedPointerId = null;
340
- }
341
- this.finalizeDrag();
342
- pointerDown = false;
343
- startedDragging = false;
344
- };
345
- this.sidebarContainer.onpointermove = event => {
346
- if (!event.isPrimary || !pointerDown) {
347
- return undefined;
348
- }
349
- // Mouse event and no buttons pressed? Cancel the event.
350
- // This can happen if the event was canceled by a focus change (e.g. by opening a
351
- // right-click menu).
352
- if (event.pointerType === 'mouse' && event.buttons === 0) {
353
- onGestureEnd(event);
354
- return undefined;
355
- }
356
- // Only capture after motion -- capturing early prevents click events in Chrome.
357
- if (capturedPointerId === null && !isRoughlyClick()) {
358
- this.sidebarContainer.setPointerCapture(event.pointerId);
359
- capturedPointerId = event.pointerId;
360
- }
361
- const x = event.clientX;
362
- const y = event.clientY;
363
- const dx = x - lastX;
364
- const dy = y - lastY;
365
- if (Math.abs(y - startY) > clickThreshold || startedDragging) {
366
- this.handleDrag(dx, dy);
367
- lastX = x;
368
- lastY = y;
369
- startedDragging = true;
370
- }
371
- };
372
- this.sidebarContainer.onpointerleave = event => {
373
- {
374
- // Capture the pointer if it exits the container while dragging.
375
- if (capturedPointerId === null && pointerDown && event.isPrimary) {
376
- this.sidebarContainer.setPointerCapture(event.pointerId);
377
- capturedPointerId = event.pointerId;
378
- }
379
- }
380
- };
294
+ const dragController = makeDraggable(this.sidebarContainer, {
295
+ draggableChildElements: dragElements,
296
+ onDrag: (deltaX, deltaY) => this.handleDrag(deltaX, deltaY),
297
+ onDragEnd: (dragStatistics) => {
298
+ gestureEndTimestamp = dragStatistics.endTimestamp;
299
+ lastGestureWasRoughlyClick = dragStatistics.roughlyClick;
300
+ this.finalizeDrag();
301
+ },
302
+ });
303
+ this.clearDragListeners = () => dragController.removeListeners();
381
304
  this.closeButton.onclick = () => {
382
305
  const wasJustDragging = performance.now() - gestureEndTimestamp < 100;
383
- const roughlyClick = isRoughlyClick();
384
306
  // Ignore the click event if it was caused by dragging the button.
385
- if (wasJustDragging && roughlyClick || !wasJustDragging) {
307
+ if ((wasJustDragging && lastGestureWasRoughlyClick) || !wasJustDragging) {
386
308
  this.sidebarVisible.set(false);
387
309
  }
388
310
  };
389
- this.sidebarContainer.onpointerup = onGestureEnd;
390
- this.sidebarContainer.onpointercancel = onGestureEnd;
391
311
  }
392
312
  /**
393
313
  * Updates the position of this menu **during** a drag. After a drag ends,
@@ -66,6 +66,7 @@ export default class IconProvider {
66
66
  makeSaveIcon(): IconElemType;
67
67
  makeConfigureDocumentIcon(): IconElemType;
68
68
  makeOverflowIcon(): IconElemType;
69
+ makeHelpIcon(): IconElemType;
69
70
  /**
70
71
  * @param pathData - SVG path data (e.g. `m10,10l30,30z`)
71
72
  * @param fill - A valid CSS color (e.g. `var(--icon-color)` or `#f0f`). This can be `none`.
@@ -779,6 +779,33 @@ class IconProvider {
779
779
  z
780
780
  `);
781
781
  }
782
+ makeHelpIcon() {
783
+ const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
784
+ svg.innerHTML = `
785
+ <circle
786
+ style="stroke-width:1.587; stroke: var(--icon-color);"
787
+ fill="none"
788
+ cx="13.23"
789
+ cy="13.23"
790
+ r="11.9"
791
+ />
792
+ <path
793
+ style="stroke-width: 3; stroke-linecap: butt; stroke: var(--icon-color);"
794
+ fill="none"
795
+ d="M 9.26,6.61 C 18.7,3.25 19.95,10.4 14.3,13.4 c -1.15,0.61 -1.32,1.32 -1.32,2.65 v 2.12"
796
+ />
797
+ <circle
798
+ style="fill: var(--icon-color);"
799
+ cx="13"
800
+ cy="21.32"
801
+ r="1.9"
802
+ />
803
+ `;
804
+ svg.setAttribute('viewBox', '0 0 26.46 26.46');
805
+ svg.setAttribute('width', '100');
806
+ svg.setAttribute('height', '100');
807
+ return svg;
808
+ }
782
809
  /**
783
810
  * @param pathData - SVG path data (e.g. `m10,10l30,30z`)
784
811
  * @param fill - A valid CSS color (e.g. `var(--icon-color)` or `#f0f`). This can be `none`.