upd: 优化文件变动处理

This commit is contained in:
purocean
2021-08-11 14:13:02 +08:00
parent 16942f2bce
commit 8916c478ee
6 changed files with 33 additions and 20 deletions
+9 -9
View File
@@ -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)
}
}
+10 -2
View File
@@ -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<Pick<Doc, 'repo' | 'path' | 'cont
filename = filename.replace(/\/$/, '') + '.md'
}
doc.path = join(currentPath, filename).replace(/^\//, '')
doc.path = join(currentPath, filename)
}
}
+5 -1
View File
@@ -3,6 +3,8 @@ import { useToast } from '@fe/support/toast'
import { useModal } from '@fe/support/modal'
import store from '@fe/support/store'
import storage from '@fe/utils/storage'
import * as utils from '@fe/utils/index'
import * as env from '@fe/utils/env'
import * as api from '@fe/support/api'
import * as action from './action'
import * as doc from './document'
@@ -36,7 +38,9 @@ const ctx = {
view,
theme,
storage,
embed
embed,
env,
utils,
}
window.ctx = ctx
@@ -4,7 +4,7 @@ import type { Doc } from '@fe/support/types'
export default {
name: 'document-history-stack',
register: (ctx: Ctx) => {
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()
}
+1 -1
View File
@@ -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('需要传入文件路径')
+2
View File
@@ -1,3 +1,5 @@
export * as path from './path'
export const encodeMarkdownLink = (path: string) => {
return path
.replace(/\(/g, '%28')