discourse/app/assets/javascripts/discourse-common/addon/lib/avatar-utils.js
David Taylor 2fde58def4
DEV: Move avatar-utils into dedicated discourse-common module (#22517)
These avatar-related helper functions are used in pretty-text, which currently means we load the entire `discourse/lib/utilities` module into the mini-racer when running pretty-text on the server side. This stops us adding any logic or imports to discourse/lib/utilities which may depend on other `discourse/` namespace features.

This commit moves the avatar-related utils into a dedicated module in the `discourse-common` namespace, adds backwards-compatibility shims, and updates the pretty-text config accordingly.
2023-07-12 09:06:16 +01:00

87 lines
2.2 KiB
JavaScript

import { getURLWithCDN } from "discourse-common/lib/get-url";
import { helperContext } from "discourse-common/lib/helpers";
import { escape } from "pretty-text/sanitizer";
import { deepMerge } from "discourse-common/lib/object";
let allowedSizes = null;
export function translateSize(size) {
switch (size) {
case "tiny":
return 24;
case "small":
return 24;
case "medium":
return 48;
case "large":
return 48;
case "extra_large":
return 96;
case "huge":
return 144;
}
return size;
}
export function getRawSize(size) {
const pixelRatio = window.devicePixelRatio || 1;
let rawSize = 1;
if (pixelRatio > 1.1 && pixelRatio < 2.1) {
rawSize = 2;
} else if (pixelRatio >= 2.1) {
rawSize = 3;
}
return size * rawSize;
}
export function getRawAvatarSize(size) {
allowedSizes ??= helperContext()
.siteSettings["avatar_sizes"].split("|")
.map((s) => parseInt(s, 10))
.sort((a, b) => a - b);
size = getRawSize(size);
for (let i = 0; i < allowedSizes.length; i++) {
if (allowedSizes[i] >= size) {
return allowedSizes[i];
}
}
return allowedSizes[allowedSizes.length - 1];
}
export function avatarUrl(template, size, { customGetURL } = {}) {
if (!template) {
return "";
}
const rawSize = getRawAvatarSize(translateSize(size));
const templatedPath = template.replace(/\{size\}/g, rawSize);
return (customGetURL || getURLWithCDN)(templatedPath);
}
export function avatarImg(options, customGetURL) {
const size = translateSize(options.size);
let url = avatarUrl(options.avatarTemplate, size, { customGetURL });
// We won't render an invalid url
if (!url) {
return "";
}
const classes =
"avatar" + (options.extraClasses ? " " + options.extraClasses : "");
let title = "";
if (options.title) {
const escaped = escape(options.title || "");
title = ` title='${escaped}' aria-label='${escaped}'`;
}
return `<img loading='lazy' alt='' width='${size}' height='${size}' src='${url}' class='${classes}'${title}>`;
}
export function tinyAvatar(avatarTemplate, options) {
return avatarImg(deepMerge({ avatarTemplate, size: "tiny" }, options));
}