2020-07-14 14:06:52 +02:00
|
|
|
import { converse } from "@converse/headless/converse-core";
|
2020-07-09 08:57:25 +02:00
|
|
|
import { directive, html } from "lit-html";
|
|
|
|
|
|
|
|
|
2020-07-14 15:45:16 +02:00
|
|
|
export const renderImage = directive((url, onLoad, onClick) => part => {
|
2020-07-09 08:57:25 +02:00
|
|
|
function onError () {
|
2020-08-31 20:29:51 +02:00
|
|
|
const u = converse.env.utils;
|
|
|
|
if (u.isURLWithImageExtension(url)) {
|
|
|
|
part.setValue(u.convertUrlToHyperlink(url));
|
|
|
|
part.commit();
|
|
|
|
} else {
|
|
|
|
// Before giving up and falling back to just rendering a hyperlink,
|
|
|
|
// we attach `.png` and try one more time.
|
|
|
|
// This works with some Imgur URLs
|
|
|
|
part.setValue(renderImage(`${url}.png`, onLoad, onClick));
|
|
|
|
part.commit();
|
|
|
|
}
|
2020-07-09 08:57:25 +02:00
|
|
|
}
|
|
|
|
part.setValue(
|
|
|
|
html`<a href="${url}"
|
|
|
|
class="chat-image__link"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener"
|
2020-07-14 15:45:16 +02:00
|
|
|
><img class="chat-image img-thumbnail" src="${url}" @click=${onClick} @error=${onError} @load=${onLoad}/></a>`
|
2020-07-09 08:57:25 +02:00
|
|
|
);
|
|
|
|
});
|