diff --git a/src/tokenizer/index.js b/src/tokenizer/index.js index fdc8b02..ee32d43 100644 --- a/src/tokenizer/index.js +++ b/src/tokenizer/index.js @@ -101,21 +101,38 @@ export default class Tokenizer extends FormApplication { /* -------------------------------------------- */ - activateListeners(html) { - let avatarView = document.querySelector(".avatar > .view"); - this.Avatar = null; - - Utils.download(this.actor.data.img) - .then(img => { - const MAX_DIMENSION = Math.max(img.naturalHeight, img.naturalWidth); - console.log("Setting Avatar dimensions to " + MAX_DIMENSION + "/" + MAX_DIMENSION); - this.Avatar = new View(MAX_DIMENSION, avatarView); - this.Avatar.addImageLayer(img); + async _initAvatar (html, inputUrl) { + const url = inputUrl ?? 'icons/svg/mystery-man.svg' + const avatarView = document.querySelector(".avatar > .view"); + if(this.Avatar) { + this.Avatar.canvas.remove() + this.Avatar.stage.remove() + this.Avatar.controlsArea.remove() + this.Avatar.menu.remove() + } + this.Avatar = null + try { + const img = await Utils.download(url) + const MAX_DIMENSION = Math.max(img.naturalHeight, img.naturalWidth); + console.log("Setting Avatar dimensions to " + MAX_DIMENSION + "/" + MAX_DIMENSION); + this.Avatar = new View(MAX_DIMENSION, avatarView); + this.Avatar.addImageLayer(img); + + // Setting the height of the form to the desired auto height + $(html).parent().parent().css("height", "auto"); + } catch (error) { + if(inputUrl) { + console.error(error) + ui.notifications.error(`Failed to load original image "${url}". File has possibly been deleted. Falling back to mystery-man.`) + this._initAvatar(html) + } else { + ui.notifications.error('Failed to load fallback image.') + } + } + } - // Setting the height of the form to the desired auto height - $(html).parent().parent().css("height", "auto"); - }) - .catch(error => ui.notifications.error(error)); + activateListeners(html) { + this._initAvatar(html, this.actor.img) let tokenView = document.querySelector(".token > .view"); @@ -180,12 +197,14 @@ export default class Tokenizer extends FormApplication { event.preventDefault(); let eventTarget = event.target == event.currentTarget ? event.target : event.currentTarget; - let view = eventTarget.dataset.target === "avatar" ? this.Avatar : this.Token; + let view = (eventTarget.dataset.target === "avatar" ? this.Avatar : this.Token) ?? this.Token ?? this.Avatar; + console.log(this.Avatar, this.Token) let type = eventTarget.dataset.type; switch (eventTarget.dataset.type) { case "upload": - Utils.upload().then(img => view.addImageLayer(img)); + const img = await Utils.upload() + view.addImageLayer(img); break; case "download": // show dialog, then download