From fcb9f2465dd2c87e55f98c105dd1beca45fbab2c Mon Sep 17 00:00:00 2001 From: Lptenoss Date: Fri, 4 Dec 2026 11:44:45 +0700 Subject: [PATCH 1/3] =?UTF-8?q?fix(viewer):=20a=20click=20on=20Save=20leav?= =?UTF-8?q?es=20the=20focus=20in=20the=20document,=20so=20Ctrl/=E2=8C=98+Z?= =?UTF-8?q?=20undoes=20there?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Save button took the focus on a click and was disabled while it saved. A disabled button loses its focus to the page, out of the viewer, so Ctrl/⌘+Z and every other shortcut did nothing until the document was clicked again. The Undo button still worked. The button now keeps the focus where it was on mousedown, as the toolbar's buttons do, and is aria-disabled while it saves, so a keyboard focus on it stays too. saveFile already refuses a second save while one runs. The e2e tests that clicked back into the document after Save no longer do. Co-Authored-By: Claude Opus 5.5 --- e2e/docx-track.spec.ts | 2 +- e2e/focus-qa3.spec.ts | 65 +++++++++++++++++++++++++++++++++++++++ e2e/markdown-math.spec.ts | 3 +- e2e/pptx.spec.ts | 6 +--- e2e/xlsx.spec.ts | 3 +- src/react/DocViewer.tsx | 12 +++++++- src/react/styles.css | 2 +- 7 files changed, 81 insertions(+), 12 deletions(-) diff --git a/e2e/docx-track.spec.ts b/e2e/docx-track.spec.ts index 7d3788be..8c46d1a7 100644 --- a/e2e/docx-track.spec.ts +++ b/e2e/docx-track.spec.ts @@ -62,7 +62,7 @@ describe('docx tracked changes', () => { await v.page.waitForTimeout(250) let ps = await docxParas((await v.save()).bytes) expect(show(ps.find((p) => show(p).startsWith('Hire'))!)).toBe('Hire the platform tea[-m]') - // typing at the caret (Save took the focus: put it back after "tea") goes in as inserted text, before the deletion + // typing at the caret, after "tea", goes in as inserted text, before the deletion await v.selectText('platform tea', 'end') await v.page.keyboard.type('ms') await v.page.waitForTimeout(250) diff --git a/e2e/focus-qa3.spec.ts b/e2e/focus-qa3.spec.ts index 972f31b3..b67c5961 100644 --- a/e2e/focus-qa3.spec.ts +++ b/e2e/focus-qa3.spec.ts @@ -86,3 +86,68 @@ describe('QA 3: spreadsheets', () => { expect(v.errors).toEqual([]) }) }) + +describe('a click on Save leaves the keyboard in the document', () => { + test('a document: the caret stays, Ctrl/⌘+Z undoes the typing just saved', async () => { + const v = await viewer({ src: 'samples/sample.docx', editable: true, onSave: true }) + await v.page.locator('.de-ebar').waitFor() + await v.selectText('Quarterly Report', 'end') + await v.page.keyboard.type('XYZ') + await v.page.waitForTimeout(200) + await v.save() + expect((await active(v)).cls).toContain('de-pages') + await v.page.keyboard.press('ControlOrMeta+z') + await v.page.waitForTimeout(200) + expect(await v.pagesText()).not.toContain('Quarterly ReportXYZ') + expect(v.errors).toEqual([]) + }) + + test('a sheet: the grid keeps the focus, Ctrl/⌘+Z takes the cell back', async () => { + const v = await viewer({ src: 'samples/sample.xlsx', editable: true, onSave: true }) + await v.page.locator('.de-ebar').waitFor() + await v.typeCell('J10', 'saved') + await v.save() + expect((await active(v)).inViewer).toBe(true) + await v.page.keyboard.press('ControlOrMeta+z') + await v.page.waitForTimeout(300) + expect(await v.shown('J10')).toBe('') + expect(v.errors).toEqual([]) + }) + + test('slides: Ctrl/⌘+Z takes back the text typed in a shape', async () => { + const v = await viewer({ src: 'samples/sample.pptx', editable: true, onSave: true }) + await v.page.locator('.de-ebar').waitFor() + const slide = v.page.locator('.de-slide-frame').first() + await slide.getByText('office-engine').first().click() + await v.page.locator('.de-txt-edit').waitFor() + await v.page.keyboard.press('ControlOrMeta+a') + await v.page.keyboard.type('Saved title') + await v.page.keyboard.press('Escape') + await v.page.keyboard.press('Escape') + await v.page.waitForTimeout(300) + await v.save() + expect((await active(v)).cls).toContain('de-scroll') + await v.page.keyboard.press('ControlOrMeta+z') + await v.page.waitForFunction(() => !document.querySelector('.de-slide-frame')?.textContent?.includes('Saved title')) + expect(await slide.innerText()).toContain('office-engine') + expect(v.errors).toEqual([]) + }) + + test('saving: the button is shown as busy but keeps a keyboard focus it had', async () => { + const v = await viewer({ src: 'samples/sample.docx', editable: true, onSave: true }) + await v.page.locator('.de-ebar').waitFor() + await v.selectText('Quarterly Report', 'end') + await v.page.keyboard.type('!') + await v.page.waitForTimeout(200) + const button = v.barButton('Save') + await button.focus() + const n = await saves(v) + await v.page.keyboard.press('Enter') + await v.waitEvent('save', n + 1) + await v.page.waitForTimeout(100) + const a = await active(v) + expect(a.tag).toBe('BUTTON') + expect(a.inViewer).toBe(true) + expect(v.errors).toEqual([]) + }) +}) diff --git a/e2e/markdown-math.spec.ts b/e2e/markdown-math.spec.ts index a62afa23..46d567de 100644 --- a/e2e/markdown-math.spec.ts +++ b/e2e/markdown-math.spec.ts @@ -124,8 +124,7 @@ describe('Markdown math in the document', () => { // (the paragraph is written again: its dollar signs are escaped only where they would be read as math) expect(saved).toContain('Euler’s identity: $e^{i\\pi} + 1 = 0$It costs $5 and $10, see `$x$`.') expect(saved).toContain('Euler’s identity: $e^{i\\pi} + 1 = 0$ is famous.') - // (Save took the focus) - await v.selectText('It costs', 'start') + // (Save left the focus in the document) await page.keyboard.press('Control+z') await page.waitForTimeout(300) expect(await inline()).toBe(3) diff --git a/e2e/pptx.spec.ts b/e2e/pptx.spec.ts index 04b45c97..2aecd97a 100644 --- a/e2e/pptx.spec.ts +++ b/e2e/pptx.spec.ts @@ -77,11 +77,7 @@ describe('pptx editing', () => { expect(s1.meta.count).toBe(5) expect((await s1.slide(0)).els.some((e: any) => e.geom === 'rect' && e.fill?.c === '#00ff00')).toBe(true) - // Save took the focus: select the shape again (it is the topmost at the slide center) - const f = (await slide1(v).boundingBox())! - await v.page.mouse.click(f.x + f.width / 2, f.y + f.height / 2) - // a click on a shape with a text body edits its text: Esc keeps the shape selected - if (await v.page.locator('.de-txt-edit').count()) await stopEditing(v) + // Save left the shape selected await v.btn('Delete').waitFor() await v.btn('Delete').click() await v.page.waitForTimeout(300) diff --git a/e2e/xlsx.spec.ts b/e2e/xlsx.spec.ts index 875ecebf..3cf37aa8 100644 --- a/e2e/xlsx.spec.ts +++ b/e2e/xlsx.spec.ts @@ -1087,8 +1087,7 @@ describe('xlsx whole columns and rows (QA 19/11/2026, P2-042)', () => { // the column holds what was typed and where the row crosses it, not a cell per row expect((xml.match(/ )} {client && canSave && ((editing && !readOnly) || dirty) && ( - diff --git a/src/react/styles.css b/src/react/styles.css index c774f625..997c28af 100644 --- a/src/react/styles.css +++ b/src/react/styles.css @@ -705,7 +705,7 @@ .de-mode-on { border-color: var(--de-accent); color: var(--de-accent); } .de-save-dirty { background: var(--de-accent); border-color: var(--de-accent); color: var(--de-accent-ink); } .de-save-dirty:hover { background: var(--de-accent-hover); } -.de-mode:disabled { opacity: 0.6; cursor: default; } +.de-mode:disabled, .de-mode[aria-disabled='true'] { opacity: 0.6; cursor: default; } .de-ebar-host { flex: none; padding: 6px 10px; From b59fbfe380e2fc6b574e6b5ee3afbad7d92ccee2 Mon Sep 17 00:00:00 2001 From: Lptenoss Date: Sat, 5 Dec 2026 04:33:27 +0700 Subject: [PATCH 2/3] fix(xlsx): a click on Save writes the cell still being typed in Since the Save button keeps the focus in the document, a click on it no longer blurs the cell being typed in, and the blur was what wrote it: the file was saved without the text. The sheet's flush now writes that cell, as Enter would, and waits for the edits sent, before a save or an export from the File menu. Co-Authored-By: Claude Opus 5.5 --- e2e/focus-qa3.spec.ts | 23 ++++++++++++++++++++++- src/react/xlsx/XlsxView.tsx | 15 ++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/e2e/focus-qa3.spec.ts b/e2e/focus-qa3.spec.ts index b67c5961..1638592a 100644 --- a/e2e/focus-qa3.spec.ts +++ b/e2e/focus-qa3.spec.ts @@ -1,6 +1,6 @@ /** QA round 3: the keyboard after a dialog, a thumbnail, a sheet tab */ import { describe, expect, test } from 'bun:test' -import { openFile, useBrowser, viewer, type Viewer } from './harness' +import { openFile, useBrowser, viewer, xlsxSheet, type Viewer } from './harness' useBrowser() @@ -114,6 +114,27 @@ describe('a click on Save leaves the keyboard in the document', () => { expect(v.errors).toEqual([]) }) + test('a sheet: a cell still being typed in, in the cell or the formula bar, is written into the file saved', async () => { + const v = await viewer({ src: 'samples/sample.xlsx', editable: true, onSave: true }) + await v.page.locator('.de-ebar').waitFor() + // no Enter: the edit is still open when Save is clicked + await v.clickCell('J10') + await v.page.keyboard.type('typed, then Save') + expect((await xlsxSheet((await v.save()).bytes)).cell('J10')).toBe('typed, then Save') + expect((await active(v)).inViewer).toBe(true) + await v.clickCell('J11') + await v.page.locator('.de-fx-input').click() + await v.page.keyboard.type('in the bar, then Save') + const s = await xlsxSheet((await v.save()).bytes) + expect(s.cell('J10')).toBe('typed, then Save') + expect(s.cell('J11')).toBe('in the bar, then Save') + // the edit is closed: Ctrl/⌘+Z takes the last cell back + await v.page.keyboard.press('ControlOrMeta+z') + await v.page.waitForTimeout(300) + expect(await v.shown('J11')).toBe('') + expect(v.errors).toEqual([]) + }) + test('slides: Ctrl/⌘+Z takes back the text typed in a shape', async () => { const v = await viewer({ src: 'samples/sample.pptx', editable: true, onSave: true }) await v.page.locator('.de-ebar').waitFor() diff --git a/src/react/xlsx/XlsxView.tsx b/src/react/xlsx/XlsxView.tsx index c6ed79c5..79bf3323 100644 --- a/src/react/xlsx/XlsxView.tsx +++ b/src/react/xlsx/XlsxView.tsx @@ -388,7 +388,11 @@ export function XlsxView({ client, onStatus, initialSheet }: XlsxViewProps) { // The workbook owns completion publication even when the grid that began the edit changed sheets. // Layout cleanup removes this listener before a replacement document can receive late dirty state. useLayoutEffect(() => store.onEdit(() => viewer.setDirty(true)), [store, viewer.setDirty]) + /** the shown grid's: writes the cell being typed in and waits for its last edit (set by the grid as it renders) */ + const flushGrid = useRef<() => Promise>(() => Promise.resolve()) useFileOps({ + // a save or an export from a click (Save, the File menu) does not blur the cell being typed in: it is written first + flush: () => flushGrid.current(), printable: () => sheetPrintable(client, cur, store.sheet(cur), meta, str), text: (format) => client.call('xlsx.text', cur, format === 'tsv' ? '\t' : ','), stats: () => { @@ -564,6 +568,7 @@ export function XlsxView({ client, onStatus, initialSheet }: XlsxViewProps) { edit={edit} setEdit={setEdit} hold={hold} + flush={flushGrid} onSheet={setActive} gotoSheet={gotoSheet} /> @@ -660,6 +665,8 @@ interface GridProps { edit: EditState | null setEdit: React.Dispatch> hold: MutableRefObject + /** set to what writes the cell being typed in and waits for the last edit sent, before the document is saved */ + flush: MutableRefObject<() => Promise> onSheet: (i: number) => void gotoSheet: (id: string) => void } @@ -856,7 +863,7 @@ export function withMerges(r: Range, merges: Merge[]): Range { /** the hidden input field of a grid at rest: inside the view, so that nothing scrolls to it */ const SINK_STYLE: CSSProperties = { left: 4, top: 4, width: 1, height: 1 } -function SheetGrid({ store, sheet, meta, client, edit, setEdit, hold, onSheet, gotoSheet }: GridProps) { +function SheetGrid({ store, sheet, meta, client, edit, setEdit, hold, flush, onSheet, gotoSheet }: GridProps) { const viewer = useViewer() const str = useStrings() const editable = viewer.editable @@ -1509,6 +1516,12 @@ function SheetGrid({ store, sheet, meta, client, edit, setEdit, hold, onSheet, g focusGrid() } + // before a save: the cell being typed in is written (as Enter would) and the edits sent are waited for + flush.current = async () => { + commit() + await lastRun.current + } + /** Ctrl+Enter: the entry goes into every cell of the selection (a formula keeps its relative references) */ const commitFill = () => { const e = edit From ad4fc5453c68e268d48d16f38c62f496feafa8fa Mon Sep 17 00:00:00 2001 From: Lptenoss Date: Sat, 5 Dec 2026 05:30:03 +0700 Subject: [PATCH 3/3] fix(viewer): a click on Save writes what the field with the focus holds, then gives the focus back Since a click on Save keeps the focus where it is, a field that writes as it loses the focus was not written before the save: a sheet's name being typed, a font size typed in the toolbar, the word an IME is still composing. The button now blurs that field first, as when it took the focus, then gives the focus back to it, unless its blur put it elsewhere or it went away with its edit. Co-Authored-By: Claude Opus 5.5 --- e2e/focus-qa3.spec.ts | 44 ++++++++++++++++++++++++++++++++++++- src/react/DocViewer.tsx | 21 +++++++++++++++++- src/react/xlsx/XlsxView.tsx | 2 +- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/e2e/focus-qa3.spec.ts b/e2e/focus-qa3.spec.ts index 1638592a..be9d87fb 100644 --- a/e2e/focus-qa3.spec.ts +++ b/e2e/focus-qa3.spec.ts @@ -1,6 +1,6 @@ /** QA round 3: the keyboard after a dialog, a thumbnail, a sheet tab */ import { describe, expect, test } from 'bun:test' -import { openFile, useBrowser, viewer, xlsxSheet, type Viewer } from './harness' +import { docxParas, openFile, paraText, runsOf, useBrowser, viewer, xlsxSheet, type Viewer } from './harness' useBrowser() @@ -102,6 +102,37 @@ describe('a click on Save leaves the keyboard in the document', () => { expect(v.errors).toEqual([]) }) + test('a document: a word an IME is still composing is in the file saved, and the typing goes on after it', async () => { + const v = await viewer({ src: 'samples/sample.docx', editable: true, onSave: true }) + await v.page.locator('.de-ebar').waitFor() + await v.selectText('Quarterly Report', 'end') + await v.page.keyboard.type(' ab') + await v.page.waitForTimeout(200) + // Telex half way through a word: "việ" is still underlined when Save is clicked + const cdp = await v.page.context().newCDPSession(v.page) + await cdp.send('Input.imeSetComposition', { text: 'việ', selectionStart: 3, selectionEnd: 3 }) + await v.page.waitForTimeout(200) + const lines = (await docxParas((await v.save()).bytes)).map(paraText) + expect(lines).toContain('Quarterly Report abviệ') + expect((await active(v)).cls).toContain('de-pages') + await v.page.keyboard.type('t') + await v.page.waitForTimeout(200) + expect(await v.pagesText()).toContain('Quarterly Report abviệt') + expect(v.errors).toEqual([]) + }) + + test('a document: a font size typed in the toolbar, with no Enter, is in the file saved', async () => { + const v = await viewer({ src: 'samples/sample.docx', editable: true, onSave: true }) + await v.page.locator('.de-ebar').waitFor() + await v.selectText('Quarterly Report') + const size = v.page.locator('.de-size-input') + await size.click() + await size.fill('31') + const runs = (await docxParas((await v.save()).bytes)).flatMap(runsOf) + expect(runs.find((r) => r.text.includes('Quarterly Report'))?.s.sz).toBe(31) + expect(v.errors).toEqual([]) + }) + test('a sheet: the grid keeps the focus, Ctrl/⌘+Z takes the cell back', async () => { const v = await viewer({ src: 'samples/sample.xlsx', editable: true, onSave: true }) await v.page.locator('.de-ebar').waitFor() @@ -135,6 +166,17 @@ describe('a click on Save leaves the keyboard in the document', () => { expect(v.errors).toEqual([]) }) + test('a sheet: a sheet name still being typed is the one saved', async () => { + const v = await viewer({ src: 'samples/sample.xlsx', editable: true, onSave: true }) + await v.page.locator('.de-ebar').waitFor() + await v.page.locator('.de-tab').filter({ hasText: 'Sales' }).dblclick() + await v.page.locator('.de-tab-input').waitFor() + await v.page.keyboard.press('ControlOrMeta+a') + await v.page.keyboard.type('Renamed') + expect((await xlsxSheet((await v.save()).bytes)).names).toEqual(['Renamed', 'Notes', 'Data']) + expect(v.errors).toEqual([]) + }) + test('slides: Ctrl/⌘+Z takes back the text typed in a shape', async () => { const v = await viewer({ src: 'samples/sample.pptx', editable: true, onSave: true }) await v.page.locator('.de-ebar').waitFor() diff --git a/src/react/DocViewer.tsx b/src/react/DocViewer.tsx index 3bed8466..7639b413 100644 --- a/src/react/DocViewer.tsx +++ b/src/react/DocViewer.tsx @@ -519,6 +519,25 @@ function Viewer(props: DocViewerProps) { handle.addEventListener('pointercancel', up) } const rootRef = useRef(null) + /** + * A click on Save leaves the focus in the field it is in. What that field writes as it loses the focus is written + * first, as when the button took the focus: a cell or a sheet's name being typed, a font size, the word an IME is + * composing. Then the focus goes back to the field, unless its blur put it elsewhere (a cell gives it to its grid) + * or the field went away with its edit (a sheet's name). + */ + const saveFromBar = async () => { + const root = rootRef.current + const scope = root?.getRootNode() as Document | ShadowRoot | undefined + const field = scope?.activeElement as HTMLElement | null | undefined + if (root && field && root.contains(field) && isTextEntry(field)) { + field.blur() + // the field's edit renders before the focus goes back + await new Promise((r) => setTimeout(r)) + const now = scope!.activeElement + if ((!now || !root.contains(now)) && field.isConnected) field.focus({ preventScroll: true }) + } + await save() + } // captureSelectAll: Ctrl/⌘+A from the rest of the page too (the viewer's own keys are handled in onKeyDown below) useEffect(() => { const root = rootRef.current @@ -771,7 +790,7 @@ function Viewer(props: DocViewerProps) { className={dirty ? 'de-mode de-save-dirty' : 'de-mode'} aria-disabled={saving} onMouseDown={(e) => e.preventDefault()} - onClick={() => void save()} + onClick={() => void saveFromBar()} title={t.bar.saveTitle} > diff --git a/src/react/xlsx/XlsxView.tsx b/src/react/xlsx/XlsxView.tsx index 79bf3323..b0c785f0 100644 --- a/src/react/xlsx/XlsxView.tsx +++ b/src/react/xlsx/XlsxView.tsx @@ -391,7 +391,7 @@ export function XlsxView({ client, onStatus, initialSheet }: XlsxViewProps) { /** the shown grid's: writes the cell being typed in and waits for its last edit (set by the grid as it renders) */ const flushGrid = useRef<() => Promise>(() => Promise.resolve()) useFileOps({ - // a save or an export from a click (Save, the File menu) does not blur the cell being typed in: it is written first + // a save or an export that does not blur the cell being typed in (the host's own, through the controller, at any time): it is written first flush: () => flushGrid.current(), printable: () => sheetPrintable(client, cur, store.sheet(cur), meta, str), text: (format) => client.call('xlsx.text', cur, format === 'tsv' ? '\t' : ','),