1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-28 17:02:04 +03:00

Images: Rolled out image memory handling to image actions

- Moved thumnbail loading out of repo into ImageResizer.
- Updated gallery and editor image handling to show errors where
  possible to indicate memory issues for resizing/thumbs.
- Updated gallery to load image data in a per-image basis via edit form
  for more resiliant thumb/data fetching. Data was previously provided
  via gallery listing, which could be affected by failing generation
  of other images.
- Updated image manager double click handling to be more pleasant and
  not flash away the edit form.
- Updated editor handlers to use main URL when thumbs fail to load.
This commit is contained in:
Dan Brown
2023-10-01 13:05:18 +01:00
parent 20bcbd76ef
commit b2d48d9a7f
15 changed files with 142 additions and 78 deletions

View File

@ -1,6 +1,4 @@
import {
onChildEvent, onSelect, removeLoading, showLoading,
} from '../services/dom';
import {onChildEvent, onSelect, removeLoading, showLoading,} from '../services/dom';
import {Component} from './component';
export class ImageManager extends Component {
@ -229,8 +227,8 @@ export class ImageManager extends Component {
this.loadGallery();
}
onImageSelectEvent(event) {
const image = JSON.parse(event.detail.data);
async onImageSelectEvent(event) {
let image = JSON.parse(event.detail.data);
const isDblClick = ((image && image.id === this.lastSelected.id)
&& Date.now() - this.lastSelectedTime < 400);
const alreadySelected = event.target.classList.contains('selected');
@ -238,12 +236,15 @@ export class ImageManager extends Component {
el.classList.remove('selected');
});
if (!alreadySelected) {
if (!alreadySelected && !isDblClick) {
event.target.classList.add('selected');
this.loadImageEditForm(image.id);
} else {
image = await this.loadImageEditForm(image.id);
} else if (!isDblClick) {
this.resetEditForm();
} else if (isDblClick) {
image = this.lastSelected;
}
this.selectButton.classList.toggle('hidden', alreadySelected);
if (isDblClick && this.callback) {
@ -265,6 +266,9 @@ export class ImageManager extends Component {
this.formContainer.innerHTML = formHtml;
this.formContainerPlaceholder.setAttribute('hidden', '');
window.$components.init(this.formContainer);
const imageDataEl = this.formContainer.querySelector('#image-manager-form-image-data');
return JSON.parse(imageDataEl.text);
}
runLoadMore() {

View File

@ -34,7 +34,7 @@ export class Actions {
const imageManager = window.$components.first('image-manager');
imageManager.show(image => {
const imageUrl = image.thumbs.display || image.url;
const imageUrl = image.thumbs?.display || image.url;
const selectedText = this.#getSelectionText();
const newText = `[![${selectedText || image.name}](${imageUrl})](${image.url})`;
this.#replaceSelection(newText, newText.length);
@ -417,7 +417,7 @@ export class Actions {
const newContent = `[![](${data.thumbs.display})](${data.url})`;
this.#findAndReplaceContent(placeHolderText, newContent);
} catch (err) {
window.$events.emit('error', this.editor.config.text.imageUploadError);
window.$events.error(err?.data?.message || this.editor.config.text.imageUploadError);
this.#findAndReplaceContent(placeHolderText, '');
console.error(err);
}

View File

@ -61,7 +61,7 @@ function paste(editor, options, event) {
editor.dom.replace(newEl, id);
}).catch(err => {
editor.dom.remove(id);
window.$events.emit('error', options.translations.imageUploadErrorText);
window.$events.error(err?.data?.message || options.translations.imageUploadErrorText);
console.error(err);
});
}, 10);

View File

@ -11,7 +11,7 @@ function register(editor) {
/** @type {ImageManager} * */
const imageManager = window.$components.first('image-manager');
imageManager.show(image => {
const imageUrl = image.thumbs.display || image.url;
const imageUrl = image.thumbs?.display || image.url;
let html = `<a href="${image.url}" target="_blank">`;
html += `<img src="${imageUrl}" alt="${image.name}">`;
html += '</a>';