refactor: update document switching logic and position handling

This commit is contained in:
purocean
2024-07-04 10:07:32 +08:00
parent c29ed44389
commit 6d2ecacf17
2 changed files with 12 additions and 9 deletions
+12
View File
@@ -344,6 +344,18 @@ export default {
})
})
ctx.registerHook('DOC_SWITCH_SKIPPED', ({ opts }) => {
if (opts?.position) {
ctx.routines.changePosition(opts.position)
}
})
ctx.registerHook('DOC_SWITCHED', ({ doc, opts }) => {
if (doc && opts?.position) {
ctx.routines.changePosition(opts.position)
}
})
ctx.markdown.registerPlugin(md => {
md.core.ruler.push('convert_relative_path', convertLink)
md.renderer.rules.link_open = (tokens, idx, options, _, slf) => {
-9
View File
@@ -20,7 +20,6 @@ import { isWindows } from '@fe/support/env'
import { getAllRepos, getRepo, inputPassword, openPath, showItemInFolder } from './base'
import { t } from './i18n'
import { getSetting, setSetting } from './setting'
import { changePosition } from './routines'
const logger = getLogger('document')
const lock = new AsyncLock()
@@ -626,10 +625,6 @@ async function _switchDoc (doc: Doc | null, opts?: SwitchDocOpts): Promise<void>
if (!force && store.state.currentFile !== undefined && isSameFile(doc, store.state.currentFile)) {
logger.debug('skip switch', doc)
triggerHook('DOC_SWITCH_SKIPPED', { doc, opts })
if (opts?.position) {
changePosition(opts.position)
}
return
}
@@ -693,10 +688,6 @@ async function _switchDoc (doc: Doc | null, opts?: SwitchDocOpts): Promise<void>
store.state.currentContent = content
triggerHook('DOC_SWITCHED', { doc: store.state.currentFile || null, opts })
if (opts?.position) {
changePosition(opts.position)
}
} catch (error: any) {
triggerHook('DOC_SWITCH_FAILED', { doc, message: error.message, opts })
useToast().show('warning', error.message.includes('Malformed') ? t('document.wrong-password') : error.message)