/
InsertImageWidget.ts
365 lines (292 loc) · 10.5 KB
/
InsertImageWidget.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import ImageComponent from '../../components/ImageComponent';
import Editor from '../../Editor';
import Erase from '../../commands/Erase';
import EditorImage from '../../image/EditorImage';
import uniteCommands from '../../commands/uniteCommands';
import SelectionTool from '../../tools/SelectionTool/SelectionTool';
import { Mat33 } from '@js-draw/math';
import fileToBase64Url from '../../util/fileToBase64Url';
import { ToolbarLocalization } from '../localization';
import BaseWidget from './BaseWidget';
import { EditorEventType } from '../../types';
import { toolbarCSSPrefix } from '../constants';
import makeFileInput from './components/makeFileInput';
import { MutableReactiveValue } from '../../util/ReactiveValue';
class ImageWrapper {
private readonly originalSrc: string;
private constructor(
private imageBase64Url: string,
private preview: HTMLImageElement,
private onUrlUpdate: ()=>void,
) {
this.originalSrc = imageBase64Url;
preview.src = imageBase64Url;
}
private updateImageData(base64DataUrl: string) {
this.preview.src = base64DataUrl;
this.imageBase64Url = base64DataUrl;
this.onUrlUpdate();
}
public decreaseSize(resizeFactor: number = 3/4) {
const canvas = document.createElement('canvas');
canvas.width = this.preview.naturalWidth * resizeFactor;
canvas.height = this.preview.naturalHeight * resizeFactor;
const ctx = canvas.getContext('2d');
ctx?.drawImage(this.preview, 0, 0, canvas.width, canvas.height);
// JPEG can be much smaller than PNG for the same image size. Prefer it if
// the image is already a JPEG.
const format =
this.originalSrc?.startsWith('data:image/jpeg;') ? 'image/jpeg' : 'image/png';
this.updateImageData(canvas.toDataURL(format));
}
public reset() {
this.updateImageData(this.originalSrc);
}
public isChanged() {
return this.imageBase64Url !== this.originalSrc;
}
public getBase64Url() {
return this.imageBase64Url;
}
public static fromSrcAndPreview(
initialBase64Src: string,
preview: HTMLImageElement,
onUrlUpdate: ()=>void,
) {
return new ImageWrapper(initialBase64Src, preview, onUrlUpdate);
}
}
export default class InsertImageWidget extends BaseWidget {
private imagePreview: HTMLImageElement;
private image: ImageWrapper|null = null;
private selectedFiles: MutableReactiveValue<File[]>|null;
private imageAltTextInput: HTMLInputElement;
private statusView: HTMLElement;
private submitButton: HTMLButtonElement;
public constructor(editor: Editor, localization?: ToolbarLocalization) {
localization ??= editor.localization;
super(editor, 'insert-image-widget', localization);
// Make the dropdown showable
this.container.classList.add('dropdownShowable');
editor.notifier.on(EditorEventType.SelectionUpdated, event => {
if (event.kind === EditorEventType.SelectionUpdated && this.isDropdownVisible()) {
this.updateInputs();
}
});
}
protected override getTitle(): string {
return this.localizationTable.image;
}
protected override createIcon(): Element | null {
return this.editor.icons.makeInsertImageIcon();
}
protected override setDropdownVisible(visible: boolean): void {
super.setDropdownVisible(visible);
// Update the dropdown just before showing.
if (this.isDropdownVisible()) {
this.updateInputs();
}
}
protected override handleClick() {
this.setDropdownVisible(!this.isDropdownVisible());
}
private static nextInputId = 0;
protected override fillDropdown(dropdown: HTMLElement): boolean {
const container = document.createElement('div');
container.classList.add(
'insert-image-widget-dropdown-content',
`${toolbarCSSPrefix}spacedList`,
`${toolbarCSSPrefix}nonbutton-controls-main-list`,
);
const {
container: chooseImageRow,
selectedFiles,
} = makeFileInput(
this.localizationTable.chooseFile,
this.editor,
'image/*',
);
const altTextRow = document.createElement('div');
this.imagePreview = document.createElement('img');
this.statusView = document.createElement('div');
const actionButtonRow = document.createElement('div');
actionButtonRow.classList.add('action-button-row');
this.statusView.classList.add('insert-image-image-status-view');
this.submitButton = document.createElement('button');
this.selectedFiles = selectedFiles;
this.imageAltTextInput = document.createElement('input');
// Label the alt text input
const imageAltTextLabel = document.createElement('label');
const altTextInputId = `insert-image-alt-text-input-${InsertImageWidget.nextInputId++}`;
this.imageAltTextInput.setAttribute('id', altTextInputId);
imageAltTextLabel.htmlFor = altTextInputId;
imageAltTextLabel.innerText = this.localizationTable.inputAltText;
this.imageAltTextInput.type = 'text';
this.imageAltTextInput.placeholder = this.localizationTable.describeTheImage;
this.statusView.setAttribute('aria-live', 'polite');
this.submitButton.innerText = this.localizationTable.submit;
this.selectedFiles.onUpdateAndNow(async files => {
if (files.length === 0) {
this.image = null;
this.onImageDataUpdate();
return;
}
this.imagePreview.style.display = 'block';
const image = files[0];
let data: string|null = null;
let errorMessage: string|null = null;
try {
data = await fileToBase64Url(image);
} catch (error) {
console.error('Image load error', error);
errorMessage = this.localizationTable.imageLoadError(error);
}
if (data) {
this.image = ImageWrapper.fromSrcAndPreview(
data, this.imagePreview, () => this.onImageDataUpdate(),
);
} else {
this.image = null;
}
this.onImageDataUpdate();
// Show the error after image update callbacks to ensure it is
// actually shown.
if (errorMessage) {
this.statusView.innerText = errorMessage;
}
});
altTextRow.replaceChildren(imageAltTextLabel, this.imageAltTextInput);
actionButtonRow.replaceChildren(this.submitButton);
container.replaceChildren(
chooseImageRow, altTextRow, this.imagePreview, this.statusView, actionButtonRow
);
dropdown.replaceChildren(container);
return true;
}
private onImageDataUpdate() {
const base64Data = this.image?.getBase64Url();
if (base64Data) {
this.submitButton.disabled = false;
this.submitButton.style.display = '';
this.imagePreview.style.display = '';
this.updateImageSizeDisplay();
} else {
this.submitButton.disabled = true;
this.submitButton.style.display = 'none';
this.statusView.innerText = '';
this.imagePreview.style.display = 'none';
this.submitButton.disabled = true;
}
}
private hideDialog() {
this.setDropdownVisible(false);
}
private updateImageSizeDisplay() {
const imageData = this.image?.getBase64Url() ?? '';
const sizeInKiB = imageData.length / 1024;
const sizeInMiB = sizeInKiB / 1024;
let units = 'KiB';
let size = sizeInKiB;
if (sizeInMiB >= 1) {
size = sizeInMiB;
units = 'MiB';
}
const sizeText = document.createElement('span');
sizeText.innerText = this.localizationTable.imageSize(
Math.round(size), units
);
// Add a button to allow decreasing the size of large images.
const decreaseSizeButton = document.createElement('button');
decreaseSizeButton.innerText = this.localizationTable.decreaseImageSize;
decreaseSizeButton.onclick = () => {
this.image?.decreaseSize();
};
const resetSizeButton = document.createElement('button');
resetSizeButton.innerText = this.localizationTable.resetImage;
resetSizeButton.onclick = () => {
this.image?.reset();
};
this.statusView.replaceChildren(sizeText);
const largeImageThreshold = 0.12; // MiB
if (sizeInMiB > largeImageThreshold) {
this.statusView.appendChild(decreaseSizeButton);
} else if (this.image?.isChanged()) {
this.statusView.appendChild(resetSizeButton);
}
}
private updateInputs() {
const resetInputs = () => {
this.selectedFiles?.set([]);
this.imageAltTextInput.value = '';
this.imagePreview.style.display = 'none';
this.submitButton.disabled = true;
this.statusView.innerText = '';
this.submitButton.style.display = '';
this.imageAltTextInput.oninput = null;
};
resetInputs();
const selectionTools = this.editor.toolController.getMatchingTools(SelectionTool);
const selectedObjects = selectionTools.map(tool => tool.getSelectedObjects()).flat();
// Check: Is there a selected image that can be edited?
let editingImage: ImageComponent|null = null;
if (selectedObjects.length === 1 && selectedObjects[0] instanceof ImageComponent) {
editingImage = selectedObjects[0];
this.imageAltTextInput.value = editingImage.getAltText() ?? '';
this.image = ImageWrapper.fromSrcAndPreview(
editingImage.getURL(),
this.imagePreview,
() => this.onImageDataUpdate(),
);
this.onImageDataUpdate();
} else if (selectedObjects.length > 0) {
// If not, clear the selection.
selectionTools.forEach(tool => tool.clearSelection());
}
// Show the submit button only when there is data to submit.
this.submitButton.style.display = 'none';
this.imageAltTextInput.oninput = () => {
if (this.imagePreview.src?.length > 0) {
this.submitButton.style.display = '';
}
};
this.submitButton.onclick = async () => {
if (!this.image) {
return;
}
const image = new Image();
image.src = this.image.getBase64Url();
image.setAttribute('alt', this.imageAltTextInput.value);
let component;
try {
component = await ImageComponent.fromImage(image, Mat33.identity);
} catch (error) {
console.error('Error loading image', error);
this.statusView.innerText = this.localizationTable.imageLoadError(error);
return;
}
if (component.getBBox().area === 0) {
this.statusView.innerText = this.localizationTable.errorImageHasZeroSize;
return;
}
this.hideDialog();
if (editingImage) {
const eraseCommand = new Erase([ editingImage ]);
// Try to preserve the original width
const originalTransform = editingImage.getTransformation();
// || 1: Prevent division by zero
const originalWidth = editingImage.getBBox().width || 1;
const newWidth = component.getBBox().transformedBoundingBox(originalTransform).width || 1;
const widthAdjustTransform = Mat33.scaling2D(originalWidth / newWidth);
await this.editor.dispatch(uniteCommands([
EditorImage.addElement(component),
component.transformBy(originalTransform.rightMul(widthAdjustTransform)),
component.setZIndex(editingImage.getZIndex()),
eraseCommand,
]));
selectionTools[0]?.setSelection([ component ]);
} else {
await this.editor.addAndCenterComponents([ component ]);
}
};
}
}