Skip to content

Commit

Permalink
Merge pull request #14 from laquasicinque/fix/unhandled-missing-token
Browse files Browse the repository at this point in the history
fix: fallback to mystery man avatar if original image missing
  • Loading branch information
MrPrimate authored Feb 27, 2021
2 parents e25b60d + 91f02ac commit fa40047
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fa40047

Please sign in to comment.