插件增加文件树菜单注册

This commit is contained in:
purocean
2021-04-27 17:39:31 +08:00
parent 0263059ebc
commit 82f5a10110
3 changed files with 33 additions and 5 deletions
+12 -5
View File
@@ -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(() => {
+1
View File
@@ -50,6 +50,7 @@ const ctx = {
return false
},
tree: tree.ctx,
markdown: markdown.ctx,
statusBar: statusBar.ctx
}
+20
View File
@@ -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
}