js-draw 1.17.0 → 1.19.1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +70 -10
- package/dist/Editor.css +35 -3
- package/dist/bundle.js +2 -2
- package/dist/bundledStyles.js +1 -1
- package/dist/cjs/Editor.d.ts +38 -21
- package/dist/cjs/Editor.js +11 -2
- package/dist/cjs/{SVGLoader.d.ts → SVGLoader/index.d.ts} +1 -1
- package/dist/cjs/{SVGLoader.js → SVGLoader/index.js} +12 -29
- package/dist/cjs/SVGLoader/utils/determineFontSize.d.ts +3 -0
- package/dist/cjs/SVGLoader/utils/determineFontSize.js +27 -0
- package/dist/cjs/Viewport.d.ts +33 -1
- package/dist/cjs/components/AbstractComponent.d.ts +17 -5
- package/dist/cjs/components/AbstractComponent.js +15 -15
- package/dist/cjs/components/Stroke.d.ts +4 -1
- package/dist/cjs/components/Stroke.js +158 -2
- package/dist/cjs/components/TextComponent.js +3 -1
- package/dist/cjs/components/builders/PolylineBuilder.d.ts +1 -1
- package/dist/cjs/components/builders/PolylineBuilder.js +9 -2
- package/dist/cjs/components/builders/PressureSensitiveFreehandLineBuilder.d.ts +1 -1
- package/dist/cjs/components/builders/PressureSensitiveFreehandLineBuilder.js +44 -51
- package/dist/cjs/image/EditorImage.js +1 -1
- package/dist/cjs/localizations/de.js +1 -1
- package/dist/cjs/localizations/es.js +1 -1
- package/dist/cjs/rendering/caching/RenderingCacheNode.js +20 -15
- package/dist/cjs/testing/createEditor.d.ts +2 -2
- package/dist/cjs/testing/createEditor.js +2 -2
- package/dist/cjs/testing/findNodeWithText.d.ts +3 -0
- package/dist/cjs/testing/findNodeWithText.js +16 -0
- package/dist/cjs/testing/firstElementAncestorOfNode.d.ts +3 -0
- package/dist/cjs/testing/firstElementAncestorOfNode.js +13 -0
- package/dist/cjs/testing/sendKeyPressRelease.d.ts +3 -0
- package/dist/cjs/testing/sendKeyPressRelease.js +8 -0
- package/dist/cjs/testing/sendPenEvent.d.ts +2 -2
- package/dist/cjs/testing/sendPenEvent.js +26 -3
- package/dist/cjs/toolbar/IconProvider.d.ts +3 -1
- package/dist/cjs/toolbar/IconProvider.js +15 -3
- package/dist/cjs/toolbar/localization.d.ts +8 -1
- package/dist/cjs/toolbar/localization.js +9 -2
- package/dist/cjs/toolbar/widgets/BaseWidget.d.ts +1 -0
- package/dist/cjs/toolbar/widgets/BaseWidget.js +1 -0
- package/dist/cjs/toolbar/widgets/EraserToolWidget.d.ts +6 -1
- package/dist/cjs/toolbar/widgets/EraserToolWidget.js +45 -5
- package/dist/cjs/toolbar/widgets/InsertImageWidget/ImageWrapper.d.ts +22 -0
- package/dist/cjs/toolbar/widgets/InsertImageWidget/ImageWrapper.js +58 -0
- package/dist/cjs/toolbar/widgets/InsertImageWidget/fileToImages.d.ts +3 -0
- package/dist/cjs/toolbar/widgets/InsertImageWidget/fileToImages.js +21 -0
- package/dist/cjs/toolbar/widgets/InsertImageWidget/index.d.ts +37 -0
- package/dist/cjs/toolbar/widgets/InsertImageWidget/index.js +281 -0
- package/dist/cjs/toolbar/widgets/PenToolWidget.js +10 -3
- package/dist/cjs/toolbar/widgets/PenToolWidget.test.d.ts +1 -0
- package/dist/cjs/toolbar/widgets/TextToolWidget.js +5 -3
- package/dist/cjs/toolbar/widgets/TextToolWidget.test.d.ts +1 -0
- package/dist/cjs/toolbar/widgets/components/makeFileInput.d.ts +12 -2
- package/dist/cjs/toolbar/widgets/components/makeFileInput.js +102 -45
- package/dist/cjs/toolbar/widgets/components/makeFileInput.test.d.ts +1 -0
- package/dist/cjs/toolbar/widgets/components/makeSnappedList.d.ts +15 -0
- package/dist/cjs/toolbar/widgets/components/makeSnappedList.js +103 -0
- package/dist/cjs/toolbar/widgets/keybindings.js +1 -1
- package/dist/cjs/tools/Eraser.d.ts +31 -6
- package/dist/cjs/tools/Eraser.js +161 -21
- package/dist/cjs/tools/PasteHandler.js +0 -1
- package/dist/cjs/tools/SelectionTool/Selection.d.ts +2 -2
- package/dist/cjs/tools/SelectionTool/Selection.js +20 -20
- package/dist/cjs/tools/SelectionTool/SelectionHandle.d.ts +8 -2
- package/dist/cjs/tools/SelectionTool/SelectionHandle.js +6 -0
- package/dist/cjs/tools/SelectionTool/SelectionTool.js +1 -1
- package/dist/cjs/tools/SelectionTool/types.d.ts +19 -0
- package/dist/cjs/tools/TextTool.js +2 -1
- package/dist/cjs/tools/TextTool.test.d.ts +1 -0
- package/dist/cjs/tools/ToolController.d.ts +2 -0
- package/dist/cjs/tools/ToolController.js +10 -1
- package/dist/cjs/tools/lib.d.ts +1 -4
- package/dist/cjs/tools/lib.js +2 -4
- package/dist/cjs/util/ReactiveValue.d.ts +2 -0
- package/dist/cjs/util/ReactiveValue.js +11 -0
- package/dist/cjs/util/bytesToSizeString.d.ts +8 -0
- package/dist/cjs/util/bytesToSizeString.js +26 -0
- package/dist/cjs/util/bytesToSizeString.test.d.ts +1 -0
- package/dist/cjs/util/stopPropagationOfScrollingWheelEvents.js +10 -6
- package/dist/cjs/util/waitForAll.d.ts +2 -0
- package/dist/cjs/util/waitForAll.js +2 -0
- package/dist/cjs/util/waitForImageLoaded.js +3 -0
- package/dist/cjs/util/waitForTimeout.d.ts +1 -0
- package/dist/cjs/util/waitForTimeout.js +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/mjs/Editor.d.ts +38 -21
- package/dist/mjs/Editor.mjs +11 -2
- package/dist/mjs/{SVGLoader.d.ts → SVGLoader/index.d.ts} +1 -1
- package/dist/mjs/{SVGLoader.mjs → SVGLoader/index.mjs} +12 -29
- package/dist/mjs/SVGLoader/index.test.d.ts +1 -0
- package/dist/mjs/SVGLoader/utils/determineFontSize.d.ts +3 -0
- package/dist/mjs/SVGLoader/utils/determineFontSize.mjs +25 -0
- package/dist/mjs/Viewport.d.ts +33 -1
- package/dist/mjs/components/AbstractComponent.d.ts +17 -5
- package/dist/mjs/components/AbstractComponent.mjs +15 -15
- package/dist/mjs/components/Stroke.d.ts +4 -1
- package/dist/mjs/components/Stroke.mjs +159 -3
- package/dist/mjs/components/TextComponent.mjs +3 -1
- package/dist/mjs/components/builders/PolylineBuilder.d.ts +1 -1
- package/dist/mjs/components/builders/PolylineBuilder.mjs +10 -3
- package/dist/mjs/components/builders/PressureSensitiveFreehandLineBuilder.d.ts +1 -1
- package/dist/mjs/components/builders/PressureSensitiveFreehandLineBuilder.mjs +45 -52
- package/dist/mjs/image/EditorImage.mjs +1 -1
- package/dist/mjs/localizations/de.mjs +1 -1
- package/dist/mjs/localizations/es.mjs +1 -1
- package/dist/mjs/rendering/caching/RenderingCacheNode.mjs +20 -15
- package/dist/mjs/testing/createEditor.d.ts +2 -2
- package/dist/mjs/testing/createEditor.mjs +2 -2
- package/dist/mjs/testing/findNodeWithText.d.ts +3 -0
- package/dist/mjs/testing/findNodeWithText.mjs +14 -0
- package/dist/mjs/testing/firstElementAncestorOfNode.d.ts +3 -0
- package/dist/mjs/testing/firstElementAncestorOfNode.mjs +11 -0
- package/dist/mjs/testing/sendKeyPressRelease.d.ts +3 -0
- package/dist/mjs/testing/sendKeyPressRelease.mjs +6 -0
- package/dist/mjs/testing/sendPenEvent.d.ts +2 -2
- package/dist/mjs/testing/sendPenEvent.mjs +3 -3
- package/dist/mjs/toolbar/IconProvider.d.ts +3 -1
- package/dist/mjs/toolbar/IconProvider.mjs +15 -3
- package/dist/mjs/toolbar/localization.d.ts +8 -1
- package/dist/mjs/toolbar/localization.mjs +9 -2
- package/dist/mjs/toolbar/widgets/BaseWidget.d.ts +1 -0
- package/dist/mjs/toolbar/widgets/BaseWidget.mjs +1 -0
- package/dist/mjs/toolbar/widgets/EraserToolWidget.d.ts +6 -1
- package/dist/mjs/toolbar/widgets/EraserToolWidget.mjs +47 -6
- package/dist/mjs/toolbar/widgets/InsertImageWidget/ImageWrapper.d.ts +22 -0
- package/dist/mjs/toolbar/widgets/InsertImageWidget/ImageWrapper.mjs +54 -0
- package/dist/mjs/toolbar/widgets/InsertImageWidget/fileToImages.d.ts +3 -0
- package/dist/mjs/toolbar/widgets/InsertImageWidget/fileToImages.mjs +16 -0
- package/dist/mjs/toolbar/widgets/InsertImageWidget/index.d.ts +37 -0
- package/dist/mjs/toolbar/widgets/InsertImageWidget/index.mjs +276 -0
- package/dist/mjs/toolbar/widgets/InsertImageWidget/index.test.d.ts +1 -0
- package/dist/mjs/toolbar/widgets/PenToolWidget.mjs +10 -3
- package/dist/mjs/toolbar/widgets/PenToolWidget.test.d.ts +1 -0
- package/dist/mjs/toolbar/widgets/TextToolWidget.mjs +5 -3
- package/dist/mjs/toolbar/widgets/TextToolWidget.test.d.ts +1 -0
- package/dist/mjs/toolbar/widgets/components/makeFileInput.d.ts +12 -2
- package/dist/mjs/toolbar/widgets/components/makeFileInput.mjs +102 -45
- package/dist/mjs/toolbar/widgets/components/makeFileInput.test.d.ts +1 -0
- package/dist/mjs/toolbar/widgets/components/makeSnappedList.d.ts +15 -0
- package/dist/mjs/toolbar/widgets/components/makeSnappedList.mjs +98 -0
- package/dist/mjs/toolbar/widgets/keybindings.mjs +1 -1
- package/dist/mjs/tools/Eraser.d.ts +31 -6
- package/dist/mjs/tools/Eraser.mjs +161 -22
- package/dist/mjs/tools/PasteHandler.mjs +0 -1
- package/dist/mjs/tools/SelectionTool/Selection.d.ts +2 -2
- package/dist/mjs/tools/SelectionTool/Selection.mjs +20 -20
- package/dist/mjs/tools/SelectionTool/SelectionHandle.d.ts +8 -2
- package/dist/mjs/tools/SelectionTool/SelectionHandle.mjs +6 -0
- package/dist/mjs/tools/SelectionTool/SelectionTool.mjs +1 -1
- package/dist/mjs/tools/SelectionTool/types.d.ts +19 -0
- package/dist/mjs/tools/TextTool.mjs +2 -1
- package/dist/mjs/tools/TextTool.test.d.ts +1 -0
- package/dist/mjs/tools/ToolController.d.ts +2 -0
- package/dist/mjs/tools/ToolController.mjs +10 -1
- package/dist/mjs/tools/lib.d.ts +1 -4
- package/dist/mjs/tools/lib.mjs +1 -4
- package/dist/mjs/util/ReactiveValue.d.ts +2 -0
- package/dist/mjs/util/ReactiveValue.mjs +11 -0
- package/dist/mjs/util/bytesToSizeString.d.ts +8 -0
- package/dist/mjs/util/bytesToSizeString.mjs +24 -0
- package/dist/mjs/util/bytesToSizeString.test.d.ts +1 -0
- package/dist/mjs/util/stopPropagationOfScrollingWheelEvents.mjs +10 -6
- package/dist/mjs/util/waitForAll.d.ts +2 -0
- package/dist/mjs/util/waitForAll.mjs +2 -0
- package/dist/mjs/util/waitForImageLoaded.mjs +3 -0
- package/dist/mjs/util/waitForTimeout.d.ts +1 -0
- package/dist/mjs/util/waitForTimeout.mjs +1 -1
- package/dist/mjs/version.mjs +1 -1
- package/package.json +4 -4
- package/src/toolbar/toolbar.scss +1 -7
- package/src/toolbar/widgets/{InsertImageWidget.scss → InsertImageWidget/index.scss} +3 -2
- package/src/toolbar/widgets/components/components.scss +2 -1
- package/src/toolbar/widgets/components/makeFileInput.scss +14 -1
- package/src/toolbar/widgets/components/makeSnappedList.scss +28 -0
- package/src/toolbar/widgets/widgets.scss +7 -0
- package/dist/cjs/toolbar/widgets/InsertImageWidget.d.ts +0 -22
- package/dist/cjs/toolbar/widgets/InsertImageWidget.js +0 -269
- package/dist/mjs/toolbar/widgets/InsertImageWidget.d.ts +0 -22
- package/dist/mjs/toolbar/widgets/InsertImageWidget.mjs +0 -264
- /package/dist/cjs/{SVGLoader.test.d.ts → SVGLoader/index.test.d.ts} +0 -0
- /package/dist/{mjs/SVGLoader.test.d.ts → cjs/toolbar/widgets/InsertImageWidget/index.test.d.ts} +0 -0
@@ -0,0 +1,98 @@
|
|
1
|
+
import stopPropagationOfScrollingWheelEvents from '../../../util/stopPropagationOfScrollingWheelEvents.mjs';
|
2
|
+
import { MutableReactiveValue, ReactiveValue } from '../../../util/ReactiveValue.mjs';
|
3
|
+
/**
|
4
|
+
* Creates a list that snaps to each item and reports the selected item.
|
5
|
+
*/
|
6
|
+
const makeSnappedList = (itemsValue) => {
|
7
|
+
const container = document.createElement('div');
|
8
|
+
container.classList.add('toolbar-snapped-scroll-list');
|
9
|
+
const visibleIndex = MutableReactiveValue.fromInitialValue(0);
|
10
|
+
let observer = null;
|
11
|
+
const createObserver = () => {
|
12
|
+
observer = new IntersectionObserver((entries) => {
|
13
|
+
for (const entry of entries) {
|
14
|
+
if (entry.isIntersecting && entry.intersectionRatio > 0.7) {
|
15
|
+
const indexString = entry.target.getAttribute('data-item-index');
|
16
|
+
if (indexString === null)
|
17
|
+
throw new Error('Could not find attribute data-item-index');
|
18
|
+
const index = Number(indexString);
|
19
|
+
visibleIndex.set(index);
|
20
|
+
break;
|
21
|
+
}
|
22
|
+
}
|
23
|
+
}, {
|
24
|
+
// Element to use as the boudning box with which to intersect.
|
25
|
+
// See https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
|
26
|
+
root: container,
|
27
|
+
// Fraction of an element that must be visible to trigger the callback:
|
28
|
+
threshold: 0.9,
|
29
|
+
});
|
30
|
+
};
|
31
|
+
const destroyObserver = () => {
|
32
|
+
if (observer) {
|
33
|
+
observer.disconnect();
|
34
|
+
visibleIndex.set(0);
|
35
|
+
observer = null;
|
36
|
+
}
|
37
|
+
};
|
38
|
+
const wrappedItems = ReactiveValue.map(itemsValue, items => {
|
39
|
+
return items.map((item, index) => {
|
40
|
+
const wrapper = document.createElement('div');
|
41
|
+
if (item.element.parentElement)
|
42
|
+
item.element.remove();
|
43
|
+
wrapper.appendChild(item.element);
|
44
|
+
wrapper.classList.add('item');
|
45
|
+
wrapper.setAttribute('data-item-index', `${index}`);
|
46
|
+
return {
|
47
|
+
element: wrapper,
|
48
|
+
data: item.data,
|
49
|
+
};
|
50
|
+
});
|
51
|
+
});
|
52
|
+
const lastItems = [];
|
53
|
+
wrappedItems.onUpdateAndNow(items => {
|
54
|
+
visibleIndex.set(-1);
|
55
|
+
for (const item of lastItems) {
|
56
|
+
observer?.unobserve(item.element);
|
57
|
+
}
|
58
|
+
container.replaceChildren();
|
59
|
+
// An observer is only necessary if there are multiple items to scroll through.
|
60
|
+
if (items.length > 1) {
|
61
|
+
createObserver();
|
62
|
+
}
|
63
|
+
else {
|
64
|
+
destroyObserver();
|
65
|
+
}
|
66
|
+
// Different styling is applied when empty
|
67
|
+
if (items.length === 0) {
|
68
|
+
container.classList.add('-empty');
|
69
|
+
}
|
70
|
+
else {
|
71
|
+
container.classList.remove('-empty');
|
72
|
+
}
|
73
|
+
for (const item of items) {
|
74
|
+
container.appendChild(item.element);
|
75
|
+
}
|
76
|
+
visibleIndex.set(0);
|
77
|
+
if (observer) {
|
78
|
+
for (const item of items) {
|
79
|
+
observer.observe(item.element);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
});
|
83
|
+
const visibleItem = ReactiveValue.map(visibleIndex, index => {
|
84
|
+
const values = itemsValue.get();
|
85
|
+
if (0 <= index && index < values.length) {
|
86
|
+
return values[index].data;
|
87
|
+
}
|
88
|
+
return null;
|
89
|
+
});
|
90
|
+
// makeSnappedList is generally shown within the toolbar. This allows users to
|
91
|
+
// scroll it with a touchpad.
|
92
|
+
stopPropagationOfScrollingWheelEvents(container);
|
93
|
+
return {
|
94
|
+
container,
|
95
|
+
visibleItem,
|
96
|
+
};
|
97
|
+
};
|
98
|
+
export default makeSnappedList;
|
@@ -3,7 +3,7 @@ import KeyboardShortcutManager from '../../shortcuts/KeyboardShortcutManager.m
|
|
3
3
|
export const resizeImageToSelectionKeyboardShortcut = 'jsdraw.toolbar.SelectionTool.resizeImageToSelection';
|
4
4
|
KeyboardShortcutManager.registerDefaultKeyboardShortcut(resizeImageToSelectionKeyboardShortcut, ['ctrlOrMeta+r'], 'Resize image to selection');
|
5
5
|
// Pen tool
|
6
|
-
export const selectStrokeTypeKeyboardShortcutIds = [1, 2, 3, 4, 5, 6, 7].map(id => `jsdraw.toolbar.PenTool.select-pen-${id}`);
|
6
|
+
export const selectStrokeTypeKeyboardShortcutIds = [1, 2, 3, 4, 5, 6, 7, 8, 9].map(id => `jsdraw.toolbar.PenTool.select-pen-${id}`);
|
7
7
|
for (let i = 0; i < selectStrokeTypeKeyboardShortcutIds.length; i++) {
|
8
8
|
const id = selectStrokeTypeKeyboardShortcutIds[i];
|
9
9
|
KeyboardShortcutManager.registerDefaultKeyboardShortcut(id, [`CtrlOrMeta+Digit${(i + 1)}`], 'Select pen style ' + (i + 1));
|
@@ -1,31 +1,56 @@
|
|
1
|
-
import { KeyPressEvent, PointerEvt } from '../inputEvents';
|
1
|
+
import { GestureCancelEvt, KeyPressEvent, PointerEvt } from '../inputEvents';
|
2
2
|
import BaseTool from './BaseTool';
|
3
3
|
import Editor from '../Editor';
|
4
4
|
import { MutableReactiveValue } from '../util/ReactiveValue';
|
5
|
+
export declare enum EraserMode {
|
6
|
+
PartialStroke = "partial-stroke",
|
7
|
+
FullStroke = "full-stroke"
|
8
|
+
}
|
9
|
+
export interface InitialEraserOptions {
|
10
|
+
thickness?: number;
|
11
|
+
mode?: EraserMode;
|
12
|
+
}
|
5
13
|
export default class Eraser extends BaseTool {
|
6
14
|
private editor;
|
7
15
|
private lastPoint;
|
8
16
|
private isFirstEraseEvt;
|
9
|
-
private toRemove;
|
10
17
|
private thickness;
|
11
18
|
private thicknessValue;
|
12
|
-
private
|
13
|
-
|
19
|
+
private modeValue;
|
20
|
+
private toRemove;
|
21
|
+
private toAdd;
|
22
|
+
private eraseCommands;
|
23
|
+
private addCommands;
|
24
|
+
constructor(editor: Editor, description: string, options?: InitialEraserOptions);
|
25
|
+
/**
|
26
|
+
* @returns a tool that briefly enables the eraser when a physical eraser is used.
|
27
|
+
* This tool should be added to the tool list after the primary tools.
|
28
|
+
*/
|
29
|
+
makeEraserSwitcherTool(): BaseTool;
|
14
30
|
private clearPreview;
|
15
31
|
private getSizeOnCanvas;
|
16
32
|
private drawPreviewAt;
|
33
|
+
/**
|
34
|
+
* @returns the eraser rectangle in canvas coordinates.
|
35
|
+
*
|
36
|
+
* For now, all erasers are rectangles or points.
|
37
|
+
*/
|
17
38
|
private getEraserRect;
|
39
|
+
/** Erases in a line from the last point to the current. */
|
18
40
|
private eraseTo;
|
19
41
|
onPointerDown(event: PointerEvt): boolean;
|
20
42
|
onPointerMove(event: PointerEvt): void;
|
21
43
|
onPointerUp(event: PointerEvt): void;
|
22
|
-
onGestureCancel(): void;
|
44
|
+
onGestureCancel(_event: GestureCancelEvt): void;
|
23
45
|
onKeyPress(event: KeyPressEvent): boolean;
|
46
|
+
/** Returns the side-length of the tip of this eraser. */
|
24
47
|
getThickness(): number;
|
48
|
+
/** Sets the side-length of this' tip. */
|
49
|
+
setThickness(thickness: number): void;
|
25
50
|
/**
|
26
51
|
* Returns a {@link MutableReactiveValue} that can be used to watch
|
27
52
|
* this tool's thickness.
|
28
53
|
*/
|
29
54
|
getThicknessValue(): MutableReactiveValue<number>;
|
30
|
-
|
55
|
+
getModeValue(): MutableReactiveValue<EraserMode>;
|
31
56
|
}
|
@@ -1,19 +1,74 @@
|
|
1
1
|
import { EditorEventType } from '../types.mjs';
|
2
2
|
import BaseTool from './BaseTool.mjs';
|
3
|
-
import { Vec2, LineSegment2, Color4, Rect2 } from '@js-draw/math';
|
3
|
+
import { Vec2, LineSegment2, Color4, Rect2, Path } from '@js-draw/math';
|
4
4
|
import Erase from '../commands/Erase.mjs';
|
5
5
|
import { PointerDevice } from '../Pointer.mjs';
|
6
6
|
import { decreaseSizeKeyboardShortcutId, increaseSizeKeyboardShortcutId } from './keybindings.mjs';
|
7
7
|
import { ReactiveValue } from '../util/ReactiveValue.mjs';
|
8
|
+
import EditorImage from '../image/EditorImage.mjs';
|
9
|
+
import uniteCommands from '../commands/uniteCommands.mjs';
|
10
|
+
import { pathToRenderable } from '../rendering/RenderablePathSpec.mjs';
|
11
|
+
export var EraserMode;
|
12
|
+
(function (EraserMode) {
|
13
|
+
EraserMode["PartialStroke"] = "partial-stroke";
|
14
|
+
EraserMode["FullStroke"] = "full-stroke";
|
15
|
+
})(EraserMode || (EraserMode = {}));
|
16
|
+
/** Handles switching from other primary tools to the eraser and back */
|
17
|
+
class EraserSwitcher extends BaseTool {
|
18
|
+
constructor(editor, eraser) {
|
19
|
+
super(editor.notifier, editor.localization.changeTool);
|
20
|
+
this.editor = editor;
|
21
|
+
this.eraser = eraser;
|
22
|
+
}
|
23
|
+
onPointerDown(event) {
|
24
|
+
if (event.allPointers.length === 1 && event.current.device === PointerDevice.Eraser) {
|
25
|
+
const toolController = this.editor.toolController;
|
26
|
+
const enabledPrimaryTools = toolController.getPrimaryTools().filter(tool => tool.isEnabled());
|
27
|
+
if (enabledPrimaryTools.length) {
|
28
|
+
this.previousEnabledTool = enabledPrimaryTools[0];
|
29
|
+
}
|
30
|
+
else {
|
31
|
+
this.previousEnabledTool = null;
|
32
|
+
}
|
33
|
+
this.previousEraserEnabledState = this.eraser.isEnabled();
|
34
|
+
this.eraser.setEnabled(true);
|
35
|
+
if (this.eraser.onPointerDown(event)) {
|
36
|
+
return true;
|
37
|
+
}
|
38
|
+
else {
|
39
|
+
this.restoreOriginalTool();
|
40
|
+
}
|
41
|
+
}
|
42
|
+
return false;
|
43
|
+
}
|
44
|
+
onPointerMove(event) {
|
45
|
+
this.eraser.onPointerMove(event);
|
46
|
+
}
|
47
|
+
restoreOriginalTool() {
|
48
|
+
this.eraser.setEnabled(this.previousEraserEnabledState);
|
49
|
+
if (this.previousEnabledTool) {
|
50
|
+
this.previousEnabledTool.setEnabled(true);
|
51
|
+
}
|
52
|
+
}
|
53
|
+
onPointerUp(event) {
|
54
|
+
this.eraser.onPointerUp(event);
|
55
|
+
this.restoreOriginalTool();
|
56
|
+
}
|
57
|
+
onGestureCancel(event) {
|
58
|
+
this.eraser.onGestureCancel(event);
|
59
|
+
this.restoreOriginalTool();
|
60
|
+
}
|
61
|
+
}
|
8
62
|
export default class Eraser extends BaseTool {
|
9
|
-
constructor(editor, description) {
|
63
|
+
constructor(editor, description, options) {
|
10
64
|
super(editor.notifier, description);
|
11
65
|
this.editor = editor;
|
12
66
|
this.lastPoint = null;
|
13
67
|
this.isFirstEraseEvt = true;
|
14
|
-
this.thickness = 10;
|
15
68
|
// Commands that each remove one element
|
16
|
-
this.
|
69
|
+
this.eraseCommands = [];
|
70
|
+
this.addCommands = [];
|
71
|
+
this.thickness = options?.thickness ?? 10;
|
17
72
|
this.thicknessValue = ReactiveValue.fromInitialValue(this.thickness);
|
18
73
|
this.thicknessValue.onUpdate(value => {
|
19
74
|
this.thickness = value;
|
@@ -22,6 +77,20 @@ export default class Eraser extends BaseTool {
|
|
22
77
|
tool: this,
|
23
78
|
});
|
24
79
|
});
|
80
|
+
this.modeValue = ReactiveValue.fromInitialValue(options?.mode ?? EraserMode.FullStroke);
|
81
|
+
this.modeValue.onUpdate(_value => {
|
82
|
+
this.editor.notifier.dispatch(EditorEventType.ToolUpdated, {
|
83
|
+
kind: EditorEventType.ToolUpdated,
|
84
|
+
tool: this,
|
85
|
+
});
|
86
|
+
});
|
87
|
+
}
|
88
|
+
/**
|
89
|
+
* @returns a tool that briefly enables the eraser when a physical eraser is used.
|
90
|
+
* This tool should be added to the tool list after the primary tools.
|
91
|
+
*/
|
92
|
+
makeEraserSwitcherTool() {
|
93
|
+
return new EraserSwitcher(this.editor, this);
|
25
94
|
}
|
26
95
|
clearPreview() {
|
27
96
|
this.editor.clearWetInk();
|
@@ -34,16 +103,24 @@ export default class Eraser extends BaseTool {
|
|
34
103
|
const size = this.getSizeOnCanvas();
|
35
104
|
const renderer = this.editor.display.getWetInkRenderer();
|
36
105
|
const rect = this.getEraserRect(point);
|
106
|
+
const rect2 = this.getEraserRect(this.lastPoint ?? point);
|
37
107
|
const fill = {
|
38
|
-
fill: Color4.
|
108
|
+
fill: Color4.transparent,
|
109
|
+
stroke: { width: size / 10, color: Color4.gray },
|
39
110
|
};
|
40
|
-
renderer.
|
111
|
+
renderer.drawPath(pathToRenderable(Path.fromConvexHullOf([...rect.corners, ...rect2.corners]), fill));
|
41
112
|
}
|
113
|
+
/**
|
114
|
+
* @returns the eraser rectangle in canvas coordinates.
|
115
|
+
*
|
116
|
+
* For now, all erasers are rectangles or points.
|
117
|
+
*/
|
42
118
|
getEraserRect(centerPoint) {
|
43
119
|
const size = this.getSizeOnCanvas();
|
44
120
|
const halfSize = Vec2.of(size / 2, size / 2);
|
45
121
|
return Rect2.fromCorners(centerPoint.minus(halfSize), centerPoint.plus(halfSize));
|
46
122
|
}
|
123
|
+
/** Erases in a line from the last point to the current. */
|
47
124
|
eraseTo(currentPoint) {
|
48
125
|
if (!this.isFirstEraseEvt && currentPoint.distanceTo(this.lastPoint) === 0) {
|
49
126
|
return;
|
@@ -60,13 +137,55 @@ export default class Eraser extends BaseTool {
|
|
60
137
|
});
|
61
138
|
// Only erase components that could be selected (and thus interacted with)
|
62
139
|
// by the user.
|
63
|
-
const
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
140
|
+
const eraseableElems = intersectingElems.filter(elem => elem.isSelectable());
|
141
|
+
if (this.modeValue.get() === EraserMode.FullStroke) {
|
142
|
+
// Remove any intersecting elements.
|
143
|
+
this.toRemove.push(...eraseableElems);
|
144
|
+
// Create new Erase commands for the now-to-be-erased elements and apply them.
|
145
|
+
const newPartialCommands = eraseableElems.map(elem => new Erase([elem]));
|
146
|
+
newPartialCommands.forEach(cmd => cmd.apply(this.editor));
|
147
|
+
this.eraseCommands.push(...newPartialCommands);
|
148
|
+
}
|
149
|
+
else {
|
150
|
+
const toErase = [];
|
151
|
+
const toAdd = [];
|
152
|
+
for (const targetElem of eraseableElems) {
|
153
|
+
toErase.push(targetElem);
|
154
|
+
// Completely delete items that can't be divided.
|
155
|
+
if (!targetElem.withRegionErased) {
|
156
|
+
continue;
|
157
|
+
}
|
158
|
+
// Completely delete items that are completely or almost completely
|
159
|
+
// contained within the eraser.
|
160
|
+
const grownRect = eraserRect.grownBy(eraserRect.maxDimension / 3);
|
161
|
+
if (grownRect.containsRect(targetElem.getExactBBox())) {
|
162
|
+
continue;
|
163
|
+
}
|
164
|
+
// Join the current and previous rectangles so that points between events are also
|
165
|
+
// erased.
|
166
|
+
const erasePath = Path.fromConvexHullOf([
|
167
|
+
...eraserRect.corners, ...this.getEraserRect(this.lastPoint ?? currentPoint).corners
|
168
|
+
].map(p => this.editor.viewport.roundPoint(p)));
|
169
|
+
toAdd.push(...targetElem.withRegionErased(erasePath, this.editor.viewport));
|
170
|
+
}
|
171
|
+
const eraseCommand = new Erase(toErase);
|
172
|
+
const newAddCommands = toAdd.map(elem => EditorImage.addElement(elem));
|
173
|
+
eraseCommand.apply(this.editor);
|
174
|
+
newAddCommands.forEach(command => command.apply(this.editor));
|
175
|
+
const finalToErase = [];
|
176
|
+
for (const item of toErase) {
|
177
|
+
if (this.toAdd.includes(item)) {
|
178
|
+
this.toAdd = this.toAdd.filter(i => i !== item);
|
179
|
+
}
|
180
|
+
else {
|
181
|
+
finalToErase.push(item);
|
182
|
+
}
|
183
|
+
}
|
184
|
+
this.toRemove.push(...finalToErase);
|
185
|
+
this.toAdd.push(...toAdd);
|
186
|
+
this.eraseCommands.push(new Erase(finalToErase));
|
187
|
+
this.addCommands.push(...newAddCommands);
|
188
|
+
}
|
70
189
|
this.drawPreviewAt(currentPoint);
|
71
190
|
this.lastPoint = currentPoint;
|
72
191
|
}
|
@@ -74,6 +193,7 @@ export default class Eraser extends BaseTool {
|
|
74
193
|
if (event.allPointers.length === 1 || event.current.device === PointerDevice.Eraser) {
|
75
194
|
this.lastPoint = event.current.canvasPos;
|
76
195
|
this.toRemove = [];
|
196
|
+
this.toAdd = [];
|
77
197
|
this.isFirstEraseEvt = true;
|
78
198
|
this.drawPreviewAt(event.current.canvasPos);
|
79
199
|
return true;
|
@@ -86,18 +206,32 @@ export default class Eraser extends BaseTool {
|
|
86
206
|
}
|
87
207
|
onPointerUp(event) {
|
88
208
|
this.eraseTo(event.current.canvasPos);
|
89
|
-
|
209
|
+
const commands = [];
|
210
|
+
if (this.addCommands.length > 0) {
|
211
|
+
this.addCommands.forEach(cmd => cmd.unapply(this.editor));
|
212
|
+
commands.push(...this.toAdd.map(a => EditorImage.addElement(a)));
|
213
|
+
this.addCommands = [];
|
214
|
+
}
|
215
|
+
if (this.eraseCommands.length > 0) {
|
90
216
|
// Undo commands for each individual component and unite into a single command.
|
91
|
-
this.
|
92
|
-
this.
|
217
|
+
this.eraseCommands.forEach(cmd => cmd.unapply(this.editor));
|
218
|
+
this.eraseCommands = [];
|
93
219
|
const command = new Erase(this.toRemove);
|
94
|
-
|
220
|
+
commands.push(command);
|
221
|
+
}
|
222
|
+
if (commands.length === 1) {
|
223
|
+
this.editor.dispatch(commands[0]); // dispatch: Makes undo-able.
|
224
|
+
}
|
225
|
+
else {
|
226
|
+
this.editor.dispatch(uniteCommands(commands));
|
95
227
|
}
|
96
228
|
this.clearPreview();
|
97
229
|
}
|
98
|
-
onGestureCancel() {
|
99
|
-
this.
|
100
|
-
this.
|
230
|
+
onGestureCancel(_event) {
|
231
|
+
this.addCommands.forEach(cmd => cmd.unapply(this.editor));
|
232
|
+
this.eraseCommands.forEach(cmd => cmd.unapply(this.editor));
|
233
|
+
this.eraseCommands = [];
|
234
|
+
this.addCommands = [];
|
101
235
|
this.clearPreview();
|
102
236
|
}
|
103
237
|
onKeyPress(event) {
|
@@ -116,9 +250,14 @@ export default class Eraser extends BaseTool {
|
|
116
250
|
}
|
117
251
|
return false;
|
118
252
|
}
|
253
|
+
/** Returns the side-length of the tip of this eraser. */
|
119
254
|
getThickness() {
|
120
255
|
return this.thickness;
|
121
256
|
}
|
257
|
+
/** Sets the side-length of this' tip. */
|
258
|
+
setThickness(thickness) {
|
259
|
+
this.thicknessValue.set(thickness);
|
260
|
+
}
|
122
261
|
/**
|
123
262
|
* Returns a {@link MutableReactiveValue} that can be used to watch
|
124
263
|
* this tool's thickness.
|
@@ -126,7 +265,7 @@ export default class Eraser extends BaseTool {
|
|
126
265
|
getThicknessValue() {
|
127
266
|
return this.thicknessValue;
|
128
267
|
}
|
129
|
-
|
130
|
-
this.
|
268
|
+
getModeValue() {
|
269
|
+
return this.modeValue;
|
131
270
|
}
|
132
271
|
}
|
@@ -10,7 +10,7 @@ import AbstractComponent from '../../components/AbstractComponent';
|
|
10
10
|
import Command from '../../commands/Command';
|
11
11
|
export default class Selection {
|
12
12
|
private editor;
|
13
|
-
private
|
13
|
+
private childwidgets;
|
14
14
|
private originalRegion;
|
15
15
|
private selectionTightBoundingBox;
|
16
16
|
private transformers;
|
@@ -52,7 +52,7 @@ export default class Selection {
|
|
52
52
|
private removedFromImage;
|
53
53
|
private addRemoveSelectionFromImage;
|
54
54
|
private removeDeletedElemsFromSelection;
|
55
|
-
private
|
55
|
+
private activeHandle;
|
56
56
|
private backgroundDragging;
|
57
57
|
onDragStart(pointer: Pointer): boolean;
|
58
58
|
onDragUpdate(pointer: Pointer): void;
|
@@ -29,7 +29,7 @@ class Selection {
|
|
29
29
|
this.hasParent = true;
|
30
30
|
// Maps IDs to whether we removed the component from the image
|
31
31
|
this.removedFromImage = {};
|
32
|
-
this.
|
32
|
+
this.activeHandle = null;
|
33
33
|
this.backgroundDragging = false;
|
34
34
|
this.selectionDuplicatedAnimationTimeout = null;
|
35
35
|
this.originalRegion = new Rect2(startPoint.x, startPoint.y, 0, 0);
|
@@ -71,14 +71,14 @@ class Selection {
|
|
71
71
|
side: Vec2.of(0.5, 0),
|
72
72
|
icon: this.editor.icons.makeRotateIcon(),
|
73
73
|
}, this, this.editor.viewport, (startPoint) => this.transformers.rotate.onDragStart(startPoint), (currentPoint) => this.transformers.rotate.onDragUpdate(currentPoint), () => this.transformers.rotate.onDragEnd());
|
74
|
-
this.
|
74
|
+
this.childwidgets = [
|
75
75
|
resizeBothHandle,
|
76
76
|
...resizeHorizontalHandles,
|
77
77
|
resizeVerticalHandle,
|
78
78
|
rotationHandle,
|
79
79
|
];
|
80
|
-
for (const
|
81
|
-
|
80
|
+
for (const widget of this.childwidgets) {
|
81
|
+
widget.addTo(this.backgroundElem);
|
82
82
|
}
|
83
83
|
this.updateUI();
|
84
84
|
}
|
@@ -293,8 +293,8 @@ class Selection {
|
|
293
293
|
else {
|
294
294
|
this.innerContainer.classList.remove('-empty');
|
295
295
|
}
|
296
|
-
for (const
|
297
|
-
|
296
|
+
for (const widget of this.childwidgets) {
|
297
|
+
widget.updatePosition(this.getScreenRegion());
|
298
298
|
}
|
299
299
|
}
|
300
300
|
// Add/remove the contents of this seleciton from the editor.
|
@@ -348,16 +348,16 @@ class Selection {
|
|
348
348
|
onDragStart(pointer) {
|
349
349
|
// Clear the HTML selection (prevent HTML drag and drop being triggered by this drag)
|
350
350
|
document.getSelection()?.removeAllRanges();
|
351
|
-
this.
|
351
|
+
this.activeHandle = null;
|
352
352
|
let result = false;
|
353
353
|
this.backgroundDragging = false;
|
354
354
|
if (this.region.containsPoint(pointer.canvasPos)) {
|
355
355
|
this.backgroundDragging = true;
|
356
356
|
result = true;
|
357
357
|
}
|
358
|
-
for (const
|
359
|
-
if (
|
360
|
-
this.
|
358
|
+
for (const widget of this.childwidgets) {
|
359
|
+
if (widget.containsPoint(pointer.canvasPos)) {
|
360
|
+
this.activeHandle = widget;
|
361
361
|
this.backgroundDragging = false;
|
362
362
|
result = true;
|
363
363
|
}
|
@@ -366,8 +366,8 @@ class Selection {
|
|
366
366
|
this.removeDeletedElemsFromSelection();
|
367
367
|
this.addRemoveSelectionFromImage(false);
|
368
368
|
}
|
369
|
-
if (this.
|
370
|
-
this.
|
369
|
+
if (this.activeHandle) {
|
370
|
+
this.activeHandle.handleDragStart(pointer);
|
371
371
|
}
|
372
372
|
if (this.backgroundDragging) {
|
373
373
|
this.transformers.drag.onDragStart(pointer.canvasPos);
|
@@ -378,25 +378,25 @@ class Selection {
|
|
378
378
|
if (this.backgroundDragging) {
|
379
379
|
this.transformers.drag.onDragUpdate(pointer.canvasPos);
|
380
380
|
}
|
381
|
-
if (this.
|
382
|
-
this.
|
381
|
+
if (this.activeHandle) {
|
382
|
+
this.activeHandle.handleDragUpdate(pointer);
|
383
383
|
}
|
384
384
|
}
|
385
385
|
onDragEnd() {
|
386
386
|
if (this.backgroundDragging) {
|
387
387
|
this.transformers.drag.onDragEnd();
|
388
388
|
}
|
389
|
-
else if (this.
|
390
|
-
this.
|
389
|
+
else if (this.activeHandle) {
|
390
|
+
this.activeHandle.handleDragEnd();
|
391
391
|
}
|
392
392
|
this.addRemoveSelectionFromImage(true);
|
393
393
|
this.backgroundDragging = false;
|
394
|
-
this.
|
394
|
+
this.activeHandle = null;
|
395
395
|
this.updateUI();
|
396
396
|
}
|
397
397
|
onDragCancel() {
|
398
398
|
this.backgroundDragging = false;
|
399
|
-
this.
|
399
|
+
this.activeHandle = null;
|
400
400
|
this.setTransform(Mat33.identity);
|
401
401
|
this.addRemoveSelectionFromImage(true);
|
402
402
|
this.updateUI();
|
@@ -424,7 +424,7 @@ class Selection {
|
|
424
424
|
return false;
|
425
425
|
}
|
426
426
|
deleteSelectedObjects() {
|
427
|
-
if (this.backgroundDragging || this.
|
427
|
+
if (this.backgroundDragging || this.activeHandle) {
|
428
428
|
this.onDragEnd();
|
429
429
|
}
|
430
430
|
return new Erase(this.selectedElems);
|
@@ -441,7 +441,7 @@ class Selection {
|
|
441
441
|
}, animationDuration);
|
442
442
|
}
|
443
443
|
async duplicateSelectedObjects() {
|
444
|
-
const wasTransforming = this.backgroundDragging || this.
|
444
|
+
const wasTransforming = this.backgroundDragging || this.activeHandle;
|
445
445
|
let tmpApplyCommand = null;
|
446
446
|
if (!wasTransforming) {
|
447
447
|
this.runSelectionDuplicatedAnimation();
|
@@ -2,6 +2,7 @@ import { Point2, Vec2 } from '@js-draw/math';
|
|
2
2
|
import Selection from './Selection';
|
3
3
|
import Pointer from '../../Pointer';
|
4
4
|
import Viewport from '../../Viewport';
|
5
|
+
import { SelectionBoxChild } from './types';
|
5
6
|
export declare enum HandleAction {
|
6
7
|
ResizeXY = "resize-xy",
|
7
8
|
Rotate = "rotate",
|
@@ -17,7 +18,7 @@ export declare const handleSize = 30;
|
|
17
18
|
export type DragStartCallback = (startPoint: Point2) => void;
|
18
19
|
export type DragUpdateCallback = (canvasPoint: Point2) => void;
|
19
20
|
export type DragEndCallback = () => Promise<void> | void;
|
20
|
-
export default class SelectionHandle {
|
21
|
+
export default class SelectionHandle implements SelectionBoxChild {
|
21
22
|
readonly presentation: HandlePresentation;
|
22
23
|
private readonly parent;
|
23
24
|
private readonly viewport;
|
@@ -34,6 +35,11 @@ export default class SelectionHandle {
|
|
34
35
|
* element visible on the screen.
|
35
36
|
*/
|
36
37
|
addTo(container: HTMLElement): void;
|
38
|
+
/**
|
39
|
+
* Removes this element from its container. Should only be called
|
40
|
+
* after {@link addTo}.
|
41
|
+
*/
|
42
|
+
remove(): void;
|
37
43
|
/**
|
38
44
|
* Returns this handle's bounding box relative to the top left of the
|
39
45
|
* selection box.
|
@@ -48,7 +54,7 @@ export default class SelectionHandle {
|
|
48
54
|
/** @returns true iff `point` (in editor **canvas** coordinates) is in this. */
|
49
55
|
containsPoint(point: Point2): boolean;
|
50
56
|
private dragLastPos;
|
51
|
-
handleDragStart(pointer: Pointer):
|
57
|
+
handleDragStart(pointer: Pointer): boolean;
|
52
58
|
handleDragUpdate(pointer: Pointer): void;
|
53
59
|
handleDragEnd(): void | Promise<void>;
|
54
60
|
setSnapToGrid(snap: boolean): void;
|
@@ -61,6 +61,11 @@ export default class SelectionHandle {
|
|
61
61
|
addTo(container) {
|
62
62
|
container.appendChild(this.element);
|
63
63
|
}
|
64
|
+
/**
|
65
|
+
* Removes this element from its container. Should only be called
|
66
|
+
* after {@link addTo}.
|
67
|
+
*/
|
68
|
+
remove() { this.element.remove(); }
|
64
69
|
/**
|
65
70
|
* Returns this handle's bounding box relative to the top left of the
|
66
71
|
* selection box.
|
@@ -109,6 +114,7 @@ export default class SelectionHandle {
|
|
109
114
|
handleDragStart(pointer) {
|
110
115
|
this.onDragStart(pointer.canvasPos);
|
111
116
|
this.dragLastPos = pointer.canvasPos;
|
117
|
+
return true;
|
112
118
|
}
|
113
119
|
handleDragUpdate(pointer) {
|
114
120
|
if (!this.dragLastPos) {
|
@@ -1,3 +1,5 @@
|
|
1
|
+
import type { Rect2, Point2 } from '@js-draw/math';
|
2
|
+
import Pointer from '../../Pointer';
|
1
3
|
export declare enum ResizeMode {
|
2
4
|
Both = 0,
|
3
5
|
HorizontalOnly = 1,
|
@@ -7,3 +9,20 @@ export declare enum TransformMode {
|
|
7
9
|
Snap = 0,
|
8
10
|
NoSnap = 1
|
9
11
|
}
|
12
|
+
/**
|
13
|
+
* Represents a child of the selection that should move with the selection
|
14
|
+
* and handle events.
|
15
|
+
*
|
16
|
+
* Although selection children should be `HTMLElement`s, the selection may be
|
17
|
+
* hidden behind an invisible element. As such, these elements should handle
|
18
|
+
* drag start/update/end events.
|
19
|
+
*/
|
20
|
+
export interface SelectionBoxChild {
|
21
|
+
updatePosition(selectionScreenBBox: Rect2): void;
|
22
|
+
containsPoint(point: Point2): boolean;
|
23
|
+
addTo(container: HTMLElement): void;
|
24
|
+
remove(): void;
|
25
|
+
handleDragStart(pointer: Pointer): boolean;
|
26
|
+
handleDragUpdate(pointer: Pointer): void;
|
27
|
+
handleDragEnd(): void;
|
28
|
+
}
|