Files
genoffice/apps/docs/tests/table-trailing-paragraph.test.ts
T
merrick-2002andGenOffice d24c964a3e Sync snapshot (2026-09-09) (#271)
- **New app: GenOffice HTML.** Open, edit and generate .html pages: a source pane with a sandboxed live preview, click-to-select elements with a floating toolbar and style panel, device-size preview, image tools and a present mode. AI generates whole pages from a brief with style directions and offers page-wide Restyle, Theme and Summarize. Export to Word runs locally through the built-in html2docx converter (native, editable OOXML; no print-to-PDF detour).
- **AI:** separate BYOK providers for web search, image generation and image/video analysis; new Codex App Server provider; Ask AI quotes the selected text and keeps it highlighted while it runs; custom AI panel text size; blocked requests retry through the Chromium network stack.
- **App:** global AutoSave setting shared by every editor; AI panel text size and chat spellcheck toggles (genoffice#249); Czech (cs) UI locale (genoffice#259); Save As defaults to the document's folder; PDF export no longer aborts on CJK file paths on Windows.
- **Docs:** Export as HTML; comments and replies edit in place from the comments pane; the caret can leave a trailing table (genoffice#266); spellcheck re-marks text as soon as it is re-enabled; AI edits are character-precise on partial selections; zoom keeps the scroll position; pasting a cell from Sheets takes the destination font.
- **Sheets:** Find no longer freezes on wide or streamed grids; Replace searches as you type; cached values of external-workbook formulas are kept instead of #ERROR! (genoffice#235); "number stored as text" is a warning, not an error (genoffice#236); paste repeats into the target range like Excel; filter criteria survive save/reopen; saves retry when Windows locks the file; many Excel-fidelity fixes for column widths, row heights, charts, slicers, number formats and CJK fonts.
- **Slides:** PDF export works for large presentations (genoffice#264); EMF/WMF picture fonts and East Asian theme fonts resolve correctly; decks with missing layouts or embedded styles still open; outline-only pie slices render.
- **PDF:** highlight and underline geometry aligns with the text; Shift+arrows extend the selection instead of paging.
- **Markdown:** Find and replace (Ctrl+F / Ctrl+H) (genoffice#253); mermaid code blocks render as diagrams (genoffice#263); better print quality.
- Thanks to community contributors @GoodScholar, @aniruddhaadak80 and @Sun8854, and to @hanyscz, @qgzln, @zyp997, @bisslot, @krakotay and @Ben03151 for the reports.

Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
2026-09-09 18:19:13 +08:00

106 lines
4.1 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { Editor } from '@tiptap/core'
import { buildBlankDocx, parseDocx } from '@genoffice/docx-engine'
import { insertTableAt } from '../src/renderer/components/ribbon-tabs'
import { blocksToPmDoc } from '../src/renderer/editor/convert'
import { editorExtensions, TABLE_TRAILING_SKIP } from '../src/renderer/editor/extensions'
async function openBlank(content?: unknown) {
const parsed = await parseDocx(await buildBlankDocx())
return new Editor({
element: document.createElement('div'),
extensions: editorExtensions,
content: (content ?? blocksToPmDoc(parsed.blocks)) as never,
})
}
const topLevel = (editor: Editor) => editor.state.doc.content.content.map((n) => n.type.name)
const cellDepth = (editor: Editor) => {
const { $from } = editor.state.selection
for (let d = $from.depth; d > 0; d--) if ($from.node(d).type.name === 'docTableCell') return d
return -1
}
describe('a table is never the last body block (issue #266)', () => {
it('inserting into an empty paragraph keeps that paragraph below the table, caret in the first cell', async () => {
const editor = await openBlank()
insertTableAt(editor, 2, 2)
expect(topLevel(editor)).toEqual(['docTable', 'docParagraph'])
expect(cellDepth(editor)).toBeGreaterThan(0)
expect(editor.state.selection.from).toBe(4)
})
it('inserting at the end of a text paragraph appends a paragraph after the table', async () => {
const editor = await openBlank()
editor.commands.insertContent('hello')
insertTableAt(editor, 1, 1)
expect(topLevel(editor)).toEqual(['docParagraph', 'docTable', 'docParagraph'])
expect(editor.state.doc.firstChild?.textContent).toBe('hello')
})
it('deleting the paragraph below a trailing table restores it', async () => {
const editor = await openBlank()
insertTableAt(editor, 1, 1)
const { doc } = editor.state
const para = doc.lastChild!
editor.view.dispatch(editor.state.tr.delete(doc.content.size - para.nodeSize, doc.content.size))
expect(topLevel(editor)).toEqual(['docTable', 'docParagraph'])
})
it('undo removes the table together with its appended paragraph', async () => {
const editor = await openBlank({
type: 'doc',
content: [{ type: 'docParagraph', content: [{ type: 'text', text: 'hello' }] }],
})
editor.commands.setTextSelection(6)
insertTableAt(editor, 1, 1)
expect(topLevel(editor)).toEqual(['docParagraph', 'docTable', 'docParagraph'])
editor.commands.undo()
expect(topLevel(editor)).toEqual(['docParagraph'])
expect(editor.state.doc.textContent).toBe('hello')
})
it('undo back to a table-last body is left alone and redo still works', async () => {
const table = {
type: 'docTable',
content: [
{
type: 'docTableRow',
content: [{ type: 'docTableCell', content: [{ type: 'docParagraph' }] }],
},
],
}
const editor = await openBlank({ type: 'doc', content: [table] })
editor.commands.insertContentAt(editor.state.doc.content.size, { type: 'docParagraph' })
expect(topLevel(editor)).toEqual(['docTable', 'docParagraph'])
editor.commands.undo()
expect(topLevel(editor)).toEqual(['docTable'])
editor.commands.redo()
expect(topLevel(editor)).toEqual(['docTable', 'docParagraph'])
})
it('load/stream transactions flagged with TABLE_TRAILING_SKIP are left alone', async () => {
const editor = await openBlank()
insertTableAt(editor, 1, 1)
const { doc } = editor.state
const para = doc.lastChild!
const tr = editor.state.tr.delete(doc.content.size - para.nodeSize, doc.content.size)
tr.setMeta(TABLE_TRAILING_SKIP, true)
editor.view.dispatch(tr)
expect(topLevel(editor)).toEqual(['docTable'])
})
it('a nested table inserted inside a cell is followed by a paragraph', async () => {
const editor = await openBlank()
insertTableAt(editor, 1, 1)
insertTableAt(editor, 1, 1)
const cell = editor.state.doc.firstChild!.firstChild!.firstChild!
expect(cell.content.content.map((n) => n.type.name)).toEqual([
'docParagraph',
'docNestedTable',
'docParagraph',
])
})
})