Skip to content
Snippets Groups Projects
Commit f54a9249 authored by Maximilian Giller's avatar Maximilian Giller
Browse files

Improved error handling when not being able to load image

parent d31345be
No related branches found
No related tags found
No related merge requests found
Pipeline #50572 passed
...@@ -25,14 +25,14 @@ ...@@ -25,14 +25,14 @@
</br> </br>
<label for="node-image">Node Image</label> <label for="node-image">Node Image</label>
</br> </br>
<img id="node-image-preview" class="preview-image" src="" /> <img id="node-image-preview" style="color:red" class="preview-image" src="" />
</br> </br>
<input type="text" id="node-image" name="node-image" placeholder="Enter file name or URL" class="bottom-space" /> <input type="text" id="node-image" name="node-image" placeholder="Enter file name or URL" class="bottom-space" />
</br> </br>
<label for="node-detail-image">Info Image</label> <label for="node-detail-image">Info Image</label>
</br> </br>
<img id="node-detail-image-preview" class="preview-image" src="" /> <img id="node-detail-image-preview" style="color:red" class="preview-image" src="" />
</br> </br>
<input type="text" id="node-detail-image" name="node-detail-image" placeholder="Enter file name or URL" class="bottom-space" /> <input type="text" id="node-detail-image" name="node-detail-image" placeholder="Enter file name or URL" class="bottom-space" />
......
editor/images/onerror.png

31.1 KiB

import { PLUGIN_PATH } from "../../../../config";
import * as Graph from "../../graph"; import * as Graph from "../../graph";
import { CONTEXT } from "../../state"; import { CONTEXT } from "../../state";
import ToolMenu from "./toolmenu"; import ToolMenu from "./toolmenu";
...@@ -29,6 +30,7 @@ const NODE_MENU = [ ...@@ -29,6 +30,7 @@ const NODE_MENU = [
NODE_DETAIL_IMG_ID, NODE_DETAIL_IMG_ID,
]; ];
const IMAGE_MENU = [NODE_IMG_ID, NODE_DETAIL_IMG_ID];
const IMAGE_FIELDS = [ const IMAGE_FIELDS = [
{ {
uri: NODE_IMG_ID, uri: NODE_IMG_ID,
...@@ -45,6 +47,8 @@ const LINK_MENU = []; ...@@ -45,6 +47,8 @@ const LINK_MENU = [];
const MENU = [...NODE_MENU, ...LINK_MENU]; const MENU = [...NODE_MENU, ...LINK_MENU];
const ERROR_IMG_PATH = PLUGIN_PATH + "editor/images/onerror.png";
export class SelectMenu extends ToolMenu { export class SelectMenu extends ToolMenu {
constructor() { constructor() {
super(); super();
...@@ -63,33 +67,69 @@ export class SelectMenu extends ToolMenu { ...@@ -63,33 +67,69 @@ export class SelectMenu extends ToolMenu {
hookMenu() { hookMenu() {
MENU.forEach((menu) => { MENU.forEach((menu) => {
if (IMAGE_MENU.includes(menu)) {
return;
}
// Subscribes to change event for each menu element // Subscribes to change event for each menu element
this.find(menu).on("change", (e) => { this.find(menu).on("change", (e) => {
var newValue = e.target.value; this.updateValue(menu, e.target.value);
var propertyKey = this.toProperty(menu);
var formatedValue = this.formatValue(propertyKey, newValue);
// Modify stored selection
this.values[SELECTION_KEY][propertyKey] = formatedValue;
// Notify tool
this.tool.onMenuChange(
SELECTION_KEY,
this.values[SELECTION_KEY]
);
}); });
}); });
// Special hook for image, to update the shown image with every change // Special hooks for image, to update the shown image with every change
IMAGE_FIELDS.forEach((imageField) => { IMAGE_FIELDS.forEach((imageField) => {
// In case image can't be loaded, show message
this.find(imageField.preview).on("error", (e) => {
var img = this.find(e.target);
// Is source even set?
if (img.attr("src") === undefined || img.attr("src") === "") {
return;
}
// Show error message
this.setImagePreview(ERROR_IMG_PATH, imageField.preview);
// Maybe graph image should also be updated, but we might not want to overwrite previously saved images.
});
// Test images before updating them
this.find(imageField.uri).on("change", (e) => { this.find(imageField.uri).on("change", (e) => {
var uri = e.target.value; var imageSource = e.target.value;
this.setImagePreview(uri, imageField.preview);
// If source is empty, always apply it
if (imageSource === undefined || imageSource === "") {
this.updateValue(imageField.uri, imageSource);
this.setImagePreview(imageSource, imageField.preview);
return;
}
// Try loading the image and only apply it on success
var img = new Image();
img.addEventListener("load", () => {
this.updateValue(imageField.uri, imageSource);
this.setImagePreview(imageSource, imageField.preview);
});
img.addEventListener("error", () => {
this.setImagePreview(ERROR_IMG_PATH, imageField.preview);
});
img.src = this.getFullImageSource(imageSource);
}); });
}); });
} }
updateValue(menu, newValue) {
var propertyKey = this.toProperty(menu);
var formatedValue = this.formatValue(propertyKey, newValue);
// Modify stored selection
this.values[SELECTION_KEY][propertyKey] = formatedValue;
// Notify tool
this.tool.onMenuChange(SELECTION_KEY, this.values[SELECTION_KEY]);
}
updateImagePreviews() { updateImagePreviews() {
IMAGE_FIELDS.forEach((imageField) => { IMAGE_FIELDS.forEach((imageField) => {
var uri = this.find(imageField.uri).val(); var uri = this.find(imageField.uri).val();
...@@ -97,22 +137,24 @@ export class SelectMenu extends ToolMenu { ...@@ -97,22 +137,24 @@ export class SelectMenu extends ToolMenu {
}); });
} }
setImagePreview(uri, previewId) { getFullImageSource(uri) {
var previewImage = this.find(previewId);
if (uri === "") { if (uri === "") {
// Show default empty image // Show default empty image
previewImage.attr("src", ""); return "";
} else if (uri.includes("/")) { } else if (uri.includes("/")) {
// Is absolute URL // Is absolute URL
previewImage.attr("src", uri); return uri;
} else { } else {
// Is file name // Is file name
var url = Graph.IMAGE_SRC + uri; return Graph.IMAGE_SRC + uri;
previewImage.attr("src", url);
} }
} }
setImagePreview(uri, previewId) {
var previewImage = this.find(previewId);
previewImage.attr("src", this.getFullImageSource(uri));
}
formatValue(propertyKey, rawValue) { formatValue(propertyKey, rawValue) {
var formattedValue = rawValue; var formattedValue = rawValue;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment