From 128aa83f739e85da7ddfbe231dc67a2c62fe46bf Mon Sep 17 00:00:00 2001 From: purocean Date: Mon, 17 Jul 2023 18:11:30 +0800 Subject: [PATCH] feat: display the file creation dialog when encountering a "NOENT" error while switching files --- src/renderer/startup.ts | 20 ++++++++++++++++++-- src/share/i18n/languages/en.ts | 4 ++++ src/share/i18n/languages/zh-CN.ts | 4 ++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/renderer/startup.ts b/src/renderer/startup.ts index 78293877..d2a46988 100644 --- a/src/renderer/startup.ts +++ b/src/renderer/startup.ts @@ -1,13 +1,15 @@ import { init } from '@fe/core/plugin' +import { getActionHandler } from '@fe/core/action' import { registerHook, triggerHook } from '@fe/core/hook' import store from '@fe/support/store' import { isElectron } from '@fe/support/env' +import { useToast } from './support/ui/toast' import { useModal } from '@fe/support/ui/modal' import * as storage from '@fe/utils/storage' import { basename } from '@fe/utils/path' import type { BuildInSettings, Doc, FrontMatterAttrs, Repo } from '@fe/types' import { reloadMainWindow } from '@fe/services/base' -import { isMarked, markDoc, switchDoc, unmarkDoc } from '@fe/services/document' +import { createDoc, isMarked, markDoc, switchDoc, toUri, unmarkDoc } from '@fe/services/document' import { refreshTree } from '@fe/services/tree' import { whenEditorReady } from '@fe/services/editor' import { getLanguage, setLanguage, t } from '@fe/services/i18n' @@ -82,9 +84,23 @@ registerHook('DOC_CREATED', refreshTree) registerHook('DOC_DELETED', refreshTree) registerHook('DOC_MOVED', refreshTree) registerHook('DOC_SWITCH_FAILED', refreshTree) -registerHook('DOC_SWITCH_FAILED', (payload?: { doc?: Doc | null, message: string }) => { +registerHook('DOC_SWITCH_FAILED', async (payload?: { doc?: Doc | null, message: string }) => { if (payload && payload.doc && payload.message?.includes('NOENT')) { unmarkDoc(payload.doc) + + // wait toast show then hide + await Promise.resolve() + await Promise.resolve() + await Promise.resolve() + useToast().hide() + + useModal().confirm({ + title: t('document.switch-noent-dialog.title'), + content: t('document.switch-noent-dialog.content', payload.doc.path), + }).then(async v => { + getActionHandler('file-tabs.close-tabs')([toUri(payload.doc)]) + v && await createDoc(payload.doc!) + }) } }) diff --git a/src/share/i18n/languages/en.ts b/src/share/i18n/languages/en.ts index ba66da39..47eb431e 100644 --- a/src/share/i18n/languages/en.ts +++ b/src/share/i18n/languages/en.ts @@ -201,6 +201,10 @@ const data = { 'title': 'Attention', 'content': 'The password doesn\'t match the old password, save it with the new password?', }, + 'switch-noent-dialog': { + 'title': 'File Not Found', + 'content': 'File [%s] not found, do you want to create it?', + } }, 'status-bar': { 'view': { diff --git a/src/share/i18n/languages/zh-CN.ts b/src/share/i18n/languages/zh-CN.ts index 5dddc231..7df36ccf 100644 --- a/src/share/i18n/languages/zh-CN.ts +++ b/src/share/i18n/languages/zh-CN.ts @@ -202,6 +202,10 @@ const data: BaseLanguage = { 'title': '提示', 'content': '密码和上一次输入的密码不一致,是否用新密码保存?', }, + 'switch-noent-dialog': { + 'title': '文件不存在', + 'content': '文件 [%s] 不存在,是否要创建?', + } }, 'status-bar': { 'view': {