Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 14 additions & 61 deletions src/lib/MarkdownViewer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -1311,67 +1311,26 @@ import { t } from './utils/i18n.js';
return true;
}

async function toggleEdit(silentSave = false) {
async function toggleEdit() {
const tab = tabManager.activeTab;
if (!tab || tab.path === undefined) return;

if (isEditing) {
// Switch back to view
if (tab.isDirty && tab.path !== '') {
// `confirmBeforeSave` always wins: when the user has asked
// for confirmation, every dirty toggle must show the modal,
// even if the caller passed `silentSave=true` (hotkey path).
const shouldSilent =
!settings.confirmBeforeSave && (silentSave || settings.autoSave);
if (shouldSilent) {
cancelPendingAutoSave(tab.id);
const success = await saveContent(tab.id);
if (!success) {
addToast(t('toast.autoSaveFailed', settings.language), 'error');
return; // If save fails, stay in edit mode
}
} else {
const response = await askCustom(t('modal.youHaveUnsavedChangesBeforeReturning', settings.language), {
title: t('modal.unsavedChanges', settings.language),
kind: 'warning',
showSave: true,
});

// Cancel only happens on save / discard. If user picks
// Cancel, the pending auto-save timer keeps running.
if (response === 'cancel') return;
if (response === 'save') {
cancelPendingAutoSave(tab.id);
const success = await saveContent(tab.id);
if (!success) return;
} else if (response === 'discard') {
cancelPendingAutoSave(tab.id);
tab.rawContent = tab.originalContent;
tab.isDirty = false;
}
}
}
// If `saveContent` left `tab.isDirty=true` (TOCTOU — user typed
// during the await), staying in edit mode is the safe default:
// a non-editable dirty tab disables auto-save, blocks Cmd+S,
// and risks getting clobbered by the next disk reload. Surface
// a hint and keep the tab editable.
if (tab.path !== '' && tab.isDirty) {
addToast(t('toast.savedNewerEdits', settings.language), 'info');
return;
}

// Switch to view — never auto-save; render in-memory content so
// unsaved edits are visible and the file stays dirty until the
// user explicitly saves with Ctrl+S.
tab.isEditing = false;
if (tab.path !== '') {
if (tab.path !== '' && !tab.isDirty) {
// Clean saved file: load from disk (handles large-file chunking).
await loadMarkdown(tab.path, { preserveEditState: true });
} else {
// Untitled: render the in-memory buffer for the preview.
// Dirty or untitled: render the current in-memory buffer.
try {
const html = (await invoke('render_markdown', { content: tab.rawContent })) as string;
const processedInfo = processMarkdownHtml(html, '', collapsedHeaders);
const processedInfo = processMarkdownHtml(html, tab.path, collapsedHeaders);
tabManager.updateTabContent(tab.id, processedInfo);
} catch (e) {
console.error('Failed to render markdown', e);
console.error('Failed to render markdown for preview', e);
}
}
} else {
Expand Down Expand Up @@ -2051,13 +2010,11 @@ import { t } from './utils/i18n.js';
}
if (cmdOrCtrl && key === 'e') {
e.preventDefault();
if (!isSplit) toggleEdit(true);
if (!isSplit) toggleEdit();
}
if (cmdOrCtrl && key === 's') {
if (isEditing || isSplit) {
e.preventDefault();
saveContent();
}
e.preventDefault();
saveContent();
}

if (cmdOrCtrl && e.shiftKey && key === 't') {
Expand Down Expand Up @@ -2395,16 +2352,12 @@ import { t } from './utils/i18n.js';
);
// Native macOS menubar — Markpad ▸ Quit and File ▸ * — bridged
// to the same handlers the in-window burger button uses, so the
// menu and the burger stay behaviourally identical. Save mirrors
// the keydown guard (`isEditing || isSplit`) so menu ⌘S in pure
// view mode is a no-op, matching the keyboard shortcut.
// menu and the burger stay behaviourally identical.
unlisteners.push(await listen('menu-app-quit', () => appExit()));
unlisteners.push(await listen('menu-file-new', () => handleNewFile()));
unlisteners.push(await listen('menu-file-open', () => selectFile()));
unlisteners.push(await listen('menu-file-close', () => closeFile()));
unlisteners.push(await listen('menu-file-save', () => {
if (isEditing || tabManager.activeTab?.isSplit) saveContent();
}));
unlisteners.push(await listen('menu-file-save', () => saveContent()));
unlisteners.push(await listen('menu-file-save-as', () => saveContentAs()));
unlisteners.push(await listen('menu-file-export-html', () => exportAsHtml()));
unlisteners.push(await listen('menu-file-export-pdf', () => exportAsPdf()));
Expand Down
2 changes: 2 additions & 0 deletions src/lib/components/HomePage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
</button>
</div>

{#if settings.showRecentFiles}
<div class="recent-section">
<h3>{t('home.recentFiles', settings.language)}</h3>
{#if recentFiles.length > 0}
Expand Down Expand Up @@ -108,6 +109,7 @@
<p class="empty-recent">{t('home.noRecentFiles', settings.language)}</p>
{/if}
</div>
{/if}
</div>
<div class="version-tag">v{version}</div>

Expand Down
7 changes: 7 additions & 0 deletions src/lib/components/Settings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,13 @@
<span class="toggle-slider"></span>
</label>
</div>
<div class="setting-item">
<label for="appearance-recent-files">{t('settings.showRecentFiles', settings.language)}</label>
<label class="toggle">
<input id="appearance-recent-files" type="checkbox" checked={settings.showRecentFiles} onchange={() => settings.toggleShowRecentFiles()} />
<span class="toggle-slider"></span>
</label>
</div>
<div class="setting-item">
<label for="appearance-toc">{t('settings.showTableOfContents', settings.language)}</label>
<label class="toggle">
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/TitleBar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@
{t('menu.openFile', currentLanguage)}
<span class="menu-shortcut">{modifier}+O</span>
</button>
{#if currentFile !== '' || (tabManager.activeTab && tabManager.activeTab.isEditing)}
{#if currentFile !== '' || (tabManager.activeTab && (tabManager.activeTab.isEditing || tabManager.activeTab.content))}
<button
class="home-menu-item"
onclick={() => {
Expand Down
8 changes: 8 additions & 0 deletions src/lib/stores/settings.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export class SettingsStore {
occurrencesHighlight = $state(false);
showWhitespace = $state(false);
startInEditor = $state(false);
showRecentFiles = $state(true);
editorMaxWidth = $state(80);
pinnedToc = $state(false);
tocSide = $state<'left' | 'right'>('left');
Expand Down Expand Up @@ -187,6 +188,7 @@ export class SettingsStore {
const savedShowToc = localStorage.getItem('editor.showToc');
const savedHighlightColor = localStorage.getItem('editor.highlightColor');
const savedStartInEditor = localStorage.getItem('editor.startInEditor');
const savedShowRecentFiles = localStorage.getItem('editor.showRecentFiles');
const savedEditorMaxWidth = localStorage.getItem('editor.maxWidth');
const savedPinnedToc = localStorage.getItem('editor.pinnedToc');
const savedTocSide = localStorage.getItem('editor.tocSide');
Expand Down Expand Up @@ -229,6 +231,7 @@ export class SettingsStore {
if (savedShowToc !== null) this.showToc = savedShowToc === 'true';
if (savedHighlightColor !== null) this.highlightColor = savedHighlightColor;
if (savedStartInEditor !== null) this.startInEditor = savedStartInEditor === 'true';
if (savedShowRecentFiles !== null) this.showRecentFiles = savedShowRecentFiles === 'true';
if (savedEditorMaxWidth !== null) this.editorMaxWidth = parseFontSize(savedEditorMaxWidth, 80, 20, 500);
if (savedPinnedToc !== null) this.pinnedToc = savedPinnedToc === 'true';
if (savedTocSide !== null) this.tocSide = savedTocSide as 'left' | 'right';
Expand Down Expand Up @@ -294,6 +297,7 @@ export class SettingsStore {
localStorage.setItem('editor.showToc', String(this.showToc));
localStorage.setItem('editor.highlightColor', this.highlightColor);
localStorage.setItem('editor.startInEditor', String(this.startInEditor));
localStorage.setItem('editor.showRecentFiles', String(this.showRecentFiles));
localStorage.setItem('editor.maxWidth', String(this.editorMaxWidth));
localStorage.setItem('editor.pinnedToc', String(this.pinnedToc));
localStorage.setItem('editor.tocSide', this.tocSide);
Expand Down Expand Up @@ -360,6 +364,10 @@ export class SettingsStore {
this.restoreStateOnReopen = !this.restoreStateOnReopen;
}

toggleShowRecentFiles() {
this.showRecentFiles = !this.showRecentFiles;
}

toggleZenMode() {
this.zenMode = !this.zenMode;
if (this.zenMode) {
Expand Down
Loading
Loading