@dialpad/dialtone-vue
Version:
Vue component library for Dialpad's design system Dialtone
89 lines (88 loc) • 2.53 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Mention = require("@tiptap/extension-mention");
const vue2 = require("@tiptap/vue-2");
const state = require("@tiptap/pm/state");
const SlashCommandComponent = require("./SlashCommandComponent.vue.cjs");
const core = require("@tiptap/core");
const slashCommandPasteMatch = (text, slashCommandRegex) => {
const matches = [...text.matchAll(slashCommandRegex)];
return matches.map((match) => {
let slashCommand = match[2];
if (!slashCommand.endsWith(" ")) slashCommand += " ";
return {
index: match.index,
text: slashCommand,
match
};
});
};
const SlashCommandPlugin = Mention.extend({
name: "slash-commands",
group: "inline",
inline: true,
addNodeView() {
return vue2.VueNodeViewRenderer(SlashCommandComponent.default);
},
parseHTML() {
return [
{
tag: "command-component"
}
];
},
addAttributes() {
return {
command: {
default: ""
},
parametersExample: {
default: ""
},
description: {
default: ""
}
};
},
renderText({ node }) {
return `/${node.attrs.command}`;
},
renderHTML({ HTMLAttributes }) {
return ["command-component", core.mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)];
},
addInputRules() {
var _a;
const suggestions = (_a = this.options.suggestion) == null ? void 0 : _a.items({ query: "" }).map((suggestion) => suggestion.command);
const slashCommandRegex = new RegExp(`^((?:\\/)(${suggestions.join("|")})) $`);
return [
core.nodeInputRule({
find: slashCommandRegex,
type: this.type,
getAttributes(attrs) {
return { command: attrs[2] };
}
})
];
},
addPasteRules() {
var _a;
const suggestions = (_a = this.options.suggestion) == null ? void 0 : _a.items({ query: "" }).map((suggestion) => suggestion.command);
const slashCommandRegex = new RegExp(`^((?:\\/)(${suggestions.join("|")})) ?$`, "g");
return [
core.nodePasteRule({
find: (text) => slashCommandPasteMatch(text, slashCommandRegex),
type: this.type,
getAttributes(attrs) {
return { command: attrs[0].trim() };
}
})
];
}
}).configure({
suggestion: {
char: "/",
pluginKey: new state.PluginKey("slashCommandSuggestion")
}
});
exports.SlashCommandPlugin = SlashCommandPlugin;
//# sourceMappingURL=slash_command.cjs.map