From 8916c478ee2d2ede1f8b3fbb9bec2f8fb4af432a Mon Sep 17 00:00:00 2001 From: purocean Date: Wed, 11 Aug 2021 12:50:23 +0800 Subject: [PATCH] =?UTF-8?q?upd:=20=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=8F=98=E5=8A=A8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/components/FileTabs.vue | 18 +++++++++--------- src/renderer/context/document.ts | 12 ++++++++++-- src/renderer/context/index.ts | 6 +++++- src/renderer/plugins/document-history-stack.ts | 13 ++++++------- src/renderer/plugins/markdown-luckysheet.ts | 2 +- src/renderer/utils/index.ts | 2 ++ 6 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/renderer/components/FileTabs.vue b/src/renderer/components/FileTabs.vue index e0d5cf0a..dc38f871 100644 --- a/src/renderer/components/FileTabs.vue +++ b/src/renderer/components/FileTabs.vue @@ -8,7 +8,7 @@ import { computed, defineComponent, onBeforeMount, onBeforeUnmount, ref, toRefs, import { Alt, Ctrl } from '@fe/context/shortcut' import { Components, Doc } from '@fe/support/types' import { useBus } from '@fe/support/bus' -import { ensureCurrentFileSaved, isEncrypted, switchDoc, toUri } from '@fe/context/document' +import { ensureCurrentFileSaved, isEncrypted, isSubOrSameFile, switchDoc, toUri } from '@fe/context/document' import { registerAction, removeAction } from '@fe/context/action' import { isBelongTo } from '@fe/utils/path' import Tabs from './Tabs.vue' @@ -32,7 +32,7 @@ export default defineComponent({ } function switchFile (file: any) { - switchDoc(file) + return switchDoc(file) } function switchTab (item: Components.FileTabs.Item) { @@ -81,12 +81,10 @@ export default defineComponent({ } function removeFile (doc?: Doc | null) { - const tab = tabs.value.find((x: Components.FileTabs.Item) => { - return x.key === toUri(doc) || (x.payload.file && doc && isBelongTo(doc.path, x.payload.file.path)) - }) + const files = tabs.value.filter((x: Components.FileTabs.Item) => isSubOrSameFile(doc, x.payload.file)) - if (tab) { - removeTabs([tab]) + if (files.length > 0) { + removeTabs(files) } } @@ -96,10 +94,12 @@ export default defineComponent({ } } - function handleMoved (payload?: { oldDoc: Doc, newDoc: Doc }) { + async function handleMoved (payload?: { oldDoc: Doc, newDoc: Doc }) { if (payload) { + if (payload.newDoc.type === 'file') { + await switchFile(payload.newDoc) + } removeFile(payload.oldDoc) - switchFile(payload.newDoc) } } diff --git a/src/renderer/context/document.ts b/src/renderer/context/document.ts index 40b4fac2..b797eafb 100644 --- a/src/renderer/context/document.ts +++ b/src/renderer/context/document.ts @@ -4,7 +4,7 @@ import { useModal } from '@fe/support/modal' import { useToast } from '@fe/support/toast' import store from '@fe/support/store' import type { Doc } from '@fe/support/types' -import { basename, dirname, join } from '@fe/utils/path' +import { basename, dirname, isBelongTo, join } from '@fe/utils/path' import { useBus } from '@fe/support/bus' import * as api from '@fe/support/api' import { getLogger } from '@fe/utils' @@ -61,6 +61,14 @@ export function isSameFile (a?: Doc | null, b?: Doc | null) { return a && b && a.repo === b.repo && a.path === b.path } +export function isSubOrSameFile (a?: Doc | null, b?: Doc | null) { + return a && b && a.repo === b.repo && + ( + isBelongTo(a.path, b.path) || + isSameFile(a, b) + ) +} + export function toUri (doc?: Doc | null) { if (doc && doc.repo && doc.path) { return encodeURI(`yank-note://${doc.repo}/${doc.path.replace(/^\//, '')}`) @@ -92,7 +100,7 @@ export async function createDoc (doc: Optional { - const stack: Doc[] = [] + let stack: Doc[] = [] let idx = -1 const backId = 'plugin.document-history-stack.back' @@ -22,10 +22,10 @@ export default { } idx = index + updateMenu() } function updateMenu () { - console.log(idx, idx < stack.length - 1, idx > 0) ctx.statusBar.tapMenus(menus => { const list = menus['status-bar-navigation']?.list || [] if (list) { @@ -36,7 +36,7 @@ export default { title: '前进', disabled: idx >= stack.length - 1, subTitle: ctx.shortcut.getKeysLabel(forwardId), - onClick: ctx.action.getActionHandler(forwardId) + onClick: () => ctx.action.getActionHandler(forwardId)() }, { id: backId, @@ -44,7 +44,7 @@ export default { title: '后退', disabled: idx <= 0, subTitle: ctx.shortcut.getKeysLabel(backId), - onClick: ctx.action.getActionHandler(backId) + onClick: () => ctx.action.getActionHandler(backId)() }, ].concat(list.filter(x => ![forwardId, backId].includes(x.id)) as any) } @@ -52,9 +52,8 @@ export default { } function removeFromStack (doc?: Doc) { - const newStack = stack.filter(x => ctx.doc.toUri(doc) !== ctx.doc.toUri(x)) - stack.splice(0, stack.length) - stack.push(...newStack) + stack = stack.filter(x => !ctx.doc.isSubOrSameFile(doc, x)) + idx = stack.length - 1 updateMenu() } diff --git a/src/renderer/plugins/markdown-luckysheet.ts b/src/renderer/plugins/markdown-luckysheet.ts index 0baf647d..7adc7f1f 100644 --- a/src/renderer/plugins/markdown-luckysheet.ts +++ b/src/renderer/plugins/markdown-luckysheet.ts @@ -361,7 +361,7 @@ async function createLuckysheet (node: Doc) { filename = filename.replace(/\/$/, '') + fileExt } - const path = join(currentPath, filename).replace(/^\//, '') + const path = join(currentPath, filename) if (!path) { throw new Error('需要传入文件路径') diff --git a/src/renderer/utils/index.ts b/src/renderer/utils/index.ts index debaa2f9..cb1b60e8 100644 --- a/src/renderer/utils/index.ts +++ b/src/renderer/utils/index.ts @@ -1,3 +1,5 @@ +export * as path from './path' + export const encodeMarkdownLink = (path: string) => { return path .replace(/\(/g, '%28')