Skip to content

Commit

Permalink
🔧 Fix exit saving logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonShiki committed Jan 11, 2023
1 parent aabc4d9 commit 52af15f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
25 changes: 22 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) => {
Expand Down
27 changes: 24 additions & 3 deletions src/components/TitleBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -18,11 +20,19 @@ interface TitleBar {
};
}

const availbleExts = [{
name: 'Markdown',
extensions: ['md']
}, {
name: 'Text file',
extensions: ['txt']
}];

const TitleBar : React.FC<TitleBar> = ({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 (
<>
Expand Down Expand Up @@ -57,8 +67,19 @@ const TitleBar : React.FC<TitleBar> = ({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();
Expand Down

0 comments on commit 52af15f

Please sign in to comment.