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,276 @@
|
|
1
|
+
import ImageComponent from '../../../components/ImageComponent.mjs';
|
2
|
+
import Erase from '../../../commands/Erase.mjs';
|
3
|
+
import EditorImage from '../../../image/EditorImage.mjs';
|
4
|
+
import uniteCommands from '../../../commands/uniteCommands.mjs';
|
5
|
+
import SelectionTool from '../../../tools/SelectionTool/SelectionTool.mjs';
|
6
|
+
import { Mat33, Vec2 } from '@js-draw/math';
|
7
|
+
import BaseWidget from '../BaseWidget.mjs';
|
8
|
+
import { EditorEventType } from '../../../types.mjs';
|
9
|
+
import { toolbarCSSPrefix } from '../../constants.mjs';
|
10
|
+
import makeFileInput from '../components/makeFileInput.mjs';
|
11
|
+
import { MutableReactiveValue } from '../../../util/ReactiveValue.mjs';
|
12
|
+
import bytesToSizeString from '../../../util/bytesToSizeString.mjs';
|
13
|
+
import { ImageWrapper } from './ImageWrapper.mjs';
|
14
|
+
import makeSnappedList from '../components/makeSnappedList.mjs';
|
15
|
+
import fileToImages from './fileToImages.mjs';
|
16
|
+
/**
|
17
|
+
* Provides a widget that allows inserting or modifying raster images.
|
18
|
+
*
|
19
|
+
* It's possible to customize the file picker used by this widget through {@link EditorSettings.image}.
|
20
|
+
*
|
21
|
+
* @example
|
22
|
+
* ```ts,runnable
|
23
|
+
* import { Editor, makeEdgeToolbar, InsertImageWidget } from 'js-draw';
|
24
|
+
*
|
25
|
+
* const editor = new Editor(document.body);
|
26
|
+
* const toolbar = makeEdgeToolbar(editor);
|
27
|
+
*
|
28
|
+
* toolbar.addWidget(new InsertImageWidget(editor));
|
29
|
+
* ```
|
30
|
+
*/
|
31
|
+
class InsertImageWidget extends BaseWidget {
|
32
|
+
constructor(editor, localization) {
|
33
|
+
localization ??= editor.localization;
|
34
|
+
super(editor, 'insert-image-widget', localization);
|
35
|
+
// Make the dropdown showable
|
36
|
+
this.container.classList.add('dropdownShowable');
|
37
|
+
editor.notifier.on(EditorEventType.SelectionUpdated, event => {
|
38
|
+
if (event.kind === EditorEventType.SelectionUpdated && this.isDropdownVisible()) {
|
39
|
+
this.updateInputs();
|
40
|
+
}
|
41
|
+
});
|
42
|
+
this.images = MutableReactiveValue.fromInitialValue([]);
|
43
|
+
this.images.onUpdateAndNow(() => {
|
44
|
+
this.onImageDataUpdate();
|
45
|
+
});
|
46
|
+
}
|
47
|
+
getTitle() {
|
48
|
+
return this.localizationTable.image;
|
49
|
+
}
|
50
|
+
createIcon() {
|
51
|
+
return this.editor.icons.makeInsertImageIcon();
|
52
|
+
}
|
53
|
+
setDropdownVisible(visible) {
|
54
|
+
super.setDropdownVisible(visible);
|
55
|
+
// Update the dropdown just before showing.
|
56
|
+
if (this.isDropdownVisible()) {
|
57
|
+
this.updateInputs();
|
58
|
+
}
|
59
|
+
else {
|
60
|
+
// Allow any previously-selected files to be freed.
|
61
|
+
this.selectedFiles?.set([]);
|
62
|
+
}
|
63
|
+
}
|
64
|
+
handleClick() {
|
65
|
+
this.setDropdownVisible(!this.isDropdownVisible());
|
66
|
+
}
|
67
|
+
fillDropdown(dropdown) {
|
68
|
+
const container = document.createElement('div');
|
69
|
+
container.classList.add('insert-image-widget-dropdown-content', `${toolbarCSSPrefix}spacedList`, `${toolbarCSSPrefix}nonbutton-controls-main-list`);
|
70
|
+
const { container: chooseImageRow, selectedFiles, } = makeFileInput(this.localizationTable.chooseFile, this.editor, {
|
71
|
+
accepts: 'image/*',
|
72
|
+
allowMultiSelect: true,
|
73
|
+
customPickerAction: this.editor.getCurrentSettings().image?.showImagePicker,
|
74
|
+
});
|
75
|
+
const altTextRow = document.createElement('div');
|
76
|
+
this.imagesPreview = makeSnappedList(this.images);
|
77
|
+
this.statusView = document.createElement('div');
|
78
|
+
const actionButtonRow = document.createElement('div');
|
79
|
+
actionButtonRow.classList.add('action-button-row');
|
80
|
+
this.statusView.classList.add('insert-image-image-status-view');
|
81
|
+
this.submitButton = document.createElement('button');
|
82
|
+
this.selectedFiles = selectedFiles;
|
83
|
+
this.imageAltTextInput = document.createElement('input');
|
84
|
+
// Label the alt text input
|
85
|
+
const imageAltTextLabel = document.createElement('label');
|
86
|
+
const altTextInputId = `insert-image-alt-text-input-${InsertImageWidget.nextInputId++}`;
|
87
|
+
this.imageAltTextInput.setAttribute('id', altTextInputId);
|
88
|
+
imageAltTextLabel.htmlFor = altTextInputId;
|
89
|
+
imageAltTextLabel.innerText = this.localizationTable.inputAltText;
|
90
|
+
this.imageAltTextInput.type = 'text';
|
91
|
+
this.imageAltTextInput.placeholder = this.localizationTable.describeTheImage;
|
92
|
+
this.statusView.setAttribute('aria-live', 'polite');
|
93
|
+
this.submitButton.innerText = this.localizationTable.submit;
|
94
|
+
this.imagesPreview.visibleItem.onUpdateAndNow(() => this.onImageDataUpdate());
|
95
|
+
this.imageAltTextInput.oninput = () => {
|
96
|
+
const currentImage = this.imagesPreview.visibleItem.get();
|
97
|
+
if (currentImage) {
|
98
|
+
currentImage.setAltText(this.imageAltTextInput.value);
|
99
|
+
this.submitButton.style.display = '';
|
100
|
+
}
|
101
|
+
};
|
102
|
+
this.selectedFiles.onUpdateAndNow(async (files) => {
|
103
|
+
if (files.length === 0) {
|
104
|
+
this.images.set([]);
|
105
|
+
return;
|
106
|
+
}
|
107
|
+
const previews = (await Promise.all(files.map(async (imageFile) => {
|
108
|
+
let renderableImages;
|
109
|
+
try {
|
110
|
+
renderableImages = await fileToImages(imageFile);
|
111
|
+
}
|
112
|
+
catch (error) {
|
113
|
+
console.error('Image load error', error);
|
114
|
+
const errorMessage = this.localizationTable.imageLoadError(error);
|
115
|
+
this.statusView.innerText = errorMessage;
|
116
|
+
return [];
|
117
|
+
}
|
118
|
+
return renderableImages.map(image => {
|
119
|
+
const { wrapper, preview } = ImageWrapper.fromRenderable(image, () => this.onImageDataUpdate());
|
120
|
+
return {
|
121
|
+
data: wrapper,
|
122
|
+
element: preview,
|
123
|
+
};
|
124
|
+
});
|
125
|
+
}))).flat();
|
126
|
+
this.images.set(previews);
|
127
|
+
});
|
128
|
+
altTextRow.replaceChildren(imageAltTextLabel, this.imageAltTextInput);
|
129
|
+
actionButtonRow.replaceChildren(this.submitButton);
|
130
|
+
container.replaceChildren(chooseImageRow, altTextRow, this.imagesPreview.container, this.statusView, actionButtonRow);
|
131
|
+
dropdown.replaceChildren(container);
|
132
|
+
return true;
|
133
|
+
}
|
134
|
+
onImageDataUpdate() {
|
135
|
+
if (!this.imagesPreview)
|
136
|
+
return;
|
137
|
+
const currentImage = this.imagesPreview.visibleItem.get();
|
138
|
+
const base64Data = currentImage?.getBase64Url();
|
139
|
+
this.imageAltTextInput.value = currentImage?.getAltText() ?? '';
|
140
|
+
if (base64Data) {
|
141
|
+
this.submitButton.disabled = false;
|
142
|
+
this.submitButton.style.display = '';
|
143
|
+
this.updateImageSizeDisplay();
|
144
|
+
}
|
145
|
+
else {
|
146
|
+
this.submitButton.disabled = true;
|
147
|
+
this.submitButton.style.display = 'none';
|
148
|
+
this.statusView.innerText = '';
|
149
|
+
this.submitButton.disabled = true;
|
150
|
+
}
|
151
|
+
if (this.images.get().length <= 1) {
|
152
|
+
this.submitButton.innerText = this.localizationTable.submit;
|
153
|
+
}
|
154
|
+
else {
|
155
|
+
this.submitButton.innerText = this.localizationTable.addAll;
|
156
|
+
}
|
157
|
+
}
|
158
|
+
hideDialog() {
|
159
|
+
this.setDropdownVisible(false);
|
160
|
+
}
|
161
|
+
updateImageSizeDisplay() {
|
162
|
+
const currentImage = this.imagesPreview.visibleItem.get();
|
163
|
+
const imageData = currentImage?.getBase64Url() ?? '';
|
164
|
+
const { size, units } = bytesToSizeString(imageData.length);
|
165
|
+
const sizeText = document.createElement('span');
|
166
|
+
sizeText.innerText = this.localizationTable.imageSize(Math.round(size), units);
|
167
|
+
// Add a button to allow decreasing the size of large images.
|
168
|
+
const decreaseSizeButton = document.createElement('button');
|
169
|
+
decreaseSizeButton.innerText = this.localizationTable.decreaseImageSize;
|
170
|
+
decreaseSizeButton.onclick = () => {
|
171
|
+
currentImage?.decreaseSize();
|
172
|
+
};
|
173
|
+
const resetSizeButton = document.createElement('button');
|
174
|
+
resetSizeButton.innerText = this.localizationTable.resetImage;
|
175
|
+
resetSizeButton.onclick = () => {
|
176
|
+
currentImage?.reset();
|
177
|
+
};
|
178
|
+
this.statusView.replaceChildren(sizeText);
|
179
|
+
const largeImageThreshold = 0.12 * 1024 * 1024; // 0.12 MiB
|
180
|
+
if (imageData.length > largeImageThreshold) {
|
181
|
+
this.statusView.appendChild(decreaseSizeButton);
|
182
|
+
}
|
183
|
+
else if (currentImage?.isChanged()) {
|
184
|
+
this.statusView.appendChild(resetSizeButton);
|
185
|
+
}
|
186
|
+
}
|
187
|
+
updateInputs() {
|
188
|
+
const resetInputs = () => {
|
189
|
+
this.selectedFiles?.set([]);
|
190
|
+
this.imageAltTextInput.value = '';
|
191
|
+
this.submitButton.disabled = true;
|
192
|
+
this.statusView.innerText = '';
|
193
|
+
this.submitButton.style.display = '';
|
194
|
+
};
|
195
|
+
resetInputs();
|
196
|
+
const selectionTools = this.editor.toolController.getMatchingTools(SelectionTool);
|
197
|
+
const selectedObjects = selectionTools.map(tool => tool.getSelectedObjects()).flat();
|
198
|
+
// Check: Is there a selected image that can be edited?
|
199
|
+
let editingImage = null;
|
200
|
+
if (selectedObjects.length === 1 && selectedObjects[0] instanceof ImageComponent) {
|
201
|
+
editingImage = selectedObjects[0];
|
202
|
+
const image = new Image();
|
203
|
+
const imageWrapper = ImageWrapper.fromSrcAndPreview(editingImage.getURL(), image, () => this.onImageDataUpdate());
|
204
|
+
imageWrapper.setAltText(editingImage.getAltText() ?? '');
|
205
|
+
this.images.set([{ data: imageWrapper, element: image }]);
|
206
|
+
}
|
207
|
+
else if (selectedObjects.length > 0) {
|
208
|
+
// If not, clear the selection.
|
209
|
+
selectionTools.forEach(tool => tool.clearSelection());
|
210
|
+
}
|
211
|
+
// Show the submit button only when there is data to submit.
|
212
|
+
this.submitButton.style.display = 'none';
|
213
|
+
this.submitButton.onclick = async () => {
|
214
|
+
const newComponents = [];
|
215
|
+
let transform = Mat33.identity;
|
216
|
+
let fullBBox = null;
|
217
|
+
for (const { data: imageWrapper } of this.images.get()) {
|
218
|
+
if (!imageWrapper) {
|
219
|
+
continue;
|
220
|
+
}
|
221
|
+
const image = new Image();
|
222
|
+
image.src = imageWrapper.getBase64Url();
|
223
|
+
image.setAttribute('alt', this.imageAltTextInput.value);
|
224
|
+
let component;
|
225
|
+
try {
|
226
|
+
component = await ImageComponent.fromImage(image, transform);
|
227
|
+
}
|
228
|
+
catch (error) {
|
229
|
+
console.error('Error loading image', error);
|
230
|
+
this.statusView.innerText = this.localizationTable.imageLoadError(error);
|
231
|
+
return;
|
232
|
+
}
|
233
|
+
const componentBBox = component.getBBox();
|
234
|
+
if (componentBBox.area === 0) {
|
235
|
+
this.statusView.innerText = this.localizationTable.errorImageHasZeroSize;
|
236
|
+
return;
|
237
|
+
}
|
238
|
+
newComponents.push(component);
|
239
|
+
fullBBox ??= componentBBox;
|
240
|
+
fullBBox.union(componentBBox);
|
241
|
+
// Update the transform for the next item.
|
242
|
+
const shift = Vec2.of(0, componentBBox.height);
|
243
|
+
transform = transform.rightMul(Mat33.translation(shift));
|
244
|
+
}
|
245
|
+
if (newComponents.length) {
|
246
|
+
if (!fullBBox) {
|
247
|
+
throw new Error('Logic error: Full bounding box must be calculated when components are to be added.');
|
248
|
+
}
|
249
|
+
this.hideDialog();
|
250
|
+
if (editingImage) {
|
251
|
+
const eraseCommand = new Erase([editingImage]);
|
252
|
+
// Try to preserve the original width
|
253
|
+
const originalTransform = editingImage.getTransformation();
|
254
|
+
// || 1: Prevent division by zero
|
255
|
+
const originalWidth = editingImage.getBBox().width || 1;
|
256
|
+
const newWidth = fullBBox.transformedBoundingBox(originalTransform).width || 1;
|
257
|
+
const widthAdjustTransform = Mat33.scaling2D(originalWidth / newWidth);
|
258
|
+
const commands = [];
|
259
|
+
for (const component of newComponents) {
|
260
|
+
commands.push(EditorImage.addElement(component), component.transformBy(originalTransform.rightMul(widthAdjustTransform)), component.setZIndex(editingImage.getZIndex()));
|
261
|
+
}
|
262
|
+
this.editor.dispatch(uniteCommands([
|
263
|
+
...commands,
|
264
|
+
eraseCommand,
|
265
|
+
]));
|
266
|
+
selectionTools[0]?.setSelection(newComponents);
|
267
|
+
}
|
268
|
+
else {
|
269
|
+
await this.editor.addAndCenterComponents(newComponents);
|
270
|
+
}
|
271
|
+
}
|
272
|
+
};
|
273
|
+
}
|
274
|
+
}
|
275
|
+
InsertImageWidget.nextInputId = 0;
|
276
|
+
export default InsertImageWidget;
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -12,6 +12,7 @@ import { selectStrokeTypeKeyboardShortcutIds } from './keybindings.mjs';
|
|
12
12
|
import { toolbarCSSPrefix } from '../constants.mjs';
|
13
13
|
import makeThicknessSlider from './components/makeThicknessSlider.mjs';
|
14
14
|
import makeGridSelector from './components/makeGridSelector.mjs';
|
15
|
+
import { makePolylineBuilder } from '../../components/builders/PolylineBuilder.mjs';
|
15
16
|
class PenToolWidget extends BaseToolWidget {
|
16
17
|
constructor(editor, tool, localization) {
|
17
18
|
super(editor, tool, 'pen', localization);
|
@@ -21,6 +22,7 @@ class PenToolWidget extends BaseToolWidget {
|
|
21
22
|
this.shapelikeIDs = ['pressure-sensitive-pen', 'freehand-pen'];
|
22
23
|
// Additional client-specified pens.
|
23
24
|
const additionalPens = editor.getCurrentSettings().pens?.additionalPenTypes ?? [];
|
25
|
+
const filterPens = editor.getCurrentSettings().pens?.filterPenTypes ?? (() => true);
|
24
26
|
// Default pen types
|
25
27
|
this.penTypes = [
|
26
28
|
// Non-shape pens
|
@@ -34,6 +36,11 @@ class PenToolWidget extends BaseToolWidget {
|
|
34
36
|
id: 'freehand-pen',
|
35
37
|
factory: makeFreehandLineBuilder,
|
36
38
|
},
|
39
|
+
{
|
40
|
+
name: this.localizationTable.roundedTipPen2,
|
41
|
+
id: 'polyline-pen',
|
42
|
+
factory: makePolylineBuilder,
|
43
|
+
},
|
37
44
|
...(additionalPens.filter(pen => !pen.isShapeBuilder)),
|
38
45
|
// Shape pens
|
39
46
|
{
|
@@ -67,7 +74,7 @@ class PenToolWidget extends BaseToolWidget {
|
|
67
74
|
factory: makeOutlinedCircleBuilder,
|
68
75
|
},
|
69
76
|
...(additionalPens.filter(pen => pen.isShapeBuilder)),
|
70
|
-
];
|
77
|
+
].filter(filterPens);
|
71
78
|
this.editor.notifier.on(EditorEventType.ToolUpdated, toolEvt => {
|
72
79
|
if (toolEvt.kind !== EditorEventType.ToolUpdated) {
|
73
80
|
throw new Error('Invalid event type!');
|
@@ -111,7 +118,7 @@ class PenToolWidget extends BaseToolWidget {
|
|
111
118
|
style.factory = record.factory;
|
112
119
|
}
|
113
120
|
const strokeFactory = record?.factory;
|
114
|
-
if (!strokeFactory || strokeFactory === makeFreehandLineBuilder || strokeFactory === makePressureSensitiveFreehandLineBuilder) {
|
121
|
+
if (!strokeFactory || strokeFactory === makeFreehandLineBuilder || strokeFactory === makePressureSensitiveFreehandLineBuilder || strokeFactory === makePolylineBuilder) {
|
115
122
|
return this.editor.icons.makePenIcon(style);
|
116
123
|
}
|
117
124
|
else {
|
@@ -131,7 +138,7 @@ class PenToolWidget extends BaseToolWidget {
|
|
131
138
|
isShapeBuilder: penType.isShapeBuilder ?? false,
|
132
139
|
};
|
133
140
|
});
|
134
|
-
const penSelector = makeGridSelector(this.localizationTable.
|
141
|
+
const penSelector = makeGridSelector(this.localizationTable.selectPenType, this.getCurrentPenTypeIdx(), allChoices.filter(choice => !choice.isShapeBuilder));
|
135
142
|
const shapeSelector = makeGridSelector(this.localizationTable.selectShape, this.getCurrentPenTypeIdx(), allChoices.filter(choice => choice.isShapeBuilder));
|
136
143
|
const onSelectorUpdate = (newPenTypeIndex) => {
|
137
144
|
this.tool.setStrokeFactory(this.penTypes[newPenTypeIndex].factory);
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -54,9 +54,11 @@ class TextToolWidget extends BaseToolWidget {
|
|
54
54
|
colorLabel.setAttribute('for', colorInput.id);
|
55
55
|
sizeInput.id = `${toolbarCSSPrefix}-text-size-input-${TextToolWidget.idCounter++}`;
|
56
56
|
sizeLabel.setAttribute('for', sizeInput.id);
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
const defaultFonts = this.editor.getCurrentSettings().text?.fonts ?? [];
|
58
|
+
for (const font of defaultFonts) {
|
59
|
+
addFontToInput(font);
|
60
|
+
}
|
61
|
+
fontInput.classList.add('font-selector');
|
60
62
|
fontInput.id = `${toolbarCSSPrefix}-text-font-input-${TextToolWidget.idCounter++}`;
|
61
63
|
fontLabel.setAttribute('for', fontInput.id);
|
62
64
|
fontInput.onchange = () => {
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -1,9 +1,19 @@
|
|
1
1
|
import { MutableReactiveValue } from '../../../util/ReactiveValue';
|
2
2
|
import { ToolbarContext } from '../../types';
|
3
|
+
export interface CustomFilePickerProps {
|
4
|
+
setOnCancelCallback(onCancel: () => void): void;
|
5
|
+
}
|
6
|
+
export type ShowCustomFilePickerCallback = (props: CustomFilePickerProps) => Promise<File[] | null>;
|
7
|
+
export interface FileInputOptions {
|
8
|
+
readonly accepts?: string;
|
9
|
+
readonly allowMultiSelect?: boolean;
|
10
|
+
readonly customPickerAction?: ShowCustomFilePickerCallback;
|
11
|
+
}
|
3
12
|
/**
|
4
|
-
* Creates a stylized file input.
|
13
|
+
* Creates a stylized file input. This file input can either use the system file picker, or a custom
|
14
|
+
* one specified by `customPickerAction`.
|
5
15
|
*/
|
6
|
-
declare const makeFileInput: (labelText: string, context: ToolbarContext, accepts?:
|
16
|
+
declare const makeFileInput: (labelText: string, context: ToolbarContext, { accepts, allowMultiSelect, customPickerAction }?: FileInputOptions) => {
|
7
17
|
container: HTMLDivElement;
|
8
18
|
input: HTMLInputElement;
|
9
19
|
selectedFiles: MutableReactiveValue<File[]>;
|
@@ -1,9 +1,10 @@
|
|
1
1
|
import ReactiveValue from '../../../util/ReactiveValue.mjs';
|
2
2
|
let idCounter = 0;
|
3
3
|
/**
|
4
|
-
* Creates a stylized file input.
|
4
|
+
* Creates a stylized file input. This file input can either use the system file picker, or a custom
|
5
|
+
* one specified by `customPickerAction`.
|
5
6
|
*/
|
6
|
-
const makeFileInput = (labelText, context, accepts = '*') => {
|
7
|
+
const makeFileInput = (labelText, context, { accepts = '*', allowMultiSelect = false, customPickerAction } = {}) => {
|
7
8
|
const container = document.createElement('div');
|
8
9
|
const label = document.createElement('label');
|
9
10
|
const input = document.createElement('input');
|
@@ -13,7 +14,9 @@ const makeFileInput = (labelText, context, accepts = '*') => {
|
|
13
14
|
container.classList.add('toolbar--file-input-container');
|
14
15
|
label.appendChild(document.createTextNode(labelText));
|
15
16
|
input.accept = accepts;
|
16
|
-
input.type = 'file';
|
17
|
+
input.type = customPickerAction ? 'button' : 'file';
|
18
|
+
input.classList.add('file-input');
|
19
|
+
input.multiple = allowMultiSelect;
|
17
20
|
// Associate the label with the input
|
18
21
|
const inputId = `js-draw-file-input-${idCounter++}`;
|
19
22
|
input.setAttribute('id', inputId);
|
@@ -24,48 +27,22 @@ const makeFileInput = (labelText, context, accepts = '*') => {
|
|
24
27
|
label.appendChild(descriptionBox);
|
25
28
|
container.replaceChildren(label, input);
|
26
29
|
const selectedFiles = ReactiveValue.fromInitialValue([]);
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/relatedTarget
|
41
|
-
const enteringElement = event.relatedTarget;
|
42
|
-
if (!enteringElement || !label.contains(enteringElement)) {
|
43
|
-
label.classList.remove('drag-target');
|
44
|
-
}
|
45
|
-
});
|
46
|
-
// See https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/File_drag_and_drop#process_the_drop
|
47
|
-
label.addEventListener('drop', event => {
|
48
|
-
event.preventDefault();
|
49
|
-
label.classList.remove('drag-target');
|
50
|
-
const fileList = [];
|
51
|
-
if (event.dataTransfer) {
|
52
|
-
fileList.push(...event.dataTransfer.files);
|
53
|
-
}
|
54
|
-
selectedFiles.set(fileList);
|
55
|
-
});
|
56
|
-
input.addEventListener('change', () => {
|
57
|
-
const fileList = input.files ?? [];
|
58
|
-
selectedFiles.set([...fileList]);
|
59
|
-
});
|
60
|
-
selectedFiles.onUpdate(files => {
|
61
|
-
if (files.length === 0 && input.files && input.files.length > 0) {
|
62
|
-
input.value = '';
|
30
|
+
let loading = false;
|
31
|
+
let cancelLoading = null;
|
32
|
+
const updateStatusText = () => {
|
33
|
+
const files = selectedFiles.get();
|
34
|
+
if (loading) {
|
35
|
+
descriptionText.textContent = context.localization.fileInput__loading;
|
36
|
+
if (cancelLoading) {
|
37
|
+
const cancelText = document.createElement('b');
|
38
|
+
cancelText.textContent = context.localization.cancel;
|
39
|
+
cancelText.classList.add('cancel-button');
|
40
|
+
descriptionText.appendChild(cancelText);
|
41
|
+
}
|
42
|
+
icon.style.display = 'none';
|
63
43
|
}
|
64
|
-
|
65
|
-
|
66
|
-
selectedFiles.onUpdateAndNow(files => {
|
67
|
-
if (files.length > 0) {
|
68
|
-
descriptionText.innerText = files.map(file => file.name).join('\n');
|
44
|
+
else if (files.length > 0) {
|
45
|
+
descriptionText.textContent = files.map(file => file.name).join('\n');
|
69
46
|
// Only show the icon when there are files
|
70
47
|
icon.style.display = 'none';
|
71
48
|
}
|
@@ -85,7 +62,7 @@ const makeFileInput = (labelText, context, accepts = '*') => {
|
|
85
62
|
// Inside a {{pair of curly braces}}?
|
86
63
|
if (i % 2 === 1) {
|
87
64
|
const boldedText = document.createElement('b');
|
88
|
-
boldedText.
|
65
|
+
boldedText.textContent = segments[i];
|
89
66
|
descriptionText.appendChild(boldedText);
|
90
67
|
}
|
91
68
|
else {
|
@@ -93,7 +70,87 @@ const makeFileInput = (labelText, context, accepts = '*') => {
|
|
93
70
|
}
|
94
71
|
}
|
95
72
|
}
|
73
|
+
};
|
74
|
+
const addFileEventListeners = () => {
|
75
|
+
// Support dropping files
|
76
|
+
label.addEventListener('dragover', event => {
|
77
|
+
event.preventDefault();
|
78
|
+
label.classList.add('drag-target');
|
79
|
+
});
|
80
|
+
label.addEventListener('dragenter', event => {
|
81
|
+
event.preventDefault();
|
82
|
+
label.classList.add('drag-target');
|
83
|
+
});
|
84
|
+
label.addEventListener('dragleave', event => {
|
85
|
+
event.preventDefault();
|
86
|
+
// Ensure the event wasn't targeting a child.
|
87
|
+
// See https://stackoverflow.com/a/54271161 and
|
88
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/relatedTarget
|
89
|
+
const enteringElement = event.relatedTarget;
|
90
|
+
if (!enteringElement || !label.contains(enteringElement)) {
|
91
|
+
label.classList.remove('drag-target');
|
92
|
+
}
|
93
|
+
});
|
94
|
+
// See https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/File_drag_and_drop#process_the_drop
|
95
|
+
label.addEventListener('drop', event => {
|
96
|
+
event.preventDefault();
|
97
|
+
label.classList.remove('drag-target');
|
98
|
+
const fileList = [];
|
99
|
+
if (event.dataTransfer) {
|
100
|
+
fileList.push(...event.dataTransfer.files);
|
101
|
+
}
|
102
|
+
selectedFiles.set(fileList);
|
103
|
+
});
|
104
|
+
input.addEventListener('change', () => {
|
105
|
+
const fileList = input.files ?? [];
|
106
|
+
selectedFiles.set([...fileList]);
|
107
|
+
});
|
108
|
+
};
|
109
|
+
addFileEventListeners();
|
110
|
+
// Support for custom file pickers
|
111
|
+
if (customPickerAction) {
|
112
|
+
const promptForFiles = async () => {
|
113
|
+
if (loading) {
|
114
|
+
cancelLoading?.();
|
115
|
+
return;
|
116
|
+
}
|
117
|
+
container.classList.add('-loading');
|
118
|
+
loading = true;
|
119
|
+
updateStatusText();
|
120
|
+
try {
|
121
|
+
const data = await customPickerAction({
|
122
|
+
setOnCancelCallback: (onCancel) => {
|
123
|
+
if (!loading) {
|
124
|
+
throw new Error('Task already completed. Can\'t register cancel handler.');
|
125
|
+
}
|
126
|
+
cancelLoading = () => {
|
127
|
+
cancelLoading = null;
|
128
|
+
updateStatusText();
|
129
|
+
onCancel();
|
130
|
+
};
|
131
|
+
updateStatusText();
|
132
|
+
},
|
133
|
+
});
|
134
|
+
if (data) {
|
135
|
+
selectedFiles.set(data);
|
136
|
+
}
|
137
|
+
}
|
138
|
+
finally {
|
139
|
+
container.classList.remove('-loading');
|
140
|
+
loading = false;
|
141
|
+
updateStatusText();
|
142
|
+
}
|
143
|
+
};
|
144
|
+
input.onclick = promptForFiles;
|
145
|
+
}
|
146
|
+
selectedFiles.onUpdate(files => {
|
147
|
+
if (files.length === 0 && input.files && input.files.length > 0) {
|
148
|
+
input.value = '';
|
149
|
+
}
|
150
|
+
cancelLoading?.();
|
96
151
|
});
|
152
|
+
// Update the status text and hide/show the icon.
|
153
|
+
selectedFiles.onUpdateAndNow(updateStatusText);
|
97
154
|
return {
|
98
155
|
container,
|
99
156
|
input,
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { ReactiveValue } from '../../../util/ReactiveValue';
|
2
|
+
export interface SnappedListItem<DataType> {
|
3
|
+
element: HTMLElement;
|
4
|
+
data: DataType;
|
5
|
+
}
|
6
|
+
type SnappedListItems<DataType> = Array<SnappedListItem<DataType>>;
|
7
|
+
export interface SnappedListControl<DataType> {
|
8
|
+
container: HTMLElement;
|
9
|
+
visibleItem: ReactiveValue<DataType | null>;
|
10
|
+
}
|
11
|
+
/**
|
12
|
+
* Creates a list that snaps to each item and reports the selected item.
|
13
|
+
*/
|
14
|
+
declare const makeSnappedList: <DataType>(itemsValue: ReactiveValue<SnappedListItems<DataType>>) => SnappedListControl<DataType>;
|
15
|
+
export default makeSnappedList;
|