1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| import { commandsCtx, Ctx, schemaCtx, themeManagerCtx } from "@milkdown/core"; import { createDropdownItem, slash, slashPlugin, WrappedAction, } from "@milkdown/plugin-slash";
const diyActions = (ctx: Ctx, input = "/"): WrappedAction[] => { const { nodes } = ctx.get(schemaCtx); const actions: Array< WrappedAction & { keyword: string[]; typeName: string } > = [ { id: "h1", dom: createDropdownItem(ctx.get(themeManagerCtx), "标签一", "h1"), command: () => ctx.get(commandsCtx).call("TurnIntoHeading", 1), keyword: ["h1", "large heading"], typeName: "heading", }, { id: "h2", dom: createDropdownItem(ctx.get(themeManagerCtx), "标签二", "h2"), command: () => ctx.get(commandsCtx).call("TurnIntoHeading", 2), keyword: ["h2", "medium heading"], typeName: "heading", }, { id: "h3", dom: createDropdownItem(ctx.get(themeManagerCtx), "标签三", "h3"), command: () => ctx.get(commandsCtx).call("TurnIntoHeading", 3), keyword: ["h3", "small heading"], typeName: "heading", }, { id: "bulletList", dom: createDropdownItem( ctx.get(themeManagerCtx), "无序列表", "bulletList" ), command: () => ctx.get(commandsCtx).call("WrapInBulletList"), keyword: ["bullet list", "ul"], typeName: "bullet_list", }, { id: "orderedList", dom: createDropdownItem( ctx.get(themeManagerCtx), "有序列表", "orderedList" ), command: () => ctx.get(commandsCtx).call("WrapInOrderedList"), keyword: ["ordered list", "ol"], typeName: "ordered_list", }, { id: "taskList", dom: createDropdownItem(ctx.get(themeManagerCtx), "任务列表", "taskList"), command: () => ctx.get(commandsCtx).call("TurnIntoTaskList"), keyword: ["task list", "task"], typeName: "task_list_item", }, { id: "image", dom: createDropdownItem(ctx.get(themeManagerCtx), "图片", "image"), command: () => ctx.get(commandsCtx).call("InsertImage"), keyword: ["image"], typeName: "image", }, { id: "blockquote", dom: createDropdownItem(ctx.get(themeManagerCtx), "引用", "quote"), command: () => ctx.get(commandsCtx).call("WrapInBlockquote"), keyword: ["quote", "blockquote"], typeName: "blockquote", }, { id: "table", dom: createDropdownItem(ctx.get(themeManagerCtx), "表格", "table"), command: () => ctx.get(commandsCtx).call("InsertTable"), keyword: ["table"], typeName: "table", }, { id: "code", dom: createDropdownItem(ctx.get(themeManagerCtx), "代码块", "code"), command: () => ctx.get(commandsCtx).call("TurnIntoCodeFence"), keyword: ["code"], typeName: "fence", }, { id: "divider", dom: createDropdownItem(ctx.get(themeManagerCtx), "分隔符", "divider"), command: () => ctx.get(commandsCtx).call("InsertHr"), keyword: ["divider", "hr"], typeName: "hr", }, { id: "h4", dom: createDropdownItem(ctx.get(themeManagerCtx), "测试", "h3"), command: () => { ctx.get(commandsCtx).call("TaskList"); }, keyword: ["h4", "Test"], typeName: "heading", }, ];
const userInput = input.slice(1).toLocaleLowerCase(); return actions .filter( (action) => !!nodes[action.typeName] && action.keyword.some((keyword) => keyword.includes(userInput)) ) .map(({ keyword, typeName, ...action }) => action); };
export const diySlash = slash.configure(slashPlugin, { config: (ctx) => { const actions: any = diyActions(ctx); return ({ isTopLevel, content, parentNode }) => { if (!isTopLevel) return null; if (!content) { return { placeholder: "输入/来使用斜杠命令..." }; } if (content.startsWith("/")) { return content === "/" ? { placeholder: "请选择类型...", actions: diyActions(ctx), } : { actions: diyActions(ctx, content), }; } }; }, });
|