Skip to content
Merged
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
50 changes: 37 additions & 13 deletions packages/tok-ui/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CommandPalette, PaletteItem } from './components/CommandPalette';
import { StatusBar } from './components/StatusBar';
import { SpreadCanvas, isRightHandPage, isRectoPage, groupIntoSpreads } from './components/SpreadCanvas';
import { ExportDialog, ExportOptions } from './components/ExportDialog';
import { WelcomeModal, addRecentProject, updateProjectClosedTime, removeRecentProject, isDemoProject, purgeAllDemoProjects } from './components/WelcomeModal';
import { WelcomeModal, addRecentProject, getRecentProjects, updateProjectClosedTime, removeRecentProject, isDemoProject, purgeAllDemoProjects } from './components/WelcomeModal';
import { SettingsModal } from './components/SettingsModal';
import { AboutModal } from './components/AboutModal';
import { UpdatesDrawer } from './components/UpdatesDrawer';
Expand Down Expand Up @@ -2658,6 +2658,14 @@ export class TypesetOkApp {
throw new Error('IPC reader not available');
}

// An imported source file keeps no title of its own, so a title the user chose earlier lives in the
// recent-projects entry; without this the title is re-derived from the file name and appears changed.
if (explicitPath && !/\.tok$/i.test(targetPath)) {
const known = getRecentProjects().find((p) => p.path === targetPath);
const savedTitle = known?.name?.replace(/\.tok$/i, '').trim();
if (savedTitle) result.title = savedTitle;
}

// Re-opening a known file (recent projects, OS file association) reuses the flow chosen the first
// time; the picker still appears for a first import and for an explicit "Import document".
const remembered = explicitPath ? recallImportTemplate(targetPath) : null;
Expand Down Expand Up @@ -3161,34 +3169,50 @@ export class TypesetOkApp {
const trimmed = newTitle.trim();
if (!trimmed || trimmed === this.documentTitle) return;
const oldTitle = this.documentTitle;
const oldPath = this.currentFilePath;
this.documentTitle = trimmed;
this.documentState.title = trimmed;
this.showDocumentTitle(trimmed, true);

const win = window as any;
if (this.currentFilePath && win.tokIpc?.renameDocumentFile) {
const dir = this.currentFilePath.replace(/[^/\\]+$/, '');
let recentPath = oldPath;
// Only a real .tok project file is renamed on disk. An imported source (.docx/.md/.txt) is the
// user's original file and must never be renamed or turned into a ".tok" file by a title change.
if (oldPath && /\.tok$/i.test(oldPath) && win.tokIpc?.renameDocumentFile) {
const dir = oldPath.replace(/[^/\\]+$/, '');
const safeNewName = trimmed.replace(/[<>:"/\\|?*]/g, '_').trim() || 'document';
const newPath = `${dir}${safeNewName}.tok`;
if (newPath !== this.currentFilePath) {
if (newPath !== oldPath) {
try {
const res = await win.tokIpc.renameDocumentFile({ oldPath: this.currentFilePath, newPath });
const res = await win.tokIpc.renameDocumentFile({ oldPath, newPath });
if (res?.success) {
removeRecentProject(oldTitle);
removeRecentProject(this.currentFilePath);
this.currentFilePath = newPath;
addRecentProject({
name: `${safeNewName}.tok`,
path: newPath,
pages: this.pages.length || 1,
lastSavedAt: new Date().toISOString()
});
recentPath = newPath;
}
} catch (e) {
console.warn('[TOK] Failed to rename physical file:', e);
}
}
}

// Keep the "recent projects" entry in sync with the new title: replace the old entry (matched by
// path or by either name form) with one that carries the title exactly as the user typed it.
if (recentPath) {
const previous = getRecentProjects().find(
(p) => p.path === oldPath || p.name === oldTitle || p.name === `${oldTitle}.tok`
);
if (oldPath) removeRecentProject(oldPath);
removeRecentProject(oldTitle);
removeRecentProject(`${oldTitle}.tok`);
addRecentProject({
name: `${trimmed}.tok`,
path: recentPath,
pages: this.pages.length || previous?.pages || 1,
template: previous?.template,
lastSavedAt: previous?.lastSavedAt || new Date().toISOString(),
lastClosedAt: previous?.lastClosedAt
});
}
this.setDirty(true);
this.showToast(tf('toastTitleRenamed', { title: trimmed }));
}
Expand Down
5 changes: 3 additions & 2 deletions packages/tok-ui/src/components/WelcomeModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,10 @@ export function updateProjectClosedTime(nameOrPath: string): void {
} catch {}
}

export function removeRecentProject(name: string): void {
export function removeRecentProject(nameOrPath: string): void {
if (!nameOrPath) return;
try {
const list = getRecentProjects().filter((p) => p.name !== name);
const list = getRecentProjects().filter((p) => p.name !== nameOrPath && p.path !== nameOrPath);
localStorage.setItem('tok_recent_projects', JSON.stringify(list));
} catch {}
}
Expand Down
21 changes: 21 additions & 0 deletions test/frontend.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3776,6 +3776,27 @@ describe('Home screen, dialogs and About screen', () => {
assert.strictEqual(getRecentProjects().length, 2);
});

test('renaming a document keeps its recent entry consistent and never renames an imported source file (issue #34)', async () => {
const { addRecentProject, removeRecentProject, getRecentProjects } = await loadTs('packages/tok-ui/src/components/WelcomeModal.ts');
const mockStorage = {};
global.localStorage = {
getItem: (k) => mockStorage[k] || null,
setItem: (k, v) => { mockStorage[k] = String(v); },
removeItem: (k) => { delete mockStorage[k]; }
};
addRecentProject({ name: 'ספר אישי.tok', path: '/docs/sefer.docx' });
addRecentProject({ name: 'ספר בראשית.tok', path: '/docs/bereshit.tok' });
removeRecentProject('/docs/sefer.docx');
assert.deepStrictEqual(getRecentProjects().map((p) => p.path), ['/docs/bereshit.tok'], 'an entry is removable by its path');
removeRecentProject('ספר בראשית.tok');
assert.strictEqual(getRecentProjects().length, 0, 'an entry is still removable by its name');

const appCode = read('packages/tok-ui/src/app.ts');
const rename = appCode.slice(appCode.indexOf('public async renameDocument'), appCode.indexOf('public setDirty'));
assert.ok(rename.includes('/\\.tok$/i.test(oldPath)'), 'only a .tok project file is renamed on disk');
assert.ok(rename.includes('name: `${trimmed}.tok`'), 'the recent entry carries the title exactly as typed, not the sanitized file name');
});

test('TEMPLATE_TITLES in app.ts contains no demo projects', () => {
const appCode = read('packages/tok-ui/src/app.ts');
assert.ok(!appCode.includes('דמו —'), 'No demo titles in TEMPLATE_TITLES');
Expand Down
Loading