@atlaskit/editor-plugin-mentions
Version:
Mentions plugin for @atlaskit/editor-core
69 lines • 2.39 kB
JavaScript
import { bind } from 'bind-event-listener';
var avatarImageClassName = 'editor-mention-avatar-image';
var agentAvatarClassName = 'editor-mention-avatar-agent';
var fallbackAvatarClassName = 'editor-mention-avatar-fallback';
var 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 var mentionAvatarRenderer = function mentionAvatarRenderer(_ref) {
var container = _ref.container;
var image;
var fallback;
var unbindImageError;
var removeImage = function removeImage() {
var _unbindImageError, _image;
(_unbindImageError = unbindImageError) === null || _unbindImageError === void 0 || _unbindImageError();
unbindImageError = undefined;
(_image = image) === null || _image === void 0 || _image.remove();
image = undefined;
};
var removeFallback = function removeFallback() {
var _fallback;
(_fallback = fallback) === null || _fallback === void 0 || _fallback.remove();
fallback = undefined;
container.classList.remove(fallbackAvatarClassName);
};
var showFallback = function showFallback() {
removeImage();
removeFallback();
fallback = container.ownerDocument.createTextNode('@');
container.appendChild(fallback);
container.classList.add(fallbackAvatarClassName);
};
var destroy = function destroy() {
removeImage();
removeFallback();
};
return {
destroy: destroy,
render: function render(_ref2) {
var appType = _ref2.appType,
avatarUrl = _ref2.avatarUrl,
isAvatarImagePreShaped = _ref2.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;
}
};
};