UNPKG

@dialpad/dialtone-vue

Version:

Vue component library for Dialpad's design system Dialtone

1 lines 4.38 kB
{"version":3,"file":"slash_command.cjs","sources":["../../../../../components/rich_text_editor/extensions/slash_command/slash_command.js"],"sourcesContent":["import Mention from '@tiptap/extension-mention';\nimport { VueNodeViewRenderer } from '@tiptap/vue-2';\nimport { PluginKey } from '@tiptap/pm/state';\n\n// Slash Command Mention component\nimport SlashCommandComponent from './SlashCommandComponent.vue';\nimport { mergeAttributes, nodeInputRule, nodePasteRule } from '@tiptap/core';\n\nconst slashCommandPasteMatch = (text, slashCommandRegex) => {\n const matches = [...text.matchAll(slashCommandRegex)];\n\n return matches\n .map(match => {\n let slashCommand = match[2];\n if (!slashCommand.endsWith(' ')) slashCommand += ' ';\n return {\n index: match.index,\n text: slashCommand,\n match,\n };\n });\n};\n\nexport const SlashCommandPlugin = Mention.extend({\n name: 'slash-commands',\n group: 'inline',\n inline: true,\n\n addNodeView () {\n return VueNodeViewRenderer(SlashCommandComponent);\n },\n\n parseHTML () {\n return [\n {\n tag: 'command-component',\n },\n ];\n },\n\n addAttributes () {\n return {\n command: {\n default: '',\n },\n parametersExample: {\n default: '',\n },\n description: {\n default: '',\n },\n };\n },\n\n renderText ({ node }) {\n return `/${node.attrs.command}`;\n },\n\n renderHTML ({ HTMLAttributes }) {\n return ['command-component', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)];\n },\n\n addInputRules () {\n const suggestions = this.options.suggestion?.items({ query: '' }).map(suggestion => suggestion.command);\n const slashCommandRegex = new RegExp(`^((?:\\\\/)(${suggestions.join('|')})) $`);\n return [\n nodeInputRule({\n find: slashCommandRegex,\n type: this.type,\n getAttributes (attrs) {\n return { command: attrs[2] };\n },\n }),\n ];\n },\n\n addPasteRules () {\n const suggestions = this.options.suggestion?.items({ query: '' }).map(suggestion => suggestion.command);\n const slashCommandRegex = new RegExp(`^((?:\\\\/)(${suggestions.join('|')})) ?$`, 'g');\n return [\n nodePasteRule({\n find: (text) => slashCommandPasteMatch(text, slashCommandRegex),\n type: this.type,\n getAttributes (attrs) {\n return { command: attrs[0].trim() };\n },\n }),\n ];\n },\n}).configure({\n suggestion: {\n char: '/',\n pluginKey: new PluginKey('slashCommandSuggestion'),\n },\n});\n"],"names":["VueNodeViewRenderer","SlashCommandComponent","mergeAttributes","nodeInputRule","nodePasteRule","PluginKey"],"mappings":";;;;;;;AAQA,MAAM,yBAAyB,CAAC,MAAM,sBAAsB;AAC1D,QAAM,UAAU,CAAC,GAAG,KAAK,SAAS,iBAAiB,CAAC;AAEpD,SAAO,QACJ,IAAI,WAAS;AACZ,QAAI,eAAe,MAAM,CAAC;AAC1B,QAAI,CAAC,aAAa,SAAS,GAAG,EAAG,iBAAgB;AACjD,WAAO;AAAA,MACL,OAAO,MAAM;AAAA,MACb,MAAM;AAAA,MACN;AAAA,IACR;AAAA,EACA,CAAK;AACL;AAEY,MAAC,qBAAqB,QAAQ,OAAO;AAAA,EAC/C,MAAM;AAAA,EACN,OAAO;AAAA,EACP,QAAQ;AAAA,EAER,cAAe;AACb,WAAOA,KAAAA,oBAAoBC,sBAAAA,OAAqB;AAAA,EACjD;AAAA,EAED,YAAa;AACX,WAAO;AAAA,MACL;AAAA,QACE,KAAK;AAAA,MACN;AAAA,IACP;AAAA,EACG;AAAA,EAED,gBAAiB;AACf,WAAO;AAAA,MACL,SAAS;AAAA,QACP,SAAS;AAAA,MACV;AAAA,MACD,mBAAmB;AAAA,QACjB,SAAS;AAAA,MACV;AAAA,MACD,aAAa;AAAA,QACX,SAAS;AAAA,MACV;AAAA,IACP;AAAA,EACG;AAAA,EAED,WAAY,EAAE,QAAQ;AACpB,WAAO,IAAI,KAAK,MAAM,OAAO;AAAA,EAC9B;AAAA,EAED,WAAY,EAAE,kBAAkB;AAC9B,WAAO,CAAC,qBAAqBC,qBAAgB,KAAK,QAAQ,gBAAgB,cAAc,CAAC;AAAA,EAC1F;AAAA,EAED,gBAAiB;;AACf,UAAM,eAAc,UAAK,QAAQ,eAAb,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