@atlaskit/editor-plugin-mentions
Version:
Mentions plugin for @atlaskit/editor-core
71 lines • 2.22 kB
JavaScript
import { bind } from 'bind-event-listener';
const avatarImageClassName = 'editor-mention-avatar-image';
const agentAvatarClassName = 'editor-mention-avatar-agent';
const fallbackAvatarClassName = 'editor-mention-avatar-fallback';
const avatarSize = 16;
/**
* Populates the fixed-size avatar slot owned by the vanilla ProseMirror NodeView.
* No React root or editor portal is created for individual mention nodes.
*/
export const mentionAvatarRenderer = ({
container
}) => {
let image;
let fallback;
let unbindImageError;
const removeImage = () => {
var _unbindImageError, _image;
(_unbindImageError = unbindImageError) === null || _unbindImageError === void 0 ? void 0 : _unbindImageError();
unbindImageError = undefined;
(_image = image) === null || _image === void 0 ? void 0 : _image.remove();
image = undefined;
};
const removeFallback = () => {
var _fallback;
(_fallback = fallback) === null || _fallback === void 0 ? void 0 : _fallback.remove();
fallback = undefined;
container.classList.remove(fallbackAvatarClassName);
};
const showFallback = () => {
removeImage();
removeFallback();
fallback = container.ownerDocument.createTextNode('@');
container.appendChild(fallback);
container.classList.add(fallbackAvatarClassName);
};
const destroy = () => {
removeImage();
removeFallback();
};
return {
destroy,
render: ({
appType,
avatarUrl,
isAvatarImagePreShaped
}) => {
container.classList.toggle(agentAvatarClassName, !isAvatarImagePreShaped && appType === 'agent');
removeFallback();
if (!avatarUrl) {
removeImage();
return;
}
if (!image) {
image = container.ownerDocument.createElement('img');
image.alt = '';
image.className = avatarImageClassName;
image.decoding = 'async';
image.draggable = false;
image.height = avatarSize;
image.loading = 'lazy';
image.width = avatarSize;
unbindImageError = bind(image, {
type: 'error',
listener: showFallback
});
container.appendChild(image);
}
image.src = avatarUrl;
}
};
};