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