diff --git a/frontend/src/components/TreeNode.vue b/frontend/src/components/TreeNode.vue index 4de2bd0d..15436bf0 100644 --- a/frontend/src/components/TreeNode.vue +++ b/frontend/src/components/TreeNode.vue @@ -42,6 +42,7 @@ import Extensions from '../useful/extensions' import { triggerHook } from '../useful/plugin' import { Components } from '../types' import SvgIcon from './SvgIcon.vue' +import { registerContextMenu, getContextMenuItems } from '../useful/plugin/tree' export default defineComponent({ name: 'tree-node', @@ -231,6 +232,10 @@ export default defineComponent({ } function showContextMenu (item: any) { + contextMenu.show(getContextMenuItems(item)) + } + + function buildContextMenu (item: any) { const menu: Components.ContextMenu.Item[] = [ { id: 'delete', label: '删除', onClick: () => deleteFile() }, { id: 'rename', label: '重命名 / 移动', onClick: () => renameFile() }, @@ -240,11 +245,11 @@ export default defineComponent({ ] if (item.type === 'dir') { - contextMenu.show([ + return [ { id: 'create', label: '创建新文件', onClick: () => createFile() }, ...menu, { id: 'openInTerminal', label: '在终端中打开', onClick: () => revealInXterminal() } - ]) + ] } else { // markdown 文件可以被标记 if (props.item.path.endsWith('.md')) { @@ -257,17 +262,19 @@ export default defineComponent({ additional.push({ id: 'duplicate', label: '重复文件', onClick: () => duplicateFile() }) } - contextMenu.show([ + return [ ...additional, ...menu, { id: 'create', label: '当前目录创建新文件', onClick: () => createFile() } - ]) + ] } else { - contextMenu.show(menu) + return menu } } } + registerContextMenu((_, item) => buildContextMenu(item)) + const currentRepoName = computed(() => currentRepo.value?.name ?? '/') const selected = computed(() => { diff --git a/frontend/src/useful/plugin/index.ts b/frontend/src/useful/plugin/index.ts index 6a3df118..14e986c3 100644 --- a/frontend/src/useful/plugin/index.ts +++ b/frontend/src/useful/plugin/index.ts @@ -50,6 +50,7 @@ const ctx = { return false }, + tree: tree.ctx, markdown: markdown.ctx, statusBar: statusBar.ctx } diff --git a/frontend/src/useful/plugin/tree.ts b/frontend/src/useful/plugin/tree.ts index 35ffbc5b..a5af8f4e 100644 --- a/frontend/src/useful/plugin/tree.ts +++ b/frontend/src/useful/plugin/tree.ts @@ -1 +1,21 @@ +import { Components } from '@/types' + export type HookType = 'ON_TREE_NODE_SELECT' + +type MenuItem = Components.ContextMenu.Item + +type BuildContextMenu = (items: MenuItem[], node: any) => MenuItem[] + +const contextMenuFunList: BuildContextMenu[] = [] + +export const registerContextMenu = (fun: BuildContextMenu) => { + contextMenuFunList.push(fun) +} + +export const getContextMenuItems = (node: any) => { + return contextMenuFunList.reduce((items, fun) => fun(items, node), [] as MenuItem[]) +} + +export const ctx = { + registerContextMenu +}