Skip to content
Open
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
13 changes: 13 additions & 0 deletions e2e/app/plain.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>office-engine e2e plain import</title>
</head>
<body>
<script type="module">
import { htmlParas, markdownParas } from '../../src/core/plain.ts'
window.__plain = { htmlParas, markdownParas }
</script>
</body>
</html>
73 changes: 73 additions & 0 deletions e2e/plain.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { describe, expect, test } from 'bun:test'
import { parasToDocx } from '../src/core/convert'
import { BlobSource } from '../src/core/source'
import { ZipArchive } from '../src/core/zip'
import { rebuildZip } from '../src/core/zipwrite'
import { docxParas, useBrowser, viewer } from './harness'

useBrowser()

describe('large plain imports in Chromium', () => {
for (const kind of ['markdown', 'html', 'code'] as const) {
test(`${kind}: imports beyond V8's function-argument limit`, async () => {
const v = await viewer({}, { page: 'plain.html' })
const result = await v.page.evaluate((kind) => {
const { htmlParas, markdownParas } = (window as unknown as { __plain: typeof import('../src/core/plain') }).__plain
const count = 150_000
const ps = kind === 'html'
? htmlParas('<h1>First</h1>' + '<p>paragraph</p>'.repeat(count) + '<h1>Last</h1>', false)
: markdownParas(kind === 'code' ? '```text\n' + 'line\n'.repeat(count) + '```\n\nLast\n' : '# First\n\n' + 'paragraph\n\n'.repeat(count) + '# Last\n', false)
const text = (i: number) => ps[i].runs.map((r) => r.text).join('')
return { count: ps.length, first: kind === 'code' ? text(0).split('line').length - 1 : text(0), last: text(ps.length - 1) }
}, kind)
expect(result).toEqual({ count: kind === 'code' ? 2 : 150_002, first: kind === 'code' ? 150_000 : 'First', last: 'Last' })
expect(v.errors).toEqual([])
})
}
})

test('a document beyond the browser scroll-height limit reaches, finds and edits its last page', async () => {
// Tall pages exercise the same scroll limit as a many-thousand-page Markdown
// import, without making the regression test parse a 50 MB fixture.
const blob = await parasToDocx(Array.from({ length: 200 }, (_, i) => ({ runs: [{ text: i === 199 ? 'END_MARKER' : `Page ${i + 1}` }] })))
const zip = await ZipArchive.open(new BlobSource(blob))
const xml = (await zip.text('word/document.xml'))
.replace(/<w:pgSz[^>]*\/>/, '<w:pgSz w:w="12240" w:h="2000000"/>')
.replaceAll('<w:p>', '<w:p><w:pPr><w:pageBreakBefore/></w:pPr>')
const file = await rebuildZip(zip, new Map([['word/document.xml', xml]]), blob.type)
const v = await viewer({ editable: true, onSave: true })
await v.page.evaluate((bytes) => window.__setProps({ src: new Uint8Array(bytes), name: 'long.docx' }), [...new Uint8Array(await file.arrayBuffer())])
await v.ready('docx')
await v.page.waitForFunction(() => document.querySelector('.de-bar')?.textContent?.includes('of 200'))
const height = await v.page.locator('.de-scroll').evaluate((el) => {
el.scrollTop = el.scrollHeight
return el.scrollHeight
})
expect(height).toBeLessThanOrEqual(12_000_000)
await v.page.locator('.de-page[data-page="200"]').waitFor()
expect(await v.page.locator('.de-page[data-page="200"]').textContent()).toContain('END_MARKER')

await v.btn('Find & replace').click()
const input = v.page.locator('.de-find input[aria-label="Find"]')
await input.fill('END_MARKER')
await input.press('Enter')
await v.page.waitForFunction(() => {
const p = document.querySelector('.de-page[data-page="200"] [data-pa]')?.getBoundingClientRect()
const sc = document.querySelector('.de-scroll')?.getBoundingClientRect()
return p && sc && p.top >= sc.top && p.bottom <= sc.bottom
}, null, { timeout: 5000 }).catch(async (e) => {
const state = await v.page.evaluate(() => {
const s = document.querySelector('.de-scroll')!
const p = document.querySelector('.de-page[data-page="200"] [data-pa]')
return { top: s.scrollTop, height: s.scrollHeight, viewport: s.getBoundingClientRect().toJSON(), paragraph: p?.getBoundingClientRect().toJSON(), status: document.querySelector('.de-bar')?.textContent, matches: document.querySelector('.de-find-count')?.textContent, pages: [...document.querySelectorAll('.de-page')].map(p => ({ page: p.getAttribute('data-page'), top: (p as HTMLElement).style.top })) }
})
throw new Error(`${e}\n${JSON.stringify(state)}`)
})
await input.press('Escape')
await v.selectText('END_MARKER', 'end')
await v.page.keyboard.type('!')
await v.page.waitForFunction(() => document.querySelector('.de-page[data-page="200"]')?.textContent?.includes('END_MARKER!'))
const saved = await docxParas((await v.save()).bytes)
expect(saved.at(-1)?.runs.filter((r) => r.t === 'text').map((r) => r.text).join('')).toBe('END_MARKER!')
expect(v.errors).toEqual([])
})
17 changes: 11 additions & 6 deletions src/core/plain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,18 @@ function diagramParas(lang: DiagramLang, src: string): ImportPara[] {
}
}

/** Append without passing a whole document as function arguments (which can exceed the engine's argument limit). */
function append<T>(target: T[], values: T[]) {
for (const value of values) target.push(value)
}

/** Markdown: the text, and code blocks in a diagram language (```mermaid, ```plantuml, ```dot) as drawings */
export function markdownParas(md: string, dom = true): ImportPara[] {
const lines = md.replace(/^/, '').split(/\r\n|\r|\n/)
const out: ImportPara[] = []
let text: string[] = []
const flush = () => {
if (text.length) out.push(...fromHtml(markdownToHtml(text.join('\n')), dom))
if (text.length) append(out, fromHtml(markdownToHtml(text.join('\n')), dom))
text = []
}
for (let i = 0; i < lines.length; i++) {
Expand All @@ -301,8 +306,8 @@ export function markdownParas(md: string, dom = true): ImportPara[] {
const lang = FENCE_LANG[fence[2].toLowerCase()]
if (lang) {
flush()
out.push(...diagramParas(lang, lines.slice(i + 1, j).join('\n')))
} else text.push(...lines.slice(i, j + 1))
append(out, diagramParas(lang, lines.slice(i + 1, j).join('\n')))
} else append(text, lines.slice(i, j + 1))
i = j
}
flush()
Expand All @@ -319,11 +324,11 @@ export function htmlParas(html: string, dom = true): ImportPara[] {
for (const m of html.matchAll(HTML_DIAGRAM)) {
const lang = FENCE_LANG[(m[2] ?? m[4]).toLowerCase()]
if (!lang) continue
out.push(...fromHtml(html.slice(at, m.index), dom))
out.push(...diagramParas(lang, decodeEntities((m[3] ?? m[5]).replace(/<br\s*\/?>/gi, '\n').replace(/<[^>]+>/g, ''))))
append(out, fromHtml(html.slice(at, m.index), dom))
append(out, diagramParas(lang, decodeEntities((m[3] ?? m[5]).replace(/<br\s*\/?>/gi, '\n').replace(/<[^>]+>/g, ''))))
at = m.index! + m[0].length
}
out.push(...fromHtml(html.slice(at), dom))
append(out, fromHtml(html.slice(at), dom))
return out
}

Expand Down
106 changes: 67 additions & 39 deletions src/react/docx/DocxView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import { pageHF, paginate, type PageSlice } from './paginate'
const PX = 4 / 3
const GAP = 16
const PAD = 24
// Stay below browser element-height limits, as the spreadsheet view does.
const MAX_SCROLL_PX = 12_000_000

const started = new WeakSet<DocumentClient>()

Expand Down Expand Up @@ -210,11 +212,37 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp
}
const skeleton = complete ? 0 : 1
const totalH = acc + skeleton * (pageH * z + GAP) + PAD - GAP
const spacerH = Math.min(totalH, MAX_SCROLL_PX)
const maxLogicalY = Math.max(0, totalH - size.h)
const maxScrollY = Math.max(0, spacerH - size.h)
const scrollScale = maxScrollY > 0 ? Math.max(1, maxLogicalY / maxScrollY) : 1
const scrollY = Math.min(maxLogicalY, scroll.y * scrollScale)
const scrollOffset = scrollY - scroll.y
const scrollToY = (y: number) => {
if (scrollRef.current) scrollRef.current.scrollTop = Math.max(0, Math.min(maxLogicalY, y)) / scrollScale
}
const scrollByY = (delta: number) => {
if (scrollRef.current) scrollRef.current.scrollTop += delta / scrollScale
}
const renderedScroll = useRef(scroll.y)
useIsoLayoutEffect(() => { renderedScroll.current = scroll.y })
// A native scroll changes the viewport before useScroll's frame-batched
// update repositions the pages. Measure a target only after that commit.
const afterScroll = (run: () => void) => {
let frames = 0
const tick = () => {
const sc = scrollRef.current
if (!sc || ++frames > 120) return
if (Math.abs(sc.scrollTop - renderedScroll.current) > 1) requestAnimationFrame(tick)
else run()
}
requestAnimationFrame(tick)
}
const totalW = Math.max(size.w, pageW * z + PAD * 2)
const left = Math.max(PAD, (totalW - pageW * z) / 2)

const first = tops.length ? lowerIndex(tops, scroll.y - pageH * z) : 0
const last = tops.length ? lowerIndex(tops, scroll.y + size.h + 200) : -1
const first = tops.length ? lowerIndex(tops, scrollY - pageH * z) : 0
const last = tops.length ? lowerIndex(tops, scrollY + size.h + 200) : -1
const pageCount = complete ? pages.length : 0
// Both ends of the selection stay mounted, so the DOM selection survives scrolling. A removed page moves its end of
// the range to the pages container, and a page mounted again there lands after that point: outside a range that ends on it.
Expand All @@ -226,7 +254,7 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp
editor.contentW = contentW
editor.defaultFont = meta?.font
editor.defaultSz = meta?.sz
const cur = tops.length ? lowerIndex(tops, scroll.y + size.h / 3) + 1 : 0
const cur = tops.length ? lowerIndex(tops, scrollY + size.h / 3) + 1 : 0
// word count (debounced, off the typing path)
const [words, setWords] = useState(0)
useEffect(() => {
Expand Down Expand Up @@ -337,11 +365,15 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp
if (isEditing) editor.input.repair()
return
}
const bi = Number(sel.f.pa.split('.')[0])
const pi = pages.findIndex((p) => bi >= p.start && bi < p.end)
if (pi >= 0 && (pi < first || pi > last)) {
scrollToY(tops[pi] - 16)
return
}
if (!editor.input.restore(sel)) {
// caret paragraph not rendered (scrolled away): bring its page into view, retry next render
const bi = Number(sel.f.pa.split('.')[0])
const pi = pages.findIndex((p) => bi >= p.start && bi < p.end)
if (pi >= 0 && scrollRef.current) scrollRef.current.scrollTop = Math.max(0, tops[pi] - 16)
if (pi >= 0) scrollToY(tops[pi] - 16)
if (pi < 0 || (pi >= first && pi <= last)) editor.pendingSel = null
return
}
Expand All @@ -353,8 +385,8 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp
if (range && sc) {
const r = range.getBoundingClientRect()
const box = sc.getBoundingClientRect()
if (r.height && r.bottom > box.bottom - 24) sc.scrollTop += r.bottom - box.bottom + 48
else if (r.height && r.top < box.top + 8) sc.scrollTop -= box.top - r.top + 48
if (r.height && r.bottom > box.bottom - 24) scrollByY(r.bottom - box.bottom + 48)
else if (r.height && r.top < box.top + 8) scrollByY(-(box.top - r.top + 48))
}
})

Expand All @@ -380,24 +412,22 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp
const sc = scrollRef.current
if (!m || !sc) return
const pi = pages.findIndex((p) => m.bi >= p.start && m.bi < p.end)
if (pi >= 0) sc.scrollTop = Math.max(0, tops[pi] - 16)
if (pi >= 0) scrollToY(tops[pi] - 16)
setFind((f) => ({ ...f, idx }))
requestAnimationFrame(() =>
requestAnimationFrame(() => {
const root = pagesRef.current
if (!root) return
const a = domFromPos(root, { pa: m.pa, o: m.a })
const b = domFromPos(root, { pa: m.pa, o: m.b })
if (!a || !b) return
const r = document.createRange()
r.setStart(a[0], a[1])
r.setEnd(b[0], b[1])
const rect = r.getBoundingClientRect()
const box = sc.getBoundingClientRect()
sc.scrollTop += rect.top - box.top - box.height / 3
if (isEditing) editor.input.restore({ a: { pa: m.pa, o: m.a }, f: { pa: m.pa, o: m.b } }, false)
}),
)
afterScroll(() => {
const root = pagesRef.current
if (!root) return
const a = domFromPos(root, { pa: m.pa, o: m.a })
const b = domFromPos(root, { pa: m.pa, o: m.b })
if (!a || !b) return
const r = document.createRange()
r.setStart(a[0], a[1])
r.setEnd(b[0], b[1])
const rect = r.getBoundingClientRect()
const box = sc.getBoundingClientRect()
scrollByY(rect.top - box.top - box.height / 3)
if (isEditing) editor.input.restore({ a: { pa: m.pa, o: m.a }, f: { pa: m.pa, o: m.b } }, false)
})
}

// highlight visible matches (CSS Custom Highlight API)
Expand Down Expand Up @@ -703,12 +733,12 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp
if (!el) return false
const r = el.getBoundingClientRect()
const box = sc.getBoundingClientRect()
if (r.top < box.top + 24 || r.bottom > box.bottom - 24) sc.scrollTop += r.top - box.top - box.height / 3
if (r.top < box.top + 24 || r.bottom > box.bottom - 24) scrollByY(r.top - box.top - box.height / 3)
return true
}
if (show()) return
sc.scrollTop = Math.max(0, tops[pi] - 16)
requestAnimationFrame(() => requestAnimationFrame(() => void show()))
scrollToY(tops[pi] - 16)
afterScroll(() => void show())
}
useEffect(() => {
if (!cm) return
Expand Down Expand Up @@ -767,14 +797,12 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp
const sc = scrollRef.current
const pi = pages.findIndex((p) => bi >= p.start && bi < p.end)
if (!sc || pi < 0) return
sc.scrollTop = Math.max(0, tops[pi] - 16)
requestAnimationFrame(() =>
requestAnimationFrame(() => {
const el = pagesRef.current?.querySelector(`[data-pa="${bi}"]`)
if (el) sc.scrollTop += el.getBoundingClientRect().top - sc.getBoundingClientRect().top - 24
if (isEditing) editor.input.restore({ a: { pa: String(bi), o: 0 }, f: { pa: String(bi), o: 0 } })
}),
)
scrollToY(tops[pi] - 16)
afterScroll(() => {
const el = pagesRef.current?.querySelector(`[data-pa="${bi}"]`)
if (el) scrollByY(el.getBoundingClientRect().top - sc.getBoundingClientRect().top - 24)
if (isEditing) editor.input.restore({ a: { pa: String(bi), o: 0 }, f: { pa: String(bi), o: 0 } })
})
}

return (
Expand All @@ -800,7 +828,7 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp
<div
ref={pagesRef}
className={isEditing ? 'de-pages de-editing' : 'de-pages'}
style={{ height: totalH, width: totalW }}
style={{ height: spacerH, width: totalW, overflow: scrollScale > 1 ? 'clip' : undefined }}
contentEditable={isEditing || undefined}
suppressContentEditableWarning
spellCheck={false}
Expand Down Expand Up @@ -842,7 +870,7 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp
blocks={st.blocks}
meta={meta}
page={page}
top={tops[i]}
top={tops[i] - scrollOffset}
left={left}
height={pageHeights[i]}
zoom={z}
Expand All @@ -854,7 +882,7 @@ export function DocxView({ client, onStatus, initialZoom = 'fit' }: DocxViewProp
/>
),
)}
{skeleton > 0 && <div className="de-page-skeleton" contentEditable={false} style={{ top: acc, left, width: pageW * z, height: pageH * z }} />}
{skeleton > 0 && <div className="de-page-skeleton" contentEditable={false} style={{ top: acc - scrollOffset, left, width: pageW * z, height: pageH * z }} />}
</div>
{imgBox && (
<div className="de-img-sel" style={{ left: imgBox.x, top: imgBox.y, width: imgBox.w, height: imgBox.h }}>
Expand Down
32 changes: 31 additions & 1 deletion test/plain.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, test } from 'bun:test'
import type { Block, Paragraph } from '../src/core/docx/types'
import { htmlToParas, parseHtml } from '../src/core/html'
import { decodeText, plainFormat } from '../src/core/plain'
import { decodeText, htmlParas, markdownParas, plainFormat } from '../src/core/plain'
import { svgImage } from '../src/core/svgfile'
import { Engine } from '../src/core/worker/engine'

Expand Down Expand Up @@ -156,6 +156,36 @@ describe('plain sources open as Office files', () => {
})
})

describe('large plain documents', () => {
const count = 150_000
const importedText = (p: { runs: { text: string }[] }) => p.runs.map((r) => r.text).join('')

test('Markdown keeps every paragraph beyond the browser function-argument limit', () => {
const ps = markdownParas('# First\n\n' + 'paragraph\n\n'.repeat(count) + '# Last\n', false)
expect(ps).toHaveLength(count + 2)
expect(ps[0].heading).toBe(1)
expect(importedText(ps[0])).toBe('First')
expect(importedText(ps[1])).toBe('paragraph')
expect(importedText(ps[count])).toBe('paragraph')
expect(importedText(ps[count + 1])).toBe('Last')
})

test('HTML keeps every paragraph beyond the browser function-argument limit', () => {
const ps = htmlParas('<h1>First</h1>' + '<p>paragraph</p>'.repeat(count) + '<h1>Last</h1>', false)
expect(ps).toHaveLength(count + 2)
expect(importedText(ps[0])).toBe('First')
expect(importedText(ps[count])).toBe('paragraph')
expect(importedText(ps[count + 1])).toBe('Last')
})

test('Markdown keeps a fenced code block with more lines than the function-argument limit', () => {
const ps = markdownParas('```text\n' + 'line\n'.repeat(count) + '```\n\nAfter\n', false)
expect(ps).toHaveLength(2)
expect(importedText(ps[0]).split('line')).toHaveLength(count + 1)
expect(importedText(ps[1])).toBe('After')
})
})

describe('plain format detection', () => {
const fmt = (name: string | undefined, type: string | undefined, s: string, whole = true) => plainFormat(name, type, enc(s), whole)

Expand Down