From 7cf31f2dc3c744295312223ad70d09b190813be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=99=2E=D7=9E=2E=20=D7=9E=D7=90=D7=95=D7=90=D7=A1?= Date: Mon, 16 Nov 2026 23:59:27 +0200 Subject: [PATCH 1/2] fix(ui): keep document title after rename and never rename imported source files (#34) --- packages/tok-ui/src/app.ts | 50 ++++++++++++++----- .../tok-ui/src/components/WelcomeModal.ts | 5 +- test/frontend.test.mjs | 21 ++++++++ 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/packages/tok-ui/src/app.ts b/packages/tok-ui/src/app.ts index ac239d0..32ab47b 100644 --- a/packages/tok-ui/src/app.ts +++ b/packages/tok-ui/src/app.ts @@ -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'; @@ -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; @@ -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` + ); + 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 })); } diff --git a/packages/tok-ui/src/components/WelcomeModal.ts b/packages/tok-ui/src/components/WelcomeModal.ts index 4281776..1192de4 100644 --- a/packages/tok-ui/src/components/WelcomeModal.ts +++ b/packages/tok-ui/src/components/WelcomeModal.ts @@ -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 {} } diff --git a/test/frontend.test.mjs b/test/frontend.test.mjs index d3bb9de..f6378f1 100644 --- a/test/frontend.test.mjs +++ b/test/frontend.test.mjs @@ -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'); From a81230487db661af989106b1195be228da73ac36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=99=2E=D7=9E=2E=20=D7=9E=D7=90=D7=95=D7=90=D7=A1?= Date: Tue, 17 Nov 2026 00:05:46 +0200 Subject: [PATCH 2/2] fix(ui): keep document title after rename and never rename imported source files (#34) --- packages/tok-ui/src/app.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/tok-ui/src/app.ts b/packages/tok-ui/src/app.ts index 32ab47b..4b52ff4 100644 --- a/packages/tok-ui/src/app.ts +++ b/packages/tok-ui/src/app.ts @@ -3201,7 +3201,7 @@ export class TypesetOkApp { const previous = getRecentProjects().find( (p) => p.path === oldPath || p.name === oldTitle || p.name === `${oldTitle}.tok` ); - removeRecentProject(oldPath); + if (oldPath) removeRecentProject(oldPath); removeRecentProject(oldTitle); removeRecentProject(`${oldTitle}.tok`); addRecentProject({