- Docs: Word-style dark page, phased fast open for long documents, fonts embedded in the DOCX, spellcheck toggle, hyperlink editing, and ~80 Word fidelity fixes (CJK/JP metrics, header/footer, tables, anchored pictures) - Sheets: hold formula recalculation while file data streams in, large-workbook scroll/load performance, combo charts, duplicate/unique conditional formatting by display text, RTL frozen-pane fix, workbooks whose ZIP entries carry a leading slash - Slides: MTX-compressed embedded font support, CJK/Latin face selection and PowerPoint script fallbacks, theme colors kept on paragraph defaults and bullets, more reliable AI edits with a layout audit after each batch - PDF: AI page ops, text block move/align, notes and markup editing, watermark and header/footer, form check boxes, metadata; the cloud Export as Word fallback is removed, conversion is always on-device - AI: Gemini tool schema translation, OpenCode Zen/Go providers, markdown tables and code blocks in chat, current date in the system prompt - Shell: Office-style ribbon collapse across all editors, native Windows ARM64 installer, BUILD_DIR override for packaging Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
476 lines
17 KiB
TypeScript
476 lines
17 KiB
TypeScript
/**
|
|
* The per-block --doc-line-factor must follow the live text, not the
|
|
* text the block had when its DOM was first rendered (blockAttrs bakes it into
|
|
* toDOM output, which ProseMirror reuses while typing).
|
|
*/
|
|
import { Editor } from '@tiptap/core'
|
|
import { afterEach, describe, expect, it } from 'vitest'
|
|
import { editorExtensions } from '../src/renderer/editor/extensions'
|
|
import { lineHeightFactor, setDocFontTable } from '../src/renderer/line-metrics'
|
|
|
|
const factorOf = (editor: Editor, index = 0): string => {
|
|
const p = editor.view.dom.querySelectorAll('p')[index] as HTMLElement
|
|
return p.style.getPropertyValue('--doc-line-factor')
|
|
}
|
|
|
|
// jsdom's CSSOM drops the min() font-size, so assert the --doc-strut custom
|
|
// property it references (font-size:min(var(--doc-strut), 1em) rides along)
|
|
const fontSizeOf = (editor: Editor, index = 0): string => {
|
|
const p = editor.view.dom.querySelectorAll('p')[index] as HTMLElement
|
|
return p.style.getPropertyValue('--doc-strut')
|
|
}
|
|
|
|
const sizedParagraph = (text: string, sizeHalfPoints: number) =>
|
|
({
|
|
type: 'doc',
|
|
content: [
|
|
{
|
|
type: 'docParagraph',
|
|
content: [
|
|
{ type: 'text', text, marks: [{ type: 'docTextStyle', attrs: { sizeHalfPoints } }] },
|
|
],
|
|
},
|
|
],
|
|
}) as never
|
|
|
|
describe('live line-height factor decorations', () => {
|
|
it('typing CJK into a paragraph created empty switches its factor to the CJK var', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: { type: 'doc', content: [{ type: 'docParagraph' }] } as never,
|
|
})
|
|
// Created empty: the mark's inherited ascii face sizes the line (Latin var)
|
|
expect(factorOf(editor)).toBe('var(--doc-line-factor-latin,1.2)')
|
|
|
|
editor.commands.insertContentAt(1, '中文正文')
|
|
expect(factorOf(editor)).toBe('var(--doc-line-factor-cjk,1.7)')
|
|
|
|
editor.destroy()
|
|
})
|
|
|
|
it('replacing CJK text with Western text switches back to the Latin factor', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: {
|
|
type: 'doc',
|
|
content: [{ type: 'docParagraph', content: [{ type: 'text', text: '中文' }] }],
|
|
} as never,
|
|
})
|
|
expect(factorOf(editor)).toBe('var(--doc-line-factor-cjk,1.7)')
|
|
|
|
editor.commands.setTextSelection({ from: 1, to: 3 })
|
|
editor.commands.insertContent('latin')
|
|
expect(factorOf(editor)).toBe('var(--doc-line-factor-latin,1.2)')
|
|
|
|
editor.destroy()
|
|
})
|
|
})
|
|
|
|
describe('fontTable-driven per-block factor', () => {
|
|
afterEach(() => setDocFontTable(null))
|
|
|
|
const krParagraph = {
|
|
type: 'doc',
|
|
content: [
|
|
{
|
|
type: 'docParagraph',
|
|
content: [
|
|
{
|
|
type: 'text',
|
|
text: '한국어 본문',
|
|
marks: [{ type: 'docTextStyle', attrs: { font: '원신한 Light' } }],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
} as never
|
|
|
|
it('a missing hangul-named run face with a sans PANOSE bakes the Malgun factor', () => {
|
|
// registry must be set before content, mirroring file-actions' open order
|
|
setDocFontTable([{ name: '원신한 Light', panose: '020B0303000000000000' }])
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: krParagraph,
|
|
})
|
|
expect(factorOf(editor)).toBe('1.7371')
|
|
editor.destroy()
|
|
})
|
|
|
|
it('without a fontTable the same face stays Batang-ward', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: krParagraph,
|
|
})
|
|
expect(factorOf(editor)).toBe('1.3029')
|
|
editor.destroy()
|
|
})
|
|
})
|
|
|
|
describe('live strut font-size decorations', () => {
|
|
it('changing the run font size updates the paragraph strut font-size', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: sizedParagraph('sized', 24),
|
|
})
|
|
expect(fontSizeOf(editor)).toBe('12pt')
|
|
|
|
editor.commands.setTextSelection({ from: 1, to: 6 })
|
|
editor.commands.setMark('docTextStyle', { sizeHalfPoints: 28 })
|
|
expect(fontSizeOf(editor)).toBe('14pt')
|
|
|
|
editor.destroy()
|
|
})
|
|
|
|
it('removes the strut font-size when part of the text loses its explicit size', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: sizedParagraph('sized', 24),
|
|
})
|
|
expect(fontSizeOf(editor)).toBe('12pt')
|
|
|
|
editor.commands.setTextSelection({ from: 1, to: 3 })
|
|
editor.commands.unsetMark('docTextStyle')
|
|
expect(fontSizeOf(editor)).toBe('')
|
|
|
|
editor.destroy()
|
|
})
|
|
|
|
it('deleting the only sized run removes the paragraph strut font-size', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: sizedParagraph('sized', 24),
|
|
})
|
|
expect(fontSizeOf(editor)).toBe('12pt')
|
|
|
|
editor.commands.deleteRange({ from: 1, to: 6 })
|
|
expect(fontSizeOf(editor)).toBe('')
|
|
|
|
editor.destroy()
|
|
})
|
|
})
|
|
|
|
describe('declared run fonts drive the Latin factor and the paragraph face', () => {
|
|
const fontParagraph = (runs: Array<{ text: string; font?: string }>) =>
|
|
({
|
|
type: 'doc',
|
|
content: [
|
|
{
|
|
type: 'docParagraph',
|
|
content: runs.map((r) => ({
|
|
type: 'text',
|
|
text: r.text,
|
|
...(r.font
|
|
? { marks: [{ type: 'docTextStyle', attrs: { font: r.font, fontAscii: r.font } }] }
|
|
: {}),
|
|
})),
|
|
},
|
|
],
|
|
}) as never
|
|
|
|
it('all runs declaring Calibri: run-font factor and the run face on the paragraph', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: fontParagraph([{ text: 'declared', font: 'Calibri' }]),
|
|
})
|
|
expect(factorOf(editor)).toBe('1.22')
|
|
const p = editor.view.dom.querySelector('p') as HTMLElement
|
|
expect(p.style.fontFamily).toContain('Calibri')
|
|
editor.destroy()
|
|
})
|
|
|
|
it('a run inheriting the body font keeps the doc var via max() and no paragraph face', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: fontParagraph([{ text: 'declared ', font: 'Calibri' }, { text: 'inherited' }]),
|
|
})
|
|
expect(factorOf(editor)).toBe('max(var(--doc-line-factor-latin,1.2), 1.22)')
|
|
const p = editor.view.dom.querySelector('p') as HTMLElement
|
|
expect(p.style.fontFamily).toBe('')
|
|
editor.destroy()
|
|
})
|
|
|
|
it('a Latin run declaring only an eastAsia face keeps the doc Latin var', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: {
|
|
type: 'doc',
|
|
content: [
|
|
{
|
|
type: 'docParagraph',
|
|
content: [
|
|
{
|
|
type: 'text',
|
|
text: 'latin only',
|
|
// eastAsia-only rFonts: parse puts the EA name in `font` and
|
|
// leaves fontAscii unset (prod sample 13's bullets)
|
|
marks: [{ type: 'docTextStyle', attrs: { font: 'Arial Unicode MS' } }],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
} as never,
|
|
})
|
|
expect(factorOf(editor)).toBe('var(--doc-line-factor-latin,1.2)')
|
|
editor.destroy()
|
|
})
|
|
|
|
it('CJK runs declaring a JP-variant Noto face take the JA substitution factor', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: fontParagraph([{ text: '日本語本文', font: 'Noto Sans CJK JP' }]),
|
|
})
|
|
expect(factorOf(editor)).toBe('1.3029')
|
|
editor.destroy()
|
|
})
|
|
|
|
it('unsetting the font mark live drops the factor override and the paragraph face', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: fontParagraph([{ text: 'declared', font: 'Calibri' }]),
|
|
})
|
|
expect(factorOf(editor)).toBe('1.22')
|
|
|
|
editor.commands.setTextSelection({ from: 1, to: 9 })
|
|
editor.commands.unsetMark('docTextStyle')
|
|
expect(factorOf(editor)).toBe('var(--doc-line-factor-latin,1.2)')
|
|
const p = editor.view.dom.querySelector('p') as HTMLElement
|
|
expect(p.style.fontFamily).toBe('')
|
|
editor.destroy()
|
|
})
|
|
})
|
|
|
|
describe('SimSun-substitution ・/〜 line lift decorations', () => {
|
|
const gapDoc = (text: string, font: string, attrs?: Record<string, unknown>) =>
|
|
({
|
|
type: 'doc',
|
|
content: [
|
|
{
|
|
type: 'docParagraph',
|
|
...(attrs ? { attrs } : {}),
|
|
content: [{ type: 'text', text, marks: [{ type: 'docTextStyle', attrs: { font } }] }],
|
|
},
|
|
],
|
|
}) as never
|
|
|
|
const liftSpans = (editor: Editor): HTMLElement[] =>
|
|
Array.from(editor.view.dom.querySelectorAll('span')).filter(
|
|
(s) => (s as HTMLElement).style.lineHeight !== '',
|
|
) as HTMLElement[]
|
|
|
|
it('wraps ・ and 〜 in a 1.7143 line-height span under a SimSun-substituted face', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: gapDoc('項目・内容〜まで', 'Noto Sans CJK JP'),
|
|
})
|
|
const spans = liftSpans(editor)
|
|
expect(spans.map((s) => s.textContent)).toEqual(['・', '〜'])
|
|
expect(spans[0].style.lineHeight).toBe(
|
|
'round(up, calc(1.7143 * 1em - var(--doc-grid-pitch,0.0001px) * 0.004), var(--doc-grid-pitch,0.0001px))',
|
|
)
|
|
editor.destroy()
|
|
})
|
|
|
|
it('scales the lift by an explicit line-spacing multiple', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: gapDoc('項目・内容', 'Noto Sans JP', { lineRule: 'auto', lineSpacing: 2 }),
|
|
})
|
|
expect(liftSpans(editor)[0].style.lineHeight).toBe(
|
|
'max(calc(var(--doc-grid-pitch,0.0001px) * 2), calc(round(up, calc(1.7143 * 1em - var(--doc-grid-pitch,0.0001px) * 0.004), var(--doc-grid-pitch,0.0001px)) * var(--doc-grid-single-mult,2)))',
|
|
)
|
|
editor.destroy()
|
|
})
|
|
|
|
it('does not lift under non-SimSun faces, without ・/〜, or with an exact rule', () => {
|
|
for (const content of [
|
|
gapDoc('項目・内容', 'Microsoft YaHei'),
|
|
gapDoc('바탕・본문', 'Batang'),
|
|
gapDoc('項目と内容', 'Noto Sans CJK JP'),
|
|
gapDoc('項目・内容', 'Noto Sans CJK JP', { lineRule: 'exact', lineRawTwips: 240 }),
|
|
]) {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content,
|
|
})
|
|
expect(liftSpans(editor)).toEqual([])
|
|
editor.destroy()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('empty paragraph line size (emptyRunSize attr)', () => {
|
|
it('renders a run-less paragraph at its paragraph-mark size', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: {
|
|
type: 'doc',
|
|
content: [{ type: 'docParagraph', attrs: { emptyRunSize: 2 } }],
|
|
} as never,
|
|
})
|
|
const p = editor.view.dom.querySelector('p') as HTMLElement
|
|
expect(p.style.fontSize).toBe('1pt')
|
|
editor.destroy()
|
|
})
|
|
|
|
it('lays the empty line with the paragraph-mark face metrics (CJK included)', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: {
|
|
type: 'doc',
|
|
content: [
|
|
{ type: 'docParagraph', attrs: { emptyRunFont: 'Times New Roman' } },
|
|
{ type: 'docParagraph', attrs: { emptyRunFont: '맑은 고딕' } },
|
|
{ type: 'docParagraph', attrs: { emptyRunFont: '\u5b8b\u4f53' } },
|
|
],
|
|
} as never,
|
|
})
|
|
const [tnr, malgun, simsun] = Array.from(editor.view.dom.querySelectorAll('p')) as HTMLElement[]
|
|
expect(tnr.style.getPropertyValue('--doc-line-factor')).toBe('1.15')
|
|
expect(tnr.style.fontFamily).toContain('Times New Roman')
|
|
// empty-line probe 2026-08-25: CJK marks size by their own face too
|
|
expect(malgun.style.getPropertyValue('--doc-line-factor')).toBe('1.7371')
|
|
expect(simsun.style.getPropertyValue('--doc-line-factor')).toBe('1.3029')
|
|
editor.destroy()
|
|
})
|
|
|
|
it('falls back to the Latin factor when the mark declares no face', () => {
|
|
const editor = new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: {
|
|
type: 'doc',
|
|
content: [
|
|
{ type: 'docParagraph', attrs: { emptyRunSize: 27 } },
|
|
{ type: 'docParagraph' },
|
|
{ type: 'docParagraph', content: [{ type: 'text', text: '\u4e2d\u6587' }] },
|
|
],
|
|
} as never,
|
|
})
|
|
const [sized, bare, cjk] = Array.from(editor.view.dom.querySelectorAll('p')) as HTMLElement[]
|
|
// sample 028: half-point marks (sz 27) inheriting Times New Roman inflated
|
|
// 1.15 -> 1.30 per empty line and pushed the page-top padding onto p4/p9
|
|
expect(sized.style.fontSize).toBe('13.5pt')
|
|
expect(sized.style.getPropertyValue('--doc-line-factor')).toBe(
|
|
'var(--doc-line-factor-latin,1.2)',
|
|
)
|
|
expect(bare.style.getPropertyValue('--doc-line-factor')).toBe(
|
|
'var(--doc-line-factor-latin,1.2)',
|
|
)
|
|
expect(cjk.style.getPropertyValue('--doc-line-factor')).toBe('var(--doc-line-factor-cjk,1.7)')
|
|
editor.destroy()
|
|
})
|
|
})
|
|
|
|
describe('per-line factors in mixed-script paragraphs', () => {
|
|
const arial = { type: 'docTextStyle', attrs: { font: 'Arial', fontAscii: 'Arial' } }
|
|
const mixedDoc = (attrs: Record<string, unknown> = {}, latinInk = true) =>
|
|
({
|
|
type: 'doc',
|
|
content: [
|
|
{
|
|
type: 'docParagraph',
|
|
attrs,
|
|
content: [
|
|
{ type: 'text', text: latinInk ? '수소는 결정. 결함' : '수소는 결정 결함' },
|
|
{ type: 'text', text: '(Dangling bond)', marks: [arial] },
|
|
{ type: 'text', text: ' 결합해' },
|
|
],
|
|
},
|
|
],
|
|
}) as never
|
|
const open = (content: unknown) =>
|
|
new Editor({
|
|
element: document.createElement('div'),
|
|
extensions: editorExtensions,
|
|
content: content as never,
|
|
})
|
|
const stretches = (editor: Editor) =>
|
|
Array.from(editor.view.dom.querySelectorAll('p span.doc-run-lf')) as HTMLElement[]
|
|
|
|
it('lays the strut at the Latin base and lifts only the Hangul stretches', () => {
|
|
const editor = open(mixedDoc())
|
|
expect(factorOf(editor)).toBe(
|
|
`max(var(--doc-line-factor-latin,1.2), ${lineHeightFactor('Arial')})`,
|
|
)
|
|
const lifted = stretches(editor)
|
|
expect(lifted.map((s) => s.textContent)).toEqual(['수소는 결정', '결함', '결합해'])
|
|
for (const s of lifted) {
|
|
expect(s.style.getPropertyValue('--doc-line-factor')).toBe('var(--doc-line-factor-kr,1.3029)')
|
|
expect(s.style.getPropertyValue('line-height')).toBe('')
|
|
}
|
|
const latin = editor.view.dom.querySelector('p span[data-doc-style]') as HTMLElement
|
|
expect(latin.textContent).toBe('(Dangling bond)')
|
|
expect(latin.querySelector('.doc-run-lf')).toBeNull()
|
|
editor.destroy()
|
|
})
|
|
|
|
it('takes the Latin base from the runs that carry Latin ink only', () => {
|
|
const editor = open(mixedDoc({}, false))
|
|
expect(factorOf(editor)).toBe(String(lineHeightFactor('Arial')))
|
|
editor.destroy()
|
|
})
|
|
|
|
it('keeps the paragraph-level factor for pure-CJK paragraphs', () => {
|
|
const editor = open({
|
|
type: 'doc',
|
|
content: [{ type: 'docParagraph', content: [{ type: 'text', text: '수소는 결정 결함' }] }],
|
|
} as never)
|
|
expect(factorOf(editor)).toBe('var(--doc-line-factor-kr,1.3029)')
|
|
expect(stretches(editor)).toHaveLength(0)
|
|
editor.destroy()
|
|
})
|
|
|
|
it('a stretch in a run with a declared CJK face takes that face', () => {
|
|
const editor = open({
|
|
type: 'doc',
|
|
content: [
|
|
{
|
|
type: 'docParagraph',
|
|
content: [
|
|
{
|
|
type: 'text',
|
|
text: '中文 abc',
|
|
marks: [
|
|
{ type: 'docTextStyle', attrs: { font: 'SimSun', fontAscii: 'Times New Roman' } },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
} as never)
|
|
expect(factorOf(editor)).toBe(String(lineHeightFactor('Times New Roman')))
|
|
const [zh] = stretches(editor)
|
|
expect(zh.textContent).toBe('中文')
|
|
expect(zh.style.getPropertyValue('--doc-line-factor')).toBe(String(lineHeightFactor('SimSun')))
|
|
editor.destroy()
|
|
})
|
|
|
|
it('mirrors an own atLeast rule on the stretch and never lifts exact lines', () => {
|
|
const atLeast = open(mixedDoc({ lineRule: 'atLeast', lineRawTwips: 240 }))
|
|
expect(stretches(atLeast)[0].style.getPropertyValue('line-height')).toBe(
|
|
'max(12.0pt, var(--doc-line-grid, calc(var(--doc-line-factor,1.2) * 1em)))',
|
|
)
|
|
atLeast.destroy()
|
|
const exact = open(mixedDoc({ lineRule: 'exact', lineRawTwips: 240 }))
|
|
expect(stretches(exact)).toHaveLength(0)
|
|
exact.destroy()
|
|
})
|
|
})
|