From 52af15f38bd5d13903116fcb054e7b8ae2ec929e Mon Sep 17 00:00:00 2001 From: SinanGentoo Date: Wed, 11 Jan 2023 18:00:44 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Fix=20exit=20saving=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.tsx | 25 ++++++++++++++++++++++--- src/components/TitleBar/index.tsx | 27 ++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index afa7c779..dafc278d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -15,13 +15,23 @@ import { settingsJotai } from "./jotais/settings"; import { useKeyPress, useInterval, useEventListener, useAsyncEffect, useUpdateLayoutEffect } from "ahooks"; import { version as getVersion, type as getType } from '@tauri-apps/api/os'; import About from "./components/About"; +import { save as saveFilePicker } from '@tauri-apps/api/dialog'; +import { documentDir } from '@tauri-apps/api/path'; import { invoke } from '@tauri-apps/api/tauri'; import FloatingToolbar from "./components/FloatingToolbar"; import { Editor } from "@milkdown/core"; +const availbleExts = [{ + name: 'Markdown', + extensions: ['md'] +}, { + name: 'Text file', + extensions: ['txt'] +}]; + function App() { const [content, setContent] = useAtom(contentJotai); - const [filePath] = useAtom(filePathJotai); + const [filePath, setFilePath] = useAtom(filePathJotai); const [loading] = useAtom(loadingJotai); const [, setToolbar] = useAtom(toolbarJotai); const [, setSaved] = useAtom(savedJotai); @@ -49,8 +59,17 @@ function App() { // Shortcuts useKeyPress('ctrl.s', async () => { - if (filePath === null) return; - await writeTextFile({ path: filePath, contents: content }); + let selected = filePath ?? ''; + if (!selected) { + const _selected = await saveFilePicker({ + defaultPath: await documentDir(), + filters: availbleExts + }); + if (_selected === null) return; + selected = _selected; + setFilePath(selected as string); + } + await writeTextFile({ path: selected, contents: content }); setSaved(true); }); useKeyPress('ctrl.f', (e) => { diff --git a/src/components/TitleBar/index.tsx b/src/components/TitleBar/index.tsx index e87db6ed..ce1ffe6b 100644 --- a/src/components/TitleBar/index.tsx +++ b/src/components/TitleBar/index.tsx @@ -6,6 +6,8 @@ import styles from './title-bar.module.scss'; import { Add20Regular, ArrowDown20Regular, FullScreenMaximize20Regular } from '@fluentui/react-icons'; import FileMenu from '../FileMenu'; import EditMenu from '../EditMenu'; +import { save as saveFilePicker } from '@tauri-apps/api/dialog'; +import { documentDir } from '@tauri-apps/api/path'; import { useAtom } from 'jotai'; import { aboutJotai, preferenceJotai } from '../../jotais/ui'; import { Editor } from '@milkdown/core'; @@ -18,11 +20,19 @@ interface TitleBar { }; } +const availbleExts = [{ + name: 'Markdown', + extensions: ['md'] +}, { + name: 'Text file', + extensions: ['txt'] +}]; + const TitleBar : React.FC = ({editorInstance}) => { const [preference] = useAtom(preferenceJotai); const [about] = useAtom(aboutJotai); const [content] = useAtom(contentJotai); - const [filePath] = useAtom(filePathJotai); + const [filePath, setFilePath] = useAtom(filePathJotai); const [saved] = useAtom(savedJotai); return ( <> @@ -57,8 +67,19 @@ const TitleBar : React.FC = ({editorInstance}) => { title: 'You haven\'t saved it yet', type: 'warning' }); - if (result && filePath !== null) { - await writeTextFile({ path: filePath, contents: content }); + + if (result) { + let selected = filePath ?? ''; + if (!selected) { + const _selected = await saveFilePicker({ + defaultPath: await documentDir(), + filters: availbleExts + }); + if (_selected === null) return; + selected = _selected; + setFilePath(selected as string); + } + await writeTextFile({ path: selected, contents: content }); } else return; } await appWindow.close();