UNPKG

@dialpad/dialtone

Version:

Dialpad's Dialtone design system monorepo

103 lines (102 loc) 3.5 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); const common_emoji = require("../../common/emoji.cjs"); const vue = require("vue"); const icon_constants = require("../icon/icon_constants.cjs"); const emoji = require("../emoji/emoji.vue.cjs"); const COMMENT_TYPE = vue.h(vue.resolveDynamicComponent(null)).type; const _sfc_main = { compatConfig: { MODE: 3 }, name: "DtEmojiTextWrapper", components: { DtEmoji: emoji.default }, props: { /** * Element type (tag name) to use for the wrapper. */ elementType: { type: String, default: "div" }, /** * The icon size to render the emojis at: 100 to 800 */ size: { type: String, default: "500", validator: (t) => Object.keys(icon_constants.ICON_SIZE_MODIFIERS).includes(t) } }, data() { return { loadingEmojiJson: true }; }, async created() { this.loadingEmojiJson = false; }, methods: { /** * Replaces the valid codes from the text content with a DtEmoji component. * @returns {Array<VNode|string>} */ replaceDtEmojis(replaceList, textContent) { if (!replaceList.length) return textContent; const escapedReplaceList = replaceList.map( (item) => item.replace(/\*/g, "\\*") ); const regexp = new RegExp(`(${escapedReplaceList.join("|")})`, "g"); const items = textContent.split(regexp); return items.filter((item) => item.trim() !== "").map((item) => { if (replaceList.includes(item)) { return vue.h(emoji.default, { code: item, size: this.size }); } return vue.h("span", { class: "d-emoji-text-wrapper__text" }, item); }); }, /** * Recursively search the Vue virtual DOM to find text * @param VNode * @returns {VNode|*} */ searchVNodes(VNode) { var _a; if (typeof VNode === "string") return this.searchCodes(VNode); if (VNode.type === COMMENT_TYPE) return VNode; if (typeof VNode.type === "symbol") return this.searchCodes(VNode.children); if ((_a = VNode.props) == null ? void 0 : _a.innerHTML) return this.searchVNodes(VNode.props.innerHTML); const children = Array.isArray(VNode.children) ? VNode.children : [VNode.children]; return vue.h(VNode.type, VNode.props, children.map((VNodeChild) => this.searchVNodes(VNodeChild))); }, // TODO: Find a way to crawl vue components replaceVueComponentVNodeContent(VNode) { }, /** * Find codes in text. * @param textContent string * @returns {Array<VNode|string>|string} */ searchCodes(textContent) { const shortcodes = common_emoji.findShortCodes(textContent); const emojis = common_emoji.findEmojis(textContent); const replaceList = [...shortcodes, ...emojis]; if (replaceList.length === 0) return textContent; return this.replaceDtEmojis(replaceList, textContent); } }, render() { const defaultSlotContent = this.$slots.default ? this.$slots.default() : []; return vue.h( this.elementType, { "data-qa": "emoji-text-wrapper", class: "d-emoji-text-wrapper" }, this.loadingEmojiJson ? defaultSlotContent : defaultSlotContent.map((VNode) => this.searchVNodes(VNode)) ); } }; const DtEmojiTextWrapper = _sfc_main; exports.default = DtEmojiTextWrapper; //# sourceMappingURL=emoji_text_wrapper.vue.cjs.map