From f1b82567e8eeaddf3738caace4f9800719ddbbdd Mon Sep 17 00:00:00 2001 From: purocean Date: Sun, 24 Nov 2024 23:42:58 +0800 Subject: [PATCH] refactor: better code --- src/renderer/components/IndexStatus.vue | 1 - src/renderer/components/SvgIcon.vue | 1 + src/renderer/plugins/view-links.tsx | 226 --------------- .../plugins/view-links/ViewLinksComponent.vue | 273 ++++++++++++++++++ src/renderer/plugins/view-links/index.ts | 61 ++++ 5 files changed, 335 insertions(+), 227 deletions(-) delete mode 100644 src/renderer/plugins/view-links.tsx create mode 100644 src/renderer/plugins/view-links/ViewLinksComponent.vue create mode 100644 src/renderer/plugins/view-links/index.ts diff --git a/src/renderer/components/IndexStatus.vue b/src/renderer/components/IndexStatus.vue index c6358d9b..29eb4039 100644 --- a/src/renderer/components/IndexStatus.vue +++ b/src/renderer/components/IndexStatus.vue @@ -49,7 +49,6 @@ function handleCommand (e: MouseEvent) { diff --git a/src/renderer/plugins/view-links/index.ts b/src/renderer/plugins/view-links/index.ts new file mode 100644 index 00000000..ef38a2a0 --- /dev/null +++ b/src/renderer/plugins/view-links/index.ts @@ -0,0 +1,61 @@ +import { ROOT_REPO_NAME_PREFIX } from '@share/misc' +import type { Plugin } from '@fe/context' +import ViewLinksComponent from './ViewLinksComponent.vue' + +export default { + name: 'view-links', + register: (ctx) => { + const actionName = 'plugin.view-links.view-document-links' + + function showLinks () { + ctx.ui.useFixedFloat().show({ + right: '20px', + top: ctx.env.isElectron ? '66px' : '36px', + component: ViewLinksComponent, + closeOnBlur: false, + onBlur (byClickSelf) { + if (!byClickSelf && ctx.store.state.currentRepoIndexStatus?.repo !== ctx.store.state.currentFile?.repo) { + ctx.ui.useFixedFloat().hide() + } + }, + }) + } + + function when () { + const currentRepoName = ctx.store.state.currentRepo?.name + const currentFileRepoName = ctx.store.state.currentFile?.repo + + const result = !ctx.args.FLAG_DEMO && // not in demo mode + ctx.args.MODE === 'normal' && // in normal mode + currentRepoName && // has current repo + currentFileRepoName && // has current file + currentFileRepoName !== ctx.args.HELP_REPO_NAME && // current file is not in help repo + !currentFileRepoName.startsWith(ROOT_REPO_NAME_PREFIX) // current file is not in root repo + + return !!result + } + + ctx.action.registerAction({ + name: actionName, + forUser: true, + description: ctx.i18n.t('command-desc.plugin_view-links_view-document-links'), + handler: showLinks, + when, + }) + + ctx.workbench.FileTabs.tapActionBtns(btns => { + if (when()) { + btns.push({ type: 'separator' }) + btns.push({ + type: 'normal', + icon: 'link-solid', + title: 'Links', + onClick: () => { + ctx.action.getActionHandler(actionName)() + }, + }) + btns.push({ type: 'separator' }) + } + }) + } +} as Plugin