mirror of
https://github.com/fancive/obsidian-parallel-reader.git
synced 2026-07-22 06:53:43 +00:00
fix: confirm before overwriting exports
Change-Id: I922fca90e46a8982686a3449b00ed793e924097b
This commit is contained in:
parent
5304ed3a48
commit
2bf4e86451
4 changed files with 45 additions and 2 deletions
|
|
@ -45,6 +45,10 @@ export const STRINGS: Record<string, Record<string, string>> = {
|
|||
copyFailed: '复制失败:{error}',
|
||||
actionFailed: '{label}失败:{error}',
|
||||
exported: '已导出 → {path}',
|
||||
exportCancelled: '已取消导出',
|
||||
confirmExportOverwrite: '导出文件已存在:{path}\n是否覆盖?',
|
||||
confirmExportCancel: '取消',
|
||||
confirmExportOverwriteButton: '覆盖',
|
||||
noCurrentNote: '没有当前笔记',
|
||||
cacheClearedFile: '已清除缓存:{name}',
|
||||
cacheClearedAll: '已清除 {count} 条缓存',
|
||||
|
|
@ -207,6 +211,10 @@ export const STRINGS: Record<string, Record<string, string>> = {
|
|||
copyFailed: 'Copy failed: {error}',
|
||||
actionFailed: '{label} failed: {error}',
|
||||
exported: 'Exported → {path}',
|
||||
exportCancelled: 'Export cancelled',
|
||||
confirmExportOverwrite: 'Export file already exists: {path}\nOverwrite it?',
|
||||
confirmExportCancel: 'Cancel',
|
||||
confirmExportOverwriteButton: 'Overwrite',
|
||||
noCurrentNote: 'No current note',
|
||||
cacheClearedFile: 'Cleared cache: {name}',
|
||||
cacheClearedAll: 'Cleared {count} cache entries',
|
||||
|
|
|
|||
22
src/modal.ts
22
src/modal.ts
|
|
@ -4,7 +4,7 @@ import { type App, Modal } from 'obsidian';
|
|||
import type { CardPatch, PluginHost, ResolvedCard } from './types';
|
||||
import { addTextButton } from './ui-helpers';
|
||||
|
||||
export function confirmRegenerateEditedCards(
|
||||
function confirmAction(
|
||||
app: App,
|
||||
title: string,
|
||||
message: string,
|
||||
|
|
@ -35,6 +35,26 @@ export function confirmRegenerateEditedCards(
|
|||
});
|
||||
}
|
||||
|
||||
export function confirmRegenerateEditedCards(
|
||||
app: App,
|
||||
title: string,
|
||||
message: string,
|
||||
cancelText: string,
|
||||
confirmText: string,
|
||||
): Promise<boolean> {
|
||||
return confirmAction(app, title, message, cancelText, confirmText);
|
||||
}
|
||||
|
||||
export function confirmExportOverwrite(
|
||||
app: App,
|
||||
title: string,
|
||||
message: string,
|
||||
cancelText: string,
|
||||
confirmText: string,
|
||||
): Promise<boolean> {
|
||||
return confirmAction(app, title, message, cancelText, confirmText);
|
||||
}
|
||||
|
||||
export class CardEditModal extends Modal {
|
||||
plugin: PluginHost;
|
||||
card: ResolvedCard;
|
||||
|
|
|
|||
13
src/view.ts
13
src/view.ts
|
|
@ -3,7 +3,7 @@
|
|||
import { ItemView, MarkdownRenderer, Menu, Notice, TFile, type WorkspaceLeaf } from 'obsidian';
|
||||
import { activeIndexAfterCardDelete, removeCardAt, updateCardAt } from './cards';
|
||||
import { cardsToMarkdown, cardToMarkdown, cardToPlain } from './markdown';
|
||||
import { CardEditModal } from './modal';
|
||||
import { CardEditModal, confirmExportOverwrite } from './modal';
|
||||
import { activeSectionLine, nextCardIndex } from './navigation';
|
||||
import type { CardPatch, PluginHost, ResolvedCard } from './types';
|
||||
import { addIconButton, addTextButton, copyToClipboard } from './ui-helpers';
|
||||
|
|
@ -438,6 +438,17 @@ export class ParallelReaderView extends ItemView {
|
|||
|
||||
const existing = app.vault.getAbstractFileByPath(targetPath);
|
||||
if (existing instanceof TFile) {
|
||||
const shouldOverwrite = await confirmExportOverwrite(
|
||||
this.app,
|
||||
this.plugin.t('displayName'),
|
||||
this.plugin.t('confirmExportOverwrite', { path: targetPath }),
|
||||
this.plugin.t('confirmExportCancel'),
|
||||
this.plugin.t('confirmExportOverwriteButton'),
|
||||
);
|
||||
if (!shouldOverwrite) {
|
||||
new Notice(this.plugin.t('exportCancelled'));
|
||||
return;
|
||||
}
|
||||
await app.vault.modify(existing, markdown);
|
||||
} else {
|
||||
await app.vault.create(targetPath, markdown);
|
||||
|
|
|
|||
|
|
@ -27,5 +27,9 @@ assert.strictEqual(t.translate({ uiLanguage: 'zh' }, 'appTitle'), '对照阅读
|
|||
assert.strictEqual(t.translate({ uiLanguage: 'en' }, '__no_such_key__'), '__no_such_key__', 'missing key returns key');
|
||||
assert.strictEqual(t.translate({ uiLanguage: 'zh' }, 'settingTestBackendButton'), '测试');
|
||||
assert.strictEqual(t.translate({ uiLanguage: 'en' }, 'confirmRegenerateProceed'), 'Regenerate');
|
||||
assert.strictEqual(
|
||||
t.translate({ uiLanguage: 'en' }, 'confirmExportOverwrite', { path: 'Reading/A.md' }),
|
||||
'Export file already exists: Reading/A.md\nOverwrite it?',
|
||||
);
|
||||
|
||||
console.log('i18n tests passed');
|
||||
|
|
|
|||
Loading…
Reference in a new issue