UNPKG

@wordpress/editor

Version:
82 lines (81 loc) 2.48 kB
// packages/editor/src/components/collab-sidebar/utils.js import { _x } from "@wordpress/i18n"; function sanitizeCommentString(str) { return str.trim(); } function noop() { } var AVATAR_BORDER_COLORS = [ "#C36EFF", // Purple "#FF51A8", // Pink "#E4780A", // Orange "#FF35EE", // Magenta "#879F11", // Olive "#46A494", // Teal "#00A2C3" // Cyan ]; function getAvatarBorderColor(userId) { return AVATAR_BORDER_COLORS[userId % AVATAR_BORDER_COLORS.length]; } function getCommentExcerpt(text, excerptLength = 10) { if (!text) { return ""; } const wordCountType = _x("words", "Word count type. Do not translate!"); const rawText = text.trim(); let trimmedExcerpt = ""; if (wordCountType === "words") { trimmedExcerpt = rawText.split(" ", excerptLength).join(" "); } else if (wordCountType === "characters_excluding_spaces") { const textWithSpaces = rawText.split("", excerptLength).join(""); const numberOfSpaces = textWithSpaces.length - textWithSpaces.replaceAll(" ", "").length; trimmedExcerpt = rawText.split("", excerptLength + numberOfSpaces).join(""); } else if (wordCountType === "characters_including_spaces") { trimmedExcerpt = rawText.split("", excerptLength).join(""); } const isTrimmed = trimmedExcerpt !== rawText; return isTrimmed ? trimmedExcerpt + "\u2026" : trimmedExcerpt; } function focusCommentThread(commentId, container, additionalSelector) { if (!container) { return; } const threadSelector = commentId && commentId !== "new" ? `[role=treeitem][id="comment-thread-${commentId}"]` : "[role=treeitem]:not([id])"; const selector = additionalSelector ? `${threadSelector} ${additionalSelector}` : threadSelector; return new Promise((resolve) => { if (container.querySelector(selector)) { return resolve(container.querySelector(selector)); } let timer = null; const observer = new window.MutationObserver(() => { if (container.querySelector(selector)) { clearTimeout(timer); observer.disconnect(); resolve(container.querySelector(selector)); } }); observer.observe(container, { childList: true, subtree: true }); timer = setTimeout(() => { observer.disconnect(); resolve(null); }, 3e3); }).then((element) => element?.focus()); } export { focusCommentThread, getAvatarBorderColor, getCommentExcerpt, noop, sanitizeCommentString }; //# sourceMappingURL=utils.mjs.map