diff --git a/PRIVACY.md b/PRIVACY.md index e2042c8..0e6d1d4 100644 --- a/PRIVACY.md +++ b/PRIVACY.md @@ -1,6 +1,6 @@ # GenOffice Privacy -Last updated: August 24, 2026 +Last updated: August 26, 2026 GenOffice opens, edits, and saves documents locally. Document editing does not upload files to GenOffice. AI features require a network connection and send @@ -20,6 +20,8 @@ subsequent analytics events. When enabled, the app sends these events: +- `install_first_launch` — marks the first analytics-enabled use of a newly + assigned anonymous `client_id`; used for retention cohorts - `app_launch` — no event-specific parameter - `file_open` — `ext`, the file extension such as `docx` or `xlsx` - `file_new` — `kind`, one of `docx`, `xlsx`, `pptx`, `md`, or `pdf` diff --git a/SECURITY.md b/SECURITY.md index 688e38b..41a386a 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -55,7 +55,9 @@ the same command pipeline as manual edits. JSON-like, prototype-free data. Errors discard all buffered operations; logs are capped. 5. Execution has statement/expression and call-depth limits to bound runaway - loops or recursion. + loops or recursion. Regular expressions run on an interpreter-owned matcher + with its own step budget (a supported subset; no native backtracking), so a + catastrophic pattern cannot stall the renderer past those limits. The Electron renderer sandbox remains defense in depth, but it is not the layout-script security boundary. The interpreter is designed so a layout diff --git a/apps/docs/src/main/docs-main.ts b/apps/docs/src/main/docs-main.ts index 46d3e07..095cf13 100644 --- a/apps/docs/src/main/docs-main.ts +++ b/apps/docs/src/main/docs-main.ts @@ -2662,6 +2662,7 @@ export function registerAiIpc(): void { await streamForProvider(provider, config, system, messages, tools, maxTokens, { signal: controller.signal, onDelta: (text) => send({ requestId, type: 'delta', text }), + onReasoningDelta: (text) => send({ requestId, type: 'reasoning', text }), onToolCall: (toolCall) => send({ requestId, type: 'tool-call', toolCall }), onActivity: ping, onStopReason: (reason) => { diff --git a/apps/docs/src/renderer/App.tsx b/apps/docs/src/renderer/App.tsx index c50456e..163ec90 100644 --- a/apps/docs/src/renderer/App.tsx +++ b/apps/docs/src/renderer/App.tsx @@ -275,10 +275,48 @@ function posFromAnchor(view: Editor['view'], anchor: LineAnchor): number | undef /** Clean pasted Word/web HTML: mso conditional comments, , and unwrapping
  • x

  • */ function cleanPastedHtml(html: string): string { - return html - .replace(//g, '') - .replace(/[\s\S]*?<\/o:p>/g, '') - .replace(/]*)>\s*]*>([\s\S]*?)<\/p>\s*<\/li>/g, '$2') + return unwrapSingleCellTable( + html + .replace(//g, '') + .replace(/[\s\S]*?<\/o:p>/g, '') + .replace(/]*)>\s*]*>([\s\S]*?)<\/p>\s*<\/li>/g, '$2'), + ) +} + +/** + * Word parity: pasting a copy of a SINGLE spreadsheet cell inserts its + * content as text, not a 1x1 table (alpha ledger r146 — a lone cell copied + * from Sheets/Excel pasted into Docs as a table and flipped the ribbon into + * table tools). Only a payload whose entire content is one table with one + * cell unwraps; anything else — multi-cell tables, tables mixed with prose — + * stays intact. + */ +function unwrapSingleCellTable(html: string): string { + if (!/ !table.contains(element)) + ) { + return html + } + const cells = table.querySelectorAll('td,th') + if (cells.length !== 1) return html + if (cells[0]!.querySelector('table')) return html + return cells[0]!.innerHTML + } catch { + return html + } } /** All runs a footnote/endnote reference may live in: paragraph runs, plus table @@ -1537,6 +1575,8 @@ export function App() { // for real-device verification: trigger export directly via CDP (same as __pageDebug) useEffect(() => { ;(window as unknown as Record).__exportPdf = exportPdf + ;(window as unknown as Record).__openPagePreview = () => + setShowPagePreview(true) }, [exportPdf]) useEffect(() => { diff --git a/apps/docs/src/renderer/components/AiAskPopover.tsx b/apps/docs/src/renderer/components/AiAskPopover.tsx index 8a07c87..c877e54 100644 --- a/apps/docs/src/renderer/components/AiAskPopover.tsx +++ b/apps/docs/src/renderer/components/AiAskPopover.tsx @@ -277,6 +277,22 @@ export function AiAskPopover({ } }} > +
    {t(open.mode === 'edit' ? 'aiAskEditTitle' : 'aiAskTitle')}
    diff --git a/apps/docs/src/renderer/components/ContextMenu.tsx b/apps/docs/src/renderer/components/ContextMenu.tsx index 01c7dfb..c7579f6 100644 --- a/apps/docs/src/renderer/components/ContextMenu.tsx +++ b/apps/docs/src/renderer/components/ContextMenu.tsx @@ -185,8 +185,17 @@ export function EditorContextMenu({ isImage || (Array.isArray(protAttrs?.textboxes) && (protAttrs.textboxes as unknown[]).length > 0) const currentWrap = (protAttrs?.imageWrap as string | null) ?? null - const setWrap = (wrap: string | null) => - editor.chain().focus().updateAttributes('docProtected', { imageWrap: wrap }).run() + const setWrap = (wrap: string | null) => { + const clearedPosition = + wrap === null + ? { imagePosH: null, imagePosV: null, imageOffsetXEmu: null, imageOffsetYEmu: null } + : {} + editor + .chain() + .focus() + .updateAttributes('docProtected', { imageWrap: wrap, ...clearedPosition }) + .run() + } // Stacking order among overlapping floating pictures. z-order only has a // visible effect on floating (front/behind) images, so the menu enables it // there; a bring-forward on an inline image also floats it (Word parity). diff --git a/apps/docs/src/renderer/components/HeaderFooterArea.tsx b/apps/docs/src/renderer/components/HeaderFooterArea.tsx index 59af94a..8ac35cf 100644 --- a/apps/docs/src/renderer/components/HeaderFooterArea.tsx +++ b/apps/docs/src/renderer/components/HeaderFooterArea.tsx @@ -26,7 +26,9 @@ export interface HfValue { function runStyle(run: Run): React.CSSProperties { const style: React.CSSProperties = {} if (run.bold) style.fontWeight = 600 + else if (run.bold === false) style.fontWeight = 'normal' if (run.italic) style.fontStyle = 'italic' + else if (run.italic === false) style.fontStyle = 'normal' if (run.underline) style.textDecoration = 'underline' if (run.strike) style.textDecoration = `${style.textDecoration ?? ''} line-through`.trim() if (run.color) style.color = `#${run.color}` @@ -36,6 +38,10 @@ function runStyle(run: Run): React.CSSProperties { if (run.font || run.fontAscii) style.fontFamily = cssRunFontFamily(run.fontAscii, run.font) if (run.caps === 'all') style.textTransform = 'uppercase' else if (run.caps === 'small') style.fontVariantCaps = 'small-caps' + else if (run.caps === 'none') { + style.textTransform = 'none' + style.fontVariantCaps = 'normal' + } return style } diff --git a/apps/docs/src/renderer/components/PaginationPreview.tsx b/apps/docs/src/renderer/components/PaginationPreview.tsx index fd7703e..0c8452c 100644 --- a/apps/docs/src/renderer/components/PaginationPreview.tsx +++ b/apps/docs/src/renderer/components/PaginationPreview.tsx @@ -17,6 +17,7 @@ import { liveSections, measureBlocks, pageNumbers, + pinnedFloatPage, sectionBidi, sectionColGeom, sectionFirstPages, @@ -111,6 +112,25 @@ export function prunedCloneHtml(kids: CloneChild[], from: number, to: number): s return parts.join('') } +/** + * Page-pinned boxes (cover art) ride the full-document clone onto every page at + * the same page coordinates; only the copy on the owning page (data-pin-page, + * stamped on the canvas wrapper before cloning) may stay visible. Expressed as + * CSS rules because React re-assigns each clone's innerHTML on every re-render + * (the {__html} wrapper is a fresh object), wiping any imperative DOM fixup. + * visibility (not display): a stray-run wrapper carries flow height the slices + * were measured with, and hidden ink must not emit glyphs into the PDF layer. + */ +export function pinnedCloneCss(pageCount: number): string { + const rules: string[] = [] + for (let i = 0; i < pageCount; i++) { + rules.push( + `.pv-page[data-pv-page="${i}"] .doc-protected-pagepinned[data-pin-page]:not([data-pin-page="${i}"]){visibility:hidden;}`, + ) + } + return rules.join('\n') +} + export interface HfSet { header: HeaderFooter | null footer: HeaderFooter | null @@ -312,6 +332,13 @@ export function PaginationPreview({ blockMetaOf, ) } + // stamp each page-pinned box's owning page on its canvas wrapper before + // cloning so the per-page CSS rules can hide the copies on other pages + for (const f of floats) { + if (!f.pinned) continue + const wrap = f.el.closest('.doc-protected-pagepinned') + if (wrap) wrap.dataset.pinPage = String(pinnedFloatPage(computed, f.anchorTop)) + } setSlices(computed) setPageNotes(pageFootnotesOf ? pageFootnotesOf(blocks, computed) : []) // Per-page full clones explode on large documents (pages × doc DOM → @@ -467,6 +494,7 @@ export function PaginationPreview({ {t('appClose')} +
    {slices.map((slice, i) => { const parts = hfFor(i) @@ -498,6 +526,7 @@ export function PaginationPreview({
    + +const TABLE_AUTO_FIT_OPTIONS: Array<[TableAutoFitMode, StringKey]> = [ + ['contents', 'ribbonAutoFitContents'], + ['window', 'ribbonAutoFitWindow'], + ['fixed', 'ribbonFixedColumnWidth'], +] + // tab values double as internal-state / external tabRequest keys; translated for display via these string keys const TAB_LABEL_KEYS: Record = { file: 'ribbonTabFile', @@ -655,6 +718,7 @@ function RibbonInner({ /** Picture Format → remove background / crop dialogs */ const [pictureDialog, setPictureDialog] = useState<'cutout' | 'crop' | null>(null) const [listDialog, setListDialog] = useState(false) + const [tablePropertiesOpen, setTablePropertiesOpen] = useState(false) useEffect(() => { if (tabRequest && (TABS as readonly string[]).includes(tabRequest.tab)) { @@ -1053,6 +1117,87 @@ function RibbonInner({ vAlign: fs.cellVAlign, } + const tableAttrs = inTable ? editor.getAttributes('docTable') : {} + const tableHeader = inTable ? repeatHeaderState(editor.state) : { enabled: false, active: false } + const tableLook = (tableAttrs.tblLook as TableLook | null) ?? { + firstRow: true, + lastRow: false, + firstColumn: true, + lastColumn: false, + bandedRows: true, + bandedColumns: false, + } + const tableAutoFitMode: TableAutoFitMode = + tableAttrs.tblAutoFit === 'contents' || tableAttrs.tblAutoFit === 'window' + ? tableAttrs.tblAutoFit + : 'fixed' + const tableAutoFitLabel = + TABLE_AUTO_FIT_OPTIONS.find(([mode]) => mode === tableAutoFitMode)?.[1] ?? + 'ribbonFixedColumnWidth' + + const applyTableProperties = (value: TablePropertiesValue) => { + if (!canEdit || !inTable) return + let measuredWidthPx = Number(tableAttrs.widthPx) || 0 + if (!(measuredWidthPx > 0)) { + try { + const rect = selectedRect(editor.state) + const tableDom = editor.view.nodeDOM(rect.tableStart - 1) + if (tableDom instanceof HTMLElement) { + const zoomEl = document.querySelector('.doc-zoom') as HTMLElement | null + const zoom = zoomEl ? parseFloat(getComputedStyle(zoomEl).zoom || '1') || 1 : 1 + measuredWidthPx = tableDom.getBoundingClientRect().width / zoom + } + } catch { + // A malformed table falls back to the section width below. + } + } + measuredWidthPx = Math.max(1, Math.round(measuredWidthPx || sectionContentWidthPx)) + const positionedWidthPx = + value.autoFit === 'window' ? Math.round(sectionContentWidthPx) : measuredWidthPx + runTableCommand(setTableAutoFit(value.autoFit, sectionContentWidthPx)) + const twips = (cm: number) => Math.max(0, Math.round(cm * TWIPS_PER_CM)) + const signedTwips = (cm: number) => Math.round(cm * TWIPS_PER_CM) + const floatX = + value.wrap === 'right' && Math.abs(value.positionXCm) < 0.001 + ? value.autoFit === 'window' + ? 0 + : Math.max(0, Math.round((sectionContentWidthPx - positionedWidthPx) * 15)) + : signedTwips(value.positionXCm) + const keepFloatSuppressed = value.floatSuppressed && value.wrap !== 'none' + runTableCommand( + updateSelectedTableAttrs({ + tblFloatWidthPx: value.wrap === 'right' ? positionedWidthPx : null, + cellMar: { + top: twips(value.marginTopCm), + right: twips(value.marginRightCm), + bottom: twips(value.marginBottomCm), + left: twips(value.marginLeftCm), + }, + cellMarEdited: true, + tblFloat: keepFloatSuppressed ? null : value.wrap, + tblFloatSource: value.wrap, + tblFloatSuppressed: keepFloatSuppressed, + tblFloatXTwips: value.wrap === 'none' ? null : floatX, + tblFloatYTwips: value.wrap === 'none' ? null : signedTwips(value.positionYCm), + tblFloatHorzAnchor: + value.wrap === 'none' ? null : (tableAttrs.tblFloatHorzAnchor ?? 'margin'), + tblFloatVertAnchor: + value.wrap === 'none' ? null : (tableAttrs.tblFloatVertAnchor ?? 'margin'), + tblFloatDistance: + value.wrap === 'none' + ? null + : { + top: twips(value.distanceCm), + right: twips(value.distanceCm), + bottom: twips(value.distanceCm), + left: twips(value.distanceCm), + }, + tblFloatEdited: true, + }), + ) + setTablePropertiesOpen(false) + } + const activeCharStyleId = fs.charStyleId const presetAccent = themeColors?.accent1?.trim().toUpperCase() || DEFAULT_PRESET_ACCENT @@ -2184,13 +2329,34 @@ function RibbonInner({ {t('ribbonNoStyle')} + {TABLE_PRESETS.map((preset) => ( + + ))} {[...(styles?.values() ?? [])] .filter((info) => info.type === 'table' && info.styleId !== 'TableNormal') - .slice(0, 8) .map((info) => (
    +
    +
    + {( + [ + ['firstRow', 'ribbonTableFirstRow'], + ['lastRow', 'ribbonTableLastRow'], + ['bandedRows', 'ribbonTableBandedRows'], + ['firstColumn', 'ribbonTableFirstColumn'], + ['lastColumn', 'ribbonTableLastColumn'], + ['bandedColumns', 'ribbonTableBandedColumns'], + ] as Array<[keyof TableLook, StringKey]> + ).map(([key, label]) => ( + + ))} +
    +
    {t('ribbonTableStyleOptions')}
    +
    +
    {['FFFFFF', 'D9EAF7', 'FFF2CC', 'E2F0D9', 'FCE4D6', 'E4DFEC'].map((hex) => ( @@ -2468,6 +2664,76 @@ function RibbonInner({
    {t('ribbonGroupCellSize')}
    +
    +
    +
    +
    + + {dropdown === 'tableAutoFit' && ( +
    + {TABLE_AUTO_FIT_OPTIONS.map(([mode, label]) => ( + + ))} +
    + )} +
    +
    +
    {t('ribbonAutoFit')}
    +
    +
    +
    +
    + + +
    +
    {t('ribbonTableData')}
    +
    ) : tab === 'home' ? ( <> @@ -3538,6 +3804,13 @@ function RibbonInner({ onClose={() => setListDialog(false)} /> )} + {tablePropertiesOpen && ( + setTablePropertiesOpen(false)} + /> + )}
    ) } @@ -3570,6 +3843,222 @@ const LIST_FMT_SAMPLES: Record = { const TWIPS_PER_CM = 567 +type TableWrapMode = 'none' | 'left' | 'right' + +interface TablePropertiesValue { + autoFit: TableAutoFitMode + wrap: TableWrapMode + floatSuppressed: boolean + positionXCm: number + positionYCm: number + distanceCm: number + marginTopCm: number + marginRightCm: number + marginBottomCm: number + marginLeftCm: number +} + +function tablePropertiesFromAttrs(attrs: Record): TablePropertiesValue { + const cm = (value: unknown, fallback = 0) => + +((Number.isFinite(Number(value)) ? Number(value) : fallback) / TWIPS_PER_CM).toFixed(2) + const margins = (attrs.cellMar as Record | null) ?? {} + const distance = (attrs.tblFloatDistance as Record | null) ?? {} + const wrap: TableWrapMode = + attrs.tblFloat === 'left' || attrs.tblFloat === 'right' + ? attrs.tblFloat + : attrs.tblFloatSource === 'left' || attrs.tblFloatSource === 'right' + ? attrs.tblFloatSource + : 'none' + return { + autoFit: + attrs.tblAutoFit === 'contents' || attrs.tblAutoFit === 'window' ? attrs.tblAutoFit : 'fixed', + wrap, + floatSuppressed: attrs.tblFloatSuppressed === true, + positionXCm: cm(attrs.tblFloatXTwips), + positionYCm: cm(attrs.tblFloatYTwips), + distanceCm: cm(distance.right ?? distance.left ?? distance.top ?? distance.bottom, 180), + marginTopCm: cm(margins.top), + marginRightCm: cm(margins.right, 108), + marginBottomCm: cm(margins.bottom), + marginLeftCm: cm(margins.left, 108), + } +} + +function TablePropertiesDialog({ + initial, + onApply, + onClose, +}: { + initial: TablePropertiesValue + onApply: (value: TablePropertiesValue) => void + onClose: () => void +}) { + const { t } = useI18n() + const [value, setValue] = useState(initial) + const update = (patch: Partial) => + setValue((current) => ({ ...current, ...patch })) + const numberInput = ( + key: + | 'positionXCm' + | 'positionYCm' + | 'distanceCm' + | 'marginTopCm' + | 'marginRightCm' + | 'marginBottomCm' + | 'marginLeftCm', + ) => ( + update({ [key]: Number(event.target.value) || 0 })} + /> + ) + const wrapLabel: StringKey = + value.wrap === 'left' + ? 'appWrapSquareLeft' + : value.wrap === 'right' + ? 'appWrapSquareRight' + : 'appWrapInline' + const autoFitLabel = + TABLE_AUTO_FIT_OPTIONS.find(([mode]) => mode === value.autoFit)?.[1] ?? 'ribbonFixedColumnWidth' + + return ( +
    e.target === e.currentTarget && onClose()}> +
    +
    + +
    +

    {t('ribbonTableProperties')}

    +

    + {t(autoFitLabel)} · {t(wrapLabel)} +

    +
    +
    + +
    +

    {t('ribbonAutoFit')}

    +
    + {TABLE_AUTO_FIT_OPTIONS.map(([mode, label]) => ( + + ))} +
    +
    + +
    +

    {t('ribbonWrapText')}

    +
    + {( + [ + ['none', 'appWrapInline'], + ['left', 'appWrapSquareLeft'], + ['right', 'appWrapSquareRight'], + ] as Array<[TableWrapMode, StringKey]> + ).map(([mode, label]) => ( + + ))} +
    + {value.wrap !== 'none' && ( +
    + + + +
    + )} +
    + +
    +

    {t('ribbonCellMargins')}

    +
    +
    + + + + +
    + +
    +
    + +
    + + +
    +
    +
    + ) +} + function ListDefineDialog({ onApply, onClose, diff --git a/apps/docs/src/renderer/components/icons.tsx b/apps/docs/src/renderer/components/icons.tsx index e7563ed..30d33b1 100644 --- a/apps/docs/src/renderer/components/icons.tsx +++ b/apps/docs/src/renderer/components/icons.tsx @@ -564,6 +564,42 @@ export function IconTableDelete(props: IconProps) { ) } +export function IconAutoFit(props: IconProps) { + return ( + + + + + + + ) +} + +export function IconRepeatHeader(props: IconProps) { + return ( + + + + + + + + ) +} + +export function IconTableProperties(props: IconProps) { + return ( + + + + + + + + + ) +} + export function IconRowInsertAbove(props: IconProps) { return ( diff --git a/apps/docs/src/renderer/doc-style-css.ts b/apps/docs/src/renderer/doc-style-css.ts index 0e92943..c88599a 100644 --- a/apps/docs/src/renderer/doc-style-css.ts +++ b/apps/docs/src/renderer/doc-style-css.ts @@ -151,17 +151,19 @@ export function docStyleCss(parsed: ParsedDocFull): string { // re-resolve the snap with their 1em (exact/atLeast lines never snap) rules.push(`${gridBlocks} span { ${gridSpanSnap} }`) } - // settings.xml w:autoHyphenation: Word breaks words at line ends document-wide. - // Chromium hyphenates only under an explicit lang; file-actions sets it on the - // editor root from docDefaults w:lang. - if (parsed.autoHyphenation) { - rules.push('.doc-page { hyphens:auto; -webkit-hyphens:auto }') - } const dd = parsed.docDefaults // Word applies the w:default="1" paragraph style (Normal) to every paragraph // without a w:pStyle, so its display merges into the document baseline here // ([data-style] rules only reach explicitly styled paragraphs). const normal = defaultParaDisplay(parsed) + // settings.xml w:autoHyphenation: Word breaks words at line ends document-wide, + // except paragraphs opted out via w:suppressAutoHyphens (pPrDefault/Normal decide + // the baseline here; explicit style values override per style below). Chromium + // hyphenates only under an explicit lang; file-actions sets it on the editor root + // from docDefaults w:lang. + if (parsed.autoHyphenation && !(normal?.suppressAutoHyphens ?? dd?.suppressAutoHyphens)) { + rules.push('.doc-page { hyphens:auto; -webkit-hyphens:auto }') + } { const decls: string[] = [] // Paragraph level also overrides this variable per paragraph's text (blockAttrs @@ -332,8 +334,11 @@ export function docStyleCss(parsed: ParsedDocFull): string { const decls: string[] = [] if (d.sizeHalfPoints) decls.push(`font-size:${d.sizeHalfPoints / 2}pt`) if (d.color) decls.push(`color:#${d.color}`) + // explicit off (w:val="0") must out-rule an inherited on from .doc-page defaults if (d.bold) decls.push('font-weight:600') + else if (d.bold === false) decls.push('font-weight:400') if (d.italic) decls.push('font-style:italic') + else if (d.italic === false) decls.push('font-style:normal') if (d.underline || d.strike) { decls.push( `text-decoration:${[d.underline && 'underline', d.strike && 'line-through'].filter(Boolean).join(' ')}`, @@ -365,6 +370,7 @@ export function docStyleCss(parsed: ParsedDocFull): string { if (d.charSpacingTwips) decls.push(`letter-spacing:${d.charSpacingTwips / 20}pt`) if (d.caps === 'all') decls.push('text-transform:uppercase') else if (d.caps === 'small') decls.push('font-variant-caps:small-caps') + else if (d.caps === 'none') decls.push('text-transform:none', 'font-variant-caps:normal') const styleLh = cssLineHeight(d.lineRule, d.lineRawTwips, d.lineSpacing) if (styleLh) decls.push(`line-height:${styleLh}`) // grid span snapping scales by the style's multiple (an explicit single @@ -406,6 +412,14 @@ export function docStyleCss(parsed: ParsedDocFull): string { if (d.indentFirstLineTwips) decls.push(`text-indent:${(d.indentFirstLineTwips / 20).toFixed(1)}pt`) if (d.align) decls.push(`text-align:${d.align}`) + if ( + parsed.autoHyphenation && + info.type === 'paragraph' && + d.suppressAutoHyphens !== undefined + ) { + const h = d.suppressAutoHyphens ? 'manual' : 'auto' + decls.push(`hyphens:${h}`, `-webkit-hyphens:${h}`) + } // style-level paragraph shading (explicit pPr w:shd is inline style and wins) if (d.shadingFill) decls.push(`background-color:#${d.shadingFill}`) // the static sheet guesses italic for h4-h6 (Word's built-in defaults); diff --git a/apps/docs/src/renderer/editor/convert.ts b/apps/docs/src/renderer/editor/convert.ts index 8b9cfcc..cfcd341 100644 --- a/apps/docs/src/renderer/editor/convert.ts +++ b/apps/docs/src/renderer/editor/convert.ts @@ -540,11 +540,23 @@ function blockToPmNode( imageHeightPx: block.imageHeightPx ?? null, imageCrop: block.imageCrop ?? null, imageFillRect: block.imageFillRect ?? null, + imageLeadingText: block.imageLeadingText ?? null, + imageLeadingFont: block.imageLeadingFont ?? null, + imageLeadingExplicitSpaceWidthPx: block.imageLeadingExplicitSpaceWidthPx ?? null, + imageLeadingImplicitSpaceCount: block.imageLeadingImplicitSpaceCount ?? null, + imageParagraphIndentLeft: block.imageParagraphIndentLeft ?? null, + imageParagraphIndentRight: block.imageParagraphIndentRight ?? null, + imageParagraphIndentFirstLine: block.imageParagraphIndentFirstLine ?? null, imageAlign: block.imageAlign ?? null, imageWrap: block.imageWrap ?? null, + imageWrapDistTopEmu: block.imageWrapDistTopEmu ?? null, + imageWrapDistBottomEmu: block.imageWrapDistBottomEmu ?? null, + imageWrapDistLeftEmu: block.imageWrapDistLeftEmu ?? null, + imageWrapDistRightEmu: block.imageWrapDistRightEmu ?? null, imageZOrder: block.imageZOrder ?? null, imageOffsetXEmu: block.imageOffsetXEmu ?? null, imageOffsetYEmu: block.imageOffsetYEmu ?? null, + imageAnchorLocked: block.imageAnchorLocked ?? false, imagePosH: block.imagePosH ?? null, imagePosV: block.imagePosV ?? null, imageRotDeg: block.imageRotDeg ?? null, @@ -648,7 +660,9 @@ export function tableModelToPmNode( (sum, _row, i) => sum + Math.max(model.rowHeightsTwips?.[i] ?? 0, 240), 0, ) - const tblFloat = minHeightTwips > 12960 ? null : (model.floatSide ?? null) + const tblFloatSource = model.floatSide ?? null + const tblFloatSuppressed = minHeightTwips > 12960 && tblFloatSource !== null + const tblFloat = tblFloatSuppressed ? null : tblFloatSource const table: PmNode = { type: 'docTable', attrs: { @@ -661,11 +675,28 @@ export function tableModelToPmNode( : null, widthPct: model.widthPct ?? null, cellMar: model.cellMarTwips ?? null, + cellSpacingTwips: model.cellSpacingTwips ?? null, + tblFill: model.fill ?? null, + cellMarEdited: false, borders: model.borders ?? null, tblAlign: model.align ?? null, tblFloat, + tblFloatSource, + tblFloatSuppressed, + tblFloatXTwips: model.floatPos?.xTwips ?? null, + tblFloatYTwips: model.floatPos?.yTwips ?? null, + tblFloatHorzAnchor: model.floatPos?.horzAnchor ?? null, + tblFloatVertAnchor: model.floatPos?.vertAnchor ?? null, + tblFloatDistance: model.floatPos?.distanceTwips ?? null, + tblFloatWidthPx: + tblFloatSource && widthPx ? widthPx.reduce((sum, width) => sum + width, 0) : null, + tblFloatEdited: false, + tblAutoFit: model.autoFit ?? (model.autoLayout ? 'contents' : 'fixed'), + tblAutoFitEdited: false, indentTwips: model.indentTwips ?? null, tblStyleId: model.tblStyleId ?? null, + tblLook: model.tableLook ?? null, + tblLookEdited: false, bidiVisual: model.bidiVisual ?? false, originalStructure: null, originalFormatting: null, @@ -675,6 +706,8 @@ export function tableModelToPmNode( attrs: { heightTwips: model.rowHeightsTwips?.[rowIndex] ?? null, heightRule: model.rowHeightRules?.[rowIndex] ?? null, + repeatHeader: model.repeatHeaderRows?.[rowIndex] ?? false, + repeatHeaderEdited: false, rawTrPr: model.rawTrPrs?.[rowIndex] ?? null, rowRevision: model.rowRevisions?.[rowIndex] ?? null, }, @@ -691,6 +724,7 @@ export function tableModelToPmNode( rowspan, clipHeightTwips: cellClipTwips(model, rowIndex, cell, rowspan), colwidth: widthPx ? widthPx.slice(start, start + colspan) : null, + gridGap: cell.gridGap ?? false, cellMar: cell.cellMarTwips ?? null, textDirection: cell.textDirection ?? null, fill: cell.fill ?? null, @@ -777,10 +811,27 @@ function cellContentNodes(cell: TableCell): PmNode[] { export function tableStructureSignature(table: PmNode): string { return JSON.stringify({ widths: table.attrs?.colWidthsPct ?? null, + widthPx: table.attrs?.widthPx ?? null, + widthPct: table.attrs?.widthPct ?? null, + autoFit: table.attrs?.tblAutoFit ?? null, + autoFitEdited: table.attrs?.tblAutoFitEdited ?? false, + cellMar: table.attrs?.cellMar ?? null, + cellMarEdited: table.attrs?.cellMarEdited ?? false, tblStyle: table.attrs?.tblStyleId ?? null, tblAlign: table.attrs?.tblAlign ?? null, + tblFloat: table.attrs?.tblFloat ?? null, + tblFloatXTwips: table.attrs?.tblFloatXTwips ?? null, + tblFloatYTwips: table.attrs?.tblFloatYTwips ?? null, + tblFloatHorzAnchor: table.attrs?.tblFloatHorzAnchor ?? null, + tblFloatVertAnchor: table.attrs?.tblFloatVertAnchor ?? null, + tblFloatDistance: table.attrs?.tblFloatDistance ?? null, + tblFloatEdited: table.attrs?.tblFloatEdited ?? false, + tblLook: table.attrs?.tblLook ?? null, + tblLookEdited: table.attrs?.tblLookEdited ?? false, rows: (table.content ?? []).map((row) => [ row.attrs?.heightTwips ?? null, + row.attrs?.repeatHeader ?? false, + row.attrs?.repeatHeaderEdited ?? false, // accepting/rejecting revisions strips records from trPr/tcPr: include them in the signature to trigger regeneration (works for empty rows too) row.attrs?.rawTrPr ?? null, row.attrs?.rowRevision ?? null, @@ -855,6 +906,7 @@ export function pmTableToModel(table: PmNode): TableModel { const rowHeightsTwips: Array = [] const rowHeightRules: NonNullable = [] + const repeatHeaderRows: Array = [] const rawTrPrs: Array = [] const rowRevisions: TableModel['rowRevisions'] = [] for (const rowNode of table.content ?? []) { @@ -862,6 +914,11 @@ export function pmTableToModel(table: PmNode): TableModel { rowHeightRules.push( (rowNode.attrs?.heightRule as NonNullable[number]) ?? null, ) + repeatHeaderRows.push( + rowNode.attrs?.repeatHeaderEdited || table.attrs?.docxIndex == null + ? !!rowNode.attrs?.repeatHeader + : null, + ) rawTrPrs.push((rowNode.attrs?.rawTrPr as string | null) ?? null) rowRevisions.push( (rowNode.attrs?.rowRevision as NonNullable[number]) ?? null, @@ -895,6 +952,7 @@ export function pmTableToModel(table: PmNode): TableModel { vAlign: (cellNode.attrs?.vAlign as TableCell['vAlign'] | null) ?? undefined, borders: (cellNode.attrs?.borders as TableCell['borders'] | null) ?? undefined, rawTcPr: (cellNode.attrs?.rawTcPr as string | null) ?? undefined, + gridGap: cellNode.attrs?.gridGap ? true : undefined, } const cellParas = (cellNode.content ?? []).filter( (n) => n.type === 'docParagraph' || n.type === 'docListItem', @@ -972,11 +1030,56 @@ export function pmTableToModel(table: PmNode): TableModel { colWidthsPct = Array.from({ length: maximumColumns }, () => 100 / maximumColumns) } const tblStyleAttr = table.attrs?.tblStyleId as string | null | undefined + const isNew = table.attrs?.docxIndex == null + const tablePropsEdited = (name: string): boolean => isNew || table.attrs?.[name] === true + const visibleFloat = + table.attrs?.tblFloat === 'left' || table.attrs?.tblFloat === 'right' + ? (table.attrs.tblFloat as 'left' | 'right') + : null + const suppressedFloat = + table.attrs?.tblFloatSuppressed && + (table.attrs?.tblFloatSource === 'left' || table.attrs?.tblFloatSource === 'right') + ? (table.attrs.tblFloatSource as 'left' | 'right') + : null + const floatSide = visibleFloat ?? suppressedFloat + const floatPosition = + floatSide && (table.attrs?.tblFloatXTwips != null || table.attrs?.tblFloatYTwips != null) + ? { + xTwips: Number(table.attrs?.tblFloatXTwips) || 0, + yTwips: Number(table.attrs?.tblFloatYTwips) || 0, + ...(table.attrs?.tblFloatHorzAnchor + ? { + horzAnchor: table.attrs.tblFloatHorzAnchor as NonNullable< + TableModel['floatPos'] + >['horzAnchor'], + } + : {}), + ...(table.attrs?.tblFloatVertAnchor + ? { + vertAnchor: table.attrs.tblFloatVertAnchor as NonNullable< + TableModel['floatPos'] + >['vertAnchor'], + } + : {}), + ...(table.attrs?.tblFloatDistance + ? { + distanceTwips: table.attrs.tblFloatDistance as NonNullable< + TableModel['floatPos'] + >['distanceTwips'], + } + : {}), + } + : undefined return { rows, ...(colWidthsPct ? { colWidthsPct } : {}), ...(colWidthsTwips ? { colWidthsTwips } : {}), + ...(table.attrs?.cellSpacingTwips + ? { cellSpacingTwips: Number(table.attrs.cellSpacingTwips) } + : {}), + ...(table.attrs?.tblFill ? { fill: String(table.attrs.tblFill) } : {}), ...(rowHeightsTwips.some((h) => h !== null) ? { rowHeightsTwips, rowHeightRules } : {}), + ...(repeatHeaderRows.some((value) => value !== null) ? { repeatHeaderRows } : {}), ...(rawTrPrs.some((r) => r !== null) ? { rawTrPrs } : {}), ...(rowRevisions.some((revision) => revision !== null) ? { rowRevisions } : {}), // null (cleared) → '' removes explicitly; undefined leaves it alone @@ -984,6 +1087,18 @@ export function pmTableToModel(table: PmNode): TableModel { // explicit only when set ('left' = remove w:jc); null leaves the original // tblPr untouched so unmapped w:jc values (e.g. 'start') survive rebuilds ...(table.attrs?.tblAlign ? { align: table.attrs.tblAlign as TableModel['align'] } : {}), + ...(tablePropsEdited('tblAutoFitEdited') + ? { autoFit: table.attrs?.tblAutoFit as NonNullable } + : {}), + ...(tablePropsEdited('cellMarEdited') && table.attrs?.cellMar + ? { cellMarTwips: table.attrs.cellMar as NonNullable } + : {}), + ...(tablePropsEdited('tblFloatEdited') + ? { floatSide, ...(floatPosition ? { floatPos: floatPosition } : {}) } + : {}), + ...(tablePropsEdited('tblLookEdited') && table.attrs?.tblLook + ? { tableLook: table.attrs.tblLook as NonNullable } + : {}), } } @@ -1044,6 +1159,10 @@ export function runsToInline(runs: Run[]): PmNode[] { wrap: run.image.wrap ?? null, offsetXEmu: run.image.offsetXEmu ?? null, offsetYEmu: run.image.offsetYEmu ?? null, + wrapDistTopEmu: run.image.wrapDistTopEmu ?? null, + wrapDistBottomEmu: run.image.wrapDistBottomEmu ?? null, + wrapDistLeftEmu: run.image.wrapDistLeftEmu ?? null, + wrapDistRightEmu: run.image.wrapDistRightEmu ?? null, border: run.image.border ?? null, lineCenterV: run.image.lineCenterV ?? false, }, @@ -1109,6 +1228,8 @@ function runMarks(run: Run): PmMark[] { run.shading || run.vertAlign || run.em || + run.bold === false || + run.italic === false || run.caps || run.cs || run.styleId || @@ -1130,6 +1251,8 @@ function runMarks(run: Run): PmMark[] { shading: run.shading ?? null, vertAlign: run.vertAlign ?? null, em: run.em ?? null, + boldOff: run.bold === false || null, + italicOff: run.italic === false || null, caps: run.caps ?? null, cs: run.cs ?? null, styleId: run.styleId ?? null, @@ -1876,6 +1999,12 @@ function imagePatchOf(node: PmNode, original: Block): ImageBlockPatch | null { return Object.keys(patch).length > 0 ? patch : null } +/** gridGap placeholders have no w:tc in the original XML: drop them so the + * patch grid's indexes line up with document-order w:tc segments */ +function patchableCells(row: TableCell[] | undefined): TableCell[] { + return (row ?? []).filter((cell) => !cell.gridGap) +} + /** per-cell text changes on an original table block; null when untouched */ function tableTextsPatch(node: PmNode, original: Block): (string[] | null)[][] | null { if (original.type !== 'table' || node.attrs?.blockType !== 'table') return null @@ -1883,15 +2012,16 @@ function tableTextsPatch(node: PmNode, original: Block): (string[] | null)[][] | const orig = original.table if (!current || !orig) return null let changed = false - const texts = current.rows.map((row, r) => - row.map((cell, c) => { - const originalCell = orig.rows[r]?.[c] + const texts = current.rows.map((row, r) => { + const origRow = patchableCells(orig.rows[r]) + return patchableCells(row).map((cell, c) => { + const originalCell = origRow[c] if (!originalCell) return null if (cell.paras.join('\n') === originalCell.paras.join('\n')) return null changed = true return cell.paras - }), - ) + }) + }) return changed ? texts : null } @@ -1901,9 +2031,10 @@ function tableTextsPatchFromModel( ): (CellTextsPatch | null)[][] | null { if (original.type !== 'table' || !original.table) return null let changed = false - const texts = current.rows.map((row, r) => - row.map((cell, c): CellTextsPatch | null => { - const originalCell = original.table!.rows[r]?.[c] + const texts = current.rows.map((row, r) => { + const origRow = patchableCells(original.table!.rows[r]) + return patchableCells(row).map((cell, c): CellTextsPatch | null => { + const originalCell = origRow[c] if (!originalCell) return null // nested tables: diff cell texts per table; changes go through the recursive surgical patch if (cell.nestedTables?.length && originalCell.nestedTables?.length) { @@ -1922,8 +2053,8 @@ function tableTextsPatchFromModel( if (cell.paras.join('\n') === originalCell.paras.join('\n')) return null changed = true return cell.paras - }), - ) + }) + }) return changed ? texts : null } @@ -1931,14 +2062,15 @@ function tableTextsPatchFromModel( function nestedTextsDiff(current: TableModel, original: TableModel): (string[] | null)[][] | null { if (current.rows.length !== original.rows.length) return null let changed = false - const grid = current.rows.map((row, r) => - row.map((cell, c) => { - const originalCell = original.rows[r]?.[c] + const grid = current.rows.map((row, r) => { + const origRow = patchableCells(original.rows[r]) + return patchableCells(row).map((cell, c) => { + const originalCell = origRow[c] if (!originalCell || cell.paras.join('\n') === originalCell.paras.join('\n')) return null changed = true return cell.paras - }), - ) + }) + }) return changed ? grid : null } diff --git a/apps/docs/src/renderer/editor/extensions.ts b/apps/docs/src/renderer/editor/extensions.ts index f2a2f45..e13548e 100644 --- a/apps/docs/src/renderer/editor/extensions.ts +++ b/apps/docs/src/renderer/editor/extensions.ts @@ -459,11 +459,12 @@ function blockAttrs( } else if (node.attrs.emptyRunSize || node.attrs.emptyRunFont) { // Word sizes an empty line by the paragraph mark / empty run, both directions if (node.attrs.emptyRunSize) styles.push(`font-size:${Number(node.attrs.emptyRunSize) / 2}pt`) - // CJK mark faces stay on the document factor: table cells deliberately keep - // the Latin factor for empty lines, and KR/JP line height is calibrated - // doc-wide — scoping to Western faces fixes their hairline surplus only + // the mark face sizes the empty line, CJK included (empty-line probe + // 2026-08-25: SimSun 1.30 / DengXian 1.36 / Malgun 1.74 / Calibri 1.24 — + // each face's own text factor; the earlier Western-only scoping starved + // CJK marks down to the document factor and doubled their grid rows) const fam = node.attrs.emptyRunFont ? String(node.attrs.emptyRunFont) : null - if (fam && !isCjkFontName(fam)) { + if (fam) { styles.push(`--doc-line-factor:${lineHeightFactor(fam)}`, `font-family:${cssFontFamily(fam)}`) } } @@ -687,6 +688,10 @@ export const DocInlineImage = Node.create({ wrap: { default: null as string | null }, offsetXEmu: { default: null as number | null }, offsetYEmu: { default: null as number | null }, + wrapDistTopEmu: { default: null as number | null }, + wrapDistBottomEmu: { default: null as number | null }, + wrapDistLeftEmu: { default: null as number | null }, + wrapDistRightEmu: { default: null as number | null }, /** picture outline (pic:spPr a:ln solid fill, display-only) */ border: { default: null as { color: string; widthPt: number } | null }, /** positionV line/center: the picture centers on its anchor line (display only) */ @@ -732,6 +737,34 @@ export const DocInlineImage = Node.create({ // square/tight/through wrap → real CSS float so the surrounding text wraps; // topBottom → block line of its own if (wrap) attrs.class += ` doc-inline-img--wrap-${wrap}` + // free-position floats honor the numeric posOffset X like the block-image + // path: X measures from the column start; right floats convert it to a + // right-edge inset so the picture is not stuck flush against the margin + const tx = node.attrs.offsetXEmu != null ? Number(node.attrs.offsetXEmu) / EMU_PER_PX : null + if (tx != null && wrap) { + if (wrap.endsWith('-right') && w > 0) { + attrs.style = `${attrs.style ?? ''};margin-right:calc(100% - ${(tx + w).toFixed(1)}px);max-width:none` + } else if (wrap.endsWith('-left')) { + attrs.style = `${attrs.style ?? ''};margin-left:${tx.toFixed(1)}px;max-width:none` + } + } + if (wrap) { + const distancePx = (attr: string): number | null => + node.attrs[attr] != null ? Number(node.attrs[attr]) / EMU_PER_PX : null + const top = distancePx('wrapDistTopEmu') + const bottom = distancePx('wrapDistBottomEmu') + const left = distancePx('wrapDistLeftEmu') + const right = distancePx('wrapDistRightEmu') + const distances = [ + top != null ? `margin-top:${top.toFixed(1)}px` : '', + bottom != null ? `margin-bottom:${bottom.toFixed(1)}px` : '', + wrap.endsWith('-right') && left != null ? `margin-left:${left.toFixed(1)}px` : '', + wrap.endsWith('-left') && right != null ? `margin-right:${right.toFixed(1)}px` : '', + ].filter(Boolean) + if (distances.length) { + attrs.style = `${attrs.style ?? ''};${distances.join(';')}` + } + } // positionV line/center: lift so the picture centers on the anchor line // (0.75em ≈ half a single-spaced line) instead of hanging below it if (node.attrs.lineCenterV && h > 0) { @@ -1499,6 +1532,8 @@ const tableCellAttrs = { textDirection: { default: null as string | null }, /** inner clip-box height (twips) when the row is hRule="exact" (computed in convert.ts) */ clipHeightTwips: { default: null as number | null }, + /** display placeholder for the row's w:gridBefore/w:gridAfter columns (borderless, not saved as w:tc) */ + gridGap: { default: false }, colspan: { default: 1 }, rowspan: { default: 1 }, colwidth: { default: null as number[] | null }, @@ -1529,6 +1564,8 @@ export function borderLineCss( function tableCellHtml(node: PmNode): Record { const attrs: Record = {} + // gap placeholders never save back as w:tc, so typed text would vanish — keep them inert + if (node.attrs.gridGap) attrs.contenteditable = 'false' if (node.attrs.colspan > 1) attrs.colspan = String(node.attrs.colspan) if (node.attrs.rowspan > 1) attrs.rowspan = String(node.attrs.rowspan) if (node.attrs.colwidth) attrs['data-colwidth'] = (node.attrs.colwidth as number[]).join(',') @@ -1545,6 +1582,8 @@ function tableCellHtml(node: PmNode): Record { } const mar = node.attrs.cellMar as Record | null const styles = [ + // gridBefore/gridAfter placeholder: bare grid space (inline border beats the --doc-b-* cell rules) + node.attrs.gridGap ? 'border:none;background:none' : '', // vertical-text cells: tbRl = vertical right-to-left (vertical-rl), btLr = rotated 90° counterclockwise (sideways-lr) node.attrs.textDirection === 'tbRl' ? 'writing-mode:vertical-rl' @@ -1656,11 +1695,30 @@ export const DocTable = Node.create({ widthPx: { default: null as number | null }, widthPct: { default: null as number | null }, cellMar: { default: null as Record | null }, + /** w:tblCellSpacing (twips, half the inter-cell gap) → CSS border-spacing */ + cellSpacingTwips: { default: null as number | null }, + /** table shading (tblPr w:shd), hex without '#' */ + tblFill: { default: null as string | null }, + cellMarEdited: { default: false }, borders: { default: null as Record | null }, tblAlign: { default: null as string | null }, tblFloat: { default: null as string | null }, + tblFloatSource: { default: null as string | null }, + tblFloatSuppressed: { default: false }, + tblFloatXTwips: { default: null as number | null }, + tblFloatYTwips: { default: null as number | null }, + tblFloatHorzAnchor: { default: null as string | null }, + tblFloatVertAnchor: { default: null as string | null }, + tblFloatDistance: { default: null as Record | null }, + /** display-only measured width used to place right AutoFit floats */ + tblFloatWidthPx: { default: null as number | null }, + tblFloatEdited: { default: false }, + tblAutoFit: { default: 'fixed' as 'contents' | 'window' | 'fixed' }, + tblAutoFitEdited: { default: false }, indentTwips: { default: null as number | null }, tblStyleId: { default: null as string | null }, + tblLook: { default: null as Record | null }, + tblLookEdited: { default: false }, /** SDT shell JSON when the table is a content-control member (chrome hit-testing) */ sdtShell: { default: null as string | null }, /** RTL table (tblPr w:bidiVisual): columns right to left */ @@ -1677,12 +1735,22 @@ export const DocTable = Node.create({ const attrs: Record = { class: 'doc-table' } if (node.attrs.docxIndex !== null) attrs['data-idx'] = String(node.attrs.docxIndex) if (node.attrs.tblStyleId) attrs['data-tbl-style'] = String(node.attrs.tblStyleId) - const tblFloated = node.attrs.tblFloat === 'left' || node.attrs.tblFloat === 'right' + const autoFit = node.attrs.tblAutoFit as 'contents' | 'window' | 'fixed' + // Imported auto-layout tables may carry a display-only expanded width from + // the fidelity pass. Keep that measured grid until the user explicitly + // chooses AutoFit Contents (the command clears widthPx). + const displayAutoFit = autoFit === 'contents' && node.attrs.widthPx ? 'fixed' : autoFit + attrs.class += ` doc-table-autofit-${displayAutoFit}` + const tblFloated = + !node.attrs.tblFloatSuppressed && + (node.attrs.tblFloat === 'left' || node.attrs.tblFloat === 'right') if (tblFloated) attrs.class += ` doc-table-float-${node.attrs.tblFloat}` if (node.attrs.bidiVisual) attrs.dir = 'rtl' const styles: string[] = [] let centerMargin: string | null = null - if (node.attrs.widthPct) styles.push(`width:${Number(node.attrs.widthPct)}%`) + if (displayAutoFit === 'contents') styles.push('width:auto') + else if (displayAutoFit === 'window') styles.push('width:100%') + else if (node.attrs.widthPct) styles.push(`width:${Number(node.attrs.widthPct)}%`) // Over-wide grids may spill into the page margins like Word/LO (clamping // them to the content box narrowed every column, wrapped cell text onto extra // lines and inflated PDF-converted documents by pages), but never past the paper: @@ -1709,11 +1777,39 @@ export const DocTable = Node.create({ } const pad = cellPadCss(node.attrs.cellMar as Record | null) if (pad) styles.push(`--doc-cell-pad:${pad}`) + // w:tblCellSpacing: each cell contributes the value on its side, so the CSS + // gap between cells is twice it; cells render individually boxed like Word + if (node.attrs.cellSpacingTwips) { + const gapPx = ((Number(node.attrs.cellSpacingTwips) * 2) / 15).toFixed(1) + styles.push('border-collapse:separate', `border-spacing:${gapPx}px`) + } + if (node.attrs.tblFill) styles.push(`background-color:#${node.attrs.tblFill}`) styles.push(...tableBordersCss(node.attrs.borders as TableBordersAttr | null)) // w:tblpPr positioning supersedes w:jc / w:tblInd: alignment or indent // margins would override the float stylesheet's wrap gaps if (tblFloated) { - // no alignment/indent margins + const distance = (node.attrs.tblFloatDistance as Record | null) ?? {} + const px = (twips: unknown): number => (Number(twips) || 0) / 15 + const x = px(node.attrs.tblFloatXTwips) + const y = px(node.attrs.tblFloatYTwips) + const top = y + Math.max(0, px(distance.top)) + const bottom = Math.max(0, px(distance.bottom)) + const left = Math.max(0, px(distance.left)) + const right = Math.max(0, px(distance.right)) + if (top) styles.push(`margin-top:${top.toFixed(1)}px`) + if (bottom) styles.push(`margin-bottom:${bottom.toFixed(1)}px`) + if (node.attrs.tblFloat === 'left') { + if (x) styles.push(`margin-left:${x.toFixed(1)}px`) + if (right) styles.push(`margin-right:${right.toFixed(1)}px`) + } else { + if (left) styles.push(`margin-left:${left.toFixed(1)}px`) + const width = Number(node.attrs.widthPx) || Number(node.attrs.tblFloatWidthPx) + if (node.attrs.tblFloatXTwips != null && width > 0) { + styles.push( + `margin-right:max(0px,calc(var(--doc-content-w,100%) - ${x.toFixed(1)}px - ${width.toFixed(1)}px))`, + ) + } + } } else if (node.attrs.tblAlign === 'center') { if (centerMargin) styles.push(centerMargin) else styles.push('margin-left:auto', 'margin-right:auto') @@ -1730,7 +1826,7 @@ export const DocTable = Node.create({ firstRowCols += Number(cell.attrs.colspan) || 1 }) const rawPct = node.attrs.colWidthsPct as number[] | null - if (rawPct?.length) { + if (displayAutoFit !== 'contents' && rawPct?.length) { // zero-width grid slots get a small floor, short grids pad with the average — // dropping the whole colgroup falls back to fixed-layout even splitting, which // is always worse than an approximate grid @@ -1762,6 +1858,8 @@ export const DocTableRow = Node.create({ return { heightTwips: { default: null as number | null }, heightRule: { default: null as 'atLeast' | 'exact' | null }, + repeatHeader: { default: false }, + repeatHeaderEdited: { default: false }, rawTrPr: { default: null as string | null }, /** trPr w:ins/w:del row-level revision ({kind, author, ...} | null) */ rowRevision: { default: null as Record | null }, @@ -1779,6 +1877,7 @@ export const DocTableRow = Node.create({ attrs.style = `height:${((h / 1440) * 96).toFixed(1)}px` if (node.attrs.heightRule === 'exact') classes.push('row-h-exact') } + attrs['data-repeat-header'] = node.attrs.repeatHeader ? '1' : '0' if (rev?.kind) { classes.push(`row-rev-${rev.kind}`) if (rev.author) attrs.title = rev.author @@ -2181,9 +2280,20 @@ export const DocProtected = Node.create({ imageCrop: { default: null as { l: number; t: number; r: number; b: number } | null }, /** fill placement (a:fillRect) fractions (negative = bleed), display-only */ imageFillRect: { default: null as { l: number; t: number; r: number; b: number } | null }, + imageLeadingText: { default: null as string | null }, + imageLeadingFont: { default: null as string | null }, + imageLeadingExplicitSpaceWidthPx: { default: null as number | null }, + imageLeadingImplicitSpaceCount: { default: null as number | null }, + imageParagraphIndentLeft: { default: null as number | null }, + imageParagraphIndentRight: { default: null as number | null }, + imageParagraphIndentFirstLine: { default: null as number | null }, /** paragraph alignment of the image (w:jc) */ imageAlign: { default: null as string | null }, imageWrap: { default: null as string | null }, + imageWrapDistTopEmu: { default: null as number | null }, + imageWrapDistBottomEmu: { default: null as number | null }, + imageWrapDistLeftEmu: { default: null as number | null }, + imageWrapDistRightEmu: { default: null as number | null }, /** * Stacking rank of a floating image among overlapping anchors * (bring-to-front / send-to-back). Written to the anchor's @@ -2196,6 +2306,8 @@ export const DocProtected = Node.create({ * or is inline. */ imageOffsetXEmu: { default: null as number | null }, + /** wp:anchor locked="1": keep the anchor paragraph fixed while dragging */ + imageAnchorLocked: { default: false }, /** margin-relative wp:align preset (Word position gallery) */ imagePosH: { default: null as string | null }, imagePosV: { default: null as string | null }, @@ -2651,11 +2763,65 @@ function protectedDomSpec(node: PmNode): DomSpec { attrs['style'] = `text-align:${imageAlign}` } if (imageWrap) attrs.class += ` img-wrap-${String(imageWrap)}` + const imageLeadingText = String(node.attrs.imageLeadingText ?? '') + const cjkFixedLeadingSpaces = + !imageWrap && + /^[ ]+$/.test(imageLeadingText) && + !!node.attrs.imageLeadingFont && + isCjkFontName(String(node.attrs.imageLeadingFont)) + // paragraph indents place the picture like its (possibly empty) first line; + // anchored pictures position from the column instead and ignore them + if (!imageWrap) { + const paragraphLayout = [ + node.attrs.imageParagraphIndentLeft + ? `margin-inline-start:${Number(node.attrs.imageParagraphIndentLeft) / 20}pt` + : '', + node.attrs.imageParagraphIndentRight + ? `margin-inline-end:${Number(node.attrs.imageParagraphIndentRight) / 20}pt` + : '', + !cjkFixedLeadingSpaces && node.attrs.imageParagraphIndentFirstLine + ? `text-indent:${Number(node.attrs.imageParagraphIndentFirstLine) / 20}pt` + : '', + ].filter(Boolean) + if (paragraphLayout.length) { + attrs.style = `${attrs.style ? `${attrs.style};` : ''}${paragraphLayout.join(';')}` + } + } + const imageLeadingStyle = [ + node.attrs.imageLeadingFont + ? `font-family:${cssFontFamily(String(node.attrs.imageLeadingFont))}` + : '', + ] + .filter(Boolean) + .join(';') + const imageLeadingSpecs: DomSpec[] = + imageLeadingText && !cjkFixedLeadingSpaces + ? [ + [ + 'span', + { + class: 'doc-image-leading-space', + ...(imageLeadingStyle ? { style: imageLeadingStyle } : {}), + }, + imageLeadingText, + ], + ] + : [] // no-wrap / behind-text anchors leave the flow like floating textboxes: // zero-height wrapper (doc-img-float), absolutely positioned inner wrap // (Word overlays them on the text instead of reserving a line) - let imgWrapTransform = '' + const explicitSpaceWidthPx = Number(node.attrs.imageLeadingExplicitSpaceWidthPx ?? 0) + const implicitSpaceCount = Number( + node.attrs.imageLeadingImplicitSpaceCount ?? + (explicitSpaceWidthPx > 0 ? 0 : imageLeadingText.length), + ) + let imgWrapTransform = cjkFixedLeadingSpaces + ? `margin-left:calc(${Number(node.attrs.imageParagraphIndentFirstLine ?? 0) / 15 + explicitSpaceWidthPx}px + ${implicitSpaceCount * 0.5}em)` + : '' let imgFloatPos = '' + // the vertical posOffset margin must not be clobbered by a wrap-distance + // margin-top below (distT is clearance, the offset is position — position wins) + let hasOffsetTopMargin = false if (imageWrap === 'front' || imageWrap === 'behind') { attrs.class += ' doc-img-float' // z-index bands keep behind-text pictures under the body text and @@ -2694,7 +2860,10 @@ function protectedDomSpec(node: PmNode): DomSpec { // a negative offset lifts via the inner wrap so the flow band keeps // its height (Word: logo above its anchor line must not push) if (ty < 0) imgWrapTransform = `margin-top:${ty.toFixed(1)}px` - else if (ty > 0) wrapperCss.push(`margin-top:${ty.toFixed(1)}px`) + else if (ty > 0) { + wrapperCss.push(`margin-top:${ty.toFixed(1)}px`) + hasOffsetTopMargin = true + } } if (node.attrs.imageOffsetXEmu != null) { const tx = Number(node.attrs.imageOffsetXEmu) / EMU_PER_PX @@ -2717,6 +2886,30 @@ function protectedDomSpec(node: PmNode): DomSpec { attrs['style'] = `${attrs['style'] ? `${attrs['style']};` : ''}${wrapperCss.join(';')}` } } + // wrap distances apply to in-flow wraps only: wrapNone (front/behind) + // anchors ignore them in Word, and margins on the zero-height wrapper + // would displace the following flow content + if (imageWrap && imageWrap !== 'front' && imageWrap !== 'behind') { + const distancePx = (attr: string): number | null => + node.attrs[attr] != null ? Number(node.attrs[attr]) / EMU_PER_PX : null + const top = distancePx('imageWrapDistTopEmu') + const bottom = distancePx('imageWrapDistBottomEmu') + const left = distancePx('imageWrapDistLeftEmu') + const right = distancePx('imageWrapDistRightEmu') + const distances = [ + !hasOffsetTopMargin && top != null ? `margin-top:${top.toFixed(1)}px` : '', + bottom != null ? `margin-bottom:${bottom.toFixed(1)}px` : '', + String(imageWrap).endsWith('-right') && left != null + ? `margin-left:${left.toFixed(1)}px` + : '', + String(imageWrap).endsWith('-left') && right != null + ? `margin-right:${right.toFixed(1)}px` + : '', + ].filter(Boolean) + if (distances.length) { + attrs.style = `${attrs.style ? `${attrs.style};` : ''}${distances.join(';')}` + } + } const imgAttrs: Record = { src: String(imageDataUrl), class: 'doc-protected-img', @@ -2778,14 +2971,23 @@ function protectedDomSpec(node: PmNode): DomSpec { 'div', attrs, moveHandleSpec(t('editorMoveImage')), + ...(imageWrap ? [imageAnchorMarkerSpec()] : []), + ...imageLeadingSpecs, [ 'span', { class: 'doc-img-wrap doc-img-crop', - style: `position:${imgFloatPos ? `absolute;${imgFloatPos}` : 'relative'};display:inline-block;overflow:hidden;width:${W}px;height:${H}px${wrapXf.length ? `;transform:${wrapXf.join(' ')}` : ''}${imgWrapTransform && !imgWrapTransform.startsWith('transform:') ? `;${imgWrapTransform}` : ''}${borderCss ? `;${borderCss}` : ''}`, + style: `position:${imgFloatPos ? `absolute;${imgFloatPos}` : 'relative'};display:inline-block;width:${W}px;height:${H}px${wrapXf.length ? `;transform:${wrapXf.join(' ')}` : ''}${imgWrapTransform && !imgWrapTransform.startsWith('transform:') ? `;${imgWrapTransform}` : ''}${borderCss ? `;${borderCss}` : ''}`, }, - ['img', imgAttrs], - ['span', { class: 'img-resize-handle' }], + [ + 'span', + { + class: 'doc-img-crop-viewport', + style: 'position:absolute;inset:0;overflow:hidden', + }, + ['img', imgAttrs], + ], + ...imageSelectionControlsSpec(), ], ] } @@ -2800,6 +3002,8 @@ function protectedDomSpec(node: PmNode): DomSpec { 'div', attrs, moveHandleSpec(t('editorMoveImage')), + ...(imageWrap ? [imageAnchorMarkerSpec()] : []), + ...imageLeadingSpecs, [ 'span', { @@ -2811,7 +3015,7 @@ function protectedDomSpec(node: PmNode): DomSpec { : {}), }, ['img', imgAttrs], - ['span', { class: 'img-resize-handle' }], + ...imageSelectionControlsSpec(), ], ] } @@ -2941,6 +3145,44 @@ function moveHandleSpec(label: string): DomSpec { ] } +type ImageResizeHandle = 'nw' | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' + +const IMAGE_RESIZE_HANDLES: readonly ImageResizeHandle[] = [ + 'nw', + 'n', + 'ne', + 'e', + 'se', + 's', + 'sw', + 'w', +] + +function imageSelectionControlsSpec(): DomSpec[] { + return IMAGE_RESIZE_HANDLES.map((direction): DomSpec => [ + 'span', + { + class: `img-resize-handle img-resize-handle-${direction}`, + 'data-resize-handle': direction, + contenteditable: 'false', + draggable: 'false', + 'aria-hidden': 'true', + }, + ]) +} + +function imageAnchorMarkerSpec(): DomSpec { + return [ + 'span', + { + class: 'doc-image-anchor-marker', + contenteditable: 'false', + 'aria-hidden': 'true', + }, + '⚓', + ] +} + export function buildProtectedDom(node: PmNode): HTMLElement { const { dom } = DOMSerializer.renderSpec(document, protectedDomSpec(node) as never) const el = dom as HTMLElement @@ -3599,11 +3841,15 @@ function imageResizePlugin(): Plugin { window.addEventListener('mouseup', onUp) return true } - if (!target.classList?.contains('img-resize-handle')) return false - const wrapper = target.closest('.doc-protected') as HTMLElement | null + const handle = target.closest('.img-resize-handle') as HTMLElement | null + const direction = handle?.dataset.resizeHandle as ImageResizeHandle | undefined + if (!handle || !direction || !IMAGE_RESIZE_HANDLES.includes(direction)) return false + const wrapper = handle.closest('.doc-protected') as HTMLElement | null + const imageBox = handle.closest('.doc-img-wrap') as HTMLElement | null const img = wrapper?.querySelector('img.doc-protected-img') as HTMLImageElement | null - if (!wrapper || !img) return false + if (!wrapper || !imageBox || !img) return false event.preventDefault() + event.stopPropagation() let pos = -1 view.state.doc.descendants((node, p) => { @@ -3614,42 +3860,126 @@ function imageResizePlugin(): Plugin { if (pos === -1) return false view.dispatch(view.state.tr.setSelection(NodeSelection.create(view.state.doc, pos))) + view.focus() // CSS `zoom` scales client coordinates; divide it back out const zoomEl = document.querySelector('.doc-zoom') as HTMLElement | null const zoom = zoomEl ? parseFloat(getComputedStyle(zoomEl).zoom || '1') || 1 : 1 // Layout-box measurements: getBoundingClientRect would include the // rotation/flip transform, swapping width/height for 90°-rotated images - const startW = img.offsetWidth - const ratio = img.offsetHeight / Math.max(1, img.offsetWidth) - const priorStyle = img.getAttribute('style') + const selectedNode = view.state.doc.nodeAt(pos) + const modelW = Number(selectedNode?.attrs.imageWidthPx) + const modelH = Number(selectedNode?.attrs.imageHeightPx) + const startW = modelW > 0 ? modelW : imageBox.offsetWidth || img.offsetWidth + const startH = modelH > 0 ? modelH : imageBox.offsetHeight || img.offsetHeight + if (!(startW > 0) || !(startH > 0)) return false + const priorBoxStyle = imageBox.getAttribute('style') + const priorImgStyle = img.getAttribute('style') + const cropped = imageBox.classList.contains('doc-img-crop') + const startImgW = parseFloat(img.style.width) || img.offsetWidth || startW + const startImgH = parseFloat(img.style.height) || img.offsetHeight || startH + const startImgLeft = parseFloat(img.style.left) || 0 + const startImgTop = parseFloat(img.style.top) || 0 + const pxLeft = /^-?\d+(?:\.\d+)?px$/.test(imageBox.style.left) + ? parseFloat(imageBox.style.left) + : null + const pxTop = /^-?\d+(?:\.\d+)?px$/.test(imageBox.style.top) + ? parseFloat(imageBox.style.top) + : null + const shiftsInFlow = imageBox.style.position !== 'absolute' const startX = event.clientX + const startY = event.clientY + const west = direction.includes('w') + const east = direction.includes('e') + const north = direction.includes('n') + const south = direction.includes('s') - const widthAt = (e: MouseEvent) => Math.max(24, startW + (e.clientX - startX) / zoom) + const geometryAt = (e: MouseEvent) => { + const dx = (e.clientX - startX) / zoom + const dy = (e.clientY - startY) / zoom + let w = startW + let h = startH + if ((west || east) && (north || south)) { + // Project the pointer onto the aspect-ratio diagonal. Corner + // handles keep picture proportions, matching Word's default. + const sx = west ? -1 : 1 + const sy = north ? -1 : 1 + const delta = + (dx * sx * startW + dy * sy * startH) / + Math.max(1, startW * startW + startH * startH) + const minScale = Math.max(24 / startW, 24 / startH) + const scale = Math.max(minScale, 1 + delta) + w = startW * scale + h = startH * scale + } else if (west || east) { + w = Math.max(24, startW + (west ? -dx : dx)) + } else if (north || south) { + h = Math.max(24, startH + (north ? -dy : dy)) + } + return { + w, + h, + shiftX: west ? startW - w : 0, + shiftY: north ? startH - h : 0, + } + } + + const restorePreview = () => { + if (priorBoxStyle === null) imageBox.removeAttribute('style') + else imageBox.setAttribute('style', priorBoxStyle) + if (priorImgStyle === null) img.removeAttribute('style') + else img.setAttribute('style', priorImgStyle) + } const onMove = (e: MouseEvent) => { - const w = widthAt(e) - img.style.width = `${w}px` - img.style.height = `${w * ratio}px` + const { w, h, shiftX, shiftY } = geometryAt(e) + imageBox.style.width = `${w}px` + imageBox.style.height = `${h}px` + if (pxLeft !== null) imageBox.style.left = `${pxLeft + shiftX}px` + else if (west && shiftsInFlow) imageBox.style.left = `${shiftX}px` + if (pxTop !== null) imageBox.style.top = `${pxTop + shiftY}px` + else if (north && shiftsInFlow) imageBox.style.top = `${shiftY}px` + if (cropped) { + const sx = w / startW + const sy = h / startH + img.style.left = `${startImgLeft * sx}px` + img.style.top = `${startImgTop * sy}px` + img.style.width = `${startImgW * sx}px` + img.style.height = `${startImgH * sy}px` + } else { + img.style.width = `${w}px` + img.style.height = `${h}px` + } } const onUp = (e: MouseEvent) => { window.removeEventListener('mousemove', onMove) window.removeEventListener('mouseup', onUp) // A plain click on the handle must not rewrite the stored size - if (Math.abs(e.clientX - startX) < 2) { - if (priorStyle === null) img.removeAttribute('style') - else img.setAttribute('style', priorStyle) + if (Math.abs(e.clientX - startX) < 2 && Math.abs(e.clientY - startY) < 2) { + restorePreview() return } - const w = Math.round(widthAt(e)) const node = view.state.doc.nodeAt(pos) - if (!node) return - view.dispatch( - view.state.tr.setNodeMarkup(pos, undefined, { - ...node.attrs, - imageWidthPx: w, - imageHeightPx: Math.round(w * ratio), - }), - ) + if (!node) { + restorePreview() + return + } + const geometry = geometryAt(e) + const w = Math.round(geometry.w) + const h = Math.round(geometry.h) + const attrs: Record = { + ...node.attrs, + imageWidthPx: w, + imageHeightPx: h, + } + if (west && node.attrs.imageOffsetXEmu != null) { + attrs.imageOffsetXEmu = + Number(node.attrs.imageOffsetXEmu) + Math.round((startW - w) * EMU_PER_PX) + } + if (north && node.attrs.imageOffsetYEmu != null) { + attrs.imageOffsetYEmu = + Number(node.attrs.imageOffsetYEmu) + Math.round((startH - h) * EMU_PER_PX) + } + view.dispatch(view.state.tr.setNodeMarkup(pos, undefined, attrs)) } window.addEventListener('mousemove', onMove) window.addEventListener('mouseup', onUp) @@ -3701,6 +4031,51 @@ export function findFloatImageAt(x: number, y: number): HTMLElement | null { return null } +interface ImageParagraphAnchor { + pos: number + node: PmNode + dom: HTMLElement + rect: DOMRect +} + +const IMAGE_ANCHOR_NODE_TYPES = new Set(['docParagraph', 'docHeading', 'docListItem']) + +function imageParagraphAnchors(view: EditorView, imagePos: number): ImageParagraphAnchor[] { + const anchors: ImageParagraphAnchor[] = [] + view.state.doc.forEach((node, pos) => { + if (pos === imagePos || !IMAGE_ANCHOR_NODE_TYPES.has(node.type.name)) return + const dom = view.nodeDOM(pos) + if (!(dom instanceof HTMLElement)) return + anchors.push({ pos, node, dom, rect: dom.getBoundingClientRect() }) + }) + return anchors +} + +function pickImageParagraphAnchor( + anchors: ImageParagraphAnchor[], + x: number, + y: number, +): ImageParagraphAnchor | null { + let best: ImageParagraphAnchor | null = null + let bestScore = Infinity + for (const anchor of anchors) { + const rect = anchor.dom.getBoundingClientRect() + anchor.rect = rect + const dy = y < rect.top ? rect.top - y : y > rect.bottom ? y - rect.bottom : 0 + const dx = x < rect.left ? rect.left - x : x > rect.right ? x - rect.right : 0 + // Vertical proximity determines the paragraph; horizontal proximity only + // breaks ties for multi-column lines at a similar Y. + const score = dy * 10000 + dx + // Stacked paragraphs share a bottom/top edge. At that exact boundary, + // prefer the paragraph beginning there instead of the one that just ended. + if (score < bestScore || (score === bestScore && rect.top > (best?.rect.top ?? -Infinity))) { + best = anchor + bestScore = score + } + } + return best +} + function floatingObjectDragPlugin(): Plugin { return new Plugin({ props: { @@ -3767,6 +4142,11 @@ function floatingObjectDragPlugin(): Plugin { // inline pictures) derive it from the rendered slot, so the drop // lands exactly where the user let go instead of jumping. const innerWrap = wrapper.querySelector('.doc-img-wrap') as HTMLElement | null + const startVisualRect = (innerWrap ?? wrapper).getBoundingClientRect() + const wrapperStartRect = wrapper.getBoundingClientRect() + const anchorLocked = !!node.attrs.imageAnchorLocked + const paragraphAnchors = isImage && !anchorLocked ? imageParagraphAnchors(view, pos) : [] + let paragraphAnchor: ImageParagraphAnchor | null = null const wrapMode = (node.attrs.imageWrap as string | null) ?? null const isSideFloat = !!wrapMode && /^(?:square|tight|through)-/.test(wrapMode) const imgW = innerWrap?.offsetWidth || Number(node.attrs.imageWidthPx ?? 0) || 0 @@ -3817,6 +4197,11 @@ function floatingObjectDragPlugin(): Plugin { // must compose with it (prepended = applied in screen space) and the // original must come back on mouseup, or the orientation vanishes const baseTransform = visual?.style.transform ?? '' + const anchorMarker = isImage + ? (wrapper.querySelector('.doc-image-anchor-marker') as HTMLElement | null) + : null + const anchorMarkerTransform = anchorMarker?.style.transform ?? '' + const anchorMarkerLeft = anchorMarker?.style.left ?? '' // 3px threshold keeps plain clicks (select, first click of a // double-click-to-edit) from nudging the object @@ -3830,12 +4215,33 @@ function floatingObjectDragPlugin(): Plugin { visual.style.transform = `translate(${dx}px, ${dy}px)` + (baseTransform ? ` ${baseTransform}` : '') } + if (isImage) { + const clientDx = e.clientX - startX + const clientDy = e.clientY - startY + paragraphAnchor = pickImageParagraphAnchor( + paragraphAnchors, + startVisualRect.left + clientDx, + startVisualRect.top + clientDy, + ) + if (anchorMarker && paragraphAnchor) { + const markerY = (paragraphAnchor.rect.top - wrapperStartRect.top) / zoom + const markerX = (paragraphAnchor.rect.left - wrapperStartRect.left) / zoom - 32 + anchorMarker.style.transform = `translateY(${markerY.toFixed(1)}px)` + anchorMarker.style.left = `${markerX.toFixed(1)}px` + anchorMarker.dataset.anchorTargetPos = String(paragraphAnchor.pos) + } + } } const onUp = (e: MouseEvent) => { window.removeEventListener('mousemove', onMove) window.removeEventListener('mouseup', onUp) if (visual) visual.style.transform = baseTransform + if (anchorMarker) { + anchorMarker.style.transform = anchorMarkerTransform + anchorMarker.style.left = anchorMarkerLeft + delete anchorMarker.dataset.anchorTargetPos + } const dx = (e.clientX - startX) / zoom const dy = (e.clientY - startY) / zoom @@ -3845,7 +4251,18 @@ function floatingObjectDragPlugin(): Plugin { // Word allows negative offsets (into the page margin / above the // anchor paragraph): no clamping const newX = Math.round((startPxX + dx) * EMU_PER_PX) - const newY = Math.round((startPxY + dy) * EMU_PER_PX) + if (isImage) { + paragraphAnchor = pickImageParagraphAnchor( + paragraphAnchors, + startVisualRect.left + (e.clientX - startX), + startVisualRect.top + (e.clientY - startY), + ) + } + const newY = Math.round( + (paragraphAnchor + ? (startVisualRect.top + (e.clientY - startY) - paragraphAnchor.rect.top) / zoom + : startPxY + dy) * EMU_PER_PX, + ) const currentNode = view.state.doc.nodeAt(pos) if (!currentNode) return @@ -3872,16 +4289,36 @@ function floatingObjectDragPlugin(): Plugin { const centerX = startPxX + dx + imgW / 2 nextWrap = `${kind}-${colW > 0 && centerX > colW / 2 ? 'right' : 'left'}` } - view.dispatch( - view.state.tr.setNodeMarkup(pos, undefined, { - ...currentNode.attrs, - imageWrap: nextWrap, - imageOffsetXEmu: newX, - imageOffsetYEmu: newY, - imagePosH: null, - imagePosV: null, - }), - ) + const attrs = { + ...currentNode.attrs, + imageWrap: nextWrap, + imageOffsetXEmu: newX, + imageOffsetYEmu: newY, + imagePosH: null, + imagePosV: null, + } + if ( + paragraphAnchor && + !currentNode.attrs.imageAnchorLocked && + !currentNode.attrs.blockRevision + ) { + // A floating picture's top-level atom is its OOXML anchor + // paragraph. Move that atom immediately before the paragraph + // selected by the drag, preserving docxIndex as the patch + // identity and rebasing Y to the new paragraph above. + const movedNode = currentNode.type.create( + attrs, + currentNode.content, + currentNode.marks, + ) + const tr = view.state.tr.delete(pos, pos + currentNode.nodeSize) + const insertPos = tr.mapping.map(paragraphAnchor.pos) + tr.insert(insertPos, movedNode) + tr.setSelection(NodeSelection.create(tr.doc, insertPos)) + view.dispatch(tr) + } else { + view.dispatch(view.state.tr.setNodeMarkup(pos, undefined, attrs)) + } } } diff --git a/apps/docs/src/renderer/editor/hf-dom.ts b/apps/docs/src/renderer/editor/hf-dom.ts index aa4db9c..41383ef 100644 --- a/apps/docs/src/renderer/editor/hf-dom.ts +++ b/apps/docs/src/renderer/editor/hf-dom.ts @@ -23,7 +23,9 @@ export function paraBorderCss(line?: { color?: string; szPt?: number }): string function applyRunStyle(span: HTMLElement, run: Run): void { if (run.bold) span.style.fontWeight = '600' + else if (run.bold === false) span.style.fontWeight = 'normal' if (run.italic) span.style.fontStyle = 'italic' + else if (run.italic === false) span.style.fontStyle = 'normal' const deco = [run.underline && 'underline', run.strike && 'line-through'].filter(Boolean) if (deco.length > 0) span.style.textDecoration = deco.join(' ') if (run.color) span.style.color = `#${run.color}` @@ -33,6 +35,10 @@ function applyRunStyle(span: HTMLElement, run: Run): void { if (run.font || run.fontAscii) span.style.fontFamily = cssRunFontFamily(run.fontAscii, run.font) if (run.caps === 'all') span.style.textTransform = 'uppercase' else if (run.caps === 'small') span.style.fontVariantCaps = 'small-caps' + else if (run.caps === 'none') { + span.style.textTransform = 'none' + span.style.fontVariantCaps = 'normal' + } } /** one tab-delimited chunk of a paragraph: the runs after the k-th tab, laid out at its stop */ diff --git a/apps/docs/src/renderer/editor/marks.ts b/apps/docs/src/renderer/editor/marks.ts index 6ab92f2..aac9cbe 100644 --- a/apps/docs/src/renderer/editor/marks.ts +++ b/apps/docs/src/renderer/editor/marks.ts @@ -400,6 +400,8 @@ const CLIPBOARD_TEXT_STYLE_TYPES: Record = {} diff --git a/apps/docs/src/renderer/editor/page-gap-nav.ts b/apps/docs/src/renderer/editor/page-gap-nav.ts index 952d662..3ffec87 100644 --- a/apps/docs/src/renderer/editor/page-gap-nav.ts +++ b/apps/docs/src/renderer/editor/page-gap-nav.ts @@ -28,19 +28,32 @@ type Rect = { left: number; right: number; top: number; bottom: number } * PM's estimate is the fallback when the DOM selection is absent or elsewhere. */ function caretRect(view: EditorView, head: number, dir: -1 | 1): Rect { + const estimate = view.coordsAtPos(head, dir === 1 ? -1 : 1) const domSel = (view.root as Document).getSelection?.() if (domSel?.focusNode && view.dom.contains(domSel.focusNode)) { - try { - const range = document.createRange() - range.setStart(domSel.focusNode, domSel.focusOffset) - range.collapse(true) - const r = range.getClientRects()[0] ?? range.getBoundingClientRect() - if (r && r.height > 0) return r - } catch { - /* element-boundary offsets: fall through to the estimate */ + // A focus parked ON the gap widget (a click near the page seam leaves it + // there) yields the gap's own multi-hundred-px box as the "caret" rect; + // every downstream computation then runs on garbage and the dispatched + // jump can land paragraphs — or the whole document — past the boundary + // (alpha ledger r143). Only a text-line-sized rect off the gap counts. + const focusEl = + domSel.focusNode.nodeType === Node.TEXT_NODE + ? domSel.focusNode.parentElement + : (domSel.focusNode as HTMLElement) + if (!focusEl?.closest?.('.page-gap')) { + try { + const range = document.createRange() + range.setStart(domSel.focusNode, domSel.focusOffset) + range.collapse(true) + const r = range.getClientRects()[0] ?? range.getBoundingClientRect() + const estimateH = Math.max(estimate.bottom - estimate.top, 8) + if (r && r.height > 0 && r.height <= estimateH * 3) return r + } catch { + /* element-boundary offsets: fall through to the estimate */ + } } } - return view.coordsAtPos(head, dir === 1 ? -1 : 1) + return estimate } /** @@ -54,12 +67,32 @@ function caretRect(view: EditorView, head: number, dir: -1 | 1): Rect { * just past it ourselves; everywhere else the browser's native motion (with * its goal-column memory) stays in charge. */ +/** consume escape valve (r147): absorbing a press twice for the SAME + * selection head means the geometry error is persistent, not a transient + * reflow race — hand the press back to native rather than freezing the key. + * One absorbed tick covers the r143 race; the next press either recomputes + * cleanly (race) or goes native (persistent). */ +let lastConsumedHead = -1 +const consumePress = (head: number): boolean => { + const firstTime = head !== lastConsumedHead + lastConsumedHead = head + return firstTime +} + function crossPageGap(view: EditorView, dir: -1 | 1, extend: boolean): boolean { const { selection, doc } = view.state if (!(selection instanceof TextSelection)) return false const caret = caretRect(view, selection.head, dir) const lineH = Math.max(caret.bottom - caret.top, 8) - const x = (caret.left + caret.right) / 2 + // Horizontal comes from the POSITION estimate, not the DOM rect: under key + // repeat the DOM selection rect can be read mid-reflow and report a stale x + // at the page margin — every hit-test below then probes the wrong column, + // the band test passes vacuously and the landing resolves garbage (alpha + // ledger r143 reopen: page 2 selections jumped to page 4). The DOM rect + // stays authoritative for the VERTICAL line only (the r122 wrap-boundary + // ambiguity it was introduced for). + const estimate = view.coordsAtPos(selection.head, dir === 1 ? -1 : 1) + const x = (estimate.left + estimate.right) / 2 // nearest gap in the pressed direction at this x (rect scan, not // elementFromPoint: the gap may sit outside the viewport) let gap: HTMLElement | null = null @@ -128,12 +161,31 @@ function crossPageGap(view: EditorView, dir: -1 | 1, extend: boolean): boolean { } } const landingY = (r: DOMRect) => (dir === 1 ? r.bottom + lineH * 0.5 : r.top - lineH * 0.5) - let targetY = landingY(gapRect) - if (ensureVisible(targetY) !== 0) targetY = landingY(gap.getBoundingClientRect()) + let landingGapRect: DOMRect = gapRect + let targetY = landingY(landingGapRect) + if (ensureVisible(targetY) !== 0) { + landingGapRect = gap.getBoundingClientRect() + targetY = landingY(landingGapRect) + } const target = view.posAtCoords({ left: x, top: targetY }) if (!target) { + // the gap IS the adjacent line (the band check passed) — native cannot + // cross an inline gap and would jump a whole page, so CONSUME the press: + // one skipped key-repeat tick, and the next press recomputes fresh restore() - return false + return consumePress(selection.head) + } + // the landing must be the line adjacent to the gap: a hit-test that + // resolves further away (stale geometry, margins) would dispatch a + // multi-paragraph jump (r143) + const landed = view.coordsAtPos(target.pos) + if ( + dir === 1 + ? landed.top > landingGapRect.bottom + lineH * 2 + : landed.bottom < landingGapRect.top - lineH * 2 + ) { + restore() + return consumePress(selection.head) } const next = extend ? TextSelection.create(doc, selection.anchor, target.pos) @@ -142,6 +194,10 @@ function crossPageGap(view: EditorView, dir: -1 | 1, extend: boolean): boolean { restore() return false } + // a successful crossing clears the absorb marker: a LATER transient + // failure at this same head must get its skipped tick again instead of + // falling straight to native (bugbot) + lastConsumedHead = -1 view.dispatch(view.state.tr.setSelection(next).scrollIntoView()) return true } diff --git a/apps/docs/src/renderer/editor/protected-render.ts b/apps/docs/src/renderer/editor/protected-render.ts index bcbc893..44cf6c2 100644 --- a/apps/docs/src/renderer/editor/protected-render.ts +++ b/apps/docs/src/renderer/editor/protected-render.ts @@ -27,6 +27,7 @@ import { import { custGeomBackgroundCss, shapeBackgroundCss, shapeTextInsetsPx } from './shape-svg' import { t } from '../i18n/locale' import { + lumHex, type ChartDisplay, type FieldDisplay, type FormulaDisplay, @@ -95,7 +96,35 @@ export function renderFieldSpec(field: FieldDisplay): DomSpec | null { return ['div', { class: 'doc-field-pagebreak' }, ['span', {}, t('editorPageBreak')]] } if (field.kind === 'text' && field.left) { - return ['div', { class: 'doc-field-text', contenteditable: 'false' }, field.left] + // carry the result runs' face/size: the passthrough div otherwise inherits + // the document default and mis-snaps on a typed line grid. Spaces must + // survive inline: prosemirror-view injects a higher-specificity + // `.ProseMirror [contenteditable=false] { white-space: normal }` + const styles: string[] = ['white-space:pre-wrap'] + if (field.szHalfPoints) styles.push(`font-size:${field.szHalfPoints / 2}pt`) + if (field.align) styles.push(`text-align:${field.align}`) + if (field.fontFamily) { + styles.push( + `--doc-line-factor:${lineHeightFactor(field.fontFamily)}`, + `font-family:${cssFontFamily(field.fontFamily)}`, + ) + } + if (field.szHalfPoints || field.fontFamily || field.lineRule) { + // explicit w:spacing beats the single-spacing snap; either way the line + // strut must recompute from the field's own size/face, not the + // wrapper's document-default computed box + styles.push( + `line-height:${cssLineHeight(field.lineRule, field.lineRawTwips, field.lineSpacing) ?? cssGridLineBase()}`, + ) + const mult = cssAutoLineMult(field.lineRule, field.lineRawTwips, field.lineSpacing) + if (mult && mult !== 1) styles.push(`--doc-line-mult:${mult}`) + } + const attrs: Record = { + class: 'doc-field-text', + contenteditable: 'false', + style: styles.join(';'), + } + return ['div', attrs, field.left] } return null } @@ -127,10 +156,28 @@ export function renderFormulaSpec(formula: FormulaDisplay): DomSpec { // ---- embedded charts: SVG preview + editable data grid ---- -/** Office theme default accent colors, used for new charts */ +/** Office theme default accent colors, used for new charts / theme-less docs */ const CHART_PALETTE = ['4472C4', 'ED7D31', 'A5A5A5', 'FFC000', '5B9BD5', '70AD47'] -const chartColor = (i: number) => `#${CHART_PALETTE[i % CHART_PALETTE.length]}` +/** series/point cycle color; repeat rounds darken like Word (accentN lumMod 60%) */ +function chartColor(chart: ChartDisplay, i: number): string { + const palette = chart.palette?.length ? chart.palette : CHART_PALETTE + const base = palette[i % palette.length] + const round = Math.floor(i / palette.length) + return `#${round > 0 ? lumHex(base, 0.6 ** round, 0) : base}` +} + +/** explicit c:ser fill beats the style cycle */ +function seriesColor(chart: ChartDisplay, s: number): string { + const explicit = chart.series[s]?.color + return explicit ? `#${explicit}` : chartColor(chart, s) +} + +/** per-point fill (pie slices): c:dPt beats the cycle */ +function pointColor(chart: ChartDisplay, s: number, i: number): string { + const explicit = chart.series[s]?.pointColors?.[i] + return explicit ? `#${explicit}` : chartColor(chart, i) +} /** * Chart preview + data grid. The grid is a chart data sheet @@ -167,7 +214,7 @@ export function renderChartSpec(chart: ChartDisplay): DomSpec { class: `doc-chart-name${ser.name !== undefined ? ' doc-chart-cell' : ''}`, 'data-ser': String(s), contenteditable: 'false', - style: `border-left-color:${chartColor(s)}`, + style: `border-left-color:${seriesColor(chart, s)}`, }, ser.name ?? t('editorChartSeries', { num: s + 1 }), ], @@ -224,9 +271,16 @@ export function drawChartSvg(dom: HTMLElement, chart: ChartDisplay | null): void if (!canvas || !chart?.series.length) return // series-name legend inside the SVG: the data grid is an editing affordance // (hidden unless the block is selected), so the printed chart must carry the - // legend itself, like Word/LibreOffice output - const legendNames = chart.series.map((s, i) => s.name ?? t('editorChartSeries', { num: i + 1 })) - const showLegend = chart.series.length > 1 || chart.series.some((s) => s.name) + // legend itself, like Word/LibreOffice output. Pie legends list the + // categories (one colored slice each), not the series. + const isPie = chart.kind === 'pie' + const legendNames = isPie + ? chart.categories + : chart.series.map((s, i) => s.name ?? t('editorChartSeries', { num: i + 1 })) + const legendColor = (i: number) => (isPie ? pointColor(chart, 0, i) : seriesColor(chart, i)) + const showLegend = isPie + ? legendNames.length > 0 + : chart.series.length > 1 || chart.series.some((s) => s.name) // the title row renders above the SVG but Word draws the title inside the // drawing extent; shrink the plot so title + plot together fill heightPx, // or pagination gains ~22px per titled chart and drifts @@ -254,6 +308,7 @@ export function drawChartSvg(dom: HTMLElement, chart: ChartDisplay | null): void svg.style.height = `${geom.height}px` if (chart.kind === 'pie') drawPie(svg, chart, geom) + else if (chart.kind === 'scatter' || chart.kind === 'bubble') drawScatter(svg, chart, geom) else if (chart.kind === 'bar' && chart.horizontal) drawAxesHorizontal(svg, chart, geom) else drawAxes(svg, chart, geom) @@ -266,7 +321,7 @@ export function drawChartSvg(dom: HTMLElement, chart: ChartDisplay | null): void y: String(geom.height - 15), width: '8', height: '8', - fill: chartColor(i), + fill: legendColor(i), }) svgEl( svg, @@ -298,21 +353,49 @@ function svgEl( return el } +/** per-category positive/negative stack sums plus the absolute total (percentStacked base) */ +function stackSums( + chart: ChartDisplay, + cols: number, +): { pos: number[]; neg: number[]; abs: number[] } { + const pos = new Array(cols).fill(0) + const neg = new Array(cols).fill(0) + const abs = new Array(cols).fill(0) + for (const ser of chart.series) { + ser.values.forEach((v, c) => { + if (v === null || c >= cols) return + if (v >= 0) pos[c] += v + else neg[c] += v + abs[c] += Math.abs(v) + }) + } + return { pos, neg, abs } +} + /** bar / line / area charts share the same axes and scale */ function drawAxes(svg: SVGElement, chart: ChartDisplay, geom: ChartGeom): void { - const values = chart.series.flatMap((s) => s.values).filter((v): v is number => v !== null) + const cols = Math.max(chart.categories.length, ...chart.series.map((s) => s.values.length), 1) + const stacked = (chart.kind === 'bar' || chart.kind === 'area') && chart.grouping !== undefined + const pct = stacked && chart.grouping === 'percentStacked' + const sums = stacked ? stackSums(chart, cols) : null + // percentStacked: each value becomes its share of the category's absolute total + const norm = (value: number, c: number) => + pct ? (sums!.abs[c] > 0 ? (value / sums!.abs[c]) * 100 : 0) : value + const values = stacked + ? [...sums!.pos.map(norm), ...sums!.neg.map(norm)] + : chart.series.flatMap((s) => s.values).filter((v): v is number => v !== null) // "nice" axis bounds (1/2/5×10ⁿ step, integer-friendly labels like Word/LO) const rawMax = Math.max(0, ...values) const rawMin = Math.min(0, ...values) const step = niceStep((rawMax - rawMin) / 5 || 1) const min = Math.floor(rawMin / step) * step // Word/LO leave headroom: the top tick sits strictly above the data maximum + // (percent axes stop at 100%) let max = Math.ceil(rawMax / step) * step || step - if (rawMax > 0 && max <= rawMax + 1e-9) max += step + if (!pct && rawMax > 0 && max <= rawMax + 1e-9) max += step const span = max - min || 1 const plotW = geom.width - geom.left - geom.right const plotH = geom.height - geom.top - geom.bottom - const cols = Math.max(chart.categories.length, ...chart.series.map((s) => s.values.length), 1) const yOf = (v: number) => geom.top + plotH - ((v - min) / span) * plotH const slotW = plotW / cols @@ -337,7 +420,7 @@ function drawAxes(svg: SVGElement, chart: ChartDisplay, geom: ChartGeom): void { class: 'doc-chart-axis-label', 'text-anchor': 'end', }, - formatAxisValue(v), + formatAxisValue(v) + (pct ? '%' : ''), ) } @@ -357,7 +440,31 @@ function drawAxes(svg: SVGElement, chart: ChartDisplay, geom: ChartGeom): void { ) }) - if (chart.kind === 'bar') { + if (chart.kind === 'bar' && stacked) { + // one bar per category, series segments cumulated up (down for negatives) + const posBase = new Array(cols).fill(0) + const negBase = new Array(cols).fill(0) + const barPad = slotW * 0.2 + chart.series.forEach((ser, s) => { + ser.values.forEach((value, c) => { + if (value === null || c >= cols) return + const v = norm(value, c) + const from = v >= 0 ? posBase[c] : negBase[c] + const to = from + v + const y0 = yOf(from) + const y1 = yOf(to) + svgEl(svg, 'rect', { + x: String(geom.left + slotW * c + barPad), + y: String(Math.min(y0, y1)), + width: String(slotW - barPad * 2), + height: String(Math.max(1, Math.abs(y0 - y1))), + fill: seriesColor(chart, s), + }) + if (v >= 0) posBase[c] = to + else negBase[c] = to + }) + }) + } else if (chart.kind === 'bar') { const groupPad = slotW * 0.15 const barW = (slotW - groupPad * 2) / chart.series.length chart.series.forEach((ser, s) => { @@ -371,10 +478,33 @@ function drawAxes(svg: SVGElement, chart: ChartDisplay, geom: ChartGeom): void { y: String(Math.min(y0, y1)), width: String(barW * 0.84), height: String(Math.max(1, Math.abs(y0 - y1))), - fill: chartColor(s), + fill: seriesColor(chart, s), }) }) }) + } else if (chart.kind === 'area' && stacked) { + // stacked / 100% stacked area: each band fills between the running total + // below it and its own cumulated top (empty cells count as 0, like Excel) + const bases = new Array(cols).fill(0) + const xAt = (c: number) => geom.left + slotW * c + slotW / 2 + chart.series.forEach((ser, s) => { + const tops = bases.map((b, c) => b + norm(ser.values[c] ?? 0, c)) + const upper = tops.map((v, c) => `${xAt(c)},${yOf(v)}`) + const lower = bases.map((v, c) => `${xAt(c)},${yOf(v)}`).reverse() + svgEl(svg, 'polygon', { + points: [...upper, ...lower].join(' '), + fill: seriesColor(chart, s), + 'fill-opacity': '0.35', + stroke: 'none', + }) + svgEl(svg, 'polyline', { + points: upper.join(' '), + fill: 'none', + stroke: seriesColor(chart, s), + 'stroke-width': '2', + }) + tops.forEach((v, c) => (bases[c] = v)) + }) } else { // line / area / other: one polyline per series through slot centers chart.series.forEach((ser, s) => { @@ -389,7 +519,7 @@ function drawAxes(svg: SVGElement, chart: ChartDisplay, geom: ChartGeom): void { const last = points[points.length - 1].split(',')[0] svgEl(svg, 'polygon', { points: `${first},${yOf(0)} ${points.join(' ')} ${last},${yOf(0)}`, - fill: chartColor(s), + fill: seriesColor(chart, s), 'fill-opacity': '0.35', stroke: 'none', }) @@ -397,27 +527,169 @@ function drawAxes(svg: SVGElement, chart: ChartDisplay, geom: ChartGeom): void { svgEl(svg, 'polyline', { points: points.join(' '), fill: 'none', - stroke: chartColor(s), + stroke: seriesColor(chart, s), 'stroke-width': '2', }) + if (chart.markers) { + for (const p of points) { + const [x, y] = p.split(',') + svgEl(svg, 'circle', { cx: x, cy: y, r: '2.5', fill: seriesColor(chart, s) }) + } + } }) } } +/** scatter / bubble: numeric x-y plot; bubble radius scales by point size (area-true) */ +function drawScatter(svg: SVGElement, chart: ChartDisplay, geom: ChartGeom): void { + interface Pt { + x: number + y: number + size: number | null + c: number + } + const pts: Pt[][] = chart.series.map((ser) => + ser.values + .map((y, c) => { + // no xVal cache at all → 1-based index axis; a null inside the cache is a gap + const x = ser.xValues ? (ser.xValues[c] ?? null) : c + 1 + return y === null || x === null ? null : { x, y, size: ser.sizes?.[c] ?? null, c } + }) + .filter((p): p is Pt => p !== null), + ) + const all = pts.flat() + if (all.length === 0) return + const axis = (vals: number[]) => { + let rawMax = Math.max(...vals) + let rawMin = Math.min(...vals) + // Excel-style auto minimum: anchor at 0 unless the data sits far above it + // (date-serial x values must not squash the points against the right edge) + if (rawMin > 0 && rawMin <= rawMax - rawMin) rawMin = 0 + if (rawMax < 0) rawMax = 0 + const step = niceStep((rawMax - rawMin) / 5 || 1) + const min = Math.floor(rawMin / step) * step + let max = Math.ceil(rawMax / step) * step || step + if (rawMax > 0 && max <= rawMax + 1e-9) max += step + return { min, max, step, span: max - min || 1 } + } + const ax = axis(all.map((p) => p.x)) + const ay = axis(all.map((p) => p.y)) + const plotW = geom.width - geom.left - geom.right + const plotH = geom.height - geom.top - geom.bottom + const xOf = (v: number) => geom.left + ((v - ax.min) / ax.span) * plotW + const yOf = (v: number) => geom.top + plotH - ((v - ay.min) / ay.span) * plotH + + // horizontal gridlines with value labels, like the category charts + const ySteps = Math.max(1, Math.round(ay.span / ay.step)) + for (let i = 0; i <= ySteps; i++) { + const v = ay.min + ay.step * i + const y = yOf(v) + svgEl(svg, 'line', { + x1: String(geom.left), + y1: String(y), + x2: String(geom.width - geom.right), + y2: String(y), + class: 'doc-chart-grid', + }) + svgEl( + svg, + 'text', + { + x: String(geom.left - 6), + y: String(y + 3), + class: 'doc-chart-axis-label', + 'text-anchor': 'end', + }, + formatAxisValue(v), + ) + } + + // x labels: date/text categories sit under their own data points (Word puts + // the cached texts along the axis); numeric x gets plain scale ticks + const catNumeric = + chart.categories.length === 0 || + chart.categories.every((c) => c === '' || Number.isFinite(Number(c))) + const labelY = String(geom.height - geom.bottom + 14) + if (catNumeric) { + const xSteps = Math.max(1, Math.round(ax.span / ax.step)) + for (let i = 0; i <= xSteps; i++) { + const v = ax.min + ax.step * i + svgEl( + svg, + 'text', + { x: String(xOf(v)), y: labelY, class: 'doc-chart-axis-label', 'text-anchor': 'middle' }, + formatAxisValue(v), + ) + } + } else { + const anchor = pts[0] ?? [] + let lastEnd = -Infinity + chart.categories.forEach((cat, c) => { + const p = anchor.find((pt) => pt.c === c) + if (!p) return + // greedy label thinning: drop labels that would overlap the previous one + const x = xOf(p.x) + const half = cat.length * 2.6 + if (x - half < lastEnd + 4) return + lastEnd = x + half + svgEl( + svg, + 'text', + { x: String(x), y: labelY, class: 'doc-chart-axis-label', 'text-anchor': 'middle' }, + cat, + ) + }) + } + + const maxSize = Math.max(0, ...all.map((p) => p.size ?? 0)) + const rMax = Math.min(plotW, plotH) * 0.125 + pts.forEach((points, s) => { + const color = seriesColor(chart, s) + if (chart.series[s]?.line && points.length > 1) { + svgEl(svg, 'polyline', { + points: points.map((p) => `${xOf(p.x)},${yOf(p.y)}`).join(' '), + fill: 'none', + stroke: color, + 'stroke-width': '2', + }) + } + // scatterStyle "line"/"smooth" draws no point markers on lined series + if (chart.kind === 'scatter' && !chart.markers && chart.series[s]?.line) return + for (const p of points) { + const bubble = chart.kind === 'bubble' && p.size !== null && maxSize > 0 + const r = bubble ? Math.max(3, rMax * Math.sqrt(Math.abs(p.size!) / maxSize)) : 3 + svgEl(svg, 'circle', { + cx: String(xOf(p.x)), + cy: String(yOf(p.y)), + r: String(r), + fill: color, + ...(bubble ? { 'fill-opacity': '0.85', stroke: '#fff', 'stroke-width': '1' } : {}), + }) + } + }) +} + /** horizontal bar charts (c:barDir="bar"): value axis on x, categories on y, * first category in the bottom row and series 1 at the bottom of each group, like Word */ function drawAxesHorizontal(svg: SVGElement, chart: ChartDisplay, geom: ChartGeom): void { - const values = chart.series.flatMap((s) => s.values).filter((v): v is number => v !== null) + const rows = Math.max(chart.categories.length, ...chart.series.map((s) => s.values.length), 1) + const stacked = chart.grouping !== undefined + const pct = chart.grouping === 'percentStacked' + const sums = stacked ? stackSums(chart, rows) : null + const norm = (value: number, c: number) => + pct ? (sums!.abs[c] > 0 ? (value / sums!.abs[c]) * 100 : 0) : value + const values = stacked + ? [...sums!.pos.map(norm), ...sums!.neg.map(norm)] + : chart.series.flatMap((s) => s.values).filter((v): v is number => v !== null) const rawMax = Math.max(0, ...values) const rawMin = Math.min(0, ...values) const step = niceStep((rawMax - rawMin) / 5 || 1) const min = Math.floor(rawMin / step) * step let max = Math.ceil(rawMax / step) * step || step - if (rawMax > 0 && max <= rawMax + 1e-9) max += step + if (!pct && rawMax > 0 && max <= rawMax + 1e-9) max += step const span = max - min || 1 const plotW = geom.width - geom.left - geom.right const plotH = geom.height - geom.top - geom.bottom - const rows = Math.max(chart.categories.length, ...chart.series.map((s) => s.values.length), 1) const xOf = (v: number) => geom.left + ((v - min) / span) * plotW const slotH = plotH / rows @@ -442,7 +714,7 @@ function drawAxesHorizontal(svg: SVGElement, chart: ChartDisplay, geom: ChartGeo class: 'doc-chart-axis-label', 'text-anchor': 'middle', }, - formatAxisValue(v), + formatAxisValue(v) + (pct ? '%' : ''), ) } @@ -461,6 +733,32 @@ function drawAxesHorizontal(svg: SVGElement, chart: ChartDisplay, geom: ChartGeo ) }) + if (stacked) { + const posBase = new Array(rows).fill(0) + const negBase = new Array(rows).fill(0) + const barPad = slotH * 0.2 + chart.series.forEach((ser, s) => { + ser.values.forEach((value, c) => { + if (value === null || c >= rows) return + const v = norm(value, c) + const from = v >= 0 ? posBase[c] : negBase[c] + const to = from + v + const x0 = xOf(from) + const x1 = xOf(to) + svgEl(svg, 'rect', { + x: String(Math.min(x0, x1)), + y: String(geom.top + plotH - slotH * (c + 1) + barPad), + width: String(Math.max(1, Math.abs(x0 - x1))), + height: String(slotH - barPad * 2), + fill: seriesColor(chart, s), + }) + if (v >= 0) posBase[c] = to + else negBase[c] = to + }) + }) + return + } + const groupPad = slotH * 0.15 const barH = (slotH - groupPad * 2) / chart.series.length chart.series.forEach((ser, s) => { @@ -474,7 +772,7 @@ function drawAxesHorizontal(svg: SVGElement, chart: ChartDisplay, geom: ChartGeo y: String(y + barH * 0.08), width: String(Math.max(1, Math.abs(x0 - x1))), height: String(barH * 0.84), - fill: chartColor(s), + fill: seriesColor(chart, s), }) }) }) @@ -502,7 +800,7 @@ function drawPie(svg: SVGElement, chart: ChartDisplay, geom: ChartGeom): void { values.filter((v) => v > 0).length === 1 ? `M ${cx - r} ${cy} A ${r} ${r} 0 1 1 ${cx - r} ${cy - 0.01} Z` : `M ${cx} ${cy} L ${x1} ${y1} A ${r} ${r} 0 ${large} 1 ${x2} ${y2} Z` - svgEl(svg, 'path', { d, fill: chartColor(i), stroke: '#fff', 'stroke-width': '1' }) + svgEl(svg, 'path', { d, fill: pointColor(chart, 0, i), stroke: '#fff', 'stroke-width': '1' }) }) } @@ -713,8 +1011,8 @@ function textSpanSpec(run: Run, autoSpace?: boolean): DomSpec { const letterSpacing = runLetterSpacingCss(run) const runStyle = [ run.color ? `color:#${run.color}` : '', - run.bold ? 'font-weight:700' : '', - run.italic ? 'font-style:italic' : '', + run.bold ? 'font-weight:700' : run.bold === false ? 'font-weight:normal' : '', + run.italic ? 'font-style:italic' : run.italic === false ? 'font-style:normal' : '', run.underline ? 'text-decoration:underline' : '', run.font || run.fontAscii || cs ? `font-family:${ @@ -727,6 +1025,7 @@ function textSpanSpec(run: Run, autoSpace?: boolean): DomSpec { letterSpacing ? `letter-spacing:${letterSpacing}` : '', run.caps === 'all' ? 'text-transform:uppercase' : '', run.caps === 'small' ? 'font-variant-caps:small-caps' : '', + run.caps === 'none' ? 'text-transform:none;font-variant-caps:normal' : '', // explicit autoSpaceDE/DN off also disables the browser's native gap (same as the editor path) autoSpace === false ? 'text-autospace:no-autospace' : '', ] @@ -963,6 +1262,8 @@ export function renderTableSpec(model: TableModel, nested = false): DomSpec { } } const style = [ + // gridBefore/gridAfter placeholder: bare grid space, never bordered/filled + cell.gridGap ? 'border:none;background:none' : '', cell.textDirection === 'tbRl' ? 'writing-mode:vertical-rl' : cell.textDirection === 'btLr' @@ -1084,6 +1385,12 @@ export function renderTableSpec(model: TableModel, nested = false): DomSpec { } const pad = cellPadCss(model.cellMarTwips ?? null) if (pad) tableStyles.push(`--doc-cell-pad:${pad}`) + // w:tblCellSpacing: the CSS gap is twice the per-cell-side value (see DocTable.renderHTML) + if (model.cellSpacingTwips) { + const gapPx = ((model.cellSpacingTwips * 2) / 15).toFixed(1) + tableStyles.push('border-collapse:separate', `border-spacing:${gapPx}px`) + } + if (model.fill) tableStyles.push(`background-color:#${model.fill}`) tableStyles.push(...tableBordersCss((model.borders as TableBordersAttr | undefined) ?? null)) if (model.align === 'center') { if (centerMargin) tableStyles.push(centerMargin) diff --git a/apps/docs/src/renderer/editor/table-handle.ts b/apps/docs/src/renderer/editor/table-handle.ts index 4e3d71f..632aa85 100644 --- a/apps/docs/src/renderer/editor/table-handle.ts +++ b/apps/docs/src/renderer/editor/table-handle.ts @@ -31,6 +31,23 @@ function tablePosAt(view: EditorView, coords: { left: number; top: number }): nu return null } +/** DOM hit-testing remains reliable for CSS-floated tables where posAtCoords may resolve to wrapped text. */ +function tablePosFromTarget(view: EditorView, target: EventTarget | null): number | null { + const element = target instanceof Element ? target : null + const table = element?.closest('table.doc-table') + if (!(table instanceof HTMLElement) || !view.dom.contains(table)) return null + let found: number | null = null + view.state.doc.descendants((node, pos) => { + if (found !== null) return false + if (node.type.name === 'docTable' && view.nodeDOM(pos) === table) { + found = pos + return false + } + return true + }) + return found +} + const MOVE_SVG = '
    ` + const boxes = (el: HTMLElement): HTMLElement[] => + Array.from(el.querySelectorAll('.doc-protected-pagepinned')) + const matches = (root: HTMLElement, css: string): boolean[] => { + const selectors = css + .split('\n') + .filter(Boolean) + .map((r) => r.slice(0, r.indexOf('{'))) + return boxes(root).map((b) => selectors.some((s) => b.matches(s))) + } + + it('hides clones owned by other pages, keeps the owning page copy', () => { + const css = pinnedCloneCss(2) + // visibility, not display: stray-run wrappers keep their measured flow height + expect(css).toContain('visibility:hidden') + expect(css).not.toContain('display') + // full-document clone: every page carries both covers (pin 0 and pin 1) + const p0 = page(0, pinned(0) + pinned(1) + '

    body

    ') + const p1 = page(1, pinned(0) + pinned(1) + '

    body

    ') + expect(matches(p0, css)).toEqual([false, true]) + expect(matches(p1, css)).toEqual([true, false]) + }) + + it('never targets unstamped wrappers', () => { + const css = pinnedCloneCss(3) + const p = page(2, pinned() + pinned(2) + pinned(0)) + expect(matches(p, css)).toEqual([false, false, true]) + }) + + it('emits no rules for an empty preview', () => { + expect(pinnedCloneCss(0)).toBe('') + }) +}) diff --git a/apps/docs/tests/protected-text-edit.test.ts b/apps/docs/tests/protected-text-edit.test.ts index 4a4a1c6..21b8b9b 100644 --- a/apps/docs/tests/protected-text-edit.test.ts +++ b/apps/docs/tests/protected-text-edit.test.ts @@ -78,4 +78,34 @@ describe('protected field and formula editing', () => { expect(saved).toEqual(source) editor.destroy() }) + + it('text-field passthrough carries run face/size and preserves spaces (public issue #118)', async () => { + const rpr = + '' + const bodyXml = + `${rpr}` + + `${rpr}\u9636\u8d70\u5230\u6cb3` + + `${rpr} ` + + `${rpr}` + + `${rpr} INCLUDEPICTURE "/tmp/x.jpeg" \\* MERGEFORMATINET ` + + `${rpr}` + + `${rpr}\u5cb8\u8fb9` + const parsed = await parseDocx(await buildDocx({ bodyXml })) + const editor = new Editor({ + element: document.createElement('div'), + extensions: editorExtensions, + content: blocksToPmDoc(parsed.blocks) as never, + }) + const el = editor.view.dom.querySelector('.doc-field-text')! + expect(el).toBeTruthy() + // mid-paragraph space run must survive into the DOM (pre-wrap keeps it) + expect(el.textContent).toContain('\u6cb3 \u5cb8') + expect(el.style.fontSize).toBe('12pt') + expect(el.style.getPropertyValue('--doc-line-factor')).toBe('1.3029') + expect(el.style.lineHeight).toContain('--doc-line-grid') + expect(el.style.textAlign).toBe('left') + // inline: prosemirror-view injects a higher-specificity white-space:normal + expect(el.style.whiteSpace).toBe('pre-wrap') + editor.destroy() + }) }) diff --git a/apps/docs/tests/table-properties.test.ts b/apps/docs/tests/table-properties.test.ts new file mode 100644 index 0000000..1ec6636 --- /dev/null +++ b/apps/docs/tests/table-properties.test.ts @@ -0,0 +1,248 @@ +import { Editor } from '@tiptap/core' +import { TextSelection } from '@tiptap/pm/state' +import { parseDocx } from '@genoffice/docx-engine' +import { describe, expect, it, vi } from 'vitest' +import { buildDocx } from '../../../packages/docx-engine/tests/helpers/build-docx' +import { blocksToPmDoc, pmTableToModel, type PmNode } from '../src/renderer/editor/convert' +import { editorExtensions } from '../src/renderer/editor/extensions' +import { + applyTablePreset, + repeatHeaderState, + setTableAutoFit, + setTableLookOption, + toggleRepeatHeaderRows, + updateSelectedTableAttrs, +} from '../src/renderer/editor/table-properties' + +const TABLE = + '' + + '' + + '' + + '' + + '' + + '' + + 'A' + + 'B' + + 'C' + + 'D' + +async function openTable(): Promise { + const parsed = await parseDocx(await buildDocx({ bodyXml: TABLE })) + const editor = new Editor({ + element: document.createElement('div'), + extensions: editorExtensions, + content: blocksToPmDoc(parsed.blocks) as never, + }) + let firstCell = -1 + editor.state.doc.descendants((node, pos) => { + if ( + firstCell < 0 && + (node.type.name === 'docTableCell' || node.type.name === 'docTableHeader') + ) { + firstCell = pos + } + }) + editor.view.dispatch( + editor.state.tr.setSelection(TextSelection.near(editor.state.doc.resolve(firstCell + 1))), + ) + return editor +} + +describe('editable table properties', () => { + it('loads AutoFit, margins, style options, and repeat-header state', async () => { + const editor = await openTable() + const table = editor.getJSON().content![0] as unknown as PmNode + expect(table.attrs).toMatchObject({ + tblAutoFit: 'window', + cellMar: { left: 120, right: 120 }, + tblLook: { + firstRow: true, + lastRow: false, + firstColumn: true, + lastColumn: false, + bandedRows: true, + bandedColumns: false, + }, + }) + expect(table.content![0].attrs).toMatchObject({ repeatHeader: true }) + expect(repeatHeaderState(editor.state)).toEqual({ enabled: true, active: true }) + editor.destroy() + }) + + it('toggles leading header rows and persists the edited model flag', async () => { + const editor = await openTable() + expect(toggleRepeatHeaderRows()(editor.state, editor.view.dispatch)).toBe(true) + const table = editor.getJSON().content![0] as unknown as PmNode + expect(table.content![0].attrs).toMatchObject({ + repeatHeader: false, + repeatHeaderEdited: true, + }) + expect(pmTableToModel(table as PmNode).repeatHeaderRows).toEqual([false, null]) + editor.destroy() + }) + + it('switches all three AutoFit modes and materializes a fixed grid', async () => { + const editor = await openTable() + expect(setTableAutoFit('contents', 600)(editor.state, editor.view.dispatch)).toBe(true) + let table = editor.getJSON().content![0] as unknown as PmNode + expect(table.attrs).toMatchObject({ + tblAutoFit: 'contents', + tblAutoFitEdited: true, + widthPx: null, + widthPct: null, + colWidthsPct: null, + }) + + expect(setTableAutoFit('window', 600)(editor.state, editor.view.dispatch)).toBe(true) + expect((editor.getJSON().content![0] as unknown as PmNode).attrs).toMatchObject({ + tblAutoFit: 'window', + widthPct: 100, + }) + + expect(setTableAutoFit('fixed', 600)(editor.state, editor.view.dispatch)).toBe(true) + table = editor.getJSON().content![0] as unknown as PmNode + expect(table.attrs).toMatchObject({ tblAutoFit: 'fixed', widthPct: null }) + expect(table.content![0].content![0].attrs?.colwidth).toEqual([300]) + expect(pmTableToModel(table as PmNode).autoFit).toBe('fixed') + editor.destroy() + }) + + it('updates cell margins, floating position, and table style options', async () => { + const editor = await openTable() + expect( + updateSelectedTableAttrs({ + cellMar: { top: 40, right: 100, bottom: 50, left: 110 }, + cellMarEdited: true, + tblFloat: 'left', + tblFloatXTwips: 720, + tblFloatYTwips: 360, + tblFloatHorzAnchor: 'margin', + tblFloatVertAnchor: 'text', + tblFloatDistance: { top: 60, right: 120, bottom: 70, left: 130 }, + tblFloatEdited: true, + })(editor.state, editor.view.dispatch), + ).toBe(true) + expect(setTableLookOption('lastRow', true)(editor.state, editor.view.dispatch)).toBe(true) + + const table = editor.getJSON().content![0] as PmNode + const model = pmTableToModel(table) + expect(model.cellMarTwips).toEqual({ top: 40, right: 100, bottom: 50, left: 110 }) + expect(model.floatSide).toBe('left') + expect(model.floatPos).toMatchObject({ + xTwips: 720, + yTwips: 360, + horzAnchor: 'margin', + vertAnchor: 'text', + }) + expect(model.tableLook?.lastRow).toBe(true) + editor.destroy() + }) + + it('positions a right AutoFit float without restoring fixed widthPx', async () => { + const editor = await openTable() + expect(setTableAutoFit('contents', 600)(editor.state, editor.view.dispatch)).toBe(true) + expect( + updateSelectedTableAttrs({ + tblFloat: 'right', + tblFloatSource: 'right', + tblFloatXTwips: 3000, + tblFloatWidthPx: 240, + tblFloatEdited: true, + })(editor.state, editor.view.dispatch), + ).toBe(true) + + const table = editor.state.doc.firstChild! + expect(table.attrs).toMatchObject({ + tblAutoFit: 'contents', + widthPx: null, + tblFloatWidthPx: 240, + }) + const spec = editor.schema.nodes.docTable.spec.toDOM!(table) as [string, Record] + expect(spec[1].class).toContain('doc-table-autofit-contents') + expect(spec[1].style).toContain('width:auto') + expect(spec[1].style).toContain( + 'margin-right:max(0px,calc(var(--doc-content-w,100%) - 200.0px - 240.0px))', + ) + editor.destroy() + }) + + it('keeps a pagination-suppressed float in the saved table model', async () => { + const editor = await openTable() + expect( + updateSelectedTableAttrs({ + tblFloat: null, + tblFloatSource: 'right', + tblFloatSuppressed: true, + tblFloatXTwips: 4800, + tblFloatYTwips: 240, + tblFloatEdited: true, + })(editor.state, editor.view.dispatch), + ).toBe(true) + + const model = pmTableToModel(editor.getJSON().content![0] as unknown as PmNode) + expect(model.floatSide).toBe('right') + expect(model.floatPos).toMatchObject({ xTwips: 4800, yTwips: 240 }) + editor.destroy() + }) + + it('applies portable built-in shading and border presets', async () => { + const editor = await openTable() + expect( + applyTablePreset({ + headerFill: 'BDD7EE', + band1Fill: 'DDEBF7', + band2Fill: 'FFFFFF', + borderColor: '9DC3E6', + })(editor.state, editor.view.dispatch), + ).toBe(true) + const table = editor.getJSON().content![0] as unknown as PmNode + expect(table.attrs?.tblStyleId).toBeNull() + expect(table.content![0].content![0].attrs).toMatchObject({ + fill: 'BDD7EE', + borders: { top: { style: 'single', color: '9DC3E6' } }, + }) + expect(table.content![1].content![0].attrs?.fill).toBe('DDEBF7') + const model = pmTableToModel(table) + expect(model.rows[0][0].fill).toBe('BDD7EE') + expect(model.rows[1][0].borders?.left?.color).toBe('9DC3E6') + editor.destroy() + }) + + it('drags a floating table handle into persisted x/y offsets', async () => { + const editor = await openTable() + updateSelectedTableAttrs({ + tblFloat: 'left', + tblFloatSource: 'left', + tblFloatXTwips: 0, + tblFloatYTwips: 0, + tblFloatEdited: true, + })(editor.state, editor.view.dispatch) + let firstCell = -1 + editor.state.doc.descendants((node, pos) => { + if ( + firstCell < 0 && + (node.type.name === 'docTableCell' || node.type.name === 'docTableHeader') + ) { + firstCell = pos + } + }) + vi.spyOn(editor.view, 'posAtCoords').mockReturnValue({ pos: firstCell, inside: firstCell }) + editor.view.dom.dispatchEvent( + new MouseEvent('mousemove', { bubbles: true, clientX: 10, clientY: 10 }), + ) + const handle = + editor.view.dom.parentElement!.querySelector('.doc-table-handle')! + handle.dispatchEvent( + new MouseEvent('mousedown', { bubbles: true, button: 0, clientX: 10, clientY: 10 }), + ) + window.dispatchEvent(new MouseEvent('mousemove', { clientX: 40, clientY: 30 })) + window.dispatchEvent(new MouseEvent('mouseup', { clientX: 40, clientY: 30 })) + + expect((editor.getJSON().content![0] as unknown as PmNode).attrs).toMatchObject({ + tblFloatXTwips: 450, + tblFloatYTwips: 300, + tblFloatEdited: true, + }) + editor.destroy() + }) +}) diff --git a/apps/markdown/src/renderer/components/AiAskPopover.tsx b/apps/markdown/src/renderer/components/AiAskPopover.tsx index edd00c2..c71857c 100644 --- a/apps/markdown/src/renderer/components/AiAskPopover.tsx +++ b/apps/markdown/src/renderer/components/AiAskPopover.tsx @@ -279,6 +279,22 @@ export function AiAskPopover({ } }} > +
    {t(open.mode === 'edit' ? 'aiAskEditTitle' : 'aiAskTitle')}
    diff --git a/apps/markdown/src/renderer/styles.css b/apps/markdown/src/renderer/styles.css index b6d9b78..40bceb3 100644 --- a/apps/markdown/src/renderer/styles.css +++ b/apps/markdown/src/renderer/styles.css @@ -2397,7 +2397,35 @@ div.tiptap-mathematics-render { box-shadow: var(--md-pop-shadow); } +.ai-ask-pop-close { + position: absolute; + top: 10px; + right: 10px; + display: inline-flex; + align-items: center; + justify-content: center; + width: 20px; + height: 20px; + padding: 0; + border: none; + border-radius: 6px; + background: transparent; + color: var(--text-dim); + cursor: pointer; +} + +.ai-ask-pop-close:hover { + background: var(--hover); + color: var(--text); +} + +.ai-ask-pop-close:focus-visible { + outline: 2px solid var(--accent); + outline-offset: 1px; +} + .ai-ask-pop-title { + padding-right: 24px; font-size: 12.5px; font-weight: 600; color: var(--text); diff --git a/apps/pdf/src/renderer/AiAskPopover.tsx b/apps/pdf/src/renderer/AiAskPopover.tsx index 9a5586f..8f74771 100644 --- a/apps/pdf/src/renderer/AiAskPopover.tsx +++ b/apps/pdf/src/renderer/AiAskPopover.tsx @@ -103,6 +103,22 @@ export function AiAskPopover({ } }} > +
    {t('aiAskTitle')}
    {shortExcerpt && (
    diff --git a/apps/pdf/src/renderer/App.tsx b/apps/pdf/src/renderer/App.tsx index e4a77f6..857b05d 100644 --- a/apps/pdf/src/renderer/App.tsx +++ b/apps/pdf/src/renderer/App.tsx @@ -5299,8 +5299,11 @@ export default function App() { const onWheel = (e: WheelEvent) => { if (!e.ctrlKey && !e.metaKey) return e.preventDefault() - if (e.deltaY < 0) zoomIn() - else zoomOut() + if (e.deltaY === 0) return + fitModeRef.current = null + // Match Docs: accumulate against the latest queued scale and avoid the + // per-event scroll anchoring that makes a continuous pinch oscillate. + setScale((current) => clampScale(current - e.deltaY * 0.006)) } el.addEventListener('wheel', onWheel, { passive: false }) return () => el.removeEventListener('wheel', onWheel) diff --git a/apps/pdf/src/renderer/styles.css b/apps/pdf/src/renderer/styles.css index 4c12a45..f74832f 100644 --- a/apps/pdf/src/renderer/styles.css +++ b/apps/pdf/src/renderer/styles.css @@ -3553,7 +3553,35 @@ button { box-shadow: var(--pdf-shadow-popup); } +.ai-ask-pop-close { + position: absolute; + top: 10px; + right: 10px; + display: inline-flex; + align-items: center; + justify-content: center; + width: 20px; + height: 20px; + padding: 0; + border: none; + border-radius: 6px; + background: transparent; + color: var(--text-dim); + cursor: pointer; +} + +.ai-ask-pop-close:hover { + background: var(--hover); + color: var(--text); +} + +.ai-ask-pop-close:focus-visible { + outline: 2px solid var(--accent); + outline-offset: 1px; +} + .ai-ask-pop-title { + padding-right: 24px; font-size: 12.5px; font-weight: 600; color: var(--text); diff --git a/apps/pdf/src/renderer/view-config.ts b/apps/pdf/src/renderer/view-config.ts index f9dc0ad..529add7 100644 --- a/apps/pdf/src/renderer/view-config.ts +++ b/apps/pdf/src/renderer/view-config.ts @@ -1,7 +1,7 @@ // cmaps/standard fonts/wasm are statically copied by the build into pdfjs/ of the renderer output (same path on the dev server) export const ASSET_BASE = new URL('pdfjs/', document.baseURI).href -export const ZOOM_STEPS = [0.5, 0.67, 0.8, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4] +export const ZOOM_STEPS = [0.1, 0.25, 0.5, 0.67, 0.8, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4] export const MIN_SCALE = ZOOM_STEPS[0] export const MAX_SCALE = ZOOM_STEPS[ZOOM_STEPS.length - 1] export const PAGE_GAP = 16 diff --git a/apps/sheets/native/xlsx-engine/src/lib.rs b/apps/sheets/native/xlsx-engine/src/lib.rs index fbc9fa8..f2dc2c6 100644 --- a/apps/sheets/native/xlsx-engine/src/lib.rs +++ b/apps/sheets/native/xlsx-engine/src/lib.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::fs::{self, File}; use std::io::{BufReader, BufWriter, Read, Write}; use std::path::{Path, PathBuf}; @@ -16,6 +16,7 @@ use zip::ZipArchive; pub mod archive; pub mod convert; pub mod recalc; +mod richdata; mod shared_formulas; mod visuals; @@ -101,6 +102,9 @@ pub struct SheetMetadata { /// instead of auto-fitting them on open. pub default_row_height_fixed: bool, pub default_column_width: Option, + /// sheetFormatPr/@baseColWidth — Excel derives its built-in default + /// column width from this (default 8) when defaultColWidth is absent. + pub base_column_width: Option, pub freeze: Option, pub hidden: bool, pub tab_color: Option, @@ -121,6 +125,26 @@ pub struct SheetMetadata { /// x14 sparkline groups from the worksheet extLst. #[serde(skip_serializing_if = "Vec::is_empty")] pub sparklines: Vec, + /// Saved `_xlnm.Print_Area` formula for this sheet (workbook.xml). + #[serde(skip_serializing_if = "Option::is_none")] + pub print_area: Option, + /// Saved `_xlnm.Print_Titles` formula for this sheet. + #[serde(skip_serializing_if = "Option::is_none")] + pub print_titles: Option, + /// In-cell rich-value pictures ("place picture in cell"). + #[serde(skip_serializing_if = "Vec::is_empty")] + pub cell_images: Vec, +} + +#[derive(Clone, Debug, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct CellImageInfo { + /// Media lookup key for the read_media command, like a visual id. + pub id: String, + pub row: usize, + pub column: usize, + #[serde(skip_serializing)] + pub media_path: String, } #[derive(Clone, Debug, Serialize)] @@ -160,6 +184,10 @@ pub struct PivotTableInfo { pub whole_table_fill: Option, #[serde(skip_serializing_if = "Option::is_none")] pub stripe_fill: Option, + /// pivotTableStyleInfo carries a style name: band rows stay bold even + /// when the resolved palette has no fills (Light 1-7, stripes off). + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub styled: bool, /// location/@firstDataRow — rows above it inside the output ref are /// header rows. #[serde(skip_serializing_if = "is_zero")] @@ -433,6 +461,9 @@ pub struct RangeResult { pub col_breaks: Vec, /// protectedRanges entries; sheet-wide, complete-only. pub protected_ranges: Vec, + /// Saved print settings; sheet-wide, complete-only, None when the sheet + /// declares none. + pub page_setup: Option, pub indexed_through_row: Option, pub indexing_complete: bool, } @@ -445,6 +476,48 @@ pub struct SheetProtectionInfo { pub has_password: bool, } +/// Inches, from ``. +#[derive(Clone, Copy, Debug, PartialEq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct PageMarginsInfo { + pub left: f64, + pub right: f64, + pub top: f64, + pub bottom: f64, + pub header: f64, + pub footer: f64, +} + +/// The sheet's saved print settings (printOptions / pageMargins / pageSetup / +/// sheetPr/pageSetUpPr / headerFooter), sanitized to Excel's valid ranges. +#[derive(Clone, Debug, Default, PartialEq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct PagePrintInfo { + #[serde(skip_serializing_if = "Option::is_none")] + pub orientation: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub paper_size: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub scale: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub fit_to_width: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub fit_to_height: Option, + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub fit_to_page: bool, + #[serde(skip_serializing_if = "Option::is_none")] + pub margins: Option, + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub print_gridlines: bool, + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub print_headings: bool, + /// Excel-encoded odd header/footer text (&L/&C/&R sections, field codes). + #[serde(skip_serializing_if = "Option::is_none")] + pub odd_header: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub odd_footer: Option, +} + #[derive(Clone, Debug, Serialize)] #[serde(rename_all = "camelCase")] pub struct ProtectedRangeInfo { @@ -607,6 +680,8 @@ impl WorkbookSessions { short_date_format, )?; let custom_table_styles = read_custom_table_styles(&mut archive, &dxf_styles); + let rich_value_images = richdata::read_rich_value_images(&mut archive); + let mut cell_image_count = 0usize; let mut sheets = Vec::with_capacity(declarations.len()); let mut runtimes = Vec::with_capacity(declarations.len()); let mut visual_sources = Vec::with_capacity(declarations.len()); @@ -643,6 +718,12 @@ impl WorkbookSessions { }) .collect(); let sparklines = read_sheet_sparklines(&mut archive, &worksheet_path)?; + let cell_images = read_sheet_cell_images( + &mut archive, + &worksheet_path, + &rich_value_images, + &mut cell_image_count, + )?; let pivot_infos = visuals::read_pivot_tables(&mut archive, &worksheet_path)?; let pivot_ranges = pivot_infos .iter() @@ -657,6 +738,10 @@ impl WorkbookSessions { header_font_color: palette.header_font_color, whole_table_fill: palette.whole_table_fill, stripe_fill: palette.stripe_fill.filter(|_| info.show_row_stripes), + styled: info + .style_name + .as_deref() + .is_some_and(|name| !name.is_empty()), path: info.path, cache_path: info.cache_path, output_ref: info.output_ref, @@ -684,6 +769,7 @@ impl WorkbookSessions { default_row_height: dimensions.default_row_height, default_row_height_fixed: dimensions.default_row_height_fixed, default_column_width: dimensions.default_column_width, + base_column_width: dimensions.base_column_width, freeze: dimensions.freeze, hidden: declaration.hidden, tab_color: dimensions.tab_color, @@ -696,6 +782,9 @@ impl WorkbookSessions { pivot_ranges, pivot_tables, sparklines, + print_area: None, + print_titles: None, + cell_images, }); visual_sources.push(SheetVisualSource { sheet_id: id, @@ -720,7 +809,13 @@ impl WorkbookSessions { ); let visual_objects = visuals::read_visual_objects(&mut archive, &visual_sources, &color_context)?; - let defined_names = read_defined_names(&mut archive)?; + let (defined_names, print_names) = read_defined_names(&mut archive)?; + for (sheet_index, sheet) in sheets.iter_mut().enumerate() { + if let Some(names) = print_names.get(&sheet_index) { + sheet.print_area = names.area.clone(); + sheet.print_titles = names.titles.clone(); + } + } let session_id = Uuid::new_v4().to_string(); let cache_directory = std::env::temp_dir().join(format!("genspark-ai-excel-{session_id}")); @@ -831,7 +926,20 @@ impl WorkbookSessions { .visuals .iter() .find(|visual| visual.id == visual_id) - .and_then(|visual| visual.media_path.as_deref()) + .and_then(|visual| { + visual + .media_path + .as_deref() + .or(visual.fill_media_path.as_deref()) + }) + .or_else(|| { + session + .sheets + .iter() + .flat_map(|sheet| sheet.cell_images.iter()) + .find(|image| image.id == visual_id) + .map(|image| image.media_path.as_str()) + }) .ok_or_else(|| SidecarError::InvalidRequest("Unknown workbook image.".into()))?; let file = File::open(&session.path)?; let mut archive = ZipArchive::new(file)?; @@ -878,6 +986,15 @@ impl WorkbookSession { let shared_strings = Arc::clone(&self.shared_strings); let styled_xfs = Arc::clone(&self.styled_xfs); let color_context = Arc::clone(&self.color_context); + // The capped overlay list is the single source of truth: only cells + // that actually got a picture record lose their cached error. + let rich_image_cells: Arc> = Arc::new( + self.sheets[sheet_index] + .cell_images + .iter() + .map(|image| (image.row, image.column)) + .collect(), + ); let cancelled = Arc::clone(&self.cancelled); let handle = thread::Builder::new() .name(format!("xlsx-index-{sheet_index}")) @@ -890,6 +1007,7 @@ impl WorkbookSession { &shared_strings, &styled_xfs, &color_context, + &rich_image_cells, &state, &cancelled, ); @@ -998,6 +1116,11 @@ impl WorkbookSession { } else { Vec::new() }; + let page_setup = if indexing_complete { + index.page_setup.clone() + } else { + None + }; let first_chunk = range.start_row / CHUNK_ROW_COUNT; let last_available_row = indexed_through_row .map(|row| row.min(range.end_row)) @@ -1042,6 +1165,7 @@ impl WorkbookSession { row_breaks, col_breaks, protected_ranges, + page_setup, indexed_through_row, indexing_complete, }) @@ -1114,6 +1238,7 @@ struct SheetIndex { row_breaks: Vec, col_breaks: Vec, protected_ranges: Vec, + page_setup: Option, /// Formula cells collected while indexing, capped at MAX_FORMULA_CELLS. formula_cells: Vec, formula_truncated: bool, @@ -1277,6 +1402,7 @@ struct SheetDimensions { default_row_height: Option, default_row_height_fixed: bool, default_column_width: Option, + base_column_width: Option, freeze: Option, tab_color: Option, show_grid_lines: bool, @@ -1304,6 +1430,7 @@ fn read_sheet_dimensions( let mut default_row_height = None; let mut default_row_height_fixed = false; let mut default_column_width = None; + let mut base_column_width = None; let mut freeze = None; let mut tab_color = None; let mut show_grid_lines = true; @@ -1363,6 +1490,9 @@ fn read_sheet_dimensions( default_column_width = attribute_value(&reader, &element, b"defaultColWidth")? .and_then(|value| value.parse::().ok()) .filter(|value| *value > 0.0); + base_column_width = attribute_value(&reader, &element, b"baseColWidth")? + .and_then(|value| value.parse::().ok()) + .filter(|value| *value > 0.0); } Event::Start(element) | Event::Empty(element) if element.local_name().as_ref() == b"pane" => @@ -1441,6 +1571,7 @@ fn read_sheet_dimensions( default_row_height, default_row_height_fixed, default_column_width, + base_column_width, freeze, tab_color, show_grid_lines, @@ -1485,6 +1616,7 @@ fn read_sheet_dimensions( default_row_height, default_row_height_fixed, default_column_width, + base_column_width, freeze, tab_color, show_grid_lines, @@ -1665,6 +1797,7 @@ fn index_worksheet( shared_strings: &[SharedString], styled_xfs: &[bool], colors: &ColorContext, + rich_image_cells: &HashSet<(usize, usize)>, state: &Arc<(Mutex, Condvar)>, cancelled: &AtomicBool, ) -> Result<(), SidecarError> { @@ -1715,6 +1848,12 @@ fn index_worksheet( let mut dv_rule: Option = None; let mut in_dv_formula = false; let mut data_validations: Vec = Vec::new(); + let mut page_print = PagePrintInfo::default(); + // customSheetView carries its own print elements; only sheet-level ones + // are what Excel prints. + let mut in_custom_sheet_views = false; + let mut in_odd_header = false; + let mut in_odd_footer = false; loop { if cancelled.load(Ordering::Acquire) { @@ -1775,7 +1914,9 @@ fn index_worksheet( )?; chunk_index = cell_chunk; } - if let Some(cell) = builder.finish(shared_strings, styled_xfs)? { + if let Some(cell) = + builder.finish(shared_strings, styled_xfs, rich_image_cells)? + { chunk.cells.push(cell); } } @@ -1854,6 +1995,16 @@ fn index_worksheet( run.text.push_str(&decoded); } } + } else if in_odd_header { + page_print + .odd_header + .get_or_insert_with(String::new) + .push_str(&decoded); + } else if in_odd_footer { + page_print + .odd_footer + .get_or_insert_with(String::new) + .push_str(&decoded); } else if in_cf_formula { if let Some(formula) = cf_rule.as_mut().and_then(|rule| rule.formulas.last_mut()) @@ -1885,6 +2036,16 @@ fn index_worksheet( run.text.push_str(&decoded); } } + } else if in_odd_header { + page_print + .odd_header + .get_or_insert_with(String::new) + .push_str(&decoded); + } else if in_odd_footer { + page_print + .odd_footer + .get_or_insert_with(String::new) + .push_str(&decoded); } else if in_cf_formula { if let Some(formula) = cf_rule.as_mut().and_then(|rule| rule.formulas.last_mut()) @@ -1932,7 +2093,9 @@ fn index_worksheet( )?; chunk_index = cell_chunk; } - if let Some(cell) = builder.finish(shared_strings, styled_xfs)? { + if let Some(cell) = + builder.finish(shared_strings, styled_xfs, rich_image_cells)? + { if cell.formula.is_some() { pending_formulas.push(cell.clone()); } @@ -1990,6 +2153,90 @@ fn index_worksheet( has_password, }); } + Event::Start(element) if element.local_name().as_ref() == b"customSheetViews" => { + in_custom_sheet_views = true; + } + Event::End(element) if element.local_name().as_ref() == b"customSheetViews" => { + in_custom_sheet_views = false; + } + Event::Start(element) | Event::Empty(element) + if element.local_name().as_ref() == b"pageSetUpPr" && !in_custom_sheet_views => + { + page_print.fit_to_page = attribute_value(&reader, &element, b"fitToPage")? + .is_some_and(|value| value == "1" || value == "true"); + } + Event::Start(element) | Event::Empty(element) + if element.local_name().as_ref() == b"printOptions" && !in_custom_sheet_views => + { + page_print.print_gridlines = attribute_value(&reader, &element, b"gridLines")? + .is_some_and(|value| value == "1" || value == "true"); + page_print.print_headings = attribute_value(&reader, &element, b"headings")? + .is_some_and(|value| value == "1" || value == "true"); + } + Event::Start(element) | Event::Empty(element) + if element.local_name().as_ref() == b"pageMargins" && !in_custom_sheet_views => + { + if let ( + Some(left), + Some(right), + Some(top), + Some(bottom), + Some(header), + Some(footer), + ) = ( + margin_attribute(&reader, &element, b"left")?, + margin_attribute(&reader, &element, b"right")?, + margin_attribute(&reader, &element, b"top")?, + margin_attribute(&reader, &element, b"bottom")?, + margin_attribute(&reader, &element, b"header")?, + margin_attribute(&reader, &element, b"footer")?, + ) { + page_print.margins = Some(PageMarginsInfo { + left, + right, + top, + bottom, + header, + footer, + }); + } + } + Event::Start(element) | Event::Empty(element) + if element.local_name().as_ref() == b"pageSetup" && !in_custom_sheet_views => + { + page_print.orientation = attribute_value(&reader, &element, b"orientation")? + .filter(|value| value == "portrait" || value == "landscape"); + page_print.paper_size = attribute_value(&reader, &element, b"paperSize")? + .and_then(|value| value.parse::().ok()) + .filter(|value| (1..=256).contains(value)); + // Excel's valid scale range; anything else means "as saved by + // a broken writer" and prints at 100. + page_print.scale = attribute_value(&reader, &element, b"scale")? + .and_then(|value| value.parse::().ok()) + .filter(|value| (10..=400).contains(value)); + page_print.fit_to_width = attribute_value(&reader, &element, b"fitToWidth")? + .and_then(|value| value.parse::().ok()) + .filter(|value| *value <= 32_767); + page_print.fit_to_height = attribute_value(&reader, &element, b"fitToHeight")? + .and_then(|value| value.parse::().ok()) + .filter(|value| *value <= 32_767); + } + Event::Start(element) + if element.local_name().as_ref() == b"oddHeader" && !in_custom_sheet_views => + { + in_odd_header = true; + } + Event::End(element) if element.local_name().as_ref() == b"oddHeader" => { + in_odd_header = false; + } + Event::Start(element) + if element.local_name().as_ref() == b"oddFooter" && !in_custom_sheet_views => + { + in_odd_footer = true; + } + Event::End(element) if element.local_name().as_ref() == b"oddFooter" => { + in_odd_footer = false; + } Event::Start(element) if element.local_name().as_ref() == b"rowBreaks" => { in_row_breaks = true; } @@ -2290,6 +2537,12 @@ fn index_worksheet( index.row_breaks = row_breaks; index.col_breaks = col_breaks; index.protected_ranges = protected_ranges; + for text in [&mut page_print.odd_header, &mut page_print.odd_footer] { + if let Some(value) = text { + truncate_utf16_units(value, 500); + } + } + index.page_setup = (page_print != PagePrintInfo::default()).then_some(page_print); index.data_validations = data_validations; condition.notify_all(); drop(index); @@ -2364,11 +2617,52 @@ fn parse_cf_rule( }) } -fn read_defined_names(archive: &mut ZipArchive) -> Result, SidecarError> { +/// Truncates in place to at most `max_units` UTF-16 code units — the wire +/// caps (zod / preload) measure JavaScript string length, where non-BMP +/// characters count as two. +fn truncate_utf16_units(value: &mut String, max_units: usize) { + let mut units = 0usize; + for (byte_index, character) in value.char_indices() { + units += character.len_utf16(); + if units > max_units { + value.truncate(byte_index); + return; + } + } +} + +fn margin_attribute( + reader: &Reader, + element: &BytesStart<'_>, + name: &[u8], +) -> Result, SidecarError> { + Ok(attribute_value(reader, element, name)? + .and_then(|value| value.parse::().ok()) + .filter(|value| value.is_finite() && (0.0..=10.0).contains(value))) +} + +/// Sheet-scoped `_xlnm.Print_Area` / `_xlnm.Print_Titles` formulas, keyed by +/// localSheetId (workbook sheet order). +#[derive(Default)] +struct SheetPrintNames { + area: Option, + titles: Option, +} + +fn read_defined_names( + archive: &mut ZipArchive, +) -> Result<(Vec, HashMap), SidecarError> { let xml = read_zip_string(archive, "xl/workbook.xml")?; let mut reader = Reader::from_str(&xml); let mut names = Vec::new(); - let mut current: Option = None; + let mut print_names: HashMap = HashMap::new(); + struct Pending { + name: String, + hidden: bool, + sheet_index: Option, + formula: String, + } + let mut current: Option = None; loop { match reader.read_event()? { Event::Start(element) if element.local_name().as_ref() == b"definedName" => { @@ -2377,15 +2671,12 @@ fn read_defined_names(archive: &mut ZipArchive) -> Result .is_some_and(|value| value == "1" || value == "true"); let sheet_index = attribute_value(&reader, &element, b"localSheetId")? .and_then(|value| value.parse::().ok()); - // _xlnm.* built-ins and hidden names stay file-only (the save - // preserves them verbatim; the editor never models them). - current = (!name.is_empty() && !name.starts_with("_xlnm") && !hidden).then_some( - DefinedName { - name, - formula: String::new(), - sheet_index, - }, - ); + current = (!name.is_empty()).then_some(Pending { + name, + hidden, + sheet_index, + formula: String::new(), + }); } Event::Text(text) => { if let Some(defined) = &mut current { @@ -2403,10 +2694,33 @@ fn read_defined_names(archive: &mut ZipArchive) -> Result } } Event::End(element) if element.local_name().as_ref() == b"definedName" => { - if let Some(mut defined) = current.take() { - if !defined.formula.is_empty() { - defined.formula = strip_future_function_markers(&defined.formula); - names.push(defined); + if let Some(defined) = current.take() { + if defined.formula.is_empty() { + continue; + } + if defined.name.starts_with("_xlnm") { + // Print names feed the PDF export; other _xlnm.* + // built-ins stay file-only. Oversized refs exceed the + // wire cap and are dropped. + if defined.formula.len() > 2_000 { + continue; + } + if let Some(sheet_index) = defined.sheet_index { + let entry = print_names.entry(sheet_index).or_default(); + if defined.name == "_xlnm.Print_Area" { + entry.area = Some(defined.formula); + } else if defined.name == "_xlnm.Print_Titles" { + entry.titles = Some(defined.formula); + } + } + // Hidden names stay file-only (the save preserves them + // verbatim; the editor never models them). + } else if !defined.hidden { + names.push(DefinedName { + name: defined.name, + formula: strip_future_function_markers(&defined.formula), + sheet_index: defined.sheet_index, + }); } } } @@ -2414,7 +2728,7 @@ fn read_defined_names(archive: &mut ZipArchive) -> Result _ => {} } } - Ok(names) + Ok((names, print_names)) } const DEFAULT_ACCENTS: [(u8, u8, u8); 6] = [ @@ -2574,9 +2888,8 @@ fn read_custom_table_styles( palettes } -/// Light-family pivot styles paint header and grand-total bands with a light -/// tint of the style's accent; Medium/Dark families are left unpainted until -/// their (solid-fill, white-text) bands are modelled. +/// Resolved fills for a built-in pivot style; see `pivot_style_palette` for +/// the per-family calibration. #[derive(Clone, Debug, Default)] struct PivotStylePalette { header_fill: Option, @@ -2585,27 +2898,44 @@ struct PivotStylePalette { stripe_fill: Option, } -/// Built-in pivot style bands. Light families paint the header band only -/// (tint 0.8 of the base). PivotStyleDark1 is pixel-calibrated against Excel -/// (header/grand-total tint 0.5 with white bold text, body 0.65, stripe -/// 0.75 of dk1); the accent Dark members and the Medium block stay unpainted -/// until they get their own calibration pass. +/// Built-in pivot style bands, pixel-calibrated against Excel ref renders. +/// Light 1-7 leave the header unfilled (RelationalPivotB6, Light2) while +/// Light 8+ tint it 0.8 (POI 54436, Light16); every Light stripe is tint 0.8 +/// of the base. Medium headers are the solid base with white bold text +/// (aspose_sample1, Medium9); Medium stripes/body and the accent Dark +/// members stay unpainted until they get their own calibration pass. +/// PivotStyleDark1: header/grand-total tint 0.5 with white bold text, body +/// 0.65, stripe 0.75 of dk1. fn pivot_style_palette(style_name: Option<&str>, colors: &ColorContext) -> PivotStylePalette { let Some(name) = style_name else { return PivotStylePalette::default(); }; + let family_base = |number: usize| { + let accent_index = number.saturating_sub(1) % 7; + if accent_index == 0 { + visuals::theme_dark1(colors).unwrap_or((0x00, 0x00, 0x00)) + } else { + visuals::theme_accent(colors, accent_index).unwrap_or(DEFAULT_ACCENTS[accent_index - 1]) + } + }; if let Some(rest) = name.strip_prefix("PivotStyleLight") { let Some(number) = rest.parse::().ok() else { return PivotStylePalette::default(); }; - let accent_index = number.saturating_sub(1) % 7; - let base = if accent_index == 0 { - visuals::theme_dark1(colors).unwrap_or((0x00, 0x00, 0x00)) - } else { - visuals::theme_accent(colors, accent_index).unwrap_or(DEFAULT_ACCENTS[accent_index - 1]) + let band = visuals::tint_to_hex(family_base(number), 0.8); + return PivotStylePalette { + header_fill: (number > 7).then(|| band.clone()), + stripe_fill: Some(band), + ..PivotStylePalette::default() + }; + } + if let Some(rest) = name.strip_prefix("PivotStyleMedium") { + let Some(number) = rest.parse::().ok() else { + return PivotStylePalette::default(); }; return PivotStylePalette { - header_fill: Some(visuals::tint_to_hex(base, 0.8)), + header_fill: Some(rgb_hex(family_base(number))), + header_font_color: Some("#FFFFFF".into()), ..PivotStylePalette::default() }; } @@ -2939,6 +3269,71 @@ fn argb_to_hex(value: &str) -> Option { .then(|| format!("#{}", hex.to_ascii_uppercase())) } +const MAX_CELL_IMAGES: usize = 500; + +/// Attribute-only scan for `` cells whose value metadata resolves to +/// an image rich value. Runs at open time and only when the workbook carries +/// richData image parts at all. +fn read_sheet_cell_images( + archive: &mut ZipArchive, + worksheet_path: &str, + images_by_vm: &HashMap, + image_count: &mut usize, +) -> Result, SidecarError> { + if images_by_vm.is_empty() { + return Ok(Vec::new()); + } + let entry = zip_entry(archive, worksheet_path)?; + let mut reader = Reader::from_reader(BufReader::new(entry)); + let mut buffer = Vec::new(); + let mut current_row = 0usize; + let mut first_row = true; + let mut next_column = 0usize; + let mut cell_images = Vec::new(); + loop { + match reader.read_event_into(&mut buffer)? { + Event::Start(element) | Event::Empty(element) + if element.local_name().as_ref() == b"row" => + { + current_row = attribute_value(&reader, &element, b"r")? + .and_then(|value| value.parse::().ok()) + .filter(|value| *value > 0) + .map(|value| value - 1) + .unwrap_or(if first_row { 0 } else { current_row + 1 }); + first_row = false; + next_column = 0; + } + Event::Start(element) | Event::Empty(element) + if element.local_name().as_ref() == b"c" => + { + let (row, column) = match attribute_value(&reader, &element, b"r")? { + Some(address) => parse_address(&address)?, + None => (current_row, next_column), + }; + next_column = column + 1; + let media_path = attribute_value(&reader, &element, b"vm")? + .and_then(|value| value.parse::().ok()) + .and_then(|vm| images_by_vm.get(&vm)); + if let Some(media_path) = media_path { + if cell_images.len() < MAX_CELL_IMAGES { + cell_images.push(CellImageInfo { + id: format!("cell-image-{image_count}"), + row, + column, + media_path: media_path.clone(), + }); + *image_count += 1; + } + } + } + Event::Eof => break, + _ => {} + } + buffer.clear(); + } + Ok(cell_images) +} + fn read_sheet_sparklines( archive: &mut ZipArchive, worksheet_path: &str, @@ -3257,6 +3652,7 @@ impl CellBuilder { self, shared_strings: &[SharedString], styled_xfs: &[bool], + rich_image_cells: &HashSet<(usize, usize)>, ) -> Result, SidecarError> { let formula = if self.formula.is_empty() { None @@ -3289,6 +3685,9 @@ impl CellBuilder { normalize_line_endings(&mut inline_text); Some(CellValue::String(inline_text)) } + // An error cell hosting an in-cell picture record renders as the + // picture, not as its cached #VALUE! placeholder. + Some("e") if rich_image_cells.contains(&(self.row, self.column)) => None, Some("str") | Some("e") => { let mut raw_value = self.raw_value; normalize_line_endings(&mut raw_value); @@ -3601,6 +4000,7 @@ mod tests { default_row_height: None, default_row_height_fixed: false, default_column_width: None, + base_column_width: None, freeze: None, hidden: false, tab_color: None, @@ -3613,6 +4013,9 @@ mod tests { pivot_ranges: Vec::new(), pivot_tables: Vec::new(), sparklines: Vec::new(), + print_area: None, + print_titles: None, + cell_images: Vec::new(), }; let range = CellRange { start_row: 0, @@ -3759,6 +4162,7 @@ mod tests { default_row_height: None, default_row_height_fixed: false, default_column_width: None, + base_column_width: None, freeze: None, hidden: false, tab_color: None, @@ -3771,11 +4175,215 @@ mod tests { pivot_ranges: Vec::new(), pivot_tables: Vec::new(), sparklines: Vec::new(), + print_area: None, + print_titles: None, + cell_images: Vec::new(), }; let json = serde_json::to_string(&sheet).unwrap(); assert!(!json.contains("sparklines")); } + fn rich_data_fixture_entries() -> Vec<(&'static str, &'static str)> { + vec![ + ( + "xl/workbook.xml", + r#""#, + ), + ( + "xl/_rels/workbook.xml.rels", + r#""#, + ), + ( + "xl/worksheets/sheet1.xml", + r#" +#VALUE!#DIV/0! +"#, + ), + ( + "xl/metadata.xml", + r#""#, + ), + ( + "xl/richData/richValueRel.xml", + r#""#, + ), + ( + "xl/richData/_rels/richValueRel.xml.rels", + r#""#, + ), + ( + "xl/richData/rdrichvalue.xml", + r#"05"#, + ), + ( + "xl/richData/rdrichvaluestructure.xml", + r#""#, + ), + ("xl/media/image1.png", "fake-png-bytes"), + ] + } + + #[test] + fn resolves_in_cell_rich_value_pictures() { + let (_dir, path) = open_fixture(&rich_data_fixture_entries()); + let mut sessions = WorkbookSessions::new(); + let metadata = sessions.open(&path).unwrap(); + let images = &metadata.sheets[0].cell_images; + assert_eq!(images.len(), 1); + assert_eq!(images[0].row, 1); + assert_eq!(images[0].column, 1); + assert_eq!(images[0].media_path, "xl/media/image1.png"); + + // The media lookup accepts the cell-image id like a visual id. + let media = sessions + .read_media(&metadata.session_id, &images[0].id) + .unwrap(); + assert_eq!(media.media_type, "image/png"); + + // The cached #VALUE! stays out of the grid; unrelated error cells keep + // their values. + let range = CellRange { + start_row: 0, + end_row: 1, + start_column: 0, + end_column: 2, + }; + let result = sessions + .read_range(&metadata.session_id, "sheet-1", &range) + .unwrap(); + assert!( + !result + .cells + .iter() + .any(|cell| cell.row == 1 && cell.column == 1 && cell.value.is_some()) + ); + assert!(result.cells.iter().any(|cell| { + cell.row == 1 + && cell.column == 2 + && matches!(&cell.value, Some(CellValue::String(text)) if text == "#DIV/0!") + })); + + // The wire payload carries only the lookup fields. + let json = serde_json::to_string(&metadata.sheets[0]).unwrap(); + assert!(json.contains("cellImages")); + assert!(!json.contains("mediaPath")); + } + + /// A valueMetadata bk may carry one rc per metadata type; XLRICHVALUE + /// still resolves when it is not the first record. + #[test] + fn resolves_rich_value_from_a_non_first_metadata_record() { + let metadata = r#""#; + let entries: Vec<(&str, &str)> = rich_data_fixture_entries() + .into_iter() + .map(|(name, content)| { + if name == "xl/metadata.xml" { + (name, metadata) + } else { + (name, content) + } + }) + .collect(); + let (_dir, path) = open_fixture(&entries); + let mut sessions = WorkbookSessions::new(); + let metadata = sessions.open(&path).unwrap(); + let images = &metadata.sheets[0].cell_images; + assert_eq!(images.len(), 1); + assert_eq!((images[0].row, images[0].column), (1, 1)); + assert_eq!(images[0].media_path, "xl/media/image1.png"); + } + + /// Past the per-sheet overlay cap the cell keeps its cached error: a + /// picture cell must render as the image or the error, never blank. + #[test] + fn over_cap_picture_cells_keep_the_cached_error() { + let cells: String = (0..=MAX_CELL_IMAGES) + .map(|index| { + format!( + r#"#VALUE!"#, + column_label(index) + ) + }) + .collect(); + let worksheet = format!( + r#" +{cells} +"# + ); + let entries: Vec<(&str, &str)> = rich_data_fixture_entries() + .into_iter() + .map(|(name, content)| { + if name == "xl/worksheets/sheet1.xml" { + (name, worksheet.as_str()) + } else { + (name, content) + } + }) + .collect(); + let (_dir, path) = open_fixture(&entries); + let mut sessions = WorkbookSessions::new(); + let metadata = sessions.open(&path).unwrap(); + assert_eq!(metadata.sheets[0].cell_images.len(), MAX_CELL_IMAGES); + let range = CellRange { + start_row: 0, + end_row: 0, + start_column: 0, + end_column: MAX_CELL_IMAGES, + }; + let result = sessions + .read_range(&metadata.session_id, "sheet-1", &range) + .unwrap(); + // Within the cap: suppressed. The one past it: error preserved. + assert!( + !result + .cells + .iter() + .any(|cell| cell.column < MAX_CELL_IMAGES && cell.value.is_some()) + ); + assert!(result.cells.iter().any(|cell| { + cell.column == MAX_CELL_IMAGES + && matches!(&cell.value, Some(CellValue::String(text)) if text == "#VALUE!") + })); + } + + fn column_label(mut index: usize) -> String { + let mut label = String::new(); + loop { + label.insert(0, (b'A' + (index % 26) as u8) as char); + if index < 26 { + return label; + } + index = index / 26 - 1; + } + } + + #[test] + fn vm_without_rich_data_parts_keeps_the_cached_error() { + let entries = rich_data_fixture_entries(); + let kept: Vec<(&str, &str)> = entries + .into_iter() + .filter(|(name, _)| !name.contains("richData") && *name != "xl/metadata.xml") + .collect(); + let (_dir, path) = open_fixture(&kept); + let mut sessions = WorkbookSessions::new(); + let metadata = sessions.open(&path).unwrap(); + assert!(metadata.sheets[0].cell_images.is_empty()); + let range = CellRange { + start_row: 0, + end_row: 1, + start_column: 0, + end_column: 2, + }; + let result = sessions + .read_range(&metadata.session_id, "sheet-1", &range) + .unwrap(); + assert!(result.cells.iter().any(|cell| { + cell.row == 1 + && cell.column == 1 + && matches!(&cell.value, Some(CellValue::String(text)) if text == "#VALUE!") + })); + } + fn open_fixture(entries: &[(&str, &str)]) -> (tempfile::TempDir, PathBuf) { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("fixture.xlsx"); @@ -4050,6 +4658,120 @@ mod tests { ); } + #[test] + fn reads_saved_print_settings_and_print_names() { + let (_dir, path) = open_fixture(&[ + ( + "xl/workbook.xml", + r#"'S'!$A$1:$C$4'S'!$1:$2S!$A$1"#, + ), + ( + "xl/_rels/workbook.xml.rels", + r#""#, + ), + ( + "xl/worksheets/sheet1.xml", + r#" + +decoy +1 + + + +&CBudget &A&CSeite &P von &N +"#, + ), + ]); + let mut sessions = WorkbookSessions::new(); + let metadata = sessions.open(&path).unwrap(); + assert_eq!( + metadata.sheets[0].print_area.as_deref(), + Some("'S'!$A$1:$C$4") + ); + assert_eq!( + metadata.sheets[0].print_titles.as_deref(), + Some("'S'!$1:$2") + ); + assert_eq!(metadata.defined_names.len(), 1); + assert_eq!(metadata.defined_names[0].name, "Visible"); + let sheet_id = metadata.sheets[0].id.clone(); + let range = CellRange { + start_row: 0, + end_row: 0, + start_column: 0, + end_column: 0, + }; + let result = loop { + let result = sessions + .read_range(&metadata.session_id, &sheet_id, &range) + .unwrap(); + if result.indexing_complete { + break result; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + }; + let setup = result.page_setup.expect("page setup"); + assert_eq!(setup.orientation.as_deref(), Some("landscape")); + assert_eq!(setup.paper_size, Some(9)); + assert_eq!(setup.scale, Some(65)); + assert_eq!(setup.fit_to_width, Some(2)); + assert_eq!(setup.fit_to_height, Some(0)); + assert!(setup.fit_to_page); + assert!(setup.print_gridlines); + assert!(setup.print_headings); + let margins = setup.margins.expect("margins"); + assert_eq!(margins.left, 0.25); + assert_eq!(margins.footer, 0.4); + assert_eq!(setup.odd_header.as_deref(), Some("&CBudget &A")); + assert_eq!(setup.odd_footer.as_deref(), Some("&CSeite &P von &N")); + } + + /// The wire caps header/footer text at 500 UTF-16 code units (JS string + /// length); an emoji-laden header must come out within that bound or the + /// schema rejects the whole range read. + #[test] + fn caps_header_text_by_utf16_length() { + let emoji_header = "😀".repeat(300); + let worksheet = format!( + r#" +1 +{emoji_header} +"# + ); + let (_dir, path) = open_fixture(&[ + ( + "xl/workbook.xml", + r#""#, + ), + ( + "xl/_rels/workbook.xml.rels", + r#""#, + ), + ("xl/worksheets/sheet1.xml", worksheet.as_str()), + ]); + let mut sessions = WorkbookSessions::new(); + let metadata = sessions.open(&path).unwrap(); + let sheet_id = metadata.sheets[0].id.clone(); + let range = CellRange { + start_row: 0, + end_row: 0, + start_column: 0, + end_column: 0, + }; + let result = loop { + let result = sessions + .read_range(&metadata.session_id, &sheet_id, &range) + .unwrap(); + if result.indexing_complete { + break result; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + }; + let header = result.page_setup.unwrap().odd_header.unwrap(); + assert_eq!(header.encode_utf16().count(), 500); + assert!(header.chars().all(|character| character == '😀')); + } + /// Fully `x:`-prefixed table parts (common .NET writers) must still parse. #[test] fn prefixed_table_part_is_parsed() { @@ -4429,6 +5151,74 @@ mod tests { assert_eq!(shape.line_width, Some(10.0)); } + /// A workbook whose first sheet is a chartsheet keeps that sheet in the + /// tab list (name and order preserved) and its absoluteAnchor-ed chart + /// flows through the visuals pipeline like a worksheet drawing. + #[test] + fn chartsheet_keeps_its_tab_and_renders_its_chart() { + let (_dir, path) = open_fixture(&[ + ( + "xl/workbook.xml", + r#""#, + ), + ( + "xl/_rels/workbook.xml.rels", + r#""#, + ), + ( + "xl/chartsheets/sheet1.xml", + r#""#, + ), + ( + "xl/chartsheets/_rels/sheet1.xml.rels", + r#""#, + ), + ( + "xl/drawings/drawing1.xml", + r#""#, + ), + ( + "xl/drawings/_rels/drawing1.xml.rels", + r#""#, + ), + ( + "xl/charts/chart1.xml", + r#"Bears8"#, + ), + ( + "xl/worksheets/sheet1.xml", + r#"8"#, + ), + ]); + let mut sessions = WorkbookSessions::new(); + let metadata = sessions.open(&path).unwrap(); + let names: Vec<&str> = metadata + .sheets + .iter() + .map(|sheet| sheet.name.as_str()) + .collect(); + assert_eq!(names, ["Chart1", "Sheet1"]); + assert_eq!(metadata.sheets[0].id, "sheet-9"); + let chart = metadata + .visuals + .iter() + .find(|visual| visual.sheet_id == "sheet-9") + .expect("chartsheet chart visual"); + assert_eq!(chart.kind, "chart"); + assert_eq!(chart.chart_path.as_deref(), Some("xl/charts/chart1.xml")); + let chart_types = &chart.chart.as_ref().expect("parsed chart").chart_types; + assert_eq!(chart_types, &["barChart"]); + // absoluteAnchor: top-left cell markers with the xdr:ext as offsets. + assert_eq!(chart.anchor.from_row, 0); + assert_eq!(chart.anchor.from_column, 0); + assert_eq!(chart.anchor.from_row_offset, 0); + assert_eq!(chart.anchor.from_column_offset, 0); + assert_eq!(chart.anchor.to_row, 0); + assert_eq!(chart.anchor.to_column, 0); + assert_eq!(chart.anchor.to_row_offset, 6077107); + assert_eq!(chart.anchor.to_column_offset, 9304587); + } + /// An OLE embed's hidden drawing fallback gets the object's progId as /// its caption so the placeholder reads as an embedded object. #[test] @@ -4611,6 +5401,66 @@ mod tests { assert!(pivot.row_grand_totals); // Light16 -> accent1 tint 0.8; no theme part, so the default accent. assert!(pivot.header_fill.is_some()); + assert!(pivot.styled); + } + + /// Light 1-7 with stripes off resolve no fills but stay styled, so the + /// renderer keeps the header/grand-total bands bold. + #[test] + fn pivot_light_low_stripes_off_stays_styled() { + let (_dir, path) = open_fixture(&[ + ( + "xl/workbook.xml", + r#""#, + ), + ( + "xl/_rels/workbook.xml.rels", + r#""#, + ), + ( + "xl/worksheets/sheet1.xml", + r#"Row Labels"#, + ), + ( + "xl/worksheets/_rels/sheet1.xml.rels", + r#""#, + ), + ( + "xl/pivotTables/pivotTable1.xml", + r#""#, + ), + ]); + let mut sessions = WorkbookSessions::new(); + let metadata = sessions.open(&path).unwrap(); + let pivot = &metadata.sheets[0].pivot_tables[0]; + assert!(pivot.styled); + assert!(pivot.header_fill.is_none()); + assert!(pivot.stripe_fill.is_none()); + assert!(pivot.whole_table_fill.is_none()); + let json = serde_json::to_string(pivot).unwrap(); + assert!(json.contains("\"styled\":true")); + } + + /// Pixel-calibrated pivot palettes (default theme accents): Light 1-7 + /// stripe-only, Light 8+ tinted header, Medium solid header + white text. + #[test] + fn pivot_style_palette_calibrated_bands() { + let colors = ColorContext::default(); + let light2 = pivot_style_palette(Some("PivotStyleLight2"), &colors); + assert!(light2.header_fill.is_none()); + assert_eq!(light2.stripe_fill.as_deref(), Some("#DAE3F3")); + assert!(light2.header_font_color.is_none()); + let light16 = pivot_style_palette(Some("PivotStyleLight16"), &colors); + assert_eq!(light16.header_fill.as_deref(), Some("#DAE3F3")); + assert_eq!(light16.stripe_fill.as_deref(), Some("#DAE3F3")); + let medium9 = pivot_style_palette(Some("PivotStyleMedium9"), &colors); + assert_eq!(medium9.header_fill.as_deref(), Some("#4472C4")); + assert_eq!(medium9.header_font_color.as_deref(), Some("#FFFFFF")); + assert!(medium9.stripe_fill.is_none()); + assert!(medium9.whole_table_fill.is_none()); + // Medium/Dark bodies are uncalibrated; Dark accents stay unpainted. + let dark2 = pivot_style_palette(Some("PivotStyleDark2"), &colors); + assert!(dark2.header_fill.is_none()); } /// rowGrandTotals="0" must reach the renderer as an explicit false. diff --git a/apps/sheets/native/xlsx-engine/src/richdata.rs b/apps/sheets/native/xlsx-engine/src/richdata.rs new file mode 100644 index 0000000..07871b0 --- /dev/null +++ b/apps/sheets/native/xlsx-engine/src/richdata.rs @@ -0,0 +1,175 @@ +//! Excel in-cell rich-value pictures ("place picture in cell"). The cell +//! carries a `vm` index and a cached #VALUE! error; the picture itself lives +//! behind xl/metadata.xml → xl/richData/richValueRel.xml → xl/media/*. + +use std::collections::HashMap; +use std::fs::File; + +use roxmltree::Node; +use zip::ZipArchive; + +use crate::SidecarError; +use crate::visuals::{parse_document, read_optional_xml, read_relationships, resolve_part_target}; + +/// `vm` attribute value (1-based valueMetadata record index) → media part +/// path, for records whose rich value is a `_localImage`. Absent or +/// malformed richData parts degrade to "no in-cell pictures". +pub fn read_rich_value_images(archive: &mut ZipArchive) -> HashMap { + read_rich_value_images_inner(archive).unwrap_or_default() +} + +fn read_rich_value_images_inner( + archive: &mut ZipArchive, +) -> Result, SidecarError> { + let workbook_rels = read_relationships(archive, "xl/workbook.xml")?; + let part_by_type = |suffix: &str| { + workbook_rels + .values() + .find(|relationship| relationship.relationship_type.ends_with(suffix)) + .map(|relationship| resolve_part_target("xl/workbook.xml", &relationship.target)) + .transpose() + }; + let (Some(metadata_path), Some(rel_part_path), Some(value_path), Some(structure_path)) = ( + part_by_type("/sheetMetadata")?, + part_by_type("/richValueRel")?, + part_by_type("/rdRichValue")?, + part_by_type("/rdRichValueStructure")?, + ) else { + return Ok(HashMap::new()); + }; + + // Structure index → position of the local-image key among its keys. + let Some(structure_xml) = read_optional_xml(archive, &structure_path)? else { + return Ok(HashMap::new()); + }; + let structure_doc = parse_document(&structure_xml, &structure_path)?; + let structures: Vec> = structure_doc + .descendants() + .filter(|node| node.has_tag_name("s")) + .map(|node| { + if node.attribute("t") != Some("_localImage") { + return None; + } + node.children() + .filter(|child| child.has_tag_name("k")) + .position(|key| key.attribute("n") == Some("_rvRel:LocalImageIdentifier")) + }) + .collect(); + + // Rich value index → richValueRel entry index (image rich values only). + let Some(value_xml) = read_optional_xml(archive, &value_path)? else { + return Ok(HashMap::new()); + }; + let value_doc = parse_document(&value_xml, &value_path)?; + let rich_values: Vec> = value_doc + .descendants() + .filter(|node| node.has_tag_name("rv")) + .map(|node| { + let structure = node.attribute("s")?.parse::().ok()?; + let key_position = (*structures.get(structure)?)?; + let value = node + .children() + .filter(|child| child.has_tag_name("v")) + .nth(key_position)?; + value.text()?.trim().parse::().ok() + }) + .collect(); + + // richValueRel entry index → media part path (through the part's rels). + let Some(rel_xml) = read_optional_xml(archive, &rel_part_path)? else { + return Ok(HashMap::new()); + }; + let rel_doc = parse_document(&rel_xml, &rel_part_path)?; + let part_rels = read_relationships(archive, &rel_part_path)?; + let media_paths: Vec> = rel_doc + .descendants() + .filter(|node| node.has_tag_name("rel")) + .map(|node| { + let id = relationship_id(node)?; + let relationship = part_rels.get(&id)?; + resolve_part_target(&rel_part_path, &relationship.target).ok() + }) + .collect(); + + // valueMetadata record index (vm is that plus one) → media part path. + let Some(metadata_xml) = read_optional_xml(archive, &metadata_path)? else { + return Ok(HashMap::new()); + }; + let metadata_doc = parse_document(&metadata_xml, &metadata_path)?; + let type_names: Vec = metadata_doc + .descendants() + .filter(|node| node.has_tag_name("metadataType")) + .map(|node| node.attribute("name").unwrap_or_default().to_owned()) + .collect(); + // futureMetadata bk records carry the rich value index (xlrd:rvb i=); + // a record without one falls back to the identity mapping. + let future_rich_value: Vec> = metadata_doc + .descendants() + .find(|node| { + node.has_tag_name("futureMetadata") && node.attribute("name") == Some("XLRICHVALUE") + }) + .map(|future| { + future + .descendants() + .filter(|node| node.has_tag_name("bk")) + .map(|record| { + record + .descendants() + .find(|node| node.has_tag_name("rvb")) + .and_then(|rvb| rvb.attribute("i")) + .and_then(|index| index.parse::().ok()) + }) + .collect() + }) + .unwrap_or_default(); + + let mut by_vm = HashMap::new(); + let Some(value_metadata) = metadata_doc + .descendants() + .find(|node| node.has_tag_name("valueMetadata")) + else { + return Ok(by_vm); + }; + for (index, record) in value_metadata + .children() + .filter(|node| node.has_tag_name("bk")) + .enumerate() + { + // rc t= is a 1-based metadataTypes index; a bk may carry one rc per + // metadata type, so find the XLRICHVALUE one wherever it sits. + let Some(future_index) = record + .children() + .filter(|node| node.has_tag_name("rc")) + .find(|rc| { + rc.attribute("t") + .and_then(|value| value.parse::().ok()) + .and_then(|value| value.checked_sub(1)) + .and_then(|value| type_names.get(value)) + .is_some_and(|name| name == "XLRICHVALUE") + }) + .and_then(|rc| rc.attribute("v")) + .and_then(|value| value.parse::().ok()) + else { + continue; + }; + let rich_value_index = future_rich_value + .get(future_index) + .copied() + .flatten() + .unwrap_or(future_index); + let Some(rel_index) = rich_values.get(rich_value_index).copied().flatten() else { + continue; + }; + let Some(media_path) = media_paths.get(rel_index).cloned().flatten() else { + continue; + }; + by_vm.insert(index as u32 + 1, media_path); + } + Ok(by_vm) +} + +fn relationship_id(node: Node<'_, '_>) -> Option { + node.attributes() + .find(|attribute| attribute.name() == "id") + .map(|attribute| attribute.value().to_owned()) +} diff --git a/apps/sheets/native/xlsx-engine/src/visuals.rs b/apps/sheets/native/xlsx-engine/src/visuals.rs index 18f07cd..5830ac8 100644 --- a/apps/sheets/native/xlsx-engine/src/visuals.rs +++ b/apps/sheets/native/xlsx-engine/src/visuals.rs @@ -129,6 +129,12 @@ pub struct DrawingAnchor { pub to_column: usize, pub to_row_offset: i64, pub to_column_offset: i64, + /// True when the file carried a real `` marker. Excel clamps + /// such an offset at its cell edge; synthesized to markers + /// (oneCellAnchor ext, absoluteAnchor, group children) encode sizes as + /// offsets past the edge and must keep walking. + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub explicit_to: bool, } #[derive(Clone, Debug, Serialize)] @@ -164,11 +170,27 @@ pub struct ChartSeries { /// spPr/a:ln color; "none" for an explicit a:noFill line. #[serde(skip_serializing_if = "Option::is_none")] pub line_color: Option, + /// spPr/a:ln/@w converted from EMU to CSS px (w / 12700 pt · 96/72). + #[serde(skip_serializing_if = "Option::is_none")] + pub line_width: Option, #[serde(skip_serializing_if = "Option::is_none")] pub smooth: Option, /// c:marker/c:symbol — "none" hides scatter/line markers. #[serde(skip_serializing_if = "Option::is_none")] pub marker: Option, + /// First outer level of a multiLvlStrCache category axis; start/end are + /// positions in the compacted innermost `categories` (end exclusive). + #[serde(skip_serializing_if = "Option::is_none")] + pub category_groups: Option>, +} + +/// One outer-level group label spanning innermost categories [start, end). +#[derive(Clone, Debug, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct CategoryGroup { + pub label: String, + pub start: usize, + pub end: usize, } /// Per-point fill override from `c:dPt`, e.g. pie slice colors. @@ -246,6 +268,8 @@ pub struct AxisInfo { pub major_gridlines: bool, /// c:delete — the axis exists for scaling but is not drawn. pub hidden: bool, + /// c:scaling/c:orientation val="maxMin" — categories/values run reversed. + pub reversed: bool, } /// Chart-title font shorthand from c:title/c:txPr//a:defRPr. @@ -314,6 +338,9 @@ pub struct ChartMetadata { /// c:scatterStyle — whether scatter points connect with lines. #[serde(skip_serializing_if = "Option::is_none")] pub scatter_style: Option, + /// Plot-level `c:lineChart/c:marker` flag; per-series symbols refine it. + #[serde(skip_serializing_if = "Option::is_none")] + pub line_markers: Option, /// Second left/right value axis (combo charts) — scaling for the line /// series and, when not hidden, a drawn right-hand scale. #[serde(skip_serializing_if = "Option::is_none")] @@ -339,6 +366,15 @@ pub struct VisualObject { pub media_path: Option, #[serde(skip_serializing_if = "Option::is_none")] pub media_type: Option, + /// a:blip/a:alphaModFix amt as 0..1; absent when fully opaque. + #[serde(skip_serializing_if = "Option::is_none")] + pub opacity: Option, + /// spPr/a:blipFill on a shape — the image painted clipped to the + /// preset geometry (the flat fill_color stays the loading fallback). + #[serde(skip_serializing_if = "Option::is_none")] + pub fill_media_path: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub fill_media_type: Option, #[serde(skip_serializing_if = "Option::is_none")] pub name: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -435,9 +471,9 @@ pub struct SheetVisualSource { } #[derive(Clone)] -struct Relationship { - target: String, - relationship_type: String, +pub(crate) struct Relationship { + pub(crate) target: String, + pub(crate) relationship_type: String, } #[derive(Clone, Default)] @@ -1214,6 +1250,9 @@ fn read_drawing( chart_path: Some(chart_path.clone()), media_path: None, media_type: None, + opacity: None, + fill_media_path: None, + fill_media_type: None, name: drawing_name(anchor_node), shape_type: None, custom_path: None, @@ -1238,16 +1277,29 @@ fn read_drawing( }); continue; } - if let Some(blip_node) = anchor_node + // Only an xdr:pic is a picture — an xdr:sp with a:blipFill keeps its + // geometry and outline and falls through to the shape branch. + if let Some(pic_node) = anchor_node .descendants() - .find(|node| node.has_tag_name("blip")) + .find(|node| node.has_tag_name("pic")) { + let Some(blip_node) = pic_node + .descendants() + .find(|node| node.has_tag_name("blip")) + else { + continue; + }; let Some(id) = relationship_id(blip_node) else { continue; }; let Some(relationship) = relationships.get(&id) else { continue; }; + // Excel draws nothing for a picture whose anchor collapses to a + // point (from == to with equal offsets, or a 0x0 ext). + if anchor_is_zero_extent(&anchor) { + continue; + } let media_path = resolve_part_target(drawing_path, &relationship.target)?; visuals.push(VisualObject { id: visual_id, @@ -1258,6 +1310,9 @@ fn read_drawing( chart_path: None, media_type: media_type_for_path(&media_path).map(ToOwned::to_owned), media_path: Some(media_path), + opacity: blip_opacity(blip_node), + fill_media_path: None, + fill_media_type: None, name: drawing_name(anchor_node), shape_type: None, custom_path: None, @@ -1294,6 +1349,7 @@ fn read_drawing( drawing_name(anchor_node), colors, drawing_path, + &relationships, Some(index), )); } @@ -1321,6 +1377,7 @@ fn shape_visual( name: Option, colors: &ColorContext, drawing_path: &str, + relationships: &HashMap, drawing_index: Option, ) -> VisualObject { { @@ -1345,6 +1402,18 @@ fn shape_visual( } else { shape_sppr.and_then(|sppr| drawing_fill_color(sppr, colors)) }; + let fill_media_path = shape_sppr + .and_then(|sppr| sppr.children().find(|node| node.has_tag_name("blipFill"))) + .and_then(|fill| fill.descendants().find(|node| node.has_tag_name("blip"))) + .and_then(relationship_id) + .and_then(|id| relationships.get(&id)) + .and_then(|relationship| { + resolve_part_target(drawing_path, &relationship.target).ok() + }); + let fill_media_type = fill_media_path + .as_deref() + .and_then(media_type_for_path) + .map(ToOwned::to_owned); let xfrm = shape_node .descendants() .find(|node| node.has_tag_name("xfrm")); @@ -1447,6 +1516,9 @@ fn shape_visual( chart_path: None, media_path: None, media_type: None, + opacity: None, + fill_media_path, + fill_media_type, name, shape_type, custom_path, @@ -1689,6 +1761,7 @@ fn expand_group( to_row_offset: anchor.from_row_offset + (child_box.1 + child_box.3).round() as i64, to_column_offset: anchor.from_column_offset + (child_box.0 + child_box.2).round() as i64, + explicit_to: false, }; *counter += 1; let child_id = format!("{visual_id}-{counter}"); @@ -1706,6 +1779,7 @@ fn expand_group( child_name, colors, drawing_path, + relationships, None, )); continue; @@ -1732,6 +1806,9 @@ fn expand_group( chart_path: Some(chart_path), media_path: None, media_type: None, + opacity: None, + fill_media_path: None, + fill_media_type: None, name: child_name, shape_type: None, custom_path: None, @@ -1756,16 +1833,18 @@ fn expand_group( }); continue; } - let Some(id) = child - .descendants() - .find(|node| node.has_tag_name("blip")) - .and_then(relationship_id) - else { + let Some(blip_node) = child.descendants().find(|node| node.has_tag_name("blip")) else { + continue; + }; + let Some(id) = relationship_id(blip_node) else { continue; }; let Some(relationship) = relationships.get(&id) else { continue; }; + if anchor_is_zero_extent(&child_anchor) { + continue; + } let media_path = resolve_part_target(drawing_path, &relationship.target)?; visuals.push(VisualObject { id: child_id, @@ -1776,6 +1855,9 @@ fn expand_group( chart_path: None, media_type: media_type_for_path(&media_path).map(ToOwned::to_owned), media_path: Some(media_path), + opacity: blip_opacity(blip_node), + fill_media_path: None, + fill_media_type: None, name: child_name, shape_type: None, custom_path: None, @@ -1812,6 +1894,31 @@ const CHART_TYPE_NAMES: [&str; 7] = [ "radarChart", ]; +/// 3D plot elements fold onto their flat pipeline at parse time (flat 2D +/// projection of the right chart type); the wire only carries the 2D name. +/// stockChart/surfaceChart stay unmapped. +const CHART_TYPE_3D_FOLDS: [(&str, &str); 4] = [ + ("bar3DChart", "barChart"), + ("line3DChart", "lineChart"), + ("pie3DChart", "pieChart"), + ("area3DChart", "areaChart"), +]; + +/// The 2D wire name of a plot element, folding 3D variants. +fn flat_plot_name(node: Node<'_, '_>) -> Option<&'static str> { + let name = node.tag_name().name(); + CHART_TYPE_NAMES + .iter() + .copied() + .find(|flat| *flat == name) + .or_else(|| { + CHART_TYPE_3D_FOLDS + .iter() + .find(|(three_d, _)| *three_d == name) + .map(|(_, flat)| *flat) + }) +} + fn read_chart( archive: &mut ZipArchive, chart_path: &str, @@ -1825,7 +1932,11 @@ fn read_chart( fn chart_metadata(document: &Document<'_>, colors: &ColorContext) -> ChartMetadata { let chart_types = CHART_TYPE_NAMES .iter() - .filter(|name| document.descendants().any(|node| node.has_tag_name(**name))) + .filter(|name| { + document + .descendants() + .any(|node| flat_plot_name(node) == Some(**name)) + }) .map(|name| (*name).to_owned()) .collect::>(); // Only the chart-level title — axes carry their own c:title deeper down. @@ -1922,6 +2033,13 @@ fn chart_metadata(document: &Document<'_>, colors: &ColorContext) -> ChartMetada .find(|node| node.has_tag_name("scatterStyle")) .and_then(|node| node.attribute("val")) .map(ToOwned::to_owned), + // Direct child only: c:ser/c:marker (a symbol container) must not + // register as the plot flag. CT_Boolean: a bare is true. + line_markers: document + .descendants() + .find(|node| node.has_tag_name("lineChart") || node.has_tag_name("line3DChart")) + .and_then(|plot| direct_child(plot, "marker")) + .map(|node| !matches!(node.attribute("val"), Some("0") | Some("false"))), secondary_y_axis, title_style, series, @@ -1984,6 +2102,10 @@ fn axis_infos(document: &Document<'_>) -> (Option, Option, O // CT_Boolean: a bare means true. hidden: direct_child(axis, "delete") .is_some_and(|node| !matches!(node.attribute("val"), Some("0") | Some("false"))), + reversed: scaling + .and_then(|node| direct_child(node, "orientation")) + .and_then(|node| node.attribute("val")) + == Some("maxMin"), }; let position = direct_child(axis, "axPos").and_then(|node| node.attribute("val")); let is_x = match position { @@ -2083,7 +2205,7 @@ fn legend_position(document: &Document<'_>) -> String { fn data_labels_node<'a>(document: &'a Document<'a>) -> Option> { let plot_labels = document .descendants() - .find(|node| CHART_TYPE_NAMES.iter().any(|name| node.has_tag_name(*name))) + .find(|node| flat_plot_name(*node).is_some()) .and_then(|plot| direct_child(plot, "dLbls")); let series_labels = document .descendants() @@ -2160,9 +2282,10 @@ fn axis_titles(document: &Document<'_>) -> Option { fn plot_grouping(document: &Document<'_>) -> Option { let plot = document.descendants().find(|node| { - ["barChart", "areaChart", "lineChart"] - .iter() - .any(|name| node.has_tag_name(*name)) + matches!( + flat_plot_name(*node), + Some("barChart") | Some("areaChart") | Some("lineChart") + ) })?; direct_child(plot, "grouping") .and_then(|node| node.attribute("val")) @@ -2186,10 +2309,14 @@ fn parse_chart_series(series: Node<'_, '_>, index: usize, colors: &ColorContext) }; let name = cached_name.unwrap_or_else(|| format!("Series{}", index + 1)); // Explicit series fill/line color, else the theme accent cycle Excel uses - // for automatic chart colors. + // for automatic chart colors — keyed by c:idx, not document position. + let accent_index = direct_child(series, "idx") + .and_then(|node| node.attribute("val")) + .and_then(|value| value.parse::().ok()) + .unwrap_or(index); let color = direct_child(series, "spPr") .and_then(|sppr| drawing_fill_color(sppr, colors)) - .or_else(|| theme_accent(colors, index % 6 + 1).map(|base| tint_to_hex(base, 0.0))); + .or_else(|| theme_accent(colors, accent_index % 6 + 1).map(|base| tint_to_hex(base, 0.0))); let category_node = direct_child(series, "cat").or_else(|| direct_child(series, "xVal")); let categories = category_node.map(cached_values).unwrap_or_default(); let category_format = category_node.and_then(cache_format_code); @@ -2231,14 +2358,18 @@ fn parse_chart_series(series: Node<'_, '_>, index: usize, colors: &ColorContext) }) }) .collect::>(); - let line_color = direct_child(series, "spPr") - .and_then(|sppr| sppr.children().find(|node| node.has_tag_name("ln"))) - .and_then(|ln| { - if ln.children().any(|node| node.has_tag_name("noFill")) { - return Some("none".into()); - } - drawing_fill_color(ln, colors) - }); + let line = direct_child(series, "spPr") + .and_then(|sppr| sppr.children().find(|node| node.has_tag_name("ln"))); + let line_color = line.and_then(|ln| { + if ln.children().any(|node| node.has_tag_name("noFill")) { + return Some("none".into()); + } + drawing_fill_color(ln, colors) + }); + let line_width = line + .and_then(|ln| ln.attribute("w")) + .and_then(|value| value.parse::().ok()) + .map(|emu| emu / 12700.0 * (96.0 / 72.0)); let smooth = direct_child(series, "smooth") .and_then(|node| node.attribute("val")) .map(|value| value == "1" || value == "true"); @@ -2261,8 +2392,10 @@ fn parse_chart_series(series: Node<'_, '_>, index: usize, colors: &ColorContext) explosion_pct, point_explosions: (!point_explosions.is_empty()).then_some(point_explosions), line_color, + line_width, smooth, marker, + category_groups: category_node.and_then(category_groups), } } @@ -2295,6 +2428,27 @@ fn formula_ref(node: Node<'_, '_>) -> Option { .filter(|value| !value.is_empty()) } +/// Both markers coincide — a zero span on both axes. (The no-ext fallbacks +/// in parse_anchor always produce a real span, so they never match.) +fn anchor_is_zero_extent(anchor: &DrawingAnchor) -> bool { + anchor.from_row == anchor.to_row + && anchor.from_row_offset == anchor.to_row_offset + && anchor.from_column == anchor.to_column + && anchor.from_column_offset == anchor.to_column_offset +} + +/// a:blip/a:alphaModFix amt (per-100000) → 0..1; None when absent or opaque. +fn blip_opacity(blip_node: Node<'_, '_>) -> Option { + let amt = blip_node + .children() + .find(|node| node.has_tag_name("alphaModFix"))? + .attribute("amt")? + .parse::() + .ok()?; + let opacity = (amt / 100_000.0).clamp(0.0, 1.0); + (opacity < 1.0).then_some(opacity) +} + fn parse_anchor(anchor: Node<'_, '_>) -> Option { let Some(from) = direct_child(anchor, "from") else { // absoluteAnchor: xdr:pos + xdr:ext in sheet EMU. Encode both corners @@ -2317,6 +2471,7 @@ fn parse_anchor(anchor: Node<'_, '_>) -> Option { to_column: 0, to_row_offset: y + cy, to_column_offset: x + cx, + explicit_to: false, }); }; let from_row = marker_value(from, "row")?; @@ -2333,6 +2488,7 @@ fn parse_anchor(anchor: Node<'_, '_>) -> Option { to_column: marker_value(to, "col").unwrap_or(from_column + 8), to_row_offset: marker_signed_value(to, "rowOff").unwrap_or(0), to_column_offset: marker_signed_value(to, "colOff").unwrap_or(0), + explicit_to: true, }); } // oneCellAnchor: the size lives in xdr:ext (EMU). Encode it as offsets @@ -2354,6 +2510,7 @@ fn parse_anchor(anchor: Node<'_, '_>) -> Option { to_column: from_column, to_row_offset: from_row_offset + cy, to_column_offset: from_column_offset + cx, + explicit_to: false, }), _ => Some(DrawingAnchor { from_row, @@ -2364,6 +2521,7 @@ fn parse_anchor(anchor: Node<'_, '_>) -> Option { to_column: from_column + 8, to_row_offset: 0, to_column_offset: 0, + explicit_to: false, }), } } @@ -2448,7 +2606,7 @@ fn parse_fill(fill: Node<'_, '_>, colors: &ColorContext) -> FillInfo { .children() .find(|node| node.has_tag_name("patternFill")) else { - return FillInfo::default(); + return parse_gradient_fill(fill, colors); }; let pattern_type = pattern.attribute("patternType"); if pattern_type == Some("none") { @@ -2483,6 +2641,39 @@ fn parse_fill(fill: Node<'_, '_>, colors: &ColorContext) -> FillInfo { FillInfo { color, theme, tint } } +/// The cell style model carries one flat color, so a gradientFill is +/// approximated by the mid-gradient blend of its outermost stops. The blend +/// carries no single theme provenance. +fn parse_gradient_fill(fill: Node<'_, '_>, colors: &ColorContext) -> FillInfo { + let Some(gradient) = fill + .children() + .find(|node| node.has_tag_name("gradientFill")) + else { + return FillInfo::default(); + }; + let mut stops = gradient + .children() + .filter(|node| node.has_tag_name("stop")) + .filter_map(|stop| { + let position = stop.attribute("position")?.parse::().ok()?; + let color = stop + .children() + .find(|node| node.has_tag_name("color")) + .and_then(|node| parse_color(node, colors))?; + Some((position, color)) + }) + .collect::>(); + // Stops are not guaranteed document-ordered. + stops.sort_by(|a, b| a.0.total_cmp(&b.0)); + let (Some((_, first)), Some((_, last))) = (stops.first(), stops.last()) else { + return FillInfo::default(); + }; + FillInfo { + color: mix_hex(first, last), + ..FillInfo::default() + } +} + fn mix_hex(first: &str, second: &str) -> Option { let parse = |hex: &str| -> Option<(u8, u8, u8)> { let value = hex.strip_prefix('#')?; @@ -2972,7 +3163,7 @@ pub fn hyperlink_targets( .collect()) } -fn read_relationships( +pub(crate) fn read_relationships( archive: &mut ZipArchive, source_path: &str, ) -> Result, SidecarError> { @@ -3006,7 +3197,7 @@ fn read_relationships( .collect()) } -fn resolve_part_target(source_path: &str, target: &str) -> Result { +pub(crate) fn resolve_part_target(source_path: &str, target: &str) -> Result { let candidate = if target.starts_with('/') { PathBuf::from(target.trim_start_matches('/')) } else { @@ -3045,7 +3236,7 @@ fn read_xml(archive: &mut ZipArchive, path: &str) -> Result, path: &str, ) -> Result, SidecarError> { @@ -3057,7 +3248,7 @@ fn read_optional_xml( Ok(Some(xml)) } -fn parse_document<'a>(xml: &'a str, path: &str) -> Result, SidecarError> { +pub(crate) fn parse_document<'a>(xml: &'a str, path: &str) -> Result, SidecarError> { Document::parse(xml) .map_err(|error| SidecarError::Workbook(format!("Invalid XML in {path}: {error}"))) } @@ -3081,7 +3272,17 @@ fn drawing_name(anchor: Node<'_, '_>) -> Option { } fn cached_values(node: Node<'_, '_>) -> Vec { - node.descendants() + // multiLvlStrCache: sweeping every c:pt would flatten L levels into one + // L×N array. The first c:lvl is the innermost (detail) level per OOXML. + let scope = node + .descendants() + .find(|child| child.has_tag_name("lvl")) + .unwrap_or(node); + // Document order, compacted: the numeric value path drops blanks the same + // way, so padding sparse c:pt/@idx gaps here would misalign categories + // against values. Aligning both caches by idx union needs a wire change. + scope + .descendants() .filter(|child| child.has_tag_name("pt")) .filter_map(|point| { point @@ -3093,6 +3294,78 @@ fn cached_values(node: Node<'_, '_>) -> Vec { .collect() } +/// First outer level of a multiLvlStrCache: each pt idx marks a group start +/// in cache-idx space; the span runs until the next pt (or ptCount). Spans +/// are remapped onto the document-order positions `cached_values` emits, so +/// sparse caches cannot misalign groups against categories. Ambiguity — +/// an outer pt without an idx, or a group whose members are not contiguous +/// in the emitted order — skips the groups entirely. +fn category_groups(node: Node<'_, '_>) -> Option> { + let cache = node + .descendants() + .find(|child| child.has_tag_name("multiLvlStrCache"))?; + let mut levels = cache.children().filter(|child| child.has_tag_name("lvl")); + let inner = levels.next()?; + let outer = levels.next()?; + let inner_idx: Vec = inner + .children() + .filter(|child| child.has_tag_name("pt")) + .enumerate() + .map(|(position, point)| numeric_attribute(point, "idx").unwrap_or(position)) + .collect(); + if inner_idx.is_empty() { + return None; + } + let outer_points: Vec> = outer + .children() + .filter(|child| child.has_tag_name("pt")) + .collect(); + let mut starts: Vec<(usize, String)> = outer_points + .iter() + .filter_map(|point| { + Some(( + numeric_attribute(*point, "idx")?, + direct_child(*point, "v")?.text()?.to_owned(), + )) + }) + .collect(); + if starts.is_empty() || starts.len() != outer_points.len() { + return None; + } + starts.sort_by_key(|(idx, _)| *idx); + let point_count = direct_child(cache, "ptCount") + .and_then(|count| numeric_attribute(count, "val")) + .unwrap_or_else(|| inner_idx.iter().max().map_or(0, |max| max + 1)); + // A group's members are the emitted positions whose idx falls in + // [start, next); document order need not be idx order, so require the + // member positions to be contiguous before expressing them as a span. + let mut groups = Vec::new(); + for (position, (idx, label)) in starts.iter().enumerate() { + let next = starts + .get(position + 1) + .map_or_else(|| point_count.max(*idx), |(next_idx, _)| *next_idx); + let members: Vec = inner_idx + .iter() + .enumerate() + .filter(|(_, inner)| (*idx..next).contains(*inner)) + .map(|(emitted, _)| emitted) + .collect(); + let (Some(&start), Some(&last)) = (members.first(), members.last()) else { + continue; + }; + let end = last + 1; + if end - start != members.len() { + return None; + } + groups.push(CategoryGroup { + label: label.clone(), + start, + end, + }); + } + (!groups.is_empty()).then_some(groups) +} + fn first_cached_value(node: Node<'_, '_>) -> Option { cached_values(node).into_iter().next().or_else(|| { node.descendants() @@ -3133,6 +3406,7 @@ fn media_type_for_path(path: &str) -> Option<&'static str> { "jpg" | "jpeg" => Some("image/jpeg"), "gif" => Some("image/gif"), "bmp" => Some("image/bmp"), + "webp" => Some("image/webp"), "svg" => Some("image/svg+xml"), // GDI metafiles: the renderer rasterizes these to PNG before display. "emf" => Some("image/x-emf"), @@ -3359,6 +3633,44 @@ mod tests { ); } + fn fill_info(body: &str, colors: &ColorContext) -> FillInfo { + let xml = format!("{body}"); + let document = Document::parse(&xml).unwrap(); + parse_fill(document.root_element(), colors) + } + + #[test] + fn gradient_fill_blends_outermost_stops() { + let colors = ColorContext { + theme: vec![ + (0xFF, 0xFF, 0xFF), + (0x00, 0x00, 0x00), + (0x00, 0x00, 0x00), + (0x00, 0x00, 0x00), + (0x44, 0x72, 0xC4), + ], + ..ColorContext::default() + }; + // Stops deliberately out of document order; theme stops resolve + // through the palette before blending. + let fill = fill_info( + r#""#, + &colors, + ); + assert_eq!(fill.color.as_deref(), Some("#A1B8E1")); + assert_eq!(fill.theme, None); + assert_eq!(fill.tint, None); + } + + #[test] + fn gradient_fill_without_resolvable_stops_stays_unfilled() { + let fill = fill_info( + r#""#, + &ColorContext::default(), + ); + assert_eq!(fill.color, None); + } + #[test] fn indexed_palette_override_wins_below_system_slots() { let colors = ColorContext { @@ -3387,13 +3699,151 @@ mod tests { #[test] fn media_types_cover_gdi_metafiles() { - assert_eq!(media_type_for_path("xl/media/image1.emf"), Some("image/x-emf")); - assert_eq!(media_type_for_path("xl/media/image1.WMF"), Some("image/x-wmf")); - assert_eq!(media_type_for_path("xl/media/image1.emz"), Some("image/x-emz")); - assert_eq!(media_type_for_path("xl/media/image1.wmz"), Some("image/x-wmz")); + assert_eq!( + media_type_for_path("xl/media/image1.emf"), + Some("image/x-emf") + ); + assert_eq!( + media_type_for_path("xl/media/image1.WMF"), + Some("image/x-wmf") + ); + assert_eq!( + media_type_for_path("xl/media/image1.emz"), + Some("image/x-emz") + ); + assert_eq!( + media_type_for_path("xl/media/image1.wmz"), + Some("image/x-wmz") + ); assert_eq!(media_type_for_path("xl/media/object1.bin"), None); } + #[test] + fn media_types_cover_webp() { + assert_eq!( + media_type_for_path("xl/media/image.webp"), + Some("image/webp") + ); + assert_eq!( + media_type_for_path("xl/media/IMAGE2.WEBP"), + Some("image/webp") + ); + } + + fn parsed_anchor(body: &str) -> DrawingAnchor { + let xml = format!( + r#"{body}"# + ); + let document = Document::parse(&xml).unwrap(); + parse_anchor(document.root_element()).unwrap() + } + + #[test] + fn zero_extent_anchor_detection() { + let marker = |row: usize, col: usize| { + format!( + "{col}0{row}0" + ) + }; + // from == to with zero offsets: Excel shows no picture. + let degenerate = parsed_anchor(&format!( + "{m}{m}", + m = marker(3, 2) + )); + assert!(anchor_is_zero_extent(°enerate)); + let spanning = parsed_anchor(&format!( + "{}{}", + marker(3, 2), + marker(5, 4) + )); + assert!(!anchor_is_zero_extent(&spanning)); + // oneCellAnchor with a real ext keeps its span. + let one_cell = parsed_anchor(&format!( + r#"{}"#, + marker(0, 0) + )); + assert!(!anchor_is_zero_extent(&one_cell)); + // oneCellAnchor with a 0x0 ext collapses. + let one_cell_zero = parsed_anchor(&format!( + r#"{}"#, + marker(0, 0) + )); + assert!(anchor_is_zero_extent(&one_cell_zero)); + // No ext at all: the 20x8-cell fallback frame stays visible. + let fallback = parsed_anchor(&format!("{}", marker(0, 0))); + assert!(!anchor_is_zero_extent(&fallback)); + // absoluteAnchor with a real ext keeps its span. + let absolute = + parsed_anchor(r#""#); + assert!(!anchor_is_zero_extent(&absolute)); + // Only real markers are flagged for cell-edge clamping; + // synthesized to markers must keep the walk-past-the-edge encoding. + assert!(degenerate.explicit_to); + assert!(spanning.explicit_to); + assert!(!one_cell.explicit_to); + assert!(!fallback.explicit_to); + assert!(!absolute.explicit_to); + } + + fn opacity_of(blip_body: &str) -> Option { + let xml = format!( + r#"{blip_body}"# + ); + let document = Document::parse(&xml).unwrap(); + blip_opacity(document.root_element()) + } + + #[test] + fn alpha_mod_fix_becomes_opacity() { + assert_eq!(opacity_of(r#""#), Some(0.2)); + assert_eq!(opacity_of(r#""#), None); + assert_eq!(opacity_of(r#""#), None); + assert_eq!(opacity_of(r#""#), None); + assert_eq!(opacity_of(""), None); + } + + #[test] + fn shape_with_blip_fill_keeps_geometry_and_carries_fill_media() { + let xml = r#""#; + let document = Document::parse(xml).unwrap(); + let relationships = HashMap::from([( + "rId1".to_owned(), + Relationship { + target: "../media/image1.png".into(), + relationship_type: String::new(), + }, + )]); + let anchor = DrawingAnchor { + from_row: 0, + from_column: 0, + from_row_offset: 0, + from_column_offset: 0, + to_row: 10, + to_column: 5, + to_row_offset: 0, + to_column_offset: 0, + explicit_to: false, + }; + let visual = shape_visual( + document.root_element(), + anchor, + "visual-1".into(), + "sheet-1", + None, + &ColorContext::default(), + "xl/drawings/drawing1.xml", + &relationships, + Some(0), + ); + assert_eq!(visual.kind, "shape"); + assert_eq!(visual.shape_type.as_deref(), Some("heart")); + assert_eq!( + visual.fill_media_path.as_deref(), + Some("xl/media/image1.png") + ); + assert_eq!(visual.fill_media_type.as_deref(), Some("image/png")); + } + #[test] fn numbers_unnamed_series_like_excel() { let chart = metadata( @@ -3483,6 +3933,7 @@ mod tests { to_column: 8, to_row_offset: 0, to_column_offset: 0, + explicit_to: false, }; let mut visuals = Vec::new(); let mut counter = 0; @@ -3546,6 +3997,7 @@ mod tests { to_column: 5, to_row_offset: 0, to_column_offset: 0, + explicit_to: false, }; let relationships = HashMap::from([( "rId7".to_owned(), @@ -3607,6 +4059,7 @@ mod tests { to_column: 1, to_row_offset: 0, to_column_offset: 0, + explicit_to: false, }; let visual = shape_visual( shape, @@ -3616,6 +4069,7 @@ mod tests { None, &ColorContext::default(), "xl/drawings/drawing1.xml", + &HashMap::new(), Some(0), ); assert_eq!(visual.rotation, Some(45.0)); @@ -3832,6 +4286,172 @@ mod tests { ); } + #[test] + fn folds_3d_plot_types_onto_flat_pipelines() { + let pie = metadata(""); + assert_eq!(pie.chart_types, vec!["pieChart"]); + let bar = metadata( + r#""#, + ); + assert_eq!(bar.chart_types, vec!["barChart"]); + assert_eq!(bar.grouping.as_deref(), Some("percentStacked")); + assert_eq!(bar.bar_direction.as_deref(), Some("col")); + let line = metadata(""); + assert_eq!(line.chart_types, vec!["lineChart", "areaChart"]); + // Unmapped 3D plots keep the old behavior: no chart type. + assert!( + metadata("") + .chart_types + .is_empty() + ); + } + + #[test] + fn default_series_accent_follows_ser_idx() { + let body = r#" + + + + "#; + let chart = metadata_with(body, &theme_colors()); + // Explicit fill wins regardless of idx. + assert_eq!(chart.series[0].color.as_deref(), Some("#FF8800")); + // idx 0 → accent1 (theme slot 4), idx 1 → accent2 (slot 5). + assert_eq!(chart.series[1].color.as_deref(), Some("#042233")); + assert_eq!(chart.series[2].color.as_deref(), Some("#052233")); + // Without c:idx the document position keeps driving the cycle. + let plain = ""; + let chart = metadata_with(plain, &theme_colors()); + assert_eq!(chart.series[0].color.as_deref(), Some("#042233")); + assert_eq!(chart.series[1].color.as_deref(), Some("#052233")); + } + + #[test] + fn reads_plot_level_line_marker_flag() { + let on = r#""#; + let chart = metadata(on); + assert_eq!(chart.line_markers, Some(true)); + // The per-series marker container stays a symbol, not the plot flag. + assert_eq!(chart.series[0].marker.as_deref(), Some("none")); + let off = r#""#; + assert_eq!(metadata(off).line_markers, Some(false)); + // Bare is CT_Boolean true; absent stays absent. + let bare = r#""#; + assert_eq!(metadata(bare).line_markers, Some(true)); + let series_only = r#""#; + assert_eq!(metadata(series_only).line_markers, None); + } + + #[test] + fn multi_level_categories_use_the_innermost_level_only() { + let body = r#"D!$B$2:$E$3 + Qtr 1Qtr 2Qtr 3Qtr 4 + 2008 + "#; + assert_eq!( + metadata(body).series[0].categories, + vec!["Qtr 1", "Qtr 2", "Qtr 3", "Qtr 4"] + ); + } + + #[test] + fn reads_series_line_width_in_px() { + let body = r#""#; + let chart = metadata(body); + // 12700 EMU = 1pt = 4/3 px. + let width = chart.series[0].line_width.unwrap(); + assert!((width - 4.0 / 3.0).abs() < 1e-9); + assert!(chart.series[1].line_width.is_none()); + let json = serde_json::to_value(&chart).unwrap(); + assert!(json["series"][0].get("lineWidth").is_some()); + assert!(json["series"][1].get("lineWidth").is_none()); + } + + #[test] + fn outer_category_levels_become_groups_with_spans() { + let body = r#" + Q1Q2Q3Q4 + Q1Q2Q3Q4 + 20082009 + "#; + let groups = metadata(body).series[0].category_groups.clone().unwrap(); + let spans: Vec<(&str, usize, usize)> = groups + .iter() + .map(|group| (group.label.as_str(), group.start, group.end)) + .collect(); + assert_eq!(spans, vec![("2008", 0, 4), ("2009", 4, 8)]); + let json = serde_json::to_value(metadata(body)).unwrap(); + assert_eq!( + json["series"][0]["categoryGroups"][0], + serde_json::json!({ "label": "2008", "start": 0, "end": 4 }) + ); + // Single-level caches carry no groups. + let flat = r#" + ab + "#; + assert!(metadata(flat).series[0].category_groups.is_none()); + } + + #[test] + fn category_group_spans_follow_the_compacted_innermost_positions() { + // Innermost idx 2 is missing: the compacted categories are [Q1, Q2, + // Q4, Q1] so the 2008 group must span 3 positions, not 4. + let body = r#" + Q1Q2Q4Q1 + 20082009 + "#; + let chart = metadata(body); + assert_eq!(chart.series[0].categories, vec!["Q1", "Q2", "Q4", "Q1"]); + let groups = chart.series[0].category_groups.clone().unwrap(); + let spans: Vec<(&str, usize, usize)> = groups + .iter() + .map(|group| (group.label.as_str(), group.start, group.end)) + .collect(); + assert_eq!(spans, vec![("2008", 0, 3), ("2009", 3, 4)]); + // An outer pt without idx cannot be placed: no groups at all. + let no_idx = r#" + Q1Q2 + 2008 + "#; + assert!(metadata(no_idx).series[0].category_groups.is_none()); + } + + #[test] + fn category_group_spans_follow_document_order_not_idx_order() { + // Innermost pts arrive out of idx order: categories emit as + // [Q1'09, Q1'08, Q2'08], so the 2008 group sits at positions 1-2 + // and the 2009 group at position 0. + let body = r#" + Q1 09Q1 08Q2 08 + 20082009 + "#; + let chart = metadata(body); + assert_eq!(chart.series[0].categories, vec!["Q1 09", "Q1 08", "Q2 08"]); + let groups = chart.series[0].category_groups.clone().unwrap(); + let spans: Vec<(&str, usize, usize)> = groups + .iter() + .map(|group| (group.label.as_str(), group.start, group.end)) + .collect(); + assert_eq!(spans, vec![("2008", 1, 3), ("2009", 0, 1)]); + // Interleaved emission leaves a group non-contiguous: a span cannot + // express it, so no groups at all. + let interleaved = r#" + Q1 08Q1 09Q2 08 + 20082009 + "#; + assert!(metadata(interleaved).series[0].category_groups.is_none()); + } + + #[test] + fn sparse_caches_stay_compacted_in_document_order() { + // Both cache kinds compact sparse points the same way, keeping + // categories aligned with values (idx-union alignment is a leftover). + let body = r#" + ca + "#; + assert_eq!(metadata(body).series[0].categories, vec!["c", "a"]); + } + #[test] fn reads_point_colors_from_srgb_and_scheme_fills() { let body = r#" diff --git a/apps/sheets/src/domain/chart-visual.ts b/apps/sheets/src/domain/chart-visual.ts index 0693a96..7415744 100644 --- a/apps/sheets/src/domain/chart-visual.ts +++ b/apps/sheets/src/domain/chart-visual.ts @@ -50,9 +50,14 @@ export interface ChartSeriesVisualState { pointExplosions?: { index: number; pct: number }[] | undefined /// spPr/a:ln color; "none" = explicit no line. lineColor?: string | undefined + /// spPr/a:ln/@w in CSS px (EMU / 12700 pt · 96/72). + lineWidth?: number | undefined smooth?: boolean | undefined /// c:marker symbol; "none" hides scatter/line markers. marker?: string | undefined + /// First outer multiLvlStrCache level; start/end index the compacted + /// `categories` (end exclusive). + categoryGroups?: { label: string; start: number; end: number }[] | undefined } /// One plot axis keyed by its side (b/t → x, l/r → y). @@ -65,6 +70,8 @@ export interface ChartAxisInfoState { majorGridlines: boolean /// c:delete — scales its series but is not drawn. hidden: boolean + /// c:scaling/c:orientation val="maxMin". + reversed: boolean } export interface ChartVisualState { @@ -97,6 +104,8 @@ export interface ChartVisualState { secondaryYAxis?: ChartAxisInfoState | undefined /// c:scatterStyle — whether scatter points connect with lines. scatterStyle?: string | undefined + /// Plot-level c:lineChart/c:marker flag; per-series symbols refine it. + lineMarkers?: boolean | undefined /// c:title/c:txPr//a:defRPr shorthand. titleStyle?: | { size?: number | undefined; bold?: boolean | undefined; color?: string | undefined } @@ -152,10 +161,12 @@ export function scatterAxisBounds( return { min, max, ticks } } -/// Excel-like value-axis scale. Explicit bounds/unit win; the auto unit is -/// the smallest 1/2/5×10^n giving at most 8 intervals, and the max rounds -/// up to a unit multiple (11162 → 0..12000 step 2000; 3750 → 0..4000 step -/// 500; 13 → 0..14 step 2 — matching Excel's defaults on the run5 corpus). +/// Excel-like value-axis scale. Explicit bounds/unit win. Excel's auto max +/// leaves ~5% headroom above the data: with bumped = span · 1.05, the unit +/// is the smallest 1/2/5×10^n giving at most 10 intervals of bumped, and +/// max = min + unit · ceil(bumped / unit). Calibrated on Excel-rendered +/// refs: 18 → 20 step 2, 148 → 160 step 20, 877 → 1000 step 100, 1000 → +/// 1200 step 200, 289753.76 → 350000 step 50000 (real-run1 + prod corpora). export function valueAxisScale( dataMax: number, explicit?: { min?: number | undefined; max?: number | undefined; majorUnit?: number | undefined }, @@ -166,8 +177,9 @@ export function valueAxisScale( if (!(span > 0)) { return { min, max: min + 1, ticks: [min, min + 0.5, min + 1] } } - const unit = explicit?.majorUnit ?? autoAxisUnit(span) - const max = explicit?.max ?? min + Math.ceil(span / unit - 1e-9) * unit + const bumped = explicit?.max === undefined ? span * 1.05 : span + const unit = explicit?.majorUnit ?? autoAxisUnit(bumped) + const max = explicit?.max ?? min + Math.ceil(bumped / unit - 1e-9) * unit return { min, max: max > min ? max : min + unit, ticks: unitTicks(min, max, unit) } } @@ -176,7 +188,9 @@ function autoAxisUnit(span: number): number { for (let guard = 0; guard < 6; guard += 1) { for (const base of [1, 2, 5]) { const unit = base * 10 ** exponent - if (span / unit <= 8 + 1e-9) return unit + // Excel allows up to 10 intervals (877 bumps to 920.85 → unit 100 / + // max 1000; 1000 bumps to 1050 → 11 intervals reject 100, unit 200). + if (span / unit <= 10 + 1e-9) return unit } exponent += 1 } @@ -422,7 +436,10 @@ export function applyChartStateEdit( : {}), ...(data?.name === undefined ? {} : { name: data.name }), ...(data?.values === undefined ? {} : { values: data.values }), - ...(data?.categories === undefined ? {} : { categories: data.categories }), + // Replacing the categories orphans the parsed outer-level spans. + ...(data?.categories === undefined + ? {} + : { categories: data.categories, categoryGroups: undefined }), ...(data?.valuesRef === undefined ? {} : { valuesRef: data.valuesRef }), ...(data?.categoriesRef === undefined ? {} : { categoriesRef: data.categoriesRef }), } diff --git a/apps/sheets/src/main/atomic-write.ts b/apps/sheets/src/main/atomic-write.ts new file mode 100644 index 0000000..6556ea9 --- /dev/null +++ b/apps/sheets/src/main/atomic-write.ts @@ -0,0 +1,45 @@ +import { randomBytes } from 'node:crypto' +import { rename, unlink, writeFile } from 'node:fs/promises' +import { basename, dirname, join } from 'node:path' + +/** Transient Windows codes: antivirus/indexer briefly locks the rename target. */ +const RETRYABLE_RENAME_CODES = new Set(['EPERM', 'EACCES', 'EBUSY']) +const RENAME_RETRIES = 4 + +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) + +/** + * Same-directory temp file + rename prevents observers from reading a partial + * export. Windows rename-over-existing can fail transiently, so retry with + * backoff and finally fall back to the prior in-place write behavior. + */ +export async function atomicWriteFile(filePath: string, data: Buffer): Promise { + const tmp = join( + dirname(filePath), + `.${basename(filePath)}.${randomBytes(6).toString('hex')}.tmp`, + ) + try { + await writeFile(tmp, data) + for (let attempt = 0; ; attempt += 1) { + try { + await rename(tmp, filePath) + return + } catch (error) { + const code = (error as NodeJS.ErrnoException).code ?? '' + if (!RETRYABLE_RENAME_CODES.has(code) || attempt >= RENAME_RETRIES) throw error + await sleep(50 * 2 ** attempt) + } + } + } catch (error) { + const retryable = RETRYABLE_RENAME_CODES.has((error as NodeJS.ErrnoException).code ?? '') + if (retryable) { + // Preserve the completed temp until the non-atomic fallback succeeds. + // If that write fails or the process exits, the new bytes still exist. + await writeFile(filePath, data) + await unlink(tmp).catch(() => {}) + return + } + await unlink(tmp).catch(() => {}) + throw error + } +} diff --git a/apps/sheets/src/main/pdf-export.ts b/apps/sheets/src/main/pdf-export.ts index b69b8c3..314db3a 100644 --- a/apps/sheets/src/main/pdf-export.ts +++ b/apps/sheets/src/main/pdf-export.ts @@ -34,12 +34,23 @@ export async function exportPdf( try { await writeFile(htmlPath, request.html, 'utf8') await window.loadFile(htmlPath) + const displayHeaderFooter = + request.headerTemplate !== undefined || request.footerTemplate !== undefined const pdf = await window.webContents.printToPDF({ landscape: request.landscape, pageSize: request.pageSize, margins: request.margins, scale: request.scale, printBackground: true, + ...(displayHeaderFooter + ? { + displayHeaderFooter: true, + // Chromium falls back to its own date/title header when a + // template is missing, so always pass both. + headerTemplate: request.headerTemplate ?? '', + footerTemplate: request.footerTemplate ?? '', + } + : {}), }) await writeFile(selection.filePath, pdf) return { canceled: false, path: selection.filePath } diff --git a/apps/sheets/src/main/sheets-main.ts b/apps/sheets/src/main/sheets-main.ts index 8cd8aed..7b0dee7 100644 --- a/apps/sheets/src/main/sheets-main.ts +++ b/apps/sheets/src/main/sheets-main.ts @@ -110,6 +110,7 @@ import { screenCaptureResultSchema, screenSourcesResultSchema, workbookPivotDefinitionSchema, + workbookExportCsvRequestSchema, workbookExportPdfRequestSchema, workbookRangeRequestSchema, workbookRangeResultSchema, @@ -120,6 +121,7 @@ import { type WorkbookSaveRequest, } from '../shared/desktop-api' import { IPC_CHANNELS } from '../shared/ipc-channels' +import { atomicWriteFile } from './atomic-write' import { closeGuardDecision } from './close-guard' import { SaveEditsTransferStore } from './save-edits-transfer' import { exportPdf } from './pdf-export' @@ -185,6 +187,16 @@ const tMain = createI18n({ btnDontSave: '不保存', btnCancel: '取消', csvSaveAsNotice: 'CSV 格式不保留样式等格式修改——另存为 .xlsx 可保留全部内容。', + menuExportCsv: '导出 CSV…', + filterCsv: 'CSV (逗号分隔)', + csvFormulaLossMsg: '当前工作表包含公式,CSV 格式无法保留。', + csvFormulaLossDetail: 'CSV 只保留纯文本值——公式会被替换为当前计算结果,格式也会丢失。', + csvKeepXlsxBtn: '另存为 .xlsx', + csvContinueBtn: '继续保存为 CSV', + csvActiveSheetOnlyNotice: 'CSV 文件只包含一张工作表——只会导出当前工作表“{name}”。', + csvKeepFormatMsg: '继续以 CSV 格式保存吗?', + csvKeepFormatDetail: + 'CSV 只保留单张工作表的纯文本值——公式、格式和其他工作表不会存入 .csv 文件。', }, en: { filterSpreadsheets: 'Spreadsheets', @@ -231,6 +243,18 @@ const tMain = createI18n({ btnDontSave: "Don't Save", btnCancel: 'Cancel', csvSaveAsNotice: "CSV files can't keep formatting — saving as .xlsx keeps all your changes.", + menuExportCsv: 'Export CSV…', + filterCsv: 'CSV (Comma delimited)', + csvFormulaLossMsg: 'This sheet contains formulas that CSV cannot keep.', + csvFormulaLossDetail: + 'CSV keeps plain values only — formulas are flattened to their current results, and formatting is lost.', + csvKeepXlsxBtn: 'Save as .xlsx', + csvContinueBtn: 'Continue as CSV', + csvActiveSheetOnlyNotice: + 'CSV files hold a single sheet — only the active sheet "{name}" will be exported.', + csvKeepFormatMsg: 'Keep saving in CSV format?', + csvKeepFormatDetail: + 'CSV keeps plain values of a single sheet only — formulas, formatting, and any additional sheets are not saved to the .csv file.', }, ja: { filterSpreadsheets: 'スプレッドシート', @@ -280,6 +304,18 @@ const tMain = createI18n({ btnCancel: 'キャンセル', csvSaveAsNotice: 'CSV 形式は書式を保持できません。.xlsx として保存すると変更をすべて保持できます。', + menuExportCsv: 'CSV をエクスポート…', + filterCsv: 'CSV (コンマ区切り)', + csvFormulaLossMsg: 'このシートには CSV 形式では保持できない数式が含まれています。', + csvFormulaLossDetail: + 'CSV は値のみを保持します。数式は現在の計算結果に置き換えられ、書式も失われます。', + csvKeepXlsxBtn: '.xlsx として保存', + csvContinueBtn: 'CSV のまま保存', + csvActiveSheetOnlyNotice: + 'CSV ファイルには 1 枚のシートしか含められません — アクティブなシート「{name}」のみがエクスポートされます。', + csvKeepFormatMsg: 'CSV 形式のまま保存しますか?', + csvKeepFormatDetail: + 'CSV は 1 枚のシートの値のみを保持します。数式、書式、追加のシートは .csv ファイルには保存されません。', }, ko: { filterSpreadsheets: '스프레드시트', @@ -329,6 +365,18 @@ const tMain = createI18n({ btnCancel: '취소', csvSaveAsNotice: 'CSV 형식은 서식을 저장할 수 없습니다. .xlsx로 저장하면 모든 변경 내용이 유지됩니다.', + menuExportCsv: 'CSV 내보내기…', + filterCsv: 'CSV (쉼표로 분리)', + csvFormulaLossMsg: '현재 시트에 CSV 형식이 유지할 수 없는 수식이 포함되어 있습니다.', + csvFormulaLossDetail: + 'CSV는 값만 유지합니다 — 수식은 현재 계산 결과로 바뀌고 서식은 손실됩니다.', + csvKeepXlsxBtn: '.xlsx로 저장', + csvContinueBtn: 'CSV로 계속 저장', + csvActiveSheetOnlyNotice: + 'CSV 파일에는 시트 하나만 포함됩니다 — 활성 시트 "{name}"만 내보냅니다.', + csvKeepFormatMsg: 'CSV 형식으로 계속 저장하시겠습니까?', + csvKeepFormatDetail: + 'CSV는 시트 하나의 값만 유지합니다 — 수식, 서식, 추가 시트는 .csv 파일에 저장되지 않습니다.', }, fr: { filterSpreadsheets: 'Feuilles de calcul', @@ -378,6 +426,19 @@ const tMain = createI18n({ btnCancel: 'Annuler', csvSaveAsNotice: 'Le format CSV ne conserve pas la mise en forme — enregistrez en .xlsx pour conserver toutes vos modifications.', + menuExportCsv: 'Exporter en CSV…', + filterCsv: 'CSV (délimité par des virgules)', + csvFormulaLossMsg: + 'Cette feuille contient des formules que le format CSV ne peut pas conserver.', + csvFormulaLossDetail: + 'Le CSV ne conserve que les valeurs — les formules sont remplacées par leur résultat actuel et la mise en forme est perdue.', + csvKeepXlsxBtn: 'Enregistrer en .xlsx', + csvContinueBtn: 'Continuer en CSV', + csvActiveSheetOnlyNotice: + "Les fichiers CSV ne contiennent qu'une seule feuille — seule la feuille active « {name} » sera exportée.", + csvKeepFormatMsg: 'Continuer à enregistrer au format CSV ?', + csvKeepFormatDetail: + "Le CSV ne conserve que les valeurs d'une seule feuille — les formules, la mise en forme et les feuilles supplémentaires ne sont pas enregistrées dans le fichier .csv.", }, de: { filterSpreadsheets: 'Tabellenkalkulationen', @@ -427,6 +488,18 @@ const tMain = createI18n({ btnCancel: 'Abbrechen', csvSaveAsNotice: 'CSV-Dateien können keine Formatierung speichern – als .xlsx speichern, um alle Änderungen zu behalten.', + menuExportCsv: 'CSV exportieren…', + filterCsv: 'CSV (Trennzeichen-getrennt)', + csvFormulaLossMsg: 'Dieses Blatt enthält Formeln, die das CSV-Format nicht speichern kann.', + csvFormulaLossDetail: + 'CSV speichert nur reine Werte – Formeln werden durch ihre aktuellen Ergebnisse ersetzt, und die Formatierung geht verloren.', + csvKeepXlsxBtn: 'Als .xlsx speichern', + csvContinueBtn: 'Als CSV fortfahren', + csvActiveSheetOnlyNotice: + 'CSV-Dateien enthalten nur ein Blatt – nur das aktive Blatt „{name}“ wird exportiert.', + csvKeepFormatMsg: 'Weiter im CSV-Format speichern?', + csvKeepFormatDetail: + 'CSV speichert nur die Werte eines einzelnen Blatts – Formeln, Formatierungen und weitere Blätter werden nicht in der .csv-Datei gespeichert.', }, es: { filterSpreadsheets: 'Hojas de cálculo', @@ -475,6 +548,18 @@ const tMain = createI18n({ btnCancel: 'Cancelar', csvSaveAsNotice: 'El formato CSV no conserva el formato: guarda como .xlsx para conservar todos tus cambios.', + menuExportCsv: 'Exportar a CSV…', + filterCsv: 'CSV (delimitado por comas)', + csvFormulaLossMsg: 'Esta hoja contiene fórmulas que el formato CSV no puede conservar.', + csvFormulaLossDetail: + 'El CSV solo conserva valores: las fórmulas se sustituyen por sus resultados actuales y el formato se pierde.', + csvKeepXlsxBtn: 'Guardar como .xlsx', + csvContinueBtn: 'Continuar como CSV', + csvActiveSheetOnlyNotice: + 'Los archivos CSV solo contienen una hoja: solo se exportará la hoja activa «{name}».', + csvKeepFormatMsg: '¿Seguir guardando en formato CSV?', + csvKeepFormatDetail: + 'CSV solo conserva los valores de una única hoja: las fórmulas, el formato y las hojas adicionales no se guardan en el archivo .csv.', }, th: { filterSpreadsheets: 'สเปรดชีต', @@ -523,6 +608,18 @@ const tMain = createI18n({ btnCancel: 'ยกเลิก', csvSaveAsNotice: 'ไฟล์ CSV ไม่สามารถเก็บการจัดรูปแบบได้ — บันทึกเป็น .xlsx เพื่อเก็บการเปลี่ยนแปลงทั้งหมดของคุณ', + menuExportCsv: 'ส่งออก CSV…', + filterCsv: 'CSV (คั่นด้วยเครื่องหมายจุลภาค)', + csvFormulaLossMsg: 'ชีตนี้มีสูตรที่รูปแบบ CSV เก็บไว้ไม่ได้', + csvFormulaLossDetail: + 'CSV เก็บเฉพาะค่าเท่านั้น — สูตรจะถูกแทนที่ด้วยผลลัพธ์ปัจจุบัน และการจัดรูปแบบจะหายไป', + csvKeepXlsxBtn: 'บันทึกเป็น .xlsx', + csvContinueBtn: 'บันทึกเป็น CSV ต่อไป', + csvActiveSheetOnlyNotice: + 'ไฟล์ CSV มีได้เพียงชีตเดียว — จะส่งออกเฉพาะชีตที่ใช้งานอยู่ “{name}” เท่านั้น', + csvKeepFormatMsg: 'บันทึกเป็นรูปแบบ CSV ต่อไปหรือไม่', + csvKeepFormatDetail: + 'CSV เก็บเฉพาะค่าของชีตเดียวเท่านั้น — สูตร การจัดรูปแบบ และชีตอื่น ๆ จะไม่ถูกบันทึกลงในไฟล์ .csv', }, id: { filterSpreadsheets: 'Lembar bentang', @@ -569,6 +666,18 @@ const tMain = createI18n({ btnCancel: 'Batal', csvSaveAsNotice: 'File CSV tidak dapat menyimpan pemformatan — simpan sebagai .xlsx untuk mempertahankan semua perubahan Anda.', + menuExportCsv: 'Ekspor CSV…', + filterCsv: 'CSV (dipisahkan koma)', + csvFormulaLossMsg: 'Lembar ini berisi rumus yang tidak dapat disimpan dalam format CSV.', + csvFormulaLossDetail: + 'CSV hanya menyimpan nilai — rumus diganti dengan hasil saat ini, dan pemformatan akan hilang.', + csvKeepXlsxBtn: 'Simpan sebagai .xlsx', + csvContinueBtn: 'Lanjutkan sebagai CSV', + csvActiveSheetOnlyNotice: + 'File CSV hanya memuat satu lembar — hanya lembar aktif “{name}” yang akan diekspor.', + csvKeepFormatMsg: 'Terus menyimpan dalam format CSV?', + csvKeepFormatDetail: + 'CSV hanya menyimpan nilai dari satu lembar — rumus, pemformatan, dan lembar tambahan tidak disimpan ke file .csv.', }, ru: { filterSpreadsheets: 'Электронные таблицы', @@ -617,6 +726,18 @@ const tMain = createI18n({ btnCancel: 'Отмена', csvSaveAsNotice: 'Формат CSV не сохраняет форматирование — сохраните в .xlsx, чтобы не потерять изменения.', + menuExportCsv: 'Экспорт в CSV…', + filterCsv: 'CSV (разделители — запятые)', + csvFormulaLossMsg: 'Этот лист содержит формулы, которые формат CSV не сохраняет.', + csvFormulaLossDetail: + 'CSV сохраняет только значения — формулы заменяются текущими результатами, а форматирование теряется.', + csvKeepXlsxBtn: 'Сохранить как .xlsx', + csvContinueBtn: 'Продолжить в CSV', + csvActiveSheetOnlyNotice: + 'Файлы CSV содержат только один лист — будет экспортирован только активный лист «{name}».', + csvKeepFormatMsg: 'Продолжить сохранение в формате CSV?', + csvKeepFormatDetail: + 'CSV сохраняет только значения одного листа — формулы, форматирование и дополнительные листы не сохраняются в файле .csv.', }, ar: { filterSpreadsheets: 'جداول البيانات', @@ -663,6 +784,17 @@ const tMain = createI18n({ btnDontSave: 'عدم الحفظ', btnCancel: 'إلغاء', csvSaveAsNotice: 'ملفات CSV لا تحتفظ بالتنسيق — احفظ بصيغة ‎.xlsx للاحتفاظ بجميع تغييراتك.', + menuExportCsv: 'تصدير CSV…', + filterCsv: 'CSV (محدد بفواصل)', + csvFormulaLossMsg: 'تحتوي هذه الورقة على صيغ لا يمكن لتنسيق CSV الاحتفاظ بها.', + csvFormulaLossDetail: 'يحتفظ CSV بالقيم فقط — تُستبدل الصيغ بنتائجها الحالية ويُفقد التنسيق.', + csvKeepXlsxBtn: 'حفظ بصيغة .xlsx', + csvContinueBtn: 'المتابعة بتنسيق CSV', + csvActiveSheetOnlyNotice: + 'ملفات CSV تحتوي على ورقة واحدة فقط — سيتم تصدير الورقة النشطة «{name}» فقط.', + csvKeepFormatMsg: 'هل تريد متابعة الحفظ بتنسيق CSV؟', + csvKeepFormatDetail: + 'يحتفظ CSV بقيم ورقة واحدة فقط — لا تُحفظ الصيغ والتنسيق والأوراق الإضافية في ملف .csv.', }, pt: { filterSpreadsheets: 'Planilhas', @@ -711,6 +843,18 @@ const tMain = createI18n({ btnCancel: 'Cancelar', csvSaveAsNotice: 'Arquivos CSV não mantêm a formatação — salve como .xlsx para manter todas as suas alterações.', + menuExportCsv: 'Exportar CSV…', + filterCsv: 'CSV (separado por vírgulas)', + csvFormulaLossMsg: 'Esta planilha contém fórmulas que o formato CSV não pode manter.', + csvFormulaLossDetail: + 'O CSV mantém apenas valores — as fórmulas são substituídas pelos resultados atuais e a formatação é perdida.', + csvKeepXlsxBtn: 'Salvar como .xlsx', + csvContinueBtn: 'Continuar como CSV', + csvActiveSheetOnlyNotice: + 'Arquivos CSV contêm apenas uma planilha — apenas a planilha ativa “{name}” será exportada.', + csvKeepFormatMsg: 'Continuar salvando no formato CSV?', + csvKeepFormatDetail: + 'O CSV mantém apenas os valores de uma única planilha — fórmulas, formatação e planilhas adicionais não são salvas no arquivo .csv.', }, it: { filterSpreadsheets: 'Fogli di calcolo', @@ -760,6 +904,18 @@ const tMain = createI18n({ btnCancel: 'Annulla', csvSaveAsNotice: 'I file CSV non conservano la formattazione: salva come .xlsx per mantenere tutte le modifiche.', + menuExportCsv: 'Esporta CSV…', + filterCsv: 'CSV (delimitato da virgole)', + csvFormulaLossMsg: 'Questo foglio contiene formule che il formato CSV non può conservare.', + csvFormulaLossDetail: + 'Il CSV conserva solo i valori: le formule vengono sostituite dai risultati attuali e la formattazione viene persa.', + csvKeepXlsxBtn: 'Salva come .xlsx', + csvContinueBtn: 'Continua come CSV', + csvActiveSheetOnlyNotice: + 'I file CSV contengono un solo foglio: verrà esportato solo il foglio attivo “{name}”.', + csvKeepFormatMsg: 'Continuare a salvare in formato CSV?', + csvKeepFormatDetail: + 'Il CSV conserva solo i valori di un singolo foglio: formule, formattazione e fogli aggiuntivi non vengono salvati nel file .csv.', }, pl: { filterSpreadsheets: 'Arkusze kalkulacyjne', @@ -808,6 +964,18 @@ const tMain = createI18n({ btnCancel: 'Anuluj', csvSaveAsNotice: 'Pliki CSV nie zachowują formatowania — zapisz jako .xlsx, aby zachować wszystkie zmiany.', + menuExportCsv: 'Eksportuj CSV…', + filterCsv: 'CSV (rozdzielany przecinkami)', + csvFormulaLossMsg: 'Ten arkusz zawiera formuły, których format CSV nie zachowuje.', + csvFormulaLossDetail: + 'CSV zachowuje tylko wartości — formuły są zastępowane bieżącymi wynikami, a formatowanie jest tracone.', + csvKeepXlsxBtn: 'Zapisz jako .xlsx', + csvContinueBtn: 'Kontynuuj jako CSV', + csvActiveSheetOnlyNotice: + 'Pliki CSV zawierają tylko jeden arkusz — wyeksportowany zostanie tylko aktywny arkusz „{name}”.', + csvKeepFormatMsg: 'Kontynuować zapisywanie w formacie CSV?', + csvKeepFormatDetail: + 'CSV zachowuje tylko wartości jednego arkusza — formuły, formatowanie i dodatkowe arkusze nie są zapisywane w pliku .csv.', }, nl: { filterSpreadsheets: 'Spreadsheets', @@ -857,6 +1025,18 @@ const tMain = createI18n({ btnCancel: 'Annuleren', csvSaveAsNotice: 'CSV-bestanden bewaren geen opmaak — sla op als .xlsx om al uw wijzigingen te behouden.', + menuExportCsv: 'CSV exporteren…', + filterCsv: 'CSV (kommagescheiden)', + csvFormulaLossMsg: 'Dit blad bevat formules die het CSV-formaat niet kan bewaren.', + csvFormulaLossDetail: + 'CSV bewaart alleen waarden — formules worden vervangen door hun huidige resultaten en opmaak gaat verloren.', + csvKeepXlsxBtn: 'Opslaan als .xlsx', + csvContinueBtn: 'Doorgaan als CSV', + csvActiveSheetOnlyNotice: + 'CSV-bestanden bevatten slechts één blad — alleen het actieve blad “{name}” wordt geëxporteerd.', + csvKeepFormatMsg: 'Doorgaan met opslaan in CSV-indeling?', + csvKeepFormatDetail: + 'CSV bewaart alleen de waarden van één blad — formules, opmaak en extra bladen worden niet in het .csv-bestand opgeslagen.', }, ms: { filterSpreadsheets: 'Hamparan', @@ -904,6 +1084,19 @@ const tMain = createI18n({ btnCancel: 'Batal', csvSaveAsNotice: 'Fail CSV tidak dapat menyimpan pemformatan — simpan sebagai .xlsx untuk mengekalkan semua perubahan anda.', + menuExportCsv: 'Eksport CSV…', + filterCsv: 'CSV (dipisahkan koma)', + csvFormulaLossMsg: + 'Helaian ini mengandungi formula yang tidak dapat disimpan dalam format CSV.', + csvFormulaLossDetail: + 'CSV hanya menyimpan nilai — formula digantikan dengan hasil semasa, dan pemformatan akan hilang.', + csvKeepXlsxBtn: 'Simpan sebagai .xlsx', + csvContinueBtn: 'Teruskan sebagai CSV', + csvActiveSheetOnlyNotice: + 'Fail CSV hanya mengandungi satu helaian — hanya helaian aktif “{name}” akan dieksport.', + csvKeepFormatMsg: 'Terus simpan dalam format CSV?', + csvKeepFormatDetail: + 'CSV hanya menyimpan nilai satu helaian — formula, pemformatan dan helaian tambahan tidak disimpan ke fail .csv.', }, he: { filterSpreadsheets: 'גיליונות אלקטרוניים', @@ -949,6 +1142,17 @@ const tMain = createI18n({ btnDontSave: 'אל תשמור', btnCancel: 'ביטול', csvSaveAsNotice: 'קובצי CSV אינם שומרים עיצוב — שמרו כ‑.xlsx כדי לשמור על כל השינויים.', + menuExportCsv: 'ייצוא CSV…', + filterCsv: 'CSV (מופרד באמצעות פסיקים)', + csvFormulaLossMsg: 'גיליון זה מכיל נוסחאות שתבנית CSV אינה יכולה לשמור.', + csvFormulaLossDetail: + 'CSV שומר ערכים בלבד — נוסחאות מוחלפות בתוצאות הנוכחיות שלהן, והעיצוב אובד.', + csvKeepXlsxBtn: 'שמירה כ-.xlsx', + csvContinueBtn: 'המשך שמירה כ-CSV', + csvActiveSheetOnlyNotice: 'קובצי CSV מכילים גיליון אחד בלבד — רק הגיליון הפעיל "{name}" ייוצא.', + csvKeepFormatMsg: 'להמשיך לשמור בתבנית CSV?', + csvKeepFormatDetail: + 'CSV שומר רק את הערכים של גיליון אחד — נוסחאות, עיצוב וגיליונות נוספים אינם נשמרים בקובץ ה-.csv.', }, hi: { filterSpreadsheets: 'स्प्रेडशीट', @@ -997,6 +1201,18 @@ const tMain = createI18n({ btnCancel: 'रद्द करें', csvSaveAsNotice: 'CSV फ़ाइलें फ़ॉर्मेटिंग सहेज नहीं सकतीं — सभी बदलाव बनाए रखने के लिए .xlsx के रूप में सहेजें।', + menuExportCsv: 'CSV निर्यात करें…', + filterCsv: 'CSV (अल्पविराम द्वारा सीमांकित)', + csvFormulaLossMsg: 'इस शीट में ऐसे सूत्र हैं जिन्हें CSV प्रारूप सहेज नहीं सकता।', + csvFormulaLossDetail: + 'CSV केवल मान रखता है — सूत्र उनके वर्तमान परिणामों से बदल दिए जाते हैं और फ़ॉर्मेटिंग खो जाती है।', + csvKeepXlsxBtn: '.xlsx के रूप में सहेजें', + csvContinueBtn: 'CSV के रूप में जारी रखें', + csvActiveSheetOnlyNotice: + 'CSV फ़ाइलों में केवल एक शीट होती है — केवल सक्रिय शीट “{name}” निर्यात की जाएगी।', + csvKeepFormatMsg: 'CSV प्रारूप में सहेजना जारी रखें?', + csvKeepFormatDetail: + 'CSV केवल एक शीट के मान रखता है — सूत्र, स्वरूपण और अतिरिक्त शीट .csv फ़ाइल में सहेजे नहीं जाते।', }, 'zh-TW': { filterSpreadsheets: '電子試算表', @@ -1042,6 +1258,15 @@ const tMain = createI18n({ btnDontSave: '不儲存', btnCancel: '取消', csvSaveAsNotice: 'CSV 格式不保留樣式等格式修改——另存為 .xlsx 可保留全部內容。', + menuExportCsv: '匯出 CSV…', + filterCsv: 'CSV (逗號分隔)', + csvFormulaLossMsg: '目前工作表包含公式,CSV 格式無法保留。', + csvFormulaLossDetail: 'CSV 只保留純文字值——公式會被取代為目前計算結果,格式也會遺失。', + csvKeepXlsxBtn: '另存為 .xlsx', + csvContinueBtn: '繼續儲存為 CSV', + csvActiveSheetOnlyNotice: 'CSV 檔案只包含一張工作表——只會匯出目前工作表「{name}」。', + csvKeepFormatMsg: '要繼續以 CSV 格式儲存嗎?', + csvKeepFormatDetail: 'CSV 只保留單張工作表的純值——公式、格式和其他工作表不會存入 .csv 檔案。', }, }) const tm = (key: Parameters[1], params?: Parameters[2]) => @@ -1058,12 +1283,19 @@ interface SessionInfo { readonly sha256: string readonly sheetNames: ReadonlyMap readonly automaticRecoveryDisabled: boolean - /// Set when the session opened a converted copy (.xls/.csv import): the + /// Set when the session opened a converted copy (.xls import): the /// first save routes through Save As, defaulting to this .xlsx path. readonly suggestSaveAs?: string /// The converted copy came from a CSV: the Save As dialog explains that /// formatting requires .xlsx (CSV keeps values only). readonly csvImport?: boolean + /// CSV session: the original .csv on disk. Save keeps the CSV identity — + /// the xlsx save lands on the temp copy and the serialized csvContent is + /// written back here. + readonly csvSourcePath?: string + /// Digest of the original .csv at open/save time — guards the write-back + /// against external modification, like restoreTargetSha. + readonly csvSourceSha?: string /// App-owned directory containing the converted CSV/XLS copy. Removed only /// after the sidecar session and its independent snapshot are closed. readonly importTempDir?: string @@ -1273,7 +1505,7 @@ export function setSheetsWorkbookOpenedHook( /** forward an application-menu File command into the sheets renderer */ export function sendSheetsMenuAction( - action: 'open' | 'save' | 'save-as' | 'export-pdf' | 'undo' | 'redo', + action: 'open' | 'save' | 'save-as' | 'export-pdf' | 'export-csv' | 'undo' | 'redo', ): void { activeSheetsWebContents?.send(IPC_CHANNELS.menuAction, action) } @@ -1447,6 +1679,7 @@ function startCaptureServer(): void { action === 'save' || action === 'save-as' || action === 'export-pdf' || + action === 'export-csv' || action === 'undo' || action === 'redo' ) { @@ -1908,6 +2141,7 @@ export function registerSheetsIpc(): void { const result = await openWorkbookSession(entry.client, prepared.openPath, entry.sessions, { suggestSaveAs: prepared.suggestSaveAs, csvImport: prepared.csvImport, + csvSourcePath: prepared.csvSourcePath, importTempDir: prepared.importTempDir, restoreTarget: prepared.restoreTarget, }) @@ -2154,6 +2388,87 @@ export function registerSheetsIpc(): void { return result }) + ipcMain.handle(IPC_CHANNELS.exportCsv, async (event, input: unknown) => { + const entry = sessionFor(event) + const request = workbookExportCsvRequestSchema.parse(input) + const parent = dialogParent(event) + if (request.hasFormulas) { + // Excel's CSV warning flow: offer keeping the formulas via .xlsx first. + const options = { + type: 'warning' as const, + message: tm('csvFormulaLossMsg'), + detail: tm('csvFormulaLossDetail'), + buttons: [tm('csvKeepXlsxBtn'), tm('csvContinueBtn'), tm('btnCancel')], + defaultId: 0, + cancelId: 2, + noLink: true, + } + const { response } = parent + ? await dialog.showMessageBox(parent, options) + : await dialog.showMessageBox(options) + if (response === 0) return { canceled: true, saveAsXlsxInstead: true } + if (response === 2) return { canceled: true } + } + let pickedPath = request.targetPath + if (pickedPath === undefined) { + const selection = await saveFileDialog(event, { + defaultPath: request.fileName, + filters: [{ name: tm('filterCsv'), extensions: ['csv'] }], + ...(request.activeSheetName + ? { + title: tm('csvActiveSheetOnlyNotice', { name: request.activeSheetName }), + message: tm('csvActiveSheetOnlyNotice', { name: request.activeSheetName }), + } + : {}), + }) + if (selection.canceled || !selection.filePath) return { canceled: true } + pickedPath = selection.filePath + } + const targetPath = pickedPath.toLowerCase().endsWith('.csv') ? pickedPath : `${pickedPath}.csv` + // UTF-8 BOM so Excel decodes the reopened file correctly. Written beside + // the destination and renamed into place: a plain writeFile creates the + // (empty) file before the data lands, and anything watching for the + // export — the e2e retry loop included — can read zero bytes in that + // window. + const csvBytes = Buffer.concat([ + Buffer.from([0xef, 0xbb, 0xbf]), + Buffer.from(request.content, 'utf8'), + ]) + await atomicWriteFile(targetPath, csvBytes) + // An export can land on a CSV session's own source file — refresh that + // session's guard digest so its next Save doesn't mistake this write for + // an external change. + const writtenSha = await sha256File(targetPath).catch(() => undefined) + if (writtenSha !== undefined) { + for (const [sessionId, session] of entry.sessions) { + if (session.csvSourcePath === targetPath) { + entry.sessions.set(sessionId, { ...session, csvSourceSha: writtenSha }) + } + } + } + return { canceled: false, path: targetPath } + }) + + // First Save of a CSV session: Excel's "keep this format?" question. The + // renderer remembers the answer for the file, so it is asked once. + ipcMain.handle(IPC_CHANNELS.csvSaveConfirm, async (event) => { + sessionFor(event) + const options = { + type: 'warning' as const, + message: tm('csvKeepFormatMsg'), + detail: tm('csvKeepFormatDetail'), + buttons: [tm('csvContinueBtn'), tm('csvKeepXlsxBtn'), tm('btnCancel')], + defaultId: 0, + cancelId: 2, + noLink: true, + } + const parent = dialogParent(event) + const { response } = parent + ? await dialog.showMessageBox(parent, options) + : await dialog.showMessageBox(options) + return response === 0 ? 'csv' : response === 1 ? 'xlsx' : 'cancel' + }) + ipcMain.handle(IPC_CHANNELS.saveWorkbook, async (event, input: unknown) => { const entry = sessionFor(event) const client = entry.client @@ -2161,9 +2476,13 @@ export function registerSheetsIpc(): void { const session = entry.sessions.get(request.sessionId) if (!session) throw new Error('Unknown workbook session.') + // A CSV session's plain Save keeps the CSV identity: the xlsx save lands + // on the temp copy and the serialized csvContent is written back to the + // original .csv afterwards. + const csvInPlace = request.mode === 'save' && session.csvSourcePath !== undefined let targetPath = session.path - // Converted imports (.xls/.csv) never save silently over the temp copy — - // the first save always asks where the .xlsx should live. + // Converted .xls imports never save silently over the temp copy — the + // first save always asks where the .xlsx should live. if (request.mode === 'save-as' || session.suggestSaveAs !== undefined) { // .xlsm keeps its extension: untouched archive entries (vbaProject.bin, // the macro-enabled content type) round-trip verbatim through the save. @@ -2172,14 +2491,28 @@ export function registerSheetsIpc(): void { ) const ext = macroEnabled ? 'xlsm' : 'xlsx' const selection = await saveFileDialog(event, { - defaultPath: session.suggestSaveAs ?? session.restoreTarget ?? session.path, - filters: [{ name: tm(macroEnabled ? 'filterXlsm' : 'filterXlsx'), extensions: [ext] }], + defaultPath: + session.suggestSaveAs ?? + session.csvSourcePath?.replace(/\.[^.]+$/, '.xlsx') ?? + session.restoreTarget ?? + session.path, + filters: macroEnabled + ? [{ name: tm('filterXlsm'), extensions: ['xlsm'] }] + : [ + { name: tm('filterXlsx'), extensions: ['xlsx'] }, + { name: tm('filterCsv'), extensions: ['csv'] }, + ], // CSV import: explain why the save goes through .xlsx (CSV keeps values only) ...(session.csvImport ? { title: tm('csvSaveAsNotice'), message: tm('csvSaveAsNotice') } : {}), }) if (selection.canceled || !selection.filePath) return { canceled: true } + // A CSV pick can't ride the xlsx pipeline: hand the path back so the + // renderer serializes the active sheet through the CSV export channel. + if (!macroEnabled && selection.filePath.toLowerCase().endsWith('.csv')) { + return { canceled: true, csvSaveAsPath: selection.filePath } + } targetPath = selection.filePath.toLowerCase().endsWith(`.${ext}`) ? selection.filePath : `${selection.filePath}.${ext}` @@ -2204,23 +2537,64 @@ export function registerSheetsIpc(): void { throw new Error(tm('errDiskChanged')) } } + // The CSV write-back gets the same external-change guard as restoreTarget; + // a deleted .csv is fine — the write recreates it. + if (csvInPlace && session.csvSourcePath !== undefined) { + const csvSha = await sha256File(session.csvSourcePath).catch(() => undefined) + if (csvSha !== undefined && csvSha !== session.csvSourceSha) { + throw new Error(tm('errDiskChanged')) + } + } const mutation = await writeWorkbookTo(client, session, request, targetPath) + if (csvInPlace && session.csvSourcePath !== undefined && request.csvContent !== undefined) { + // The temp copy already holds the saved bytes: refresh the session's + // guard digest first, so a failed CSV write-back below leaves a + // retryable session instead of stranding the next Save on + // errDiskChanged against its own write. + const savedSha = await sha256File(session.path).catch(() => undefined) + if (savedSha !== undefined && entry.sessions.has(request.sessionId)) { + entry.sessions.set(request.sessionId, { ...session, sha256: savedSha }) + } + // UTF-8 BOM so Excel decodes the reopened file correctly. + await writeFile( + session.csvSourcePath, + Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from(request.csvContent, 'utf8')]), + ) + } + // The sidecar session still streams the pre-save bytes; swap it for a // fresh session over the saved file so future reads match the disk state. entry.sessions.delete(request.sessionId) await cleanupSessionResources({ tempRoot: app.getPath('temp'), snapshotPath: session.snapshotPath, - importTempDir: session.importTempDir, + // A CSV in-place save keeps saving into the temp copy — its directory + // must survive the session swap. + importTempDir: csvInPlace ? undefined : session.importTempDir, closeSidecar: () => client.close(request.sessionId), }) - const file = await openWorkbookSession(client, targetPath, entry.sessions) + const file = await openWorkbookSession( + client, + targetPath, + entry.sessions, + csvInPlace + ? { + csvImport: true, + csvSourcePath: session.csvSourcePath, + importTempDir: session.importTempDir, + } + : undefined, + ) // Notify shell (if running) so it can update the tab title and record the // saved path in recent files (mirrors the open hook; covers Save As + first - // save after converting an .xls/.csv import). - workbookOpenedHook?.(event.sender, targetPath) + // save after converting an .xls/.csv import). A CSV session's user-visible + // file is the original .csv, not the temp copy the xlsx save landed on. + workbookOpenedHook?.( + event.sender, + (csvInPlace ? session.csvSourcePath : undefined) ?? targetPath, + ) // The file on disk now carries these edits clearWorkbookRecovery(targetPath) if (session.suggestSaveAs !== undefined) clearWorkbookRecovery(session.suggestSaveAs) @@ -2260,10 +2634,16 @@ export function registerSheetsIpc(): void { const entry = sessionFor(event) const request = resolveTransferredEdits(entry, workbookSaveRequestSchema.parse(input)) const session = entry.sessions.get(request.sessionId) - // A converted import has no original file to recover into; a restored - // recovery session is backed by the recovery copy itself — writing over - // the file the sidecar streams from would corrupt the open session. - if (!session || session.suggestSaveAs !== undefined || session.restoreTarget !== undefined) + // A converted import has no original file to recover into (and a CSV + // session's original can't hold the workbook bytes); a restored recovery + // session is backed by the recovery copy itself — writing over the file + // the sidecar streams from would corrupt the open session. + if ( + !session || + session.suggestSaveAs !== undefined || + session.csvSourcePath !== undefined || + session.restoreTarget !== undefined + ) return { ok: false } if (session.automaticRecoveryDisabled) return { ok: false } try { @@ -2523,6 +2903,7 @@ export function registerSheetsAiIpc(): void { await streamForProvider(provider, config, system, messages, tools, maxTokens, { signal: controller.signal, onDelta: (text) => send({ requestId, type: 'delta', text }), + onReasoningDelta: (text) => send({ requestId, type: 'reasoning', text }), onToolCall: (toolCall) => send({ requestId, type: 'tool-call', toolCall }), onActivity: ping, }) @@ -3025,18 +3406,19 @@ async function openWorkbookSession( options?: { suggestSaveAs?: string | undefined csvImport?: boolean | undefined + csvSourcePath?: string | undefined importTempDir?: string | undefined restoreTarget?: string | undefined }, ): Promise { - const { suggestSaveAs, csvImport, importTempDir, restoreTarget } = options ?? {} + const { suggestSaveAs, csvImport, csvSourcePath, importTempDir, restoreTarget } = options ?? {} // Snapshot first, then the sidecar opens the snapshot (not the live path): // everything the session serves — cell reads, media, recalc, saves — comes // from the same bytes, even if the file on disk changes right after the // copy. The digest also describes exactly those bytes. const snapshotPath = await snapshotWorkbook(path) try { - const [opened, digest, restoreTargetSha] = await Promise.all([ + const [opened, digest, restoreTargetSha, csvSourceSha] = await Promise.all([ client .open(snapshotPath, getUiLang(), systemShortDate()) .then((result) => sidecarOpenResultSchema.parse(result)), @@ -3045,6 +3427,9 @@ async function openWorkbookSession( restoreTarget === undefined ? Promise.resolve(undefined) : sha256File(restoreTarget).catch(() => undefined), + csvSourcePath === undefined + ? Promise.resolve(undefined) + : sha256File(csvSourcePath).catch(() => undefined), ]) sessions.set(opened.sessionId, { path, @@ -3054,6 +3439,8 @@ async function openWorkbookSession( automaticRecoveryDisabled: !allowsAutomaticWorkbookRecovery(opened.sheets), ...(suggestSaveAs === undefined ? {} : { suggestSaveAs }), ...(csvImport ? { csvImport } : {}), + ...(csvSourcePath === undefined ? {} : { csvSourcePath }), + ...(csvSourceSha === undefined ? {} : { csvSourceSha }), ...(importTempDir === undefined ? {} : { importTempDir }), ...(restoreTarget === undefined ? {} : { restoreTarget }), ...(restoreTargetSha === undefined ? {} : { restoreTargetSha }), @@ -3066,6 +3453,7 @@ async function openWorkbookSession( sha256: digest, readOnly: false, needsSaveAs: suggestSaveAs !== undefined, + ...(csvSourcePath === undefined ? {} : { csvPath: csvSourcePath }), restoredFromRecovery: restoreTarget !== undefined, automaticRecoveryDisabled: !allowsAutomaticWorkbookRecovery(opened.sheets), }) @@ -3100,6 +3488,7 @@ async function prepareWorkbookForOpen( openPath: string suggestSaveAs?: string csvImport?: boolean + csvSourcePath?: string importTempDir?: string restoreTarget?: string }> { @@ -3137,12 +3526,12 @@ async function prepareWorkbookForOpen( await cleanupImportTempDirectory(app.getPath('temp'), directory) throw error } - return { - openPath, - importTempDir: directory, - suggestSaveAs: path.replace(/\.[^.]+$/, '.xlsx'), - ...(extension === 'csv' ? { csvImport: true } : {}), - } + // CSV keeps its file identity: Save writes the values back to the original + // .csv (Excel's behavior), so no Save As detour is suggested. Legacy .xls + // still routes the first save through Save As to a fresh .xlsx. + return extension === 'csv' + ? { openPath, importTempDir: directory, csvImport: true, csvSourcePath: path } + : { openPath, importTempDir: directory, suggestSaveAs: path.replace(/\.[^.]+$/, '.xlsx') } } /** shell-injected items appended to the File menu (e.g. Back to Home) */ @@ -3192,6 +3581,10 @@ function installApplicationMenu(): void { label: tm('menuExportPdf'), click: () => sendMenuAction('export-pdf'), }, + { + label: tm('menuExportCsv'), + click: () => sendMenuAction('export-csv'), + }, { type: 'separator' }, closeActiveTabHook ? { diff --git a/apps/sheets/src/preload/index.ts b/apps/sheets/src/preload/index.ts index c740a01..cd5edff 100644 --- a/apps/sheets/src/preload/index.ts +++ b/apps/sheets/src/preload/index.ts @@ -24,6 +24,7 @@ import type { WorkbookFormulaCellsResult, WorkbookMediaRequest, WorkbookMediaResult, + WorkbookPagePrintSettings, WorkbookPivotDefinition, WorkbookPivotRequest, WorkbookRangeRequest, @@ -40,6 +41,7 @@ import type { } from '../shared/desktop-api' import { IPC_CHANNELS, + MAX_CSV_EXPORT_CHARS, MAX_SAVE_EDITS, MAX_SAVE_EDITS_TOTAL, SAVE_EDITS_CHUNK_MAX, @@ -254,7 +256,9 @@ const desktopApi: DesktopApi = { }) || typeof request.scale !== 'number' || request.scale < 0.1 || - request.scale > 2 + request.scale > 2 || + (request.headerTemplate !== undefined && !isBoundedString(request.headerTemplate, 50_000)) || + (request.footerTemplate !== undefined && !isBoundedString(request.footerTemplate, 50_000)) ) { throw new Error('Invalid PDF export request.') } @@ -268,6 +272,43 @@ const desktopApi: DesktopApi = { } return result as { canceled: true } | { canceled: false; path: string } }, + async exportCsv(request) { + if ( + !isRecord(request) || + typeof request.fileName !== 'string' || + request.fileName.length === 0 || + request.fileName.length > 255 || + typeof request.content !== 'string' || + request.content.length > MAX_CSV_EXPORT_CHARS || + typeof request.hasFormulas !== 'boolean' || + (request.activeSheetName !== undefined && + (typeof request.activeSheetName !== 'string' || request.activeSheetName.length > 255)) || + (request.targetPath !== undefined && + (typeof request.targetPath !== 'string' || request.targetPath.length === 0)) + ) { + throw new Error('Invalid CSV export request.') + } + const result: unknown = await ipcRenderer.invoke(IPC_CHANNELS.exportCsv, request) + if ( + !isRecord(result) || + typeof result.canceled !== 'boolean' || + (result.canceled === false && typeof result.path !== 'string') || + (result.canceled === true && + result.saveAsXlsxInstead !== undefined && + typeof result.saveAsXlsxInstead !== 'boolean') + ) { + throw new Error('Invalid CSV export response.') + } + return result as + { canceled: true; saveAsXlsxInstead?: boolean } | { canceled: false; path: string } + }, + async confirmCsvSave() { + const result: unknown = await ipcRenderer.invoke(IPC_CHANNELS.csvSaveConfirm) + if (result !== 'csv' && result !== 'xlsx' && result !== 'cancel') { + throw new Error('Invalid CSV save confirmation response.') + } + return result + }, async closeWorkbook(sessionId) { if (!isUuid(sessionId)) throw new Error('Invalid workbook session.') await ipcRenderer.invoke(IPC_CHANNELS.closeWorkbook, sessionId) @@ -285,6 +326,7 @@ const desktopApi: DesktopApi = { action === 'save' || action === 'save-as' || action === 'export-pdf' || + action === 'export-csv' || action === 'undo' || action === 'redo' ) @@ -564,6 +606,7 @@ function parseWorkbookFile(input: unknown): WorkbookFile { definedNames, readOnly, needsSaveAs, + csvPath, restoredFromRecovery, } = input if ( @@ -580,6 +623,7 @@ function parseWorkbookFile(input: unknown): WorkbookFile { !Array.isArray(definedNames) || typeof readOnly !== 'boolean' || (needsSaveAs !== undefined && typeof needsSaveAs !== 'boolean') || + (csvPath !== undefined && (typeof csvPath !== 'string' || csvPath.length === 0)) || (restoredFromRecovery !== undefined && typeof restoredFromRecovery !== 'boolean') ) { throw new Error('Invalid workbook response.') @@ -781,6 +825,7 @@ function parseWorkbookFile(input: unknown): WorkbookFile { // mean "use the built-in size". defaultRowHeight: normalizedDefaultSize(sheet.defaultRowHeight), defaultColumnWidth: normalizedDefaultSize(sheet.defaultColumnWidth), + baseColumnWidth: normalizedDefaultSize(sheet.baseColumnWidth), freeze: parseFreeze(sheet.freeze), hidden: sheet.hidden, tabColor: sheet.tabColor, @@ -793,6 +838,9 @@ function parseWorkbookFile(input: unknown): WorkbookFile { pivotRanges: sheet.pivotRanges.map(parseCellArea), pivotTables: parsePivotTableInfos(sheet.pivotTables ?? []), sparklines: parseSparklineGroups(sheet.sparklines ?? []), + cellImages: parseCellImages(sheet.cellImages ?? []), + ...(isBoundedString(sheet.printArea, 2_000) ? { printArea: sheet.printArea } : {}), + ...(isBoundedString(sheet.printTitles, 2_000) ? { printTitles: sheet.printTitles } : {}), } }) if (parsedSheets.length === 0) throw new Error('Workbook contains no worksheets.') @@ -852,6 +900,7 @@ function parseWorkbookFile(input: unknown): WorkbookFile { definedNames: parsedDefinedNames, readOnly, ...(needsSaveAs === undefined ? {} : { needsSaveAs }), + ...(csvPath === undefined ? {} : { csvPath }), ...(restoredFromRecovery === undefined ? {} : { restoredFromRecovery }), ...(themeColors === undefined ? {} : { themeColors: themeColors as string[] }), ...(parsedThemeFonts === undefined ? {} : { themeFonts: parsedThemeFonts }), @@ -1195,11 +1244,70 @@ function parseRangeResult(input: unknown): WorkbookRangeResult { rowBreaks: parseBreaks(input.rowBreaks, 'row breaks'), colBreaks: parseBreaks(input.colBreaks, 'column breaks'), protectedRanges, + pageSetup: parsePagePrintSettings(input.pageSetup), indexedThroughRow: input.indexedThroughRow, indexingComplete: input.indexingComplete, } } +function parsePagePrintSettings(input: unknown): WorkbookRangeResult['pageSetup'] { + if (input === null || input === undefined) return null + const boundedInt = (value: unknown, min: number, max: number): boolean => + typeof value === 'number' && Number.isInteger(value) && value >= min && value <= max + if ( + !isRecord(input) || + (input.orientation !== undefined && + input.orientation !== 'portrait' && + input.orientation !== 'landscape') || + (input.paperSize !== undefined && !boundedInt(input.paperSize, 1, 256)) || + (input.scale !== undefined && !boundedInt(input.scale, 10, 400)) || + (input.fitToWidth !== undefined && !boundedInt(input.fitToWidth, 0, 32_767)) || + (input.fitToHeight !== undefined && !boundedInt(input.fitToHeight, 0, 32_767)) || + (input.fitToPage !== undefined && typeof input.fitToPage !== 'boolean') || + (input.printGridlines !== undefined && typeof input.printGridlines !== 'boolean') || + (input.printHeadings !== undefined && typeof input.printHeadings !== 'boolean') || + (input.oddHeader !== undefined && !isBoundedString(input.oddHeader, 500)) || + (input.oddFooter !== undefined && !isBoundedString(input.oddFooter, 500)) + ) { + throw new Error('Invalid workbook page setup response.') + } + let margins: WorkbookPagePrintSettings['margins'] + if (input.margins !== undefined) { + const record = input.margins + if ( + !isRecord(record) || + !['left', 'right', 'top', 'bottom', 'header', 'footer'].every((edge) => { + const value = record[edge] + return typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= 10 + }) + ) { + throw new Error('Invalid workbook page margins response.') + } + margins = { + left: record.left as number, + right: record.right as number, + top: record.top as number, + bottom: record.bottom as number, + header: record.header as number, + footer: record.footer as number, + } + } + const validated = input as WorkbookPagePrintSettings + return { + ...(validated.orientation === undefined ? {} : { orientation: validated.orientation }), + ...(validated.paperSize === undefined ? {} : { paperSize: validated.paperSize }), + ...(validated.scale === undefined ? {} : { scale: validated.scale }), + ...(validated.fitToWidth === undefined ? {} : { fitToWidth: validated.fitToWidth }), + ...(validated.fitToHeight === undefined ? {} : { fitToHeight: validated.fitToHeight }), + ...(validated.fitToPage === undefined ? {} : { fitToPage: validated.fitToPage }), + ...(margins === undefined ? {} : { margins }), + ...(validated.printGridlines === undefined ? {} : { printGridlines: validated.printGridlines }), + ...(validated.printHeadings === undefined ? {} : { printHeadings: validated.printHeadings }), + ...(validated.oddHeader === undefined ? {} : { oddHeader: validated.oddHeader }), + ...(validated.oddFooter === undefined ? {} : { oddFooter: validated.oddFooter }), + } +} + function parseRichRuns(input: unknown): WorkbookRichRun[] { if (!Array.isArray(input)) throw new Error('Invalid workbook rich text response.') return input.map((run) => { @@ -1344,6 +1452,11 @@ function parseSaveRequest(input: WorkbookSaveRequest): WorkbookSaveRequest { if (input.mode !== 'save' && input.mode !== 'save-as') invalid('mode') if (input.restoreWriteBack !== undefined && typeof input.restoreWriteBack !== 'boolean') invalid('restore flag') + if ( + input.csvContent !== undefined && + (typeof input.csvContent !== 'string' || input.csvContent.length > MAX_CSV_EXPORT_CHARS) + ) + invalid('csv content') if (input.editsTransferId !== undefined && !isUuid(input.editsTransferId)) invalid('edits transfer id') cappedArray('cell edits', input.edits, MAX_SAVE_EDITS) @@ -2114,7 +2227,15 @@ function isDefinedNamesState(input: unknown): boolean { function parseSaveResult(input: unknown): WorkbookSaveResult { if (!isRecord(input)) throw new Error('Invalid workbook save response.') - if (input.canceled === true) return { canceled: true } + if (input.canceled === true) { + if (input.csvSaveAsPath !== undefined) { + if (typeof input.csvSaveAsPath !== 'string' || input.csvSaveAsPath.length === 0) { + throw new Error('Invalid workbook save response.') + } + return { canceled: true, csvSaveAsPath: input.csvSaveAsPath } + } + return { canceled: true } + } if ( input.canceled !== false || !Array.isArray(input.touchedEntries) || @@ -2190,6 +2311,24 @@ function parseSparklineGroups(input: unknown): { }) } +function parseCellImages(input: unknown): { id: string; row: number; column: number }[] { + if (!Array.isArray(input) || input.length > 500) { + throw new Error('Invalid worksheet cell-image metadata.') + } + return input.map((entry) => { + if ( + !isRecord(entry) || + typeof entry.id !== 'string' || + entry.id.length === 0 || + !isNonnegativeInteger(entry.row) || + !isNonnegativeInteger(entry.column) + ) { + throw new Error('Invalid worksheet cell-image metadata.') + } + return { id: entry.id, row: entry.row, column: entry.column } + }) +} + function parsePivotTableInfos(input: unknown): { path: string cachePath: string | null @@ -2198,6 +2337,7 @@ function parsePivotTableInfos(input: unknown): { headerFontColor?: string wholeTableFill?: string stripeFill?: string + styled?: boolean firstDataRow?: number rowGrandTotals?: boolean }[] { @@ -2216,6 +2356,7 @@ function parsePivotTableInfos(input: unknown): { (entry.headerFontColor !== undefined && typeof entry.headerFontColor !== 'string') || (entry.wholeTableFill !== undefined && typeof entry.wholeTableFill !== 'string') || (entry.stripeFill !== undefined && typeof entry.stripeFill !== 'string') || + (entry.styled !== undefined && typeof entry.styled !== 'boolean') || (entry.firstDataRow !== undefined && !isNonnegativeInteger(entry.firstDataRow)) || (entry.rowGrandTotals !== undefined && typeof entry.rowGrandTotals !== 'boolean') ) { @@ -2229,6 +2370,7 @@ function parsePivotTableInfos(input: unknown): { ...(entry.headerFontColor === undefined ? {} : { headerFontColor: entry.headerFontColor }), ...(entry.wholeTableFill === undefined ? {} : { wholeTableFill: entry.wholeTableFill }), ...(entry.stripeFill === undefined ? {} : { stripeFill: entry.stripeFill }), + ...(entry.styled === undefined ? {} : { styled: entry.styled }), ...(entry.firstDataRow === undefined ? {} : { firstDataRow: entry.firstDataRow }), ...(entry.rowGrandTotals === undefined ? {} : { rowGrandTotals: entry.rowGrandTotals }), } @@ -2409,6 +2551,13 @@ function parseVisualObject(input: unknown): WorkbookVisualObject { const customPath = input.customPath === undefined ? undefined : parseCustomPath(input.customPath) if ( !isOptionalString(input.shapeType) || + (input.opacity !== undefined && + (typeof input.opacity !== 'number' || + !Number.isFinite(input.opacity) || + input.opacity < 0 || + input.opacity > 1)) || + !isOptionalString(input.fillMediaPath) || + !isOptionalString(input.fillMediaType) || !isOptionalString(input.fillColor) || !isOptionalString(input.lineColor) || (input.lineWidth !== undefined && @@ -2447,6 +2596,9 @@ function parseVisualObject(input: unknown): WorkbookVisualObject { ...(typeof input.chartPath === 'string' ? { chartPath: input.chartPath } : {}), ...(typeof input.mediaPath === 'string' ? { mediaPath: input.mediaPath } : {}), ...(typeof input.mediaType === 'string' ? { mediaType: input.mediaType } : {}), + ...(input.opacity === undefined ? {} : { opacity: input.opacity }), + ...(input.fillMediaPath === undefined ? {} : { fillMediaPath: input.fillMediaPath }), + ...(input.fillMediaType === undefined ? {} : { fillMediaType: input.fillMediaType }), ...(typeof input.name === 'string' ? { name: input.name } : {}), ...(input.shapeType === undefined ? {} : { shapeType: input.shapeType }), ...(customPath === undefined ? {} : { customPath }), @@ -2571,7 +2723,8 @@ function parseDrawingAnchor(input: Record): WorkbookVisualObjec ] if ( values.some((value) => !isNonnegativeInteger(value)) || - offsets.some((value) => typeof value !== 'number' || !Number.isInteger(value)) + offsets.some((value) => typeof value !== 'number' || !Number.isInteger(value)) || + (input.explicitTo !== undefined && typeof input.explicitTo !== 'boolean') ) { throw new Error('Invalid workbook drawing anchor.') } @@ -2584,6 +2737,7 @@ function parseDrawingAnchor(input: Record): WorkbookVisualObjec fromColumnOffset: input.fromColumnOffset as number, toRowOffset: input.toRowOffset as number, toColumnOffset: input.toColumnOffset as number, + ...(input.explicitTo === undefined ? {} : { explicitTo: input.explicitTo }), } } @@ -2612,8 +2766,10 @@ function parseChart(input: unknown): NonNullable !isOptionalString(input.dataLabelFormat) || !isOptionalEnum(input.grouping, CHART_GROUPINGS) || (input.gridlines !== undefined && typeof input.gridlines !== 'boolean') || + !isOptionalString(input.categoryAxisFormat) || !isOptionalFiniteNumber(input.gapWidthPct) || - !isOptionalFiniteNumber(input.holeSizePct) + !isOptionalFiniteNumber(input.holeSizePct) || + (input.lineMarkers !== undefined && typeof input.lineMarkers !== 'boolean') ) { throw new Error('Invalid workbook chart response.') } @@ -2636,12 +2792,14 @@ function parseChart(input: unknown): NonNullable !Array.isArray(entry.values) || entry.values.some((value) => typeof value !== 'number' || !Number.isFinite(value)) || !isOptionalString(entry.numberFormat) || + !isOptionalString(entry.categoryFormat) || !isOptionalString(entry.color) || !isOptionalString(entry.trendline) || !isOptionalString(entry.valuesRef) || !isOptionalString(entry.categoriesRef) || !isOptionalFiniteNumber(entry.explosionPct) || !isOptionalString(entry.lineColor) || + !isOptionalFiniteNumber(entry.lineWidth) || (entry.smooth !== undefined && typeof entry.smooth !== 'boolean') || !isOptionalString(entry.marker) ) { @@ -2649,12 +2807,14 @@ function parseChart(input: unknown): NonNullable } const pointColors = parseChartPointColors(entry.pointColors) const pointExplosions = parseChartPointExplosions(entry.pointExplosions) + const categoryGroups = parseChartCategoryGroups(entry.categoryGroups) return { name: entry.name, ...(entry.nameRef === undefined ? {} : { nameRef: entry.nameRef }), categories: entry.categories, values: entry.values, ...(entry.numberFormat === undefined ? {} : { numberFormat: entry.numberFormat }), + ...(entry.categoryFormat === undefined ? {} : { categoryFormat: entry.categoryFormat }), ...(entry.color === undefined ? {} : { color: entry.color }), ...(entry.trendline === undefined ? {} : { trendline: entry.trendline }), ...(entry.valuesRef === undefined ? {} : { valuesRef: entry.valuesRef }), @@ -2663,8 +2823,10 @@ function parseChart(input: unknown): NonNullable ...(entry.explosionPct === undefined ? {} : { explosionPct: entry.explosionPct }), ...(pointExplosions === undefined ? {} : { pointExplosions }), ...(entry.lineColor === undefined ? {} : { lineColor: entry.lineColor }), + ...(entry.lineWidth === undefined ? {} : { lineWidth: entry.lineWidth }), ...(entry.smooth === undefined ? {} : { smooth: entry.smooth }), ...(entry.marker === undefined ? {} : { marker: entry.marker }), + ...(categoryGroups === undefined ? {} : { categoryGroups }), } }) return { @@ -2681,6 +2843,9 @@ function parseChart(input: unknown): NonNullable ...(input.dataLabelFormat === undefined ? {} : { dataLabelFormat: input.dataLabelFormat }), ...(input.grouping === undefined ? {} : { grouping: input.grouping }), ...(input.gridlines === undefined ? {} : { gridlines: input.gridlines }), + ...(input.categoryAxisFormat === undefined + ? {} + : { categoryAxisFormat: input.categoryAxisFormat }), ...(valueAxis === undefined ? {} : { valueAxis }), ...(input.gapWidthPct === undefined ? {} : { gapWidthPct: input.gapWidthPct }), ...(input.holeSizePct === undefined ? {} : { holeSizePct: input.holeSizePct }), @@ -2689,6 +2854,7 @@ function parseChart(input: unknown): NonNullable ...(secondaryYAxis === undefined ? {} : { secondaryYAxis }), ...(titleStyle === undefined ? {} : { titleStyle }), ...(input.scatterStyle === undefined ? {} : { scatterStyle: input.scatterStyle }), + ...(input.lineMarkers === undefined ? {} : { lineMarkers: input.lineMarkers }), } } @@ -2720,6 +2886,7 @@ function parseChartAxisInfo(input: unknown): numFmt?: string majorGridlines: boolean hidden: boolean + reversed: boolean } | undefined { if (input === undefined) return undefined @@ -2731,7 +2898,8 @@ function parseChartAxisInfo(input: unknown): !isOptionalFiniteNumber(input.majorUnit) || !isOptionalString(input.numFmt) || typeof input.majorGridlines !== 'boolean' || - (input.hidden !== undefined && typeof input.hidden !== 'boolean') + (input.hidden !== undefined && typeof input.hidden !== 'boolean') || + (input.reversed !== undefined && typeof input.reversed !== 'boolean') ) { throw new Error('Invalid workbook chart axis.') } @@ -2743,6 +2911,7 @@ function parseChartAxisInfo(input: unknown): ...(input.numFmt === undefined ? {} : { numFmt: input.numFmt }), majorGridlines: input.majorGridlines, hidden: input.hidden === true, + reversed: input.reversed === true, } } @@ -2791,6 +2960,24 @@ function parseChartPointColors( }) } +function parseChartCategoryGroups( + input: unknown, +): Array<{ label: string; start: number; end: number }> | undefined { + if (input === undefined) return undefined + if (!Array.isArray(input)) throw new Error('Invalid workbook chart category groups.') + return input.map((entry) => { + if ( + !isRecord(entry) || + typeof entry.label !== 'string' || + !isNonnegativeInteger(entry.start) || + !isNonnegativeInteger(entry.end) + ) { + throw new Error('Invalid workbook chart category groups.') + } + return { label: entry.label, start: entry.start, end: entry.end } + }) +} + function parseChartPointExplosions( input: unknown, ): Array<{ index: number; pct: number }> | undefined { @@ -2842,6 +3029,10 @@ function isOptionalString(input: unknown): input is string | undefined { return input === undefined || typeof input === 'string' } +function isBoundedString(input: unknown, maxLength: number): input is string { + return typeof input === 'string' && input.length >= 1 && input.length <= maxLength +} + function isOptionalNullableString(input: unknown): input is string | null | undefined { return input === undefined || input === null || typeof input === 'string' } diff --git a/apps/sheets/src/renderer/App.tsx b/apps/sheets/src/renderer/App.tsx index c57435e..7972e04 100644 --- a/apps/sheets/src/renderer/App.tsx +++ b/apps/sheets/src/renderer/App.tsx @@ -109,7 +109,7 @@ import '@univerjs/preset-sheets-note/lib/index.css' import { UniverSheetsSortPreset } from '@univerjs/preset-sheets-sort' import UniverPresetSheetsSortEnUS from '@univerjs/preset-sheets-sort/locales/en-US' import '@univerjs/preset-sheets-sort/lib/index.css' -import { UniverSheetsTablePreset } from '@univerjs/preset-sheets-table' +import { UniverSheetsTablePreset, UniverSheetsTableUIPlugin } from '@univerjs/preset-sheets-table' import UniverPresetSheetsTableEnUS from '@univerjs/preset-sheets-table/locales/en-US' import '@univerjs/preset-sheets-table/lib/index.css' import { greenTheme } from '@univerjs/themes' @@ -301,7 +301,7 @@ import { installMultiRowAutofit } from './autofit-multi-row' import { installCopyMaterialize } from './copy-materialize' import { applyUniverLocale } from './univer-locales' import { installRuleDetail } from './univer-rule-detail' -import { installPopulatedDataValidationArrow } from './data-validation-arrow' +import { installActiveCellDataValidationChrome } from './data-validation-dropdown' import { installFormulaNullResultFix } from './formula-null-result' import { installNumberFormatFix } from './numfmt-fix' import { installIfsEmptySetFix } from './ifs-empty-set' @@ -316,6 +316,7 @@ import { handlePageLayoutCommand as handlePageLayoutCommandImpl, type PageLayoutContext, } from './page-layout-actions' +import { handleExportCsv as handleExportCsvImpl, type CsvExportContext } from './csv-export' import { effectivePageBreaks, installPageBreakPreview } from './page-break-preview' import { mapProtectedRanges } from './protected-ranges' import { handleSave as handleSaveImpl, type SaveContext } from './save-actions' @@ -489,9 +490,11 @@ export function App(): React.JSX.Element { const state = lazyWorkbookRef.current if (saving || !state || journalSize(state.editJournal) === 0) return // Never while the in-cell editor is open (saving reloads the workbook - // and would wipe the edit), and never for converted .xls/.csv imports - // whose first save opens a Save As dialog. - if (editingCellRef.current || state.file.needsSaveAs) return + // and would wipe the edit), never for converted .xls imports whose + // first save opens a Save As dialog, and never for CSV sessions — + // AutoSave would silently flatten the user's file. + if (editingCellRef.current || state.file.needsSaveAs || state.file.csvPath !== undefined) + return saving = true void handleSaveRef.current('save', true).finally(() => { saving = false @@ -520,6 +523,7 @@ export function App(): React.JSX.Element { if ( editingCellRef.current || state.file.needsSaveAs || + state.file.csvPath !== undefined || state.file.restoredFromRecovery || state.file.automaticRecoveryDisabled ) @@ -698,6 +702,15 @@ export function App(): React.JSX.Element { } } + function csvExportContext(): CsvExportContext { + return { + univerRef, + lazyWorkbookRef, + setMessage, + requestSaveAs: () => void handleSaveRef.current('save-as'), + } + } + function saveContext(): SaveContext { return { univerRef, @@ -1418,6 +1431,9 @@ export function App(): React.JSX.Element { UniverSheetsFindReplacePreset(), UniverSheetsSortPreset(), UniverSheetsTablePreset(), + // No floating table-name chip: its 32px gap above row-0 tables shifts + // the grid vs Excel and strands the filter overlay at pre-gap coords. + { plugins: [[UniverSheetsTableUIPlugin, { hideAnchor: true }]] }, ], }) loadSnapshotIntoUniver(runtime, initialSnapshot, 'new-workbook', 'Untitled') @@ -1521,7 +1537,9 @@ export function App(): React.JSX.Element { // imports (needsSaveAs) count as never-saved, like Excel. const cellFilenameDisposable = installCellFilenameFunction(runtime, () => { const file = lazyWorkbookRef.current?.file - return file && !file.needsSaveAs ? (file.path ?? null) : null + // A CSV session's on-disk identity is the original .csv, not the + // converted temp copy the session streams from. + return file && !file.needsSaveAs ? (file.csvPath ?? file.path ?? null) : null }) // RATE converges near -100% via bisection instead of erroring. const rateFallbackDisposable = installRateFallback(runtime) @@ -1542,8 +1560,8 @@ export function App(): React.JSX.Element { // Copy/cut load their selection into the lazy window first so streamed // workbooks don't serialize blanks for never-viewed rows. const copyMaterializeDisposable = installCopyMaterialize(runtime, lazyWorkbookRef, setMessage) - // List-validation arrows stay discoverable on values without cluttering empty template rows. - const dataValidationArrowDisposable = installPopulatedDataValidationArrow(runtime) + // Validation dropdowns and input messages follow the active cell, matching Excel. + const dataValidationChromeDisposable = installActiveCellDataValidationChrome(runtime) // Univer's own UI (rule-management panels, dialogs) follows the app // language instead of hard-coded English. void applyUniverLocale(runtime, getLang()) @@ -2499,7 +2517,7 @@ export function App(): React.JSX.Element { multiRowAutofitDisposable.dispose() nullResultDisposable.dispose() copyMaterializeDisposable.dispose() - dataValidationArrowDisposable.dispose() + dataValidationChromeDisposable.dispose() ruleDetailDisposable() lazyFindDisposable.dispose() crossHighlightRef.current?.dispose() @@ -4046,6 +4064,7 @@ export function App(): React.JSX.Element { rowColStyleKeys: new Map(), sheetProtections: new Map(), sheetPageBreaks: new Map(), + sheetFilePageSetups: new Map(), sheetProtectedRanges: new Map(), uninstalledDefinedNames: new Set(), appliedCfSheets: new Set(), @@ -4074,6 +4093,9 @@ export function App(): React.JSX.Element { failures: 0, formulaCells: new Map(), overlay: new Map(), + follow: new Map(), + running: false, + lastRunAt: 0, }, } // Column outline levels arrive with the sheet metadata; seed them now. @@ -4314,6 +4336,8 @@ export function App(): React.JSX.Element { void handleInspectWorkbook() } else if (action === 'export-pdf') { void handleExportPdfImpl(pageLayoutContext()) + } else if (action === 'export-csv') { + void handleExportCsvImpl(csvExportContext()) } else if (action === 'undo' || action === 'redo') { // The shell's own text fields (AI prompt, dialog inputs) keep native // text undo; everywhere else ⌘Z means workbook history. diff --git a/apps/sheets/src/renderer/WorkbookVisuals.tsx b/apps/sheets/src/renderer/WorkbookVisuals.tsx index 5056366..c3dbb56 100644 --- a/apps/sheets/src/renderer/WorkbookVisuals.tsx +++ b/apps/sheets/src/renderer/WorkbookVisuals.tsx @@ -1,4 +1,4 @@ -import { useEffect, useId, useRef, useState, useSyncExternalStore } from 'react' +import { Fragment, useEffect, useId, useRef, useState, useSyncExternalStore } from 'react' import { createPortal } from 'react-dom' import { BooleanNumber, numfmt } from '@univerjs/core' import { isMetafileMime, metafileToDataUrl } from '@genoffice/docx-engine/metafile' @@ -156,14 +156,15 @@ export function installWorkbookVisuals( const rowHeight = (index: number): number => Math.max(worksheet.getRowHeight(index), 1) let marginX = Math.max(0, visual.anchor.fromColumnOffset / EMU_PER_PIXEL) let marginY = Math.max(0, visual.anchor.fromRowOffset / EMU_PER_PIXEL) + const explicitTo = visual.anchor.explicitTo === true let width = markerSpan( { index: fromColumn, offset: marginX }, - markerFrom(toColumn, visual.anchor.toColumnOffset), + clampExplicitTo(markerFrom(toColumn, visual.anchor.toColumnOffset), explicitTo, columnWidth), columnWidth, ) let height = markerSpan( { index: fromRow, offset: marginY }, - markerFrom(toRow, visual.anchor.toRowOffset), + clampExplicitTo(markerFrom(toRow, visual.anchor.toRowOffset), explicitTo, rowHeight), rowHeight, ) // A rotated shape's anchor holds its rotated bounds while xfrm ext keeps @@ -328,6 +329,63 @@ export function installSparklines( return disposables } +export interface CellImageState { + readonly id: string + readonly row: number + readonly column: number +} + +/// In-cell rich-value pictures ("place picture in cell"): one pass-through +/// float DOM per host cell, bytes fetched through the same media IPC as +/// floating images. +export function installCellImages( + runtime: UniverRuntime, + sessionId: string, + images: readonly CellImageState[], + sheetId: string, +): Disposable[] { + const workbook = runtime.univerAPI.getActiveWorkbook() + const worksheet = workbook?.getSheetBySheetId(sheetId) + if (!workbook || !worksheet) return [] + const disposables: Disposable[] = [] + for (const image of images) { + if (image.row >= worksheet.getMaxRows() || image.column >= worksheet.getMaxColumns()) continue + const componentKey = `cell-image-${sessionId}-${sheetId}-${image.id}` + disposables.push( + runtime.univerAPI.registerComponent(componentKey, () => ( + + )), + ) + const range = worksheet.getRange(image.row, image.column, 1, 1) + const floating = worksheet.addFloatDomToRange( + range, + { componentKey, allowTransform: false, eventPassThrough: true }, + {}, + componentKey, + ) + if (floating) disposables.push(floating) + } + return disposables +} + +/// Excel fits the picture inside the cell box, preserving its aspect ratio; +/// while the bytes load the cell just stays blank. +function CellImage({ + sessionId, + imageId, +}: { + readonly sessionId: string + readonly imageId: string +}): React.JSX.Element | null { + const { url, unavailable } = useWorkbookMediaUrl(sessionId, imageId) + if (unavailable || !url) return null + return ( +
    + +
    + ) +} + function Sparkline({ values, type, @@ -419,7 +477,7 @@ function WorkbookVisual({ if (visual.kind === 'image') { return } - return + return } /// A drag commit tears down and re-registers every float DOM, dropping the @@ -634,6 +692,20 @@ export function walkMarker( return { index, offset } } +/// A real `` marker never reaches past its own cell: Excel clamps +/// the offset at the cell edge (broken writers store the picture's full +/// size there, e.g. ClosedXML). Synthesized to markers (oneCellAnchor ext, +/// absoluteAnchor, group children) encode sizes as offsets past the edge +/// and must keep walking, so only explicit-to anchors clamp. +export function clampExplicitTo( + to: AnchorMarker, + explicitTo: boolean, + sizeOf: (index: number) => number, +): AnchorMarker { + if (!explicitTo || to.offset <= sizeOf(to.index)) return to + return { index: to.index, offset: sizeOf(to.index) } +} + /// Pixel distance between two markers (negative when `to` sits before `from`). function markerSpan( from: AnchorMarker, @@ -735,8 +807,17 @@ function EditableShapeVisual({ const maxRow = XLSX_MAX_ROW let fromX = markerFrom(anchor.fromColumn, anchor.fromColumnOffset) let fromY = markerFrom(anchor.fromRow, anchor.fromRowOffset) - let toX = markerFrom(anchor.toColumn, anchor.toColumnOffset) - let toY = markerFrom(anchor.toRow, anchor.toRowOffset) + // Drag from the clamped geometry the user sees, not the raw overflowing + // offsets — otherwise walkMarker re-expands an explicit-to anchor to its + // walked size on commit. The rebuilt anchor is fully normalized, so it + // rightly drops the explicitTo flag. + const explicitTo = anchor.explicitTo === true + let toX = clampExplicitTo( + markerFrom(anchor.toColumn, anchor.toColumnOffset), + explicitTo, + columnWidth, + ) + let toY = clampExplicitTo(markerFrom(anchor.toRow, anchor.toRowOffset), explicitTo, rowHeight) // RTL sheets render mirrored geometry from logical anchors, so a screen // drag maps to the opposite logical x-shift and the opposite logical edge. const rtl = config.rightToLeft === BooleanNumber.TRUE @@ -995,7 +1076,11 @@ function EditableShapeVisual({ ) : visual.kind === 'image' ? ( ) : ( - + )} {!textEditing && (
    `) - } - for (let column = area.startColumn; column <= area.endColumn; column += 1) { - const key = `${row}:${column}` - if (merges.covered.has(key)) continue - const anchor = merges.anchors.get(key) - const span = anchor - ? ` rowspan="${Math.min(anchor.rows, area.endRow - row + 1)}"` + - ` colspan="${Math.min(anchor.columns, area.endColumn - column + 1)}"` - : '' - const inArea = row >= area.startRow && row <= area.endRow - const text = inArea - ? (display[row - area.startRow]?.[column - area.startColumn] ?? '') - : cellDisplay(worksheet, row, column) - const rawValue = inArea ? raw[row - area.startRow]?.[column - area.startColumn] : undefined - const style = worksheet.getRange(row, column).getCellStyleData() - cells.push( - `${escapeHtml(text)}`, - ) - } - const heightPt = Math.max(worksheet.getRowHeight(row) * 0.75, 10) - return `${cells.join('')}` + let totalCells = 0 + for (const area of areas) { + const rows = area.endRow - area.startRow + 1 + const columns = area.endColumn - area.startColumn + 1 + if (rows < 1 || columns < 1) throw new PrintError(t('appPrintNothing')) + totalCells += rows * columns } + if (totalCells > MAX_PRINT_CELLS) throw new PrintError(t('appPrintTooLarge')) - // Session header/footer ride the table's thead/tfoot, which Chromium - // repeats at the top/bottom of every printed page. - const totalColumns = columns + (headings ? 1 : 0) - const resolveCodes = (text: string): string => - resolveHeaderFooterText(text, fileName.replace(/\.pdf$/, ''), sheetName, new Date()) - const headerRow = headerFooterRow(pageSetup.header, 'th', totalColumns, resolveCodes) - const footerRow = headerFooterRow(pageSetup.footer, 'td', totalColumns, resolveCodes) - - const headParts: string[] = [] - if (headerRow !== '') headParts.push(headerRow) - if (headings) { - const letters = Array.from( + let maxContentWidthPt = 0 + const tables: string[] = [] + for (const area of areas) { + const rows = area.endRow - area.startRow + 1 + const columns = area.endColumn - area.startColumn + 1 + const grid = worksheet.getRange(area.startRow, area.startColumn, rows, columns) + const display = grid.getDisplayValues() + const raw = grid.getValues() + const merges = mergeMaps(worksheet, area) + const columnWidthsPt = Array.from( { length: columns }, - (_, offset) => ``, + (_, offset) => worksheet.getColumnWidth(area.startColumn + offset) * 0.75, + ) + maxContentWidthPt = Math.max( + maxContentWidthPt, + rowHeaderPt + columnWidthsPt.reduce((total, width) => total + width, 0), + ) + + const bodyRow = (row: number): string => { + const cells: string[] = [] + if (headings) { + cells.push(``) + } + for (let column = area.startColumn; column <= area.endColumn; column += 1) { + const key = `${row}:${column}` + if (merges.covered.has(key)) continue + const anchor = merges.anchors.get(key) + const span = anchor + ? ` rowspan="${Math.min(anchor.rows, area.endRow - row + 1)}"` + + ` colspan="${Math.min(anchor.columns, area.endColumn - column + 1)}"` + : '' + const inArea = row >= area.startRow && row <= area.endRow + const text = inArea + ? (display[row - area.startRow]?.[column - area.startColumn] ?? '') + : cellDisplay(worksheet, row, column) + const rawValue = inArea ? raw[row - area.startRow]?.[column - area.startColumn] : undefined + const style = worksheet.getRange(row, column).getCellStyleData() + cells.push( + `${escapeHtml(text)}`, + ) + } + const heightPt = Math.max(worksheet.getRowHeight(row) * 0.75, 10) + return `${cells.join('')}` + } + + const headParts: string[] = [] + if (headings) { + const letters = Array.from( + { length: columns }, + (_, offset) => ``, + ) + headParts.push(`${letters.join('')}`) + } + if (titles) { + for (let row = titles.start; row <= titles.end; row += 1) headParts.push(bodyRow(row)) + } + + const bodyParts: string[] = [] + for (let row = area.startRow; row <= area.endRow; row += 1) { + // Title rows already repeat via the table header. + if (titles && row >= titles.start && row <= titles.end) continue + bodyParts.push(bodyRow(row)) + } + + const colgroup = `${headings ? `` : ''}${columnWidthsPt + .map((width) => ``) + .join('')}` + tables.push( + `
    ${row + 1}
    ${columnLabel(area.startColumn + offset)}${row + 1}
    ${columnLabel(area.startColumn + offset)}
    ${colgroup}${headParts.join('')}${bodyParts.join('')}
    `, ) - headParts.push(`${headings ? '' : ''}${letters.join('')}`) - } - if (titles) { - for (let row = titles.start; row <= titles.end; row += 1) headParts.push(bodyRow(row)) } - const bodyParts: string[] = [] - for (let row = area.startRow; row <= area.endRow; row += 1) { - // Title rows already repeat via the table header. - if (titles && row >= titles.start && row <= titles.end) continue - bodyParts.push(bodyRow(row)) - } - - const colgroup = `${headings ? `` : ''}${columnWidthsPt - .map((width) => ``) - .join('')}` const html = `${colgroup}${headParts.join('')}` + - `${bodyParts.join('')}` + - `${footerRow === '' ? '' : `${footerRow}`}
    ` +` + + tables.join('') + + `` - const margins = MARGIN_PRESETS[pageSetup.margins ?? 'normal'] - const pageSize = PAPER_SIZES[pageSetup.paperSize ?? 9] ?? 'A4' - const landscape = pageSetup.orientation === 'landscape' + const margins = setup.margins + const pageSize = PAPER_SIZES[setup.paperSize] ?? 'A4' + const landscape = setup.orientation === 'landscape' + const now = new Date() + const baseName = fileName.replace(/\.pdf$/, '') + const headerTemplate = setup.header + ? buildHeaderFooterTemplate(setup.header, 'header', margins, baseName, sheetName, now) + : undefined + const footerTemplate = setup.footer + ? buildHeaderFooterTemplate(setup.footer, 'footer', margins, baseName, sheetName, now) + : undefined return { fileName, html, landscape, pageSize, margins: { top: margins.top, bottom: margins.bottom, left: margins.left, right: margins.right }, - scale: computeScale( - pageSetup, - pageSize, - landscape, - margins, - rowHeaderPt + columnWidthsPt.reduce((total, width) => total + width, 0), - ), + scale: computeScale(setup, pageSize, landscape, margins, maxContentWidthPt), + ...(headerTemplate === undefined ? {} : { headerTemplate }), + ...(footerTemplate === undefined ? {} : { footerTemplate }), } } -/// Resolves the field codes a static layout can know (&D date, &T time, -/// &F file name, &A sheet name, && literal &); page-dependent codes (&P, -/// &N) render empty — the HTML-to-PDF path has no page counter. -function resolveHeaderFooterText( +/// One left/center/right header or footer as a Chromium print template +/// (rendered in the page's margin box; undefined when the parts are empty). +export function buildHeaderFooterTemplate( + parts: HeaderFooterParts, + kind: 'header' | 'footer', + margins: PrintMargins, + fileName: string, + sheetName: string, + now: Date, +): string | undefined { + const sections = [parts.left ?? '', parts.center ?? '', parts.right ?? ''] + if (sections.every((text) => text === '')) return undefined + const rendered = sections.map((text) => renderHeaderFooterHtml(text, fileName, sheetName, now)) + // Excel offsets the header/footer from the paper edge by its own margin. + const offset = + kind === 'header' + ? `padding-top:${round(margins.header)}in` + : `padding-bottom:${round(margins.footer)}in` + const spanStyle = 'flex:1;white-space:pre-wrap' + // Chromium's template document is content-box; without an inline + // border-box the width:100% + side padding overflows the page and + // shifts/clips the sections. + return ( + `
    ` + + `${rendered[0]}` + + `${rendered[1]}` + + `${rendered[2]}
    ` + ) +} + +/// Field codes → template HTML: &P/&N become Chromium's live pageNumber/ +/// totalPages spans, static codes (&D &T &F &A, && literal) resolve now, +/// everything else is HTML-escaped verbatim. +export function renderHeaderFooterHtml( text: string, fileName: string, sheetName: string, now: Date, ): string { - return text.replace(/&(&|[A-Za-z])/g, (match, code: string) => { + let html = '' + let literal = '' + const flush = (): void => { + html += escapeHtml(literal) + literal = '' + } + for (let index = 0; index < text.length; index += 1) { + const character = text[index] ?? '' + if (character !== '&') { + literal += character + continue + } + const code = text[index + 1] + if (code === undefined) { + literal += '&' + break + } + index += 1 switch (code) { case '&': - return '&' + literal += '&' + break case 'P': + flush() + html += '' + break case 'N': - return '' + flush() + html += '' + break case 'D': - return now.toLocaleDateString() + literal += now.toLocaleDateString() + break case 'T': - return now.toLocaleTimeString() + literal += now.toLocaleTimeString() + break case 'F': - return fileName + literal += fileName + break case 'A': - return sheetName + literal += sheetName + break default: - return match + literal += `&${code}` } - }) -} - -/// One left/center/right header or footer row spanning the whole table -/// ('' when the parts are empty). -function headerFooterRow( - parts: HeaderFooterParts | null | undefined, - cell: 'th' | 'td', - colspan: number, - resolveCodes: (text: string) => string, -): string { - const sections = [parts?.left, parts?.center, parts?.right].map((text) => - resolveCodes(text ?? ''), - ) - if (sections.every((text) => text === '')) return '' - const spans = sections.map((text) => `${escapeHtml(text)}`).join('') - return `<${cell} class="hf" colspan="${colspan}">
    ${spans}
    ` + } + flush() + return html } /// Excel's fit-to-width only shrinks; an explicit scale applies as-is. function computeScale( - pageSetup: PageSetupJournalState, + setup: EffectivePageSetup, pageSize: WorkbookExportPdfRequest['pageSize'], landscape: boolean, margins: { left: number; right: number; top: number; bottom: number }, contentWidthPt: number, ): number { - const fitPages = pageSetup.fitToPage === true ? (pageSetup.fitToWidth ?? 0) : 0 - if (fitPages > 0) { + const fitPages = setup.fitToPage ? setup.fitToWidth : 0 + if (fitPages > 0 && contentWidthPt > 0) { const paperWidthIn = typeof pageSize === 'string' ? landscape @@ -290,8 +335,8 @@ function computeScale( const printableWidthPt = (paperWidthIn - margins.left - margins.right) * 72 return clamp((printableWidthPt * fitPages) / contentWidthPt, 0.1, 1) } - if (pageSetup.fitToPage !== true && pageSetup.scale !== undefined) { - return clamp(pageSetup.scale / 100, 0.1, 2) + if (!setup.fitToPage) { + return clamp(setup.scale / 100, 0.1, 2) } return 1 } diff --git a/apps/sheets/src/renderer/print-settings.ts b/apps/sheets/src/renderer/print-settings.ts new file mode 100644 index 0000000..5c2e64e --- /dev/null +++ b/apps/sheets/src/renderer/print-settings.ts @@ -0,0 +1,294 @@ +/** + * Effective print settings for the PDF export: the session's Page Layout + * journal overlaid on the workbook's saved page setup (worksheet pageSetup / + * pageMargins / printOptions / headerFooter plus the workbook-level + * _xlnm.Print_Area / _xlnm.Print_Titles defined names). + */ +import { columnLabel, parseRange } from '../domain/cell-address' +import type { WorkbookPagePrintSettings } from '../shared/desktop-api' +import type { HeaderFooterParts, PageSetupJournalState, StructuralJournalOp } from './edit-journal' +import { fileRangeToScreenRange, fileToScreen } from './view-transform' + +export interface PrintMargins { + readonly left: number + readonly right: number + readonly top: number + readonly bottom: number + readonly header: number + readonly footer: number +} + +/// Concrete values the print layout consumes; every field resolved. +export interface EffectivePageSetup { + readonly orientation: 'portrait' | 'landscape' + /// OOXML paper-size code. + readonly paperSize: number + /// Percent; applies only when fitToPage is off. + readonly scale: number + /// Pages across; 0 = automatic. Applies only when fitToPage is on. + readonly fitToWidth: number + readonly fitToPage: boolean + /// Inches. + readonly margins: PrintMargins + readonly printGridlines: boolean + readonly printHeadings: boolean + /// Plain A1 ranges to print ([] = the used range). + readonly printAreas: readonly string[] + /// Rows repeated at the top of every page ("1:2"), or null. + readonly printTitles: string | null + readonly header: HeaderFooterParts | null + readonly footer: HeaderFooterParts | null +} + +/// Inches, mirroring the gateway's margin presets. +const MARGIN_PRESETS: Record<'normal' | 'wide' | 'narrow', PrintMargins> = { + normal: { left: 0.7, right: 0.7, top: 0.75, bottom: 0.75, header: 0.3, footer: 0.3 }, + wide: { left: 1, right: 1, top: 1, bottom: 1, header: 0.5, footer: 0.5 }, + narrow: { left: 0.25, right: 0.25, top: 0.75, bottom: 0.75, header: 0.3, footer: 0.3 }, +} + +/// The export wire caps margins at 3in per side. +const MAX_MARGIN_INCHES = 3 + +export interface FilePrintNames { + readonly printArea?: string | undefined + readonly printTitles?: string | undefined +} + +/// File-space A1 areas → this session's screen space (envelope semantics, +/// like streamed merges). Fully deleted areas drop out; when nothing +/// survives the caller falls back to the used range. +function mapAreasToScreen(areas: readonly string[], ops: readonly StructuralJournalOp[]): string[] { + if (ops.length === 0) return [...areas] + const mapped: string[] = [] + for (const area of areas) { + let bounds + try { + bounds = parseRange(area) + } catch { + continue + } + const screen = fileRangeToScreenRange(ops, bounds) + if (screen === null) continue + mapped.push( + `${columnLabel(screen.startColumn)}${screen.startRow + 1}` + + `:${columnLabel(screen.endColumn)}${screen.endRow + 1}`, + ) + } + return mapped +} + +/// File-space title rows → screen space (row axis only — column edits must +/// not drop them). Deleted rows shrink the span; all deleted → null. +function mapTitleRowsToScreen( + titles: string | null, + ops: readonly StructuralJournalOp[], +): string | null { + if (titles === null || ops.length === 0) return titles + const match = /^([0-9]{1,7}):([0-9]{1,7})$/.exec(titles) + if (!match) return null + const rows: number[] = [] + for (let row = Number(match[1]) - 1; row <= Number(match[2]) - 1; row += 1) { + const screen = fileToScreen(ops, 'row', row) + if (screen !== null) rows.push(screen) + } + if (rows.length === 0) return null + const start = Math.min(...rows) + const end = Math.max(...rows) + // Inserts/moves between title rows can stretch the surviving span past + // the layout's 21-row cap (parseTitleRows would throw): drop the titles + // instead, matching the cap semantics at parse time. + if (end - start > 20) return null + return `${start + 1}:${end + 1}` +} + +export function resolveEffectivePageSetup( + journal: PageSetupJournalState, + file: WorkbookPagePrintSettings | null, + names: FilePrintNames | null, + /// This session's structural edits: file-sourced print names live in + /// original workbook coordinates and must shift into screen space + /// (journal-sourced values are already screen space). + ops: readonly StructuralJournalOp[] = [], +): EffectivePageSetup { + const clampMargin = (value: number): number => Math.min(Math.max(value, 0), MAX_MARGIN_INCHES) + const fileMargins = file?.margins + const margins = + journal.margins !== undefined + ? MARGIN_PRESETS[journal.margins] + : fileMargins !== undefined + ? { + left: clampMargin(fileMargins.left), + right: clampMargin(fileMargins.right), + top: clampMargin(fileMargins.top), + bottom: clampMargin(fileMargins.bottom), + header: clampMargin(fileMargins.header), + footer: clampMargin(fileMargins.footer), + } + : MARGIN_PRESETS.normal + const fitToPage = journal.fitToPage ?? file?.fitToPage ?? false + // OOXML default when fitToPage is on and fitToWidth absent is 1 page. + const fitToWidth = journal.fitToWidth ?? file?.fitToWidth ?? (file?.fitToPage === true ? 1 : 0) + const printArea = + journal.printArea !== undefined + ? journal.printArea === null + ? [] + : [journal.printArea] + : mapAreasToScreen(printAreasFromFormula(names?.printArea), ops) + const printTitles = + journal.printTitles !== undefined + ? journal.printTitles + : mapTitleRowsToScreen(printTitleRowsFromFormula(names?.printTitles), ops) + const header = + journal.header !== undefined + ? journal.header + : file?.oddHeader !== undefined + ? decodeHeaderFooter(file.oddHeader) + : null + const footer = + journal.footer !== undefined + ? journal.footer + : file?.oddFooter !== undefined + ? decodeHeaderFooter(file.oddFooter) + : null + return { + orientation: journal.orientation ?? file?.orientation ?? 'portrait', + paperSize: journal.paperSize ?? file?.paperSize ?? 9, + scale: journal.scale ?? file?.scale ?? 100, + fitToWidth, + fitToPage, + margins, + printGridlines: journal.printGridlines ?? file?.printGridlines ?? false, + printHeadings: journal.printHeadings ?? file?.printHeadings ?? false, + printAreas: printArea, + printTitles, + header, + footer, + } +} + +/// Splits a defined-name formula on commas outside quoted sheet names. +function splitAreas(formula: string): string[] { + const parts: string[] = [] + let current = '' + let quoted = false + for (const character of formula) { + if (character === "'") quoted = !quoted + if (character === ',' && !quoted) { + parts.push(current) + current = '' + } else { + current += character + } + } + parts.push(current) + return parts +} + +/// Strips the (optional) sheet qualifier and `$` anchors of one reference. +function plainReference(part: string): string { + return part + .slice(part.lastIndexOf('!') + 1) + .replace(/\$/g, '') + .trim() +} + +/// `'S 1'!$A$1:$K$84,'S 1'!$M$1:$N$9` → ['A1:K84', 'M1:N9']. Anything the +/// print layout cannot crop to (full-column spans, 3-D refs, #REF!) yields +/// [] so the export falls back to the used range instead of dropping content. +export function printAreasFromFormula(formula: string | undefined): string[] { + if (formula === undefined || formula === '') return [] + const areas: string[] = [] + for (const part of splitAreas(formula)) { + const reference = plainReference(part).toUpperCase() + if (/^[A-Z]{1,3}[0-9]{1,7}$/.test(reference)) { + areas.push(`${reference}:${reference}`) + continue + } + if (/^[A-Z]{1,3}[0-9]{1,7}:[A-Z]{1,3}[0-9]{1,7}$/.test(reference)) { + areas.push(reference) + continue + } + return [] + } + return areas +} + +/// `'S'!$1:$2` → '1:2'; column-repeat parts are skipped (unsupported), spans +/// beyond the layout's 21-row title cap are dropped. +export function printTitleRowsFromFormula(formula: string | undefined): string | null { + if (formula === undefined || formula === '') return null + for (const part of splitAreas(formula)) { + const match = /^([0-9]{1,7}):([0-9]{1,7})$/.exec(plainReference(part)) + if (!match) continue + const start = Number(match[1]) + const end = Number(match[2]) + if (start <= end && end - start <= 20) return `${start}:${end}` + } + return null +} + +/// Excel's encoded header/footer → left/center/right parts. Field codes the +/// layout resolves (&P &N &D &T &F &A, && literal) stay verbatim; formatting +/// codes (font/size/color/bold/…) and unsupported codes (&G picture, &Z +/// path) are stripped. Text before the first section marker is centered. +export function decodeHeaderFooter(encoded: string): HeaderFooterParts | null { + const sections = { L: '', C: '', R: '' } + let current: 'L' | 'C' | 'R' = 'C' + let index = 0 + while (index < encoded.length) { + const character = encoded[index] ?? '' + if (character !== '&') { + sections[current] += character + index += 1 + continue + } + const next = encoded[index + 1] + if (next === undefined) break + if (next === '&') { + sections[current] += '&&' + index += 2 + continue + } + // &"Font Name,Style" — skip the whole quoted spec. + if (next === '"') { + const close = encoded.indexOf('"', index + 2) + index = close === -1 ? encoded.length : close + 1 + continue + } + // &nn font size. + if (/[0-9]/.test(next)) { + index += 2 + while (index < encoded.length && /[0-9]/.test(encoded[index] ?? '')) index += 1 + continue + } + const code = next.toUpperCase() + // &K followed by an RRGGBB or theme color spec. + if (code === 'K') { + index += 8 + continue + } + if (code === 'L' || code === 'C' || code === 'R') { + current = code + index += 2 + continue + } + if ( + code === 'P' || + code === 'N' || + code === 'D' || + code === 'T' || + code === 'F' || + code === 'A' + ) { + sections[current] += `&${code}` + } + index += 2 + } + if (sections.L === '' && sections.C === '' && sections.R === '') return null + return { + ...(sections.L === '' ? {} : { left: sections.L }), + ...(sections.C === '' ? {} : { center: sections.C }), + ...(sections.R === '' ? {} : { right: sections.R }), + } +} diff --git a/apps/sheets/src/renderer/save-actions.ts b/apps/sheets/src/renderer/save-actions.ts index 1aac73b..6908847 100644 --- a/apps/sheets/src/renderer/save-actions.ts +++ b/apps/sheets/src/renderer/save-actions.ts @@ -21,6 +21,7 @@ import { toSaveVisualAdds, toSaveVisualEdits, } from './edit-journal' +import { activeCsvSheet, handleExportCsv, serializeActiveSheetCsv } from './csv-export' import { t } from './i18n/locale' import { abortStagedEditsTransfer, stageEditsForSave, type StagedEdits } from './save-edits-staging' import { showToast } from './toast-bus' @@ -57,6 +58,10 @@ export interface SaveContext { ) => void } +/// CSV files whose "keep this format?" question was already answered with +/// "Continue as CSV" — asked once per file, like modern Excel's banner. +const confirmedCsvSaves = new Set() + /** * mode 'recovery': assemble the very same payload but hand it to the * crash-recovery writer instead of the save pipeline — no dialogs, no status @@ -228,6 +233,40 @@ export async function handleSave( if (mode !== 'recovery') ctx.setMessage(t('appNoEditsToSave')) return } + // CSV session: Save keeps the CSV identity — Excel's "keep this format?" + // question once per file, then the active sheet rides the save request as + // serialized CSV text for the main process to write back. + let csvContent: string | undefined + if (mode === 'save' && state.file.csvPath !== undefined) { + const csvPath = state.file.csvPath + if (!state.flags.preloadComplete) { + ctx.setMessage(t('appCsvExportNeedsFullLoad')) + return + } + if (!confirmedCsvSaves.has(csvPath)) { + const choice = await window.desktopApi.confirmCsvSave() + if (choice === 'cancel') { + ctx.setMessage(t('appSaveCanceled')) + return + } + if (choice === 'xlsx') { + await handleSave(ctx, 'save-as', quiet) + return + } + confirmedCsvSaves.add(csvPath) + } + const active = activeCsvSheet(ctx.univerRef.current) + const serialized = active === null ? null : serializeActiveSheetCsv(active.sheet, state) + if (serialized === 'too-large') { + ctx.setMessage(t('appCsvExportTooLarge')) + return + } + if (serialized === null) { + ctx.setMessage(t('appSaveFailed')) + return + } + csvContent = serialized + } // Sheet ops rebuild workbook.xml's tab list, so the save needs the final // on-screen order. const sheetOrder = @@ -306,6 +345,7 @@ export async function handleSave( sessionId: state.file.sessionId, mode, ...(restoreWriteBack ? { restoreWriteBack: true } : {}), + ...(csvContent === undefined ? {} : { csvContent }), edits: staged.edits, bulkConstantFills, ...(staged.editsTransferId === undefined ? {} : { editsTransferId: staged.editsTransferId }), @@ -335,6 +375,21 @@ export async function handleSave( }) if (ctx.lazyWorkbookRef.current !== state) return if (result.canceled) { + if (result.csvSaveAsPath !== undefined) { + // The user picked CSV in the Save As dialog: no xlsx was written — + // serialize the active sheet to the chosen path instead. The journal + // stays pending; the session keeps its identity (a copy semantics). + await handleExportCsv( + { + univerRef: ctx.univerRef, + lazyWorkbookRef: ctx.lazyWorkbookRef, + setMessage: ctx.setMessage, + requestSaveAs: () => void handleSave(ctx, 'save-as', quiet), + }, + result.csvSaveAsPath, + ) + return + } ctx.setMessage(t('appSaveCanceled')) return } diff --git a/apps/sheets/src/renderer/styles.css b/apps/sheets/src/renderer/styles.css index 3537165..37cbfae 100644 --- a/apps/sheets/src/renderer/styles.css +++ b/apps/sheets/src/renderer/styles.css @@ -39,6 +39,10 @@ --sheets-chip-bg: #f3f7f4; --sheets-member-bg: #f6f8f7; --sheets-member-ink: #5c6660; + --sheets-validation-prompt-bg: #ffffe1; + --sheets-validation-prompt-border: #808080; + --sheets-validation-prompt-ink: #555; + --sheets-validation-prompt-shadow: rgb(0 0 0 / 18%); /* content-side constants: charts, shapes and their selection chrome sit on * the paper-like grid and never follow the theme */ @@ -124,6 +128,10 @@ --sheets-chip-bg: #24312a; --sheets-member-bg: #2f2f2f; --sheets-member-ink: #a9b2ac; + --sheets-validation-prompt-bg: #3c3826; + --sheets-validation-prompt-border: #8f8663; + --sheets-validation-prompt-ink: #f2ecd0; + --sheets-validation-prompt-shadow: rgb(0 0 0 / 45%); } @media (prefers-color-scheme: dark) { @@ -152,6 +160,10 @@ --sheets-chip-bg: #24312a; --sheets-member-bg: #2f2f2f; --sheets-member-ink: #a9b2ac; + --sheets-validation-prompt-bg: #3c3826; + --sheets-validation-prompt-border: #8f8663; + --sheets-validation-prompt-ink: #f2ecd0; + --sheets-validation-prompt-shadow: rgb(0 0 0 / 45%); } } @@ -2160,6 +2172,9 @@ textarea:focus { .chart-svg { width: 100%; + /* Wide anchors: without a height cap the aspect-derived height overflows + the frame and clips the bottom axis rows (side-legend layouts). */ + max-height: 100%; min-height: 0; flex: 1 1 auto; } @@ -2396,6 +2411,69 @@ textarea:focus { pointer-events: none; } +.data-validation-dropdown-button { + width: 100%; + height: 100%; + box-sizing: border-box; + display: flex; + align-items: center; + justify-content: center; + padding: 0; + border: 1px solid var(--border-strong); + border-radius: 0; + background: var(--surface-subtle); + color: var(--text-muted); + cursor: default; + appearance: none; +} + +.univer-z-10:has(.data-validation-dropdown-button), +.univer-z-10:has(.data-validation-input-message) { + overflow: visible !important; +} + +.univer-z-10:has(.data-validation-input-message) > .univer-overflow-hidden { + overflow: visible !important; +} + +.data-validation-dropdown-button:hover { + border-color: var(--border-hover); + background: var(--hover); + color: var(--text); +} + +.data-validation-dropdown-button svg { + width: 8px; + height: 8px; + display: block; + fill: currentcolor; + pointer-events: none; +} + +.data-validation-input-message { + width: 100%; + height: 100%; + box-sizing: border-box; + padding: 4px; + overflow: hidden; + border: 1px solid var(--sheets-validation-prompt-border); + background: var(--sheets-validation-prompt-bg); + box-shadow: 1px 1px 2px var(--sheets-validation-prompt-shadow); + color: var(--sheets-validation-prompt-ink); + font-family: inherit; + font-size: 9px; + line-height: 12px; + white-space: pre-wrap; + overflow-wrap: anywhere; + pointer-events: none; + user-select: none; + transform: translateY(4px); +} + +.data-validation-input-message-title { + font-weight: 600; +} + .page-break-line { width: 100%; height: 100%; @@ -2806,6 +2884,23 @@ textarea:focus { object-fit: contain; } +/* In-cell rich-value picture: fits the cell box, aspect preserved (Excel). */ +.xlsx-cell-image { + width: 100%; + height: 100%; + display: flex; + align-items: center; + justify-content: center; + overflow: hidden; + pointer-events: none; +} + +.xlsx-cell-image img { + max-width: 100%; + max-height: 100%; + object-fit: contain; +} + .xlsx-shape, .xlsx-visual-loading { display: flex; diff --git a/apps/sheets/src/renderer/univer-state.ts b/apps/sheets/src/renderer/univer-state.ts index 3dbeaf6..fd71c46 100644 --- a/apps/sheets/src/renderer/univer-state.ts +++ b/apps/sheets/src/renderer/univer-state.ts @@ -7,7 +7,11 @@ import { BorderType, LocalUndoRedoService, type IRange } from '@univerjs/core' import { SheetInterceptorService } from '@univerjs/sheets' -import type { WorkbookFile, WorkbookPivotDefinition } from '../shared/desktop-api' +import type { + WorkbookFile, + WorkbookPagePrintSettings, + WorkbookPivotDefinition, +} from '../shared/desktop-api' import type { createUniver } from './create-univer' import type { EditJournal } from './edit-journal' import { netAxisDelta } from './view-transform' @@ -43,6 +47,9 @@ export interface LazyWorkbookState { /// File-side manual page breaks (0-based index of the row/column after the /// break, file coordinates), known once a sheet finishes indexing. readonly sheetPageBreaks: Map + /// File-side saved print settings (pageSetup / margins / headerFooter), + /// known once a sheet finishes indexing. + readonly sheetFilePageSetups: Map /// File-side allow-edit ranges, known once a sheet finishes indexing. readonly sheetProtectedRanges: Map< string, @@ -104,6 +111,16 @@ export interface LazyWorkbookState { failures: number readonly formulaCells: Map> readonly overlay: Map> + /// per-sheet: viewport row the last SUCCESSFUL overlay window was + /// anchored at and whether it covered every formula band; a partial + /// window re-anchors when the user scrolls far from it (alpha ledger + /// r141). Written only after the sidecar run succeeds — early writes + /// latched stale flags on failure (bugbot). + readonly follow: Map + /// re-anchor throttle: no new run while one is in flight, and at most + /// one every few seconds — each run reads thousands of sidecar cells + running: boolean + lastRunAt: number } } diff --git a/apps/sheets/src/renderer/univer-sync.ts b/apps/sheets/src/renderer/univer-sync.ts index 51a1f98..6b43bab 100644 --- a/apps/sheets/src/renderer/univer-sync.ts +++ b/apps/sheets/src/renderer/univer-sync.ts @@ -100,6 +100,7 @@ import { type AdvancedFilterCondition, } from './AdvancedFilterDialog' import { + installCellImages, installSparklines, installWorkbookVisuals, isChartEditorOpen, @@ -388,9 +389,14 @@ export function loadWorkbookSkeleton(runtime: UniverRuntime | null, file: Workbo ...(sheet.defaultRowHeight === null ? {} : { defaultRowHeight: (sheet.defaultRowHeight * 96) / 72 }), - ...(sheet.defaultColumnWidth === null - ? {} - : { defaultColumnWidth: characterWidthToPixels(sheet.defaultColumnWidth) }), + // No defaultColWidth in the file ≠ Univer's 88px default: Excel + // derives its built-in width from baseColWidth (default 8) plus + // cell padding, snapped to 1/256 chars (~74px at MDW 8, live + // Excel shows 8.0 chars). The narrower column matters: General + // numbers switch to scientific when the digits stop fitting. + defaultColumnWidth: characterWidthToPixels( + sheet.defaultColumnWidth ?? paddedBaseColumnWidth(sheet.baseColumnWidth), + ), ...(sheet.freeze === null ? {} : { @@ -424,6 +430,15 @@ export function characterWidthToPixels(width: number): number { return width === 0 ? 0 : Math.floor(((256 * width + Math.floor(128 / mdw)) / 256) * mdw) + 5 } +/// Excel's built-in default column width when sheetFormatPr carries no +/// defaultColWidth: baseColWidth (default 8) plus cell padding, snapped to +/// the format's 1/256-char granularity (ECMA-376 §18.3.1.81). +export function paddedBaseColumnWidth(base: number | null | undefined): number { + const chars = base ?? 8 + const mdw = getWorkbookMdw() + return Math.trunc(((chars * mdw + 5) / mdw) * 256) / 256 +} + const CJK_FAMILY_NAME = /[\u1100-\u11ff\u3000-\u303f\u3040-\u30ff\u3130-\u318f\u3400-\u4dbf\u4e00-\u9fff\uac00-\ud7af\uf900-\ufaff\uff00-\uffef]/ @@ -464,7 +479,12 @@ export function resolveNormalMdwFamily(file: WorkbookFile): string { export function measureNormalFontMdw(file: WorkbookFile): number { const size = file.styles?.[0]?.fontSize ?? 11 const family = resolveNormalMdwFamily(file) - if (family === 'Calibri' && size === 11) return 7 + // Mac Excel (the fidelity reference) lays Calibri 11 out at MDW 8, not the + // Windows GDI 7: live probes and ref print geometry both fit + // floor((w+16/256)*8) within 1pt across corpora (prod_054 50.86ch→305pt, + // prod_027 32.44ch→195pt, lo/built-in_ranges 13.71ch→82pt) while MDW 7 + // misses by 12%+, wrapping borderline text a line early. + if (family === 'Calibri' && size === 11) return 8 const perPt = EXCEL_DIGIT_PER_PT[family] if (perPt !== undefined) return clampMdw(Math.round(perPt * size)) // CJK faces without a table entry: canvas would measure a macOS @@ -1062,7 +1082,13 @@ export async function activateFormulaClosure( if (!state || state.formulaMode || state.closure.status !== 'idle') return state.closure.status = 'pending' const giveUp = (): void => { - if (lazyWorkbookRef.current === state) state.closure.status = 'unavailable' + if (lazyWorkbookRef.current !== state) return + state.closure.status = 'unavailable' + // Cache-mode fallback starts NOW, not at the first edit: the file's + // cached values may be stale or poisoned (saved by an earlier broken + // session) and would otherwise display until the user edits something + // (alpha ledger r141 reopen). + queueFormulaRecalc(runtime, lazyWorkbookRef, setMessage) } const inputs: ClosureSheetInput[] = [] @@ -1674,7 +1700,8 @@ export function queueFormulaRecalc( ): void { const state = lazyWorkbookRef.current if (!state || state.formulaMode || state.closure.status !== 'unavailable') return - if (state.recalc.failures >= RECALC_MAX_FAILURES) return + // minimal states (tests, partial teardown) may carry no recalc slot + if (!state.recalc || state.recalc.failures >= RECALC_MAX_FAILURES) return // The engine loads the file from disk; session structural edits would // desync every coordinate — fail soft to cached values. if ([...state.editJournal.structuralOps.values()].some((ops) => ops.length > 0)) return @@ -1757,12 +1784,24 @@ async function runFormulaRecalc( } } const generation = ++state.recalc.generation + state.recalc.running = true + state.recalc.lastRunAt = Date.now() try { const keys = await recalcFormulaCellKeys(state, sheetId) - if (lazyWorkbookRef.current !== state || !keys || keys.size === 0) return + if (lazyWorkbookRef.current !== state) return + if (!keys || keys.size === 0) { + // nothing to recalculate on this sheet: mark it followed so the + // finished-run chain below doesn't loop on it forever + if (keys) state.recalc.follow.set(sheetId, { anchorRow: 0, complete: true }) + return + } const viewportStartRow = state.loadedRanges.get(sheetId)?.startRow ?? 0 const reads = recalcReadRanges(keys, viewportStartRow, RECALC_READ_BUDGET) - if (reads.length === 0) return + if (reads.length === 0) { + state.recalc.follow.set(sheetId, { anchorRow: viewportStartRow, complete: true }) + return + } + const windowComplete = reads.length === closureFetchRanges(keys).length const result = await window.desktopApi.recalcWorkbook({ sessionId: state.file.sessionId, edits, @@ -1786,6 +1825,7 @@ async function runFormulaRecalc( overlay.set(`${cell.row}:${cell.column}`, { v: cell.number ?? cell.formatted }) } state.recalc.overlay.set(sheetId, overlay) + state.recalc.follow.set(sheetId, { anchorRow: viewportStartRow, complete: windowComplete }) const loaded = state.loadedRanges.get(sheetId) if (loaded && overlay.size > 0) { journalSuppression.active = true @@ -1813,6 +1853,31 @@ async function runFormulaRecalc( // failures toward the kill switch. if (lazyWorkbookRef.current !== state || state.recalc.generation !== generation) return state.recalc.failures += 1 + } finally { + if (state.recalc.generation === generation) { + state.recalc.running = false + // A sheet switched to while this run was in flight got its first-follow + // queue attempt swallowed by the running/cooldown guards, and nothing + // rechecks a fully loaded viewport — chain one run for it. STRICTLY a + // different sheet: chaining on the run's own sheet would turn failures + // and incomplete indexes into an immediate retry loop that burns the + // RECALC_MAX_FAILURES budget before any edit (bugbot). + try { + const activeId = runtime.univerAPI.getActiveWorkbook()?.getActiveSheet()?.getSheetId() + if ( + activeId && + activeId !== sheetId && + lazyWorkbookRef.current === state && + state.closure.status === 'unavailable' && + state.recalc.failures < RECALC_MAX_FAILURES && + !state.recalc.follow.has(activeId) + ) { + queueFormulaRecalc(runtime, lazyWorkbookRef, setMessage) + } + } catch { + /* workbook mid-teardown */ + } + } } } @@ -1885,6 +1950,7 @@ async function loadFrozenColumnStrip( mapped.screen.rows, sheet.columnWidths, sheet.defaultRowHeightFixed, + sheet.defaultRowHeight, stripPatchRange, ), ) @@ -2013,13 +2079,36 @@ async function loadRange( ) state.loadedRanges.set(sheetId, availableRange) patchedRange = availableRange + // Partial recalc windows follow the user: far outside the anchored + // window the grid would show raw (possibly poisoned) file cache — + // re-run the sidecar recalc around the new viewport (r141 reopen). + const recalc = state.recalc + const follow = recalc.follow.get(sheetId) + if ( + !state.formulaMode && + state.closure.status === 'unavailable' && + !recalc.running && + // a pending debounce counts as in flight: re-queuing would RESET + // the timer and starve the open-time recalc while streaming keeps + // patching every ~250ms (bugbot) + recalc.timer === null && + Date.now() - recalc.lastRunAt > 3000 && + (!follow || + (!follow.complete && Math.abs(availableRange.startRow - follow.anchorRow) > 200)) + ) { + queueFormulaRecalc(runtime, lazyWorkbookRef, setMessage) + } } } const result = mapped.raw + const hasStructuralOps = (state.editJournal.structuralOps.get(sheetId)?.length ?? 0) > 0 // formulaMode skips the closure/recalc paths that normally fill // formulaText; cache-only (defined-name) cells still need it for the - // formula bar and Show Formulas view. - if (state.formulaMode) storeFormulaText(state, sheetId, result.cells) + // formula bar and Show Formulas view. Harvest from screen.cells — raw + // holds only the last batch of an over-cap read — except under + // structural ops, where the store must stay in file coordinates. + if (state.formulaMode) + storeFormulaText(state, sheetId, hasStructuralOps ? result.cells : mapped.screen.cells) recordHyperlinks(state, sheetId, mapped.screen.hyperlinks) keepActiveSheet(worksheet, () => { applyRowProperties(worksheet, state, sheetId, mapped.screen.rows) @@ -2035,6 +2124,7 @@ async function loadRange( mapped.screen.rows, sheetMeta.columnWidths, sheetMeta.defaultRowHeightFixed, + sheetMeta.defaultRowHeight, patchedRange, ), ) @@ -2043,7 +2133,6 @@ async function loadRange( // file-space ranges; Univer shifts the installed models itself on later // structural edits, but a fresh install after a shift would be stale — // skip it (rare: the sheet was being edited before it first rendered). - const hasStructuralOps = (state.editJournal.structuralOps.get(sheetId)?.length ?? 0) > 0 if (!hasStructuralOps) { await applyConditionalRules(worksheet, state, sheetId, result.conditionalRules) if (result.indexingComplete) { @@ -2380,8 +2469,9 @@ export function applyRowProperties( } if (row.height !== undefined) { const px = Math.round((row.height * 96) / 72) - // Excel renders the stored ht verbatim on open — it never re-measures - // wrapped content when loading a file, so neither do we. + // Paint the stored ht first; the open-time wrap measure + // (wrapAutoFitRows) re-fits auto-mode wrap rows afterwards, matching + // Excel's own open-time re-measure of cached heights. worksheet.setRowHeightsForced(row.row, 1, px) if (!row.customHeight && px >= defaultRowHeightPx(state, sheetId)) { // Without customHeight the row is still in Excel's auto mode: a @@ -2749,11 +2839,15 @@ const EXCEL_ERROR_LITERALS = new Set([ ]) const keepsCacheMemo = new Map() -function formulaKeepsCache(formula: string): boolean { +export function formulaKeepsCache(formula: string): boolean { let cached = keepsCacheMemo.get(formula) if (cached === undefined) { if (keepsCacheMemo.size > 20_000) keepsCacheMemo.clear() - cached = containsUnresolvedNames(formula) + // Google Sheets exports unevaluable functions as + // IFERROR(__xludf.DUMMYFUNCTION("..."), ); recalculating turns + // the float-repr literal (46235.0) into a string that numfmt skips. + // The cached IS the computed value — keep it, like Excel does. + cached = formula.includes('__xludf.') || containsUnresolvedNames(formula) keepsCacheMemo.set(formula, cached) } return cached @@ -2812,30 +2906,37 @@ export function inheritedWrapLookup( } } -/// Rows Excel DOES auto-fit when opening a file: the row is in auto mode (no -/// stored ht of its own), the sheet default is not user-fixed (sheetFormatPr -/// customHeight), and at least one loaded cell wraps real text. Everything -/// else keeps its stored/default height verbatim (#884); numeric wrap cells -/// and no-wrap manual breaks never change the line count, so neither counts. +/// Rows Excel DOES auto-fit when opening a file: the row is in auto mode +/// (no customHeight — a cached ht alone does not opt out: Excel live-probes +/// re-fit ht="30" rows to 16pt on open, prod_054/prod_027), the sheet +/// default is not user-fixed (sheetFormatPr customHeight), the cached ht is +/// not a sub-default spacer, and at least one loaded cell wraps real text. +/// User-fixed heights keep their stored value verbatim (#884); numeric wrap +/// cells and no-wrap manual breaks never change the line count, so neither +/// counts. export function wrapAutoFitRows( cells: WorkbookRangeResult['cells'], styles: readonly WorkbookCellStyle[], rows: WorkbookRangeResult['rows'], columnWidths: WorkbookFile['sheets'][number]['columnWidths'], defaultRowHeightFixed: boolean | undefined, + defaultRowHeight: number | null | undefined, range: IRange, ): number[] { if (defaultRowHeightFixed) return [] const inheritedWrap = inheritedWrapLookup(styles, rows, columnWidths) - const storedHeights = new Set() + const lockedHeights = new Set() + const defaultPt = defaultRowHeight ?? 15 for (const row of rows) { - if (row.height !== undefined) storedHeights.add(row.row) + if (row.customHeight || (row.height !== undefined && row.height < defaultPt)) { + lockedHeights.add(row.row) + } } const qualifying = new Set() for (const cell of cells) { if (cell.row < range.startRow || cell.row > range.endRow) continue if (cell.column < range.startColumn || cell.column > range.endColumn) continue - if (qualifying.has(cell.row) || storedHeights.has(cell.row)) continue + if (qualifying.has(cell.row) || lockedHeights.has(cell.row)) continue const style = cell.styleIndex === undefined ? undefined : styles[cell.styleIndex] const wraps = style ? style.wrapText === true : inheritedWrap(cell.row, cell.column) if (!wraps) continue @@ -2972,9 +3073,12 @@ function patchWorksheetRangeInner( // rewriting the formula would re-dirty its dependency web (r141). if (pinnedKeys?.has(`${cell.row}:${cell.column}`)) continue const keepsCache = useFormulas && cell.formula ? formulaKeepsCache(cell.formula) : false - // A kept (cache-only) formula with no cached value must show blank — - // falling back to the formula string would print it as literal text. - const displayValue = cell.value ?? (keepsCache ? '' : cell.formula) ?? '' + // A formula cell with no cached value must show blank — falling back to + // the formula string would print it as literal text (r141 reopen: cache + // mode painted "=A750+1" walls). The formula-string fallback survives + // only for engine-installed cells, where displayValue is never painted. + const displayValue = + cell.value ?? (cell.formula ? (useFormulas && !keepsCache ? cell.formula : '') : '') ?? '' const row = matrix[cell.row - range.startRow] const style = cell.styleIndex === undefined ? undefined : styles[cell.styleIndex] // CSE array follower: its dead cached value would block the master's @@ -3301,15 +3405,16 @@ function applyTableBanding( } /// Excel keeps pivot styling out of cell xfs entirely; approximate the -/// Light-family bands (header rows + grand-total row) with the fill resolved -/// sidecar-side from pivotTableStyleInfo. +/// style bands (header rows, grand-total row, alternating row stripes) with +/// the fills resolved sidecar-side from pivotTableStyleInfo. export function applyPivotStyling( matrix: ICellData[][], range: IRange, pivotTables: WorkbookFile['sheets'][number]['pivotTables'], ): void { for (const pivot of pivotTables) { - if (!pivot.headerFill && !pivot.wholeTableFill) continue + // Fills imply a named style; the extra checks keep stale sidecars working. + if (!pivot.styled && !pivot.headerFill && !pivot.wholeTableFill && !pivot.stripeFill) continue let bounds: ReturnType try { bounds = parseRange(pivot.outputRef) @@ -3322,27 +3427,36 @@ export function applyPivotStyling( const rowEnd = Math.min(range.endRow, bounds.endRow) const columnStart = Math.max(range.startColumn, bounds.startColumn) const columnEnd = Math.min(range.endColumn, bounds.endColumn) + // Solid-header families (headerFontColor, no body fill: Medium) keep the + // header treatment off the grand-total row; tinted Light headers and the + // filled Dark1 body extend it (refs: aspose_sample1 vs POI 54436/Dark1). + const totalTakesHeaderFill = Boolean(pivot.wholeTableFill) || !pivot.headerFontColor for (let row = rowStart; row <= rowEnd; row += 1) { - const isBand = row <= headerEndRow || row === totalRow + const isHeader = row <= headerEndRow + const isBand = isHeader || row === totalRow + const headerTreated = isHeader || (row === totalRow && totalTakesHeaderFill) // Excel's pivot stripe covers the first data row, then alternates. const isStripe = !isBand && pivot.stripeFill !== undefined && (row - headerEndRow - 1) % 2 === 0 - const fill = isBand ? pivot.headerFill : isStripe ? pivot.stripeFill : pivot.wholeTableFill - if (!fill) continue + const fill = headerTreated + ? pivot.headerFill + : isStripe + ? pivot.stripeFill + : isBand + ? undefined + : pivot.wholeTableFill + // Fill-less bands (Light 1-7, Medium grand total) still bold their rows. + if (!fill && !isBand) continue for (let column = columnStart; column <= columnEnd; column += 1) { const cell = matrix[row - range.startRow]?.[column - range.startColumn] if (!cell) continue const style = (cell.s ?? {}) as IStyleData - if (styleHasFill(style)) continue + if (fill && styleHasFill(style)) continue cell.s = { ...style, - bg: { rgb: fill }, - ...(isBand - ? { - bl: BooleanNumber.TRUE, - ...(pivot.headerFontColor ? { cl: { rgb: pivot.headerFontColor } } : {}), - } - : {}), + ...(fill ? { bg: { rgb: fill } } : {}), + ...(isBand ? { bl: BooleanNumber.TRUE } : {}), + ...(headerTreated && pivot.headerFontColor ? { cl: { rgb: pivot.headerFontColor } } : {}), } } } @@ -3644,6 +3758,7 @@ export async function preloadEntireWorkbook( screen.rows, sheet.columnWidths, sheet.defaultRowHeightFixed, + sheet.defaultRowHeight, screenRange, ), ) @@ -3710,6 +3825,9 @@ function captureSheetFileState( colBreaks: [...result.colBreaks], }) } + if (!state.sheetFilePageSetups.has(sheetId) && result.pageSetup != null) { + state.sheetFilePageSetups.set(sheetId, result.pageSetup) + } if (!state.sheetProtectedRanges.has(sheetId)) { // File coordinates → this session's screen space; later structural ops // remap the stored set incrementally (see the App structural listener). @@ -4652,7 +4770,7 @@ type CfHighlightBuilder = ReturnType< ReturnType['whenCellNotEmpty'] > -function buildConditionalRule( +export function buildConditionalRule( worksheet: UniverWorksheet, dxfStyles: readonly WorkbookCellStyle[], rule: WorkbookRangeResult['conditionalRules'][number], @@ -4739,17 +4857,54 @@ function buildConditionalRule( const highlight = buildHighlightCondition(builder, rule, anchor, coveredCells) if (!highlight) return null const built = applyDxfFormat(highlight, dxfStyles, rule.dxfIndex).setRanges(ranges).build() - // The facade builder has no number-format setter, but IHighlightCell.style - // is an IStyleBase which carries n — a dxf numFmt goes onto the built rule - // (a numFmt-only dxf builds with no style object at all). - const pattern = rule.dxfIndex === undefined ? undefined : dxfStyles[rule.dxfIndex]?.numberFormat - if (pattern) { - const target = built.rule as { style?: { n?: { pattern: string } } } - target.style = { ...target.style, n: { pattern } } - } + patchBuiltHighlightRule( + built.rule as BuiltHighlightRule, + rule.ruleType, + rule.dxfIndex === undefined ? undefined : dxfStyles[rule.dxfIndex], + ) return built } +export type BuiltHighlightRule = { + operator?: string + value?: string + /// The builder's IStyleBase plus the patched-on keys. + style?: { + n?: { pattern: string } + bd?: Partial>> + [key: string]: unknown + } +} + +/// IHighlightCell supports more than the facade builder exposes: style is an +/// IStyleBase (the paint path merges its n and bd verbatim), and the +/// error-presence text operators have no setter at all. Patch the built rule +/// for a dxf number format, dxf borders, and containsErrors / +/// notContainsErrors (built as a placeholder text condition in +/// buildHighlightCondition). +export function patchBuiltHighlightRule( + target: BuiltHighlightRule, + ruleType: string, + dxf: WorkbookCellStyle | undefined, +): void { + if (ruleType === 'containsErrors' || ruleType === 'notContainsErrors') { + target.operator = ruleType + delete target.value + } + if (!dxf) return + // A numFmt-only dxf builds with no style object at all. + if (dxf.numberFormat) { + target.style = { ...target.style, n: { pattern: dxf.numberFormat } } + } + const bd = { + ...(dxf.borderTop ? { t: toUniverBorder(dxf.borderTop) } : {}), + ...(dxf.borderBottom ? { b: toUniverBorder(dxf.borderBottom) } : {}), + ...(dxf.borderLeft ? { l: toUniverBorder(dxf.borderLeft) } : {}), + ...(dxf.borderRight ? { r: toUniverBorder(dxf.borderRight) } : {}), + } + if (Object.keys(bd).length > 0) target.style = { ...target.style, bd } +} + function toCfValue(cfvo: { kind: string; value?: string | undefined }): IValueConfig { switch (cfvo.kind) { case 'min': @@ -4919,6 +5074,12 @@ function buildHighlightCondition( return builder.whenCellEmpty() case 'notContainsBlanks': return builder.whenCellNotEmpty() + case 'containsErrors': + case 'notContainsErrors': + // Univer's calculate unit evaluates these operators but the facade + // builder has no setter; build a placeholder text condition and + // re-target the operator on the built rule (patchBuiltHighlightRule). + return builder.whenTextContains('') case 'duplicateValues': return builder.setDuplicateValues() case 'uniqueValues': @@ -5334,8 +5495,14 @@ export function queueSparklineInstall( cells: entry.cells, })), ] - sparklineDisposablesRef.current = - sparklines.length === 0 ? [] : installSparklines(runtime, sparklines, sheetId) + // In-cell rich-value pictures share the per-cell float DOM lifecycle. + const cellImages = sheetMeta?.cellImages ?? [] + sparklineDisposablesRef.current = [ + ...(sparklines.length === 0 ? [] : installSparklines(runtime, sparklines, sheetId)), + ...(cellImages.length === 0 + ? [] + : installCellImages(runtime, state.file.sessionId, cellImages, sheetId)), + ] }, 100) } diff --git a/apps/sheets/src/renderer/workbook-ops.ts b/apps/sheets/src/renderer/workbook-ops.ts index 85c0ffe..1a4dfb1 100644 --- a/apps/sheets/src/renderer/workbook-ops.ts +++ b/apps/sheets/src/renderer/workbook-ops.ts @@ -5,6 +5,8 @@ * helpers applied against the live Univer runtime. Extracted from App.tsx; * every function receives its runtime and state explicitly. */ +import { isDefaultFormat, numfmt } from '@univerjs/core' +import { DATE_1904_OFFSET, isCalendarDatePattern } from './numfmt-fix' import type { AddPivotOperation, AddTableColumnOperation, @@ -221,6 +223,46 @@ export function applyAiTableColumnAdd( }) } +/// Width-independent pivot source read. getValues() goes through the view +/// interceptors, which clip numbers to the column width (#### fill, +/// budget-limited General) — lies that must not become pivot labels, +/// aggregates, saved sharedItems, or recompute keys (a date column displayed +/// as #### broke timeline binding and slicer recompute). Re-format raw +/// values through each cell's own pattern instead; General numbers stay +/// numeric. +export function readPivotSourceGrid( + range: { + getRawValues(): unknown[][] + getNumberFormats(): string[][] + getFormulas(): string[][] + }, + date1904: boolean, +): (string | number | boolean | null)[][] { + const patterns = range.getNumberFormats() + // 1904 workbooks: file-loaded static serials count from 1904-01-01, but + // formula results come from the 1900-based engine — same rule as the + // display interceptor's calendar-date shift. + const formulas = date1904 ? range.getFormulas() : null + return (range.getRawValues() as (string | number | boolean | null)[][]).map((row, rowOffset) => + row.map((value, columnOffset) => { + if (typeof value !== 'number') return value + const pattern = patterns[rowOffset]?.[columnOffset] ?? '' + if (pattern === '' || isDefaultFormat(pattern)) return value + const shift = + formulas !== null && + isCalendarDatePattern(pattern) && + (formulas[rowOffset]?.[columnOffset] ?? '') === '' + ? DATE_1904_OFFSET + : 0 + try { + return numfmt.format(pattern, value + shift, { nbsp: true, throws: false }) + } catch { + return value + } + }), + ) +} + export function applyAiPivotAdd( runtime: UniverRuntime, state: LazyWorkbookState, @@ -257,9 +299,10 @@ export function applyAiPivotAdd( // Univer's Nullable narrows to the scalar union we aggregate // over; undefined behaves like an empty cell throughout. - const grid = sourceSheet - .getRange(source.startRow, source.startColumn, sourceRows, sourceColumns) - .getValues() as (string | number | boolean | null)[][] + const grid = readPivotSourceGrid( + sourceSheet.getRange(source.startRow, source.startColumn, sourceRows, sourceColumns), + state.file.date1904 === true, + ) const fieldNames = (grid[0] ?? []).map((value) => String(value ?? '').trim()) if (fieldNames.some((name) => name.length === 0)) { throw new Error(t('appPivotHeaderBlank')) diff --git a/apps/sheets/src/shared/desktop-api.ts b/apps/sheets/src/shared/desktop-api.ts index 7f13df7..045fbd8 100644 --- a/apps/sheets/src/shared/desktop-api.ts +++ b/apps/sheets/src/shared/desktop-api.ts @@ -1,6 +1,11 @@ import { z } from 'zod' -import { MAX_SAVE_EDITS, MAX_SAVE_EDITS_TOTAL, SAVE_EDITS_CHUNK_MAX } from './ipc-channels' +import { + MAX_CSV_EXPORT_CHARS, + MAX_SAVE_EDITS, + MAX_SAVE_EDITS_TOTAL, + SAVE_EDITS_CHUNK_MAX, +} from './ipc-channels' import { ADDABLE_SHAPE_TYPES } from './shape-types' import type { AiChatRequest, @@ -52,6 +57,10 @@ const worksheetMetadataSchema = z /// compatibility with an older sidecar binary. defaultRowHeightFixed: z.boolean().optional(), defaultColumnWidth: z.number().nonnegative().nullable(), + /// sheetFormatPr/@baseColWidth — the built-in default column width is + /// derived from this (default 8) when defaultColWidth is absent. + /// Optional for compatibility with an older sidecar binary. + baseColumnWidth: z.number().nonnegative().nullable().optional(), freeze: z .object({ frozenColumns: z.number().int().nonnegative(), @@ -140,6 +149,8 @@ const worksheetMetadataSchema = z /// Body band fill for solid families (Dark); stripe overlays it. wholeTableFill: z.string().optional(), stripeFill: z.string().optional(), + /// A named pivotTableStyleInfo: band rows bold even with no fills. + styled: z.boolean().optional(), firstDataRow: z.number().int().nonnegative().optional(), rowGrandTotals: z.boolean().optional(), }) @@ -173,6 +184,25 @@ const worksheetMetadataSchema = z ) .max(100) .default([]), + /// Saved `_xlnm.Print_Area` / `_xlnm.Print_Titles` formulas for this + /// sheet, verbatim from workbook.xml. + printArea: z.string().min(1).max(2_000).optional(), + printTitles: z.string().min(1).max(2_000).optional(), + /// In-cell rich-value pictures (Excel "place picture in cell"): the id is + /// a media lookup key for readWorkbookMedia, like a visual id. + /// Defaulted for stale sidecar binaries, like sparklines. + cellImages: z + .array( + z + .object({ + id: z.string().min(1), + row: z.number().int().nonnegative(), + column: z.number().int().nonnegative(), + }) + .strict(), + ) + .max(500) + .default([]), }) .strict() const richRunSchema = z @@ -274,6 +304,11 @@ const drawingAnchorSchema = z toColumn: z.number().int().nonnegative(), toRowOffset: z.number().int(), toColumnOffset: z.number().int(), + /// True when the file carried a real `` marker: its offset + /// clamps at the cell edge (Excel behavior for broken writers) instead + /// of walking past it like synthesized oneCellAnchor/absoluteAnchor + /// encodings. + explicitTo: z.boolean().optional(), }) .strict() const chartAxisInfoSchema = z @@ -286,6 +321,8 @@ const chartAxisInfoSchema = z majorGridlines: z.boolean(), /// c:delete — the axis scales series but is not drawn. hidden: z.boolean().default(false), + /// c:scaling/c:orientation val="maxMin". + reversed: z.boolean().default(false), }) .strict() @@ -342,9 +379,24 @@ const visualObjectSchema = z .optional(), /// spPr/a:ln color; "none" for an explicit noFill line. lineColor: z.string().optional(), + /// spPr/a:ln/@w in CSS px (EMU / 12700 pt · 96/72). + lineWidth: z.number().finite().optional(), smooth: z.boolean().optional(), /// c:marker symbol — "none" hides scatter/line markers. marker: z.string().optional(), + /// First outer multiLvlStrCache level; start/end index the + /// compacted `categories` (end exclusive). + categoryGroups: z + .array( + z + .object({ + label: z.string(), + start: z.number().int().nonnegative(), + end: z.number().int().nonnegative(), + }) + .strict(), + ) + .optional(), }) .strict(), ), @@ -390,6 +442,8 @@ const visualObjectSchema = z secondaryYAxis: chartAxisInfoSchema.optional(), /// c:scatterStyle — whether scatter points connect with lines. scatterStyle: z.string().optional(), + /// Plot-level c:lineChart/c:marker flag; per-series symbols refine it. + lineMarkers: z.boolean().optional(), /// c:title/c:txPr//a:defRPr shorthand. titleStyle: z .object({ @@ -405,6 +459,12 @@ const visualObjectSchema = z chartPath: z.string().optional(), mediaPath: z.string().optional(), mediaType: z.string().optional(), + /// a:blip/a:alphaModFix amt as 0..1 picture opacity. + opacity: z.number().min(0).max(1).optional(), + /// spPr/a:blipFill on a shape — the image painted clipped to the + /// preset geometry. + fillMediaPath: z.string().optional(), + fillMediaType: z.string().optional(), /// Renderer-only: preview bytes for images added this session (not yet in /// the package); the sidecar never emits this. mediaDataUrl: z.string().optional(), @@ -526,9 +586,14 @@ export const workbookFileSchema = z .strict(), ), readOnly: z.boolean(), - /// Converted import (.xls/.csv): the first save opens a Save As dialog, + /// Converted .xls import: the first save opens a Save As dialog, /// so background flows (AutoSave) must not trigger mode 'save'. needsSaveAs: z.boolean().optional(), + /// CSV session: the original .csv on disk. Save keeps the CSV identity — + /// the renderer sends csvContent with the save and the main process + /// writes it back here. Background flows (AutoSave, crash recovery) + /// stand down: silently flattening the file would lose data. + csvPath: z.string().min(1).optional(), /// Session opened from a restored crash-recovery copy: Save silently /// writes back to the original file, and the 30s recovery writer stands /// down (it would overwrite the copy the sidecar is streaming from). @@ -673,6 +738,38 @@ export const workbookRangeResultSchema = z /// sheet-wide, complete-only. rowBreaks: z.array(z.number().int().nonnegative()).max(1_024), colBreaks: z.array(z.number().int().nonnegative()).max(1_024), + /// Saved print settings (pageSetup / pageMargins / printOptions / + /// headerFooter); sheet-wide, complete-only. Absent/null when the sheet + /// declares none (also for stale sidecar binaries). + pageSetup: z + .object({ + orientation: z.enum(['portrait', 'landscape']).optional(), + paperSize: z.number().int().min(1).max(256).optional(), + scale: z.number().int().min(10).max(400).optional(), + fitToWidth: z.number().int().min(0).max(32_767).optional(), + fitToHeight: z.number().int().min(0).max(32_767).optional(), + fitToPage: z.boolean().optional(), + /// Inches. + margins: z + .object({ + left: z.number().min(0).max(10), + right: z.number().min(0).max(10), + top: z.number().min(0).max(10), + bottom: z.number().min(0).max(10), + header: z.number().min(0).max(10), + footer: z.number().min(0).max(10), + }) + .strict() + .optional(), + printGridlines: z.boolean().optional(), + printHeadings: z.boolean().optional(), + /// Excel-encoded odd header/footer (&L/&C/&R sections, field codes). + oddHeader: z.string().min(1).max(500).optional(), + oddFooter: z.string().min(1).max(500).optional(), + }) + .strict() + .nullish() + .default(null), /// Allow-edit ranges (); sheet-wide, complete-only. protectedRanges: z .array( @@ -1651,6 +1748,9 @@ export const workbookSaveRequestSchema = z /// change is the workbook bytes themselves, so the request is valid with /// an otherwise empty payload (like an explicit Save As). restoreWriteBack: z.boolean().optional(), + /// CSV session in-place save: the active sheet serialized as CSV text. + /// Written back to the session's original .csv after the xlsx save. + csvContent: z.string().max(MAX_CSV_EXPORT_CHARS).optional(), edits: z.array(workbookCellEditSchema).max(MAX_SAVE_EDITS), /// Edit sets above MAX_SAVE_EDITS arrive through the chunked transfer /// (saveEditsBegin/saveEditsChunk) instead of inline: the request then @@ -1854,7 +1954,15 @@ export const workbookSaveRequestSchema = z }) export const workbookSaveResultSchema = z.union([ - z.object({ canceled: z.literal(true) }).strict(), + z + .object({ + canceled: z.literal(true), + /// The user picked the CSV format in the Save As dialog: no xlsx was + /// written; the renderer serializes the active sheet to this path via + /// the CSV export channel instead. + csvSaveAsPath: z.string().min(1).optional(), + }) + .strict(), z .object({ canceled: z.literal(false), @@ -2089,6 +2197,8 @@ export type WorkbookRangeRequest = z.infer export type WorkbookFormulaCellsRequest = z.infer export type WorkbookFormulaCellsResult = z.infer export type WorkbookRangeResult = z.infer +/// The sheet's saved print settings as parsed from the file. +export type WorkbookPagePrintSettings = NonNullable export type WorkbookRecalcRequest = z.infer export type WorkbookRecalcResult = z.infer export type WorkbookMediaRequest = z.infer @@ -2168,6 +2278,8 @@ const agentMessageSchema = z.union([ role: z.literal('assistant'), text: z.string(), toolCalls: z.array(agentToolCallSchema).optional(), + // captured model thinking, echoed back for interleaved-thinking models + reasoning: z.string().optional(), }) .strict(), z.object({ role: z.literal('tool'), results: z.array(agentToolResultSchema) }).strict(), @@ -2234,6 +2346,10 @@ export const workbookExportPdfRequestSchema = z }) .strict(), scale: z.number().min(0.1).max(2), + /// Chromium print header/footer templates (rendered in the margin + /// boxes; `pageNumber`/`totalPages` spans resolve per page). + headerTemplate: z.string().min(1).max(50_000).optional(), + footerTemplate: z.string().min(1).max(50_000).optional(), }) .strict() @@ -2245,6 +2361,38 @@ export const workbookExportPdfResultSchema = z.union([ export type WorkbookExportPdfRequest = z.infer export type WorkbookExportPdfResult = z.infer +/// CSV export of the active sheet: the renderer serializes display values, +/// the main process runs the loss warning + save dialog and writes the bytes. +export const workbookExportCsvRequestSchema = z + .object({ + fileName: z.string().min(1).max(255), + content: z.string().max(MAX_CSV_EXPORT_CHARS), + /// The exported sheet carries formulas — CSV keeps their values only, so + /// the main process offers Save As .xlsx first (Excel's warning flow). + hasFormulas: z.boolean(), + /// Set when the workbook has more sheets than the exported one. + activeSheetName: z.string().max(255).optional(), + /// Write straight to this path (from the Save As dialog's CSV pick) + /// instead of opening another save dialog. + targetPath: z.string().min(1).optional(), + }) + .strict() + +export const workbookExportCsvResultSchema = z.union([ + z + .object({ + canceled: z.literal(true), + /// The user chose "Save as .xlsx" in the formula-loss warning; the + /// renderer routes into the regular Save As flow instead. + saveAsXlsxInstead: z.boolean().optional(), + }) + .strict(), + z.object({ canceled: z.literal(false), path: z.string().min(1) }).strict(), +]) + +export type WorkbookExportCsvRequest = z.infer +export type WorkbookExportCsvResult = z.infer + // ---- Chat attachments (local files fed to the agent via tools; same structure // as apps/docs and apps/slides) ---- @@ -2349,6 +2497,9 @@ export interface DesktopApi { baseName: string, ): Promise<{ renamed: boolean; name?: string }> exportPdf(request: WorkbookExportPdfRequest): Promise + exportCsv(request: WorkbookExportCsvRequest): Promise + /// First Save of a CSV session: native "keep this format?" dialog. + confirmCsvSave(): Promise<'csv' | 'xlsx' | 'cancel'> closeWorkbook(sessionId: string): Promise openExternal(url: string): Promise /// Application-menu File commands (Open/Save/Save As); returns unsubscribe. @@ -2408,7 +2559,7 @@ export interface DesktopApi { getPathForFile(file: File): string } -export type MenuAction = 'open' | 'save' | 'save-as' | 'export-pdf' | 'undo' | 'redo' +export type MenuAction = 'open' | 'save' | 'save-as' | 'export-pdf' | 'export-csv' | 'undo' | 'redo' export interface WebSearchResult { results: Array<{ title: string; url: string; snippet: string }> diff --git a/apps/sheets/src/shared/ipc-channels.ts b/apps/sheets/src/shared/ipc-channels.ts index e276896..760a545 100644 --- a/apps/sheets/src/shared/ipc-channels.ts +++ b/apps/sheets/src/shared/ipc-channels.ts @@ -15,6 +15,10 @@ export const MAX_SAVE_EDITS = 1_000_000 export const SAVE_EDITS_CHUNK_MAX = 100_000 export const MAX_SAVE_EDITS_TOTAL = 10_000_000 +/// Upper bound on one CSV export's serialized text (shared by the zod schema +/// and the preload validator, like MAX_SAVE_EDITS above). +export const MAX_CSV_EXPORT_CHARS = 64_000_000 + export const IPC_CHANNELS = { selectWorkbook: 'workbook:select', readWorkbookRange: 'workbook:read-range', @@ -40,6 +44,8 @@ export const IPC_CHANNELS = { closeSaveRequest: 'workbook:close-save-request', closeSaveResult: 'workbook:close-save-result', exportPdf: 'workbook:export-pdf', + exportCsv: 'workbook:export-csv', + csvSaveConfirm: 'workbook:csv-save-confirm', openExternal: 'shell:open-external', menuAction: 'menu:action', aiGetSettings: 'ai:get-settings', diff --git a/apps/sheets/tests/anchor-explicit-to.test.ts b/apps/sheets/tests/anchor-explicit-to.test.ts new file mode 100644 index 0000000..e696af1 --- /dev/null +++ b/apps/sheets/tests/anchor-explicit-to.test.ts @@ -0,0 +1,41 @@ +import { describe, expect, it } from 'vitest' + +import { + clampExplicitTo, + EMU_PER_PIXEL, + markerFrom, + walkMarker, +} from '../src/renderer/WorkbookVisuals' + +const columnWidth = (): number => 64 + +describe('clampExplicitTo', () => { + it('clamps a real offset at the cell edge like Excel', () => { + // closedxml_picture-webp.xlsx: to = col 0 + 3524250 EMU (~370px) while + // col 0 is ~64px wide — Excel shows a col-sized sliver, not 370px. + const to = markerFrom(0, 3524250) + expect(to.offset).toBeCloseTo(3524250 / EMU_PER_PIXEL) + expect(clampExplicitTo(to, true, columnWidth)).toEqual({ index: 0, offset: 64 }) + }) + + it('keeps in-cell offsets untouched', () => { + const to = markerFrom(3, 10 * EMU_PER_PIXEL) + expect(clampExplicitTo(to, true, columnWidth)).toBe(to) + }) + + it('never clamps synthesized markers (oneCellAnchor ext, absoluteAnchor)', () => { + const to = markerFrom(0, 3524250) + expect(clampExplicitTo(to, false, columnWidth)).toBe(to) + }) + + it('drag commits walk from the clamped edge, not the raw overflow', () => { + // commitDrag seeds its to marker this way: without the clamp, walkMarker + // normalizes the ~370px overflow into real columns and the committed + // anchor re-expands the visual to its walked size. + const to = clampExplicitTo(markerFrom(0, 3524250), true, columnWidth) + expect(walkMarker(to, 10, columnWidth, 100)).toEqual({ index: 1, offset: 10 }) + // Normal in-cell anchors take the identical path (clamp is a no-op). + const normal = clampExplicitTo(markerFrom(2, 10 * EMU_PER_PIXEL), true, columnWidth) + expect(walkMarker(normal, 10, columnWidth, 100)).toEqual({ index: 2, offset: 20 }) + }) +}) diff --git a/apps/sheets/tests/atomic-write.test.ts b/apps/sheets/tests/atomic-write.test.ts new file mode 100644 index 0000000..2c9d52c --- /dev/null +++ b/apps/sheets/tests/atomic-write.test.ts @@ -0,0 +1,101 @@ +import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs' +import { rename, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it, vi } from 'vitest' + +import { atomicWriteFile } from '../src/main/atomic-write' + +vi.mock('node:fs/promises', async (importOriginal) => { + const actual = await importOriginal() + return { ...actual, rename: vi.fn(actual.rename), writeFile: vi.fn(actual.writeFile) } +}) + +let dir = '' + +afterEach(() => { + if (dir) rmSync(dir, { recursive: true, force: true }) + dir = '' + vi.mocked(rename).mockClear() + vi.mocked(writeFile).mockClear() +}) + +const epermError = () => + Object.assign(new Error('EPERM: operation not permitted'), { code: 'EPERM' }) + +describe('atomicWriteFile', () => { + it('replaces the target and leaves no temp file behind', async () => { + dir = mkdtempSync(join(tmpdir(), 'sheets-aw-')) + const target = join(dir, 'data.csv') + writeFileSync(target, 'old') + + await atomicWriteFile(target, Buffer.from('new')) + + expect(readFileSync(target, 'utf8')).toBe('new') + expect(readdirSync(dir)).toEqual(['data.csv']) + }) + + it('retries transient Windows rename locks', async () => { + dir = mkdtempSync(join(tmpdir(), 'sheets-aw-')) + const target = join(dir, 'data.csv') + writeFileSync(target, 'old') + vi.mocked(rename).mockRejectedValueOnce(epermError()).mockRejectedValueOnce(epermError()) + + await atomicWriteFile(target, Buffer.from('new')) + + expect(vi.mocked(rename)).toHaveBeenCalledTimes(3) + expect(readFileSync(target, 'utf8')).toBe('new') + expect(readdirSync(dir)).toEqual(['data.csv']) + }) + + it('falls back to an in-place write when the rename stays locked', async () => { + dir = mkdtempSync(join(tmpdir(), 'sheets-aw-')) + const target = join(dir, 'data.csv') + writeFileSync(target, 'old') + for (let attempt = 0; attempt < 5; attempt += 1) { + vi.mocked(rename).mockRejectedValueOnce(epermError()) + } + + await atomicWriteFile(target, Buffer.from('new')) + + expect(readFileSync(target, 'utf8')).toBe('new') + expect(readdirSync(dir)).toEqual(['data.csv']) + }) + + it('preserves the completed temp file when the fallback write fails', async () => { + dir = mkdtempSync(join(tmpdir(), 'sheets-aw-')) + const target = join(dir, 'data.csv') + writeFileSync(target, 'old') + for (let attempt = 0; attempt < 5; attempt += 1) { + vi.mocked(rename).mockRejectedValueOnce(epermError()) + } + const actual = await vi.importActual('node:fs/promises') + const fallbackError = Object.assign(new Error('EIO: fallback write failed'), { code: 'EIO' }) + vi.mocked(writeFile) + .mockImplementationOnce(actual.writeFile) + .mockRejectedValueOnce(fallbackError) + + await expect(atomicWriteFile(target, Buffer.from('new'))).rejects.toThrow( + 'fallback write failed', + ) + + const files = readdirSync(dir) + expect(files).toContain('data.csv') + const temp = files.find((file) => file !== 'data.csv') + expect(temp).toBeDefined() + expect(readFileSync(join(dir, temp!), 'utf8')).toBe('new') + }) + + it('uses distinct temp files for concurrent writes to one target', async () => { + dir = mkdtempSync(join(tmpdir(), 'sheets-aw-')) + const target = join(dir, 'data.csv') + + await Promise.all([ + atomicWriteFile(target, Buffer.from('first')), + atomicWriteFile(target, Buffer.from('second')), + ]) + + expect(['first', 'second']).toContain(readFileSync(target, 'utf8')) + expect(readdirSync(dir)).toEqual(['data.csv']) + }) +}) diff --git a/apps/sheets/tests/cell-font-fallback.test.ts b/apps/sheets/tests/cell-font-fallback.test.ts index 5cc3c7f..42eb888 100644 --- a/apps/sheets/tests/cell-font-fallback.test.ts +++ b/apps/sheets/tests/cell-font-fallback.test.ts @@ -6,7 +6,7 @@ import { withSansSerifFallback, } from '../src/renderer/cell-font-fallback' -const EMOJI = '"Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji"' +const EMOJI = '"Cell Text Dingbats", "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji"' describe('withSansSerifFallback', () => { it('appends sans-serif to a bare family string', () => { diff --git a/apps/sheets/tests/cf-rule-patch.test.ts b/apps/sheets/tests/cf-rule-patch.test.ts new file mode 100644 index 0000000..0b6d82a --- /dev/null +++ b/apps/sheets/tests/cf-rule-patch.test.ts @@ -0,0 +1,147 @@ +import { describe, expect, it } from 'vitest' +import { BorderStyleTypes } from '@univerjs/core' +import { FConditionalFormattingBuilder } from '@univerjs/sheets-conditional-formatting/facade' +import { + buildConditionalRule, + patchBuiltHighlightRule, + type BuiltHighlightRule, +} from '../src/renderer/univer-sync' +import type { WorkbookCellStyle } from '../src/shared/desktop-api' + +type CfRule = Parameters[2] + +const worksheet = { + newConditionalFormattingRule: () => new FConditionalFormattingBuilder(), +} as unknown as Parameters[0] + +function cfRule(overrides: Partial): CfRule { + return { + ranges: [{ startRow: 0, startColumn: 0, endRow: 3, endColumn: 0 }], + ruleType: 'cellIs', + formulas: [], + priority: 1, + percent: false, + bottom: false, + cfvos: [], + colors: [], + iconReverse: false, + showValue: true, + ...overrides, + } +} + +function dxf(overrides: Partial): WorkbookCellStyle { + return { + bold: false, + italic: false, + underline: false, + strikethrough: false, + wrapText: false, + diagonalUp: false, + diagonalDown: false, + ...overrides, + } +} + +describe('patchBuiltHighlightRule', () => { + it('re-targets the placeholder text condition to containsErrors', () => { + const target: BuiltHighlightRule = { + operator: 'containsText', + value: '', + style: { bg: { rgb: '#FF0000' } }, + } + patchBuiltHighlightRule(target, 'containsErrors', dxf({ fillColor: '#FF0000' })) + expect(target.operator).toBe('containsErrors') + expect('value' in target).toBe(false) + expect(target.style).toMatchObject({ bg: { rgb: '#FF0000' } }) + }) + + it('re-targets to notContainsErrors', () => { + const target: BuiltHighlightRule = { operator: 'containsText', value: '' } + patchBuiltHighlightRule(target, 'notContainsErrors', undefined) + expect(target.operator).toBe('notContainsErrors') + expect('value' in target).toBe(false) + }) + + it('leaves other rule types untouched', () => { + const target: BuiltHighlightRule = { operator: 'containsText', value: 'apple' } + patchBuiltHighlightRule(target, 'containsText', undefined) + expect(target).toEqual({ operator: 'containsText', value: 'apple' }) + }) + + it('injects dxf borders onto the built style', () => { + const target: BuiltHighlightRule = {} + patchBuiltHighlightRule( + target, + 'cellIs', + dxf({ + fontColor: '#008000', + borderTop: { style: 'thin', color: '#008000' }, + borderBottom: { style: 'medium' }, + }), + ) + expect(target.style?.bd).toEqual({ + t: { s: BorderStyleTypes.THIN, cl: { rgb: '#008000' } }, + b: { s: BorderStyleTypes.MEDIUM, cl: { rgb: '#000000' } }, + }) + }) + + it('merges borders and number format without dropping builder style keys', () => { + const target: BuiltHighlightRule = { style: { bg: { rgb: '#FFFF00' } } } + patchBuiltHighlightRule( + target, + 'expression', + dxf({ numberFormat: '0.00%', borderLeft: { style: 'dashed', color: '#0000FF' } }), + ) + expect(target.style).toMatchObject({ + bg: { rgb: '#FFFF00' }, + n: { pattern: '0.00%' }, + bd: { l: { s: BorderStyleTypes.DASHED, cl: { rgb: '#0000FF' } } }, + }) + }) + + it('does not create a style for a border-free dxf', () => { + const target: BuiltHighlightRule = {} + patchBuiltHighlightRule(target, 'cellIs', dxf({ fillColor: '#FF0000' })) + expect(target.style).toBeUndefined() + }) +}) + +describe('buildConditionalRule', () => { + it('builds notContainsErrors as a text rule with the error operator and dxf fill', () => { + const built = buildConditionalRule( + worksheet, + [dxf({ fillColor: '#FF0000' })], + cfRule({ ruleType: 'notContainsErrors', dxfIndex: 0, formulas: ['NOT(ISERROR(A1))'] }), + ) + expect(built?.rule).toMatchObject({ + type: 'highlightCell', + subType: 'text', + operator: 'notContainsErrors', + }) + expect((built?.rule as { value?: string }).value).toBeUndefined() + expect((built?.rule as BuiltHighlightRule).style?.bg).toBeTruthy() + expect(built?.ranges).toEqual([{ startRow: 0, startColumn: 0, endRow: 3, endColumn: 0 }]) + }) + + it('builds containsErrors', () => { + const built = buildConditionalRule( + worksheet, + [], + cfRule({ ruleType: 'containsErrors', formulas: ['ISERROR(A1)'] }), + ) + expect(built?.rule).toMatchObject({ subType: 'text', operator: 'containsErrors' }) + }) + + it('carries a dxf border onto the built cellIs rule style', () => { + const built = buildConditionalRule( + worksheet, + [dxf({ bold: true, borderTop: { style: 'thin', color: '#92D050' } })], + cfRule({ ruleType: 'cellIs', operator: 'equal', dxfIndex: 0, formulas: ['1'] }), + ) + expect(built?.rule).toMatchObject({ subType: 'number', operator: 'equal' }) + const style = (built?.rule as BuiltHighlightRule).style + expect(style?.bd).toEqual({ t: { s: BorderStyleTypes.THIN, cl: { rgb: '#92D050' } } }) + expect(style?.bl).toBe(1) + }) +}) diff --git a/apps/sheets/tests/chart-legend-swatch.test.ts b/apps/sheets/tests/chart-legend-swatch.test.ts new file mode 100644 index 0000000..435509e --- /dev/null +++ b/apps/sheets/tests/chart-legend-swatch.test.ts @@ -0,0 +1,51 @@ +import { describe, expect, it } from 'vitest' + +import { + legendSwatchColor, + legendUsesLineSwatches, + lineStroke, +} from '../src/renderer/WorkbookVisuals' + +describe('lineStroke', () => { + it('prefers the explicit series line color over the fill/accent default', () => { + expect(lineStroke({ color: '#9BBB59', lineColor: '#10253F' }, 0)).toBe('#10253F') + expect(lineStroke({ color: '#9BBB59' }, 0)).toBe('#9BBB59') + }) + + it('returns null for an explicit noFill line and falls back to the palette', () => { + expect(lineStroke({ color: '#9BBB59', lineColor: 'none' }, 0)).toBeNull() + expect(lineStroke({}, 0)).toBe('#4472c4') + }) +}) + +describe('legendSwatchColor', () => { + const series = { color: '#9BBB59', lineColor: '#10253F' } + + it('mirrors the drawn stroke on line-family charts', () => { + expect(legendSwatchColor(series, 0, true)).toBe('#10253F') + // noFill line: the swatch keeps the series color instead of vanishing. + expect(legendSwatchColor({ color: '#9BBB59', lineColor: 'none' }, 0, true)).toBe('#9BBB59') + expect(legendSwatchColor({}, 1, true)).toBe('#ed7d31') + }) + + it('keeps the fill resolution for non-line charts', () => { + expect(legendSwatchColor(series, 0, false)).toBe('#9BBB59') + expect(legendSwatchColor({}, 1, false)).toBe('#ed7d31') + }) +}) + +describe('legendUsesLineSwatches', () => { + it('follows the render dispatch: only a winning line/radar plot strokes', () => { + expect(legendUsesLineSwatches(['lineChart'])).toBe(true) + expect(legendUsesLineSwatches(['radarChart'])).toBe(true) + // Area wins the cascade over line: the drawn series are fills, so the + // swatches must keep the fill resolution. + expect(legendUsesLineSwatches(['lineChart', 'areaChart'])).toBe(false) + expect(legendUsesLineSwatches(['lineChart', 'scatterChart'])).toBe(false) + // Bar combos render BarChart; pie keeps its own legend. + expect(legendUsesLineSwatches(['barChart', 'lineChart'])).toBe(false) + expect(legendUsesLineSwatches(['radarChart', 'barChart'])).toBe(false) + expect(legendUsesLineSwatches(['pieChart', 'lineChart'])).toBe(false) + expect(legendUsesLineSwatches(['barChart'])).toBe(false) + }) +}) diff --git a/apps/sheets/tests/chart-state-edit.test.ts b/apps/sheets/tests/chart-state-edit.test.ts index 1b55e4f..2061a77 100644 --- a/apps/sheets/tests/chart-state-edit.test.ts +++ b/apps/sheets/tests/chart-state-edit.test.ts @@ -66,6 +66,18 @@ describe('applyChartStateEdit', () => { expect(next.series[0]?.categories).toEqual(['a', 'b']) }) + it('drops the parsed outer-level groups when an edit replaces the categories', () => { + const chart = base() + const first = chart.series[0] + if (first) first.categoryGroups = [{ label: '2008', start: 0, end: 2 }] + const kept = applyChartStateEdit(chart, { series: [{ index: 0, values: [5, 6] }] }) + expect(kept.series[0]?.categoryGroups).toEqual([{ label: '2008', start: 0, end: 2 }]) + const replaced = applyChartStateEdit(chart, { + series: [{ index: 0, categories: ['x', 'y'] }], + }) + expect(replaced.series[0]?.categoryGroups).toBeUndefined() + }) + it('merges slice explosions per point over the series default', () => { const pie = applyChartStateEdit(base(), { chartType: 'pie', explosionPct: 10 }) expect(pie.chartTypes).toEqual(['pieChart']) @@ -248,6 +260,7 @@ describe('transposeChartSeries', () => { describe('formatCategoryLabel', () => { it('formats numeric category text through its number format', () => { expect(formatCategoryLabel('44562', 'mmm\\-yy')).toBe('Jan-22') + expect(formatCategoryLabel('41387', 'mmm\\ yyyy')).toBe('Apr 2013') expect(formatCategoryLabel('0.152', '0.0%')).toBe('15.2%') }) @@ -368,6 +381,32 @@ describe('valueAxisScale', () => { expect(valueAxisScale(3750).ticks).toHaveLength(9) // 57362: 13 → 0..14 step 2 expect(valueAxisScale(13)).toEqual({ min: 0, max: 14, ticks: [0, 2, 4, 6, 8, 10, 12, 14] }) + // prod_055: 877 → 0..1000 step 100 (the earlier 900 reading came from a + // page-clipped ref; the plot area runs past the 900 gridline). + expect(valueAxisScale(877).max).toBe(1000) + expect(valueAxisScale(877).ticks).toHaveLength(11) + }) + + it('leaves Excel 5% auto-max headroom, calibrated on the real-run1 refs', () => { + // aspose_sample1 pivot chart: 18 → 0..20 step 2 + expect(valueAxisScale(18)).toEqual({ + min: 0, + max: 20, + ticks: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20], + }) + // aspose_sampleModifyLineChart: 148 → 0..160 step 20 + expect(valueAxisScale(148).max).toBe(160) + expect(valueAxisScale(148).ticks[1]).toBe(20) + // aspose_sampleGetFonts stacked: 289753.76 → 0..350000 step 50000 + expect(valueAxisScale(289753.76).max).toBe(350000) + expect(valueAxisScale(289753.76).ticks[1]).toBe(50000) + // aspose_sampleDisableTextWrappingForDataLabels: 1000 → 0..1200 step + // 200 (the 5% bump makes 11 intervals of 100, pushing the unit to 200) + expect(valueAxisScale(1000).max).toBe(1200) + expect(valueAxisScale(1000).ticks[1]).toBe(200) + // phpss_32readwriteLineChartNoPointMarkers1: 3490 → 0..4000 step 500 + expect(valueAxisScale(3490).max).toBe(4000) + expect(valueAxisScale(3490).ticks[1]).toBe(500) }) it('honours explicit bounds and unit', () => { diff --git a/apps/sheets/tests/column-width-mdw.test.ts b/apps/sheets/tests/column-width-mdw.test.ts index 5169edb..0c1802c 100644 --- a/apps/sheets/tests/column-width-mdw.test.ts +++ b/apps/sheets/tests/column-width-mdw.test.ts @@ -10,6 +10,7 @@ import { generalCharBudget } from '../src/renderer/numfmt-fix' import { characterWidthToPixels, measureNormalFontMdw, + paddedBaseColumnWidth, resolveNormalMdwFamily, toUniverStyle, } from '../src/renderer/univer-sync' @@ -65,6 +66,19 @@ describe('workbook MDW', () => { setWorkbookMdw(Number.NaN) expect(getWorkbookMdw()).toBe(7) }) + + it('derives the built-in default width from baseColWidth (prod_039)', () => { + // MDW 7 reproduces the classic 8.7109375 chars Excel writes into files; + // MDW 8 gives 8.625 chars = 74px, matching live Excel's 8.0-char default + // column — narrow enough that 10-digit General numbers go scientific. + setWorkbookMdw(7) + expect(paddedBaseColumnWidth(null)).toBe(8.7109375) + setWorkbookMdw(8) + expect(paddedBaseColumnWidth(null)).toBe(8.625) + expect(characterWidthToPixels(paddedBaseColumnWidth(null))).toBe(74) + expect(generalCharBudget(74)).toBe(8) + expect(paddedBaseColumnWidth(10)).toBe(10.625) + }) }) describe('measureNormalFontMdw', () => { @@ -130,7 +144,14 @@ describe('measureNormalFontMdw', () => { normalFontName: 'Nonexistent Face', }) expect(resolveNormalMdwFamily(file)).toBe('Calibri') - expect(measureNormalFontMdw(file)).toBe(7) + expect(measureNormalFontMdw(file)).toBe(8) + }) + + it('lays Calibri 11 out at the Mac Excel MDW 8, not the GDI 7', () => { + // Live probes + ref print geometry: prod_054 50.86ch → 305pt and + // prod_027 32.44ch → 195pt both fit floor((w+16/256)*8); MDW 7 wraps + // wide wrap columns a line early and re-fits their rows too tall. + expect(measureNormalFontMdw(workbook({ fontFamily: 'Calibri', fontSize: 11 }))).toBe(8) }) it('quotes the family for canvas measurement so odd names still measure', () => { diff --git a/apps/sheets/tests/csv-export.test.ts b/apps/sheets/tests/csv-export.test.ts new file mode 100644 index 0000000..c8ee0c6 --- /dev/null +++ b/apps/sheets/tests/csv-export.test.ts @@ -0,0 +1,205 @@ +import { readFileSync } from 'node:fs' +import { dirname, join } from 'node:path' +import { fileURLToPath } from 'node:url' +import { describe, expect, it } from 'vitest' + +import { parseCsv } from '../src/gateway/csv-import' +import { + csvField, + csvFromDisplayRows, + serializeActiveSheetCsv, + withoutFormulaView, +} from '../src/renderer/csv-export' +import { + workbookExportCsvRequestSchema, + workbookExportCsvResultSchema, + workbookSaveResultSchema, +} from '../src/shared/desktop-api' + +const here = dirname(fileURLToPath(import.meta.url)) +const read = (rel: string) => readFileSync(join(here, '..', rel), 'utf8') + +describe('csvField', () => { + it('passes plain text through unquoted', () => { + expect(csvField('hello')).toBe('hello') + expect(csvField('12.5')).toBe('12.5') + expect(csvField('')).toBe('') + }) + + it('quotes fields carrying commas, quotes, or newlines', () => { + expect(csvField('a,b')).toBe('"a,b"') + expect(csvField('say "hi"')).toBe('"say ""hi"""') + expect(csvField('line1\nline2')).toBe('"line1\nline2"') + }) + + it('normalizes CR/CRLF line breaks before quoting', () => { + expect(csvField('a\r\nb')).toBe('"a\nb"') + expect(csvField('a\rb')).toBe('"a\nb"') + }) + + it('leaves tabs unquoted (CSV only cares about its own delimiter)', () => { + expect(csvField('a\tb')).toBe('a\tb') + }) +}) + +describe('csvFromDisplayRows', () => { + it('joins rows with CRLF and ends the file with one', () => { + expect( + csvFromDisplayRows([ + ['a', 'b'], + ['c', ''], + ]), + ).toBe('a,b\r\nc,\r\n') + }) + + it('serializes an empty grid to an empty string', () => { + expect(csvFromDisplayRows([])).toBe('') + }) + + it('round-trips through the CSV importer', () => { + const rows = [ + ['name', 'note', 'amount'], + ['Ada, Bob', 'says "ok"', '1,234.50'], + ['multi\nline', '', '-3'], + ] + expect(parseCsv(csvFromDisplayRows(rows), ',')).toEqual(rows) + }) +}) + +function fakeCsvSheet(rows: string[][]) { + const columns = Math.max(1, ...rows.map((row) => row.length)) + return { + getLastRow: () => rows.length - 1, + getLastColumn: () => columns - 1, + getSheetId: () => 's1', + getSheetName: () => 'Sheet1', + getRange: (row: number, column: number, numRows: number, numColumns: number) => ({ + getDisplayValues: () => + rows + .slice(row, row + numRows) + .map((cells) => + Array.from({ length: numColumns }, (_, offset) => cells[column + offset] ?? ''), + ), + }), + getSheet: () => ({ getCellMatrix: () => ({ forValue: () => undefined }) }), + } +} + +describe('serializeActiveSheetCsv', () => { + it('serializes the display grid with quoting and rectangular padding', () => { + const sheet = fakeCsvSheet([['name', 'note'], ['a,b', 'say "hi"', 'extra'], ['solo']]) + expect(serializeActiveSheetCsv(sheet as never, null)).toBe( + 'name,note,\r\n"a,b","say ""hi""",extra\r\nsolo,,\r\n', + ) + }) +}) + +describe('withoutFormulaView', () => { + it('reads with the flag suspended and restores it afterwards', () => { + const sheets = new Set(['s1', 's2']) + const during = withoutFormulaView(sheets, 's1', () => sheets.has('s1')) + expect(during).toBe(false) + expect(sheets.has('s1')).toBe(true) + }) + + it('restores the flag when the read throws', () => { + const sheets = new Set(['s1']) + expect(() => + withoutFormulaView(sheets, 's1', () => { + throw new Error('boom') + }), + ).toThrow('boom') + expect(sheets.has('s1')).toBe(true) + }) + + it('never adds the flag to a sheet that did not have it', () => { + const sheets = new Set() + withoutFormulaView(sheets, 's1', () => undefined) + expect(sheets.size).toBe(0) + }) +}) + +describe('workbookExportCsv schemas', () => { + it('accepts a minimal request and one with the active-sheet name', () => { + expect( + workbookExportCsvRequestSchema.parse({ + fileName: 'data.csv', + content: 'a,b\r\n', + hasFormulas: false, + }), + ).toBeTruthy() + expect( + workbookExportCsvRequestSchema.parse({ + fileName: 'data.csv', + content: '', + hasFormulas: true, + activeSheetName: 'Sheet2', + }), + ).toBeTruthy() + }) + + it('rejects unknown keys and oversized names', () => { + expect(() => + workbookExportCsvRequestSchema.parse({ + fileName: 'data.csv', + content: '', + hasFormulas: false, + html: 'nope', + }), + ).toThrow() + expect(() => + workbookExportCsvRequestSchema.parse({ + fileName: 'x'.repeat(256), + content: '', + hasFormulas: false, + }), + ).toThrow() + }) + + it('accepts a direct targetPath for the Save As CSV reroute', () => { + expect( + workbookExportCsvRequestSchema.parse({ + fileName: 'data.csv', + content: 'a\r\n', + hasFormulas: false, + targetPath: '/tmp/data.csv', + }), + ).toBeTruthy() + }) + + it('lets a canceled save carry the picked CSV path', () => { + expect( + workbookSaveResultSchema.parse({ canceled: true, csvSaveAsPath: '/tmp/data.csv' }), + ).toEqual({ canceled: true, csvSaveAsPath: '/tmp/data.csv' }) + expect(workbookSaveResultSchema.parse({ canceled: true })).toEqual({ canceled: true }) + }) + + it('parses both result variants including the Save-As-xlsx reroute', () => { + expect(workbookExportCsvResultSchema.parse({ canceled: true })).toEqual({ canceled: true }) + expect( + workbookExportCsvResultSchema.parse({ canceled: true, saveAsXlsxInstead: true }), + ).toEqual({ canceled: true, saveAsXlsxInstead: true }) + expect(workbookExportCsvResultSchema.parse({ canceled: false, path: '/tmp/a.csv' })).toEqual({ + canceled: false, + path: '/tmp/a.csv', + }) + }) +}) + +describe('Export CSV wiring', () => { + it('has the File-menu item forwarding export-csv to the renderer', () => { + const mainSrc = read('src/main/sheets-main.ts') + expect(mainSrc).toMatch(/menuExportCsv[\s\S]{0,80}sendMenuAction\('export-csv'\)/) + expect(mainSrc).toContain("action === 'export-csv'") + }) + + it('dispatches the menu action into handleExportCsv', () => { + const appSrc = read('src/renderer/App.tsx') + expect(appSrc).toMatch(/action === 'export-csv'[\s\S]{0,80}handleExportCsvImpl/) + }) + + it('allows export-csv through the preload menu-action allowlist', () => { + const preloadSrc = read('src/preload/index.ts') + expect(preloadSrc).toContain("action === 'export-csv'") + }) +}) diff --git a/apps/sheets/tests/error-checking.test.ts b/apps/sheets/tests/error-checking.test.ts index 38ff616..1c8607c 100644 --- a/apps/sheets/tests/error-checking.test.ts +++ b/apps/sheets/tests/error-checking.test.ts @@ -6,6 +6,7 @@ import { scanStreamedWorkbookErrors, type SheetError, } from '../src/renderer/error-checking' +import { FILE_READ_BATCH_CELLS, MAX_SCAN_CELLS } from '../src/renderer/ai/workbook-search' import { ensureLazyRangeLoaded, readSheetRangeMapped } from '../src/renderer/univer-sync' import type { LazyWorkbookState } from '../src/renderer/univer-state' @@ -154,6 +155,55 @@ describe('scanStreamedWorkbookErrors', () => { expect(scan.errors).toHaveLength(0) expect(scan.truncated).toBe(true) }) + + it('reads back journal formula results and reports their errors', async () => { + const lazyState = state() + lazyState.editJournal.cells.set( + 's1', + new Map([['0', { row: 3, column: 1, value: null, formula: '=1/0', hasValue: true }]]), + ) + mockRead.mockImplementation(async () => mapped([])) + const scan = await scanStreamedWorkbookErrors(lazyState, { + resolveFormulaValue: () => '#DIV/0!', + }) + expect(scan.errors).toEqual([{ sheetId: 's1', row: 3, column: 1, value: '#DIV/0!' }]) + }) + + it('does not let style-only journal entries hide file errors', async () => { + const lazyState = state() + lazyState.editJournal.cells.set( + 's1', + new Map([['0', { row: 20, column: 2, value: null, hasValue: false }]]), + ) + mockRead.mockImplementation(async (_state, sheetId) => + sheetId === 's1' ? mapped([{ row: 20, column: 2, value: '#VALUE!' }]) : mapped([]), + ) + const scan = await scanStreamedWorkbookErrors(lazyState) + expect(scan.errors).toEqual([{ sheetId: 's1', row: 20, column: 2, value: '#VALUE!' }]) + }) + + it('follows live membership: session-added sheets scanned, deleted ones skipped', async () => { + const lazyState = state() + lazyState.editJournal.cells.set( + 's3', + new Map([['0', { row: 1, column: 0, value: '#REF!', hasValue: true }]]), + ) + mockRead.mockImplementation(async () => mapped([])) + const scan = await scanStreamedWorkbookErrors(lazyState, { liveSheetIds: ['s2', 's3'] }) + expect(scan.errors).toEqual([{ sheetId: 's3', row: 1, column: 0, value: '#REF!' }]) + // s1 was deleted this session: its file pages are never read. + expect(mockRead.mock.calls.every(([, sheetId]) => sheetId === 's2')).toBe(true) + }) + + it('stops at the scan-cell budget instead of paging the whole file', async () => { + const lazyState = state() + ;(lazyState.file.sheets as { rowCount: number }[])[0]!.rowCount = 300_000 + mockRead.mockImplementation(async () => mapped([])) + const scan = await scanStreamedWorkbookErrors(lazyState) + expect(scan.truncated).toBe(true) + const cellsPerBatch = Math.floor(FILE_READ_BATCH_CELLS / 4) * 4 + expect(mockRead).toHaveBeenCalledTimes(Math.ceil(MAX_SCAN_CELLS / cellsPerBatch)) + }) }) describe('runStreamedErrorCheck', () => { @@ -246,4 +296,25 @@ describe('runStreamedErrorCheck', () => { harnessData.setMessage.mock.calls.filter(([text]) => text === 'appCheckingErrors'), ).toHaveLength(1) }) + + it('bails out when the workbook is swapped mid-scan', async () => { + const harnessData = harness(state(), []) + mockRead.mockImplementation(async () => { + harnessData.lazyWorkbookRef.current = state() // file switch during the paged read + return mapped([{ row: 300, column: 0, value: '#DIV/0!' }]) + }) + await runStreamedErrorCheck(harnessData) + expect(mockEnsure).not.toHaveBeenCalled() + expect(harnessData.workbook.setActiveSheet).not.toHaveBeenCalled() + // Only the initial scanning status — no stale found/not-found result. + expect(harnessData.setMessage).toHaveBeenCalledTimes(1) + // The in-flight guard is released: the new workbook scans normally. + mockRead.mockImplementation(async () => mapped([])) + await runStreamedErrorCheck(harnessData) + expect(harnessData.setMessage.mock.calls.map(([text]) => text)).toEqual([ + 'appCheckingErrors', + 'appCheckingErrors', + 'appNoErrorsFound', + ]) + }) }) diff --git a/apps/sheets/tests/formula-closure.test.ts b/apps/sheets/tests/formula-closure.test.ts index 80c6a11..0d2e5b8 100644 --- a/apps/sheets/tests/formula-closure.test.ts +++ b/apps/sheets/tests/formula-closure.test.ts @@ -10,6 +10,7 @@ import { shiftPinnedCells, type ClosureSheetInput, } from '../src/renderer/formula-closure' +import { formulaKeepsCache } from '../src/renderer/univer-sync' const sheet = ( id: string, @@ -72,6 +73,21 @@ describe('containsUnresolvedNames', () => { }) }) +describe('formulaKeepsCache', () => { + it('keeps the cached value for Google Sheets __xludf exports (prod_016)', () => { + // Recalculating turns the float-repr fallback literal (46235.0) into a + // string the numfmt layer skips; the cached is the computed value. + expect(formulaKeepsCache('IFERROR(__xludf.DUMMYFUNCTION("""COMPUTED_VALUE"""),46235.0)')).toBe( + true, + ) + expect(formulaKeepsCache('IFERROR(__xludf.DUMMYFUNCTION("X"),"✖")')).toBe(true) + }) + + it('recalculates ordinary formulas', () => { + expect(formulaKeepsCache('SUM(A1:A5)')).toBe(false) + }) +}) + describe('computeFormulaClosure', () => { it('collects formulas plus referenced precedents across sheets', () => { const result = computeFormulaClosure( diff --git a/apps/sheets/tests/numfmt-fix.test.ts b/apps/sheets/tests/numfmt-fix.test.ts index c00b64d..59edfab 100644 --- a/apps/sheets/tests/numfmt-fix.test.ts +++ b/apps/sheets/tests/numfmt-fix.test.ts @@ -179,6 +179,8 @@ describe('formatGeneral', () => { it('switches to scientific when the integer part cannot fit', () => { expect(formatGeneral(123456789012, 8)).toBe('1.23E+11') expect(formatGeneral(1234567890123456, 20)).toBe('1.23457E+15') + // prod_039: 10-digit phone numbers in a built-in-width column. + expect(formatGeneral(7028015141, 8)).toBe('7.03E+09') }) it('uses scientific for tiny fractions instead of rounding to 0', () => { diff --git a/apps/sheets/tests/pivot-band-rows.test.ts b/apps/sheets/tests/pivot-band-rows.test.ts index a42de13..2b481a1 100644 --- a/apps/sheets/tests/pivot-band-rows.test.ts +++ b/apps/sheets/tests/pivot-band-rows.test.ts @@ -7,12 +7,18 @@ import { applyPivotStyling } from '../src/renderer/univer-sync' /// firstHeaderRow="1" firstDataRow="1"/> where A8 = "Row Labels" (header), /// A9/A10 = data, A11 = Grand Total. firstDataRow is the offset of the FIRST /// DATA row inside the ref, so header rows are offsets 0..firstDataRow-1. -function paint(pivot: { +interface PivotSpec { outputRef: string headerFill?: string + headerFontColor?: string + wholeTableFill?: string + stripeFill?: string + styled?: boolean firstDataRow?: number rowGrandTotals?: boolean -}): boolean[] { +} + +function paintCells(pivot: PivotSpec): ICellData[][] { const range = { startRow: 0, startColumn: 0, endRow: 19, endColumn: 4 } const matrix: ICellData[][] = Array.from({ length: 20 }, () => Array.from({ length: 5 }, () => ({})), @@ -20,7 +26,15 @@ function paint(pivot: { applyPivotStyling(matrix, range, [ { path: 'xl/pivotTables/pivotTable1.xml', cachePath: null, ...pivot }, ]) - return matrix.map((row) => Boolean(row[0]?.s)) + return matrix +} + +function paint(pivot: PivotSpec): boolean[] { + return paintCells(pivot).map((row) => Boolean(row[0]?.s)) +} + +function fills(pivot: PivotSpec): (string | undefined)[] { + return paintCells(pivot).map((row) => ((row[0]?.s ?? {}) as { bg?: { rgb?: string } }).bg?.rgb) } describe('applyPivotStyling', () => { @@ -62,8 +76,111 @@ describe('applyPivotStyling', () => { expect(painted[10]).toBe(false) }) - it('paints nothing without a style fill', () => { + it('paints nothing without a pivot style', () => { const painted = paint({ outputRef: 'A8:B11', firstDataRow: 1, rowGrandTotals: true }) expect(painted.every((row) => !row)).toBe(true) }) + + // Light 1-7 with stripes off: no fills survive, but a named style still + // bolds the header and grand-total bands. + it('bolds band rows of a styled pivot even when every fill is absent', () => { + const cells = paintCells({ + outputRef: 'A8:B11', + styled: true, + firstDataRow: 1, + rowGrandTotals: true, + }) + const bold = (row: number) => ((cells[row]?.[0]?.s ?? {}) as { bl?: number }).bl + expect(bold(7)).toBe(1) // A8 header + expect(cells[8]?.[0]?.s).toBeUndefined() // data rows untouched + expect(cells[9]?.[0]?.s).toBeUndefined() + expect(bold(10)).toBe(1) // A11 grand total + const rowFills = fills({ + outputRef: 'A8:B11', + styled: true, + firstDataRow: 1, + rowGrandTotals: true, + }) + expect(rowFills.every((fill) => fill === undefined)).toBe(true) + }) + + // Ground truth from oxmlsdk_RelationalPivotB6 (PivotStyleLight2, + // showRowStripes): unfilled header, stripes from the first data row. + it('stripes alternate data rows and leave a fill-less header unfilled but bold', () => { + const cells = paintCells({ + outputRef: 'A8:B14', + stripeFill: '#DCE6F1', + firstDataRow: 1, + rowGrandTotals: true, + }) + const rowFills = fills({ + outputRef: 'A8:B14', + stripeFill: '#DCE6F1', + firstDataRow: 1, + rowGrandTotals: true, + }) + expect(rowFills[7]).toBeUndefined() // A8 header: no fill for Light 1-7 + expect((cells[7]?.[0]?.s as { bl?: number }).bl).toBe(1) // ...but bold + expect(rowFills[8]).toBe('#DCE6F1') // A9 first data row starts the stripe + expect(rowFills[9]).toBeUndefined() + expect(rowFills[10]).toBe('#DCE6F1') + expect(rowFills[11]).toBeUndefined() + expect(rowFills[12]).toBe('#DCE6F1') + expect(rowFills[13]).toBeUndefined() // A14 grand total: band, not stripe + }) + + // Ground truth from aspose_sample1 (PivotStyleMedium9): solid accent header + // with white bold text. + it('paints the header font color with the header fill', () => { + const cells = paintCells({ + outputRef: 'A8:B11', + headerFill: '#4F81BD', + headerFontColor: '#FFFFFF', + firstDataRow: 1, + rowGrandTotals: false, + }) + const header = cells[7]?.[0]?.s as { bg?: { rgb?: string }; cl?: { rgb?: string }; bl?: number } + expect(header.bg?.rgb).toBe('#4F81BD') + expect(header.cl?.rgb).toBe('#FFFFFF') + expect(header.bl).toBe(1) + }) + + // Ground truth from aspose_sample1 (Medium9, G6:H7 with grand totals): the + // solid header does not bleed onto the grand-total row, which Excel leaves + // plain black-on-white bold. + it('keeps the solid header treatment off the grand-total row', () => { + const cells = paintCells({ + outputRef: 'A8:B11', + headerFill: '#4F81BD', + headerFontColor: '#FFFFFF', + firstDataRow: 1, + rowGrandTotals: true, + }) + type CellStyle = { bg?: { rgb?: string }; cl?: { rgb?: string }; bl?: number } + const header = cells[7]?.[0]?.s as CellStyle + expect(header.bg?.rgb).toBe('#4F81BD') + expect(header.cl?.rgb).toBe('#FFFFFF') + const total = cells[10]?.[0]?.s as CellStyle + expect(total.bg).toBeUndefined() + expect(total.cl).toBeUndefined() + expect(total.bl).toBe(1) + }) + + // Dark1 fills its body, so its calibrated grand-total band keeps the + // header fill and white text. + it('extends the header treatment to the grand total when the body is filled', () => { + const cells = paintCells({ + outputRef: 'A8:B11', + headerFill: '#808080', + headerFontColor: '#FFFFFF', + wholeTableFill: '#A6A6A6', + stripeFill: '#BFBFBF', + firstDataRow: 1, + rowGrandTotals: true, + }) + const total = cells[10]?.[0]?.s as { bg?: { rgb?: string }; cl?: { rgb?: string }; bl?: number } + expect(total.bg?.rgb).toBe('#808080') + expect(total.cl?.rgb).toBe('#FFFFFF') + expect(total.bl).toBe(1) + }) }) diff --git a/apps/sheets/tests/print-settings.test.ts b/apps/sheets/tests/print-settings.test.ts new file mode 100644 index 0000000..27521b9 --- /dev/null +++ b/apps/sheets/tests/print-settings.test.ts @@ -0,0 +1,355 @@ +import { describe, expect, it } from 'vitest' + +import { buildSheetPrintPayload, renderHeaderFooterHtml } from '../src/renderer/print-html' +import { + decodeHeaderFooter, + printAreasFromFormula, + printTitleRowsFromFormula, + resolveEffectivePageSetup, + type EffectivePageSetup, +} from '../src/renderer/print-settings' +import type { PrintWorksheet } from '../src/renderer/print-html' + +describe('decodeHeaderFooter', () => { + it('splits left/center/right sections and keeps resolvable field codes', () => { + expect(decodeHeaderFooter('&L&A&CSeite &P von &N')).toEqual({ + left: '&A', + center: 'Seite &P von &N', + }) + }) + + it('defaults unmarked text to the center section', () => { + expect(decodeHeaderFooter('Quarterly Report')).toEqual({ center: 'Quarterly Report' }) + }) + + it('strips font, size, color and unsupported codes', () => { + expect(decodeHeaderFooter('&C&"Broadway,Bold Italic"&12&KFF0000Big &BRed&B Title&G')).toEqual({ + center: 'Big Red Title', + }) + }) + + it('drops the picture/path codes but keeps the file code', () => { + expect(decodeHeaderFooter('&C&P / &N&R&Z&F')).toEqual({ + center: '&P / &N', + right: '&F', + }) + }) + + it('keeps escaped ampersands verbatim', () => { + expect(decodeHeaderFooter('&LProfit && Loss')).toEqual({ left: 'Profit && Loss' }) + }) + + it('returns null when everything strips away', () => { + expect(decodeHeaderFooter('&L&G')).toBeNull() + expect(decodeHeaderFooter('')).toBeNull() + }) + + it('accepts lowercase section markers', () => { + expect(decodeHeaderFooter('&lLeft&rRight')).toEqual({ left: 'Left', right: 'Right' }) + }) +}) + +describe('printAreasFromFormula', () => { + it('parses a quoted sheet-qualified absolute range', () => { + expect(printAreasFromFormula("'W PS Mustermann Hans'!$A$1:$K$84")).toEqual(['A1:K84']) + }) + + it('parses multiple areas and quoted commas', () => { + expect(printAreasFromFormula("'a,b'!$A$1:$B$2,'a,b'!$D$3:$E$4")).toEqual(['A1:B2', 'D3:E4']) + }) + + it('expands a single-cell area', () => { + expect(printAreasFromFormula('Sheet1!$B$2')).toEqual(['B2:B2']) + }) + + it('falls back to the used range for refs it cannot crop to', () => { + expect(printAreasFromFormula("'S'!$A:$C")).toEqual([]) + expect(printAreasFromFormula("'S'!#REF!")).toEqual([]) + expect(printAreasFromFormula("'S'!$A$1:$B$2,'S'!$C:$D")).toEqual([]) + }) + + it('returns [] when absent', () => { + expect(printAreasFromFormula(undefined)).toEqual([]) + }) +}) + +describe('printTitleRowsFromFormula', () => { + it('extracts the repeated row span', () => { + expect(printTitleRowsFromFormula("'S'!$17:$17")).toBe('17:17') + }) + + it('skips a column-repeat part and finds the rows', () => { + expect(printTitleRowsFromFormula("'S'!$A:$B,'S'!$1:$3")).toBe('1:3') + }) + + it('drops spans beyond the layout title cap', () => { + expect(printTitleRowsFromFormula("'S'!$1:$40")).toBeNull() + }) + + it('returns null when absent or column-only', () => { + expect(printTitleRowsFromFormula(undefined)).toBeNull() + expect(printTitleRowsFromFormula("'S'!$A:$B")).toBeNull() + }) +}) + +describe('resolveEffectivePageSetup', () => { + it('defaults to A4 portrait at 100% with normal margins', () => { + const setup = resolveEffectivePageSetup({}, null, null) + expect(setup.orientation).toBe('portrait') + expect(setup.paperSize).toBe(9) + expect(setup.scale).toBe(100) + expect(setup.fitToPage).toBe(false) + expect(setup.margins).toEqual({ + left: 0.7, + right: 0.7, + top: 0.75, + bottom: 0.75, + header: 0.3, + footer: 0.3, + }) + expect(setup.printAreas).toEqual([]) + expect(setup.header).toBeNull() + expect(setup.footer).toBeNull() + }) + + it('applies the saved file settings when the session touched nothing', () => { + const setup = resolveEffectivePageSetup( + {}, + { + orientation: 'landscape', + paperSize: 1, + scale: 65, + margins: { left: 0.98, right: 0.98, top: 5, bottom: 0.79, header: 0, footer: 0.51 }, + printGridlines: true, + oddFooter: '&CSeite &P von &N', + }, + { printArea: "'S'!$A$1:$K$84", printTitles: "'S'!$17:$17" }, + ) + expect(setup.orientation).toBe('landscape') + expect(setup.paperSize).toBe(1) + expect(setup.scale).toBe(65) + // margins clamp to the export wire's 3in cap + expect(setup.margins.top).toBe(3) + expect(setup.margins.left).toBe(0.98) + expect(setup.printGridlines).toBe(true) + expect(setup.printAreas).toEqual(['A1:K84']) + expect(setup.printTitles).toBe('17:17') + expect(setup.footer).toEqual({ center: 'Seite &P von &N' }) + expect(setup.header).toBeNull() + }) + + it('lets the session journal win over the file', () => { + const setup = resolveEffectivePageSetup( + { + orientation: 'portrait', + printArea: 'B2:C3', + printTitles: null, + header: null, + margins: 'narrow', + }, + { + orientation: 'landscape', + margins: { left: 1, right: 1, top: 1, bottom: 1, header: 0.5, footer: 0.5 }, + oddHeader: '&CFile Header', + }, + { printArea: "'S'!$A$1:$K$84", printTitles: "'S'!$1:$2" }, + ) + expect(setup.orientation).toBe('portrait') + expect(setup.printAreas).toEqual(['B2:C3']) + expect(setup.printTitles).toBeNull() + expect(setup.header).toBeNull() + expect(setup.margins.left).toBe(0.25) + }) + + it('clears the file print area when the session cleared it', () => { + const setup = resolveEffectivePageSetup({ printArea: null }, null, { + printArea: "'S'!$A$1:$K$84", + }) + expect(setup.printAreas).toEqual([]) + }) + + it('defaults fitToWidth to one page when the file only sets fitToPage', () => { + const setup = resolveEffectivePageSetup({}, { fitToPage: true }, null) + expect(setup.fitToPage).toBe(true) + expect(setup.fitToWidth).toBe(1) + }) + + it('shifts the file print area through rows inserted above', () => { + const setup = resolveEffectivePageSetup({}, null, { printArea: "'S'!$A$1:$K$84" }, [ + { kind: 'insert-rows', index: 0, count: 2 }, + ]) + expect(setup.printAreas).toEqual(['A3:K86']) + }) + + it('shrinks the file print area when a column inside it is deleted', () => { + const setup = resolveEffectivePageSetup({}, null, { printArea: "'S'!$A$1:$K$84" }, [ + { kind: 'remove-cols', index: 2, count: 1 }, + ]) + expect(setup.printAreas).toEqual(['A1:J84']) + }) + + it('falls back to the used range when the edits delete the whole area', () => { + const setup = resolveEffectivePageSetup({}, null, { printArea: "'S'!$B$2:$C$3" }, [ + { kind: 'remove-rows', index: 1, count: 2 }, + ]) + expect(setup.printAreas).toEqual([]) + }) + + it('shifts the file title rows through structural edits', () => { + const setup = resolveEffectivePageSetup({}, null, { printTitles: "'S'!$17:$17" }, [ + { kind: 'insert-rows', index: 0, count: 3 }, + ]) + expect(setup.printTitles).toBe('20:20') + }) + + it('keeps title rows through column edits and drops them when deleted', () => { + const columnEdit = resolveEffectivePageSetup({}, null, { printTitles: "'S'!$1:$2" }, [ + { kind: 'remove-cols', index: 0, count: 3 }, + ]) + expect(columnEdit.printTitles).toBe('1:2') + const deleted = resolveEffectivePageSetup({}, null, { printTitles: "'S'!$1:$2" }, [ + { kind: 'remove-rows', index: 0, count: 2 }, + ]) + expect(deleted.printTitles).toBeNull() + }) + + it('does not remap session-set print areas (already screen space)', () => { + const setup = resolveEffectivePageSetup({ printArea: 'B2:C3' }, null, null, [ + { kind: 'insert-rows', index: 0, count: 5 }, + ]) + expect(setup.printAreas).toEqual(['B2:C3']) + }) + + it('drops title rows stretched past the cap by inserts between them', () => { + const setup = resolveEffectivePageSetup({}, null, { printTitles: "'S'!$1:$2" }, [ + { kind: 'insert-rows', index: 1, count: 25 }, + ]) + expect(setup.printTitles).toBeNull() + // the export payload still builds instead of throwing on the span + const payload = buildSheetPrintPayload(fakeWorksheet(), setup, 'Book.pdf', 'S1') + expect(payload.html).toContain('') + }) +}) + +describe('renderHeaderFooterHtml', () => { + const now = new Date(2026, 0, 2, 3, 4, 5) + + it('turns &P/&N into live Chromium spans', () => { + expect(renderHeaderFooterHtml('Seite &P von &N', 'Book', 'S1', now)).toBe( + 'Seite von ', + ) + }) + + it('resolves static codes and escapes markup', () => { + const html = renderHeaderFooterHtml('&A <&F> && more', 'Bud 2, + getLastColumn: () => 1, + getRowHeight: () => 20, + getColumnWidth: () => 100, + getMergedRanges: () => [], + getRange: ((row: number, column: number, numRows?: number, numColumns?: number) => ({ + getDisplayValues: () => + usedGrid + .slice(row, row + (numRows ?? 1)) + .map((cells) => cells.slice(column, column + (numColumns ?? 1))), + getValues: () => + usedGrid + .slice(row, row + (numRows ?? 1)) + .map((cells) => cells.slice(column, column + (numColumns ?? 1))), + getCellStyleData: () => null, + })) as PrintWorksheet['getRange'], + } +} + +function payloadSetup(overrides: Partial): EffectivePageSetup { + return { + orientation: 'portrait', + paperSize: 9, + scale: 100, + fitToWidth: 0, + fitToPage: false, + margins: { left: 0.7, right: 0.7, top: 0.75, bottom: 0.75, header: 0.3, footer: 0.3 }, + printGridlines: false, + printHeadings: false, + printAreas: [], + printTitles: null, + header: null, + footer: null, + ...overrides, + } +} + +describe('buildSheetPrintPayload', () => { + it('crops the layout to the print area', () => { + const payload = buildSheetPrintPayload( + fakeWorksheet(), + payloadSetup({ printAreas: ['A1:A2'] }), + 'Book.pdf', + 'S1', + ) + expect(payload.html).toContain('A1') + expect(payload.html).toContain('A2') + expect(payload.html).not.toContain('B1') + expect(payload.html).not.toContain('A3') + }) + + it('emits one page-broken table per print area', () => { + const payload = buildSheetPrintPayload( + fakeWorksheet(), + payloadSetup({ printAreas: ['A1:A1', 'B2:B2'] }), + 'Book.pdf', + 'S1', + ) + expect(payload.html.match(/
    /g)).toHaveLength(2) + expect(payload.html).toContain('table + table { break-before: page; }') + }) + + it('carries the page geometry and header/footer templates', () => { + const payload = buildSheetPrintPayload( + fakeWorksheet(), + payloadSetup({ + orientation: 'landscape', + paperSize: 1, + scale: 65, + margins: { left: 0.98, right: 0.98, top: 0.98, bottom: 0.79, header: 0, footer: 0.51 }, + footer: { center: 'Seite &P von &N' }, + }), + 'Book.pdf', + 'S1', + ) + expect(payload.landscape).toBe(true) + expect(payload.pageSize).toBe('Letter') + expect(payload.scale).toBeCloseTo(0.65, 5) + expect(payload.margins).toEqual({ top: 0.98, bottom: 0.79, left: 0.98, right: 0.98 }) + expect(payload.headerTemplate).toBeUndefined() + expect(payload.footerTemplate).toContain('') + expect(payload.footerTemplate).toContain('') + expect(payload.footerTemplate).toContain('padding-bottom:0.51in') + // the template document is content-box: without an inline border-box the + // padded 100%-wide row overflows the page and shifts/clips the sections + expect(payload.footerTemplate).toContain('box-sizing:border-box') + }) + + it('shrinks to fit the width when fit-to-page is on', () => { + // content 150pt wide (two 100px columns at 0.75), A4 printable ~493pt + const payload = buildSheetPrintPayload( + fakeWorksheet(), + payloadSetup({ fitToPage: true, fitToWidth: 1 }), + 'Book.pdf', + 'S1', + ) + expect(payload.scale).toBe(1) + }) +}) diff --git a/apps/sheets/tests/row-height-import.test.ts b/apps/sheets/tests/row-height-import.test.ts index 67b6e34..6861ce3 100644 --- a/apps/sheets/tests/row-height-import.test.ts +++ b/apps/sheets/tests/row-height-import.test.ts @@ -155,29 +155,34 @@ describe('wrapAutoFitRows', () => { [], [], false, + null, range, ) expect(rows).toEqual([0, 1]) }) - it('keeps rows with a stored ht verbatim — Excel renders them as-is (#884)', () => { + it('re-fits cached-ht auto rows but keeps user-fixed and spacer rows (prod_054)', () => { const rows = wrapAutoFitRows( [ { row: 0, column: 0, value: 'wrapping text', styleIndex: 0 }, { row: 1, column: 0, value: 'wrapping text', styleIndex: 0 }, + { row: 2, column: 0, value: 'wrapping text', styleIndex: 0 }, ] as never, styles, - // Both an auto-fit cache (no customHeight) and a user-fixed height - // count: Excel never re-measures a stored ht on open. + // A cached ht without customHeight stays in auto mode — Excel re-fits + // it on open (live probe: ht="30" rows reopen at 16pt). customHeight + // and sub-default spacer heights stay verbatim. [ - { row: 0, height: 38.25, hidden: false }, + { row: 0, height: 30, hidden: false }, { row: 1, height: 56, customHeight: true, hidden: false }, + { row: 2, height: 6, hidden: false }, ] as never, [], false, + 15, range, ) - expect(rows).toEqual([]) + expect(rows).toEqual([0]) }) it('selects nothing when sheetFormatPr customHeight fixes the default (prod_012)', () => { @@ -187,6 +192,7 @@ describe('wrapAutoFitRows', () => { [], [], true, + null, range, ) expect(rows).toEqual([]) @@ -203,6 +209,7 @@ describe('wrapAutoFitRows', () => { [], [], false, + null, range, ) expect(rows).toEqual([]) @@ -218,6 +225,7 @@ describe('wrapAutoFitRows', () => { [], [], false, + null, range, ) expect(rows).toEqual([]) @@ -238,6 +246,7 @@ describe('wrapAutoFitRows', () => { ] as never, [{ startColumn: 2, endColumn: 5, hidden: false, styleIndex: 0 }] as never, false, + null, range, ) expect(rows).toEqual([0, 1]) @@ -253,6 +262,7 @@ describe('wrapAutoFitRows', () => { { startColumn: 3, endColumn: 4, hidden: false, styleIndex: 1 }, ] as never, false, + null, range, ) expect(rows).toEqual([]) diff --git a/apps/sheets/tests/univer-data-validation.test.ts b/apps/sheets/tests/univer-data-validation.test.ts index c482b7d..8e85b8d 100644 --- a/apps/sheets/tests/univer-data-validation.test.ts +++ b/apps/sheets/tests/univer-data-validation.test.ts @@ -1,7 +1,13 @@ import { DataValidationRenderMode } from '@univerjs/core' import { describe, expect, it, vi } from 'vitest' -import { installPopulatedDataValidationArrow } from '../src/renderer/data-validation-arrow' +import { + dataValidationInputMessageHeight, + getDataValidationInputMessage, + installActiveCellDataValidationChrome, + openActiveDataValidationDropdown, + shouldShowDataValidationDropdown, +} from '../src/renderer/data-validation-dropdown' import { toUniverDvRule } from '../src/renderer/univer-sync' const listRule = { @@ -24,6 +30,16 @@ describe('toUniverDvRule', () => { }) }) + it('preserves an OOXML request to suppress the dropdown', () => { + expect( + toUniverDvRule({ ...listRule, suppressDropdown: true }, 'file-dv-sheet-1-0'), + ).toMatchObject({ + type: 'list', + showDropDown: false, + renderMode: DataValidationRenderMode.TEXT, + }) + }) + it('drops a list rule formula2 — Univer reads it as per-item chip colors', () => { // LibreOffice writes junk formula2 ("0") on list validations; passing it // through painted validated cells black. @@ -45,38 +61,287 @@ describe('toUniverDvRule', () => { }) }) -describe('installPopulatedDataValidationArrow', () => { - it('overlays an arrow only for populated cells without repainting their value', () => { - const drawWith = vi.fn((..._args: unknown[]) => undefined) - const isHit = vi.fn((..._args: unknown[]) => true) - const rule = { renderMode: DataValidationRenderMode.TEXT } - const validator = { - canvasRender: { - drawWith, - isHit, - _dataValidationModel: { getRuleByLocation: () => rule }, +function createDropdownHarness() { + const active = { unitId: 'book-1', sheetId: 'sheet-1', row: 17, col: 4 } + const validation = { + present: true, + criteria: 'list', + showDropDown: true, + showInputMessage: false, + promptTitle: 'Type', + prompt: 'Return or Exchange', + merged: false, + } + const handlers = new Map void>() + const eventDisposes = new Map>() + const floatingDisposes: Array> = [] + const range = { + getRow: () => active.row, + getLastRow: () => active.row + (validation.merged ? 1 : 0), + getDataValidation: () => + validation.present + ? { + rule: { + showDropDown: validation.showDropDown, + showInputMessage: validation.showInputMessage, + promptTitle: validation.promptTitle, + prompt: validation.prompt, + }, + getCriteriaType: () => validation.criteria, + } + : null, + } + const addFloatDomToRange = vi.fn( + (_range: unknown, _layer: unknown, _layout: unknown, _id: string) => { + const dispose = vi.fn() + floatingDisposes.push(dispose) + return { dispose } + }, + ) + const worksheet = { + getSheetId: () => active.sheetId, + getRange: () => range, + getCellMergeData: () => (validation.merged ? range : null), + getRowHeight: () => 20, + addFloatDomToRange, + } + const executeCommand = vi.fn() + const componentDisposes: Array> = [] + const runtime = { + univerAPI: { + Event: { + SelectionChanged: 'selection-changed', + ActiveSheetChanged: 'active-sheet-changed', + CommandExecuted: 'command-executed', }, - } - const runtime = { - univer: { - __getInjector: () => ({ - get: () => ({ getValidatorItem: () => validator }), + getActiveWorkbook: () => ({ + getId: () => active.unitId, + getActiveSheet: () => worksheet, + getActiveCell: () => ({ + getRow: () => active.row, + getColumn: () => active.col, }), - }, - } - const location = { unitId: 'book-1', subUnitId: 'sheet-1', row: 17, col: 4 } - const populated = { ...location, data: { v: 'Return' } } - const empty = { ...location, data: { v: null } } + }), + addEvent: vi.fn((event: string, handler: (event?: { id: string }) => void) => { + const dispose = vi.fn() + handlers.set(event, handler) + eventDisposes.set(event, dispose) + return { dispose } + }), + registerComponent: vi.fn(() => { + const dispose = vi.fn() + componentDisposes.push(dispose) + return { dispose } + }), + executeCommand, + }, + } + return { + active, + validation, + handlers, + eventDisposes, + floatingDisposes, + range, + addFloatDomToRange, + executeCommand, + componentDisposes, + runtime, + } +} - const disposable = installPopulatedDataValidationArrow(runtime as never) - validator.canvasRender.drawWith({}, populated) - validator.canvasRender.drawWith({}, empty) - expect(drawWith).toHaveBeenCalledTimes(1) - expect(drawWith.mock.calls[0]?.[1]).toMatchObject({ data: { v: '' } }) - expect(validator.canvasRender.isHit({}, populated)).toBe(true) - expect(validator.canvasRender.isHit({}, empty)).toBe(false) - expect(rule.renderMode).toBe(DataValidationRenderMode.TEXT) +describe('active-cell data-validation chrome', () => { + it('shows only list rules whose dropdown is not suppressed', () => { + expect( + shouldShowDataValidationDropdown({ + rule: { showDropDown: true }, + getCriteriaType: () => 'list', + }), + ).toBe(true) + expect( + shouldShowDataValidationDropdown({ + rule: { showDropDown: false }, + getCriteriaType: () => 'list', + }), + ).toBe(false) + expect( + shouldShowDataValidationDropdown({ + rule: { showDropDown: true }, + getCriteriaType: () => 'whole', + }), + ).toBe(false) + expect(shouldShowDataValidationDropdown(null)).toBe(false) + }) + + it('shows and sizes only enabled non-empty input messages', () => { + const message = getDataValidationInputMessage({ + rule: { + showInputMessage: true, + promptTitle: 'Type', + prompt: 'Return or Exchange', + }, + getCriteriaType: () => 'list', + }) + expect(message).toEqual({ title: 'Type', prompt: 'Return or Exchange' }) + expect(dataValidationInputMessageHeight(message!)).toBe(40) + expect( + dataValidationInputMessageHeight({ + title: 'Type', + prompt: 'A long validation message that wraps across several display lines', + }), + ).toBeGreaterThan(40) + expect( + getDataValidationInputMessage({ + rule: { showInputMessage: false, prompt: 'Hidden' }, + getCriteriaType: () => 'list', + }), + ).toBeNull() + expect( + getDataValidationInputMessage({ + rule: { showInputMessage: true }, + getCriteriaType: () => 'list', + }), + ).toBeNull() + }) + + it('anchors one button outside the active validation cell', () => { + const harness = createDropdownHarness() + const disposable = installActiveCellDataValidationChrome(harness.runtime as never) + + expect(harness.addFloatDomToRange).toHaveBeenCalledTimes(1) + expect(harness.addFloatDomToRange.mock.calls[0]?.[2]).toEqual({ + width: 14, + height: 14, + marginX: '100%', + marginY: 0, + verticalOffsetAlign: 'bottom', + }) + harness.handlers.get('selection-changed')?.() + expect(harness.addFloatDomToRange).toHaveBeenCalledTimes(1) disposable.dispose() }) + + it('anchors the input message from the cell midpoint below the cell', () => { + const harness = createDropdownHarness() + harness.validation.showInputMessage = true + const disposable = installActiveCellDataValidationChrome(harness.runtime as never) + + expect(harness.addFloatDomToRange).toHaveBeenCalledTimes(2) + expect(harness.addFloatDomToRange.mock.calls[1]?.[2]).toEqual({ + width: 108, + height: 40, + marginX: '50%', + marginY: '100%', + }) + + disposable.dispose() + }) + + it('shows an input message for non-list validation without a dropdown', () => { + const harness = createDropdownHarness() + harness.validation.criteria = 'whole' + harness.validation.showInputMessage = true + const disposable = installActiveCellDataValidationChrome(harness.runtime as never) + + expect(harness.addFloatDomToRange).toHaveBeenCalledTimes(1) + expect(harness.addFloatDomToRange.mock.calls[0]?.[2]).toMatchObject({ + marginX: '50%', + marginY: '100%', + }) + + disposable.dispose() + }) + + it('anchors the button to the edge of a merged validation cell', () => { + const harness = createDropdownHarness() + harness.validation.merged = true + const disposable = installActiveCellDataValidationChrome(harness.runtime as never) + + expect(harness.addFloatDomToRange.mock.calls[0]?.[2]).toEqual({ + width: 14, + height: 14, + marginX: '100%', + marginY: 0, + verticalOffsetAlign: 'bottom', + }) + + disposable.dispose() + }) + + it('moves with selection and disappears outside the validation range', () => { + const harness = createDropdownHarness() + const disposable = installActiveCellDataValidationChrome(harness.runtime as never) + + harness.active.row = 18 + harness.handlers.get('selection-changed')?.() + expect(harness.floatingDisposes[0]).toHaveBeenCalledTimes(1) + expect(harness.addFloatDomToRange).toHaveBeenCalledTimes(2) + + harness.validation.present = false + harness.handlers.get('selection-changed')?.() + expect(harness.floatingDisposes[1]).toHaveBeenCalledTimes(1) + expect(harness.addFloatDomToRange).toHaveBeenCalledTimes(2) + + disposable.dispose() + }) + + it('reacts when a validation rule arrives after workbook streaming', () => { + const harness = createDropdownHarness() + harness.validation.present = false + const disposable = installActiveCellDataValidationChrome(harness.runtime as never) + expect(harness.addFloatDomToRange).not.toHaveBeenCalled() + + harness.validation.present = true + harness.handlers.get('command-executed')?.({ id: 'other.command' }) + expect(harness.addFloatDomToRange).not.toHaveBeenCalled() + harness.handlers.get('command-executed')?.({ id: 'data-validation.mutation.addRule' }) + expect(harness.addFloatDomToRange).toHaveBeenCalledTimes(1) + + disposable.dispose() + }) + + it('opens the dropdown for the current active cell', () => { + const harness = createDropdownHarness() + const preventDefault = vi.fn() + const stopPropagation = vi.fn() + + openActiveDataValidationDropdown( + harness.runtime as never, + { + preventDefault, + stopPropagation, + } as never, + ) + + expect(preventDefault).toHaveBeenCalledTimes(1) + expect(stopPropagation).toHaveBeenCalledTimes(1) + expect(harness.executeCommand).toHaveBeenCalledWith( + 'sheet.operation.show-data-validation-dropdown', + { + unitId: 'book-1', + subUnitId: 'sheet-1', + row: 17, + column: 4, + }, + ) + + harness.validation.showDropDown = false + openActiveDataValidationDropdown(harness.runtime as never) + expect(harness.executeCommand).toHaveBeenCalledTimes(1) + }) + + it('disposes the floating control, events, and component', () => { + const harness = createDropdownHarness() + const disposable = installActiveCellDataValidationChrome(harness.runtime as never) + disposable.dispose() + + expect(harness.floatingDisposes[0]).toHaveBeenCalledTimes(1) + expect(harness.componentDisposes).toHaveLength(2) + expect(harness.componentDisposes[0]).toHaveBeenCalledTimes(1) + expect(harness.componentDisposes[1]).toHaveBeenCalledTimes(1) + expect(harness.eventDisposes.get('selection-changed')).toHaveBeenCalledTimes(1) + expect(harness.eventDisposes.get('active-sheet-changed')).toHaveBeenCalledTimes(1) + expect(harness.eventDisposes.get('command-executed')).toHaveBeenCalledTimes(1) + }) }) diff --git a/apps/sheets/tests/view-transform.test.ts b/apps/sheets/tests/view-transform.test.ts index ff30cb9..6b32255 100644 --- a/apps/sheets/tests/view-transform.test.ts +++ b/apps/sheets/tests/view-transform.test.ts @@ -120,6 +120,7 @@ describe('mapRangeResultToScreen', () => { rowBreaks: [], colBreaks: [], protectedRanges: [], + pageSetup: null, indexedThroughRow: null, indexingComplete: true, }) diff --git a/apps/sheets/tests/xlsx-richdata-passthrough.test.ts b/apps/sheets/tests/xlsx-richdata-passthrough.test.ts new file mode 100644 index 0000000..bf3285c --- /dev/null +++ b/apps/sheets/tests/xlsx-richdata-passthrough.test.ts @@ -0,0 +1,101 @@ +import JSZip from 'jszip' +import { describe, expect, it } from 'vitest' + +import { + applyCellEditsToXlsx, + assertOnlyTouchedEntriesChanged, + type CellEdit, +} from '../src/gateway/xlsx-gateway' + +const contentTypes = ` + + + + + + + + + + +` + +const worksheet = ` + + #VALUE! +` + +/// Minimal modern-Excel "picture in cell" package: the cell carries vm= and a +/// cached #VALUE!, the picture lives behind xl/metadata.xml → xl/richData. +async function buildRichDataFixture(): Promise { + const zip = new JSZip() + zip.file('[Content_Types].xml', contentTypes) + zip.file( + '_rels/.rels', + ` +`, + ) + zip.file( + 'xl/workbook.xml', + ` +`, + ) + zip.file( + 'xl/_rels/workbook.xml.rels', + ` +`, + ) + zip.file('xl/worksheets/sheet1.xml', worksheet) + zip.file( + 'xl/metadata.xml', + ``, + ) + zip.file( + 'xl/richData/richValueRel.xml', + ``, + ) + zip.file( + 'xl/richData/_rels/richValueRel.xml.rels', + ``, + ) + zip.file( + 'xl/richData/rdrichvalue.xml', + `05`, + ) + zip.file( + 'xl/richData/rdrichvaluestructure.xml', + ``, + ) + zip.file('xl/media/image1.png', Buffer.from('fake-png-bytes')) + return zip.generateAsync({ type: 'nodebuffer', compression: 'DEFLATE' }) +} + +describe('richData passthrough on save', () => { + it('keeps the metadata, richData, and media parts byte-identical', async () => { + const edit: CellEdit = { + sheetName: 'Sheet1', + row: 0, + column: 0, + writeValue: true, + cell: { value: 'hello' }, + } + const mutation = await applyCellEditsToXlsx(await buildRichDataFixture(), [edit]) + expect(() => assertOnlyTouchedEntriesChanged(mutation)).not.toThrow() + const before = new Map(mutation.beforeEntries.map((entry) => [entry.path, entry.sha256])) + const after = new Map(mutation.afterEntries.map((entry) => [entry.path, entry.sha256])) + for (const part of [ + 'xl/metadata.xml', + 'xl/richData/richValueRel.xml', + 'xl/richData/_rels/richValueRel.xml.rels', + 'xl/richData/rdrichvalue.xml', + 'xl/richData/rdrichvaluestructure.xml', + 'xl/media/image1.png', + ]) { + expect(after.get(part), part).toBe(before.get(part)) + } + // The untouched picture cell keeps its vm= marker and cached error. + const saved = await JSZip.loadAsync(mutation.buffer) + const sheet = await saved.file('xl/worksheets/sheet1.xml')?.async('string') + expect(sheet).toContain('#VALUE!') + }) +}) diff --git a/apps/shell/electron-builder.cjs b/apps/shell/electron-builder.cjs index 60263ac..44eddac 100644 --- a/apps/shell/electron-builder.cjs +++ b/apps/shell/electron-builder.cjs @@ -19,15 +19,37 @@ * into the packaged app's package.json via extraMetadata and read back by * src/main/analytics.ts. When either is unset — every source/fork build — * nothing is injected and the app runs with analytics fully disabled. + * + * GENOFFICE_FONT_CDN_URL — base URL for the curated downloadable-font catalog. + * Official release jobs inject it through extraMetadata so the endpoint stays + * out of source. Without it, font download prompts/catalog entries are hidden; + * users can still install local font files. */ const { execFileSync } = require('node:child_process') const { existsSync, rmSync } = require('node:fs') const { join } = require('node:path') +function normalizeHttpsBaseUrl(name, value) { + if (!value || !value.trim()) return null + try { + const url = new URL(value.trim()) + if (url.protocol !== 'https:' || url.username || url.password || url.search || url.hash) { + throw new Error('invalid') + } + return `${url.origin}${url.pathname.replace(/\/+$/, '')}` + } catch { + throw new Error(`${name} must be an HTTPS base URL without credentials, query, or fragment`) + } +} + const updateUrl = process.env.GENOFFICE_UPDATE_URL const ga4MeasurementId = process.env.GENOFFICE_GA4_MEASUREMENT_ID const ga4ApiSecret = process.env.GENOFFICE_GA4_API_SECRET +const fontCdnUrl = normalizeHttpsBaseUrl( + 'GENOFFICE_FONT_CDN_URL', + process.env.GENOFFICE_FONT_CDN_URL, +) // GENOFFICE_MAC_X64=1 — opt into packaging the Intel (x64) dmg/zip alongside // arm64. Off by default: Intel packages must only ever ship signed with the @@ -469,15 +491,16 @@ if (updateUrl) { ] } -// CI's "-c.extraMetadata.version=..." CLI override deep-merges with this -// block, so both survive together in the packaged package.json. +// CI's "-c.extraMetadata.version=..." CLI override deep-merges with this block, +// so the version and all injected feature settings survive together. +const extraMetadata = {} if (ga4MeasurementId && ga4ApiSecret) { - config.extraMetadata = { - genofficeAnalytics: { - measurementId: ga4MeasurementId, - apiSecret: ga4ApiSecret, - }, + extraMetadata.genofficeAnalytics = { + measurementId: ga4MeasurementId, + apiSecret: ga4ApiSecret, } } +if (fontCdnUrl) extraMetadata.genofficeFontCdn = { baseUrl: fontCdnUrl } +if (Object.keys(extraMetadata).length) config.extraMetadata = extraMetadata module.exports = config diff --git a/apps/shell/src/main/analytics.ts b/apps/shell/src/main/analytics.ts index 505cc4a..6446684 100644 --- a/apps/shell/src/main/analytics.ts +++ b/apps/shell/src/main/analytics.ts @@ -1,5 +1,5 @@ import { randomUUID } from 'node:crypto' -import { readAppSettings, writeAppSetting } from './app-settings' +import { readAppSettings, writeAppSetting, writeAppSettings } from './app-settings' /** * Anonymous usage analytics via the GA4 Measurement Protocol. @@ -23,6 +23,15 @@ import { readAppSettings, writeAppSetting } from './app-settings' export const ANALYTICS_ENABLED_KEY = 'analyticsEnabled' /** app-settings.json key: random UUID identifying this install anonymously */ export const ANALYTICS_CLIENT_ID_KEY = 'analyticsClientId' +/** true while a newly identified install still needs its cohort-start event */ +export const ANALYTICS_FIRST_LAUNCH_PENDING_KEY = 'analyticsFirstLaunchPending' +/** Custom event used as the inclusion criterion for install-retention cohorts. */ +export const INSTALL_FIRST_LAUNCH_EVENT = 'install_first_launch' + +export interface AnalyticsClientState { + clientId: string + firstLaunchPending: boolean +} export interface AnalyticsKeys { measurementId: string @@ -68,16 +77,36 @@ export function analyticsEnabledFrom(settings: Record): boolean * Read the persistent anonymous install id, creating and saving one on first * use. Storage failures fall back to a per-session id rather than blocking. */ -export function ensureAnalyticsClientId(settingsPath: string): string { - const existing = readAppSettings(settingsPath)[ANALYTICS_CLIENT_ID_KEY] - if (typeof existing === 'string' && existing.trim()) return existing +export function ensureAnalyticsClientState(settingsPath: string): AnalyticsClientState { + const settings = readAppSettings(settingsPath) + const existing = settings[ANALYTICS_CLIENT_ID_KEY] + if (typeof existing === 'string' && existing.trim()) { + return { + clientId: existing, + // Missing means this id predates retention tracking; do not misclassify + // every existing install as newly acquired when the feature ships. + firstLaunchPending: settings[ANALYTICS_FIRST_LAUNCH_PENDING_KEY] === true, + } + } + const created = randomUUID() try { - writeAppSetting(settingsPath, ANALYTICS_CLIENT_ID_KEY, created) + writeAppSettings(settingsPath, { + [ANALYTICS_CLIENT_ID_KEY]: created, + [ANALYTICS_FIRST_LAUNCH_PENDING_KEY]: true, + }) } catch { // unwritable settings file: report under a session-scoped id instead } - return created + return { clientId: created, firstLaunchPending: true } +} + +export function ensureAnalyticsClientId(settingsPath: string): string { + return ensureAnalyticsClientState(settingsPath).clientId +} + +export function markAnalyticsFirstLaunchSent(settingsPath: string): void { + writeAppSetting(settingsPath, ANALYTICS_FIRST_LAUNCH_PENDING_KEY, false) } /** GA4 event name rules: letters/digits/underscore, must start with a letter */ @@ -103,6 +132,10 @@ export interface AnalyticsOptions { getClientId: () => string /** re-checked per event so the Settings toggle applies immediately */ isEnabled: () => boolean + /** true only for a newly created anonymous install id */ + shouldTrackFirstLaunch?: () => boolean + /** persists successful delivery so failed sends can retry on a later launch */ + onFirstLaunchSent?: () => void /** attached to every event; evaluated per event so live-changeable values * (UI language) stay fresh */ baseParams?: () => Record @@ -129,6 +162,8 @@ export function createAnalytics(options: AnalyticsOptions): Analytics { keys, getClientId, isEnabled, + shouldTrackFirstLaunch = () => false, + onFirstLaunchSent = () => {}, baseParams = () => ({}), getCountryCode = () => null, fetchFn = fetch, @@ -142,6 +177,8 @@ export function createAnalytics(options: AnalyticsOptions): Analytics { // one session per process run; GA4 uses it to group events and count // engaged sessions (together with engagement_time_msec) const sessionId = Date.now().toString() + let firstLaunchPending: boolean | null = null + let firstLaunchInFlight = false return { active: true, @@ -150,27 +187,60 @@ export function createAnalytics(options: AnalyticsOptions): Analytics { try { if (!isEnabled()) return const countryCode = readCountryCode(getCountryCode) + const commonParams = { + session_id: sessionId, + engagement_time_msec: 100, + ...clampParams(baseParams()), + } + const includeFirstLaunch = + !firstLaunchInFlight && (firstLaunchPending ??= shouldTrackFirstLaunch()) const body = JSON.stringify({ client_id: getClientId(), ...(countryCode ? { user_location: { country_id: countryCode } } : {}), events: [ + ...(includeFirstLaunch + ? [ + { + name: INSTALL_FIRST_LAUNCH_EVENT, + params: commonParams, + }, + ] + : []), { name, params: { - session_id: sessionId, - engagement_time_msec: 100, - ...clampParams(baseParams()), + ...commonParams, ...clampParams(params), }, }, ], }) - void fetchFn(endpoint, { + const request = fetchFn(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body, signal: AbortSignal.timeout(SEND_TIMEOUT_MS), - }).catch(() => {}) + }) + if (includeFirstLaunch) { + firstLaunchInFlight = true + void request.then( + (response) => { + firstLaunchInFlight = false + if (!response.ok) return + firstLaunchPending = false + try { + onFirstLaunchSent() + } catch { + // A later process retries if persisting the receipt fails. + } + }, + () => { + firstLaunchInFlight = false + }, + ) + } else { + void request.catch(() => {}) + } } catch { // analytics must never surface an error into the app } diff --git a/apps/shell/src/main/index.ts b/apps/shell/src/main/index.ts index 642ab35..d9a52b2 100644 --- a/apps/shell/src/main/index.ts +++ b/apps/shell/src/main/index.ts @@ -53,8 +53,9 @@ import { ANALYTICS_ENABLED_KEY, analyticsEnabledFrom, createAnalytics, - ensureAnalyticsClientId, + ensureAnalyticsClientState, extractPackagedAnalyticsKeys, + markAnalyticsFirstLaunchSent, } from './analytics' import type { Analytics, AnalyticsKeys } from './analytics' import { @@ -362,10 +363,14 @@ function persistAnalyticsPreference(enabled: boolean): boolean { function initAnalytics(): void { try { + let clientState: ReturnType | null = null + const getClientState = () => (clientState ??= ensureAnalyticsClientState(APP_SETTINGS_PATH())) analytics = createAnalytics({ keys: resolveAnalyticsKeys(), - getClientId: () => ensureAnalyticsClientId(APP_SETTINGS_PATH()), + getClientId: () => getClientState().clientId, isEnabled: analyticsEnabled, + shouldTrackFirstLaunch: () => getClientState().firstLaunchPending, + onFirstLaunchSent: () => markAnalyticsFirstLaunchSent(APP_SETTINGS_PATH()), // Country-only approximation from OS regional settings. This avoids an // IP lookup while populating GA4's built-in Country dimension. getCountryCode: () => app.getLocaleCountryCode(), diff --git a/apps/shell/src/renderer/src/Home.tsx b/apps/shell/src/renderer/src/Home.tsx index a07807b..c6872ab 100644 --- a/apps/shell/src/renderer/src/Home.tsx +++ b/apps/shell/src/renderer/src/Home.tsx @@ -1440,7 +1440,12 @@ export function Home() { const moveFileTo = async (filePath: string, targetProjectId: string) => { setMoveFileMenu(null) setRowMenu(null) - await window.aiOfficeProject?.moveFile(filePath, targetProjectId) + try { + await window.aiOfficeProject?.moveFile(filePath, targetProjectId) + } catch (error) { + window.alert(error instanceof Error ? error.message : String(error)) + return + } refresh() if (selectedProjectId) { setProjectFileEntries((prev) => prev.filter((e) => e.path !== filePath)) @@ -1454,10 +1459,17 @@ export function Home() { // re-selected or re-moved while the sequential IPC loop is in flight const moved = new Set(paths) setProjectFileEntries((prev) => prev.filter((e) => !moved.has(e.path))) - for (const path of paths) { - await window.aiOfficeProject?.moveFile(path, targetProjectId) + try { + for (const path of paths) { + await window.aiOfficeProject?.moveFile(path, targetProjectId) + } + } catch (error) { + window.alert(error instanceof Error ? error.message : String(error)) + } finally { + // A bulk move can fail after earlier paths succeeded; reload to restore + // unmoved rows while keeping successfully moved rows out of this project. + refresh() } - refresh() } // ── New file (passes projectId when a project is selected) ── diff --git a/apps/shell/tests/analytics.test.ts b/apps/shell/tests/analytics.test.ts index f043707..e53de2c 100644 --- a/apps/shell/tests/analytics.test.ts +++ b/apps/shell/tests/analytics.test.ts @@ -5,12 +5,16 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { ANALYTICS_CLIENT_ID_KEY, ANALYTICS_ENABLED_KEY, + ANALYTICS_FIRST_LAUNCH_PENDING_KEY, + INSTALL_FIRST_LAUNCH_EVENT, analyticsEnabledFrom, createAnalytics, ensureAnalyticsClientId, + ensureAnalyticsClientState, extractAnalyticsKeys, extractPackagedAnalyticsKeys, isValidEventName, + markAnalyticsFirstLaunchSent, } from '../src/main/analytics' /** @@ -115,6 +119,30 @@ describe('ensureAnalyticsClientId', () => { const saved = JSON.parse(readFileSync(settingsPath, 'utf8')) as Record expect(saved.language).toBe('ja') }) + + it('keeps a new install pending until its cohort-start event is delivered', () => { + const created = ensureAnalyticsClientState(settingsPath) + expect(created.clientId).toMatch(/^[0-9a-f-]{36}$/) + expect(created.firstLaunchPending).toBe(true) + expect(JSON.parse(readFileSync(settingsPath, 'utf8'))[ANALYTICS_FIRST_LAUNCH_PENDING_KEY]).toBe( + true, + ) + expect(ensureAnalyticsClientState(settingsPath)).toEqual(created) + + markAnalyticsFirstLaunchSent(settingsPath) + expect(ensureAnalyticsClientState(settingsPath)).toEqual({ + clientId: created.clientId, + firstLaunchPending: false, + }) + }) + + it('does not classify an id created before retention tracking as new', () => { + writeFileSync(settingsPath, JSON.stringify({ [ANALYTICS_CLIENT_ID_KEY]: 'legacy-install-id' })) + expect(ensureAnalyticsClientState(settingsPath)).toEqual({ + clientId: 'legacy-install-id', + firstLaunchPending: false, + }) + }) }) describe('isValidEventName', () => { @@ -136,16 +164,19 @@ describe('createAnalytics', () => { it('is a strict no-op without keys', () => { const fetchFn = okFetch() const getClientId = vi.fn(() => 'c') + const shouldTrackFirstLaunch = vi.fn(() => true) const analytics = createAnalytics({ keys: null, getClientId, isEnabled: () => true, + shouldTrackFirstLaunch, fetchFn, }) expect(analytics.active).toBe(false) analytics.track('app_launch') expect(fetchFn).not.toHaveBeenCalled() expect(getClientId).not.toHaveBeenCalled() + expect(shouldTrackFirstLaunch).not.toHaveBeenCalled() }) it('sends a Measurement Protocol payload with keys present', () => { @@ -201,34 +232,103 @@ describe('createAnalytics', () => { expect(payload.events).toHaveLength(1) }) + it('prepends one cohort-start event for a newly identified install', async () => { + const fetchFn = okFetch() + const shouldTrackFirstLaunch = vi.fn(() => true) + const onFirstLaunchSent = vi.fn() + const analytics = createAnalytics({ + keys: KEYS, + getClientId: () => 'new-install-id', + isEnabled: () => true, + shouldTrackFirstLaunch, + onFirstLaunchSent, + baseParams: () => ({ app_version: '1.0.0' }), + fetchFn, + }) + + analytics.track('app_launch') + const firstPayload = JSON.parse((fetchFn.mock.calls[0][1] as RequestInit).body as string) as { + events: Array<{ name: string; params: Record }> + } + expect(firstPayload.events.map((event) => event.name)).toEqual([ + INSTALL_FIRST_LAUNCH_EVENT, + 'app_launch', + ]) + expect(firstPayload.events[0].params.app_version).toBe('1.0.0') + expect(firstPayload.events[0].params.session_id).toMatch(/^\d+$/) + await vi.waitFor(() => expect(onFirstLaunchSent).toHaveBeenCalledTimes(1)) + + analytics.track('file_open', { ext: 'docx' }) + const laterPayload = JSON.parse((fetchFn.mock.calls[1][1] as RequestInit).body as string) as { + events: Array<{ name: string }> + } + expect(laterPayload.events.map((event) => event.name)).toEqual(['file_open']) + expect(shouldTrackFirstLaunch).toHaveBeenCalledTimes(1) + }) + + it('retries the cohort-start event after a failed delivery', async () => { + const fetchFn = vi + .fn() + .mockRejectedValueOnce(new Error('offline')) + .mockResolvedValue(new Response(null, { status: 204 })) + const onFirstLaunchSent = vi.fn() + const analytics = createAnalytics({ + keys: KEYS, + getClientId: () => 'new-install-id', + isEnabled: () => true, + shouldTrackFirstLaunch: () => true, + onFirstLaunchSent, + fetchFn, + }) + + analytics.track('app_launch') + await vi.waitFor(() => expect(fetchFn).toHaveBeenCalledTimes(1)) + await Promise.resolve() + analytics.track('file_open') + + const retryPayload = JSON.parse((fetchFn.mock.calls[1][1] as RequestInit).body as string) as { + events: Array<{ name: string }> + } + expect(retryPayload.events.map((event) => event.name)).toEqual([ + INSTALL_FIRST_LAUNCH_EVENT, + 'file_open', + ]) + await vi.waitFor(() => expect(onFirstLaunchSent).toHaveBeenCalledTimes(1)) + }) + it('honors the runtime gate and stops immediately after opt-out', () => { const fetchFn = okFetch() const getClientId = vi.fn(() => 'c') const getCountryCode = vi.fn(() => 'US') + const shouldTrackFirstLaunch = vi.fn(() => false) let enabled = false const analytics = createAnalytics({ keys: KEYS, getClientId, isEnabled: () => enabled, getCountryCode, + shouldTrackFirstLaunch, fetchFn, }) analytics.track('pre_consent') expect(fetchFn).not.toHaveBeenCalled() expect(getClientId).not.toHaveBeenCalled() expect(getCountryCode).not.toHaveBeenCalled() + expect(shouldTrackFirstLaunch).not.toHaveBeenCalled() enabled = true analytics.track('post_consent') expect(fetchFn).toHaveBeenCalledTimes(1) expect(getClientId).toHaveBeenCalledTimes(1) expect(getCountryCode).toHaveBeenCalledTimes(1) + expect(shouldTrackFirstLaunch).toHaveBeenCalledTimes(1) enabled = false analytics.track('after_opt_out') expect(fetchFn).toHaveBeenCalledTimes(1) expect(getClientId).toHaveBeenCalledTimes(1) expect(getCountryCode).toHaveBeenCalledTimes(1) + expect(shouldTrackFirstLaunch).toHaveBeenCalledTimes(1) }) it('creates and persists the client id only when an eligible consented event needs it', () => { diff --git a/apps/shell/tests/privacy-doc.test.ts b/apps/shell/tests/privacy-doc.test.ts index c891c48..b68df2a 100644 --- a/apps/shell/tests/privacy-doc.test.ts +++ b/apps/shell/tests/privacy-doc.test.ts @@ -1,16 +1,18 @@ import { readFileSync } from 'node:fs' import { join } from 'node:path' import { describe, expect, it } from 'vitest' +import { INSTALL_FIRST_LAUNCH_EVENT } from '../src/main/analytics' describe('PRIVACY.md analytics disclosure', () => { it('lists every event emitted by the shell main process', () => { const source = readFileSync(join(__dirname, '../src/main/index.ts'), 'utf8') const privacy = readFileSync(join(__dirname, '../../../PRIVACY.md'), 'utf8') - const emitted = new Set( - [...source.matchAll(/\banalytics\.track\(\s*['"]([a-z][a-z0-9_]*)['"]/g)].map( + const emitted = new Set([ + ...[...source.matchAll(/\banalytics\.track\(\s*['"]([a-z][a-z0-9_]*)['"]/g)].map( (match) => match[1], ), - ) + INSTALL_FIRST_LAUNCH_EVENT, + ]) const documented = new Set( [...privacy.matchAll(/^- `([a-z][a-z0-9_]*)` —/gm)].map((match) => match[1]), ) diff --git a/apps/slides/src/main/ai-ipc.ts b/apps/slides/src/main/ai-ipc.ts index 2ed862f..0ec2dd2 100644 --- a/apps/slides/src/main/ai-ipc.ts +++ b/apps/slides/src/main/ai-ipc.ts @@ -177,6 +177,7 @@ export function registerAiIpc(): void { await streamForProvider(provider, config, system, messages, tools, maxTokens, { signal: controller.signal, onDelta: (text) => send({ requestId, type: 'delta', text }), + onReasoningDelta: (text) => send({ requestId, type: 'reasoning', text }), onToolCall: (toolCall) => send({ requestId, type: 'tool-call', toolCall }), onActivity: ping, }) diff --git a/apps/slides/src/main/font-catalog.ts b/apps/slides/src/main/font-catalog.ts new file mode 100644 index 0000000..5225aa9 --- /dev/null +++ b/apps/slides/src/main/font-catalog.ts @@ -0,0 +1,514 @@ +/** OFL font catalog for the build-injected mirror. Generated by the mirror script — do not hand-edit files/hashes. */ +export interface CatalogFile { + style: 'regular' | 'bold' | 'italic' | 'boldItalic' + file: string + sha256: string + bytes: number +} +export interface CatalogFamily { + family: string + /** Primary script, for grouping in the picker */ + script: 'latin' | 'ja' | 'ko' | 'sc' | 'tc' + files: CatalogFile[] +} + +export const FONT_CATALOG: CatalogFamily[] = [ + { + family: 'Open Sans', + script: 'latin', + files: [ + { + style: 'italic', + file: 'OpenSans-italic.ttf', + sha256: '1bc0594a83762b2c25d38915a34464b7fec6088298ca22d151757bf98d6700ec', + bytes: 127392, + }, + { + style: 'boldItalic', + file: 'OpenSans-boldItalic.ttf', + sha256: '6d3884d0d57c1b67e9c50c54eca7b69c535c114e76bd7a9cfda0258588185553', + bytes: 127156, + }, + { + style: 'regular', + file: 'OpenSans-regular.ttf', + sha256: '47ed1cfaa7ffca04a04ab850b2938fd52c09d1b44cfa3e2980878e166b0cbd87', + bytes: 122116, + }, + { + style: 'bold', + file: 'OpenSans-bold.ttf', + sha256: '75367e6da7d914eaed87883ba51b2a2b5816b5cdbb39f96ec5c81a5b032bfd5a', + bytes: 122132, + }, + ], + }, + { + family: 'Roboto', + script: 'latin', + files: [ + { + style: 'italic', + file: 'Roboto-italic.ttf', + sha256: '4d3035883c531846bd1ce2dc89f36d932e72dc114f56062c7e3a9cdd74f3d3ea', + bytes: 127988, + }, + { + style: 'boldItalic', + file: 'Roboto-boldItalic.ttf', + sha256: 'be7639306a4fc4a02ce2ae2ff59eb1335e4db4a7379dd0a384ed7cc053817789', + bytes: 128772, + }, + { + style: 'regular', + file: 'Roboto-regular.ttf', + sha256: 'dece7c71bbe61710787f0839c722c65e87355dd2eb4a823e6b5488121ff81e4b', + bytes: 123720, + }, + { + style: 'bold', + file: 'Roboto-bold.ttf', + sha256: '35757eaa996b3359022f47f164a67ee9eac04971af24c5b8fbd4b89e844ff190', + bytes: 124292, + }, + ], + }, + { + family: 'Lato', + script: 'latin', + files: [ + { + style: 'italic', + file: 'Lato-italic.ttf', + sha256: 'ae2f4abf0bf802b3710c474f05d1a34f086ef57001f5751d03e428e113f5a684', + bytes: 72936, + }, + { + style: 'boldItalic', + file: 'Lato-boldItalic.ttf', + sha256: '0c55b8d34d395c056230c7c5741244de73d0d3cf5fe01b5a0f54cb025fb942fd', + bytes: 74836, + }, + { + style: 'regular', + file: 'Lato-regular.ttf', + sha256: 'f43f1c7780d69792278f04b136c934a0298fc66f2e974bac13dd2e53adc52bde', + bytes: 72312, + }, + { + style: 'bold', + file: 'Lato-bold.ttf', + sha256: '02e8f4be57ddc47cb7882c41b60216b6085b1ceaf717514be2b88fa15396238d', + bytes: 70576, + }, + ], + }, + { + family: 'Montserrat', + script: 'latin', + files: [ + { + style: 'italic', + file: 'Montserrat-italic.ttf', + sha256: 'afb8986a68a0d5189122bd11ed236bd4087abb2b4415b5df1e2bc2579f5d1241', + bytes: 181552, + }, + { + style: 'boldItalic', + file: 'Montserrat-boldItalic.ttf', + sha256: '24ffadc3cd172e73f8205c11b3136baf8daec40b97855eb710c8c5074747eb1b', + bytes: 182608, + }, + { + style: 'regular', + file: 'Montserrat-regular.ttf', + sha256: '84f3d3275dfb159dd311c61828fd754a82ec31c377120518e2cd3e7d34394e96', + bytes: 180068, + }, + { + style: 'bold', + file: 'Montserrat-bold.ttf', + sha256: '247f052d2f5b84a5bbe7831c3b08a71ca391ff54f1dfb9be88a3d3da569e3a38', + bytes: 181028, + }, + ], + }, + { + family: 'Poppins', + script: 'latin', + files: [ + { + style: 'italic', + file: 'Poppins-italic.ttf', + sha256: 'd455310e3d1e4393723dbe5836cb27c351751123b89a74c7034ec60c861d5556', + bytes: 178112, + }, + { + style: 'boldItalic', + file: 'Poppins-boldItalic.ttf', + sha256: '733b74494c0b5856045e34eea8b899f02458ca8e0b7ebd2a887a86b64d99b0d0', + bytes: 172720, + }, + { + style: 'regular', + file: 'Poppins-regular.ttf', + sha256: 'cdedb1729acac414ed01744a11da7badb86adf13108e7bd3fa161b9323f7fe54', + bytes: 154628, + }, + { + style: 'bold', + file: 'Poppins-bold.ttf', + sha256: '1984efdda0fbe207d7ac20feac2ba7c2768c92a90094b02a206c9d58cc30ff2e', + bytes: 150292, + }, + ], + }, + { + family: 'Inter', + script: 'latin', + files: [ + { + style: 'italic', + file: 'Inter-italic.ttf', + sha256: 'ef8f755d933b9310804cc9152f1f18cb2ac460d6b05054ec913d889c9f11f79e', + bytes: 328252, + }, + { + style: 'boldItalic', + file: 'Inter-boldItalic.ttf', + sha256: 'c1811d32ac0f6eed1bbf57529f2ecdfc6c717d48a969dd864942436315a1b198', + bytes: 330132, + }, + { + style: 'regular', + file: 'Inter-regular.ttf', + sha256: '1b08e7fc267a5c7e1d614100f604b83e7e8a0be241f0f288faa2b3ac93a683ba', + bytes: 324820, + }, + { + style: 'bold', + file: 'Inter-bold.ttf', + sha256: 'b37284b5701b6b168dfc770aa1a4ac492106422fd3ba76bc7641e37434e8019c', + bytes: 326468, + }, + ], + }, + { + family: 'Source Sans 3', + script: 'latin', + files: [ + { + style: 'italic', + file: 'SourceSans3-italic.ttf', + sha256: '4c51c2a49d1bd7046376443c1577b76f251944a6366f5f7918a8377e20e50c1f', + bytes: 209376, + }, + { + style: 'boldItalic', + file: 'SourceSans3-boldItalic.ttf', + sha256: '3d685c1b7bc3660ab9d92a6e0c04cdf8605065f09c46617fe078b8f1c260d5f3', + bytes: 209356, + }, + { + style: 'regular', + file: 'SourceSans3-regular.ttf', + sha256: 'b6f2cc8d9905e97b5cd7a294367bb529814385f299fb23057060394cbd6bbffb', + bytes: 234708, + }, + { + style: 'bold', + file: 'SourceSans3-bold.ttf', + sha256: 'e53acf3eb590b2ff183288401da9f3b829703e9716bc88e0da821e3a2ce9c175', + bytes: 234640, + }, + ], + }, + { + family: 'Oswald', + script: 'latin', + files: [ + { + style: 'regular', + file: 'Oswald-regular.ttf', + sha256: 'bd6ff5a83bcdc92d7aac6dbb16e4dd2b0b0320bec6402bb81f5dd9735b500e46', + bytes: 86068, + }, + { + style: 'bold', + file: 'Oswald-bold.ttf', + sha256: 'a6ee8b91ec269a6f48631ba4ba1a3ab7e5039e5482dda8dc9672ee4620d7bd68', + bytes: 86416, + }, + ], + }, + { + family: 'Raleway', + script: 'latin', + files: [ + { + style: 'italic', + file: 'Raleway-italic.ttf', + sha256: '947194506dcf722728b84075659c60350c6a730d1ef8bbfe03acb4980e0b165a', + bytes: 136572, + }, + { + style: 'boldItalic', + file: 'Raleway-boldItalic.ttf', + sha256: '4f8f9bad6e888d63aea502d11b85db15ac6b751bacb1198fb4ca5b722b1fbc68', + bytes: 136680, + }, + { + style: 'regular', + file: 'Raleway-regular.ttf', + sha256: '943300b002516feaf028c27aceaa2f473f2e1c0229ed913652a406d43277d9ba', + bytes: 138808, + }, + { + style: 'bold', + file: 'Raleway-bold.ttf', + sha256: '6cd34aa42be80b5ae85cc8c816f241d46a6d77a3a3277a6a20962b134b567cea', + bytes: 138748, + }, + ], + }, + { + family: 'Nunito', + script: 'latin', + files: [ + { + style: 'italic', + file: 'Nunito-italic.ttf', + sha256: '49a2a2cbe1e0b58a4313217cc74fc122c8a4e5ba1539e67f2a87e0832d881281', + bytes: 128400, + }, + { + style: 'boldItalic', + file: 'Nunito-boldItalic.ttf', + sha256: '4d6c0d42a74b753d33fba2c33f117264ca8c0e3f40df7a4313419ad5e6c95eae', + bytes: 128268, + }, + { + style: 'regular', + file: 'Nunito-regular.ttf', + sha256: '9ecbdba35fa74f10a1a916c31649b802a12deb9e66c7b1fe4650afcfd4d16971', + bytes: 125528, + }, + { + style: 'bold', + file: 'Nunito-bold.ttf', + sha256: '1a025dfce6e6e03bbe31ba82277ec5ed96df8b8dd58b3df6267269490b90b1dc', + bytes: 125464, + }, + ], + }, + { + family: 'Merriweather', + script: 'latin', + files: [ + { + style: 'italic', + file: 'Merriweather-italic.ttf', + sha256: 'afff81272b7653417bb6f8bef57add055b1f9fda36f27eba5f32b40752bd08da', + bytes: 595652, + }, + { + style: 'boldItalic', + file: 'Merriweather-boldItalic.ttf', + sha256: 'e9864a6790bf712a04e20f3662b206a1535186ed3e877fd859dd5fb9682335df', + bytes: 599500, + }, + { + style: 'regular', + file: 'Merriweather-regular.ttf', + sha256: 'e029d3b71e5bae3877c73f4f2b15af28db16593d49b0aed2a328d37ff7075846', + bytes: 585976, + }, + { + style: 'bold', + file: 'Merriweather-bold.ttf', + sha256: '90b7db4460fcd1a5ca49575c2668a793f3cd6447664d312a068d690e1c01e6bc', + bytes: 586052, + }, + ], + }, + { + family: 'Playfair Display', + script: 'latin', + files: [ + { + style: 'italic', + file: 'PlayfairDisplay-italic.ttf', + sha256: '6021680663c58b344d8404c289c755aadc662a02189ffe4f5ca79378f9e97840', + bytes: 110816, + }, + { + style: 'boldItalic', + file: 'PlayfairDisplay-boldItalic.ttf', + sha256: '954e20ee10ae685d0a5a6e6ee380165ea9c8eb329c0e22a58739496714f66991', + bytes: 111128, + }, + { + style: 'regular', + file: 'PlayfairDisplay-regular.ttf', + sha256: '1609a54fa3ad1abc8d0037132f73700333241ea0b65672079998e352f151dea7', + bytes: 123240, + }, + { + style: 'bold', + file: 'PlayfairDisplay-bold.ttf', + sha256: 'adefc53e7b3d483f1fa5e85edd82b7689ca79db25a1f6786bd7949cdcfeec601', + bytes: 123536, + }, + ], + }, + { + family: 'Work Sans', + script: 'latin', + files: [ + { + style: 'italic', + file: 'WorkSans-italic.ttf', + sha256: 'd38632ddd219dea11541e99bcbcf9b0569ca6aa3fcdb942fbddbaf79cc3a6a99', + bytes: 109088, + }, + { + style: 'boldItalic', + file: 'WorkSans-boldItalic.ttf', + sha256: '2907c2e697650caf6cd7dde599e3a7471d3e8713ce5c694f838a0296f464d6d6', + bytes: 110364, + }, + { + style: 'regular', + file: 'WorkSans-regular.ttf', + sha256: 'a131e7022b34ef1950a9d6bdda16f5d4509cc1fe1af5991a4552febd2a4027e5', + bytes: 114144, + }, + { + style: 'bold', + file: 'WorkSans-bold.ttf', + sha256: 'e8398b22225a1f971df9bb12fa8fb9dd4c76e80aa4d67d45bde60ebbc6c5ad81', + bytes: 115632, + }, + ], + }, + { + family: 'Rubik', + script: 'latin', + files: [ + { + style: 'italic', + file: 'Rubik-italic.ttf', + sha256: '16fc8b9b98fdf8c100a2266321c23da5b686aac084645053ad169f0a5b9fadee', + bytes: 173148, + }, + { + style: 'boldItalic', + file: 'Rubik-boldItalic.ttf', + sha256: '5df975955c4daa64e3872facb84017eca844b82dccaf295ec5462ac66640ba7e', + bytes: 173968, + }, + { + style: 'regular', + file: 'Rubik-regular.ttf', + sha256: 'cc6a4222027f8e57ff86a50c5ba88f12326d7223bde9bf6531538633ebad3343', + bytes: 174992, + }, + { + style: 'bold', + file: 'Rubik-bold.ttf', + sha256: '2d6393d64232ea3e0eeff79165d80df61c0859213abdd638b0ec1e81d21098dc', + bytes: 175628, + }, + ], + }, + { + family: 'Noto Sans JP', + script: 'ja', + files: [ + { + style: 'regular', + file: 'NotoSansJP-regular.ttf', + sha256: '4593dfcdc70ee68852606c90d75dfc6e022feda12780a55615239c53c54ef086', + bytes: 5324144, + }, + { + style: 'bold', + file: 'NotoSansJP-bold.ttf', + sha256: 'a3910e15eea0451ffbea62e894e951ea9f7c812c8b4b5e947593b6ecc2d4b057', + bytes: 5319680, + }, + ], + }, + { + family: 'Noto Sans KR', + script: 'ko', + files: [ + { + style: 'regular', + file: 'NotoSansKR-regular.ttf', + sha256: 'c733940a7dc687142848b30a491e97138ed58dc58c4cae33c44e3ee52da411cb', + bytes: 6163256, + }, + { + style: 'bold', + file: 'NotoSansKR-bold.ttf', + sha256: '5ebb0def0fe9e7c853253eca8ec9c1066adc479f2e248533b412ed0c6a663abc', + bytes: 6159248, + }, + ], + }, + { + family: 'Noto Sans SC', + script: 'sc', + files: [ + { + style: 'regular', + file: 'NotoSansSC-regular.ttf', + sha256: '450625c8d46ab3df97b7904ded955ec2746d17ec76740cb1e91d1ba63a0f89af', + bytes: 10540644, + }, + { + style: 'bold', + file: 'NotoSansSC-bold.ttf', + sha256: '0066a522a1ac007c1d72bc4fccb114f80ff7294641c78cead9715bd14d43b9ea', + bytes: 10530408, + }, + ], + }, + { + family: 'Noto Sans TC', + script: 'tc', + files: [ + { + style: 'regular', + file: 'NotoSansTC-regular.ttf', + sha256: '619662a0583f38311e92666927e5edbfd30f2a1fbe8593685660bd11bdd46a10', + bytes: 7090820, + }, + { + style: 'bold', + file: 'NotoSansTC-bold.ttf', + sha256: '33e8464f3432fd9eba5fa6ff74f5fb9ee612cad703877bd71c36e6f167c0a7e3', + bytes: 7085600, + }, + ], + }, + { + family: 'Nanum Gothic', + script: 'ko', + files: [ + { + style: 'regular', + file: 'NanumGothic-regular.ttf', + sha256: '370dbd31aa8abd8f8c057f7d41a71d38f2e71f070fa50a8cc20fecef88a9baab', + bytes: 2052640, + }, + { + style: 'bold', + file: 'NanumGothic-bold.ttf', + sha256: 'd81d6344e36f05ff1fe42b18cc9d18feece8283ae1132e1ef890b5f421eefb4a', + bytes: 2071704, + }, + ], + }, +] diff --git a/apps/slides/src/main/font-store.ts b/apps/slides/src/main/font-store.ts new file mode 100644 index 0000000..d30c034 --- /dev/null +++ b/apps/slides/src/main/font-store.ts @@ -0,0 +1,186 @@ +/** + * Downloadable/installable font store (main process). + * + * A curated catalog of OFL-licensed families is mirrored on the GenOffice CDN + * (versioned paths, sha256-pinned). Downloads and user-installed font files both + * land in /fonts, which the FontRegistry scans as a private dir — the + * same measure-and-register pipeline as Office DFonts, so a newly installed font + * immediately drives both layout metrics and canvas drawing. + */ +import { createHash } from 'node:crypto' +import { mkdirSync, readFileSync, writeFileSync, existsSync, copyFileSync } from 'node:fs' +import { basename, join } from 'node:path' +import { app, net } from 'electron' +import type { OpenedPptx } from '@genoffice/pptx-engine' +import { familyAvailable, fontFileFamilies, setUserFontDir } from './fonts' +import { FONT_CATALOG, type CatalogFamily } from './font-catalog' + +function normalizeFontCdnBaseUrl(value: unknown): string | null { + if (typeof value !== 'string' || !value.trim()) return null + try { + const url = new URL(value.trim()) + if (url.protocol !== 'https:' || url.username || url.password || url.search || url.hash) { + return null + } + const path = url.pathname.replace(/\/+$/, '') + return `${url.origin}${path}` + } catch { + return null + } +} + +/** Read the build-injected font CDN URL from packaged app metadata. */ +export function extractFontCdnBaseUrl(pkg: unknown): string | null { + if (!pkg || typeof pkg !== 'object') return null + const raw = (pkg as Record).genofficeFontCdn + if (!raw || typeof raw !== 'object') return null + return normalizeFontCdnBaseUrl((raw as Record).baseUrl) +} + +/** + * Official packages receive the URL through electron-builder extraMetadata. + * Source/dev builds may opt in with an environment variable; without either, + * all downloadable-font UI stays disabled while local font installation works. + */ +export function fontCdnBaseUrl(): string | null { + if (!app.isPackaged) return normalizeFontCdnBaseUrl(process.env.GENOFFICE_FONT_CDN_URL) + try { + const pkg = JSON.parse(readFileSync(join(app.getAppPath(), 'package.json'), 'utf8')) as unknown + return extractFontCdnBaseUrl(pkg) + } catch { + return null + } +} + +export function fontStoreDir(): string { + return join(app.getPath('userData'), 'fonts') +} + +/** Wire the store dir into the font registry; call once at startup. */ +export function initFontStore(): void { + setUserFontDir(fontStoreDir()) +} + +const downloading = new Map>() + +export interface FontCatalogEntry { + family: string + script: CatalogFamily['script'] + installed: boolean + downloading: boolean +} + +export function listFontCatalog(): FontCatalogEntry[] { + if (!fontCdnBaseUrl()) return [] + return FONT_CATALOG.map((f) => ({ + family: f.family, + script: f.script, + installed: familyAvailable(f.family), + downloading: downloading.has(f.family), + })) +} + +async function fetchVerified(url: string, sha256: string): Promise { + const res = await net.fetch(url) + if (!res.ok) throw new Error(`download failed: HTTP ${res.status}`) + const buf = Buffer.from(await res.arrayBuffer()) + const got = createHash('sha256').update(buf).digest('hex') + if (got !== sha256) throw new Error('download failed: checksum mismatch') + return buf +} + +/** Download every style file of a catalog family into the store. Throws on any failure; + * a concurrent call for the same family joins the in-flight download. */ +export function downloadFontFamily(family: string): Promise { + const entry = FONT_CATALOG.find((f) => f.family === family) + if (!entry) return Promise.reject(new Error(`not in catalog: ${family}`)) + const baseUrl = fontCdnBaseUrl() + if (!baseUrl) return Promise.reject(new Error('font downloads are unavailable')) + const inFlight = downloading.get(family) + if (inFlight) return inFlight + const run = (async () => { + const dir = fontStoreDir() + mkdirSync(dir, { recursive: true }) + for (const file of entry.files) { + const dest = join(dir, file.file) + if (existsSync(dest)) continue + const url = new URL(encodeURIComponent(file.file), `${baseUrl}/`).toString() + const buf = await fetchVerified(url, file.sha256) + writeFileSync(dest, buf) + } + })().finally(() => downloading.delete(family)) + downloading.set(family, run) + return run +} + +const SFNT_MAGIC = new Set(['00010000', '4f54544f', '74746366', '74727565']) // sfnt / OTTO / ttcf / true + +/** + * Copy user-picked font files into the store, renamed to their primary family + * name so the filename-keyed registry index can find them. Returns the family + * names that were installed. + */ +export function installLocalFontFiles(paths: string[]): string[] { + const dir = fontStoreDir() + mkdirSync(dir, { recursive: true }) + const installed: string[] = [] + for (const p of paths) { + let head: string + try { + head = readFileSync(p).subarray(0, 4).toString('hex') + } catch { + continue + } + if (!SFNT_MAGIC.has(head)) continue + const families = fontFileFamilies(p) + const primary = families[0] + if (!primary) continue + const ext = + basename(p) + .match(/\.(ttc|otc|otf)$/i)?.[1] + ?.toLowerCase() ?? 'ttf' + // Family-derived name = registry index key; suffix keeps distinct style files apart + const styleTag = /bold\s*italic/i.test(basename(p)) + ? '-BoldItalic' + : /bold/i.test(basename(p)) + ? '-Bold' + : /italic|oblique/i.test(basename(p)) + ? '-Italic' + : '' + const dest = join(dir, `${primary.replace(/[\\/:]/g, '')}${styleTag}.${ext}`) + try { + copyFileSync(p, dest) + installed.push(...families) + } catch { + /* unreadable/locked source: skip */ + } + } + return [...new Set(installed)] +} + +/** Deck-referenced families that are missing locally but present in the catalog. */ +export function missingCatalogFonts(opened: OpenedPptx): string[] { + if (!fontCdnBaseUrl()) return [] + const wanted = new Set() + type TextLike = { paragraphs?: Array<{ runs?: Array<{ fontFamily?: string }> }> } + const collectText = (text: TextLike | undefined): void => { + for (const p of text?.paragraphs ?? []) + for (const r of p.runs ?? []) if (r.fontFamily) wanted.add(r.fontFamily) + } + const walk = (els: unknown[]): void => { + for (const el of els as Array<{ + type?: string + children?: unknown[] + text?: TextLike + rows?: Array> + }>) { + if (!el || typeof el !== 'object') continue + if (el.children) walk(el.children) + collectText(el.text) + for (const row of el.rows ?? []) for (const cell of row) collectText(cell.text) + } + } + for (const s of opened.deck.slides) walk(s.elements as unknown[]) + const inCatalog = new Set(FONT_CATALOG.map((f) => f.family)) + return [...wanted].filter((f) => inCatalog.has(f) && !familyAvailable(f)).sort() +} diff --git a/apps/slides/src/main/fonts.ts b/apps/slides/src/main/fonts.ts index 3c3b6f4..18a52eb 100644 --- a/apps/slides/src/main/fonts.ts +++ b/apps/slides/src/main/fonts.ts @@ -40,6 +40,20 @@ const BUNDLED_FONTS: Record = { 'Carlito-BoldItalic': carlitoBoldItalic, } +/** + * User font store (downloaded catalog fonts + fonts installed via the in-app entry). + * Injected by slides-main at startup (fonts.ts stays free of electron imports); invisible + * to Chromium, so faces resolved from here get the same private FontFace treatment as + * Office DFonts. + */ +let userFontDir: string | null = null +export function setUserFontDir(dir: string): void { + userFontDir = dir +} +export function getUserFontDir(): string | null { + return userFontDir +} + function fontDirs(): string[] { switch (process.platform) { case 'darwin': @@ -150,6 +164,9 @@ const ALIASES: Record = { 'lucida sans': ['Lucida Grande', 'Arial'], // —— Japanese —— 'yu gothic': [...YU_GOTHIC, ...HIRAGINO_SANS], + // Yu Gothic UI faces live inside YuGothM/YuGothB.ttc (like Meiryo UI in meiryo.ttc); + // the origKey bonus in rankFaces picks them out of the alias target's collection + 'yu gothic ui': [...YU_GOTHIC, ...HIRAGINO_SANS], 游ゴシック: [...YU_GOTHIC, ...HIRAGINO_SANS], 游ゴシック体: [...YU_GOTHIC, ...HIRAGINO_SANS], 'yu mincho': [...YU_MINCHO, ...HIRAGINO_MINCHO], @@ -483,6 +500,10 @@ class FontRegistry { } // System dirs first so same-named Office copies (arial.ttf…) resolve non-private for (const dir of fontDirs()) this.scanFlatDir(dir) + if (userFontDir) { + this.privateDirs.push(userFontDir) + this.scanFlatDir(userFontDir) + } for (const dir of officeFontDirs()) { this.privateDirs.push(dir) this.scanFlatDir(dir) @@ -842,10 +863,40 @@ export function getPrivateFontData(id: string): ArrayBuffer | null { } } +/** Process-wide registry; reset after the user font store changes so new files get indexed. */ +let sharedRegistry: FontRegistry | null = null +function getRegistry(): FontRegistry { + return (sharedRegistry ??= new FontRegistry()) +} +export function resetFontRegistry(): void { + sharedRegistry = null +} + +/** True when the family resolves without same-script/class substitution (alias hits count as available). */ +export function familyAvailable(family: string): boolean { + const hit = getRegistry().resolve({ + fontFamily: family, + fontSizePx: 100, + bold: false, + italic: false, + }) + return !!hit && !hit.substituted +} + +/** Family names (name 1/16) declared by a local font file; [] when unreadable. */ +export function fontFileFamilies(path: string): string[] { + try { + const faces = readFaceDir(readFileSync(path)) + return [...new Set(faces.map((f) => f.display).filter(Boolean))] + } catch { + return [] + } +} + /** Create a metrics provider injected with system fonts (falls back to heuristics per run when no font is found). */ export function createSystemFontMetrics(): FontMetricsProvider { initShapedMetrics() - const registry = new FontRegistry() + const registry = getRegistry() const cache = new Map< string, { font: OpentypeFontLike; family: string; substituted?: boolean } | undefined diff --git a/apps/slides/src/main/page-spec.ts b/apps/slides/src/main/page-spec.ts index c63067a..24ed0b8 100644 --- a/apps/slides/src/main/page-spec.ts +++ b/apps/slides/src/main/page-spec.ts @@ -19,8 +19,10 @@ import { promoteSlideBackground, savePptx, type Paragraph, + type TextElement, type TextRun, } from '@genoffice/pptx-engine' +import { buildRenderSlide, EMU_PER_PX_96, type FontMetricsProvider } from '@genoffice/pptx-render' import { coverCropFractions } from '../shared/cover-crop' export const SPEC_CANVAS_W = 1280 @@ -317,6 +319,47 @@ export interface BuildPageDeps { fetchImage: (url: string) => Promise<{ bytes: Uint8Array; ext: string } | null> /** Decodes natural pixel size for cover-cropping; null skips the crop */ imageDims?: (bytes: Uint8Array) => { width: number; height: number } | null + /** Font metrics for the post-build text measurement; absent skips the box-height fix */ + fontMetrics?: FontMetricsProvider +} + +/** + * The LLM sizes text boxes from a rough chars-per-line heuristic, which routinely + * undersizes big CJK titles; the box has no autofit, so the canvas draws the overflow + * past the selection frame (and PowerPoint past the shape). Re-measure every text box + * with the real layout engine — on the reopened (parsed) model, the exact input the + * landed page will render from — and grow too-short boxes to their content height. + * Grow-only, plain text boxes only (shape label boxes are design intent); middle/bottom + * anchored boxes shift up so the rendered glyphs stay exactly where they were. + * Returns the re-saved bytes, or null when every box already fits. + */ +async function growTextBoxesToContent( + bytes: Uint8Array, + metrics: FontMetricsProvider, +): Promise { + const opened = await openPptx(bytes) + const slide = opened.deck.slides[0] + if (!slide) return null + const baseWidthPx = opened.deck.size.cx / EMU_PER_PX_96 // native px → vp.scale = 1 + const rendered = buildRenderSlide(slide, opened.deck.size, { fitWidthPx: baseWidthPx, metrics }) + let changed = false + for (const node of rendered.nodes) { + if (node.type !== 'text' || !node.text) continue + const el = slide.elements.find((e) => e.id === node.sourceId) + if (el?.type !== 'text') continue + const t = node.text + const needH = Math.max(t.contentHeight, t.inkBottom ?? 0) + t.insets.t + t.insets.b + const growPx = needH - node.box.h + if (growPx < 0.5) continue + const tel = el as TextElement + const offset = { ...tel.transform.offset, cy: Math.max(1, Math.round(needH * EMU_PER_PX_96)) } + if (t.anchor === 'middle') offset.y -= Math.round((growPx / 2) * EMU_PER_PX_96) + else if (t.anchor === 'bottom') offset.y -= Math.round(growPx * EMU_PER_PX_96) + tel.transform = { ...tel.transform, offset } + tel.dirtyTransform = true + changed = true + } + return changed ? savePptx(opened) : null } function toEngineParagraphs(paragraphs: SpecParagraph[]): Paragraph[] { @@ -440,5 +483,7 @@ export async function buildPagePptx( } promoteSlideBackground(slide, opened.deck.size) - return { bytes: await savePptx(opened), imageFailures } + let bytes = await savePptx(opened) + if (deps.fontMetrics) bytes = (await growTextBoxesToContent(bytes, deps.fontMetrics)) ?? bytes + return { bytes, imageFailures } } diff --git a/apps/slides/src/main/session-state.ts b/apps/slides/src/main/session-state.ts index c3628a7..a2ada51 100644 --- a/apps/slides/src/main/session-state.ts +++ b/apps/slides/src/main/session-state.ts @@ -20,7 +20,7 @@ import { type FontMetricsProvider, type RenderSlide, } from '@genoffice/pptx-render' -import { createSystemFontMetrics } from './fonts' +import { createSystemFontMetrics, resetFontRegistry } from './fonts' import { tiffToPng } from './tiff-decode' import { neutralizeJpegOrientation } from './jpeg-orientation' import { displayMime } from './media-mime' @@ -362,6 +362,12 @@ export function getFontMetrics(): FontMetricsProvider { return fontMetrics } +/** Drop the cached metrics (and its font registry) after the user font store changes. */ +export function resetFontMetrics(): void { + resetFontRegistry() + fontMetrics = null +} + export function buildAllRenderSlides(opened: OpenedPptx, fitWidthPx: number): RenderSlide[] { return opened.deck.slides.map((s, i) => buildRenderSlide(s, opened.deck.size, { diff --git a/apps/slides/src/main/slides-main.ts b/apps/slides/src/main/slides-main.ts index d8e8230..30f8a9c 100644 --- a/apps/slides/src/main/slides-main.ts +++ b/apps/slides/src/main/slides-main.ts @@ -204,6 +204,7 @@ import { dialogParent, endHistoryBatch, getFontMetrics, + resetFontMetrics, journalOps, makeMediaResolver, pushHistory, @@ -226,6 +227,13 @@ import { } from './session-state' import { registerAiIpc, registerSlidesOnlyAiIpc } from './ai-ipc' import { listPrivateFontFaces, getPrivateFontData } from './fonts' +import { + downloadFontFamily, + initFontStore, + installLocalFontFiles, + listFontCatalog, + missingCatalogFonts, +} from './font-store' /** One slide, copied from any deck open in this process, waiting to be pasted into another. */ let slideClipboard: { bundle: SlideBundle; png?: string } | null = null @@ -362,7 +370,6 @@ function trackSlidesWebContents(wc: WebContents): void { else untitledRecovery.delete(wc.id) sessions.delete(wc.id) pendingByWc.delete(wc.id) - clipboards.delete(wc.id) lastSlidePaste.delete(wc.id) closeSaveWaiters.get(wc.id)?.(false) closeSaveWaiters.delete(wc.id) @@ -371,8 +378,8 @@ function trackSlidesWebContents(wc: WebContents): void { }) } -// ── In-app element clipboard (isolated per webContents; pasteCount drives cascading offset) ─ -const clipboards = new Map() +// ── In-app element clipboard (app-wide, so elements copied in one deck paste into any other open deck; pasteCount drives cascading offset) ─ +let elementClipboard: { items: ElementClipboardItem[]; pasteCount: number } | null = null /** Shell hook: a view opened a file (including ⌘O inside a tab) — used to update tab titles and de-duplicate paths */ let slidesOpenedHook: ((wc: WebContents, path: string) => void) | null = null @@ -1031,6 +1038,50 @@ export function registerSlidesIpc(): void { ipcMain.handle('slides:private-font-faces', () => listPrivateFontFaces()) ipcMain.handle('slides:private-font-data', (_e, id: string) => getPrivateFontData(id)) + initFontStore() + // Fonts changed (download or local install): rebuild every open session with a fresh + // registry and push the relaid-out slides + a re-sync ping to all attached windows. + const afterFontsChanged = (): void => { + resetFontMetrics() + const seen = new Set() + for (const [wcId, session] of sessions) { + if (seen.has(session)) continue + seen.add(session) + const payload = { + slides: buildAllRenderSlides(session.opened, session.fitWidthPx), + size: { cx: session.opened.deck.size.cx, cy: session.opened.deck.size.cy }, + } + for (const id of attachedIds(session)) + webContents.fromId(id)?.send('slides:deck-changed', payload) + void wcId + } + for (const wc of webContents.getAllWebContents()) wc.send('slides:fonts-changed') + } + ipcMain.handle('slides:font-catalog', () => listFontCatalog()) + ipcMain.handle('slides:font-download', async (_e, family: string) => { + try { + await downloadFontFamily(family) + afterFontsChanged() + return { ok: true } + } catch (err) { + return { ok: false, error: err instanceof Error ? err.message : String(err) } + } + }) + ipcMain.handle('slides:font-install-local', async () => { + const r = await showOpenDialogWithMemory(dialog, dialogParent(), { + properties: ['openFile', 'multiSelections'], + filters: [{ name: 'Fonts', extensions: ['ttf', 'otf', 'ttc', 'otc'] }], + }) + if (r.canceled || !r.filePaths.length) return { families: [] } + const families = installLocalFontFiles(r.filePaths) + if (families.length) afterFontsChanged() + return { families } + }) + ipcMain.handle('slides:font-missing', (e) => { + const session = sessions.get(e.sender.id) + return session ? missingCatalogFonts(session.opened) : [] + }) + // Shared shim core: one undo step wrapping a transaction; on failure the step is // erased (the executor already restored the model, so history stays consistent). // Handlers keep only surface translation and result shaping around this. @@ -1512,7 +1563,9 @@ export function registerSlidesIpc(): void { ): Promise<{ ok: boolean; marker?: string; error?: string }> => { if (!cloudSlideEnabled()) return { ok: false, error: 'cloud slide generation is disabled' } try { - // ultra = opus-class model, matching the local path's quality tier; GENOFFICE_CLOUD_SLIDE_TIER=standard opts down + // Ultra resolves to the opus-class slide model server-side; standard is the + // lighter MiniMax M3 model. Keep an explicit escape hatch for quality + // comparisons and emergency rollback. const tier = process.env.GENOFFICE_CLOUD_SLIDE_TIER === 'standard' ? 'standard' : 'ultra' const started = Date.now() const { bytes, model } = await gskSlideGenerate({ @@ -1555,6 +1608,7 @@ export function registerSlidesIpc(): void { try { const started = Date.now() const { bytes, imageFailures } = await buildPagePptx(parsed.spec, { + fontMetrics: getFontMetrics(), fetchImage: async (url) => { const resp = await fetchRemoteImage(url) if (!resp || !resp.ok) return null @@ -3074,18 +3128,20 @@ export function registerSlidesIpc(): void { }, ) - ipcMain.handle('slides:clipboard-external', () => { - // Our marker still present = the last copy came from this app -> use internal element paste - // (on macOS custom formats don't appear in availableFormats, so check via readBuffer) - const marker = (format: string) => { - try { - return clipboard.readBuffer(format).length > 0 - } catch { - return false - } + // Our marker still present = the last copy came from this app -> use internal element paste + // (on macOS custom formats don't appear in availableFormats, so check via readBuffer) + const clipboardMarker = (format: string) => { + try { + return clipboard.readBuffer(format).length > 0 + } catch { + return false } - if (slideClipboard && marker('io.genoffice.slides.slide')) return { kind: 'slide' } - if (marker('io.genoffice.slides.elements')) return { kind: 'internal' } + } + + ipcMain.handle('slides:clipboard-external', () => { + if (slideClipboard && clipboardMarker('io.genoffice.slides.slide')) return { kind: 'slide' } + if (elementClipboard && clipboardMarker('io.genoffice.slides.elements')) + return { kind: 'internal' } const img = clipboard.readImage() if (!img.isEmpty()) return { kind: 'image', base64: img.toPNG().toString('base64'), ext: 'png' } const text = clipboard.readText() @@ -3093,6 +3149,14 @@ export function registerSlidesIpc(): void { return { kind: 'none' } }) + // Menu-enable probe: is there anything a paste would act on? (no image decode) + ipcMain.handle('slides:clipboard-probe', () => { + if (slideClipboard && clipboardMarker('io.genoffice.slides.slide')) return true + if (elementClipboard && clipboardMarker('io.genoffice.slides.elements')) return true + if (clipboard.availableFormats().some((f) => f.startsWith('image/'))) return true + return clipboard.readText().trim().length > 0 + }) + ipcMain.handle('slides:copy-elements', (e, op: CopyElementsOp) => { const session = sessions.get(e.sender.id) if (!session) return 0 @@ -3103,7 +3167,7 @@ export function registerSlidesIpc(): void { .filter((el): el is NonNullable => !!el) .map((el) => copyElementData(session.opened, slide, el)) if (items.length) { - clipboards.set(e.sender.id, { items, pasteCount: 0 }) + elementClipboard = { items, pasteCount: 0 } // Write our marker to the OS clipboard: an external copy overwrites it, so at paste time it tells whether internal or external is newer clipboard.writeBuffer('io.genoffice.slides.elements', Buffer.from('1')) } @@ -3112,7 +3176,7 @@ export function registerSlidesIpc(): void { ipcMain.handle('slides:paste-elements', (e, op: PasteElementsOp) => { const session = sessions.get(e.sender.id) - const clip = clipboards.get(e.sender.id) + const clip = elementClipboard if (!session || !clip?.items.length) return null if (!session.opened.deck.slides[op.slideIndex]) return null const baseWidthPx = session.opened.deck.size.cx / EMU_PER_PX_96 diff --git a/apps/slides/src/preload/index.ts b/apps/slides/src/preload/index.ts index bf7e533..ba7b361 100644 --- a/apps/slides/src/preload/index.ts +++ b/apps/slides/src/preload/index.ts @@ -109,6 +109,15 @@ const api: SlidesApi = { setShowFullScreen: (on) => ipcRenderer.invoke('slides:show-fullscreen', on), privateFontFaces: () => ipcRenderer.invoke('slides:private-font-faces'), privateFontData: (id) => ipcRenderer.invoke('slides:private-font-data', id), + fontCatalog: () => ipcRenderer.invoke('slides:font-catalog'), + fontDownload: (family) => ipcRenderer.invoke('slides:font-download', family), + fontInstallLocal: () => ipcRenderer.invoke('slides:font-install-local'), + fontMissing: () => ipcRenderer.invoke('slides:font-missing'), + onFontsChanged: (handler) => { + const listener = () => handler() + ipcRenderer.on('slides:fonts-changed', listener) + return () => ipcRenderer.removeListener('slides:fonts-changed', listener) + }, openPptx: (fitWidthPx) => ipcRenderer.invoke('slides:open', fitWidthPx), openPptxPath: (path, fitWidthPx) => ipcRenderer.invoke('slides:open-path', path, fitWidthPx), consumePendingOpen: (fitWidthPx) => ipcRenderer.invoke('slides:consume-pending-open', fitWidthPx), @@ -216,6 +225,7 @@ const api: SlidesApi = { pasteSlide: (op: PasteSlideOp) => ipcRenderer.invoke('slides:paste-slide', op), repasteSlide: (op: RepasteSlideOp) => ipcRenderer.invoke('slides:repaste-slide', op), hasSlideClipboard: () => ipcRenderer.invoke('slides:has-slide-clipboard'), + clipboardProbe: () => ipcRenderer.invoke('slides:clipboard-probe'), deleteSlide: (slideIndex: number) => ipcRenderer.invoke('slides:delete-slide', slideIndex), reorderElement: (op: ReorderElementOp) => ipcRenderer.invoke('slides:reorder-element', op), editTableCell: (op: EditTableCellOp) => ipcRenderer.invoke('slides:edit-table-cell', op), diff --git a/apps/slides/src/renderer/App.tsx b/apps/slides/src/renderer/App.tsx index 1c7b303..b2afd86 100644 --- a/apps/slides/src/renderer/App.tsx +++ b/apps/slides/src/renderer/App.tsx @@ -64,6 +64,7 @@ import { } from './components/AiAskPopover' import { anchorId, + buildSelectionInstruction, describeNode, EDIT_QUEUE_MAX, resolveQueueItem, @@ -271,6 +272,40 @@ export function App() { if (slides.length) void syncPrivateFonts() }, [slides]) const [path, setPath] = useState(null) + // Deck references catalog fonts that are missing locally → one-click download banner + const [missingFonts, setMissingFonts] = useState([]) + const [fontBannerBusy, setFontBannerBusy] = useState(false) + const missingFontsDismissed = useRef(false) + const refreshMissingFonts = useCallback(() => { + if (missingFontsDismissed.current) return + window.slidesApi + .fontMissing?.() + .then((m) => setMissingFonts(m ?? [])) + .catch(() => {}) + }, []) + const hasSlides = slides.length > 0 + useEffect(() => { + missingFontsDismissed.current = false + if (hasSlides) refreshMissingFonts() + }, [path, hasSlides, refreshMissingFonts]) + // Font store changed (download / local install): re-register FontFaces, refresh the banner + useEffect( + () => + window.slidesApi.onFontsChanged?.(() => { + void syncPrivateFonts() + refreshMissingFonts() + }), + [refreshMissingFonts], + ) + const downloadMissingFonts = useCallback(async () => { + setFontBannerBusy(true) + try { + for (const f of missingFonts) await window.slidesApi.fontDownload?.(f) + } finally { + setFontBannerBusy(false) + refreshMissingFonts() + } + }, [missingFonts, refreshMissingFonts]) /** AiPanel reset key: incremented only on applyOpen (open/new file), not on draft path updates */ const [aiPanelKey, setAiPanelKey] = useState(0) /** Theme body default font (fallback for the font box when the selection has no text element) */ @@ -370,6 +405,13 @@ export function App() { const [images, setImages] = useState>(new Map()) const imageLoaderRef = useRef | null>(null) const [hasClipboard, setHasClipboard] = useState(false) + // The clipboard is app-wide (copies land from other windows) and external content counts too + useEffect(() => { + const probe = () => void window.slidesApi.clipboardProbe().then(setHasClipboard) + probe() + window.addEventListener('focus', probe) + return () => window.removeEventListener('focus', probe) + }, []) const [transition, setTransition] = useState('none') // ── Animations tab: current page's animation list + pane/preview ───────────── const [animations, setAnimations] = useState([]) @@ -1956,6 +1998,8 @@ export function App() { const onCanvasContextMenu = useCallback( (sourceId: string | null, x: number, y: number, cell?: { row: number; col: number }) => { if (editing) return + // The clipboard may have been filled from another window (or externally) since our last copy + void window.slidesApi.clipboardProbe().then(setHasClipboard) if (sourceId) { setSelectedIds((prev) => (prev.includes(sourceId) ? prev : [sourceId])) setCtxMenu({ kind: 'element', x, y, targetId: sourceId, ...(cell ? { cell } : {}) }) @@ -3095,6 +3139,30 @@ export function App() { )}
    + {missingFonts.length > 0 && ( +
    + + {t('fontMissingBanner')} + {missingFonts.join(', ')} + + + +
    + )}
    {!slide ? (
    {t('appStartOpening')}
    @@ -4030,9 +4098,14 @@ export function App() { : (instruction) => { askClosedAtRef.current = Date.now() setAskState(null) - // A normal run reads the live canvas selection, so the - // instruction lands scoped to the still-selected element(s) - pushAiPreset(instruction) + // Carry the popover's frozen durable targets into the run. + // The canvas keeps parse-time source ids for rendering, while + // the AI inventory and edit tools speak durable ids. + pushAiPreset( + buildSelectionInstruction(current, askTargets, instruction), + true, + instruction, + ) } } /> diff --git a/apps/slides/src/renderer/NodeBody.tsx b/apps/slides/src/renderer/NodeBody.tsx index 489d880..590c7a4 100644 --- a/apps/slides/src/renderer/NodeBody.tsx +++ b/apps/slides/src/renderer/NodeBody.tsx @@ -22,6 +22,7 @@ import type { } from '@genoffice/pptx-render' import { featheredImage, + featheredShapeCanvas, fillToKonva, processedImage, processedImageKey, @@ -575,16 +576,44 @@ export const NodeBody = React.memo(function NodeBody({ } else { const rounded = presetToShapeKind(shape.presetGeometry) === 'roundRect' || shape.cornerRadiusPx != null - geom = ( - - ) + const cornerRadius = rounded ? (shape.cornerRadiusPx ?? Math.min(box.w, box.h) * 0.167) : 0 + if ( + shape.softEdgePx && + shape.fill.kind === 'solid' && + !shape.fillOverlay && + box.w >= 1 && + box.h >= 1 + ) { + const feathered = featheredShapeCanvas( + shape.fill.color, + box.w, + box.h, + shape.softEdgePx, + cornerRadius, + shape.stroke ? { color: shape.stroke.color, widthPx: shape.stroke.widthPx } : undefined, + ) + geom = ( + + ) + } else { + geom = ( + + ) + } } // fillOverlay: PowerPoint blends the overlay against the shape's own fill in isolation. diff --git a/apps/slides/src/renderer/TextEditOverlay.tsx b/apps/slides/src/renderer/TextEditOverlay.tsx index d647b62..e2011bb 100644 --- a/apps/slides/src/renderer/TextEditOverlay.tsx +++ b/apps/slides/src/renderer/TextEditOverlay.tsx @@ -111,38 +111,6 @@ function browserFontBox( return m } -/** Where the canvas actually draws the baseline, relative to the engine's baselineY. - * The Konva adapter positions Text by top = baselineY − 0.8em, and Konva paints with - * canvas2d 'middle' semantics — so the visible baseline lands at top + emHeightAscent, - * i.e. engineBaseline + (emHeightAscent − 0.8em). ≈0 for Latin sans (the 0.8 was tuned - * for it) but several px for CJK/serif metrics; the editor must match the pixels. */ -let baselineDropCtx: CanvasRenderingContext2D | null = null -const baselineDropCache = new Map() -function konvaBaselineDrop( - family: string, - sizePx: number, - bold?: boolean, - italic?: boolean, -): number { - const key = `${family}|${Math.round(sizePx * 10)}|${bold ? 'b' : ''}${italic ? 'i' : ''}` - const hit = baselineDropCache.get(key) - if (hit !== undefined) return hit - baselineDropCtx ??= document.createElement('canvas').getContext('2d') - if (!baselineDropCtx) return 0 - const ctx = baselineDropCtx - ctx.font = `${italic ? 'italic ' : ''}${bold ? 'bold ' : ''}${sizePx}px ${family}` - // Same ink measured from both baselines: the ascent difference IS the exact distance - // from the 'middle' anchor down to the alphabetic baseline for this font (measuring - // beats modelling — Chromium derives 'middle' from per-font metrics) - ctx.textBaseline = 'alphabetic' - const a1 = ctx.measureText('Hg').actualBoundingBoxAscent - ctx.textBaseline = 'middle' - const a2 = ctx.measureText('Hg').actualBoundingBoxAscent - const drop = a1 > 0 || a2 > 0 ? sizePx / 2 + (a1 - a2) - 0.8 * sizePx : 0 - baselineDropCache.set(key, drop) - return drop -} - /** * Layout lines → the editor's initial DOM: one
    per model paragraph (data-src-para records * the source paragraph index, with paragraph alignment); wrap/word-split fragments merged back @@ -205,32 +173,20 @@ export function populateEditorDom( const indentPx = (!vertical && first.indentPx) || 0 if (indentPx && !first.runs.some((r) => r.isBullet)) p.style.textIndent = `${indentPx}px` // ── Glyph-position fidelity vs the canvas renderer ── - // Vertical: the canvas draws the dominant run's baseline at - // lineTop + engineAscent + konvaBaselineDrop (the adapter's 0.8em top approximation - // filtered through Konva's 'middle'-baseline painting); CSS puts the DOM baseline at + // Vertical: the canvas paints the baseline exactly at the engine's position, + // lineTop + leadAbove + ascent (the Konva adapter converts baselineY to a node top + // with the measured per-font 'middle'-baseline offset); CSS puts the DOM baseline at // half-leading + browser ascent. The difference is several px on CJK/serif or // lnSpc ≠ 100% text and reads as the text jumping when editing starts. Measure both // sides and cancel the difference with a relative offset (flow is unaffected). let engineAscent = 0 let domBaseline = 0 - let dominant: GlyphRun | null = null for (const r of first.runs) { const a = r.ascentPx ?? r.fontSizePx * 0.8 - if (a > engineAscent) { - engineAscent = a - dominant = r - } + if (a > engineAscent) engineAscent = a } - const drop = dominant - ? konvaBaselineDrop( - displayFontFamily(dominant.fontFamily ?? ''), - dominant.fontSizePx, - dominant.bold, - dominant.italic, - ) - : 0 // lnSpc>100%: the canvas pins glyphs to the slot bottom (leadAbove below the line top) - const canvasBaseline = (first.leadAbove ?? 0) + engineAscent + drop + const canvasBaseline = (first.leadAbove ?? 0) + engineAscent const participants: Array<{ family: string; size: number; bold?: boolean; italic?: boolean }> = first.runs.map((r) => ({ family: displayFontFamily(r.fontFamily ?? ''), diff --git a/apps/slides/src/renderer/ai/AiPanel.tsx b/apps/slides/src/renderer/ai/AiPanel.tsx index 94b689d..a280a17 100644 --- a/apps/slides/src/renderer/ai/AiPanel.tsx +++ b/apps/slides/src/renderer/ai/AiPanel.tsx @@ -913,7 +913,8 @@ export function AiPanel({ getSelectedIds: () => (queueRunResolverRef.current ? [] : selectedRef.current), applySlide: (i, updated) => applySlideRef.current(i, updated), applyDeck: (all, goTo) => applyDeckRef.current(all, goTo), - setSpeakerNotes: (i, text) => onSetSpeakerNotesRef.current?.(i, text) ?? Promise.resolve(false), + setSpeakerNotes: (i, text) => + onSetSpeakerNotesRef.current?.(i, text) ?? Promise.resolve(false), landGeneratedPages: async ( pageMarkers: string[], mode?: 'replace' | 'append' | 'insert_at', @@ -2806,6 +2807,7 @@ function ClarifyCard({ onSkip: () => void }) { const { t } = useI18n() + const decideAnswer = t('aiClarifyDecideAnswer') // Per question: set of selected options + the "Other" free text const [picked, setPicked] = useState>>({}) const [other, setOther] = useState>({}) @@ -2851,12 +2853,18 @@ function ClarifyCard({ useEffect(() => cancelAdvance, []) - const toggle = (qid: string, opt: string, multi?: boolean) => { + const toggle = (qid: string, opt: string, multi?: boolean, exclusive?: boolean) => { setPicked((prev) => { const cur = new Set(prev[qid] ?? []) if (multi) { - if (cur.has(opt)) cur.delete(opt) - else cur.add(opt) + if (exclusive) { + cur.clear() + cur.add(opt) + } else { + cur.delete(decideAnswer) + if (cur.has(opt)) cur.delete(opt) + else cur.add(opt) + } } else { cur.clear() cur.add(opt) @@ -2879,7 +2887,7 @@ function ClarifyCard({ const q = questions[qIdx] const isLast = qIdx === questions.length - 1 const selCount = picked[q.id]?.size ?? 0 - const decideAnswer = t('aiClarifyDecideAnswer') + const hasAnswer = selCount > 0 || !!other[q.id]?.trim() const renderOpt = (opt: string, label: string) => (
    -
    {q.label}
    +
    +
    {q.label}
    + {q.multi && {t('aiClarifyMulti')}} +
    {q.description &&
    {q.description}
    }
    {q.options.map((opt) => renderOpt(opt, opt))} @@ -2943,7 +2962,15 @@ function ClarifyCard({ value={other[q.id] ?? ''} onChange={(e) => { cancelAdvance() - setOther((p) => ({ ...p, [q.id]: e.target.value })) + const value = e.target.value + setOther((p) => ({ ...p, [q.id]: value })) + if (value.trim()) { + setPicked((prev) => { + const cur = new Set(prev[q.id] ?? []) + cur.delete(decideAnswer) + return { ...prev, [q.id]: cur } + }) + } }} />
    @@ -2956,19 +2983,23 @@ function ClarifyCard({ {t('aiClarifySkip')} {isLast ? ( - ) : ( - /* Only multi-select advances via the filled foot arrow; single-select advances by picking */ + /* Multi-select waits for an explicit Next; single-select advances by picking. */ q.multi && ( ) )} diff --git a/apps/slides/src/renderer/ai/bounded-regex.ts b/apps/slides/src/renderer/ai/bounded-regex.ts new file mode 100644 index 0000000..d5761b8 --- /dev/null +++ b/apps/slides/src/renderer/ai/bounded-regex.ts @@ -0,0 +1,433 @@ +/** + * Budgeted regular-expression engine for layout scripts. Native `RegExp` + * matching runs outside the interpreter's step accounting, so a + * catastrophic-backtracking pattern could freeze the renderer past the step + * limit. This matcher supports the subset layout scripts need (literals, + * classes, groups, alternation, quantifiers, anchors, i/m/s flags) and charges + * every match step against a hard budget instead. + */ + +type PredefName = 'd' | 'D' | 'w' | 'W' | 's' | 'S' + +type RxNode = + | { kind: 'seq'; parts: RxNode[] } + | { kind: 'alt'; options: RxNode[] } + | { kind: 'char'; ch: string } + | { kind: 'dot' } + | { + kind: 'class' + negated: boolean + singles: number[] + ranges: [number, number][] + predefs: PredefName[] + } + | { kind: 'predef'; name: PredefName } + | { kind: 'anchor'; at: 'start' | 'end' | 'word' | 'nonWord' } + | { kind: 'repeat'; min: number; max: number; inner: RxNode } + +const MAX_PATTERN_LENGTH = 500 +const MAX_REPEAT_COUNT = 1000 +const MAX_MATCH_STEPS = 1_000_000 +const MAX_MATCH_DEPTH = 2000 + +const BUDGET_MESSAGE = 'Layout script regular expression exceeded its execution budget' + +export interface BoundedRegex { + test(input: string): boolean +} + +const unavailable = (feature: string): never => { + throw new Error(`Regular-expression ${feature} is not available in layout scripts`) +} + +const isLineTerminator = (c: string): boolean => + c === '\n' || c === '\r' || c === '\u2028' || c === '\u2029' + +const predefMatch = (name: PredefName, c: string): boolean => { + switch (name) { + case 'd': + return c >= '0' && c <= '9' + case 'D': + return !(c >= '0' && c <= '9') + case 'w': + return /[0-9A-Za-z_]/.test(c) + case 'W': + return !/[0-9A-Za-z_]/.test(c) + case 's': + return /\s/.test(c) + case 'S': + return !/\s/.test(c) + } +} + +export function compileBoundedRegex(source: string, flags: string): BoundedRegex { + for (const flag of flags) if (!'gimsd'.includes(flag)) unavailable(`flag "${flag}"`) + if (source.length > MAX_PATTERN_LENGTH) + throw new Error( + `Regular-expression pattern is longer than the ${MAX_PATTERN_LENGTH}-character layout-script limit`, + ) + const ignoreCase = flags.includes('i') + const multiline = flags.includes('m') + const dotAll = flags.includes('s') + + let pos = 0 + const syntaxError = (): never => { + throw new Error(`Invalid regular expression: /${source}/${flags}`) + } + const charNode = (ch: string): RxNode => ({ kind: 'char', ch }) + + const parseEscape = (inClass: boolean): RxNode => { + pos += 1 + const ch = source[pos] + if (ch === undefined) return syntaxError() + pos += 1 + switch (ch) { + case 'd': + case 'D': + case 'w': + case 'W': + case 's': + case 'S': + return { kind: 'predef', name: ch } + case 'b': + return inClass ? charNode('\b') : { kind: 'anchor', at: 'word' } + case 'B': + if (inClass) syntaxError() + return { kind: 'anchor', at: 'nonWord' } + case 'n': + return charNode('\n') + case 'r': + return charNode('\r') + case 't': + return charNode('\t') + case 'f': + return charNode('\f') + case 'v': + return charNode('\v') + case '0': + return charNode('\0') + case 'x': { + const hex = source.slice(pos, pos + 2) + if (!/^[0-9a-fA-F]{2}$/.test(hex)) syntaxError() + pos += 2 + return charNode(String.fromCharCode(parseInt(hex, 16))) + } + case 'u': { + const hex = source.slice(pos, pos + 4) + if (!/^[0-9a-fA-F]{4}$/.test(hex)) syntaxError() + pos += 4 + return charNode(String.fromCharCode(parseInt(hex, 16))) + } + case 'c': { + const letter = source[pos] + if (!letter || !/[a-zA-Z]/.test(letter)) return syntaxError() + pos += 1 + return charNode(String.fromCharCode(letter.toUpperCase().charCodeAt(0) - 64)) + } + case 'k': + return unavailable('named backreference') + case 'p': + case 'P': + return unavailable('unicode property escape') + default: + if (ch >= '1' && ch <= '9') return unavailable('backreference') + if (/[a-zA-Z]/.test(ch)) return syntaxError() + return charNode(ch) + } + } + + const parseClass = (): RxNode => { + pos += 1 + let negated = false + if (source[pos] === '^') { + negated = true + pos += 1 + } + const singles: number[] = [] + const ranges: [number, number][] = [] + const predefs: PredefName[] = [] + while (source[pos] !== ']') { + if (pos >= source.length) syntaxError() + let lo: number + if (source[pos] === '\\') { + const node = parseEscape(true) + if (node.kind === 'predef') { + predefs.push(node.name) + continue + } + lo = (node as { ch: string }).ch.charCodeAt(0) + } else { + lo = source.charCodeAt(pos) + pos += 1 + } + if (source[pos] === '-' && pos + 1 < source.length && source[pos + 1] !== ']') { + pos += 1 + let hi: number + if (source[pos] === '\\') { + const node = parseEscape(true) + if (node.kind === 'predef') return syntaxError() + hi = (node as { ch: string }).ch.charCodeAt(0) + } else { + hi = source.charCodeAt(pos) + pos += 1 + } + if (hi < lo) syntaxError() + ranges.push([lo, hi]) + } else { + singles.push(lo) + } + } + pos += 1 + return { kind: 'class', negated, singles, ranges, predefs } + } + + const parseGroup = (): RxNode => { + pos += 1 + if (source[pos] === '?') { + const next = source[pos + 1] + if (next === ':') pos += 2 + else if (next === '<' && source[pos + 2] !== '=' && source[pos + 2] !== '!') { + const end = source.indexOf('>', pos + 2) + if (end === -1) return syntaxError() + pos = end + 1 + } else return unavailable(next === '=' || next === '!' ? 'lookahead' : 'lookbehind') + } + const inner = parseAlternation() + if (source[pos] !== ')') syntaxError() + pos += 1 + return inner + } + + const parseAtom = (): RxNode => { + const ch = source[pos] + if (ch === undefined) return syntaxError() + switch (ch) { + case '^': + pos += 1 + return { kind: 'anchor', at: 'start' } + case '$': + pos += 1 + return { kind: 'anchor', at: 'end' } + case '.': + pos += 1 + return { kind: 'dot' } + case '(': + return parseGroup() + case '[': + return parseClass() + case '\\': + return parseEscape(false) + case ')': + case '*': + case '+': + case '?': + return syntaxError() + default: + pos += 1 + return charNode(ch) + } + } + + const parseQuantifier = (): { min: number; max: number } | undefined => { + const ch = source[pos] + let quantifier: { min: number; max: number } | undefined + if (ch === '*') { + quantifier = { min: 0, max: Infinity } + pos += 1 + } else if (ch === '+') { + quantifier = { min: 1, max: Infinity } + pos += 1 + } else if (ch === '?') { + quantifier = { min: 0, max: 1 } + pos += 1 + } else if (ch === '{') { + const match = /^\{(\d+)(?:,(\d*))?\}/.exec(source.slice(pos)) + if (match) { + const min = Number(match[1]) + const max = match[2] === undefined ? min : match[2] === '' ? Infinity : Number(match[2]) + if (max < min) syntaxError() + if (min > MAX_REPEAT_COUNT || (max !== Infinity && max > MAX_REPEAT_COUNT)) + unavailable(`repetition beyond {${MAX_REPEAT_COUNT}}`) + quantifier = { min, max } + pos += match[0].length + } + } + if (!quantifier) return undefined + if (source[pos] === '?') pos += 1 + if (source[pos] === '*' || source[pos] === '+' || source[pos] === '?') syntaxError() + return quantifier + } + + const parseQuantified = (): RxNode => { + const atom = parseAtom() + const quantifier = parseQuantifier() + if (!quantifier) return atom + if (atom.kind === 'anchor') syntaxError() + return { kind: 'repeat', min: quantifier.min, max: quantifier.max, inner: atom } + } + + const parseSequence = (): RxNode => { + const parts: RxNode[] = [] + while (pos < source.length && source[pos] !== '|' && source[pos] !== ')') + parts.push(parseQuantified()) + return parts.length === 1 ? parts[0] : { kind: 'seq', parts } + } + + const parseAlternation = (): RxNode => { + const options = [parseSequence()] + while (source[pos] === '|') { + pos += 1 + options.push(parseSequence()) + } + return options.length === 1 ? options[0] : { kind: 'alt', options } + } + + const root = parseAlternation() + if (pos < source.length) syntaxError() + + const charEq = (a: string, b: string): boolean => + a === b || (ignoreCase && a.toLowerCase() === b.toLowerCase()) + + const classMatch = (node: Extract, c: string): boolean => { + const codeMatches = (code: number): boolean => + node.singles.includes(code) || node.ranges.some(([lo, hi]) => code >= lo && code <= hi) + let hit = codeMatches(c.charCodeAt(0)) || node.predefs.some((name) => predefMatch(name, c)) + if (!hit && ignoreCase) { + const lower = c.toLowerCase() + const upper = c.toUpperCase() + hit = + (lower !== c && lower.length === 1 && codeMatches(lower.charCodeAt(0))) || + (upper !== c && upper.length === 1 && codeMatches(upper.charCodeAt(0))) + } + return node.negated ? !hit : hit + } + + const singleMatch = (node: RxNode, c: string): boolean => { + switch (node.kind) { + case 'char': + return charEq(c, node.ch) + case 'dot': + return dotAll || !isLineTerminator(c) + case 'predef': + return predefMatch(node.name, c) + case 'class': + return classMatch(node, c) + default: + return false + } + } + + const isSingle = (node: RxNode): boolean => + node.kind === 'char' || node.kind === 'dot' || node.kind === 'predef' || node.kind === 'class' + + const isWordChar = (c: string | undefined): boolean => c !== undefined && /[0-9A-Za-z_]/.test(c) + + const startsAnchored = (node: RxNode): boolean => { + switch (node.kind) { + case 'anchor': + return node.at === 'start' + case 'seq': + return node.parts.length > 0 && startsAnchored(node.parts[0]) + case 'alt': + return node.options.every(startsAnchored) + case 'repeat': + return node.min >= 1 && startsAnchored(node.inner) + default: + return false + } + } + const anchoredOnly = !multiline && startsAnchored(root) + + const test = (input: string): boolean => { + let budget = MAX_MATCH_STEPS + let depth = 0 + const step = (): void => { + budget -= 1 + if (budget < 0) throw new Error(BUDGET_MESSAGE) + } + + const match = (node: RxNode, at: number, cont: (p: number) => boolean): boolean => { + step() + depth += 1 + if (depth > MAX_MATCH_DEPTH) { + depth -= 1 + throw new Error(BUDGET_MESSAGE) + } + try { + switch (node.kind) { + case 'char': + case 'dot': + case 'predef': + case 'class': + return at < input.length && singleMatch(node, input[at]) && cont(at + 1) + case 'anchor': { + let ok: boolean + switch (node.at) { + case 'start': + ok = at === 0 || (multiline && isLineTerminator(input[at - 1])) + break + case 'end': + ok = at === input.length || (multiline && isLineTerminator(input[at])) + break + case 'word': + ok = isWordChar(input[at - 1]) !== isWordChar(input[at]) + break + case 'nonWord': + ok = isWordChar(input[at - 1]) === isWordChar(input[at]) + break + } + return ok && cont(at) + } + case 'seq': { + const run = (index: number, p: number): boolean => + index >= node.parts.length + ? cont(p) + : match(node.parts[index], p, (np) => run(index + 1, np)) + return run(0, at) + } + case 'alt': + return node.options.some((option) => match(option, at, cont)) + case 'repeat': { + if (isSingle(node.inner)) { + let count = 0 + let p = at + while (count < node.max && p < input.length && singleMatch(node.inner, input[p])) { + step() + count += 1 + p += 1 + } + while (count >= node.min) { + step() + if (cont(p)) return true + count -= 1 + p -= 1 + } + return false + } + const rep = (count: number, p: number): boolean => { + step() + if ( + count < node.max && + match(node.inner, p, (np) => (np === p ? cont(p) : rep(count + 1, np))) + ) + return true + return count >= node.min && cont(p) + } + return rep(0, at) + } + } + } finally { + depth -= 1 + } + } + + const lastStart = anchoredOnly ? 0 : input.length + for (let start = 0; start <= lastStart; start += 1) { + step() + if (match(root, start, () => true)) return true + } + return false + } + + return { test } +} diff --git a/apps/slides/src/renderer/ai/edit-queue.ts b/apps/slides/src/renderer/ai/edit-queue.ts index ae76918..5a517bc 100644 --- a/apps/slides/src/renderer/ai/edit-queue.ts +++ b/apps/slides/src/renderer/ai/edit-queue.ts @@ -117,6 +117,28 @@ export function promptLabel(desc: NodeDescriptor): string { return desc.text ? `${kind}, "${truncate(desc.text, 60)}"` : kind } +/** + * Freeze a selection-scoped "Send now" request into the model instruction. + * The popover already resolved canvas source ids to durable ids; carrying those + * refs here prevents a later live-selection read from substituting a nearby + * element while the AI panel opens. + */ +export function buildSelectionInstruction( + slideIndex: number, + targets: ReadonlyArray<{ id: string; desc: NodeDescriptor }>, + instruction: string, +): string { + return [ + 'The user invoked "Send now" for a fixed canvas selection.', + `Apply the request on page ${slideIndex + 1} (slideIndex=${slideIndex}) to exactly these elements:`, + ...targets.map((target) => `- ${target.id} (${promptLabel(target.desc)})`), + '', + `Requested change: ${instruction}`, + '', + 'Use the listed ids directly. Do not infer or substitute a nearby element, and do not modify unlisted elements.', + ].join('\n') +} + interface FoundNode { node: RenderNode /** 0 = top level, 1 = child of a top-level group, 2+ = deeper (edit tools patch one level only) */ diff --git a/apps/slides/src/renderer/ai/layout-script-interpreter.ts b/apps/slides/src/renderer/ai/layout-script-interpreter.ts index 8ad0853..124ef90 100644 --- a/apps/slides/src/renderer/ai/layout-script-interpreter.ts +++ b/apps/slides/src/renderer/ai/layout-script-interpreter.ts @@ -1,5 +1,7 @@ import { parse } from 'acorn' +import { compileBoundedRegex, type BoundedRegex } from './bounded-regex' + type AstNode = { type: string start?: number @@ -31,10 +33,11 @@ class ScriptFunction { } class RegexValue { - constructor( - readonly source: string, - readonly flags: string, - ) {} + readonly matcher: BoundedRegex + + constructor(source: string, flags: string) { + this.matcher = compileBoundedRegex(source, flags) + } } class Scope { @@ -402,8 +405,7 @@ export function interpretLayoutScript( throw new Error(`String property "${key}" is not available in layout scripts`) } if (target instanceof RegexValue) { - if (key === 'test') - return new Builtin((value) => new RegExp(target.source, target.flags).test(String(value))) + if (key === 'test') return new Builtin((value) => target.matcher.test(String(value))) throw new Error(`Regular-expression property "${key}" is not available in layout scripts`) } if (target instanceof Builtin) { diff --git a/apps/slides/src/renderer/ai/slide-qc.ts b/apps/slides/src/renderer/ai/slide-qc.ts index f9783a7..33a1be3 100644 --- a/apps/slides/src/renderer/ai/slide-qc.ts +++ b/apps/slides/src/renderer/ai/slide-qc.ts @@ -10,7 +10,7 @@ import { type AgentSkill, type AgentTransport, } from '@genoffice/agent-core' -import { getProviderAdapter, type AiSettings } from '@genoffice/ai-provider' +import { getProviderAdapter, modelLacksVision, type AiSettings } from '@genoffice/ai-provider' import { auditSlideLayout } from './layout-audit' import { createSlidesSkill, formatSlideDump, type DeckAccess } from './slides-skill' @@ -23,9 +23,12 @@ export function isQcEnabled(): boolean { export const QC_MAX_PAGES = 20 /** Unknown providers are treated as text-only: omitting an image is safer than a user-visible 400. */ -export function settingsSupportVision(settings: Pick): boolean { +export function settingsSupportVision( + settings: Pick, +): boolean { try { - return getProviderAdapter(settings.provider).capabilities.vision + if (!getProviderAdapter(settings.provider).capabilities.vision) return false + return !modelLacksVision(settings.providers?.[settings.provider]?.model ?? '') } catch { return false } diff --git a/apps/slides/src/renderer/ai/slides-skill.ts b/apps/slides/src/renderer/ai/slides-skill.ts index efbabc1..d5830f8 100644 --- a/apps/slides/src/renderer/ai/slides-skill.ts +++ b/apps/slides/src/renderer/ai/slides-skill.ts @@ -290,7 +290,7 @@ Native tools (only for modifying/refining existing pages, not for generating fro - add_slide clones a layout into a new page (layout-preserving blank page); add_text_box lays out text; add_shape makes color blocks/accent bars (kind supports any OOXML preset geometry rect/roundRect/ellipse/star5…). - For data display use add_chart (native bar/line/pie charts); for structured comparisons use add_table (cells can pre-fill text; later edit_table_cell edits cells, edit_table_structure adds/removes rows/columns); for flows/cycles/hierarchies/lists use add_smartart. - set_slide_background sets a solid background (slideIndex=-1 for all pages); on dark backgrounds remember to lighten the text. -- set_speaker_notes writes the page's speaker notes (演讲者备注/备注, shown in presenter view and saved into the .pptx); it does not touch canvas content. Use it when the user asks to add/update/clear notes for a page. +- set_speaker_notes writes the page's speaker notes (shown in presenter view and saved into the .pptx); it does not touch canvas content. Use it when the user asks to add/update/clear notes for a page. - Refine page by page, element by element; 2–4 elements per page is enough — fewer beats crowded. - Keep replies short, say what you did; don't recite tool results back to the user. @@ -1182,14 +1182,15 @@ const TOOLS: AgentToolDef[] = [ { name: 'set_speaker_notes', description: - "Overwrite the speaker notes (演讲者备注/备注) of a page. Notes are shown in presenter view and saved into the .pptx notesSlide part; they do not affect canvas content. text replaces the page's current notes entirely; pass text:\"\" to clear them. Call when the user asks to add/update/remove notes for a page.", + 'Overwrite the speaker notes of a page. Notes are shown in presenter view and saved into the .pptx notesSlide part; they do not affect canvas content. text replaces the page\'s current notes entirely; pass text:"" to clear them. Call when the user asks to add/update/remove notes for a page.', inputSchema: { type: 'object', properties: { slideIndex: { type: 'integer', description: 'Page number (0-based)' }, text: { type: 'string', - description: 'Full speaker notes text; paragraphs separated by newlines. Empty string clears the notes.', + description: + 'Full speaker notes text; paragraphs separated by newlines. Empty string clears the notes.', }, }, required: ['slideIndex', 'text'], @@ -1477,7 +1478,14 @@ function buildDeckOutline(slides: RenderSlide[], current: number, selectedIds: s `The presentation has ${slides.length} pages; page ${current + 1} is currently shown. ${canvas}`, `(Page order is the current actual order and may differ from generation time or earlier conversation; the user's "page N" refers to this outline)`, ] - if (selectedIds.length > 0) lines.push(`User selected elements: ${selectedIds.join(', ')}`) + if (selectedIds.length > 0) { + const currentSlide = slides[current] + const selectedRefs = selectedIds.map((id) => { + const node = currentSlide ? findNodeById(currentSlide.nodes, id) : undefined + return node?.durableId ?? node?.sourceId ?? id + }) + lines.push(`User selected elements: ${selectedRefs.join(', ')}`) + } slides.forEach((slide, i) => { lines.push(`Page ${i + 1} (slideIndex=${i}):`) const infos = collectNodeInfos(slide.nodes) diff --git a/apps/slides/src/renderer/components/RibbonHomeTab.tsx b/apps/slides/src/renderer/components/RibbonHomeTab.tsx index d0cc77e..1d68f9e 100644 --- a/apps/slides/src/renderer/components/RibbonHomeTab.tsx +++ b/apps/slides/src/renderer/components/RibbonHomeTab.tsx @@ -6,6 +6,7 @@ import { saveEditSelection } from '../TextEditOverlay' import { armColorInput } from '../color-input' import { displayFontFamily } from '../konva-adapter' import { useSystemFontFamilies } from '../system-fonts' +import { useFontCatalog } from '../font-manager' import { GensparkMark, IconAiAskSelection, @@ -154,6 +155,23 @@ export function RibbonHomeTab({ rb }: { rb: RibbonTabCtx }) { // (opening via the caret or focusing shows the full list) const [fontFilter, setFontFilter] = useState('') const { families: systemFontFamilies, load: loadSystemFonts } = useSystemFontFamilies() + const { + catalog: fontCatalog, + busy: fontBusy, + failed: fontFailed, + load: loadFontCatalog, + download: downloadFont, + installLocal: installLocalFonts, + } = useFontCatalog() + // Catalog families stay listed after install (store fonts are invisible to + // queryLocalFonts). Installed ones dedupe against the built-in/system sections; + // uninstalled ones always show here so built-in names like Noto Sans JP keep an + // in-picker download path (the built-in row is hidden below while uninstalled). + const catalogFonts = fontCatalog.filter( + (c) => + !c.installed || (!FONT_FAMILIES.includes(c.family) && !systemFontFamilies.includes(c.family)), + ) + const uninstalledCatalog = new Set(fontCatalog.filter((c) => !c.installed).map((c) => c.family)) const matchesFontFilter = (f: string) => !fontFilter.trim() || f.toLowerCase().includes(fontFilter.trim().toLowerCase()) const EMU_PER_PX = 9525 @@ -505,6 +523,7 @@ export function RibbonHomeTab({ rb }: { rb: RibbonTabCtx }) { // popup (size/color/...) before opening the font list closePanels(['font']) loadSystemFonts() + loadFontCatalog() setFontOpen(true) }} onBlur={() => { @@ -537,7 +556,10 @@ export function RibbonHomeTab({ rb }: { rb: RibbonTabCtx }) { if (editing || hasTextSelection) { closeSiblingPanels(e, closePanels, 'font') setFontFilter('') - if (!fontOpen) loadSystemFonts() + if (!fontOpen) { + loadSystemFonts() + loadFontCatalog() + } setFontOpen((v) => !v) } }} @@ -557,6 +579,9 @@ export function RibbonHomeTab({ rb }: { rb: RibbonTabCtx }) { : FONT_FAMILIES ) .filter(matchesFontFilter) + // Built-in names that are uninstalled catalog fonts render in the + // downloadable section instead (apply-only would set a missing font) + .filter((f) => f === curFontFamily || !uninstalledCatalog.has(f)) .map((f) => ( + ) : ( + + ), + )} + + )} +
    )}
    diff --git a/apps/slides/src/renderer/font-manager.ts b/apps/slides/src/renderer/font-manager.ts new file mode 100644 index 0000000..b8d9b2e --- /dev/null +++ b/apps/slides/src/renderer/font-manager.ts @@ -0,0 +1,80 @@ +import { useCallback, useEffect, useState } from 'react' + +export interface CatalogEntry { + family: string + script: 'latin' | 'ja' | 'ko' | 'sc' | 'tc' + installed: boolean + downloading: boolean +} + +let cached: CatalogEntry[] | null = null + +/** + * Downloadable font catalog + install actions. Loaded lazily from the picker's + * open click (same pattern as useSystemFontFamilies); download/install push new + * layouts from main via deck-changed, so callers only refresh list state here. + */ +export function useFontCatalog(): { + readonly catalog: CatalogEntry[] + readonly busy: ReadonlySet + readonly failed: ReadonlySet + readonly load: () => void + readonly download: (family: string) => Promise + readonly installLocal: () => Promise +} { + const [catalog, setCatalog] = useState(cached ?? []) + const [busy, setBusy] = useState>(new Set()) + const [failed, setFailed] = useState>(new Set()) + + const load = useCallback(() => { + void window.slidesApi + .fontCatalog?.() + .then((c) => { + cached = c + setCatalog(c) + }) + .catch(() => {}) + }, []) + + const download = useCallback( + async (family: string): Promise => { + setBusy((s) => new Set(s).add(family)) + setFailed((s) => { + const n = new Set(s) + n.delete(family) + return n + }) + try { + const r = await window.slidesApi.fontDownload?.(family) + if (!r?.ok) throw new Error(r?.error) + return true + } catch { + setFailed((s) => new Set(s).add(family)) + return false + } finally { + setBusy((s) => { + const n = new Set(s) + n.delete(family) + return n + }) + load() + } + }, + [load], + ) + + const installLocal = useCallback(async (): Promise => { + try { + const r = await window.slidesApi.fontInstallLocal?.() + return r?.families ?? [] + } catch { + return [] + } finally { + load() + } + }, [load]) + + useEffect(() => window.slidesApi.onFontsChanged?.(load), [load]) + + return { catalog, busy, failed, load, download, installLocal } +} diff --git a/apps/slides/src/renderer/i18n/strings-ai.ts b/apps/slides/src/renderer/i18n/strings-ai.ts index 9424c32..31521a8 100644 --- a/apps/slides/src/renderer/i18n/strings-ai.ts +++ b/apps/slides/src/renderer/i18n/strings-ai.ts @@ -121,11 +121,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: '({n}页)', aiPageN: '第{n}页', aiClarifyTitle: '先确认几个问题,让 PPT 更贴合你的需求', + aiClarifyMulti: '可多选', aiClarifySelected: '已选 {n} 项', aiClarifyDecide: '帮我决定', aiClarifyDecideAnswer: '(帮我决定)', aiClarifyOther: '其他(可填写)', - aiClarifySkip: '跳过', + aiClarifySkip: '跳过调研', + aiClarifyNext: '下一题', aiClarifySubmit: '开始生成', aiSumReadAttachment: '读取附件', aiSumReadAttachmentName: '读取附件 {name}', @@ -366,11 +368,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} pages)', aiPageN: 'Page {n}', aiClarifyTitle: 'A few quick questions to tailor the deck to you', + aiClarifyMulti: 'Multiple choice', aiClarifySelected: '{n} selected', aiClarifyDecide: 'Decide for me', aiClarifyDecideAnswer: '(decide for me)', aiClarifyOther: 'Other (type in)', - aiClarifySkip: 'Skip', + aiClarifySkip: 'Skip survey', + aiClarifyNext: 'Next', aiClarifySubmit: 'Generate', aiSumReadAttachment: 'Read attachment', aiSumReadAttachmentName: 'Read attachment {name}', @@ -610,11 +614,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: '({n}ページ)', aiPageN: '{n}ページ目', aiClarifyTitle: 'ご要望に合ったスライドにするため、いくつか確認させてください', + aiClarifyMulti: '複数選択', aiClarifySelected: '{n} 件選択', aiClarifyDecide: 'おまかせ', aiClarifyDecideAnswer: '(おまかせ)', aiClarifyOther: 'その他(自由記入)', - aiClarifySkip: 'スキップ', + aiClarifySkip: 'アンケートをスキップ', + aiClarifyNext: '次へ', aiClarifySubmit: '生成する', aiSumReadAttachment: '添付ファイルを読み取り', aiSumReadAttachmentName: '添付ファイル {name} を読み取り', @@ -855,11 +861,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: '({n}페이지)', aiPageN: '{n}페이지', aiClarifyTitle: '몇 가지 질문에 답하면 요구에 더 맞는 PPT를 만들 수 있습니다', + aiClarifyMulti: '복수 선택', aiClarifySelected: '{n}개 선택', aiClarifyDecide: '알아서 결정', aiClarifyDecideAnswer: '(알아서 결정)', aiClarifyOther: '기타(직접 입력)', - aiClarifySkip: '건너뛰기', + aiClarifySkip: '설문 건너뛰기', + aiClarifyNext: '다음', aiClarifySubmit: '생성하기', aiSumReadAttachment: '첨부 파일 읽기', aiSumReadAttachmentName: '첨부 파일 {name} 읽기', @@ -1102,11 +1110,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} pages)', aiPageN: 'Page {n}', aiClarifyTitle: 'Quelques questions rapides pour adapter la présentation à vos besoins', + aiClarifyMulti: 'Choix multiple', aiClarifySelected: '{n} sélectionnés', aiClarifyDecide: 'Décidez pour moi', aiClarifyDecideAnswer: '(décidez pour moi)', aiClarifyOther: 'Autre (à saisir)', - aiClarifySkip: 'Passer', + aiClarifySkip: 'Ignorer le questionnaire', + aiClarifyNext: 'Suivant', aiClarifySubmit: 'Générer', aiSumReadAttachment: 'Lecture de la pièce jointe', aiSumReadAttachmentName: 'Lecture de la pièce jointe {name}', @@ -1350,11 +1360,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} Seiten)', aiPageN: 'Seite {n}', aiClarifyTitle: 'Ein paar kurze Fragen, um die Präsentation auf Sie zuzuschneiden', + aiClarifyMulti: 'Mehrfachauswahl', aiClarifySelected: '{n} ausgewählt', aiClarifyDecide: 'Entscheide für mich', aiClarifyDecideAnswer: '(entscheide für mich)', aiClarifyOther: 'Sonstiges (eingeben)', - aiClarifySkip: 'Überspringen', + aiClarifySkip: 'Umfrage überspringen', + aiClarifyNext: 'Weiter', aiClarifySubmit: 'Generieren', aiSumReadAttachment: 'Anlage gelesen', aiSumReadAttachmentName: 'Anlage {name} gelesen', @@ -1597,11 +1609,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} páginas)', aiPageN: 'Página {n}', aiClarifyTitle: 'Unas preguntas rápidas para adaptar la presentación a ti', + aiClarifyMulti: 'Selección múltiple', aiClarifySelected: '{n} seleccionados', aiClarifyDecide: 'Decide por mí', aiClarifyDecideAnswer: '(decide por mí)', aiClarifyOther: 'Otro (escríbelo)', - aiClarifySkip: 'Omitir', + aiClarifySkip: 'Omitir encuesta', + aiClarifyNext: 'Siguiente', aiClarifySubmit: 'Generar', aiSumReadAttachment: 'Leer datos adjuntos', aiSumReadAttachmentName: 'Leer archivo adjunto {name}', @@ -1840,11 +1854,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} หน้า)', aiPageN: 'หน้า {n}', aiClarifyTitle: 'ขอถามคำถามสั้น ๆ เพื่อให้งานนำเสนอตรงกับความต้องการของคุณยิ่งขึ้น', + aiClarifyMulti: 'เลือกได้หลายข้อ', aiClarifySelected: 'เลือกแล้ว {n}', aiClarifyDecide: 'ตัดสินใจให้ฉัน', aiClarifyDecideAnswer: '(ตัดสินใจให้ฉัน)', aiClarifyOther: 'อื่น ๆ (พิมพ์เอง)', - aiClarifySkip: 'ข้าม', + aiClarifySkip: 'ข้ามแบบสอบถาม', + aiClarifyNext: 'ถัดไป', aiClarifySubmit: 'สร้าง', aiSumReadAttachment: 'อ่านสิ่งที่แนบ', aiSumReadAttachmentName: 'อ่านสิ่งที่แนบ {name}', @@ -2086,11 +2102,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} halaman)', aiPageN: 'Halaman {n}', aiClarifyTitle: 'Beberapa pertanyaan singkat agar presentasi lebih sesuai kebutuhan Anda', + aiClarifyMulti: 'Pilih beberapa', aiClarifySelected: '{n} dipilih', aiClarifyDecide: 'Putuskan untuk saya', aiClarifyDecideAnswer: '(putuskan untuk saya)', aiClarifyOther: 'Lainnya (isi sendiri)', - aiClarifySkip: 'Lewati', + aiClarifySkip: 'Lewati survei', + aiClarifyNext: 'Berikutnya', aiClarifySubmit: 'Buat', aiSumReadAttachment: 'Baca lampiran', aiSumReadAttachmentName: 'Baca lampiran {name}', @@ -2332,11 +2350,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} стр.)', aiPageN: 'Страница {n}', aiClarifyTitle: 'Несколько коротких вопросов, чтобы презентация лучше отвечала вашим задачам', + aiClarifyMulti: 'Несколько вариантов', aiClarifySelected: 'выбрано: {n}', aiClarifyDecide: 'Решите за меня', aiClarifyDecideAnswer: '(решите за меня)', aiClarifyOther: 'Другое (впишите свой вариант)', - aiClarifySkip: 'Пропустить', + aiClarifySkip: 'Пропустить опрос', + aiClarifyNext: 'Далее', aiClarifySubmit: 'Сгенерировать', aiSumReadAttachment: 'Чтение вложения', aiSumReadAttachmentName: 'Чтение вложения {name}', @@ -2575,11 +2595,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} صفحات)', aiPageN: 'الصفحة {n}', aiClarifyTitle: 'بضعة أسئلة سريعة ليكون العرض أنسب لاحتياجاتك', + aiClarifyMulti: 'اختيار متعدد', aiClarifySelected: 'تم تحديد {n}', aiClarifyDecide: 'قرِّر بالنيابة عني', aiClarifyDecideAnswer: '(قرِّر بالنيابة عني)', aiClarifyOther: 'أخرى (يمكن الكتابة)', - aiClarifySkip: 'تخطَّ', + aiClarifySkip: 'تخطي الاستبيان', + aiClarifyNext: 'التالي', aiClarifySubmit: 'إنشاء', aiSumReadAttachment: 'قراءة المرفق', aiSumReadAttachmentName: 'قراءة المرفق {name}', @@ -2821,11 +2843,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} páginas)', aiPageN: 'Página {n}', aiClarifyTitle: 'Algumas perguntas rápidas para adaptar a apresentação a você', + aiClarifyMulti: 'Seleção múltipla', aiClarifySelected: '{n} selecionados', aiClarifyDecide: 'Decida por mim', aiClarifyDecideAnswer: '(decida por mim)', aiClarifyOther: 'Outro (digite)', - aiClarifySkip: 'Pular', + aiClarifySkip: 'Pular pesquisa', + aiClarifyNext: 'Próxima', aiClarifySubmit: 'Gerar', aiSumReadAttachment: 'Ler anexo', aiSumReadAttachmentName: 'Ler anexo {name}', @@ -3067,11 +3091,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} pagine)', aiPageN: 'Pagina {n}', aiClarifyTitle: 'Qualche domanda rapida per adattare la presentazione alle tue esigenze', + aiClarifyMulti: 'Scelta multipla', aiClarifySelected: '{n} selezionati', aiClarifyDecide: 'Decidi tu per me', aiClarifyDecideAnswer: '(decidi tu per me)', aiClarifyOther: 'Altro (da digitare)', - aiClarifySkip: 'Salta', + aiClarifySkip: 'Salta sondaggio', + aiClarifyNext: 'Avanti', aiClarifySubmit: 'Genera', aiSumReadAttachment: "Lettura dell'allegato", aiSumReadAttachmentName: "Lettura dell'allegato {name}", @@ -3313,11 +3339,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} stron)', aiPageN: 'Strona {n}', aiClarifyTitle: 'Kilka szybkich pytań, aby dopasować prezentację do twoich potrzeb', + aiClarifyMulti: 'Wielokrotny wybór', aiClarifySelected: 'wybrano: {n}', aiClarifyDecide: 'Zdecyduj za mnie', aiClarifyDecideAnswer: '(zdecyduj za mnie)', aiClarifyOther: 'Inne (wpisz)', - aiClarifySkip: 'Pomiń', + aiClarifySkip: 'Pomiń ankietę', + aiClarifyNext: 'Dalej', aiClarifySubmit: 'Generuj', aiSumReadAttachment: 'Odczyt załącznika', aiSumReadAttachmentName: 'Odczyt załącznika {name}', @@ -3560,11 +3588,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: " ({n} pagina's)", aiPageN: 'Pagina {n}', aiClarifyTitle: 'Een paar korte vragen om de presentatie op u af te stemmen', + aiClarifyMulti: 'Meerdere keuzes', aiClarifySelected: '{n} geselecteerd', aiClarifyDecide: 'Beslis voor mij', aiClarifyDecideAnswer: '(beslis voor mij)', aiClarifyOther: 'Anders (typ zelf)', - aiClarifySkip: 'Overslaan', + aiClarifySkip: 'Enquête overslaan', + aiClarifyNext: 'Volgende', aiClarifySubmit: 'Genereren', aiSumReadAttachment: 'Bijlage gelezen', aiSumReadAttachmentName: 'Bijlage {name} gelezen', @@ -3807,11 +3837,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} halaman)', aiPageN: 'Halaman {n}', aiClarifyTitle: 'Beberapa soalan ringkas untuk menyesuaikan persembahan dengan keperluan anda', + aiClarifyMulti: 'Pilihan berbilang', aiClarifySelected: '{n} dipilih', aiClarifyDecide: 'Putuskan untuk saya', aiClarifyDecideAnswer: '(putuskan untuk saya)', aiClarifyOther: 'Lain-lain (taip sendiri)', - aiClarifySkip: 'Langkau', + aiClarifySkip: 'Langkau tinjauan', + aiClarifyNext: 'Seterusnya', aiClarifySubmit: 'Jana', aiSumReadAttachment: 'Baca lampiran', aiSumReadAttachmentName: 'Baca lampiran {name}', @@ -4049,11 +4081,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} עמודים)', aiPageN: 'עמוד {n}', aiClarifyTitle: 'כמה שאלות קצרות כדי להתאים את המצגת אליך', + aiClarifyMulti: 'בחירה מרובה', aiClarifySelected: 'נבחרו {n}', aiClarifyDecide: 'החלט בשבילי', aiClarifyDecideAnswer: '(החלט בשבילי)', aiClarifyOther: 'אחר (הקלד)', - aiClarifySkip: 'דלג', + aiClarifySkip: 'דלג על השאלון', + aiClarifyNext: 'הבא', aiClarifySubmit: 'צור', aiSumReadAttachment: 'קריאת קובץ מצורף', aiSumReadAttachmentName: 'קריאת הקובץ המצורף {name}', @@ -4294,11 +4328,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: ' ({n} पृष्ठ)', aiPageN: 'पृष्ठ {n}', aiClarifyTitle: 'प्रेज़ेंटेशन को आपके अनुरूप बनाने के लिए कुछ छोटे प्रश्न', + aiClarifyMulti: 'एकाधिक चयन', aiClarifySelected: '{n} चयनित', aiClarifyDecide: 'मेरे लिए तय करें', aiClarifyDecideAnswer: '(मेरे लिए तय करें)', aiClarifyOther: 'अन्य (स्वयं लिखें)', - aiClarifySkip: 'छोड़ें', + aiClarifySkip: 'सर्वेक्षण छोड़ें', + aiClarifyNext: 'अगला', aiClarifySubmit: 'जनरेट करें', aiSumReadAttachment: 'अटैचमेंट पढ़ा गया', aiSumReadAttachmentName: 'अटैचमेंट {name} पढ़ा गया', @@ -4532,11 +4568,13 @@ export const aiStrings = defineStrings({ aiPagesSuffix: '({n}頁)', aiPageN: '第{n}頁', aiClarifyTitle: '先確認幾個問題,讓簡報更貼合你的需求', + aiClarifyMulti: '可複選', aiClarifySelected: '已選 {n} 項', aiClarifyDecide: '幫我決定', aiClarifyDecideAnswer: '(幫我決定)', aiClarifyOther: '其他(可填寫)', - aiClarifySkip: '略過', + aiClarifySkip: '略過問卷', + aiClarifyNext: '下一題', aiClarifySubmit: '開始產生', aiSumReadAttachment: '讀取附件', aiSumReadAttachmentName: '讀取附件 {name}', diff --git a/apps/slides/src/renderer/i18n/strings-ribbon.ts b/apps/slides/src/renderer/i18n/strings-ribbon.ts index 65b5c9b..9a81d97 100644 --- a/apps/slides/src/renderer/i18n/strings-ribbon.ts +++ b/apps/slides/src/renderer/i18n/strings-ribbon.ts @@ -72,6 +72,13 @@ export const ribbonStrings = defineStrings({ // Home: font ribbonGroupFont: '字体', ribbonFontsSystem: '系统字体', + ribbonFontsDownloadable: '可下载字体', + ribbonFontInstallLocal: '安装字体文件…', + ribbonFontDownloading: '下载中…', + ribbonFontDownloadFailed: '下载失败,点击重试', + fontMissingBanner: '此文稿使用了未安装的字体:', + fontMissingDownloadAll: '全部下载', + fontMissingDismiss: '忽略', ribbonGroupParagraph: '段落', ribbonCharSpacing: '字符间距', ribbonChangeCase: '更改大小写', @@ -789,6 +796,13 @@ export const ribbonStrings = defineStrings({ ribbonAddSectionTip: 'Add a section before the current slide to organize long decks', ribbonGroupFont: 'Font', ribbonFontsSystem: 'System fonts', + ribbonFontsDownloadable: 'Downloadable fonts', + ribbonFontInstallLocal: 'Install font file…', + ribbonFontDownloading: 'Downloading…', + ribbonFontDownloadFailed: 'Download failed — click to retry', + fontMissingBanner: 'This presentation uses fonts that are not installed:', + fontMissingDownloadAll: 'Download all', + fontMissingDismiss: 'Dismiss', ribbonGroupParagraph: 'Paragraph', ribbonCharSpacing: 'Character Spacing', ribbonChangeCase: 'Change Case', @@ -1507,6 +1521,13 @@ export const ribbonStrings = defineStrings({ // Home: font ribbonGroupFont: 'フォント', ribbonFontsSystem: 'システムフォント', + ribbonFontsDownloadable: 'ダウンロード可能なフォント', + ribbonFontInstallLocal: 'フォントファイルをインストール…', + ribbonFontDownloading: 'ダウンロード中…', + ribbonFontDownloadFailed: 'ダウンロード失敗 — クリックで再試行', + fontMissingBanner: 'このプレゼンテーションには未インストールのフォントが使われています:', + fontMissingDownloadAll: 'すべてダウンロード', + fontMissingDismiss: '閉じる', ribbonGroupParagraph: '段落', ribbonCharSpacing: '文字の間隔', ribbonChangeCase: '文字種の変換', @@ -2246,6 +2267,13 @@ export const ribbonStrings = defineStrings({ // Home: font ribbonGroupFont: '글꼴', ribbonFontsSystem: '시스템 글꼴', + ribbonFontsDownloadable: '다운로드 가능한 글꼴', + ribbonFontInstallLocal: '글꼴 파일 설치…', + ribbonFontDownloading: '다운로드 중…', + ribbonFontDownloadFailed: '다운로드 실패 — 클릭하여 재시도', + fontMissingBanner: '이 프레젠테이션에 설치되지 않은 글꼴이 사용되었습니다:', + fontMissingDownloadAll: '모두 다운로드', + fontMissingDismiss: '닫기', ribbonGroupParagraph: '단락', ribbonCharSpacing: '문자 간격', ribbonChangeCase: '대/소문자 바꾸기', @@ -2977,6 +3005,13 @@ export const ribbonStrings = defineStrings({ 'Ajouter une section avant la diapositive actuelle pour organiser les longues présentations', ribbonGroupFont: 'Police', ribbonFontsSystem: 'Polices système', + ribbonFontsDownloadable: 'Polices téléchargeables', + ribbonFontInstallLocal: 'Installer un fichier de police…', + ribbonFontDownloading: 'Téléchargement…', + ribbonFontDownloadFailed: 'Échec du téléchargement — cliquez pour réessayer', + fontMissingBanner: 'Cette présentation utilise des polices non installées :', + fontMissingDownloadAll: 'Tout télécharger', + fontMissingDismiss: 'Ignorer', ribbonGroupParagraph: 'Paragraphe', ribbonCharSpacing: 'Espacement des caractères', ribbonChangeCase: 'Modifier la casse', @@ -3718,6 +3753,13 @@ export const ribbonStrings = defineStrings({ 'Abschnitt vor der aktuellen Folie hinzufügen (zum Organisieren langer Präsentationen)', ribbonGroupFont: 'Schriftart', ribbonFontsSystem: 'Systemschriftarten', + ribbonFontsDownloadable: 'Herunterladbare Schriftarten', + ribbonFontInstallLocal: 'Schriftdatei installieren…', + ribbonFontDownloading: 'Wird heruntergeladen…', + ribbonFontDownloadFailed: 'Download fehlgeschlagen – zum Wiederholen klicken', + fontMissingBanner: 'Diese Präsentation verwendet nicht installierte Schriftarten:', + fontMissingDownloadAll: 'Alle herunterladen', + fontMissingDismiss: 'Ignorieren', ribbonGroupParagraph: 'Absatz', ribbonCharSpacing: 'Zeichenabstand', ribbonChangeCase: 'Groß-/Kleinschreibung', @@ -4455,6 +4497,13 @@ export const ribbonStrings = defineStrings({ 'Agregar una sección antes de la diapositiva actual para organizar presentaciones largas', ribbonGroupFont: 'Fuente', ribbonFontsSystem: 'Fuentes del sistema', + ribbonFontsDownloadable: 'Fuentes descargables', + ribbonFontInstallLocal: 'Instalar archivo de fuente…', + ribbonFontDownloading: 'Descargando…', + ribbonFontDownloadFailed: 'Error de descarga: haga clic para reintentar', + fontMissingBanner: 'Esta presentación usa fuentes no instaladas:', + fontMissingDownloadAll: 'Descargar todo', + fontMissingDismiss: 'Descartar', ribbonGroupParagraph: 'Párrafo', ribbonCharSpacing: 'Espaciado entre caracteres', ribbonChangeCase: 'Cambiar mayúsculas y minúsculas', @@ -5192,6 +5241,13 @@ export const ribbonStrings = defineStrings({ ribbonAddSectionTip: 'เพิ่มส่วนก่อนสไลด์ปัจจุบัน (ช่วยจัดระเบียบงานนำเสนอที่ยาว)', ribbonGroupFont: 'ฟอนต์', ribbonFontsSystem: 'แบบอักษรของระบบ', + ribbonFontsDownloadable: 'แบบอักษรที่ดาวน์โหลดได้', + ribbonFontInstallLocal: 'ติดตั้งไฟล์แบบอักษร…', + ribbonFontDownloading: 'กำลังดาวน์โหลด…', + ribbonFontDownloadFailed: 'ดาวน์โหลดล้มเหลว — คลิกเพื่อลองใหม่', + fontMissingBanner: 'งานนำเสนอนี้ใช้แบบอักษรที่ยังไม่ได้ติดตั้ง:', + fontMissingDownloadAll: 'ดาวน์โหลดทั้งหมด', + fontMissingDismiss: 'ปิด', ribbonGroupParagraph: 'ย่อหน้า', ribbonCharSpacing: 'ระยะห่างอักขระ', ribbonChangeCase: 'เปลี่ยนตัวพิมพ์', @@ -5905,6 +5961,13 @@ export const ribbonStrings = defineStrings({ 'Tambahkan bagian sebelum slide saat ini untuk mengatur presentasi yang panjang', ribbonGroupFont: 'Font', ribbonFontsSystem: 'Font sistem', + ribbonFontsDownloadable: 'Font yang dapat diunduh', + ribbonFontInstallLocal: 'Instal file font…', + ribbonFontDownloading: 'Mengunduh…', + ribbonFontDownloadFailed: 'Unduhan gagal — klik untuk mencoba lagi', + fontMissingBanner: 'Presentasi ini menggunakan font yang belum terpasang:', + fontMissingDownloadAll: 'Unduh semua', + fontMissingDismiss: 'Abaikan', ribbonGroupParagraph: 'Paragraf', ribbonCharSpacing: 'Penspasian Karakter', ribbonChangeCase: 'Ubah Huruf Besar/Kecil', @@ -6631,6 +6694,13 @@ export const ribbonStrings = defineStrings({ 'Добавить раздел перед текущим слайдом (удобно для организации длинных презентаций)', ribbonGroupFont: 'Шрифт', ribbonFontsSystem: 'Системные шрифты', + ribbonFontsDownloadable: 'Загружаемые шрифты', + ribbonFontInstallLocal: 'Установить файл шрифта…', + ribbonFontDownloading: 'Загрузка…', + ribbonFontDownloadFailed: 'Ошибка загрузки — нажмите, чтобы повторить', + fontMissingBanner: 'В этой презентации используются неустановленные шрифты:', + fontMissingDownloadAll: 'Загрузить все', + fontMissingDismiss: 'Скрыть', ribbonGroupParagraph: 'Абзац', ribbonCharSpacing: 'Межзнаковый интервал', ribbonChangeCase: 'Изменить регистр', @@ -7355,6 +7425,13 @@ export const ribbonStrings = defineStrings({ ribbonAddSectionTip: 'إضافة مقطع قبل الشريحة الحالية لتنظيم العروض التقديمية الطويلة', ribbonGroupFont: 'خط', ribbonFontsSystem: 'خطوط النظام', + ribbonFontsDownloadable: 'خطوط قابلة للتنزيل', + ribbonFontInstallLocal: 'تثبيت ملف خط…', + ribbonFontDownloading: 'جارٍ التنزيل…', + ribbonFontDownloadFailed: 'فشل التنزيل — انقر لإعادة المحاولة', + fontMissingBanner: 'يستخدم هذا العرض خطوطًا غير مثبتة:', + fontMissingDownloadAll: 'تنزيل الكل', + fontMissingDismiss: 'تجاهل', ribbonGroupParagraph: 'فقرة', ribbonCharSpacing: 'تباعد الأحرف', ribbonChangeCase: 'تغيير حالة الأحرف', @@ -8068,6 +8145,13 @@ export const ribbonStrings = defineStrings({ 'Adiciona uma seção antes do slide atual para organizar apresentações longas', ribbonGroupFont: 'Fonte', ribbonFontsSystem: 'Fontes do sistema', + ribbonFontsDownloadable: 'Fontes para download', + ribbonFontInstallLocal: 'Instalar arquivo de fonte…', + ribbonFontDownloading: 'Baixando…', + ribbonFontDownloadFailed: 'Falha no download — clique para tentar novamente', + fontMissingBanner: 'Esta apresentação usa fontes não instaladas:', + fontMissingDownloadAll: 'Baixar tudo', + fontMissingDismiss: 'Dispensar', ribbonGroupParagraph: 'Parágrafo', ribbonCharSpacing: 'Espaçamento de caracteres', ribbonChangeCase: 'Alterar maiúsculas e minúsculas', @@ -8799,6 +8883,13 @@ export const ribbonStrings = defineStrings({ 'Aggiunge una sezione prima della diapositiva corrente per organizzare presentazioni lunghe', ribbonGroupFont: 'Carattere', ribbonFontsSystem: 'Caratteri di sistema', + ribbonFontsDownloadable: 'Caratteri scaricabili', + ribbonFontInstallLocal: 'Installa file di carattere…', + ribbonFontDownloading: 'Download in corso…', + ribbonFontDownloadFailed: 'Download non riuscito — fai clic per riprovare', + fontMissingBanner: 'Questa presentazione usa caratteri non installati:', + fontMissingDownloadAll: 'Scarica tutto', + fontMissingDismiss: 'Ignora', ribbonGroupParagraph: 'Paragrafo', ribbonCharSpacing: 'Spaziatura caratteri', ribbonChangeCase: 'Maiuscole/minuscole', @@ -9536,6 +9627,13 @@ export const ribbonStrings = defineStrings({ 'Dodaje sekcję przed bieżącym slajdem, aby ułatwić organizację długich prezentacji', ribbonGroupFont: 'Czcionka', ribbonFontsSystem: 'Czcionki systemowe', + ribbonFontsDownloadable: 'Czcionki do pobrania', + ribbonFontInstallLocal: 'Zainstaluj plik czcionki…', + ribbonFontDownloading: 'Pobieranie…', + ribbonFontDownloadFailed: 'Pobieranie nie powiodło się — kliknij, aby ponowić', + fontMissingBanner: 'Ta prezentacja używa niezainstalowanych czcionek:', + fontMissingDownloadAll: 'Pobierz wszystkie', + fontMissingDismiss: 'Odrzuć', ribbonGroupParagraph: 'Akapit', ribbonCharSpacing: 'Odstępy między znakami', ribbonChangeCase: 'Zmień wielkość liter', @@ -10265,6 +10363,13 @@ export const ribbonStrings = defineStrings({ 'Voegt een sectie toe vóór de huidige dia om lange presentaties te ordenen', ribbonGroupFont: 'Lettertype', ribbonFontsSystem: 'Systeemlettertypen', + ribbonFontsDownloadable: 'Downloadbare lettertypen', + ribbonFontInstallLocal: 'Lettertypebestand installeren…', + ribbonFontDownloading: 'Downloaden…', + ribbonFontDownloadFailed: 'Download mislukt — klik om opnieuw te proberen', + fontMissingBanner: 'Deze presentatie gebruikt niet-geïnstalleerde lettertypen:', + fontMissingDownloadAll: 'Alles downloaden', + fontMissingDismiss: 'Negeren', ribbonGroupParagraph: 'Alinea', ribbonCharSpacing: 'Tekenafstand', ribbonChangeCase: 'Hoofdlettergebruik wijzigen', @@ -10999,6 +11104,13 @@ export const ribbonStrings = defineStrings({ 'Menambah seksyen sebelum slaid semasa untuk mengatur persembahan yang panjang', ribbonGroupFont: 'Fon', ribbonFontsSystem: 'Fon sistem', + ribbonFontsDownloadable: 'Fon boleh muat turun', + ribbonFontInstallLocal: 'Pasang fail fon…', + ribbonFontDownloading: 'Memuat turun…', + ribbonFontDownloadFailed: 'Muat turun gagal — klik untuk cuba lagi', + fontMissingBanner: 'Persembahan ini menggunakan fon yang belum dipasang:', + fontMissingDownloadAll: 'Muat turun semua', + fontMissingDismiss: 'Abaikan', ribbonGroupParagraph: 'Paragraph', ribbonCharSpacing: 'Character Spacing', ribbonChangeCase: 'Change Case', @@ -11727,6 +11839,13 @@ export const ribbonStrings = defineStrings({ ribbonAddSectionTip: 'מוסיף מקטע לפני השקופית הנוכחית לארגון מצגות ארוכות', ribbonGroupFont: 'גופן', ribbonFontsSystem: 'גופני מערכת', + ribbonFontsDownloadable: 'גופנים להורדה', + ribbonFontInstallLocal: 'התקנת קובץ גופן…', + ribbonFontDownloading: 'מוריד…', + ribbonFontDownloadFailed: 'ההורדה נכשלה — לחץ לניסיון חוזר', + fontMissingBanner: 'מצגת זו משתמשת בגופנים שאינם מותקנים:', + fontMissingDownloadAll: 'הורד הכול', + fontMissingDismiss: 'התעלם', ribbonGroupParagraph: 'פסקה', ribbonCharSpacing: 'ריווח תווים', ribbonChangeCase: 'שינוי רישיות', @@ -12432,6 +12551,13 @@ export const ribbonStrings = defineStrings({ 'लंबी प्रस्तुतियों को व्यवस्थित करने के लिए वर्तमान स्लाइड से पहले अनुभाग जोड़ता है', ribbonGroupFont: 'फ़ॉन्ट', ribbonFontsSystem: 'सिस्टम फ़ॉन्ट', + ribbonFontsDownloadable: 'डाउनलोड योग्य फ़ॉन्ट', + ribbonFontInstallLocal: 'फ़ॉन्ट फ़ाइल इंस्टॉल करें…', + ribbonFontDownloading: 'डाउनलोड हो रहा है…', + ribbonFontDownloadFailed: 'डाउनलोड विफल — पुनः प्रयास के लिए क्लिक करें', + fontMissingBanner: 'यह प्रस्तुति ऐसे फ़ॉन्ट उपयोग करती है जो इंस्टॉल नहीं हैं:', + fontMissingDownloadAll: 'सभी डाउनलोड करें', + fontMissingDismiss: 'खारिज करें', ribbonGroupParagraph: 'अनुच्छेद', ribbonCharSpacing: 'वर्ण रिक्ति', ribbonChangeCase: 'केस बदलें', @@ -13151,6 +13277,13 @@ export const ribbonStrings = defineStrings({ ribbonAddSectionTip: '新增章節(在目前頁前分節,便於組織長簡報)', ribbonGroupFont: '字型', ribbonFontsSystem: '系統字型', + ribbonFontsDownloadable: '可下載字型', + ribbonFontInstallLocal: '安裝字型檔案…', + ribbonFontDownloading: '下載中…', + ribbonFontDownloadFailed: '下載失敗,點擊重試', + fontMissingBanner: '此簡報使用了未安裝的字型:', + fontMissingDownloadAll: '全部下載', + fontMissingDismiss: '忽略', ribbonGroupParagraph: '段落', ribbonCharSpacing: '字元間距', ribbonChangeCase: '變更大小寫', diff --git a/apps/slides/src/renderer/keyboard-actions.ts b/apps/slides/src/renderer/keyboard-actions.ts index 5a9986c..7ab7f49 100644 --- a/apps/slides/src/renderer/keyboard-actions.ts +++ b/apps/slides/src/renderer/keyboard-actions.ts @@ -20,7 +20,11 @@ function inTextField(): boolean { // Shortcuts: ⌘/Ctrl+Z undo, ⇧⌘Z / Ctrl+Y redo, ⌘/Ctrl+=/-/0 zoom, // Delete/Backspace delete selection, arrow keys nudge selection -export function handleGlobalKeydown(ctx: ActionCtx, e: KeyboardEvent): void { +export function handleGlobalKeydown( + ctx: ActionCtx, + e: KeyboardEvent, + platform = navigator.platform, +): void { if (ctx.slideShow || ctx.presenter) return // In show/presenter view: navigation keys are handled by those views const mod = e.metaKey || e.ctrlKey const inField = inTextField() @@ -31,6 +35,22 @@ export function handleGlobalKeydown(ctx: ActionCtx, e: KeyboardEvent): void { showActions.startSlideShow(ctx, !e.shiftKey) return } + // PowerPoint for macOS: ⌘+Enter starts from the current slide. Keep + // Ctrl+Enter untouched on Windows, where PowerPoint uses it to move between + // placeholders (and Shift+F5 already starts from the current slide). + if ( + /mac/i.test(platform) && + e.metaKey && + !e.ctrlKey && + !e.altKey && + !e.shiftKey && + e.key === 'Enter' + ) { + if (e.defaultPrevented || editing || inField || ctx.cropTarget) return + e.preventDefault() + showActions.startSlideShow(ctx, false) + return + } // Undo/redo (menu accelerators normally intercept; fallback for shell/menuless scenarios) if (mod && !e.altKey && (e.key === 'z' || e.key === 'Z')) { if (editing || (inField && !shouldRouteUndoToDeck(e.target as HTMLElement))) return diff --git a/apps/slides/src/renderer/konva-adapter.ts b/apps/slides/src/renderer/konva-adapter.ts index 89039b7..cbcb607 100644 --- a/apps/slides/src/renderer/konva-adapter.ts +++ b/apps/slides/src/renderer/konva-adapter.ts @@ -1018,6 +1018,52 @@ export function duotoneImage( return c } +/** Solid-fill shape with : the (rounded) rect pre-rendered with feathered edges. */ +const featherShapeCache = new Map() +export function featheredShapeCanvas( + color: string, + w: number, + h: number, + radPx: number, + cornerRadiusPx = 0, + stroke?: { color: string; widthPx: number }, +): { canvas: HTMLCanvasElement; pad: number } { + const cw = Math.max(1, Math.round(w)) + const ch = Math.max(1, Math.round(h)) + const rad = Math.max(1, Math.round(radPx)) + // Canvas strokes are centered on the path: pad the canvas so the outer half survives + const pad = stroke ? Math.ceil(stroke.widthPx / 2) : 0 + const key = `${color}|${cw}|${ch}|${rad}|${Math.round(cornerRadiusPx)}|${stroke ? `${stroke.color}/${stroke.widthPx}` : ''}` + let c = featherShapeCache.get(key) + if (!c) { + c = document.createElement('canvas') + c.width = cw + 2 * pad + c.height = ch + 2 * pad + const ctx = c.getContext('2d')! + ctx.beginPath() + if (cornerRadiusPx > 0) ctx.roundRect(pad, pad, cw, ch, cornerRadiusPx) + else ctx.rect(pad, pad, cw, ch) + ctx.fillStyle = normalizeColor(color) + ctx.fill() + // The outline feathers together with the fill (PowerPoint applies softEdge to the composite) + if (stroke) { + ctx.strokeStyle = normalizeColor(stroke.color) + ctx.lineWidth = stroke.widthPx + ctx.stroke() + } + // Same alpha ramp as featheredImage: mask edge at 0.5r inset, blur 0.67r. The ramp + // starts from the composite's outer edge (stroke included) — the full padded canvas — + // so the padded outline half is inside the mask interior, not under its falloff. + ctx.globalCompositeOperation = 'destination-in' + ctx.filter = `blur(${(rad * 2) / 3}px)` + ctx.fillStyle = '#000' + ctx.fillRect(rad * 0.5, rad * 0.5, c.width - rad, c.height - rad) + if (featherShapeCache.size > 100) featherShapeCache.clear() + featherShapeCache.set(key, c) + } + return { canvas: c, pad } +} + export function featheredImage( img: HTMLImageElement, cacheKey: string, @@ -1242,18 +1288,67 @@ export function displayFontFamily(name: string): string { return `'${name}', 'PingFang SC', 'Microsoft YaHei', sans-serif` } +/** + * Distance from a Konva Text node's top to its visible alphabetic baseline, in px. + * + * Konva paints with canvas2d textBaseline='middle' anchored at half the line height + * (lineHeight defaults to 1 → 0.5em below the top). Where the alphabetic baseline sits + * relative to that 'middle' anchor is a per-font quantity Chromium derives from font + * metrics; measuring the same ink from both baselines yields it exactly. The ratio is + * measured once per family/style at a fixed size (font metrics scale linearly) so the + * engine's baselineY can be converted to a Konva top with no per-font error — the old + * fixed 0.8em approximation was tuned for Latin sans and sat several px high for + * CJK/serif fonts, drawing their glyphs visibly below the layout position. + */ +const MEASURE_PX = 100 +let baselineCtx: CanvasRenderingContext2D | null | undefined +const baselineAscentRatio = new Map() +if (typeof document !== 'undefined') + // Web fonts (private/embedded faces) finish loading after first paint; drop ratios + // measured against the fallback font so the next render measures the real face. + document.fonts?.addEventListener?.('loadingdone', () => baselineAscentRatio.clear()) + +export function konvaBaselineAscent( + family: string, + sizePx: number, + bold?: boolean, + italic?: boolean, +): number { + const key = `${family}|${bold ? 'b' : ''}${italic ? 'i' : ''}` + const hit = baselineAscentRatio.get(key) + if (hit !== undefined) return hit * sizePx + if (baselineCtx === undefined) + baselineCtx = + typeof document !== 'undefined' ? document.createElement('canvas').getContext('2d') : null + if (!baselineCtx) return sizePx * 0.8 // no canvas (unit tests): legacy approximation + const ctx = baselineCtx + ctx.font = `${italic ? 'italic ' : ''}${bold ? 'bold ' : ''}${MEASURE_PX}px ${family}` + // Same ink measured from both baselines: the ascent difference IS the exact distance + // from the 'middle' anchor down to the alphabetic baseline for this font + ctx.textBaseline = 'alphabetic' + const a1 = ctx.measureText('Hg').actualBoundingBoxAscent + ctx.textBaseline = 'middle' + const a2 = ctx.measureText('Hg').actualBoundingBoxAscent + const ratio = a1 > 0 || a2 > 0 ? 0.5 + (a1 - a2) / MEASURE_PX : 0.8 + baselineAscentRatio.set(key, ratio) + return ratio * sizePx +} + export function glyphToDraw(run: GlyphRun): GlyphDraw { const styleParts: string[] = [] - if (run.bold && !NO_SYNTHETIC_BOLD.has(run.fontFamily.normalize('NFKC').toLowerCase())) - styleParts.push('bold') + const effectiveBold = + !!run.bold && !NO_SYNTHETIC_BOLD.has(run.fontFamily.normalize('NFKC').toLowerCase()) + if (effectiveBold) styleParts.push('bold') if (run.italic) styleParts.push('italic') + const family = displayFontFamily(run.fontFamily) return { text: run.text, x: run.x, - // Konva Text positions by top; its browser-backed text baseline is closest to 0.8em. - y: run.baselineY - run.fontSizePx * 0.8, + // Konva Text positions by top; place it so the painted baseline lands exactly on the + // engine's baselineY (see konvaBaselineAscent). + y: run.baselineY - konvaBaselineAscent(family, run.fontSizePx, effectiveBold, run.italic), fontSize: run.fontSizePx, - fontFamily: displayFontFamily(run.fontFamily), + fontFamily: family, fill: normalizeColor(run.color), fontStyle: styleParts.join(' ') || 'normal', textDecoration: [run.underline ? 'underline' : '', run.strike ? 'line-through' : ''] diff --git a/apps/slides/src/renderer/styles.css b/apps/slides/src/renderer/styles.css index 63132b2..4c6e630 100644 --- a/apps/slides/src/renderer/styles.css +++ b/apps/slides/src/renderer/styles.css @@ -4523,6 +4523,64 @@ body.vib .slide-list { cursor: default; } +.rb-font-download { + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; +} +.rb-font-download-tag { + font-size: 11px; + color: var(--text-secondary); +} +.rb-font-install-local { + border-top: 1px solid var(--border); + margin-top: 4px; + color: var(--accent); +} + +.font-missing-banner { + display: flex; + align-items: center; + gap: 10px; + padding: 6px 14px; + font-size: 12.5px; + background: var(--accent-soft); + color: var(--text); + border-bottom: 1px solid var(--border); +} +.font-missing-banner .fmb-text { + flex: 1; + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.font-missing-banner .fmb-fonts { + margin-left: 6px; + color: var(--text-secondary); +} +.font-missing-banner button { + border: 1px solid var(--border); + border-radius: 5px; + background: var(--surface); + color: var(--text); + padding: 3px 10px; + font-size: 12px; + cursor: pointer; +} +.font-missing-banner button:hover { + background: var(--hover); +} +.font-missing-banner .fmb-download { + color: var(--accent); + font-weight: 600; +} +.font-missing-banner .fmb-download:disabled { + opacity: 0.6; + cursor: default; +} + .rb-color-underbar { position: absolute; left: -1px; @@ -7297,14 +7355,38 @@ body.show-curtain::after { } } +.ai-clarify-question-head { + display: flex; + align-items: flex-start; + gap: 8px; + margin: 0 0 8px; +} + .ai-clarify-label { + flex: 1; + min-width: 0; font-size: 16px; font-weight: 600; color: var(--color-text-primary); - margin: 0 0 8px; + margin: 0; line-height: 1.4; } +.ai-clarify-multi-badge { + flex-shrink: 0; + margin-top: 2px; + padding: 2px 7px; + color: var(--color-text-secondary); + background: var(--color-bg-subtle); + border: 1px solid var(--color-border-strong); + border-radius: 999px; + font-size: 10px; + font-weight: 600; + line-height: 1.4; + letter-spacing: 0.03em; + white-space: nowrap; +} + .ai-clarify-desc { font-size: 14px; color: var(--color-text-secondary); @@ -7470,24 +7552,34 @@ body.show-curtain::after { margin-left: auto; } -/* 32px filled advance arrow */ +/* Explicit multi-select advance action: text avoids looking like an unlabeled pager. */ .ai-clarify-next { - width: 32px; - height: 32px; + min-width: 82px; + height: 36px; display: inline-flex; align-items: center; justify-content: center; - padding: 0; + gap: 6px; + padding: 0 14px; + font-family: inherit; background: var(--color-btn-primary); color: var(--color-btn-primary-text); border: none; border-radius: 8px; - font-size: 16px; + font-size: 14px; + font-weight: 600; + white-space: nowrap; cursor: pointer; transition: opacity 150ms cubic-bezier(0.4, 0, 0.2, 1); } -.ai-clarify-next:hover { +.ai-clarify-next-arrow { + font-size: 16px; + font-weight: 400; + line-height: 1; +} + +.ai-clarify-next:hover:not(:disabled) { opacity: 0.85; } @@ -7496,9 +7588,14 @@ body.show-curtain::after { box-shadow: 0 0 0 3px var(--slides-focus-ring-soft); } +.ai-clarify-next:disabled { + opacity: 0.38; + cursor: default; +} + .ai-clarify-skip { - height: 29px; - padding: 0 16px; + height: 36px; + padding: 0 14px; font-family: inherit; font-size: 14px; font-weight: 500; @@ -7506,6 +7603,7 @@ body.show-curtain::after { background: transparent; border: 1px solid var(--color-border-default); border-radius: 8px; + white-space: nowrap; cursor: pointer; transition: border-color 150ms cubic-bezier(0.4, 0, 0.2, 1), @@ -7518,7 +7616,7 @@ body.show-curtain::after { } .ai-clarify-submit { - height: 29px; + height: 36px; padding: 0 16px; font-family: inherit; font-size: 14px; @@ -7527,19 +7625,25 @@ body.show-curtain::after { background: var(--color-btn-primary); border: 1px solid transparent; border-radius: 8px; + white-space: nowrap; cursor: pointer; transition: filter 150ms cubic-bezier(0.4, 0, 0.2, 1); } -.ai-clarify-submit:hover { +.ai-clarify-submit:hover:not(:disabled) { filter: brightness(0.92); } -.ai-clarify-submit:active { +.ai-clarify-submit:active:not(:disabled) { filter: brightness(0.88); transform: scale(0.98); } +.ai-clarify-submit:disabled { + opacity: 0.38; + cursor: default; +} + .ai-clarify-skip:focus-visible, .ai-clarify-submit:focus-visible { outline: none; diff --git a/apps/slides/src/shared/ipc.ts b/apps/slides/src/shared/ipc.ts index 358386e..628dd14 100644 --- a/apps/slides/src/shared/ipc.ts +++ b/apps/slides/src/shared/ipc.ts @@ -1183,6 +1183,23 @@ export interface SlidesApi { > /** Single-face sfnt bytes for one private face (null = gone/unreadable) */ privateFontData: (id: string) => Promise + /** Curated downloadable (OFL) font catalog with per-family install state */ + fontCatalog: () => Promise< + Array<{ + family: string + script: 'latin' | 'ja' | 'ko' | 'sc' | 'tc' + installed: boolean + downloading: boolean + }> + > + /** Download a catalog family into the user font store; layouts refresh via deck-changed */ + fontDownload: (family: string) => Promise<{ ok: boolean; error?: string }> + /** File picker → install local font files into the user font store */ + fontInstallLocal: () => Promise<{ families: string[] }> + /** Families this deck references that are missing locally but downloadable */ + fontMissing: () => Promise + /** The user font store changed (download/local install): re-sync private FontFaces */ + onFontsChanged: (handler: () => void) => () => void consumePendingOpen: (fitWidthPx: number) => Promise /** New blank presentation (single blank 16:9 page, untitled) */ newBlank: (fitWidthPx: number) => Promise @@ -1322,6 +1339,8 @@ export interface SlidesApi { ) => Promise<{ slides: RenderSlide[]; index: number; sourceId?: string } | null> /** Is there a slide on the clipboard? (drives the Paste Slide menu item) */ hasSlideClipboard: () => Promise + /** Is there anything a paste would act on (internal elements/slide, or external image/text)? Drives the Paste menu item */ + clipboardProbe: () => Promise /** Delete a slide (refused when only one page remains); returns the full RenderSlide array */ deleteSlide: (slideIndex: number) => Promise /** Bring element to front/back or move one layer forward/backward */ diff --git a/apps/slides/tests/ai-edit-queue.test.ts b/apps/slides/tests/ai-edit-queue.test.ts index da6e7e9..aff7c4e 100644 --- a/apps/slides/tests/ai-edit-queue.test.ts +++ b/apps/slides/tests/ai-edit-queue.test.ts @@ -7,6 +7,7 @@ import { describe, it, expect } from 'vitest' import type { PlacedBox, RenderSlide } from '@genoffice/pptx-render' import { buildPageInstruction, + buildSelectionInstruction, describeNode, groupByPage, resolveQueueItem, @@ -165,6 +166,19 @@ describe('grouping and prompt assembly', () => { expect(prompt).toContain('Quarterly review') expect(prompt).toContain('Break into two lines') }) + + it('freezes Send now to the selected durable id and visible text', () => { + const prompt = buildSelectionInstruction( + 0, + [{ id: 'dur-title', desc: describeNode(title as never) }], + 'Make the font red', + ) + expect(prompt).toContain('slideIndex=0') + expect(prompt).toContain('dur-title') + expect(prompt).toContain('Quarterly review') + expect(prompt).toContain('Make the font red') + expect(prompt).toContain('do not modify unlisted elements') + }) }) describe('describeNode', () => { diff --git a/apps/slides/tests/font-store.test.ts b/apps/slides/tests/font-store.test.ts new file mode 100644 index 0000000..df51461 --- /dev/null +++ b/apps/slides/tests/font-store.test.ts @@ -0,0 +1,200 @@ +import { mkdtempSync, readFileSync, writeFileSync, existsSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest' +import { createBlankPptx, openPptx } from '@genoffice/pptx-engine' + +const storeDir = mkdtempSync(join(tmpdir(), 'font-store-')) +const fontCdnBaseUrl = 'https://fonts.example.test/v1' + +vi.mock('electron', () => ({ + app: { getPath: () => storeDir, getAppPath: () => storeDir, isPackaged: false }, + net: { fetch: vi.fn() }, +})) + +const availability = new Map() +vi.mock('../src/main/fonts', () => ({ + familyAvailable: (f: string) => availability.get(f) ?? false, + fontFileFamilies: (p: string) => { + // The magic gate runs before this; tests hand-label families per path + return p.includes('brand') ? ['Brand Sans'] : ['Test Family'] + }, + setUserFontDir: vi.fn(), +})) + +import { FONT_CATALOG } from '../src/main/font-catalog' +import { + downloadFontFamily, + extractFontCdnBaseUrl, + installLocalFontFiles, + listFontCatalog, + missingCatalogFonts, +} from '../src/main/font-store' +import { net } from 'electron' +import { createHash } from 'node:crypto' + +beforeEach(() => { + availability.clear() + vi.stubEnv('GENOFFICE_FONT_CDN_URL', fontCdnBaseUrl) + vi.mocked(net.fetch).mockReset() +}) +afterEach(() => vi.unstubAllEnvs()) + +describe('font catalog', () => { + it('every family ships regular+bold files with pinned hashes but no endpoint', () => { + expect(FONT_CATALOG.length).toBeGreaterThanOrEqual(15) + for (const fam of FONT_CATALOG) { + const styles = fam.files.map((f) => f.style) + expect(styles).toContain('regular') + expect(styles).toContain('bold') + for (const f of fam.files) { + expect(f.file).toMatch(/\.ttf$/) + expect(f).not.toHaveProperty('url') + expect(f.sha256).toMatch(/^[0-9a-f]{64}$/) + expect(f.bytes).toBeGreaterThan(10_000) + } + } + }) + + it('reports installed state from the registry', () => { + availability.set('Poppins', true) + const list = listFontCatalog() + expect(list.find((e) => e.family === 'Poppins')?.installed).toBe(true) + expect(list.find((e) => e.family === 'Montserrat')?.installed).toBe(false) + }) + + it('hides the downloadable catalog when no CDN URL is configured', () => { + vi.stubEnv('GENOFFICE_FONT_CDN_URL', '') + expect(listFontCatalog()).toEqual([]) + }) + + it('extracts and validates the packaged CDN URL', () => { + expect( + extractFontCdnBaseUrl({ + genofficeFontCdn: { baseUrl: ' https://fonts.example.test/v1/ ' }, + }), + ).toBe(fontCdnBaseUrl) + expect( + extractFontCdnBaseUrl({ genofficeFontCdn: { baseUrl: 'http://fonts.example.test/v1' } }), + ).toBeNull() + expect( + extractFontCdnBaseUrl({ + genofficeFontCdn: { baseUrl: 'https://user@fonts.example.test/v1' }, + }), + ).toBeNull() + expect( + extractFontCdnBaseUrl({ + genofficeFontCdn: { baseUrl: 'https://fonts.example.test/v1?token=secret' }, + }), + ).toBeNull() + }) +}) + +describe('downloadFontFamily', () => { + it('verifies the checksum and writes files into the store', async () => { + const fam = FONT_CATALOG[0]! + const payloads = new Map( + fam.files.map((f) => { + const buf = Buffer.from(`sfnt-bytes-${f.style}`) + const url = new URL(encodeURIComponent(f.file), `${fontCdnBaseUrl}/`).toString() + return [url, buf] as const + }), + ) + // Re-pin hashes to the fake payloads for the test + for (const f of fam.files) { + const url = new URL(encodeURIComponent(f.file), `${fontCdnBaseUrl}/`).toString() + f.sha256 = createHash('sha256').update(payloads.get(url)!).digest('hex') + } + vi.mocked(net.fetch).mockImplementation(async (url: unknown) => { + const buf = payloads.get(String(url))! + return new Response(new Uint8Array(buf), { status: 200 }) + }) + await downloadFontFamily(fam.family) + for (const f of fam.files) { + const p = join(storeDir, 'fonts', f.file) + expect(existsSync(p)).toBe(true) + expect(readFileSync(p).toString()).toContain('sfnt-bytes') + } + }) + + it('rejects a checksum mismatch', async () => { + const fam = FONT_CATALOG[1]! + vi.mocked(net.fetch).mockResolvedValue( + new Response(new Uint8Array(Buffer.from('tampered')), { status: 200 }), + ) + await expect(downloadFontFamily(fam.family)).rejects.toThrow(/checksum/) + }) + + it('rejects unknown families', async () => { + await expect(downloadFontFamily('Meiryo UI')).rejects.toThrow(/not in catalog/) + }) + + it('rejects downloads when no CDN URL is configured', async () => { + vi.stubEnv('GENOFFICE_FONT_CDN_URL', '') + await expect(downloadFontFamily(FONT_CATALOG[0]!.family)).rejects.toThrow(/unavailable/) + expect(net.fetch).not.toHaveBeenCalled() + }) +}) + +describe('installLocalFontFiles', () => { + it('accepts sfnt files, renames to the family, and skips non-fonts', () => { + const src = join(storeDir, 'brand_v2_final.ttf') + writeFileSync(src, Buffer.concat([Buffer.from([0, 1, 0, 0]), Buffer.from('x'.repeat(64))])) + const junk = join(storeDir, 'junk.ttf') + writeFileSync(junk, Buffer.from('MZ not a font')) + const families = installLocalFontFiles([src, junk]) + expect(families).toEqual(['Brand Sans']) + expect(existsSync(join(storeDir, 'fonts', 'Brand Sans.ttf'))).toBe(true) + }) +}) + +describe('missingCatalogFonts', () => { + it('reports deck-referenced catalog families that are unavailable, including table cells', async () => { + const { deck } = await openPptx(await createBlankPptx()) + const slide = deck.slides[0]! + ;(slide.elements as unknown[]).push( + { + type: 'text', + text: { + paragraphs: [ + { runs: [{ fontFamily: 'Poppins' }, { fontFamily: 'Arial' }] }, + { runs: [{ fontFamily: 'Montserrat' }] }, + ], + }, + }, + { + type: 'table', + rows: [[{ text: { paragraphs: [{ runs: [{ fontFamily: 'Rubik' }] }] } }]], + }, + ) + availability.set('Montserrat', true) + const missing = missingCatalogFonts({ deck } as never) + expect(missing).toEqual(['Poppins', 'Rubik']) + + vi.stubEnv('GENOFFICE_FONT_CDN_URL', '') + expect(missingCatalogFonts({ deck } as never)).toEqual([]) + }) +}) + +describe('concurrent downloads', () => { + it('a second call joins the in-flight download instead of resolving early', async () => { + const fam = FONT_CATALOG[2]! + let releaseFetch: (() => void) | null = null + const gate = new Promise((res) => (releaseFetch = res)) + const payload = Buffer.from('slow-bytes') + for (const f of fam.files) f.sha256 = createHash('sha256').update(payload).digest('hex') + vi.mocked(net.fetch).mockImplementation(async () => { + await gate + return new Response(new Uint8Array(payload), { status: 200 }) + }) + const first = downloadFontFamily(fam.family) + const second = downloadFontFamily(fam.family) + let secondDone = false + void second.then(() => (secondDone = true)) + await new Promise((r) => setTimeout(r, 20)) + expect(secondDone).toBe(false) + releaseFetch!() + await Promise.all([first, second]) + expect(secondDone).toBe(true) + }) +}) diff --git a/apps/slides/tests/generated-page-temp.test.ts b/apps/slides/tests/generated-page-temp.test.ts index 4b6843b..4686caa 100644 --- a/apps/slides/tests/generated-page-temp.test.ts +++ b/apps/slides/tests/generated-page-temp.test.ts @@ -18,6 +18,7 @@ afterEach(async () => { async function tempRoot(): Promise { const root = await mkdtemp(join(tmpdir(), 'genoffice-slides-temp-test-')) + roots.push(root) return root } @@ -37,12 +38,13 @@ describe('generated-page temp sweep', () => { const expiredOwned1 = join(cloudDir, `${expiredUuid1}.pptx`) const expiredOwned2 = join(localDir, `${expiredUuid2}.pptx`) const recentOwned = join(localDir, `${recentUuid}.pptx`) - const _arbitrary = join(localDir, 'random-file.txt') + const arbitrary = join(localDir, 'random-file.txt') await Promise.all([ writeFile(expiredOwned1, 'old-expired'), writeFile(expiredOwned2, 'old-expired'), writeFile(recentOwned, 'recent'), + writeFile(arbitrary, 'unrelated'), ]) // Use the TTL constant for cutoff @@ -55,13 +57,15 @@ describe('generated-page temp sweep', () => { const removed = await cleanupExpiredGeneratedPages(root, now) - expect(removed).toEqual(expect.arrayContaining([expect.stringMatching(/\.pptx$/)])) + expect(removed).toEqual(expect.arrayContaining([expiredOwned1, expiredOwned2])) + expect(removed).toHaveLength(2) // Expired files should be gone for (const path of removed) { expect(existsSync(path)).toBe(false) } // Non-expired and non-matching files should remain (not deleted) expect(existsSync(recentOwned)).toBe(true) + expect(existsSync(arbitrary)).toBe(true) }) it('skips directories and non-.pptx files', async () => { diff --git a/apps/slides/tests/keyboard-actions.test.ts b/apps/slides/tests/keyboard-actions.test.ts index 865e61d..3540d34 100644 --- a/apps/slides/tests/keyboard-actions.test.ts +++ b/apps/slides/tests/keyboard-actions.test.ts @@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import { handleGlobalKeydown } from '../src/renderer/keyboard-actions' import * as clipboardActions from '../src/renderer/clipboard-actions' import * as slideActions from '../src/renderer/slide-actions' +import * as showActions from '../src/renderer/show-actions' import type { ActionCtx } from '../src/renderer/action-context' vi.mock('../src/renderer/clipboard-actions', () => ({ @@ -50,6 +51,76 @@ function selectText(): void { sel.addRange(range) } +describe('slide show shortcuts', () => { + beforeEach(() => { + vi.clearAllMocks() + document.body.innerHTML = '' + }) + + it('starts from the current slide with Command+Enter on macOS', () => { + const ctx = makeCtx({ current: 2, slides: [{}, {}, {}] }) + const e = keydown('Enter') + + handleGlobalKeydown(ctx, e, 'MacIntel') + + expect(e.defaultPrevented).toBe(true) + expect(showActions.startSlideShow).toHaveBeenCalledWith(ctx, false) + }) + + it('starts from the current slide with Shift+F5 on Windows', () => { + const ctx = makeCtx({ current: 2, slides: [{}, {}, {}] }) + const e = keydown('F5', { metaKey: false, shiftKey: true }) + + handleGlobalKeydown(ctx, e, 'Win32') + + expect(e.defaultPrevented).toBe(true) + expect(showActions.startSlideShow).toHaveBeenCalledWith(ctx, false) + }) + + it('does not repurpose Ctrl+Enter on Windows', () => { + const ctx = makeCtx() + const e = keydown('Enter', { metaKey: false, ctrlKey: true }) + + handleGlobalKeydown(ctx, e, 'Win32') + + expect(e.defaultPrevented).toBe(false) + expect(showActions.startSlideShow).not.toHaveBeenCalled() + }) + + it('does not start a show from a text field on macOS', () => { + const input = document.createElement('textarea') + document.body.appendChild(input) + input.focus() + const ctx = makeCtx() + const e = keydown('Enter') + + handleGlobalKeydown(ctx, e, 'MacIntel') + + expect(e.defaultPrevented).toBe(false) + expect(showActions.startSlideShow).not.toHaveBeenCalled() + }) + + it('does not start a show when another handler consumed Enter', () => { + const ctx = makeCtx() + const e = keydown('Enter') + e.preventDefault() + + handleGlobalKeydown(ctx, e, 'MacIntel') + + expect(showActions.startSlideShow).not.toHaveBeenCalled() + }) + + it('does not start a show while confirming a crop', () => { + const ctx = makeCtx({ cropTarget: {} }) + const e = keydown('Enter') + + handleGlobalKeydown(ctx, e, 'MacIntel') + + expect(e.defaultPrevented).toBe(false) + expect(showActions.startSlideShow).not.toHaveBeenCalled() + }) +}) + describe('copy shortcuts with a DOM text selection', () => { beforeEach(() => { vi.clearAllMocks() diff --git a/apps/slides/tests/page-spec.test.ts b/apps/slides/tests/page-spec.test.ts index 77936a7..6149c55 100644 --- a/apps/slides/tests/page-spec.test.ts +++ b/apps/slides/tests/page-spec.test.ts @@ -5,6 +5,7 @@ */ import { describe, it, expect } from 'vitest' import { openPptx, type TextElement, type PictureElement } from '@genoffice/pptx-engine' +import { HeuristicMetrics } from '@genoffice/pptx-render' import { parsePageSpec, buildPagePptx, type PageSpec } from '../src/main/page-spec' // 1x1 red PNG @@ -157,3 +158,73 @@ describe('buildPagePptx', () => { expect(pics[0]!.srcRect?.l ?? 0).toBeGreaterThan(0) }) }) + +describe('buildPagePptx text-box height fix', () => { + const deps = { fetchImage: async () => null, fontMetrics: new HeuristicMetrics() } + const EMU_PER_PX = 9525 + const px = (emu: number) => emu / EMU_PER_PX + const bigTitle = (extra: Record = {}) => ({ + type: 'text' as const, + x: 100, + y: 100, + w: 600, + h: 30, + paragraphs: [{ runs: [{ text: '成都来了就不想走', sizePt: 40, bold: true }] }], + ...extra, + }) + // 40pt = 53.33px glyphs on the default 1.2em line box → one line ≈ 64px + const oneLinePx = 40 * (96 / 72) * 1.2 + + it('grows an undersized top-anchored box to the measured content height', async () => { + const { bytes } = await buildPagePptx({ elements: [bigTitle()] }, deps) + const opened = await openPptx(bytes) + const el = opened.deck.slides[0]!.elements.find((e): e is TextElement => e.type === 'text')! + expect(px(el.transform.offset.cy)).toBeCloseTo(oneLinePx, 0) + // Top anchor: the box only grows downward, glyphs don't move + expect(el.transform.offset.y).toBe(100 * EMU_PER_PX) + }) + + it('shifts a middle-anchored box up so the rendered glyphs stay in place', async () => { + const { bytes } = await buildPagePptx({ elements: [bigTitle({ valign: 'middle' })] }, deps) + const opened = await openPptx(bytes) + const el = opened.deck.slides[0]!.elements.find((e): e is TextElement => e.type === 'text')! + expect(px(el.transform.offset.cy)).toBeCloseTo(oneLinePx, 0) + expect(px(el.transform.offset.y)).toBeCloseTo(100 - (oneLinePx - 30) / 2, 0) + }) + + it('leaves tall-enough boxes and undersized shape labels untouched', async () => { + const spec: PageSpec = { + elements: [ + bigTitle({ h: 100 }), + { + type: 'shape', + shape: 'roundRect', + x: 100, + y: 400, + w: 600, + h: 30, + fill: '#FFFFFF', + paragraphs: [{ runs: [{ text: '成都来了就不想走', sizePt: 40 }] }], + }, + ], + } + const { bytes } = await buildPagePptx(spec, deps) + const opened = await openPptx(bytes) + const slide = opened.deck.slides[0]! + const text = slide.elements.find((e): e is TextElement => e.type === 'text')! + const shape = slide.elements.find((e) => e.type === 'shape')! + // Content (≈64px) fits the 100px box → no change; shape height is design intent + expect(text.transform.offset.cy).toBe(100 * EMU_PER_PX) + expect(shape.transform.offset.cy).toBe(30 * EMU_PER_PX) + }) + + it('skips the fix entirely when no font metrics are injected', async () => { + const { bytes } = await buildPagePptx( + { elements: [bigTitle()] }, + { fetchImage: async () => null }, + ) + const opened = await openPptx(bytes) + const el = opened.deck.slides[0]!.elements.find((e): e is TextElement => e.type === 'text')! + expect(el.transform.offset.cy).toBe(30 * EMU_PER_PX) + }) +}) diff --git a/apps/slides/tests/read-colors.test.ts b/apps/slides/tests/read-colors.test.ts index d61300d..9ccc2e1 100644 --- a/apps/slides/tests/read-colors.test.ts +++ b/apps/slides/tests/read-colors.test.ts @@ -86,10 +86,10 @@ const slideOf = (nodes: RenderNode[]): RenderSlide => ({ nodes, }) -const access = (slide: RenderSlide): DeckAccess => ({ +const access = (slide: RenderSlide, selectedIds: string[] = []): DeckAccess => ({ getSlides: () => [slide], getCurrent: () => 0, - getSelectedIds: () => [], + getSelectedIds: () => selectedIds, applySlide: () => {}, applyDeck: () => {}, fitWidthPx: 1280, @@ -158,6 +158,27 @@ describe('deck outline main fills', () => { const skill = createSlidesSkill(access(slide)) expect(skill.buildContext!()).not.toContain('main fills') }) + + it('reports a canvas source-id selection with the durable id shown in the outline', () => { + const title = { + ...shape('source-title', box(0, 0, 500, 100), { + runs: [{ text: 'Main title', color: '#FFFFFF' }], + }), + durableId: 'e_title', + } + const subtitle = { + ...shape('source-subtitle', box(0, 120, 500, 60), { + runs: [{ text: 'Subtitle below', color: '#FFFFFF' }], + }), + durableId: 'e_subtitle', + } + const outline = createSlidesSkill(access(slideOf([title, subtitle]), ['source-title'])) + .buildContext!() + expect(outline).toContain('User selected elements: e_title') + expect(outline).not.toContain('User selected elements: source-title') + expect(outline).toContain('- e_title | shape | "Main title"') + expect(outline).toContain('- e_subtitle | shape | "Subtitle below"') + }) }) describe('layout-script els colors', () => { diff --git a/apps/slides/tests/slide-qc.test.ts b/apps/slides/tests/slide-qc.test.ts index c18cc5d..f48a6af 100644 --- a/apps/slides/tests/slide-qc.test.ts +++ b/apps/slides/tests/slide-qc.test.ts @@ -14,6 +14,7 @@ import { qcSlidePage, settingsSupportVision, } from '../src/renderer/ai/slide-qc' +import { defaultAiSettings, type AiProviderId } from '@genoffice/ai-provider' import type { DeckAccess } from '../src/renderer/ai/slides-skill' const access: DeckAccess = { @@ -103,10 +104,22 @@ describe('isQcEnabled', () => { }) describe('vision capability fallback', () => { - it('does not send screenshots to providers marked as text-only', () => { - expect(settingsSupportVision({ provider: 'deepseek' })).toBe(false) - expect(settingsSupportVision({ provider: 'glm' })).toBe(false) - expect(settingsSupportVision({ provider: 'gemini' })).toBe(true) + it('uses the selected model when a provider mixes text and vision models', () => { + const withProvider = (provider: AiProviderId) => ({ ...defaultAiSettings(), provider }) + const deepseek = withProvider('deepseek') + expect(settingsSupportVision(deepseek)).toBe(false) + deepseek.providers.deepseek.model = 'deepseek-v4-flash-vision-exp' + expect(settingsSupportVision(deepseek)).toBe(true) + expect(settingsSupportVision(withProvider('glm'))).toBe(false) + expect(settingsSupportVision(withProvider('gemini'))).toBe(true) + }) + + it('does not send screenshots to text-only models under a vision-capable provider', () => { + const settings = defaultAiSettings() + settings.providers.genspark.model = 'deep-seek-v4-flash' + expect(settingsSupportVision(settings)).toBe(false) + settings.providers.genspark.model = 'claude-opus-4-7' + expect(settingsSupportVision(settings)).toBe(true) }) it('recognizes image-capability errors from optimistic custom endpoints', () => { diff --git a/apps/slides/tests/slide-script.test.ts b/apps/slides/tests/slide-script.test.ts index 0e4d9e0..49fe266 100644 --- a/apps/slides/tests/slide-script.test.ts +++ b/apps/slides/tests/slide-script.test.ts @@ -345,6 +345,63 @@ describe('runLayoutScript security boundary', () => { ]) expect(runLayoutScript(code, els, canvas).error).toBeTruthy() }) + + it('bounds catastrophic-backtracking regex instead of freezing', () => { + const r = runLayoutScript( + ` + let long = ''; + for (let i = 0; i < 200; i++) long += 'aaaaaaaaaa'; + return /(a+)+$/.test(long + 'b'); + `, + els, + canvas, + ) + expect(r.error).toMatch(/execution budget/) + expect(r.ops).toEqual([]) + }) + + it('supports the common regex subset with bounded execution', () => { + const cases: Array<[string, string]> = [ + [`return /^(shape|text)$/.test(els[0].type);`, 'true'], + [`return /TITLE/i.test(els[0].text);`, 'true'], + [`return /\\d{2,4}/.test('year 2026');`, 'true'], + [`return /[a-z]+_[0-9]+/.test('shape_12');`, 'true'], + [`return /^\\s*$/.test(' ');`, 'true'], + [`return /b.t/s.test('b\\nt') && /^t/m.test('a\\nt');`, 'true'], + [`return /(a?)*$/.test('b');`, 'true'], + [`return /xyz/.test(els[0].text);`, 'false'], + ] + for (const [code, expected] of cases) { + const r = runLayoutScript(code, els, canvas) + expect(r.error).toBeUndefined() + expect(r.returned).toBe(expected) + } + }) + + it('scans long text and honors start anchors without exhausting the budget', () => { + const r = runLayoutScript( + ` + let long = ''; + for (let i = 0; i < 500; i++) long += 'lorem ipsum '; + return /needle|dolor$/.test(long + 'needle') && /^lorem/.test(long) && !/^needle/.test(long); + `, + els, + canvas, + ) + expect(r.error).toBeUndefined() + expect(r.returned).toBe('true') + }) + + it('rejects regex features that cannot be bounded', () => { + for (const code of [ + `return /(a)\\1/.test('aa');`, + `return /a(?=b)/.test('ab');`, + `return /(?<=a)b/.test('ab');`, + `return /\\p{L}/u.test('a');`, + `return /a/y.test('a');`, + ]) + expect(runLayoutScript(code, els, canvas).error).toBeTruthy() + }) }) // ── execute_slide_script tool chain ──────────────────────────── diff --git a/apps/slides/tests/system-fonts.test.ts b/apps/slides/tests/system-fonts.test.ts index 133b8cc..6f6a1ba 100644 --- a/apps/slides/tests/system-fonts.test.ts +++ b/apps/slides/tests/system-fonts.test.ts @@ -83,6 +83,11 @@ describe.runIf(mac)( expect(ui).toBeLessThan(plain * 0.85) }) + it.runIf(office)('Yu Gothic UI picks the UI face out of YuGothM/YuGothB.ttc', () => { + expect(m.displayFamily!(style('Yu Gothic UI'))).toBe('Yu Gothic UI') + expect(m.displayFamily!(style('Yu Gothic UI', { bold: true }))).toMatch(/Yu Gothic UI/) + }) + it.runIf(office)('legacy Korean fonts (Gulim/Batang) parse via layout-table strip', () => { expect(m.displayFamily!(style('굴림'))).toMatch(/Gulim/) expect(m.displayFamily!(style('바탕'))).toMatch(/Batang/) diff --git a/e2e/assets/font-manager-rubik/[Content_Types].xml b/e2e/assets/font-manager-rubik/[Content_Types].xml new file mode 100644 index 0000000..c51e34c --- /dev/null +++ b/e2e/assets/font-manager-rubik/[Content_Types].xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/e2e/assets/font-manager-rubik/_rels/.rels b/e2e/assets/font-manager-rubik/_rels/.rels new file mode 100644 index 0000000..b75589a --- /dev/null +++ b/e2e/assets/font-manager-rubik/_rels/.rels @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/e2e/assets/font-manager-rubik/ppt/_rels/presentation.xml.rels b/e2e/assets/font-manager-rubik/ppt/_rels/presentation.xml.rels new file mode 100644 index 0000000..e5a51ae --- /dev/null +++ b/e2e/assets/font-manager-rubik/ppt/_rels/presentation.xml.rels @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/e2e/assets/font-manager-rubik/ppt/presentation.xml b/e2e/assets/font-manager-rubik/ppt/presentation.xml new file mode 100644 index 0000000..1b9299b --- /dev/null +++ b/e2e/assets/font-manager-rubik/ppt/presentation.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/e2e/assets/font-manager-rubik/ppt/slideLayouts/_rels/slideLayout1.xml.rels b/e2e/assets/font-manager-rubik/ppt/slideLayouts/_rels/slideLayout1.xml.rels new file mode 100644 index 0000000..69d8ac5 --- /dev/null +++ b/e2e/assets/font-manager-rubik/ppt/slideLayouts/_rels/slideLayout1.xml.rels @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/e2e/assets/font-manager-rubik/ppt/slideLayouts/slideLayout1.xml b/e2e/assets/font-manager-rubik/ppt/slideLayouts/slideLayout1.xml new file mode 100644 index 0000000..51a20c2 --- /dev/null +++ b/e2e/assets/font-manager-rubik/ppt/slideLayouts/slideLayout1.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/e2e/assets/font-manager-rubik/ppt/slideMasters/_rels/slideMaster1.xml.rels b/e2e/assets/font-manager-rubik/ppt/slideMasters/_rels/slideMaster1.xml.rels new file mode 100644 index 0000000..98abcba --- /dev/null +++ b/e2e/assets/font-manager-rubik/ppt/slideMasters/_rels/slideMaster1.xml.rels @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/e2e/assets/font-manager-rubik/ppt/slideMasters/slideMaster1.xml b/e2e/assets/font-manager-rubik/ppt/slideMasters/slideMaster1.xml new file mode 100644 index 0000000..6df6a70 --- /dev/null +++ b/e2e/assets/font-manager-rubik/ppt/slideMasters/slideMaster1.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/e2e/assets/font-manager-rubik/ppt/slides/_rels/slide1.xml.rels b/e2e/assets/font-manager-rubik/ppt/slides/_rels/slide1.xml.rels new file mode 100644 index 0000000..593c255 --- /dev/null +++ b/e2e/assets/font-manager-rubik/ppt/slides/_rels/slide1.xml.rels @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/e2e/assets/font-manager-rubik/ppt/slides/slide1.xml b/e2e/assets/font-manager-rubik/ppt/slides/slide1.xml new file mode 100644 index 0000000..b283735 --- /dev/null +++ b/e2e/assets/font-manager-rubik/ppt/slides/slide1.xml @@ -0,0 +1,2 @@ + +Rubik headline for font manager smoke \ No newline at end of file diff --git a/e2e/assets/font-manager-rubik/ppt/theme/theme1.xml b/e2e/assets/font-manager-rubik/ppt/theme/theme1.xml new file mode 100644 index 0000000..fd50451 --- /dev/null +++ b/e2e/assets/font-manager-rubik/ppt/theme/theme1.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/e2e/pdf-fit-zoom.spec.ts b/e2e/pdf-fit-zoom.spec.ts new file mode 100644 index 0000000..5c7ee7d --- /dev/null +++ b/e2e/pdf-fit-zoom.spec.ts @@ -0,0 +1,54 @@ +import { test, expect } from '@playwright/test' +import { mkdtemp, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { launchShell, waitForPageWithUrl, closeAndSaveVideo } from './helpers' + +/** Single empty page at a photo-scan size (3024×4032 pt), like image-based PDFs */ +function hugePagePdf(): Buffer { + const objects = [ + '<>', + '<>', + '<>', + ] + let body = '%PDF-1.4\n' + const offsets: number[] = [] + objects.forEach((obj, i) => { + offsets.push(body.length) + body += `${i + 1} 0 obj\n${obj}\nendobj\n` + }) + const xrefStart = body.length + body += `xref\n0 ${objects.length + 1}\n0000000000 65535 f \n` + for (const off of offsets) body += `${String(off).padStart(10, '0')} 00000 n \n` + body += `trailer\n<>\nstartxref\n${xrefStart}\n%%EOF\n` + return Buffer.from(body, 'latin1') +} + +test('image-sized PDF opens at true fit-to-width, not the old 50% zoom floor', async () => { + const dir = await mkdtemp(join(tmpdir(), 'genoffice-pdf-fit-')) + const pdfPath = join(dir, 'huge.pdf') + await writeFile(pdfPath, hugePagePdf()) + + const launched = await launchShell({ + onboardingSeen: true, + videoDir: 'pdf-fit-zoom', + openFile: pdfPath, + }) + try { + const editorPage = await waitForPageWithUrl(launched.app, 'pdf/out') + await expect(editorPage.locator('.pdf-page').first()).toBeVisible() + + await expect + .poll(async () => Number(await editorPage.locator('.zoom-slider').inputValue())) + .toBeLessThan(50) + + const pageW = await editorPage + .locator('.pdf-page') + .first() + .evaluate((el) => el.getBoundingClientRect().width) + const viewW = await editorPage.locator('.pdf-scroll').evaluate((el) => el.clientWidth) + expect(pageW).toBeLessThanOrEqual(viewW) + } finally { + await closeAndSaveVideo(launched, 'pdf-fit-zoom') + } +}) diff --git a/e2e/sheets-csv-export.spec.ts b/e2e/sheets-csv-export.spec.ts new file mode 100644 index 0000000..6460472 --- /dev/null +++ b/e2e/sheets-csv-export.spec.ts @@ -0,0 +1,61 @@ +import { test, expect } from '@playwright/test' +import { existsSync } from 'node:fs' +import { copyFile, mkdtemp, readFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join, resolve } from 'node:path' +import type { Page } from '@playwright/test' +import { launchShell, closeAndSaveVideo, waitForPageWithUrl } from './helpers' + +const FIXTURE = resolve(__dirname, '../apps/sheets/fixtures/generated/compatibility-basic.xlsx') + +async function waitForWorkbook(page: Page): Promise { + await page.waitForFunction(() => document.body.textContent?.includes('Sheet1'), null, { + timeout: 30_000, + }) + await page.waitForTimeout(1_500) +} + +test.describe('sheets: export the active sheet as CSV', () => { + test('File > Export CSV writes a BOM-prefixed CSV of the grid', async () => { + const scratch = await mkdtemp(join(tmpdir(), 'genoffice-csv-e2e-')) + const workbook = join(scratch, 'export-source.xlsx') + const target = join(scratch, 'exported.csv') + await copyFile(FIXTURE, workbook) + + const launched = await launchShell({ + onboardingSeen: true, + videoDir: 'sheets-csv-export', + openFile: workbook, + }) + try { + const sheets = await waitForPageWithUrl(launched.app, 'sheets/out') + await waitForWorkbook(sheets) + + // Stub the native dialogs: pick the target path, answer "Continue as + // CSV" (button index 1) if the formula-loss warning appears. + await launched.app.evaluate(({ dialog }, csvPath) => { + dialog.showSaveDialog = async () => ({ canceled: false, filePath: csvPath }) + dialog.showMessageBox = (async () => ({ response: 1, checkboxChecked: false })) as never + }, target) + + // The export refuses politely until the workbook preload finishes, so + // keep re-sending the menu action until the file lands. + await expect(async () => { + await launched.app.evaluate(({ webContents }) => { + const wc = webContents.getAllWebContents().find((w) => w.getURL().includes('sheets/out')) + wc?.send('menu:action', 'export-csv') + }) + expect(existsSync(target)).toBe(true) + }).toPass({ timeout: 30_000, intervals: [2_000] }) + + const bytes = await readFile(target) + expect([bytes[0], bytes[1], bytes[2]]).toEqual([0xef, 0xbb, 0xbf]) + const text = bytes.subarray(3).toString('utf8') + // the fixture's numeric cell renders as-is, rows end with CRLF + expect(text).toContain('10') + expect(text).toContain('\r\n') + } finally { + await closeAndSaveVideo(launched, 'sheets-csv-export') + } + }) +}) diff --git a/e2e/sheets-csv-inplace-save.spec.ts b/e2e/sheets-csv-inplace-save.spec.ts new file mode 100644 index 0000000..1cd4f09 --- /dev/null +++ b/e2e/sheets-csv-inplace-save.spec.ts @@ -0,0 +1,79 @@ +import { test, expect } from '@playwright/test' +import { mkdtemp, readFile, writeFile } from 'node:fs/promises' +import { readdirSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import type { Page } from '@playwright/test' +import { launchShell, closeAndSaveVideo, waitForPageWithUrl } from './helpers' + +async function waitForWorkbook(page: Page): Promise { + await page.waitForFunction(() => document.body.textContent?.includes('Sheet1'), null, { + timeout: 30_000, + }) + await page.waitForTimeout(1_500) +} + +/** center of cell A1: right of the ~46px row header, below the ~24px column header */ +async function cellA1(page: Page): Promise<{ x: number; y: number }> { + const grid = await page.evaluate(() => { + for (const canvas of document.querySelectorAll('canvas')) { + const rect = canvas.getBoundingClientRect() + if (rect.width > 500 && rect.height > 300) return { x: rect.x, y: rect.y } + } + return null + }) + if (!grid) throw new Error('worksheet canvas not found') + return { x: grid.x + 46 + 43, y: grid.y + 24 + 12 } +} + +test.describe('sheets: a CSV keeps its identity through Save', () => { + test('Cmd+S writes the edit back to the original .csv', async () => { + const scratch = await mkdtemp(join(tmpdir(), 'genoffice-csv-save-e2e-')) + const csvSource = join(scratch, 'data.csv') + await writeFile(csvSource, 'name,amount\r\nalpha,10\r\n') + + const launched = await launchShell({ + onboardingSeen: true, + videoDir: 'sheets-csv-inplace-save', + openFile: csvSource, + }) + try { + const sheets = await waitForPageWithUrl(launched.app, 'sheets/out') + await waitForWorkbook(sheets) + + const a1 = await cellA1(sheets) + await sheets.mouse.click(a1.x, a1.y) + await expect(sheets.locator('.name-box')).toHaveValue('A1') + await sheets.keyboard.type('Hello', { delay: 50 }) + await sheets.keyboard.press('Enter') + + // Answer the "keep this format?" question with the default: Continue as CSV. + await launched.app.evaluate(({ dialog }) => { + dialog.showMessageBox = (async () => ({ + response: 0, + checkboxChecked: false, + })) as never + }) + + // Save may be refused politely until the preload finishes — retry. + await expect(async () => { + await launched.app.evaluate(({ webContents }) => { + const wc = webContents.getAllWebContents().find((w) => w.getURL().includes('sheets/out')) + wc?.send('menu:action', 'save') + }) + const bytes = await readFile(csvSource) + expect(bytes.subarray(3).toString('utf8')).toContain('Hello') + }).toPass({ timeout: 30_000, intervals: [2_000] }) + + const bytes = await readFile(csvSource) + expect([bytes[0], bytes[1], bytes[2]]).toEqual([0xef, 0xbb, 0xbf]) + const text = bytes.subarray(3).toString('utf8') + expect(text).toContain('Hello,amount') + expect(text).toContain('alpha,10') + // No Save As detour: the folder holds only the .csv, no stray .xlsx. + expect(readdirSync(scratch)).toEqual(['data.csv']) + } finally { + await closeAndSaveVideo(launched, 'sheets-csv-inplace-save') + } + }) +}) diff --git a/e2e/sheets-insert-gallery.spec.ts b/e2e/sheets-insert-gallery.spec.ts index cc3766a..0bc82a7 100644 --- a/e2e/sheets-insert-gallery.spec.ts +++ b/e2e/sheets-insert-gallery.spec.ts @@ -28,9 +28,9 @@ async function gridOrigin(page: Page): Promise<{ x: number; y: number }> { return grid } -/** center of a cell: ~46px row header, ~24px column header, ~86px × ~23px cells */ +/** center of a cell: ~46px row header, ~24px column header, ~74px × ~23px cells */ function cellPoint(origin: { x: number; y: number }, row: number, column: number) { - return { x: origin.x + 46 + column * 86 + 43, y: origin.y + 24 + row * 23 + 11 } + return { x: origin.x + 46 + column * 74 + 37, y: origin.y + 24 + row * 23 + 11 } } test.describe('sheets: Insert → Recommended Charts and Icons', () => { diff --git a/e2e/sheets-insert-tier2.spec.ts b/e2e/sheets-insert-tier2.spec.ts index 50fab2a..b309bfe 100644 --- a/e2e/sheets-insert-tier2.spec.ts +++ b/e2e/sheets-insert-tier2.spec.ts @@ -35,9 +35,9 @@ async function gridOrigin(page: Page): Promise<{ x: number; y: number }> { return grid } -/** center of a cell: ~46px row header, ~24px column header, ~86px × ~23px cells */ +/** center of a cell: ~46px row header, ~24px column header, ~74px × ~23px cells */ function cellPoint(origin: { x: number; y: number }, row: number, column: number) { - return { x: origin.x + 46 + column * 86 + 43, y: origin.y + 24 + row * 23 + 11 } + return { x: origin.x + 46 + column * 74 + 37, y: origin.y + 24 + row * 23 + 11 } } test.describe('sheets: Insert → Equation and Checkbox', () => { diff --git a/e2e/sheets-ribbon-batch.spec.ts b/e2e/sheets-ribbon-batch.spec.ts index c9b32a6..b5ca8a9 100644 --- a/e2e/sheets-ribbon-batch.spec.ts +++ b/e2e/sheets-ribbon-batch.spec.ts @@ -139,7 +139,7 @@ test.describe('sheets: ribbon batch-2 features', () => { const status = sheets.locator('.workbook-status') const origin = await gridOrigin(sheets) const cell = (column: number, row: number): { x: number; y: number } => ({ - x: origin.x + 87 * column + 43, + x: origin.x + 74 * column + 37, y: origin.y + 24 * row + 12, }) diff --git a/e2e/slides-font-manager.spec.ts b/e2e/slides-font-manager.spec.ts new file mode 100644 index 0000000..57e7308 --- /dev/null +++ b/e2e/slides-font-manager.spec.ts @@ -0,0 +1,87 @@ +import { test, expect } from '@playwright/test' +import { execFileSync } from 'node:child_process' +import { mkdtemp } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join, resolve } from 'node:path' +import { launchShell, closeAndSaveVideo, waitForPageWithUrl } from './helpers' + +/** + * The fixture deck (one slide whose only run uses the catalog font Rubik) is + * kept as plain-text OOXML parts under assets/font-manager-rubik/ so no binary + * lives in the repo; zip them into a real .pptx at test time. + */ +async function buildRubikFixture(): Promise { + const out = join( + await mkdtemp(join(tmpdir(), 'genoffice-font-manager-')), + 'font-manager-rubik.pptx', + ) + execFileSync('zip', ['-X', '-q', '-r', out, '.'], { + cwd: resolve(__dirname, 'assets/font-manager-rubik'), + }) + return out +} + +/** + * Font manager smoke: source builds without an injected font CDN hide download + * prompts. Providing GENOFFICE_FONT_CDN_URL enables the catalog/banner; the + * actual network download runs only with E2E_FONT_CDN=1. + */ +test('font download UI follows CDN configuration', async () => { + test.setTimeout(180_000) + const launched = await launchShell({ + onboardingSeen: true, + videoDir: 'slides-font-manager', + openFile: await buildRubikFixture(), + }) + try { + const editorPage = await waitForPageWithUrl(launched.app, 'slides/out') + await editorPage.waitForSelector('.stage-wrap canvas', { timeout: 30_000 }) + + const banner = editorPage.locator('.font-missing-banner') + const fontState = await editorPage.evaluate(async () => { + const api = ( + window as unknown as { + slidesApi: { + fontCatalog(): Promise> + fontMissing(): Promise + } + } + ).slidesApi + return { + catalog: await api.fontCatalog(), + missing: await api.fontMissing(), + } + }) + const configured = Boolean(process.env.GENOFFICE_FONT_CDN_URL?.trim()) + if (!configured) { + expect(fontState.catalog).toEqual([]) + expect(fontState.missing).toEqual([]) + await expect(banner).toHaveCount(0) + return + } + + expect(fontState.catalog.some((c) => c.family === 'Rubik' && !c.installed)).toBe(true) + expect(fontState.missing).toContain('Rubik') + await expect(banner).toBeVisible({ timeout: 20_000 }) + await expect(banner).toContainText('Rubik') + + if (process.env.E2E_FONT_CDN === '1') { + await banner.locator('.fmb-download').click() + await expect(banner).toBeHidden({ timeout: 60_000 }) + const installed = await editorPage.evaluate(async () => { + const api = ( + window as unknown as { + slidesApi: { + fontCatalog(): Promise> + } + } + ).slidesApi + const cat = await api.fontCatalog() + return cat.find((c) => c.family === 'Rubik')?.installed + }) + expect(installed).toBe(true) + } + } finally { + await closeAndSaveVideo(launched, 'slides-font-manager') + } +}) diff --git a/packages/agent-core/src/electron-transport.ts b/packages/agent-core/src/electron-transport.ts index f832bde..1081d00 100644 --- a/packages/agent-core/src/electron-transport.ts +++ b/packages/agent-core/src/electron-transport.ts @@ -13,8 +13,9 @@ import type { */ export interface IpcStreamChunk { requestId: string - /** 'ping' = wire-level keepalive; re-arms the silence watchdog and carries no payload */ - type: 'delta' | 'tool-call' | 'done' | 'error' | 'ping' + /** 'ping' = wire-level keepalive; re-arms the silence watchdog and carries no payload; + * 'reasoning' = model thinking delta (text carries it) */ + type: 'delta' | 'reasoning' | 'tool-call' | 'done' | 'error' | 'ping' text?: string toolCall?: AgentToolCall error?: string @@ -95,6 +96,9 @@ export function createIpcTransport(options: IpcTransportOptions): AgentTra } else if (chunk.type === 'delta') { armSilence() cb.onDelta(chunk.text ?? '') + } else if (chunk.type === 'reasoning') { + armSilence() + if (chunk.text) cb.onReasoning?.(chunk.text) } else if (chunk.type === 'tool-call') { armSilence() if (chunk.toolCall) cb.onToolCall(chunk.toolCall) diff --git a/packages/agent-core/src/loop.ts b/packages/agent-core/src/loop.ts index 44a7ea1..dd9f327 100644 --- a/packages/agent-core/src/loop.ts +++ b/packages/agent-core/src/loop.ts @@ -181,6 +181,7 @@ export class AgentLoop { private inputParseFails = 0 private turnStopReason: string | null = null private turnText = '' + private turnReasoning = '' private toolCalls: AgentToolCall[] = [] /** tools actually executed during this run, fed to skill.verifyResponse */ private executedCalls: ExecutedToolCall[] = [] @@ -285,6 +286,11 @@ export class AgentLoop { // drop it so the model never sees two adjacent user turns as one combined instruction while (this.history.at(-1)?.role === 'user') this.history.pop() this.trimHistory() + // Reasoning echo only matters inside a run's own tool loop; drop it from + // finished runs so it stops costing tokens on every later request. + this.history = this.history.map((m) => + m.role === 'assistant' && m.reasoning ? { ...m, reasoning: undefined } : m, + ) if (userMsg.role === 'user') { userMsg = { ...userMsg, text: sanitizeAgentPayload(userMsg.text) } } @@ -475,6 +481,7 @@ export class AgentLoop { private startTurn(retriesUsed = 0): void { const generation = this.generation this.turnText = '' + this.turnReasoning = '' this.toolCalls = [] this.turnStopReason = null // Some transports emit an extra onDone after cancel — this turn may finalize only once @@ -491,6 +498,10 @@ export class AgentLoop { this.turnText += text this.options.events?.onText?.(this.turnText) }, + onReasoning: (text) => { + if (generation !== this.generation || settled) return + this.turnReasoning += text + }, onToolCall: (call) => { if (generation !== this.generation || settled) return this.toolCalls.push(call) @@ -601,6 +612,8 @@ export class AgentLoop { role: 'assistant', text: this.turnText, toolCalls: toolCalls.map(({ id, name, input }) => ({ id, name, input })), + // interleaved-thinking models degrade in tool loops unless their reasoning is echoed back + ...(this.turnReasoning ? { reasoning: this.turnReasoning } : {}), }) const generation = this.generation const results: AgentToolResult[] = [] diff --git a/packages/agent-core/src/types.ts b/packages/agent-core/src/types.ts index 1eca8f6..06f75ec 100644 --- a/packages/agent-core/src/types.ts +++ b/packages/agent-core/src/types.ts @@ -34,7 +34,14 @@ export interface AgentImage { export type AgentMessage = | { role: 'user'; text: string; images?: AgentImage[] | undefined } - | { role: 'assistant'; text: string; toolCalls?: AgentToolCall[] | undefined } + /** reasoning: opaque vendor thinking captured during the turn; interleaved-thinking + * models (MiniMax M3, DeepSeek V4) degrade in tool loops unless it is echoed back */ + | { + role: 'assistant' + text: string + toolCalls?: AgentToolCall[] | undefined + reasoning?: string | undefined + } | { role: 'tool'; results: AgentToolResult[] } /** @@ -91,6 +98,8 @@ export interface AgentStreamRequest { export interface AgentStreamCallbacks { onDelta(text: string): void + /** raw model reasoning delta; the loop stores it on the assistant message for interleaved-thinking echo */ + onReasoning?(text: string): void /** complete parsed tool call (arguments finished streaming) */ onToolCall(call: AgentToolCall): void /** Phase changes within the model stream (thinking / responding / tool-input); older transports may omit this */ diff --git a/packages/agent-core/tests/electron-transport.test.ts b/packages/agent-core/tests/electron-transport.test.ts index 8bd71e4..8d3586b 100644 --- a/packages/agent-core/tests/electron-transport.test.ts +++ b/packages/agent-core/tests/electron-transport.test.ts @@ -39,6 +39,7 @@ function setup( }) const cb = { onDelta: vi.fn(), + onReasoning: vi.fn(), onToolCall: vi.fn(), onStopReason: vi.fn(), onDone: vi.fn(), @@ -65,6 +66,15 @@ describe('createIpcTransport', () => { expect(cb.onToolCall).toHaveBeenCalledWith({ id: 'c1', name: 'read', input: {} }) }) + it('forwards reasoning chunks separately from text deltas', () => { + const { cb, emit } = setup() + emit({ type: 'reasoning', text: 'thinking…' }) + emit({ type: 'reasoning' }) // payload-less chunk carries nothing + expect(cb.onReasoning).toHaveBeenCalledTimes(1) + expect(cb.onReasoning).toHaveBeenCalledWith('thinking…') + expect(cb.onDelta).not.toHaveBeenCalled() + }) + it('ignores chunks for other requestIds', () => { const { cb, emit } = setup() emit({ requestId: 'someone-else', type: 'delta', text: 'nope' }) diff --git a/packages/agent-core/tests/loop.test.ts b/packages/agent-core/tests/loop.test.ts index 0ffa08d..aa6d432 100644 --- a/packages/agent-core/tests/loop.test.ts +++ b/packages/agent-core/tests/loop.test.ts @@ -139,6 +139,39 @@ describe('AgentLoop', () => { expect(transport.requests[1].messageCount).toBe(3) }) + it('stores streamed reasoning on the tool-calling assistant message and drops it on the next run', async () => { + const transport = scriptedTransport([ + (cb) => { + cb.onReasoning?.('the user wants ') + cb.onReasoning?.('a change') + cb.onToolCall({ id: 't1', name: 'do_thing', input: {} }) + cb.onDone() + }, + (cb) => { + cb.onDelta('All done') + cb.onDone() + }, + (cb) => { + cb.onDelta('Done again') + cb.onDone() + }, + ]) + const loop = new AgentLoop({ transport, skill: makeSkill() }) + loop.run('make a change') + await flush() + await flush() + const withTools = loop.messages[1] as Extract + expect(withTools.reasoning).toBe('the user wants a change') + // the final text-only turn carries no reasoning field + expect('reasoning' in (loop.messages[3] as object)).toBe(false) + + // a new run strips reasoning from finished runs (echo only matters inside a run's tool loop) + loop.run('another change') + await flush() + const stripped = loop.messages[1] as Extract + expect(stripped.reasoning).toBeUndefined() + }) + it('emits onToolStart before each execution, paired with onToolExecuted', async () => { const transport = scriptedTransport([ (cb) => { diff --git a/packages/ai-provider/src/index.ts b/packages/ai-provider/src/index.ts index 526f2d7..cfafebc 100644 --- a/packages/ai-provider/src/index.ts +++ b/packages/ai-provider/src/index.ts @@ -18,7 +18,7 @@ export { defaultAiSettings, resolveAiSettings, } from './providers' -export { AI_PROVIDER_ADAPTERS, getProviderAdapter } from './registry' +export { AI_PROVIDER_ADAPTERS, getProviderAdapter, modelLacksVision } from './registry' export type { AiProtocol, ProviderAdapter, diff --git a/packages/ai-provider/src/protocols/openai-compatible.ts b/packages/ai-provider/src/protocols/openai-compatible.ts index 46d4e51..16ee12c 100644 --- a/packages/ai-provider/src/protocols/openai-compatible.ts +++ b/packages/ai-provider/src/protocols/openai-compatible.ts @@ -2,6 +2,7 @@ import type { AgentMessage, AgentToolCall, AgentToolDef } from '@genoffice/agent import { aiFetch } from '../fetch' import { httpBodyDetail } from '../http-error' import { gensparkAttributionHeaders } from '../providers' +import { modelEchoesReasoning } from '../registry' import type { AiChatResponse, AiProviderConfig } from '../types' import { createStreamWatchdog, type StreamWatchdog } from '../watchdog' import { @@ -13,7 +14,11 @@ import { type StreamCallbacks, } from './shared' -function openAiMessages(system: string, messages: AgentMessage[]): unknown[] { +function openAiMessages( + system: string, + messages: AgentMessage[], + echoReasoning: boolean, +): unknown[] { const out: unknown[] = [{ role: 'system', content: system }] for (const m of messages) { if (m.role === 'user') { @@ -38,6 +43,7 @@ function openAiMessages(system: string, messages: AgentMessage[]): unknown[] { out.push({ role: 'assistant', content: m.text || (hasTools ? null : '(no content)'), + ...(echoReasoning && m.reasoning ? { reasoning_content: m.reasoning } : {}), ...(hasTools ? { tool_calls: m.toolCalls!.map((call) => ({ @@ -63,6 +69,7 @@ function emitOpenAiJsonMessage(bodyText: string, cb: StreamCallbacks): void { choices?: Array<{ message?: { content?: string | null + reasoning_content?: string tool_calls?: Array<{ id?: string; function?: { name?: string; arguments?: string } }> } finish_reason?: string | null @@ -77,6 +84,7 @@ function emitOpenAiJsonMessage(bodyText: string, cb: StreamCallbacks): void { if (msg.error) throw new Error(sseErrorText(msg.error, 'Model error')) const choice = msg.choices?.[0] let emitted = false + if (choice?.message?.reasoning_content) cb.onReasoningDelta?.(choice.message.reasoning_content) if (choice?.message?.content) { emitted = true cb.onDelta(choice.message.content) @@ -104,6 +112,8 @@ function emitOpenAiJsonMessage(bodyText: string, cb: StreamCallbacks): void { /** Per-endpoint request shaping resolved from the provider registry. */ export interface OpenAiRequestOptions { omitTemperature?: boolean | undefined + /** send the output cap as OpenAI's renamed `max_completion_tokens` (GPT-5.x/o-series 400 on `max_tokens`) */ + useMaxCompletionTokens?: boolean | undefined /** vendor-specific fields merged into the request body (e.g. DeepSeek's `thinking`) */ bodyExtras?: Record | undefined } @@ -149,8 +159,10 @@ async function openAiCompatibleTurn( }, body: JSON.stringify({ model: config.model, - max_tokens: maxTokens, - messages: openAiMessages(system, messages), + ...(options.useMaxCompletionTokens + ? { max_completion_tokens: maxTokens } + : { max_tokens: maxTokens }), + messages: openAiMessages(system, messages, modelEchoesReasoning(config.model)), ...(tools.length > 0 ? { tools: tools.map((t) => ({ @@ -212,6 +224,9 @@ async function openAiCompatibleTurn( choices?: Array<{ delta?: { content?: string + /** DeepSeek/MiniMax native and LiteLLM-normalized thinking stream; OpenRouter uses `reasoning` */ + reasoning_content?: string + reasoning?: string tool_calls?: Array<{ index: number id?: string @@ -228,6 +243,8 @@ async function openAiCompatibleTurn( if (event.error) throw new Error(sseErrorText(event.error, 'Model stream error')) const choice = event.choices?.[0] if (!choice) continue + const reasoning = choice.delta?.reasoning_content ?? choice.delta?.reasoning + if (typeof reasoning === 'string' && reasoning) cb.onReasoningDelta?.(reasoning) if (choice.delta?.content) { emitted = true cb.onDelta(choice.delta.content) diff --git a/packages/ai-provider/src/protocols/shared.ts b/packages/ai-provider/src/protocols/shared.ts index 8adb3e4..3593284 100644 --- a/packages/ai-provider/src/protocols/shared.ts +++ b/packages/ai-provider/src/protocols/shared.ts @@ -34,6 +34,8 @@ export async function* sseLines( export interface StreamCallbacks { onDelta: (text: string) => void onToolCall: (call: AgentToolCall) => void + /** raw model reasoning deltas (reasoning_content); stored so interleaved-thinking models get it echoed back */ + onReasoningDelta?: (text: string) => void /** normalized stop reason ('max_tokens' when the output was cut off by the token limit) */ onStopReason?: (reason: string) => void /** bytes arrived on the wire (fires per network chunk, including SSE pings; used for keepalive) */ diff --git a/packages/ai-provider/src/providers.ts b/packages/ai-provider/src/providers.ts index dfdd9a0..f0ce1c7 100644 --- a/packages/ai-provider/src/providers.ts +++ b/packages/ai-provider/src/providers.ts @@ -75,10 +75,10 @@ export const AI_PROVIDERS: AiProviderMeta[] = [ { id: 'deepseek', label: 'DeepSeek', - // V4 ids per api-docs.deepseek.com (2026-08). The deepseek-chat / - // deepseek-reasoner aliases were retired 2026-07-24; thinking mode is now - // a request parameter, so both ids drive the tool-calling agent loop. - models: ['deepseek-v4-pro', 'deepseek-v4-flash'], + // V4 ids per api-docs.deepseek.com (2026-08). Vision Exp is available + // through the normal DeepSeek API key; indirect-route aliases such as + // `-openrouter` do not belong in this direct-provider list. + models: ['deepseek-v4-pro', 'deepseek-v4-flash', 'deepseek-v4-flash-vision-exp'], defaultModel: 'deepseek-v4-pro', keyPlaceholder: 'sk-...', }, diff --git a/packages/ai-provider/src/registry.ts b/packages/ai-provider/src/registry.ts index 01e17ec..0a7f439 100644 --- a/packages/ai-provider/src/registry.ts +++ b/packages/ai-provider/src/registry.ts @@ -18,6 +18,8 @@ export interface ResolvedEndpoint { baseUrl: string /** the endpoint fixes its sampling and rejects a temperature field (Kimi K3: "only 1 is allowed") */ omitTemperature?: boolean + /** the endpoint wants the output cap as OpenAI's renamed `max_completion_tokens` (GPT-5.x/o-series 400 on `max_tokens`) */ + useMaxCompletionTokens?: boolean /** vendor-specific request fields merged into the chat-completions body */ bodyExtras?: Record } @@ -44,6 +46,25 @@ export function modelHasFixedSampling(model: string): boolean { return /(^|\/)(kimi-k3|gpt-5)/.test(model) } +/** + * Model ids that reject image input even under a vision-capable provider. + * DeepSeek V4 Pro and Flash are text-only; their -vision* branches are + * excluded so the direct Vision Exp model can receive screenshots. + */ +export function modelLacksVision(model: string): boolean { + return /(^|\/)deep-?seek-v4-(?:pro(?:$|-)|flash(?!-vision))/.test(model) +} + +/** + * Interleaved-thinking families whose vendors want the reasoning echoed back + * on assistant messages: MiniMax documents that stripping it degrades + * multi-turn tool use, and DeepSeek V4 rejects tool turns without it. Gated + * per model because other vendors may reject the unknown field. + */ +export function modelEchoesReasoning(model: string): boolean { + return /(^|\/)(minimax-m|deep-?seek-v4)/i.test(model) +} + /** * DeepSeek V4 thinks by default, and once a request carries `tools` the API * rejects (400) every later turn whose assistant messages don't echo back the @@ -58,7 +79,11 @@ const DEEPSEEK_NON_THINKING = { thinking: { type: 'disabled' } } function fixedEndpoint( protocol: AiProtocol, baseUrl: string, - extras?: { omitTemperature?: boolean; bodyExtras?: Record }, + extras?: { + omitTemperature?: boolean + useMaxCompletionTokens?: boolean + bodyExtras?: Record + }, ) { return (config: AiProviderConfig): ResolvedEndpoint => { const omit = extras?.omitTemperature || modelHasFixedSampling(config.model) @@ -66,6 +91,7 @@ function fixedEndpoint( protocol, baseUrl: config.baseUrl || baseUrl, ...(omit ? { omitTemperature: true } : {}), + ...(extras?.useMaxCompletionTokens ? { useMaxCompletionTokens: true } : {}), ...(extras?.bodyExtras ? { bodyExtras: extras.bodyExtras } : {}), } } @@ -103,7 +129,7 @@ export const AI_PROVIDER_ADAPTERS: Record = { }, deepseek: { meta: metaOf('deepseek'), - capabilities: { auth: 'api-key', vision: false }, + capabilities: { auth: 'api-key', vision: true }, resolveEndpoint: fixedEndpoint('openai-compatible', 'https://api.deepseek.com/v1', { bodyExtras: DEEPSEEK_NON_THINKING, }), @@ -111,7 +137,11 @@ export const AI_PROVIDER_ADAPTERS: Record = { openai: { meta: metaOf('openai'), capabilities: { auth: 'api-key', vision: true }, - resolveEndpoint: fixedEndpoint('openai-compatible', 'https://api.openai.com/v1'), + // every current OpenAI model accepts the renamed field, so it is safe endpoint-wide; + // other openai-compatible vendors (and the LiteLLM-backed Genspark proxy) still expect `max_tokens` + resolveEndpoint: fixedEndpoint('openai-compatible', 'https://api.openai.com/v1', { + useMaxCompletionTokens: true, + }), }, kimi: { meta: metaOf('kimi'), diff --git a/packages/ai-provider/src/stream.ts b/packages/ai-provider/src/stream.ts index 5221713..aa6db35 100644 --- a/packages/ai-provider/src/stream.ts +++ b/packages/ai-provider/src/stream.ts @@ -32,6 +32,7 @@ export async function streamForProvider( case 'openai-compatible': return streamOpenAiCompatible(baseUrl, config, system, messages, tools, maxTokens, cb, { omitTemperature: endpoint.omitTemperature, + useMaxCompletionTokens: endpoint.useMaxCompletionTokens, bodyExtras: endpoint.bodyExtras, }) } diff --git a/packages/ai-provider/src/types.ts b/packages/ai-provider/src/types.ts index 8d75a26..195c988 100644 --- a/packages/ai-provider/src/types.ts +++ b/packages/ai-provider/src/types.ts @@ -81,8 +81,9 @@ export interface AiStreamRequest { export interface AiStreamChunk { requestId: string - /** 'ping' = wire-level keepalive so the renderer can tell a live stream from a dead one */ - type: 'delta' | 'tool-call' | 'done' | 'error' | 'ping' + /** 'ping' = wire-level keepalive so the renderer can tell a live stream from a dead one; + * 'reasoning' = model thinking delta (text carries it), stored for interleaved-thinking echo */ + type: 'delta' | 'reasoning' | 'tool-call' | 'done' | 'error' | 'ping' text?: string /** complete parsed tool call (emitted once its arguments finish streaming) */ toolCall?: AgentToolCall diff --git a/packages/ai-provider/tests/providers.test.ts b/packages/ai-provider/tests/providers.test.ts index 5b9fa93..a410db3 100644 --- a/packages/ai-provider/tests/providers.test.ts +++ b/packages/ai-provider/tests/providers.test.ts @@ -27,6 +27,17 @@ describe('defaultAiSettings', () => { }) }) +describe('provider model catalog', () => { + it('offers DeepSeek Vision Exp only through the direct BYOK provider', () => { + const genspark = AI_PROVIDERS.find((provider) => provider.id === 'genspark')! + const deepseek = AI_PROVIDERS.find((provider) => provider.id === 'deepseek')! + + expect(deepseek.models).toContain('deepseek-v4-flash-vision-exp') + expect(genspark.models).not.toContain('deep-seek-v4-flash') + expect(genspark.models).not.toContain('deep-seek-v4-flash-vision-exp-openrouter') + }) +}) + describe('resolveAiSettings', () => { it('returns fresh defaults when nothing is stored', () => { const defaults = defaultAiSettings({ anthropic: 'sk-ant-preset' }) diff --git a/packages/ai-provider/tests/registry.test.ts b/packages/ai-provider/tests/registry.test.ts index 195d0ac..04e42d5 100644 --- a/packages/ai-provider/tests/registry.test.ts +++ b/packages/ai-provider/tests/registry.test.ts @@ -1,5 +1,10 @@ import { describe, expect, it } from 'vitest' -import { AI_PROVIDER_ADAPTERS, getProviderAdapter } from '../src/registry' +import { + AI_PROVIDER_ADAPTERS, + getProviderAdapter, + modelEchoesReasoning, + modelLacksVision, +} from '../src/registry' import { AI_PROVIDERS, GENSPARK_LLM_BASE_URLS } from '../src/providers' import type { AiProviderConfig, AiProviderId } from '../src/types' @@ -52,6 +57,7 @@ describe('provider registry', () => { expect(AI_PROVIDER_ADAPTERS.openai.resolveEndpoint(config('gpt-4.1-mini'))).toEqual({ protocol: 'openai-compatible', baseUrl: 'https://api.openai.com/v1', + useMaxCompletionTokens: true, }) }) @@ -61,6 +67,7 @@ describe('provider registry', () => { protocol: 'openai-compatible', baseUrl: 'https://api.openai.com/v1', omitTemperature: true, + useMaxCompletionTokens: true, }) } }) @@ -163,3 +170,26 @@ describe('fixed-sampling models on indirect routes', () => { }) }) }) + +describe('modelLacksVision', () => { + it('flags text-only DeepSeek V4 models but not the vision branch', () => { + expect(modelLacksVision('deep-seek-v4-flash')).toBe(true) + expect(modelLacksVision('deep-seek-v4-flash-baseten')).toBe(true) + expect(modelLacksVision('deepseek-v4-pro')).toBe(true) + expect(modelLacksVision('deepseek-v4-flash')).toBe(true) + expect(modelLacksVision('deepseek-v4-flash-vision-exp')).toBe(false) + expect(modelLacksVision('deep-seek-v4-flash-vision-exp-openrouter')).toBe(false) + expect(modelLacksVision('claude-opus-4-7')).toBe(false) + }) +}) + +describe('modelEchoesReasoning', () => { + it('flags interleaved-thinking families on any route, case-insensitively', () => { + expect(modelEchoesReasoning('MiniMax-M3')).toBe(true) + expect(modelEchoesReasoning('minimax-m2p7')).toBe(true) + expect(modelEchoesReasoning('deep-seek-v4-flash')).toBe(true) + expect(modelEchoesReasoning('deepseek-v4-pro')).toBe(true) + expect(modelEchoesReasoning('gpt-5.6-luna')).toBe(false) + expect(modelEchoesReasoning('kimi-k3')).toBe(false) + }) +}) diff --git a/packages/ai-provider/tests/stream.test.ts b/packages/ai-provider/tests/stream.test.ts index ed6256a..e11c497 100644 --- a/packages/ai-provider/tests/stream.test.ts +++ b/packages/ai-provider/tests/stream.test.ts @@ -71,6 +71,38 @@ describe('streamForProvider: temperature policy', () => { expect('temperature' in bodies[0]).toBe(false) expect(bodies[1].temperature).toBe(0.3) }) + + // issue genspark-ai/genoffice#147: every model in the OpenAI BYOK dropdown is GPT-5.x, + // and api.openai.com 400s `max_tokens` for that family + it('caps OpenAI via max_completion_tokens and other vendors via max_tokens', async () => { + const fetchMock = vi.fn().mockImplementation(() => Promise.resolve(okTurn())) + vi.stubGlobal('fetch', fetchMock) + await streamForProvider( + 'openai', + { apiKey: 'k', model: 'gpt-5.6-luna' }, + 'sys', + [], + [], + 100, + collector().cb, + ) + await streamForProvider( + 'kimi', + { apiKey: 'k', model: 'kimi-k3' }, + 'sys', + [], + [], + 100, + collector().cb, + ) + const bodies = fetchMock.mock.calls.map((call) => + JSON.parse((call[1] as RequestInit).body as string), + ) + expect(bodies[0].max_completion_tokens).toBe(100) + expect('max_tokens' in bodies[0]).toBe(false) + expect(bodies[1].max_tokens).toBe(100) + expect('max_completion_tokens' in bodies[1]).toBe(false) + }) }) describe('streamForProvider: empty SSE streams surface as errors', () => { @@ -948,3 +980,62 @@ it('rejects an unknown provider id', async () => { streamForProvider('unknown' as never, { apiKey: 'k', model: 'm' }, 'sys', [], [], 100, cb), ).rejects.toThrow(/Unknown provider/) }) + +describe('streamForProvider: interleaved-thinking reasoning', () => { + const reasoningTurn = () => + okResponse( + sseStream([ + 'data: {"choices":[{"delta":{"reasoning_content":"hmm "}}]}', + 'data: {"choices":[{"delta":{"reasoning_content":"ok"}}]}', + 'data: {"choices":[{"delta":{"content":"hi"},"finish_reason":"stop"}]}', + ]), + ) + const toolLoopMessages = [ + { role: 'user' as const, text: 'q' }, + { + role: 'assistant' as const, + text: '', + toolCalls: [{ id: 't1', name: 'f', input: {} }], + reasoning: 'earlier thoughts', + }, + { role: 'tool' as const, results: [{ id: 't1', name: 'f', output: '42' }] }, + ] + + it('surfaces reasoning deltas and echoes stored reasoning for thinking families', async () => { + const fetchMock = vi.fn().mockImplementation(() => Promise.resolve(reasoningTurn())) + vi.stubGlobal('fetch', fetchMock) + const reasoning: string[] = [] + const { deltas, cb } = collector() + await streamForProvider( + 'genspark', + { apiKey: 'k', model: 'deep-seek-v4-flash' }, + 'sys', + toolLoopMessages, + [], + 100, + { ...cb, onReasoningDelta: (t) => reasoning.push(t) }, + ) + expect(reasoning.join('')).toBe('hmm ok') + expect(deltas.join('')).toBe('hi') + const body = JSON.parse((fetchMock.mock.calls[0]![1] as RequestInit).body as string) + const assistant = body.messages.find((m: { role: string }) => m.role === 'assistant') + expect(assistant.reasoning_content).toBe('earlier thoughts') + }) + + it('does not echo reasoning to families that never emitted it over this protocol', async () => { + const fetchMock = vi.fn().mockImplementation(() => Promise.resolve(reasoningTurn())) + vi.stubGlobal('fetch', fetchMock) + await streamForProvider( + 'genspark', + { apiKey: 'k', model: 'gpt-5.6-luna' }, + 'sys', + toolLoopMessages, + [], + 100, + collector().cb, + ) + const body = JSON.parse((fetchMock.mock.calls[0]![1] as RequestInit).body as string) + const assistant = body.messages.find((m: { role: string }) => m.role === 'assistant') + expect('reasoning_content' in assistant).toBe(false) + }) +}) diff --git a/packages/docx-engine/src/chart.ts b/packages/docx-engine/src/chart.ts index d59d96d..1b0f9e4 100644 --- a/packages/docx-engine/src/chart.ts +++ b/packages/docx-engine/src/chart.ts @@ -1,5 +1,5 @@ import JSZip from 'jszip' -import type { ChartDisplay, ChartSeries, NewChart } from './types' +import type { ChartDisplay, ChartSeries, NewChart, ThemeColors } from './types' import { attrsOf, childrenOf, @@ -22,64 +22,318 @@ const CHART_KINDS: Record = { 'c:doughnutChart': 'pie', 'c:areaChart': 'area', 'c:area3DChart': 'area', + 'c:scatterChart': 'scatter', + 'c:bubbleChart': 'bubble', } /** * Read the display model of a chart part (word/charts/chartN.xml). Only the * caches Word writes next to the data references (c:strCache / c:numCache) * are read — the embedded workbook is never opened. Returns null when the - * part has no series with cached values (e.g. scatter charts). + * part has no series with cached values. */ -export function parseChartPartXml(xml: string, partPath: string): ChartDisplay | null { +export function parseChartPartXml( + xml: string, + partPath: string, + theme?: ThemeColors | null, +): ChartDisplay | null { const parsed = xmlParser.parse(xml) as XNode[] if (xml.includes(' nameOf(n) === 'c:chartSpace') const chart = space ? findChild(space, 'c:chart') : undefined const plotArea = chart ? findChild(chart, 'c:plotArea') : undefined - if (!chart || !plotArea) return null + if (!space || !chart || !plotArea) return null // first plotted chart type wins (combo charts render their primary series) const plot = childrenOf(plotArea).find((c) => (nameOf(c) ?? '').endsWith('Chart')) if (!plot) return null const kind = CHART_KINDS[nameOf(plot) ?? ''] ?? 'other' const horizontal = kind === 'bar' && attrsOf(findChild(plot, 'c:barDir') ?? {})['val'] === 'bar' + const groupingVal = attrsOf(findChild(plot, 'c:grouping') ?? {})['val'] + const grouping = + (kind === 'bar' || kind === 'area') && + (groupingVal === 'stacked' || groupingVal === 'percentStacked') + ? groupingVal + : undefined + const scatterStyle = attrsOf(findChild(plot, 'c:scatterStyle') ?? {})['val'] + const markers = + kind === 'line' + ? attrsOf(findChild(plot, 'c:marker') ?? {})['val'] === '1' + : kind === 'scatter' + ? scatterStyle === undefined || scatterStyle.toLowerCase().includes('marker') + : false + const scatterLines = kind === 'scatter' && /line|smooth/i.test(scatterStyle ?? '') let categories: string[] = [] const series: ChartSeries[] = [] for (const ser of findChildren(plot, 'c:ser')) { - const val = findChild(ser, 'c:val') - const rawValues = val ? cachePoints(val) : [] - const values = rawValues.map((v) => { - if (v === null || v.trim() === '') return null - const n = Number(v) - return Number.isFinite(n) ? n : null - }) + // scatter/bubble series carry x/y pairs instead of category/value caches + const val = findChild(ser, 'c:val') ?? findChild(ser, 'c:yVal') + const values = val ? cacheNumbers(val) : [] if (values.length === 0) continue - const cat = findChild(ser, 'c:cat') + const cat = findChild(ser, 'c:cat') ?? findChild(ser, 'c:xVal') if (cat && categories.length === 0) { categories = cachePoints(cat).map((v) => v ?? '') // date-formatted numeric caches hold Excel serials; display them as dates const fmt = catFormatCode(cat) if (fmt && /[yd]/i.test(fmt)) { categories = categories.map((v) => serialDateText(v) ?? v) + } else if (nameOf(cat) === 'c:xVal') { + // x caches carry raw doubles (0.70000000000000062); trim for display + categories = categories.map((v) => { + const n = Number(v) + return v !== '' && Number.isFinite(n) ? String(Math.round(n * 10000) / 10000) : v + }) } } const name = seriesName(ser) - series.push({ ...(name !== undefined ? { name } : {}), values }) + const entry: ChartSeries = { ...(name !== undefined ? { name } : {}), values } + const color = solidFillHex(findChild(ser, 'c:spPr'), theme) + if (color) entry.color = color + const pointColors = dataPointColors(ser, theme) + if (pointColors) entry.pointColors = pointColors + if (kind === 'scatter' || kind === 'bubble') { + const xVal = findChild(ser, 'c:xVal') + const xValues = xVal ? cacheNumbers(xVal) : [] + if (xValues.some((v) => v !== null)) entry.xValues = xValues + const sizeVal = findChild(ser, 'c:bubbleSize') + const sizes = sizeVal ? cacheNumbers(sizeVal) : [] + if (sizes.some((v) => v !== null)) entry.sizes = sizes + if (scatterLines && !seriesLineHidden(ser)) entry.line = true + } + series.push(entry) } if (series.length === 0) return null - const title = chartTitle(chart) + const palette = chartPalette(chartStyleVal(space), theme) + let title = chartTitle(chart) + // Office names a single-series chart's auto title after the series + if (title === 'Chart Title' && series.length === 1 && series[0].name) title = series[0].name return { partPath, kind, ...(horizontal ? { horizontal } : {}), + ...(grouping ? { grouping } : {}), + ...(markers ? { markers } : {}), ...(title !== undefined ? { title } : {}), categories, series, + ...(palette ? { palette } : {}), } } +/** numeric cache of a c:val / c:yVal / c:xVal / c:bubbleSize container */ +function cacheNumbers(container: XNode): (number | null)[] { + return cachePoints(container).map((v) => { + if (v === null || v.trim() === '') return null + const n = Number(v) + return Number.isFinite(n) ? n : null + }) +} + +/** the series a:ln is an explicit a:noFill (scatter series drawn markers-only) */ +function seriesLineHidden(ser: XNode): boolean { + const ln = findChild(findChild(ser, 'c:spPr') ?? {}, 'a:ln') + return ln !== undefined && findChild(ln, 'a:noFill') !== undefined +} + +/** c:dPt explicit fills, sparse by point index (pie slices, highlighted bars) */ +function dataPointColors(ser: XNode, theme?: ThemeColors | null): (string | null)[] | null { + const out: (string | null)[] = [] + let any = false + for (const dPt of findChildren(ser, 'c:dPt')) { + const idx = parseInt(attrsOf(findChild(dPt, 'c:idx') ?? {})['val'] ?? '', 10) + if (!Number.isFinite(idx) || idx < 0) continue + const color = solidFillHex(findChild(dPt, 'c:spPr'), theme) + if (!color) continue + out[idx] = color + any = true + } + if (!any) return null + for (let i = 0; i < out.length; i++) out[i] ??= null + return out +} + +// ---- chart style + color resolution ---- + +/** scheme slots charts reference in fills, resolved against the doc theme */ +const SCHEME_SLOTS: Record = { + accent1: 'accent1', + accent2: 'accent2', + accent3: 'accent3', + accent4: 'accent4', + accent5: 'accent5', + accent6: 'accent6', + dk1: 'dk1', + lt1: 'lt1', + dk2: 'dk2', + lt2: 'lt2', + tx1: 'dk1', + bg1: 'lt1', + tx2: 'dk2', + bg2: 'lt2', +} + +/** resolve an spPr solid fill to hex (no '#'); schemeClr goes through the theme */ +function solidFillHex(spPr: XNode | undefined, theme?: ThemeColors | null): string | undefined { + if (!spPr) return undefined + const fill = findChild(spPr, 'a:solidFill') + if (!fill) return undefined + for (const clr of childrenOf(fill)) { + const name = nameOf(clr) + let base: string | undefined + if (name === 'a:srgbClr') base = attrsOf(clr)['val'] + else if (name === 'a:sysClr') base = attrsOf(clr)['lastClr'] + else if (name === 'a:schemeClr') { + const slot = SCHEME_SLOTS[attrsOf(clr)['val'] ?? ''] + base = slot ? theme?.[slot] : undefined + } + if (!base || !/^[0-9A-Fa-f]{6}$/.test(base)) continue + return applyColorMods(base.toUpperCase(), clr) + } + return undefined +} + +/** apply the transforms charts use (lumMod/lumOff/shade/tint) to a hex color */ +function applyColorMods(hex: string, clr: XNode): string { + let lumMod: number | undefined + let lumOff: number | undefined + let shadeV: number | undefined + let tintV: number | undefined + for (const child of childrenOf(clr)) { + const v = parseInt(attrsOf(child)['val'] ?? '', 10) + if (!Number.isFinite(v)) continue + const name = nameOf(child) + if (name === 'a:lumMod') lumMod = v / 100000 + else if (name === 'a:lumOff') lumOff = v / 100000 + else if (name === 'a:shade') shadeV = v / 100000 + else if (name === 'a:tint') tintV = v / 100000 + } + let out = hex + if (shadeV !== undefined) out = shadeHex(out, shadeV) + if (tintV !== undefined) out = tintHex(out, tintV) + if (lumMod !== undefined || lumOff !== undefined) out = lumHex(out, lumMod ?? 1, lumOff ?? 0) + return out +} + +const hexRgb = (hex: string): [number, number, number] => [ + parseInt(hex.slice(0, 2), 16), + parseInt(hex.slice(2, 4), 16), + parseInt(hex.slice(4, 6), 16), +] + +const rgbHex = (r: number, g: number, b: number): string => + [r, g, b] + .map((c) => + Math.max(0, Math.min(255, Math.round(c))) + .toString(16) + .padStart(2, '0'), + ) + .join('') + .toUpperCase() + +/** a:shade — scale toward black */ +export function shadeHex(hex: string, factor: number): string { + const [r, g, b] = hexRgb(hex) + return rgbHex(r * factor, g * factor, b * factor) +} + +/** a:tint — scale toward white */ +export function tintHex(hex: string, factor: number): string { + const [r, g, b] = hexRgb(hex) + const t = (c: number) => c * factor + 255 * (1 - factor) + return rgbHex(t(r), t(g), t(b)) +} + +/** a:lumMod/a:lumOff — HSL luminance L' = L*mod + off */ +export function lumHex(hex: string, mod: number, off: number): string { + const [r, g, b] = hexRgb(hex).map((c) => c / 255) + const max = Math.max(r, g, b) + const min = Math.min(r, g, b) + let h = 0 + const l0 = (max + min) / 2 + const d = max - min + let s = 0 + if (d !== 0) { + s = d / (1 - Math.abs(2 * l0 - 1)) + if (max === r) h = ((g - b) / d) % 6 + else if (max === g) h = (b - r) / d + 2 + else h = (r - g) / d + 4 + h *= 60 + if (h < 0) h += 360 + } + const l = Math.max(0, Math.min(1, l0 * mod + off)) + const c = (1 - Math.abs(2 * l - 1)) * s + const x = c * (1 - Math.abs(((h / 60) % 2) - 1)) + const m = l - c / 2 + const seg = + h < 60 + ? [c, x, 0] + : h < 120 + ? [x, c, 0] + : h < 180 + ? [0, c, x] + : h < 240 + ? [0, x, c] + : h < 300 + ? [x, 0, c] + : [c, 0, x] + return rgbHex((seg[0] + m) * 255, (seg[1] + m) * 255, (seg[2] + m) * 255) +} + +/** + * c:style 1-48 (Word 2010 wraps it: mc:Choice c14:style 101-148 with a + * c:style fallback). The gallery column decides the series colors: 1 = + * grayscale, 2 = accent cycle, 3-8 = single accent. + */ +function chartStyleVal(space: XNode): number | undefined { + let node = findChild(space, 'c:style') + if (!node) { + const alt = findChild(space, 'mc:AlternateContent') + const choice = alt ? findChild(alt, 'mc:Choice') : undefined + const fallback = alt ? findChild(alt, 'mc:Fallback') : undefined + node = + (choice ? childrenOf(choice).find((c) => (nameOf(c) ?? '').endsWith(':style')) : undefined) ?? + (fallback ? findChild(fallback, 'c:style') : undefined) + } + let v = parseInt(attrsOf(node ?? {})['val'] ?? '', 10) + if (v > 100) v -= 100 + return Number.isFinite(v) && v >= 1 && v <= 48 ? v : undefined +} + +/** Word's grayscale chart-style series colors (style column 1), approximated */ +const GRAYSCALE_PALETTE = ['595959', 'D9D9D9', 'A6A6A6', '404040', 'BFBFBF', '8C8C8C'] + +/** series color cycle for a chart style column, from the doc theme accents */ +function chartPalette( + styleVal: number | undefined, + theme?: ThemeColors | null, +): string[] | undefined { + const pos = styleVal === undefined ? 2 : ((styleVal - 1) % 8) + 1 + if (pos === 1) return GRAYSCALE_PALETTE + const accents = [ + theme?.accent1, + theme?.accent2, + theme?.accent3, + theme?.accent4, + theme?.accent5, + theme?.accent6, + ] + if (accents.some((a) => !a || !/^[0-9A-Fa-f]{6}$/.test(a))) return undefined + const list = accents.map((a) => a!.toUpperCase()) + if (pos === 2) return list + const base = list[pos - 3] + // monochromatic column: one hue, tint/shade ladder between series + return [ + base, + tintHex(base, 0.6), + shadeHex(base, 0.75), + tintHex(base, 0.3), + shadeHex(base, 0.5), + tintHex(base, 0.8), + ] +} + /** cx layoutId → closest classic display kind (degrade: shapes differ, data/labels/title survive) */ const CHARTEX_KINDS: Record = { clusteredColumn: 'bar', @@ -100,7 +354,11 @@ const CHARTEX_KINDS: Record = { function parseChartexPartXml(parsed: XNode[], partPath: string): ChartDisplay | null { const space = parsed.find((n) => nameOf(n) === 'cx:chartSpace') if (!space) return null - const chartData = findChild(space, 'cx:chartData') + // lenient lookup: consumers must skip unknown elements, and test corpora + // rename cx:chartData to prove it — accept any child that carries cx:data + const chartData = + findChild(space, 'cx:chartData') ?? + childrenOf(space).find((c) => findChild(c, 'cx:data') !== undefined) // data id → {cats, vals}: strDim/numDim carry cx:lvl point caches (the first // strDim level holds the leaf labels of hierarchical charts) const dataById = new Map() diff --git a/packages/docx-engine/src/generate.ts b/packages/docx-engine/src/generate.ts index 6585188..51c00b8 100644 --- a/packages/docx-engine/src/generate.ts +++ b/packages/docx-engine/src/generate.ts @@ -1678,6 +1678,58 @@ function tableCellXml( * after structural edits; untouched tables and text-only edits keep their * original XML through the byte-preserving patch paths. */ +const TBL_PR_ORDER = [ + 'w:tblStyle', + 'w:tblpPr', + 'w:tblOverlap', + 'w:bidiVisual', + 'w:tblStyleRowBandSize', + 'w:tblStyleColBandSize', + 'w:tblW', + 'w:jc', + 'w:tblCellSpacing', + 'w:tblInd', + 'w:tblBorders', + 'w:shd', + 'w:tblLayout', + 'w:tblCellMar', + 'w:tblLook', +] as const + +/** Replace/remove/insert one tblPr child while keeping the OOXML schema order. */ +function setTblPrChild(tblPr: string, name: string, xml: string | null): string { + const selfClosing = /^]*)?\/>$/.test(tblPr) + const open = selfClosing ? tblPr.replace(/\/>$/, '>') : /^]*)?>/.exec(tblPr)?.[0] + if (!open) return tblPr + const inner = selfClosing ? '' : tblPr.slice(open.length, tblPr.lastIndexOf('')) + const children = splitXmlChildren(inner).filter((child) => child.name !== name) + if (xml) { + const order = TBL_PR_ORDER.indexOf(name as (typeof TBL_PR_ORDER)[number]) + const at = children.findIndex((child) => { + const childOrder = TBL_PR_ORDER.indexOf(child.name as (typeof TBL_PR_ORDER)[number]) + return order >= 0 && childOrder >= 0 && childOrder > order + }) + children.splice(at < 0 ? children.length : at, 0, { name, xml }) + } + return `${open}${children.map((child) => child.xml).join('')}` +} + +function tableLookXml(look: NonNullable): string { + const val = + (look.firstRow ? 0x20 : 0) | + (look.lastRow ? 0x40 : 0) | + (look.firstColumn ? 0x80 : 0) | + (look.lastColumn ? 0x100 : 0) | + (look.bandedRows ? 0 : 0x200) | + (look.bandedColumns ? 0 : 0x400) + return ( + `` + ) +} + export function generateTableModelXml(model: TableModel, originalTableXml?: string): string { const columnCount = Math.max( 1, @@ -1727,18 +1779,52 @@ export function generateTableModelXml(model: TableModel, originalTableXml?: stri const rows = model.rows .map((row, ri) => { let gridColumn = 0 - const cells = row.map((cell) => { + const cells: string[] = [] + // gridGap placeholders are the display stand-ins for w:gridBefore/w:gridAfter: + // they advance the grid but are never written as w:tc + const edges = { before: 0, wBefore: 0, after: 0, wAfter: 0 } + for (const cell of row) { const span = Math.max(1, cell.colSpan ?? 1) const width = widths .slice(gridColumn, gridColumn + span) .reduce((sum, value) => sum + value, 0) gridColumn += span - return tableCellXml(cell, width) - }) + if (cell.gridGap) { + if (cells.length === 0) { + edges.before += span + edges.wBefore += width + } else { + edges.after += span + edges.wAfter += width + } + continue + } + cells.push(tableCellXml(cell, width)) + } // trPr: original bytes passed through (tblHeader/cantSplit etc.); row height is // replaced/inserted/removed per the model let trPr = model.rawTrPrs?.[ri] ?? '' if (trPr && !trPr.endsWith('')) trPr = '' + if (!trPr && (edges.before > 0 || edges.after > 0)) { + // CT_TrPr schema order: gridBefore, gridAfter, wBefore, wAfter + trPr = + '' + + (edges.before > 0 ? `` : '') + + (edges.after > 0 ? `` : '') + + (edges.before > 0 ? `` : '') + + (edges.after > 0 ? `` : '') + + '' + } + const repeatHeader = model.repeatHeaderRows?.[ri] + if (repeatHeader !== null && repeatHeader !== undefined) { + trPr = trPr.replace(/]*)?\/>/g, '') + if (repeatHeader) { + const tag = '' + trPr = trPr ? trPr.replace('', `${tag}`) : `${tag}` + } else if (/^]*)?>\s*<\/w:trPr>$/.test(trPr)) { + trPr = '' + } + } const h = model.rowHeightsTwips?.[ri] if (h != null && h > 0) { const rule = model.rowHeightRules?.[ri] === 'exact' ? 'exact' : 'atLeast' @@ -1760,52 +1846,93 @@ export function generateTableModelXml(model: TableModel, originalTableXml?: stri model.indentTwips && Math.round(model.indentTwips) !== 0 ? `` : '' - // floating table (P19 canvas): tblpPr + tblOverlap lead the fresh tblPr - // (CT_TblPr order: tblStyle, tblpPr, tblOverlap, …, tblW) - const tblpPr = model.floatPos - ? `` + - '' - : '' let tblPr = originalTblPr ?? - `${tblpPr}${tblInd}${borders}` + - `${cellMar}` + `${tblInd}${borders}` // Table style reference: '' = remove, non-empty = replace/insert (tblStyle is the // first tblPr child) if (model.tblStyleId !== undefined) { - tblPr = tblPr.replace(/]*\/>/, '') - if (model.tblStyleId !== '') { - const tag = `` - tblPr = //.test(tblPr) - ? tblPr.replace('', `${tag}`) - : tblPr.replace(/(]*)?>)/, `$1${tag}`) - // Style banding/header-row switches default to 04A0 (firstRow + banding) - if (!/]/.test(tblPr)) { - tblPr = tblPr.replace( - '', - '', - ) + tblPr = setTblPrChild( + tblPr, + 'w:tblStyle', + model.tblStyleId === '' ? null : ``, + ) + } + // Floating positioning + wrapping. null explicitly returns the table to the + // text flow; undefined leaves an imported tblpPr byte-for-byte intact. + const requestedFloatSide = + model.floatSide === undefined && model.floatPos + ? model.floatPos.xTwips > 4680 + ? 'right' + : 'left' + : model.floatSide + if (requestedFloatSide !== undefined || (!originalTblPr && model.floatPos)) { + if (!requestedFloatSide) { + tblPr = setTblPrChild(tblPr, 'w:tblpPr', null) + tblPr = setTblPrChild(tblPr, 'w:tblOverlap', null) + } else { + const pos = model.floatPos ?? { + xTwips: requestedFloatSide === 'right' ? Math.max(0, 9360 - totalWidth) : 0, + yTwips: 0, } + const distance = pos.distanceTwips ?? {} + const attr = (name: string, value: number | undefined) => + value == null ? '' : ` w:${name}="${Math.max(0, Math.round(value))}"` + const tag = + `` + tblPr = setTblPrChild(tblPr, 'w:tblpPr', tag) + tblPr = setTblPrChild(tblPr, 'w:tblOverlap', '') } } + const effectiveAutoFit = + model.autoFit ?? (!originalTblPr ? (model.autoLayout ? 'contents' : 'fixed') : null) + if (effectiveAutoFit) { + tblPr = setTblPrChild( + tblPr, + 'w:tblW', + effectiveAutoFit === 'window' + ? '' + : effectiveAutoFit === 'contents' + ? '' + : ``, + ) + tblPr = setTblPrChild( + tblPr, + 'w:tblLayout', + ``, + ) + } + if (model.cellMarTwips !== undefined) { + tblPr = setTblPrChild(tblPr, 'w:tblCellMar', cellMar || null) + } + if (model.tableLook) { + tblPr = setTblPrChild(tblPr, 'w:tblLook', tableLookXml(model.tableLook)) + } else if (model.tblStyleId && !/]/.test(tblPr)) { + tblPr = setTblPrChild( + tblPr, + 'w:tblLook', + tableLookXml({ + firstRow: true, + lastRow: false, + firstColumn: true, + lastColumn: false, + bandedRows: true, + bandedColumns: false, + }), + ) + } // Table alignment (w:jc): explicit align replaces/removes the original; // undefined keeps whatever the original tblPr carried if (model.align) { - tblPr = tblPr.replace(/]*\/>/, '') - if (model.align !== 'left') { - const tag = `` - // schema order puts w:jc right after w:tblW (after w:tblStyle when there is no tblW) - if (/]*\/>/.test(tblPr)) tblPr = tblPr.replace(/(]*\/>)/, `$1${tag}`) - else if (/]*\/>/.test(tblPr)) - tblPr = tblPr.replace(/(]*\/>)/, `$1${tag}`) - else if (//.test(tblPr)) - tblPr = tblPr.replace('', `${tag}`) - else tblPr = tblPr.replace(/(]*)?>)/, `$1${tag}`) - } + tblPr = setTblPrChild( + tblPr, + 'w:jc', + model.align === 'left' ? null : ``, + ) } return `${tblPr}${grid}${rows}` } diff --git a/packages/docx-engine/src/index.ts b/packages/docx-engine/src/index.ts index a268ae0..09725d7 100644 --- a/packages/docx-engine/src/index.ts +++ b/packages/docx-engine/src/index.ts @@ -57,6 +57,7 @@ export { patchChartWorkbookXlsxBase64, parseChartPartXml, patchChartPartXml, + lumHex, CHART_WORKBOOK_REL_TYPE, type ChartPatch, type ChartSeriesPatch, diff --git a/packages/docx-engine/src/ooxml-normalize.ts b/packages/docx-engine/src/ooxml-normalize.ts new file mode 100644 index 0000000..a735799 --- /dev/null +++ b/packages/docx-engine/src/ooxml-normalize.ts @@ -0,0 +1,431 @@ +/** + * ISO Strict OOXML (purl.oclc.org URIs) and non-canonical namespace prefixes, + * normalized to the transitional URIs + canonical prefixes that the rest of + * the engine matches literally (`w:p`, `wps:wsp`, rel-type strings...). Runs + * once per XML part at zip load, so parse offsets, patch splices and saved + * output all see the same bytes. + */ + +const T = 'http://schemas.openxmlformats.org/' +const S = 'http://purl.oclc.org/ooxml/' +const MS = 'http://schemas.microsoft.com/office/' + +/** ISO 29500 Strict namespace -> ECMA transitional equivalent (official 1:1 pairs) */ +const STRICT_TO_TRANSITIONAL: ReadonlyMap = new Map([ + [`${S}wordprocessingml/main`, `${T}wordprocessingml/2006/main`], + [`${S}officeDocument/relationships`, `${T}officeDocument/2006/relationships`], + [`${S}officeDocument/math`, `${T}officeDocument/2006/math`], + [`${S}officeDocument/extendedProperties`, `${T}officeDocument/2006/extended-properties`], + [`${S}officeDocument/customProperties`, `${T}officeDocument/2006/custom-properties`], + [`${S}officeDocument/docPropsVTypes`, `${T}officeDocument/2006/docPropsVTypes`], + [`${S}officeDocument/bibliography`, `${T}officeDocument/2006/bibliography`], + [`${S}officeDocument/characteristics`, `${T}officeDocument/2006/characteristics`], + [`${S}officeDocument/customXml`, `${T}officeDocument/2006/customXml`], + [`${S}schemaLibrary/main`, `${T}schemaLibrary/2006/main`], + [`${S}drawingml/main`, `${T}drawingml/2006/main`], + [`${S}drawingml/wordprocessingDrawing`, `${T}drawingml/2006/wordprocessingDrawing`], + [`${S}drawingml/spreadsheetDrawing`, `${T}drawingml/2006/spreadsheetDrawing`], + [`${S}drawingml/picture`, `${T}drawingml/2006/picture`], + [`${S}drawingml/chart`, `${T}drawingml/2006/chart`], + [`${S}drawingml/chartDrawing`, `${T}drawingml/2006/chartDrawing`], + [`${S}drawingml/diagram`, `${T}drawingml/2006/diagram`], + [`${S}drawingml/lockedCanvas`, `${T}drawingml/2006/lockedCanvas`], + [`${S}drawingml/compatibility`, `${T}drawingml/2006/compatibility`], +]) + +const STRICT_REL_BASE = `${S}officeDocument/relationships/` +const TRANSITIONAL_REL_BASE = `${T}officeDocument/2006/relationships/` +/** relationship-type tails that differ between the strict and transitional families */ +const REL_TAIL: Readonly> = { + extendedProperties: 'extended-properties', + customProperties: 'custom-properties', +} + +const W_URI = `${T}wordprocessingml/2006/main` +const A_URI = `${T}drawingml/2006/main` +const WP_URI = `${T}drawingml/2006/wordprocessingDrawing` +const WPS_URI = `${MS}word/2010/wordprocessingShape` +const MC_URI = `${T}markup-compatibility/2006` + +/** + * Prefixes the engine matches literally, keyed by (transitional) namespace URI. + * A binding of one of these URIs to any other prefix gets renamed — matching + * by URI + localName, never by localName alone, so unrelated namespaces that + * happen to reuse a local name are left untouched. + */ +const CANONICAL_PREFIX_BY_URI: ReadonlyMap = new Map([ + [W_URI, 'w'], + [`${T}officeDocument/2006/relationships`, 'r'], + [`${T}officeDocument/2006/math`, 'm'], + [A_URI, 'a'], + [WP_URI, 'wp'], + [`${T}drawingml/2006/picture`, 'pic'], + [`${T}drawingml/2006/chart`, 'c'], + [`${T}drawingml/2006/diagram`, 'dgm'], + [`${T}drawingml/2006/lockedCanvas`, 'lc'], + [MC_URI, 'mc'], + [WPS_URI, 'wps'], + [`${MS}word/2010/wordprocessingGroup`, 'wpg'], + [`${MS}word/2010/wordprocessingCanvas`, 'wpc'], + [`${MS}word/2010/wordml`, 'w14'], + [`${MS}word/2012/wordml`, 'w15'], + ['urn:schemas-microsoft-com:vml', 'v'], + ['urn:schemas-microsoft-com:office:office', 'o'], + ['urn:schemas-microsoft-com:office:word', 'w10'], +]) + +// Strict "universal measures" ("612pt", "2.54cm"...) -> the plain numeric unit +// each attribute is typed with (twips / half-points / eighth-points / points). +const PT_PER_UNIT: Readonly> = { + pt: 1, + in: 72, + pc: 12, + pi: 12, + cm: 72 / 2.54, + mm: 72 / 25.4, +} +const MEASURE_RE = /^(-?\d+(?:\.\d+)?)(mm|cm|in|pt|pc|pi)$/ +const TWIPS = 20 +const HALF_POINTS = 2 +const EIGHTH_POINTS = 8 +const POINTS = 1 + +type AttrFactors = Readonly> +const MARGIN_OR_BORDER: AttrFactors = { w: TWIPS, sz: EIGHTH_POINTS, space: POINTS } +/** element localName -> attr localName -> target units per point (w: namespace only) */ +const MEASURE_ATTRS: ReadonlyMap = new Map([ + ['pgSz', { w: TWIPS, h: TWIPS }], + [ + 'pgMar', + { + top: TWIPS, + right: TWIPS, + bottom: TWIPS, + left: TWIPS, + header: TWIPS, + footer: TWIPS, + gutter: TWIPS, + }, + ], + [ + 'ind', + { left: TWIPS, right: TWIPS, start: TWIPS, end: TWIPS, firstLine: TWIPS, hanging: TWIPS }, + ], + ['spacing', { before: TWIPS, after: TWIPS, line: TWIPS, val: TWIPS }], + ['tab', { pos: TWIPS }], + ['defaultTabStop', { val: TWIPS }], + ['cols', { space: TWIPS }], + ['col', { w: TWIPS, space: TWIPS }], + ['tblInd', { w: TWIPS }], + ['tblW', { w: TWIPS }], + ['tcW', { w: TWIPS }], + ['tblCellSpacing', { w: TWIPS }], + ['object', { dxaOrig: TWIPS, dyaOrig: TWIPS }], + ['trHeight', { val: TWIPS }], + ['framePr', { w: TWIPS, h: TWIPS, x: TWIPS, y: TWIPS, hSpace: TWIPS, vSpace: TWIPS }], + [ + 'tblpPr', + { + leftFromText: TWIPS, + rightFromText: TWIPS, + topFromText: TWIPS, + bottomFromText: TWIPS, + tblpX: TWIPS, + tblpY: TWIPS, + }, + ], + ['sz', { val: HALF_POINTS }], + ['szCs', { val: HALF_POINTS }], + ['kern', { val: HALF_POINTS }], + ['position', { val: HALF_POINTS }], + ['hps', { val: HALF_POINTS }], + ['hpsRaise', { val: HALF_POINTS }], + ['hpsBaseText', { val: HALF_POINTS }], + // cell margins (w) and borders (sz/space) share these element names + ['top', MARGIN_OR_BORDER], + ['bottom', MARGIN_OR_BORDER], + ['left', MARGIN_OR_BORDER], + ['right', MARGIN_OR_BORDER], + ['start', MARGIN_OR_BORDER], + ['end', MARGIN_OR_BORDER], + ['between', MARGIN_OR_BORDER], + ['bar', MARGIN_OR_BORDER], + ['insideH', MARGIN_OR_BORDER], + ['insideV', MARGIN_OR_BORDER], + ['tl2br', MARGIN_OR_BORDER], + ['tr2bl', MARGIN_OR_BORDER], +]) + +function convertMeasure(value: string, factor: number): string { + const m = MEASURE_RE.exec(value) + if (!m) return value + return String(Math.round(parseFloat(m[1]) * PT_PER_UNIT[m[2]]! * factor)) +} + +function mapUriValue(value: string): string { + const direct = STRICT_TO_TRANSITIONAL.get(value) + if (direct) return direct + if (value.startsWith(STRICT_REL_BASE)) { + const tail = value.slice(STRICT_REL_BASE.length) + return TRANSITIONAL_REL_BASE + (REL_TAIL[tail] ?? tail) + } + return value +} + +const XMLNS_DECL_RE = /xmlns(?::([A-Za-z_][\w.-]*))?\s*=\s*(["'])([^"']*)\2/g + +/** Cheap pre-check; normalizeOoxmlXml is only worth calling when this is true. */ +export function needsOoxmlNormalization(xml: string): boolean { + if (xml.includes(S)) return true + XMLNS_DECL_RE.lastIndex = 0 + let m: RegExpExecArray | null + while ((m = XMLNS_DECL_RE.exec(xml)) !== null) { + const canonical = CANONICAL_PREFIX_BY_URI.get(m[3]!) + if (canonical !== undefined && canonical !== (m[1] ?? '')) return true + } + return false +} + +interface Attr { + name: string + value: string +} + +interface Frame { + /** rewritten name to emit for the matching end tag */ + name: string + scoped: boolean + wspScope: boolean +} + +const TAG_NAME_RE = /^<\/?\s*([^\s/>]+)/ +const ATTR_RE = /([^\s=/>]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g + +function splitQName(name: string): [prefix: string, local: string] { + const i = name.indexOf(':') + return i < 0 ? ['', name] : [name.slice(0, i), name.slice(i + 1)] +} + +export function normalizeOoxmlXml(xml: string): string { + // scope stacks; lookups walk from the top so inner declarations shadow outer ones + const bindings: Array> = [new Map()] // source prefix -> transitional URI + const renames: Array> = [new Map()] // source prefix -> output prefix + const outBindings: Array> = [new Map()] // output prefix -> transitional URI + const frames: Frame[] = [] + let wspDepth = 0 + + const lookup = (stack: Array>, key: string): string | undefined => { + for (let i = stack.length - 1; i >= 0; i--) { + const v = stack[i]!.get(key) + if (v !== undefined) return v + } + return undefined + } + const renamePrefix = (prefix: string): string => lookup(renames, prefix) ?? prefix + + let out = '' + let cursor = 0 + while (cursor < xml.length) { + const start = xml.indexOf('<', cursor) + if (start < 0) break + out += xml.slice(cursor, start) + + const specialEnd = xml.startsWith('' + : xml.startsWith('' + : xml.startsWith('' + : xml.startsWith('' + : null + if (specialEnd !== null) { + const at = xml.indexOf(specialEnd, start + 2) + const end = at < 0 ? xml.length : at + specialEnd.length + out += xml.slice(start, end) + cursor = end + continue + } + + // find the tag end, tolerating '>' inside quoted attribute values + let quote = '' + let end = start + 1 + for (; end < xml.length; end += 1) { + const ch = xml[end]! + if (quote) { + if (ch === quote) quote = '' + } else if (ch === '"' || ch === "'") { + quote = ch + } else if (ch === '>') { + break + } + } + if (end >= xml.length) { + out += xml.slice(start) + cursor = xml.length + continue + } + const tag = xml.slice(start, end + 1) + cursor = end + 1 + + if (tag.startsWith('` + if (frame.scoped) { + bindings.pop() + renames.pop() + outBindings.pop() + } + if (frame.wspScope) wspDepth -= 1 + } else { + out += tag + } + continue + } + + const selfClosing = /\/\s*>$/.test(tag) + const name = TAG_NAME_RE.exec(tag)?.[1] + if (!name) { + out += tag + continue + } + const attrs: Attr[] = [] + ATTR_RE.lastIndex = TAG_NAME_RE.exec(tag)![0].length + let am: RegExpExecArray | null + while ((am = ATTR_RE.exec(tag)) !== null) { + attrs.push({ name: am[1]!, value: am[2] ?? am[3] ?? '' }) + } + + // namespace declarations open a scope; strict URIs map to transitional + const newBindings = new Map() + const declByPrefix = new Map() + for (const attr of attrs) { + if (attr.name !== 'xmlns' && !attr.name.startsWith('xmlns:')) continue + attr.value = mapUriValue(attr.value) + const declPrefix = attr.name === 'xmlns' ? '' : attr.name.slice(6) + newBindings.set(declPrefix, attr.value) + declByPrefix.set(declPrefix, attr) + } + let scoped = false + if (newBindings.size > 0) { + scoped = true + bindings.push(newBindings) + const newRenames = new Map() + renames.push(newRenames) + const newOut = new Map() + outBindings.push(newOut) + const drop = new Set() + const inject: Attr[] = [] + // declarations that keep their prefix claim it in the output first, so a + // rename can neither steal it nor be blocked by a prefix that is itself + // being renamed away + for (const [prefix, uri] of newBindings) { + const canonical = CANONICAL_PREFIX_BY_URI.get(uri) + if (canonical === undefined || canonical === prefix) newOut.set(prefix, uri) + } + for (const [prefix, uri] of newBindings) { + const canonical = CANONICAL_PREFIX_BY_URI.get(uri) + if (canonical === undefined || canonical === prefix) continue + const bound = lookup(outBindings, canonical) + if (bound !== undefined && bound !== uri) { + newOut.set(prefix, uri) // canonical prefix taken by another namespace: keep as-is + continue + } + newRenames.set(prefix, canonical) + // by captured reference: finding by name could hit a declaration whose + // name an earlier rename already rewrote to this prefix + const decl = declByPrefix.get(prefix)! + if (bound === uri) { + if (prefix !== '') drop.add(decl) // canonical binding already exists + } else if (prefix === '') { + inject.push({ name: `xmlns:${canonical}`, value: uri }) // keep xmlns= for other content + } else { + decl.name = `xmlns:${canonical}` + } + newOut.set(canonical, uri) + if (prefix === '') newOut.set('', uri) + } + if (drop.size > 0 || inject.length > 0) { + attrs.splice(0, attrs.length, ...attrs.filter((a) => !drop.has(a)), ...inject) + } + } + + const [prefix, local] = splitQName(name) + const elementUri = lookup(bindings, prefix) + + // Word 2013 strict writes wordprocessingShape children in the wp (drawing) + // namespace inside ; the engine + // matches wps:*, so shift those elements (only there) into the wps prefix. + let targetPrefix = renamePrefix(prefix) + if (wspDepth > 0 && elementUri === WP_URI) { + targetPrefix = 'wps' + if (lookup(outBindings, 'wps') !== WPS_URI) { + attrs.push({ name: 'xmlns:wps', value: WPS_URI }) + if (!scoped) { + scoped = true + bindings.push(new Map()) + renames.push(new Map()) + outBindings.push(new Map()) + } + outBindings[outBindings.length - 1]!.set('wps', WPS_URI) + } + } + const newName = targetPrefix === '' ? local : `${targetPrefix}:${local}` + + const inWNs = elementUri === W_URI + const factors = inWNs ? MEASURE_ATTRS.get(local) : undefined + const kept: Attr[] = [] + for (const attr of attrs) { + if (attr.name === 'xmlns' || attr.name.startsWith('xmlns:')) { + kept.push(attr) + continue + } + const [ap, al] = splitQName(attr.name) + if (inWNs && local === 'document' && al === 'conformance') continue + if (ap !== '' && ap !== 'xml') { + const np = renamePrefix(ap) + if (np !== ap) attr.name = `${np}:${al}` + } + // MCE prefix lists must keep referring to the renamed prefixes + const mceList = + (al === 'Ignorable' || al === 'MustUnderstand') && lookup(bindings, ap) === MC_URI + ? true + : al === 'Requires' && ap === '' && elementUri === MC_URI + if (mceList) { + attr.value = attr.value + .split(/\s+/) + .map((p) => renamePrefix(p)) + .join(' ') + } else { + attr.value = mapUriValue(attr.value) + const factor = factors?.[al] + if (factor !== undefined && (ap === '' || lookup(bindings, ap) === W_URI)) { + attr.value = convertMeasure(attr.value, factor) + } + } + kept.push(attr) + } + + const attrText = kept.map((a) => ` ${a.name}="${a.value.replace(/"/g, '"')}"`).join('') + out += `<${newName}${attrText}${selfClosing ? '/>' : '>'}` + + const wspScope = + !selfClosing && local === 'graphicData' && elementUri === A_URI + ? attrs.some((a) => a.name === 'uri' && a.value === WPS_URI) + : false + if (wspScope) wspDepth += 1 + + if (selfClosing) { + if (scoped) { + bindings.pop() + renames.pop() + outBindings.pop() + } + } else { + frames.push({ name: newName, scoped, wspScope }) + } + } + out += xml.slice(cursor) + return out +} diff --git a/packages/docx-engine/src/parse.ts b/packages/docx-engine/src/parse.ts index 7f487e4..f79b53f 100644 --- a/packages/docx-engine/src/parse.ts +++ b/packages/docx-engine/src/parse.ts @@ -13,9 +13,15 @@ import { sectionSettingsFromXml, xmlFlagOn } from './section' import { findSourcesPart, parseSourcesXml } from './sources' import { decodeSymbolChar, decodeSymbolText } from './symbol-fonts' import { FONT_TABLE_PART_PATH, parseFontTable } from './font-table' -import { THEME_PART_PATH, readThemeColors, readThemeFonts, resolveThemeColor } from './theme' +import { + DEFAULT_THEME_COLORS, + THEME_PART_PATH, + readThemeColors, + readThemeFonts, + resolveThemeColor, +} from './theme' import { PAGE_MARK, TOTAL_PAGES_MARK } from './types' -import { loadDocxZip } from './zip-load' +import { assertZipWithinLimits, loadDocxZip } from './zip-load' import type { Block, @@ -72,39 +78,7 @@ import { type XNode, } from './xml-utils' -const MAX_ZIP_PARTS = 10000 -const MAX_PART_UNCOMPRESSED_BYTES = 512 * 1024 * 1024 -const MAX_TOTAL_UNCOMPRESSED_BYTES = 1.5 * 1024 * 1024 * 1024 - -/** - * Reject zip bombs before any part is inflated, using the declared - * uncompressed sizes from the central directory (JSZip keeps them in - * the lazy `_data` compressed object). - */ -export function assertZipWithinLimits(zip: JSZip): void { - const files = Object.values(zip.files).filter((f) => !f.dir) - if (files.length > MAX_ZIP_PARTS) { - throw new Error(`docx rejected: ${files.length} parts exceeds the ${MAX_ZIP_PARTS} limit`) - } - let total = 0 - for (const file of files) { - const size = - (file as unknown as { _data?: { uncompressedSize?: number } })._data?.uncompressedSize ?? 0 - if (size > MAX_PART_UNCOMPRESSED_BYTES) { - throw new Error( - `docx rejected: part ${file.name} declares ${size} uncompressed bytes ` + - `(limit ${MAX_PART_UNCOMPRESSED_BYTES})`, - ) - } - if (size > 0) total += size - } - if (total > MAX_TOTAL_UNCOMPRESSED_BYTES) { - throw new Error( - `docx rejected: total uncompressed size ${total} exceeds the ` + - `${MAX_TOTAL_UNCOMPRESSED_BYTES} limit`, - ) - } -} +export { assertZipWithinLimits } /** w:br w:type → run-text control char (\f page, \v column; else soft \n) */ const BREAK_CHAR: Record = { page: '\f', column: '\v' } @@ -234,6 +208,7 @@ export async function parseDocx(bytes: Uint8Array): Promise]*\/>|/g)] @@ -262,6 +237,7 @@ export async function parseDocx(bytes: Uint8Array): Promise /** "footnote:" / "endnote:" -> display number */ noteNumbers: Map - /** live palette for w:themeColor resolution, null when the doc has no theme */ + /** live palette for w:themeColor resolution (built-in Office palette when the doc has no theme part) */ themeColors?: ThemeColors | null /** theme font scheme for w:asciiTheme/... resolution */ themeFonts?: ThemeFonts | null /** pre-resolved a:blip rId -> data/external URL for pictures inside w:tbl (extractCell is sync, media reads are async) */ mediaByRid?: Map + /** pre-fetched external textbox parts (wps:txbx r:txbx -> word/txbx*.xml content; extractTextboxes is sync) */ + externalTxbxByRid?: Map /** comment ids anchored only by a bare w:commentReference (no range markers anywhere in document.xml, LibreOffice style) */ referenceOnlyComments?: Set /** page geometry of the section governing a document.xml byte offset (page/margin-anchored drawings) */ @@ -968,7 +946,29 @@ async function buildBlock( !detect.includes(' 0 && + (ctx.firstPageBreakAt === undefined || el.start < ctx.firstPageBreakAt), + } + : undefined, + ) const strayText = plainText(stripTextboxes(detect)).trim() // paragraphs mixing an inline VML picture with real text stay on the // editable run-image path below; boxes would drop the picture @@ -1048,13 +1048,16 @@ async function buildBlock( ...(hPt > 0 ? { ruleThicknessPx: Math.max(1, Math.round((hPt / 72) * 96)) } : {}), } } - // OLE embed sharing the paragraph with real text: an "Embedded object" - // block renders only the preview picture, silently dropping the text. - // Keep the text editable with the object as a run-level image (the + // OLE embed sharing the paragraph with real text or another drawing: an + // "Embedded object" block renders only the preview picture, silently + // dropping the rest. Keep the paragraph on the run-image path (the // w:object fragment round-trips verbatim). Only when every object's // preview resolved — otherwise the passthrough below at least keeps the // object bytes safe. - if (detect.includes('/g) ?? [] const displayable = objects.every((o) => { @@ -1072,8 +1075,13 @@ async function buildBlock( } } if (detect.includes(' w:tab) don't count. */ +const LAYOUT_RUN_CONTENT = /^w:(?:br|cr|tab|sym|footnoteReference|endnoteReference)$/ +function hasLayoutRunContent(node: XNode): boolean { + for (const child of childrenOf(node)) { + const name = nameOf(child) + if (name === 'w:pPr' || name === 'w:rPr') continue + if (name !== undefined && LAYOUT_RUN_CONTENT.test(name)) return true + if (hasLayoutRunContent(child)) return true + } + return false +} + /** parse a w:p as editable text content (paragraph / heading / listItem) */ function buildTextParagraph( base: Pick, @@ -1351,6 +1373,20 @@ function buildTextParagraph( if (styleId && ctx.styles.get(styleId)?.display?.vanish === true && staysVanished(xml)) { return { ...base, type: 'passthrough', label: 'Hidden paragraph', invisibleMarker: true } } + // empty paragraph whose mark is hidden by direct w:vanish (label/card + // templates end on one): Word gives it no height — rendering it as a normal + // empty line can push a trailing blank page + const markRPr = pPr ? findChild(pPr, 'w:rPr') : undefined + if ( + markRPr && + onOffOf(markRPr, 'w:vanish') === true && + onOffOf(markRPr, 'w:specVanish') !== true && + plainText(xml).trim() === '' && + staysVanished(xml) && + !hasLayoutRunContent(pNode) + ) { + return { ...base, type: 'passthrough', label: 'Hidden paragraph', invisibleMarker: true } + } let format = pPr ? extractParaFormat(pPr) : undefined // style-chain autoSpace off reaches the block: the renderer reads it per paragraph if (format?.autoSpace === undefined && styleId) { @@ -2231,16 +2267,24 @@ interface DrawingAnchorMeta { * fragment). Used to attach each drawing's own anchor placement to the shapes * it carries — a paragraph can anchor several drawings at distinct offsets. */ +/** next `` open tag at or after `from`, attributes tolerated + * (``, TestFiles mcdoc) */ +function drawingOpenAt(xml: string, from: number): number { + const re = /]/g + re.lastIndex = from + return re.exec(xml)?.index ?? -1 +} + function topLevelDrawings(xml: string): string[] { const out: string[] = [] let i = 0 for (;;) { - const start = xml.indexOf('', i) + const start = drawingOpenAt(xml, i) if (start === -1) break let depth = 0 let j = start for (;;) { - const open = xml.indexOf('', j + 1) + const open = drawingOpenAt(xml, j + 1) const close = xml.indexOf('', j + 1) if (close === -1) return out // malformed; bail with what we have if (open !== -1 && open < close) { @@ -2331,7 +2375,12 @@ function resolveAnchorPagePos( sect: SectionSettings | undefined, ): ResolvedAnchorPos | null { if (!sect) return null - if (meta.relH !== 'page' && meta.relH !== 'margin') return null + // column-relative align in a single-column section: the column IS the margin + // box, so it resolves identically (multi-column needs the hosting column and + // stays on the legacy flow placement) + const relH = + meta.relH === 'column' && sect.columns <= 1 && meta.pctH === undefined ? 'margin' : meta.relH + if (relH !== 'page' && relH !== 'margin') return null if (meta.pctH === undefined && meta.alignH === undefined) return null const pageW = sect.pageWidth * EMU_PER_TWIP const pageH = sect.pageHeight * EMU_PER_TWIP @@ -2339,7 +2388,7 @@ function resolveAnchorPagePos( const marR = sect.marginRight * EMU_PER_TWIP const marT = sect.marginTop * EMU_PER_TWIP const w = meta.extentXEmu ?? 0 - const refW = meta.relH === 'page' ? pageW : pageW - marL - marR + const refW = relH === 'page' ? pageW : pageW - marL - marR const relX = meta.pctH !== undefined ? Math.round((refW * meta.pctH) / 100000) @@ -2348,7 +2397,7 @@ function resolveAnchorPagePos( : meta.alignH === 'right' || meta.alignH === 'outside' ? refW - w : 0 - const pageX = meta.relH === 'page' ? relX : marL + relX + const pageX = relH === 'page' ? relX : marL + relX const pos: ResolvedAnchorPos = { xEmu: pageX - marL, outsideColumn: pageX + w <= marL || pageX >= pageW - marR, @@ -2586,6 +2635,22 @@ function extractTextboxes( collectTopNodes(childrenOf(shape), 'w:txbxContent', topContents) const ordinal = txbxOrdinal if (!nested) txbxOrdinal += topContents.length + // external textbox part (wps:txbx r:txbx, word/txbx*.xml): its root + // w14:txbx carries the w:p list directly. Display-only — it stays out of + // topContents, so the box takes no save ordinal and turns readOnly below. + if (contents.length === 0) { + const extRid = attrsOf(findChild(shape, 'wps:txbx') ?? {})['r:txbx'] + const extXml = extRid ? ctx.externalTxbxByRid?.get(extRid) : undefined + if (extXml) { + try { + const nodes = xmlParser.parse(extXml) as XNode[] + const root = nodes.find((n) => nameOf(n)?.endsWith(':txbx')) + if (root) contents.push(root) + } catch { + /* unreadable part: fall through to the textless-shape path */ + } + } + } const spPr = findChild(shape, 'wps:spPr') const prstOf = spPr ? attrsOf(findChild(spPr, 'a:prstGeom') ?? {})['prst'] : undefined const custGeomNode = spPr ? findChild(spPr, 'a:custGeom') : undefined @@ -3585,12 +3650,12 @@ function extractRuns( } } else if (SIMPLE_INLINE_FIELD_RE.test(fieldInstr)) { pushRun({ text: fieldCached || ' ', instrField: fieldInstr.trim() }, rev) - } else if (/^\s*HYPERLINK[\s"]/.test(fieldInstr)) { - // non-convertible HYPERLINK (backslash file target, \l bookmark...): - // the cached result is still the visible text — keep it as plain runs. - // Body paragraphs with these fields stay on the passthrough path - // (onlyXeFields rejects them), so this only feeds display-only - // contexts such as textbox content. + } else if (fieldCachedRuns.length > 0) { + // any other field (HYPERLINK with switches, MERGEFIELD mail-merge + // labels...): the cached result is still the visible text — keep it + // as plain runs. Body paragraphs with these fields stay on the + // passthrough path (onlyXeFields rejects them), so this only feeds + // display-only contexts such as table cells and textbox content. for (const cached of fieldCachedRuns) pushRun(cached, rev) } fieldInstr = '' @@ -3655,17 +3720,27 @@ function extractRuns( else pendingRefIds.push(id) } } - const run = buildRun( - node, - link, - ctx.themeColors, - ctx.themeFonts, - withImages ? ctx.mediaByRid : undefined, - ctx.styles, - paraRtl, - ctx.xmlSpacePreserve, - ) - if (run) pushRun(run, rev) + // Run.image is singular: a run carrying several drawings/picts must split + // into one run per picture, or every picture past the first is dropped — + // and permanently lost from the file once the paragraph is edited + const nodes = + withImages && + childrenOf(node).filter((c) => IMAGE_RUN_CHILDREN.has(nameOf(c) ?? '')).length > 1 + ? splitImageRun(node) + : [node] + for (const part of nodes) { + const run = buildRun( + part, + link, + ctx.themeColors, + ctx.themeFonts, + withImages ? ctx.mediaByRid : undefined, + ctx.styles, + paraRtl, + ctx.xmlSpacePreserve, + ) + if (run) pushRun(run, rev) + } } const walk = (nodes: XNode[], link?: Run['link'], rev?: RevCtx) => { for (const node of nodes) { @@ -3707,6 +3782,12 @@ function extractRuns( walk(childrenOf(node), { href, rId, ...(tooltip ? { tooltip } : {}) }, rev) } else if (name === 'w:smartTag' || name === 'w:sdt' || name === 'w:sdtContent') { walk(childrenOf(node), link, rev) + } else if (name === 'w:fldSimple') { + // single-element field form: the children are the cached result runs + // (Word shows them until the field refreshes) — MERGEFIELD address + // labels, DATE stamps... Dropping them blanks mail-merge documents. + // Skip only inside an enclosing complex field's instruction phase. + if (fieldDepth === 0 || fieldSeparated) walk(childrenOf(node), link, rev) } else if (name === 'w:br') { // Word honors a sitting outside any (direct child of w:p / w:ins) pushRun({ text: BREAK_CHAR[attrsOf(node)['w:type'] ?? ''] ?? '\n' }, rev) @@ -3717,6 +3798,26 @@ function extractRuns( return mergeRuns(runs) } +const IMAGE_RUN_CHILDREN = new Set(['w:drawing', 'w:pict', 'w:object', 'mc:AlternateContent']) + +/** synthetic single-picture runs (rPr copied into each) in document order */ +function splitImageRun(rNode: XNode): XNode[] { + const attrs = rNode[':@'] + const rPr = findChild(rNode, 'w:rPr') + const parts: XNode[][] = [] + let current: XNode[] = rPr ? [rPr] : [] + for (const child of childrenOf(rNode)) { + if (nameOf(child) === 'w:rPr') continue + current.push(child) + if (IMAGE_RUN_CHILDREN.has(nameOf(child) ?? '')) { + parts.push(current) + current = rPr ? [rPr] : [] + } + } + if (current.length > (rPr ? 1 : 0)) parts.push(current) + return parts.map((children) => ({ 'w:r': children, ...(attrs ? { ':@': attrs } : {}) })) +} + /** exact fragments in document order (w:ruby has no attributes and cannot nest) */ function rubyFragmentsOf(xml: string): string[] { return xml.match(/[\s\S]*?<\/w:ruby>/g) ?? [] @@ -3896,6 +3997,14 @@ function buildRun( if (meta.imageWrap) image.wrap = meta.imageWrap if (meta.imageOffsetXEmu !== undefined) image.offsetXEmu = meta.imageOffsetXEmu if (meta.imageOffsetYEmu !== undefined) image.offsetYEmu = meta.imageOffsetYEmu + if (meta.imageWrapDistTopEmu !== undefined) + image.wrapDistTopEmu = meta.imageWrapDistTopEmu + if (meta.imageWrapDistBottomEmu !== undefined) + image.wrapDistBottomEmu = meta.imageWrapDistBottomEmu + if (meta.imageWrapDistLeftEmu !== undefined) + image.wrapDistLeftEmu = meta.imageWrapDistLeftEmu + if (meta.imageWrapDistRightEmu !== undefined) + image.wrapDistRightEmu = meta.imageWrapDistRightEmu // Word centers the object on the anchor line (tdf#162551: the picture // juts above the line rather than hanging below it) if ( @@ -3912,11 +4021,14 @@ function buildRun( // inline OLE embed (w:object, whose preview is also a v:imagedata): same // run-level image treatment; the fragment round-trips verbatim on save if (!image) { - const pict = - findChild(rNode, 'w:pict') ?? - findChild(rNode, 'w:object') ?? - (choice && (findChild(choice, 'w:pict') ?? findChild(choice, 'w:object'))) - if (pict) { + // a run can carry several (an empty w:pict next to the real w:object): + // take the first with a resolvable preview picture + const picts = [ + findChild(rNode, 'w:pict'), + findChild(rNode, 'w:object'), + ...(choice ? [findChild(choice, 'w:pict'), findChild(choice, 'w:object')] : []), + ].filter((n): n is XNode => n !== undefined) + for (const pict of picts) { const pictXml = serializeXNode(pict) const rId = /]*r:id="([^"]+)"/.exec(pictXml)?.[1] const dataUrl = rId ? mediaByRid.get(rId) : undefined @@ -3933,6 +4045,7 @@ function buildRun( else if (wTw > 0) image.widthPx = Math.round(wTw / 15) if (h > 0) image.heightPx = Math.round((h / 72) * 96) else if (hTw > 0) image.heightPx = Math.round(hTw / 15) + break } } } @@ -3987,8 +4100,11 @@ function buildRun( const spc = parseInt(attrsOf(findChild(rPr, 'w:spacing') ?? {})['w:val'] ?? '', 10) if (spc) run.charSpacingTwips = spc // w:caps wins over w:smallCaps when both are on (Word) - if (onOffOf(rPr, 'w:caps')) run.caps = 'all' - else if (onOffOf(rPr, 'w:smallCaps')) run.caps = 'small' + const capsOn = onOffOf(rPr, 'w:caps') + const smallCapsOn = onOffOf(rPr, 'w:smallCaps') + if (capsOn) run.caps = 'all' + else if (smallCapsOn) run.caps = 'small' + else if (capsOn === false || smallCapsOn === false) run.caps = 'none' const wScale = parseInt(attrsOf(findChild(rPr, 'w:w') ?? {})['w:val'] ?? '', 10) if (wScale > 0 && wScale !== 100) run.charScalePct = wScale const highlight = attrsOf(findChild(rPr, 'w:highlight') ?? {})['w:val'] @@ -4141,7 +4257,8 @@ function tcwColumnWidths(tbl: XNode): number[] | undefined { const cols: number[] = [] let colCount = 0 for (const tr of childrenThroughSdt(tbl, 'w:tr')) { - let idx = 0 + const edges = rowGridEdges(tr) + let idx = edges.before for (const tc of childrenThroughSdt(tr, 'w:tc')) { const tcPr = findChild(tc, 'w:tcPr') const span = Math.max( @@ -4154,13 +4271,155 @@ function tcwColumnWidths(tbl: XNode): number[] | undefined { if (span === 1 && w > 0) cols[idx] = Math.max(cols[idx] || 0, w) idx += span } - colCount = Math.max(colCount, idx) + colCount = Math.max(colCount, idx + edges.after) } if (colCount === 0) return undefined for (let i = 0; i < colCount; i++) if (!(cols[i] > 0)) return undefined return cols.slice(0, colCount) } +/** trPr w:gridBefore/w:gridAfter column counts plus their w:wBefore/w:wAfter widths (twips) */ +function rowGridEdges(tr: XNode): { + before: number + after: number + wBefore?: number + wAfter?: number +} { + const trPr = findChild(tr, 'w:trPr') + if (!trPr) return { before: 0, after: 0 } + const count = (name: string) => { + const v = Number(attrsOf(findChild(trPr, name) ?? {})['w:val']) + return Number.isFinite(v) && v > 0 ? Math.floor(v) : 0 + } + const width = (name: string) => { + const a = attrsOf(findChild(trPr, name) ?? {}) + const w = !a['w:type'] || a['w:type'] === 'dxa' ? Number(a['w:w']) : NaN + return w > 0 ? w : undefined + } + return { + before: count('w:gridBefore'), + after: count('w:gridAfter'), + wBefore: width('w:wBefore'), + wAfter: width('w:wAfter'), + } +} + +/** boundary positions within TOL twips fuse into one grid line (rounding drift across generator-written rows) */ +const GRID_SNAP_TOL = 20 + +/** + * Repair grids whose rows do not all span the declared column count (legacy + * generators emit gridSpan/tblGrid pairs that disagree with the cells' w:tcW). + * Word lays each such row out from the cells' preferred widths, so the true + * grid is the union of every row's tcW boundaries; without this, a cell mapped + * onto a leftover sliver column collapses to one character per line. + * Mutates the cells' colSpan and returns the rebuilt column widths, or + * undefined when the grid is already consistent or a width is unresolvable. + */ +function reconcileGridColumns( + rows: TableCell[][], + rowTcws: Array>, + gridCols: number[] | undefined, +): number[] | undefined { + const spanSums = rows.map((row) => row.reduce((sum, c) => sum + (c.colSpan ?? 1), 0)) + const colCount = gridCols?.length ?? Math.max(...spanSums) + if (spanSums.every((sum) => sum === colCount)) return undefined + const rowBounds: number[][] = [] + for (let r = 0; r < rows.length; r++) { + const bounds: number[] = [] + let x = 0 + let pos = 0 + for (let c = 0; c < rows[r].length; c++) { + const span = rows[r][c].colSpan ?? 1 + let w = rowTcws[r][c] + if (!(w !== undefined && w > 0)) { + w = gridCols?.slice(pos, pos + span).reduce((sum, v) => sum + v, 0) + } + if (!(w !== undefined && w > 0)) return undefined + x += w + bounds.push(x) + pos += span + } + rowBounds.push(bounds) + } + const sorted = rowBounds.flat().sort((a, b) => a - b) + const reps: number[] = [] + for (const b of sorted) { + if (reps.length === 0 || b - reps[reps.length - 1] > GRID_SNAP_TOL) reps.push(b) + } + // Word caps table grids at 63 columns; a wider union means garbage input + if (reps.length === 0 || reps.length > 96) return undefined + const repIndex = (b: number) => { + let lo = 0 + let hi = reps.length - 1 + while (lo < hi) { + const mid = (lo + hi + 1) >> 1 + if (reps[mid] <= b) lo = mid + else hi = mid - 1 + } + return reps[lo] <= b && b - reps[lo] <= GRID_SNAP_TOL ? lo : -1 + } + const newSpans: number[][] = [] + for (const bounds of rowBounds) { + const spans: number[] = [] + let prev = -1 + for (const b of bounds) { + const idx = repIndex(b) + if (idx <= prev) return undefined + spans.push(idx - prev) + prev = idx + } + newSpans.push(spans) + } + rows.forEach((row, r) => + row.forEach((cell, c) => { + if (newSpans[r][c] > 1) cell.colSpan = newSpans[r][c] + else delete cell.colSpan + }), + ) + return reps.map((v, i) => v - (i > 0 ? reps[i - 1] : 0)) +} + +/** ST_Shd pct values that are not the literal digits in the name */ +const SHD_PCT_EXACT: Record = { pct12: 12.5, pct37: 37.5, pct62: 62.5, pct87: 87.5 } + +function blendHex(fg: string, bg: string, ratio: number): string { + const ch = (hex: string, i: number) => parseInt(hex.slice(i, i + 2), 16) + const mix = (i: number) => + Math.round(ch(fg, i) * ratio + ch(bg, i) * (1 - ratio)) + .toString(16) + .padStart(2, '0') + return (mix(0) + mix(2) + mix(4)).toUpperCase() +} + +/** + * w:shd → single display color. Patterns (pctNN, stripes, crosses) approximate + * as the pattern color blended over the fill at the pattern's ink coverage — + * Word's actual dot/stripe raster is out of scope for cell backgrounds. + */ +function shdDisplayFill(shd: XNode | undefined): string | undefined { + if (!shd) return undefined + const a = attrsOf(shd) + const hex = (v: string | undefined) => { + const s = v ? stripHash(v) : undefined + return s && /^[0-9a-fA-F]{6}$/.test(s) ? s : undefined + } + const fill = a['w:fill'] === 'auto' ? undefined : hex(a['w:fill']) + const val = a['w:val'] ?? 'clear' + if (val === 'clear' || val === 'nil') return fill + const ink = a['w:color'] === 'auto' ? undefined : hex(a['w:color']) + if (val === 'solid') return ink ?? '000000' + let ratio: number | undefined + if (val.startsWith('pct')) { + ratio = (SHD_PCT_EXACT[val] ?? Number(val.slice(3))) / 100 + if (!(ratio > 0 && ratio <= 1)) ratio = undefined + } else if (/stripe|cross/i.test(val)) { + ratio = val.startsWith('thin') ? 0.25 : 0.5 + } + if (ratio === undefined) return fill + return blendHex(ink ?? '000000', fill ?? 'FFFFFF', ratio) +} + /** Real documents rarely nest past 3-4 levels; below the cap the subtree flattens so stress files (POI nests 5000) cannot blank the page. */ const MAX_TABLE_NEST_DEPTH = 8 @@ -4197,14 +4456,35 @@ function flattenedTableModel(tbl: XNode): TableModel | undefined { return { rows: [[cell]], autoLayout: true } } +function tableLookOf(tblPr: XNode | undefined): NonNullable { + const look = attrsOf(findChild(tblPr ?? {}, 'w:tblLook') ?? {}) + const bits = parseInt(look['w:val'] ?? '', 16) + const flag = (attr: string, bit: number, dflt: boolean): boolean => + look[attr] !== undefined + ? look[attr] !== '0' && look[attr] !== 'false' + : Number.isFinite(bits) + ? (bits & bit) !== 0 + : dflt + return { + firstRow: flag('w:firstRow', 0x20, true), + lastRow: flag('w:lastRow', 0x40, false), + firstColumn: flag('w:firstColumn', 0x80, true), + lastColumn: flag('w:lastColumn', 0x100, false), + bandedRows: !flag('w:noHBand', 0x200, false), + bandedColumns: !flag('w:noVBand', 0x400, true), + } +} + function extractTableModel(tbl: XNode, ctx: BuildContext, depth = 1): TableModel | undefined { const grid = findChild(tbl, 'w:tblGrid') let colWidthsPct: number[] | undefined let colWidthsTwips: number[] | undefined + let gridWidthsRaw: number[] | undefined if (grid) { const widths = findChildren(grid, 'w:gridCol').map((c) => Number(attrsOf(c)['w:w']) || 0) const total = widths.reduce((a, b) => a + b, 0) if (total > 0) { + gridWidthsRaw = widths colWidthsPct = widths.map((w) => (w / total) * 100) if (widths.every((w) => w > 0)) colWidthsTwips = widths } @@ -4248,6 +4528,7 @@ function extractTableModel(tbl: XNode, ctx: BuildContext, depth = 1): TableModel const tblIndTwips = !tblInd['w:type'] || tblInd['w:type'] === 'dxa' ? Number(tblInd['w:w']) : NaN const tblpPr = attrsOf(findChild(tblPrNode ?? {}, 'w:tblpPr') ?? {}) let floatSide: TableModel['floatSide'] + let floatPos: TableModel['floatPos'] if (Object.keys(tblpPr).length > 0) { const xSpec = tblpPr['w:tblpXSpec'] // no alignment keyword: an absolute X past mid-body (~9360 twips of usable @@ -4256,39 +4537,111 @@ function extractTableModel(tbl: XNode, ctx: BuildContext, depth = 1): TableModel xSpec === 'right' || xSpec === 'outside' || (!xSpec && Number(tblpPr['w:tblpX']) > 4680) ? 'right' : 'left' + const x = Number(tblpPr['w:tblpX']) + const y = Number(tblpPr['w:tblpY']) + const distanceTwips: CellMargins = {} + const distanceAttrs = { + top: 'w:topFromText', + right: 'w:rightFromText', + bottom: 'w:bottomFromText', + left: 'w:leftFromText', + } as const + for (const [side, attr] of Object.entries(distanceAttrs) as Array< + [keyof CellMargins, (typeof distanceAttrs)[keyof typeof distanceAttrs]] + >) { + const value = Number(tblpPr[attr]) + if (Number.isFinite(value) && value >= 0) distanceTwips[side] = value + } + const horzAnchor = tblpPr['w:horzAnchor'] + const vertAnchor = tblpPr['w:vertAnchor'] + floatPos = { + xTwips: Number.isFinite(x) ? x : floatSide === 'right' ? 9360 : 0, + yTwips: Number.isFinite(y) ? y : 0, + ...(horzAnchor === 'page' || horzAnchor === 'margin' || horzAnchor === 'text' + ? { horzAnchor } + : {}), + ...(vertAnchor === 'page' || vertAnchor === 'margin' || vertAnchor === 'text' + ? { vertAnchor } + : {}), + ...(Object.keys(distanceTwips).length > 0 ? { distanceTwips } : {}), + } } + const spacingOf = (node: XNode | undefined) => { + const a = attrsOf(findChild(node ?? {}, 'w:tblCellSpacing') ?? {}) + const w = !a['w:type'] || a['w:type'] === 'dxa' ? Number(a['w:w']) : NaN + return w > 0 ? w : undefined + } + let cellSpacing = spacingOf(tblPrNode) + const tblFill = shdDisplayFill(findChild(tblPrNode ?? {}, 'w:shd')) + const rows: TableCell[][] = [] + // per-cell w:tcW (dxa twips), aligned with rows: grid-repair input + const rowTcws: Array> = [] + const rowEdges: Array> = [] const rowHeightsTwips: Array = [] const rowHeightRules: NonNullable = [] + const repeatHeaderRows: boolean[] = [] const rowRevisions: NonNullable = [] for (const tr of childrenThroughSdt(tbl, 'w:tr')) { const cells: TableCell[] = [] + const tcws: Array = [] for (const tc of childrenThroughSdt(tr, 'w:tc')) { const cell = extractCell(tc, ctx, depth) + const a = attrsOf(findChildren(findChild(tc, 'w:tcPr') ?? {}, 'w:tcW').at(-1) ?? {}) + const rawW = !a['w:type'] || a['w:type'] === 'dxa' ? Number(a['w:w']) : NaN + const tcw = rawW > 0 ? rawW : undefined const prev = cells[cells.length - 1] // Legacy horizontal merge: continue cells fold into the cell to their left (same effect // as gridSpan) if (cell.hMerge === 'continue' && prev) { prev.colSpan = (prev.colSpan ?? 1) + (cell.colSpan ?? 1) + const prevW = tcws[tcws.length - 1] + tcws[tcws.length - 1] = prevW !== undefined && tcw !== undefined ? prevW + tcw : undefined continue } cells.push(cell) + tcws.push(tcw) } if (cells.length > 0) { rows.push(cells) + rowTcws.push(tcws) + rowEdges.push(rowGridEdges(tr)) const trPr = findChild(tr, 'w:trPr') + cellSpacing = cellSpacing ?? spacingOf(trPr) const trH = trPr ? attrsOf(findChild(trPr, 'w:trHeight') ?? {}) : {} const h = Number(trH['w:val']) const hasH = Number.isFinite(h) && h > 0 // Word clamps trHeight to 31680 twips / 22in (MS-OI29500 2.1.51); some generators leak EMU-scale values here rowHeightsTwips.push(hasH ? Math.min(h, 31680) : null) rowHeightRules.push(hasH ? (trH['w:hRule'] === 'exact' ? 'exact' : 'atLeast') : null) + repeatHeaderRows.push(trPr ? boolProp(trPr, 'w:tblHeader') : false) rowRevisions.push(trPr ? rowRevisionOf(trPr) : null) } } if (rows.length === 0) return undefined applyTableStyleDisplay(rows, findChild(tbl, 'w:tblPr'), ctx) + // w:gridBefore/w:gridAfter offset rows within the grid: placeholder cells keep + // the offset visible (inserted after style banding so real cells keep their look) + if (rowEdges.some((e) => e.before > 0 || e.after > 0)) { + rows.forEach((cells, i) => { + const e = rowEdges[i] + if (e.before > 0) { + cells.unshift({ paras: [], gridGap: true, ...(e.before > 1 ? { colSpan: e.before } : {}) }) + rowTcws[i].unshift(e.wBefore) + } + if (e.after > 0) { + cells.push({ paras: [], gridGap: true, ...(e.after > 1 ? { colSpan: e.after } : {}) }) + rowTcws[i].push(e.wAfter) + } + }) + } + const reconciled = reconcileGridColumns(rows, rowTcws, gridWidthsRaw) + if (reconciled) { + colWidthsTwips = reconciled + const total = reconciled.reduce((a, b) => a + b, 0) + colWidthsPct = reconciled.map((w) => (w / total) * 100) + } // Borders/margins from the table style (styles.xml, basedOn chain included): fallback // when the document level declares none const styleIdEarly = attrsOf(findChild(tblPrNode ?? {}, 'w:tblStyle') ?? {})['w:val'] @@ -4306,18 +4659,25 @@ function extractTableModel(tbl: XNode, ctx: BuildContext, depth = 1): TableModel // pct widths still lay out with the autofit algorithm (only w:tblLayout // fixed switches it off), so their columns keep the min-content floor if (!fixedLayout && (autoWidth || widthPct)) model.autoLayout = true + model.autoFit = + fixedLayout || (!autoWidth && !widthPct) ? 'fixed' : widthPct === 100 ? 'window' : 'contents' if (effCellMar) model.cellMarTwips = effCellMar + if (cellSpacing) model.cellSpacingTwips = cellSpacing + if (tblFill) model.fill = tblFill if (effBorders) model.borders = effBorders if (tblAlign) model.align = tblAlign if (floatSide) model.floatSide = floatSide + if (floatPos) model.floatPos = floatPos if (Number.isFinite(tblIndTwips) && tblIndTwips !== 0) model.indentTwips = tblIndTwips const tblStyle = attrsOf(findChild(findChild(tbl, 'w:tblPr') ?? {}, 'w:tblStyle') ?? {})['w:val'] if (tblStyle) model.tblStyleId = tblStyle + model.tableLook = tableLookOf(tblPrNode) if (tblPrNode && boolProp(tblPrNode, 'w:bidiVisual')) model.bidiVisual = true if (rowHeightsTwips.some((h) => h !== null)) { model.rowHeightsTwips = rowHeightsTwips model.rowHeightRules = rowHeightRules } + model.repeatHeaderRows = repeatHeaderRows if (rowRevisions.some((r) => r !== null)) model.rowRevisions = rowRevisions return model } @@ -4360,12 +4720,14 @@ function attachRawTablePr(xml: string, rows: TableCell[][], rawTrPrs: Array k.name === 'w:trPr') if (trPr) rawTrPrs[ri] = trPr.xml const tcs = kids.filter((k) => k.name === 'w:tc') - if (tcs.length !== rows[ri].length) return + // gridGap placeholders have no w:tc in the source: align against real cells only + const targets = rows[ri].filter((cell) => !cell.gridGap) + if (tcs.length !== targets.length) return tcs.forEach((tc, ci) => { const tcOpenEnd = tc.xml.indexOf('>') + 1 const tcInner = tc.xml.slice(tcOpenEnd, tc.xml.lastIndexOf('')) const tcPr = splitXmlChildren(tcInner).find((k) => k.name === 'w:tcPr') - if (tcPr) rows[ri][ci].rawTcPr = tcPr.xml + if (tcPr) targets[ci].rawTcPr = tcPr.xml }) }) } @@ -4527,8 +4889,8 @@ function extractCell(tc: XNode, ctx: BuildContext, depth: number): TableCell { if (vMerge) { cell.vMerge = attrsOf(vMerge)['w:val'] === 'restart' ? 'restart' : 'continue' } - const fill = attrsOf(findChild(tcPr, 'w:shd') ?? {})['w:fill'] - if (fill && fill !== 'auto') cell.fill = stripHash(fill) + const fill = shdDisplayFill(findChild(tcPr, 'w:shd')) + if (fill) cell.fill = fill const vAlign = attrsOf(findChild(tcPr, 'w:vAlign') ?? {})['w:val'] if (vAlign === 'center' || vAlign === 'bottom' || vAlign === 'top') cell.vAlign = vAlign const tcMar = cellMarginsOf(findChild(tcPr, 'w:tcMar')) @@ -4707,7 +5069,7 @@ async function hfImages(zip: JSZip, partPath: string, partXml: string): Promise< const jc = /[\s\S]*?<\/w:drawing>/g)) { + for (const m of partXml.matchAll(/][\s\S]*?<\/w:drawing>/g)) { const frag = m[0] if (!/]/.test(frag) && onCellRun(m.index!)) continue // mc:AlternateContent may hold several blips (mac Word: PDF Choice + PNG @@ -5297,8 +5659,11 @@ async function parseSources(zip: JSZip): Promise { async function parseTheme( zip: JSZip, ): Promise<{ fonts: ThemeFonts | null; colors: ThemeColors | null }> { + // no theme part: Word still resolves schemeClr/themeColor references + // against the built-in Office palette, so a missing part must not null out + // the color context (themeless docx4j/mc test docs, sample real_run2 09/10) const file = zip.file(THEME_PART_PATH) - if (!file) return { fonts: null, colors: null } + if (!file) return { fonts: null, colors: { ...DEFAULT_THEME_COLORS } } const xml = await file.async('string') const fonts = readThemeFonts(xml) if (fonts) { @@ -5469,7 +5834,54 @@ function fieldDisplayOf(xml: string, styles?: Map): FieldDisp return { kind: 'pageBreak' } } if (visible !== '') { - return { kind: 'text', left: visible } + // face/size of the visible result runs (same rule as tocLine): without + // them the passthrough div inherits the document default and a SimSun + // field paragraph mis-snaps to a double cell on a typed line grid + let sz = 0 + let font: string | undefined + const runRe = /]*)?>([\s\S]*?)<\/w:r>/g + let run: RegExpExecArray | null + while ((run = runRe.exec(xml)) !== null) { + if (!/)/.test(run[1]) || run[1].includes(' sz) sz = v + if (!font) { + const fonts = /]*/.exec(run[1])?.[0] ?? '' + font = /w:eastAsia="([^"]+)"/.exec(fonts)?.[1] ?? /w:ascii="([^"]+)"/.exec(fonts)?.[1] + } + } + // explicit paragraph alignment: the passthrough div would inherit the + // document default (justify in CJK docs) and stretch short lines + const pPr = /[\s\S]*?<\/w:pPr>/.exec(xml)?.[0] ?? '' + const jc = /]*)\/>/.exec(pPr)?.[1] ?? '' + const line = lineTwipsOf(/w:line="([^"]+)"/.exec(spacingAttrs)?.[1]) + const lineRule = (/w:lineRule="(auto|atLeast|exact)"/.exec(spacingAttrs)?.[1] ?? 'auto') as + 'auto' | 'atLeast' | 'exact' + return { + kind: 'text', + left: visible, + ...(sz > 0 ? { szHalfPoints: sz } : {}), + ...(font ? { fontFamily: font } : {}), + ...(align ? { align } : {}), + ...(line > 0 && lineRule + ? { + lineRule, + lineRawTwips: line, + ...(lineRule === 'auto' ? { lineSpacing: Math.round((line / 240) * 100) / 100 } : {}), + } + : {}), + } } return undefined } @@ -5657,11 +6069,23 @@ type ImageMeta = Pick< Block, | 'imageWidthPx' | 'imageHeightPx' + | 'imageLeadingText' + | 'imageLeadingFont' + | 'imageLeadingExplicitSpaceWidthPx' + | 'imageLeadingImplicitSpaceCount' + | 'imageParagraphIndentLeft' + | 'imageParagraphIndentRight' + | 'imageParagraphIndentFirstLine' | 'imageAlign' | 'imageWrap' + | 'imageWrapDistTopEmu' + | 'imageWrapDistBottomEmu' + | 'imageWrapDistLeftEmu' + | 'imageWrapDistRightEmu' | 'imageZOrder' | 'imageOffsetXEmu' | 'imageOffsetYEmu' + | 'imageAnchorLocked' | 'imagePosH' | 'imagePosV' | 'imageRotDeg' @@ -5703,6 +6127,48 @@ function picBorderOf(xml: string): { color: string; widthPt: number } | undefine function imageMeta(xml: string): ImageMeta { const meta: ImageMeta = {} + const drawingAt = xml.search(/]/) + if (drawingAt >= 0 && /^]/.test(xml)) { + const leadingXml = xml.slice(0, drawingAt) + const leadingText = plainText(leadingXml) + if (leadingText !== '') meta.imageLeadingText = leadingText + const pPr = rawPPrOf(xml) + const leadingFonts = [...leadingXml.matchAll(/]*\/?>/g)].at(-1)?.[0] + meta.imageLeadingFont = + /w:eastAsia="([^"]+)"/.exec(leadingFonts ?? '')?.[1] ?? + /w:ascii="([^"]+)"/.exec(leadingFonts ?? '')?.[1] + let explicitSpaceWidthPx = 0 + let implicitSpaceCount = 0 + for (const runMatch of leadingXml.matchAll(/]*)?>([\s\S]*?)<\/w:r>/g)) { + const runBody = runMatch[1] + const text = plainText(`${runBody}`) + if (!/^[ ]+$/.test(text)) continue + const rPr = /]*>[\s\S]*?<\/w:rPr>/.exec(runBody)?.[0] + const sizeHalfPoints = Number(/]*w:val="(\d+)"/.exec(rPr ?? '')?.[1] ?? NaN) + if (Number.isFinite(sizeHalfPoints)) { + // Word's CJK single-byte spaces are half-em: half-points / 3 = CSS px. + explicitSpaceWidthPx += (text.length * sizeHalfPoints) / 3 + } else { + implicitSpaceCount += text.length + } + } + if (explicitSpaceWidthPx > 0) { + meta.imageLeadingExplicitSpaceWidthPx = explicitSpaceWidthPx + } + if (implicitSpaceCount > 0) meta.imageLeadingImplicitSpaceCount = implicitSpaceCount + const ind = pPr ? /]*\/?>/.exec(pPr)?.[0] : undefined + const twips = (name: string): number | undefined => { + const value = Number(new RegExp(`\\bw:${name}="(-?\\d+)"`).exec(ind ?? '')?.[1] ?? NaN) + return Number.isFinite(value) ? value : undefined + } + meta.imageParagraphIndentLeft = twips('left') + meta.imageParagraphIndentRight = twips('right') + meta.imageParagraphIndentFirstLine = twips('firstLine') + if (meta.imageParagraphIndentFirstLine === undefined) { + const hanging = twips('hanging') + if (hanging !== undefined) meta.imageParagraphIndentFirstLine = -hanging + } + } const extent = /]*\/?>/.exec(xml)?.[0] if (extent) { const cx = parseInt(/cx="(\d+)"/.exec(extent)?.[1] ?? '', 10) @@ -5749,7 +6215,16 @@ function imageMeta(xml: string): ImageMeta { } const anchor = /]*>/.exec(xml)?.[0] if (anchor) { + const wrapDistance = (attr: string): number | undefined => { + const value = Number(new RegExp(`\\b${attr}="(\\d+)"`).exec(anchor)?.[1] ?? NaN) + return Number.isFinite(value) ? value : undefined + } + meta.imageWrapDistTopEmu = wrapDistance('distT') + meta.imageWrapDistBottomEmu = wrapDistance('distB') + meta.imageWrapDistLeftEmu = wrapDistance('distL') + meta.imageWrapDistRightEmu = wrapDistance('distR') if (/allowOverlap="(?:0|false)"/.test(anchor)) meta.imageNoOverlap = true + if (/\blocked="(?:1|true)"/.test(anchor)) meta.imageAnchorLocked = true // relativeHeight = 251658240 base + zOrder; keep the delta so overlapping // anchors round-trip their paint order and the editor can reorder them const relHeight = Number(/relativeHeight="(\d+)"/.exec(anchor)?.[1] ?? NaN) @@ -5773,13 +6248,17 @@ function imageMeta(xml: string): ImageMeta { xml, ) // wrapText names the side the text goes on — the object floats opposite; - // with bothSides, an absolute X past mid-body (~4680 twips usable half on - // Letter/A4) means the object hugs the right side + // with bothSides, the object's CENTER past mid-body (~4680 twips usable + // half on Letter/A4) means it hugs the right side: a left-edge test + // misclassifies wide pictures whose X sits before the midline but whose + // body fills the right half (public issue #118) const wrapText = /]*wrapText="([^"]+)"/.exec(xml)?.[1] const posH = /]*>([\s\S]*?)<\/wp:positionH>/.exec(xml)?.[1] ?? '' const offX = Number(/(-?\d+)<\/wp:posOffset>/.exec(posH)?.[1] ?? NaN) + const extentCx = Number(/]*\bcx="(\d+)"/.exec(xml)?.[1] ?? NaN) + const centerX = offX + (Number.isFinite(extentCx) ? extentCx / 2 : 0) const side = - alignRight || wrapText === 'left' || (wrapText !== 'right' && offX > 4680 * 635) + alignRight || wrapText === 'left' || (wrapText !== 'right' && centerX > 4680 * 635) ? 'right' : 'left' // centered object wrapping both sides: no both-side wrap in the @@ -5876,6 +6355,29 @@ async function mediaDataUrl( return `data:${mime};base64,${await file.async('base64')}` } +/** + * Pre-fetch external textbox parts referenced by `` + * (older Word builds store the w:p list in word/txbx*.xml instead of an + * inline w:txbxContent; extractTextboxes is sync). + */ +async function externalTxbxParts( + documentXml: string, + zip: JSZip, + rels: Map, +): Promise> { + const out = new Map() + for (const m of documentXml.matchAll(/]*\br:txbx="([^"]+)"/g)) { + const rId = m[1] + if (out.has(rId)) continue + const rel = rels.get(rId) + if (!rel || rel.targetMode === 'External') continue + const path = rel.target.startsWith('/') ? rel.target.slice(1) : `word/${rel.target}` + const file = zip.file(path.replace(/^word\/\.\.\//, '')) + if (file) out.set(rId, await file.async('string')) + } + return out +} + /** * Pre-resolve blip rIds found inside w:tbl (extractCell is sync, media reads * are async). Scoped to tables to bound memory. @@ -5897,6 +6399,8 @@ async function tableBlipMedia( slice = slice.slice(from, slice.lastIndexOf('') + ''.length) } for (const m of slice.matchAll(/]*r:(?:embed|link)="([^"]+)"/g)) rIds.add(m[1]) + // legacy VML pictures and OLE previews inside cells (w:pict / w:object) + for (const m of slice.matchAll(/]*r:id="([^"]+)"/g)) rIds.add(m[1]) } for (const rId of rIds) { const rel = rels.get(rId) @@ -5915,8 +6419,11 @@ async function tableBlipMedia( async function resolveBlipMedia(xml: string, ctx: BuildContext): Promise { const media = ctx.mediaByRid if (!media) return + // embed and link matched separately: on a blip carrying both, the greedy + // combined pattern only captured the link and left the embedded part unresolved const refs = [ - ...xml.matchAll(/]*r:(?:embed|link)="([^"]+)"/g), + ...xml.matchAll(/]*r:embed="([^"]+)"/g), + ...xml.matchAll(/]*r:link="([^"]+)"/g), ...xml.matchAll(/]*r:id="([^"]+)"/g), ] for (const m of refs) { @@ -6374,7 +6881,7 @@ async function oleDisplay( * patched into it at save time (the body paragraph itself never changes). */ async function extractChart(xml: string, ctx: BuildContext): Promise { - const rId = /]*r:id="([^"]+)"/.exec(xml)?.[1] + const rId = /]*r:id="([^"]+)"/.exec(xml)?.[1] const rel = rId ? ctx.rels.get(rId) : undefined if (!rel || rel.targetMode === 'External') return null const path = (rel.target.startsWith('/') ? rel.target.slice(1) : `word/${rel.target}`).replace( @@ -6384,7 +6891,7 @@ async function extractChart(xml: string, ctx: BuildContext): Promise 0) docDefaults = dd } @@ -6804,8 +7312,11 @@ function styleDisplayOf( if (rf.eaSlotEmpty && font && font === rf.eastAsia) display.eaSlotEmpty = true const spc = parseInt(attrsOf(findChild(rPr, 'w:spacing') ?? {})['w:val'] ?? '', 10) if (spc) display.charSpacingTwips = spc - if (onOffOf(rPr, 'w:caps')) display.caps = 'all' - else if (onOffOf(rPr, 'w:smallCaps')) display.caps = 'small' + const capsOn = onOffOf(rPr, 'w:caps') + const smallCapsOn = onOffOf(rPr, 'w:smallCaps') + if (capsOn) display.caps = 'all' + else if (smallCapsOn) display.caps = 'small' + else if (capsOn === false || smallCapsOn === false) display.caps = 'none' // w:specVanish marks a style separator, not hidden text const vanish = onOffOf(rPr, 'w:vanish') if (vanish !== undefined && onOffOf(rPr, 'w:specVanish') !== true) display.vanish = vanish @@ -6841,6 +7352,10 @@ function styleDisplayOf( const pbb = onOffOf(pPr, 'w:pageBreakBefore') if (pbb !== undefined) display.pageBreakBefore = pbb } + { + const sah = onOffOf(pPr, 'w:suppressAutoHyphens') + if (sah !== undefined) display.suppressAutoHyphens = sah + } { // tri-state so a child style's explicit off survives the basedOn merge const ctx = onOffOf(pPr, 'w:contextualSpacing') @@ -6890,7 +7405,11 @@ async function parseRels(zip: JSZip, path: string): Promise const rels = new Map() const file = zip.file(path) if (!file) return rels - const parsed = xmlParser.parse(await file.async('string')) as XNode[] + // fast-xml-parser rejects a DOCTYPE declaring external entities; drop the + // prologue instead of failing the whole document (entities never resolve — + // XXE-safe — and Relationship elements carry everything in attributes) + const relsXml = (await file.async('string')).replace(/[]|\[[\s\S]*?\])*>/i, '') + const parsed = xmlParser.parse(relsXml) as XNode[] const root = parsed.find((n) => nameOf(n) === 'Relationships') if (!root) return rels for (const relNode of findChildren(root, 'Relationship')) { diff --git a/packages/docx-engine/src/theme.ts b/packages/docx-engine/src/theme.ts index d5f3555..6eddf4a 100644 --- a/packages/docx-engine/src/theme.ts +++ b/packages/docx-engine/src/theme.ts @@ -127,6 +127,23 @@ const THEME_COLOR_SLOTS: Record = { const SLOT_FALLBACK: Partial> = { dk1: '000000', lt1: 'FFFFFF' } +/** built-in Office palette: what Word resolves theme colors against when a + * document ships no word/theme/theme1.xml part */ +export const DEFAULT_THEME_COLORS: Readonly = { + dk1: '000000', + lt1: 'FFFFFF', + dk2: '44546A', + lt2: 'E7E6E6', + accent1: '4472C4', + accent2: 'ED7D31', + accent3: 'A5A5A5', + accent4: 'FFC000', + accent5: '5B9BD5', + accent6: '70AD47', + hlink: '0563C1', + folHlink: '954F72', +} + /** * Resolve a w:themeColor reference (+ optional w:themeTint / w:themeShade, * hex 00-FF) against the palette. sRGB per-channel approximation of Word's @@ -180,8 +197,8 @@ export function applyThemeColors(themeXml: string, colors: ThemeColors): string * format scheme skeleton. */ export function buildThemeXml(fonts: ThemeFonts, colors: ThemeColors): string { - const c = (tag: (typeof COLOR_TAGS)[number], fallback: string) => - `` + const c = (tag: (typeof COLOR_TAGS)[number]) => + `` const font = (tag: string, typeface: string) => `<${tag}>` + `` @@ -192,16 +209,16 @@ export function buildThemeXml(fonts: ThemeFonts, colors: ThemeColors): string { `` + '' + '' + - c('dk2', '44546A') + - c('lt2', 'E7E6E6') + - c('accent1', '4472C4') + - c('accent2', 'ED7D31') + - c('accent3', 'A5A5A5') + - c('accent4', 'FFC000') + - c('accent5', '5B9BD5') + - c('accent6', '70AD47') + - '' + - '' + + c('dk2') + + c('lt2') + + c('accent1') + + c('accent2') + + c('accent3') + + c('accent4') + + c('accent5') + + c('accent6') + + `` + + `` + '' + `${font('a:majorFont', fonts.major)}${font('a:minorFont', fonts.minor)}` + '' + diff --git a/packages/docx-engine/src/types.ts b/packages/docx-engine/src/types.ts index cad6f24..bafe3bd 100644 --- a/packages/docx-engine/src/types.ts +++ b/packages/docx-engine/src/types.ts @@ -43,8 +43,8 @@ export interface Run { rtl?: boolean /** Character spacing (w:spacing, twips, may be negative). Display only; saving is kept faithful by rawRPr */ charSpacingTwips?: number - /** w:caps ('all') / w:smallCaps ('small') display transform. Display only; saving is kept faithful by rawRPr */ - caps?: 'all' | 'small' + /** w:caps ('all') / w:smallCaps ('small') display transform, 'none' = explicit off. Display only; saving is kept faithful by rawRPr */ + caps?: 'all' | 'small' | 'none' /** Horizontal character scale percent (w:w). Display only (approximated as spacing); saving is kept faithful by rawRPr */ charScalePct?: number /** OOXML named highlight color (w:highlight), e.g. "yellow" */ @@ -142,6 +142,11 @@ export interface Run { wrap?: ImageWrap offsetXEmu?: number offsetYEmu?: number + /** wp:anchor text-wrap distances (EMU; display-only) */ + wrapDistTopEmu?: number + wrapDistBottomEmu?: number + wrapDistLeftEmu?: number + wrapDistRightEmu?: number /** wp:anchor allowOverlap="0" (display-only collision hint) */ noOverlap?: boolean /** picture outline (pic:spPr a:ln solid fill, display-only) */ @@ -556,8 +561,8 @@ export interface NoteRun { fontAscii?: string /** East-Asian font (w:eastAsia) — save-side only, parse does not recover it */ font?: string - /** w:caps ('all') / w:smallCaps ('small') display transform */ - caps?: 'all' | 'small' + /** w:caps ('all') / w:smallCaps ('small') display transform, 'none' = explicit off */ + caps?: 'all' | 'small' | 'none' } /** One footnote or endnote (display/edit model; separators are preserved separately). */ @@ -587,6 +592,10 @@ export interface FieldDisplay { /** display-only metrics of the entry paragraph (direct pPr/run values; Word * sizes TOC lines by them while the style carries nothing) */ szHalfPoints?: number + /** face of the visible result runs (text fields): drives the line factor */ + fontFamily?: string + /** explicit paragraph alignment (text fields): overrides the doc default */ + align?: 'left' | 'center' | 'right' lineRule?: 'auto' | 'atLeast' | 'exact' lineRawTwips?: number lineSpacing?: number @@ -608,6 +617,16 @@ export interface ChartSeries { name?: string /** cached numeric values by category position; null = gap in the cache */ values: (number | null)[] + /** explicit series fill (c:ser/c:spPr solid fill), hex without '#' */ + color?: string + /** explicit per-point fills (c:dPt), e.g. pie slice colors; sparse by point index */ + pointColors?: (string | null)[] + /** scatter/bubble: numeric x of each point (c:xVal) */ + xValues?: (number | null)[] + /** bubble: point sizes (c:bubbleSize) */ + sizes?: (number | null)[] + /** scatter: connect the points (c:scatterStyle line*, unless the series line is a:noFill) */ + line?: boolean } /** @@ -619,13 +638,19 @@ export interface ChartSeries { export interface ChartDisplay { /** zip path of the chart part this model was read from */ partPath: string - kind: 'bar' | 'line' | 'pie' | 'area' | 'other' + kind: 'bar' | 'line' | 'pie' | 'area' | 'scatter' | 'bubble' | 'other' /** bar charts: c:barDir="bar" (horizontal bars); default is column direction */ horizontal?: boolean + /** c:grouping: series stack per category (percentStacked normalizes each category to 100%) */ + grouping?: 'stacked' | 'percentStacked' + /** line charts: draw point markers (c:marker) */ + markers?: boolean /** chart title; undefined when the chart has no title part (then not editable) */ title?: string categories: string[] series: ChartSeries[] + /** series color cycle from the chart style + theme accents (hex, no '#'); absent = Office default */ + palette?: string[] /** display size (from wp:extent), editable via the corner resize handle */ widthPx?: number heightPx?: number @@ -736,6 +761,8 @@ export interface TableCell { /** per nested table: how many cell paragraphs precede it (reading-order anchor); absent = after all paragraphs */ nestedTableAnchors?: number[] colSpan?: number + /** display-only placeholder spanning a row's w:gridBefore/w:gridAfter columns (no borders/fill, not written back as w:tc) */ + gridGap?: boolean /** 'restart' opens a vertical merge, 'continue' is a merged-away cell */ vMerge?: 'restart' | 'continue' /** cell shading fill, hex without '#' (w:shd w:fill) */ @@ -759,6 +786,18 @@ export interface TableCell { cellRevision?: { kind: 'ins' | 'del' } & RevisionInfo } +export type TableAutoFitMode = 'contents' | 'window' | 'fixed' + +/** Word's Table Style Options (w:tblLook). */ +export interface TableLook { + firstRow: boolean + lastRow: boolean + firstColumn: boolean + lastColumn: boolean + bandedRows: boolean + bandedColumns: boolean +} + /** Editable interchange model of a table block, extracted from or generated to OOXML. */ export interface TableModel { rows: TableCell[][] @@ -770,8 +809,14 @@ export interface TableModel { widthPct?: number /** autofit layout (no fixed w:tblLayout; w:tblW auto/absent/zero or pct): display may widen columns to min-content */ autoLayout?: boolean + /** editable Word AutoFit mode (w:tblLayout + w:tblW) */ + autoFit?: TableAutoFitMode /** table-level default cell margins (w:tblCellMar) */ cellMarTwips?: CellMargins + /** w:tblCellSpacing (twips, half the gap between adjacent cells); cells render boxed with gaps */ + cellSpacingTwips?: number + /** table shading (tblPr w:shd), hex without '#'; shows through transparent cells and cell-spacing gaps */ + fill?: string /** table-level borders (w:tblBorders); undefined = not declared (default gridlines apply) */ borders?: TableBorders /** table horizontal alignment (tblPr w:jc); default left */ @@ -788,19 +833,25 @@ export interface TableModel { yTwips: number horzAnchor?: 'page' | 'margin' | 'text' vertAnchor?: 'page' | 'margin' | 'text' + /** gap between the floating table and surrounding text (w:*FromText) */ + distanceTwips?: CellMargins } /** floating table (w:tblpPr): text wraps around the side opposite the anchor */ - floatSide?: 'left' | 'right' + floatSide?: 'left' | 'right' | null /** per-row height (twips, w:trHeight; null = not set), aligned with rows */ rowHeightsTwips?: Array /** per-row height rule (w:trHeight w:hRule; absent/legacy models = atLeast), aligned with rows */ rowHeightRules?: Array<'atLeast' | 'exact' | null> + /** per-row repeat-as-header flag (w:trPr/w:tblHeader), aligned with rows */ + repeatHeaderRows?: Array /** per-row raw … bytes (passed through verbatim), aligned with rows */ rawTrPrs?: Array /** row-level revisions (trPr w:ins/w:del = inserted/deleted row), aligned with rows */ rowRevisions?: Array<({ kind: 'ins' | 'del' } & RevisionInfo) | null> /** table style reference (w:tblStyle w:val); '' = explicitly no style, undefined = leave as-is */ tblStyleId?: string + /** conditional table-style switches (w:tblLook) */ + tableLook?: TableLook /** RTL table (tblPr w:bidiVisual): columns display right to left */ bidiVisual?: boolean } @@ -956,10 +1007,23 @@ export interface Block { imageCrop?: { l: number; t: number; r: number; b: number } /** fill placement (a:stretch/a:fillRect) as fractions of the shape box; negative = bleed (display-only) */ imageFillRect?: { l: number; t: number; r: number; b: number } + /** Preserved whitespace before an inline picture and its paragraph indents (display-only). */ + imageLeadingText?: string + imageLeadingFont?: string + imageLeadingExplicitSpaceWidthPx?: number + imageLeadingImplicitSpaceCount?: number + imageParagraphIndentLeft?: number + imageParagraphIndentRight?: number + imageParagraphIndentFirstLine?: number /** paragraph alignment of the image (w:jc) */ imageAlign?: 'left' | 'center' | 'right' /** text wrapping of a floating image (wp:anchor); absent = inline (in line with text) */ imageWrap?: ImageWrap + /** wp:anchor text-wrap distances (EMU; display-only) */ + imageWrapDistTopEmu?: number + imageWrapDistBottomEmu?: number + imageWrapDistLeftEmu?: number + imageWrapDistRightEmu?: number /** * stacking rank of a floating image among overlapping anchors, decoded from * wp:anchor relativeHeight minus the 251658240 base (0 when at/below base). @@ -984,6 +1048,8 @@ export interface Block { */ imageOffsetXEmu?: number imageOffsetYEmu?: number + /** wp:anchor locked="1": moving the picture must not change its anchor paragraph */ + imageAnchorLocked?: boolean /** margin-relative wp:align of a floating image (Word position-gallery presets) */ imagePosH?: 'left' | 'center' | 'right' imagePosV?: 'top' | 'center' | 'bottom' @@ -1246,8 +1312,8 @@ export interface StyleDisplay { charSpacingTwips?: number /** style-level pPr w:tabs (Word's built-in Header/Footer styles carry the center/right stops) */ tabStops?: TabStop[] - /** w:caps ('all') / w:smallCaps ('small') display transform */ - caps?: 'all' | 'small' + /** w:caps ('all') / w:smallCaps ('small') display transform, 'none' = explicit off */ + caps?: 'all' | 'small' | 'none' /** multiple of single line spacing (w:spacing w:line, lineRule auto) */ lineSpacing?: number /** F1: lineRule for accurate height calculation */ @@ -1269,6 +1335,8 @@ export interface StyleDisplay { keepLines?: boolean /** pageBreakBefore from style definition */ pageBreakBefore?: boolean + /** w:suppressAutoHyphens from style definition (tri-state: explicit off overrides the chain) */ + suppressAutoHyphens?: boolean /** F1: contextualSpacing from style definition */ contextualSpacing?: boolean /** paragraph alignment from the style (w:pPr w:jc) */ @@ -1364,6 +1432,8 @@ export interface DocDefaults { spaceAfterAuto?: boolean /** rPrDefault w:lang w:val (BCP-47) — hyphenation/locale of the body text */ lang?: string + /** pPrDefault w:suppressAutoHyphens — every paragraph opts out of w:autoHyphenation */ + suppressAutoHyphens?: boolean } /** word/settings.xml w:documentProtection (only the editing restriction subset). */ diff --git a/packages/docx-engine/src/zip-load.ts b/packages/docx-engine/src/zip-load.ts index f3b16fd..0eabd52 100644 --- a/packages/docx-engine/src/zip-load.ts +++ b/packages/docx-engine/src/zip-load.ts @@ -1,4 +1,5 @@ import JSZip from 'jszip' +import { needsOoxmlNormalization, normalizeOoxmlXml } from './ooxml-normalize' const EOCD_SIG = 0x06054b50 const CENTRAL_SIG = 0x02014b50 @@ -49,7 +50,71 @@ function neutralizeUnicodePathFields(bytes: Uint8Array): Uint8Array { return out ?? bytes } +const MAX_ZIP_PARTS = 10000 +const MAX_PART_UNCOMPRESSED_BYTES = 512 * 1024 * 1024 +const MAX_TOTAL_UNCOMPRESSED_BYTES = 1.5 * 1024 * 1024 * 1024 + +/** + * Reject zip bombs before any part is inflated, using the declared + * uncompressed sizes from the central directory (JSZip keeps them in + * the lazy `_data` compressed object). + */ +export function assertZipWithinLimits(zip: JSZip): void { + const files = Object.values(zip.files).filter((f) => !f.dir) + if (files.length > MAX_ZIP_PARTS) { + throw new Error(`docx rejected: ${files.length} parts exceeds the ${MAX_ZIP_PARTS} limit`) + } + let total = 0 + for (const file of files) { + const size = + (file as unknown as { _data?: { uncompressedSize?: number } })._data?.uncompressedSize ?? 0 + if (size > MAX_PART_UNCOMPRESSED_BYTES) { + throw new Error( + `docx rejected: part ${file.name} declares ${size} uncompressed bytes ` + + `(limit ${MAX_PART_UNCOMPRESSED_BYTES})`, + ) + } + if (size > 0) total += size + } + if (total > MAX_TOTAL_UNCOMPRESSED_BYTES) { + throw new Error( + `docx rejected: total uncompressed size ${total} exceeds the ` + + `${MAX_TOTAL_UNCOMPRESSED_BYTES} limit`, + ) + } +} + +// The gate parts carry the strict / non-canonical-prefix markers whenever the +// package needs normalizing (strict packages are strict in document.xml and +// their rels; prefix oddities live in document.xml). +const NORMALIZE_GATE_PARTS = ['word/document.xml', 'word/_rels/document.xml.rels', '_rels/.rels'] + +/** + * ISO Strict OOXML and non-canonical namespace prefixes are normalized here, + * at the single zip entry point, so parseDocx offsets and saveDocx patches + * operate on identical part text (and saved packages come out transitional). + */ +async function normalizeOoxmlParts(zip: JSZip): Promise { + let needed = false + for (const path of NORMALIZE_GATE_PARTS) { + const file = zip.file(path) + if (file && needsOoxmlNormalization(await file.async('string'))) { + needed = true + break + } + } + if (!needed) return + for (const file of Object.values(zip.files)) { + if (file.dir || !/\.(xml|rels)$/i.test(file.name)) continue + const xml = await file.async('string') + if (needsOoxmlNormalization(xml)) zip.file(file.name, normalizeOoxmlXml(xml)) + } +} + /** Load a docx/zip resolving part names the way Word does. */ export async function loadDocxZip(bytes: Uint8Array): Promise { - return JSZip.loadAsync(neutralizeUnicodePathFields(bytes)) + const zip = await JSZip.loadAsync(neutralizeUnicodePathFields(bytes)) + assertZipWithinLimits(zip) + await normalizeOoxmlParts(zip) + return zip } diff --git a/packages/docx-engine/tests/character-styles.test.ts b/packages/docx-engine/tests/character-styles.test.ts index 6b31c18..5399d3b 100644 --- a/packages/docx-engine/tests/character-styles.test.ts +++ b/packages/docx-engine/tests/character-styles.test.ts @@ -234,9 +234,9 @@ describe('styleUpserts style write-back', () => { describe('toggle-off (w:val="0") overrides inherited formatting', () => { const TOGGLE_STYLES = '' + - '' + + '' + '' + - '' + + '' + '' it('records explicit off in the style display and through basedOn chains', async () => { @@ -251,23 +251,26 @@ describe('toggle-off (w:val="0") overrides inherited formatting', () => { italic: true, strike: true, underline: true, + caps: 'small', }) const child = doc.styles.get('PlainChild')!.display! expect(child.bold).toBe(false) expect(child.italic).toBe(false) expect(child.strike).toBe(false) expect(child.underline).toBe(false) + expect(child.caps).toBe('none') // the grandchild inherits the explicit off, not the grandparent's on const grand = doc.styles.get('GrandChild')!.display! expect(grand.bold).toBe(false) expect(grand.underline).toBe(false) + expect(grand.caps).toBe('none') }) it('records explicit off on runs as false, absent as undefined', async () => { const doc = await parseDocx( await buildDocx({ bodyXml: - 'off' + + 'off' + 'on' + 'unset', }), @@ -275,8 +278,10 @@ describe('toggle-off (w:val="0") overrides inherited formatting', () => { const runs = doc.blocks[0].runs! expect(runs[0].bold).toBe(false) expect(runs[0].underline).toBe(false) + expect(runs[0].caps).toBe('none') expect(runs[1].bold).toBe(true) expect(runs[2].bold).toBeUndefined() expect(runs[2].underline).toBeUndefined() + expect(runs[2].caps).toBeUndefined() }) }) diff --git a/packages/docx-engine/tests/chart-parse-model.test.ts b/packages/docx-engine/tests/chart-parse-model.test.ts new file mode 100644 index 0000000..a73ff94 --- /dev/null +++ b/packages/docx-engine/tests/chart-parse-model.test.ts @@ -0,0 +1,186 @@ +import { describe, expect, it } from 'vitest' +import { parseChartPartXml } from '../src/index' +import type { ThemeColors } from '../src/types' + +const THEME: ThemeColors = { + dk1: '000000', + lt1: 'FFFFFF', + accent1: '4F81BD', + accent2: 'C0504D', + accent3: '9BBB59', + accent4: '8064A2', + accent5: '4BACC6', + accent6: 'F79646', +} + +const chartSpace = (inner: string, pre = '') => + '' + + `${pre}${inner}` + +const numCache = (tag: string, values: (number | string)[], fmt = 'General') => + `S!$A$1${fmt}` + + `` + + values.map((v, i) => `${v}`).join('') + + `` + +const strCache = (tag: string, values: string[]) => + `S!$A$1` + + values.map((v, i) => `${v}`).join('') + + `` + +const barSer = (i: number, values: number[], extra = '') => + `` + + `${strCache('tx', [`Series ${i + 1}`])}${extra}` + + `${strCache('cat', ['A', 'B'])}${numCache('val', values)}` + +describe('parseChartPartXml grouping and colors', () => { + const bar = (grouping: string, extra = '') => + chartSpace( + `` + + `${barSer(0, [1, 2], extra)}${barSer(1, [3, 4])}`, + ) + + it('keeps stacked / percentStacked grouping and drops clustered', () => { + expect(parseChartPartXml(bar('stacked'), 'p')!.grouping).toBe('stacked') + expect(parseChartPartXml(bar('percentStacked'), 'p')!.grouping).toBe('percentStacked') + expect(parseChartPartXml(bar('clustered'), 'p')!.grouping).toBeUndefined() + }) + + it('reads explicit series solid fills, resolving schemeClr through the theme', () => { + const srgb = '' + expect(parseChartPartXml(bar('clustered', srgb), 'p')!.series[0].color).toBe('FF0000') + const scheme = '' + expect(parseChartPartXml(bar('clustered', scheme), 'p', THEME)!.series[0].color).toBe('9BBB59') + // without a theme the scheme reference cannot resolve + expect(parseChartPartXml(bar('clustered', scheme), 'p')!.series[0].color).toBeUndefined() + }) + + it('darkens lumMod-modified scheme colors', () => { + const mod = + '' + + '' + const color = parseChartPartXml(bar('clustered', mod), 'p', THEME)!.series[0].color! + const lum = (hex: string) => + [0, 2, 4].reduce((a, i) => a + parseInt(hex.slice(i, i + 2), 16), 0) + expect(lum(color)).toBeLessThan(lum('4F81BD')) + }) + + it('collects c:dPt point fills sparsely by index', () => { + const dPt = + '' + + '' + const display = parseChartPartXml(bar('clustered', dPt), 'p')! + expect(display.series[0].pointColors).toEqual([null, '00FF00']) + }) +}) + +describe('parseChartPartXml chart-style palette', () => { + const clustered = `${barSer(0, [1, 2])}` + + it('theme accents drive the default cycle', () => { + expect(parseChartPartXml(chartSpace(clustered), 'p', THEME)!.palette).toEqual([ + '4F81BD', + 'C0504D', + '9BBB59', + '8064A2', + '4BACC6', + 'F79646', + ]) + expect(parseChartPartXml(chartSpace(clustered), 'p')!.palette).toBeUndefined() + }) + + it('style column 1 is grayscale, also via the c14 AlternateContent form', () => { + const direct = parseChartPartXml(chartSpace(clustered, ''), 'p', THEME)! + expect(direct.palette![0]).toBe('595959') + const alt = + '' + + '' + + '' + expect(parseChartPartXml(chartSpace(clustered, alt), 'p', THEME)!.palette![0]).toBe('595959') + }) + + it('single-accent columns lead with their accent', () => { + const display = parseChartPartXml(chartSpace(clustered, ''), 'p', THEME)! + expect(display.palette![0]).toBe('9BBB59') + }) +}) + +describe('parseChartPartXml scatter and bubble', () => { + it('reads x/y pairs, date category texts and the noFill line suppression', () => { + const ser = + '' + + strCache('tx', ['S1']) + + '' + + numCache('xVal', [37377, 37408], 'm/d/yyyy') + + numCache('yVal', [55, 57]) + + '' + const xml = chartSpace( + `${ser}`, + ) + const display = parseChartPartXml(xml, 'p')! + expect(display.kind).toBe('scatter') + expect(display.markers).toBe(true) + expect(display.categories).toEqual(['5/1/2002', '6/1/2002']) + expect(display.series[0].values).toEqual([55, 57]) + expect(display.series[0].xValues).toEqual([37377, 37408]) + expect(display.series[0].line).toBeUndefined() + }) + + it('keeps the line for lineMarker series without noFill', () => { + const ser = + `${numCache('xVal', [1, 2])}` + + `${numCache('yVal', [3, 4])}` + const xml = chartSpace( + `${ser}`, + ) + expect(parseChartPartXml(xml, 'p')!.series[0].line).toBe(true) + }) + + it('names a single-series auto title after the series', () => { + const ser = + `${strCache('tx', ['Y-Values'])}` + + `${numCache('xVal', [1, 2])}${numCache('yVal', [3, 4])}` + const xml = chartSpace(`${ser}`).replace( + '', + '', + ) + expect(parseChartPartXml(xml, 'p')!.title).toBe('Y-Values') + }) + + it('reads bubble sizes and trims raw double x texts', () => { + const ser = + '' + + strCache('tx', ['Y-Values']) + + numCache('xVal', ['0.70000000000000062', 1.8]) + + numCache('yVal', [2.7, 3.2]) + + numCache('bubbleSize', [10, 4]) + + '' + const display = parseChartPartXml(chartSpace(`${ser}`), 'p')! + expect(display.kind).toBe('bubble') + expect(display.categories).toEqual(['0.7', '1.8']) + expect(display.series[0].sizes).toEqual([10, 4]) + }) +}) + +describe('parseChartexPartXml leniency', () => { + it('parses a waterfall whose cx:chartData was renamed (unknown-element corpus)', () => { + const xml = + '' + + '' + + 'S!$A$2:$A$4' + + '100-4060' + + '' + + '' + + 'Series1' + + '' + + '' + const display = parseChartPartXml(xml, 'word/charts/chartEx1.xml')! + expect(display.kind).toBe('bar') + expect(display.series[0]).toEqual({ name: 'Series1', values: [100, -40, 60] }) + }) +}) diff --git a/packages/docx-engine/tests/field-display.test.ts b/packages/docx-engine/tests/field-display.test.ts index 50bdd52..172fa1f 100644 --- a/packages/docx-engine/tests/field-display.test.ts +++ b/packages/docx-engine/tests/field-display.test.ts @@ -24,6 +24,19 @@ const TOC_ENTRY_PARAGRAPH = const FIELD_END_PAGEBREAK_PARAGRAPH = '' +// paste artifact from Word web copy: an INCLUDEPICTURE field with a dead local +// path sandwiched between styled text runs (public issue #118 demo.docx shape) +const RPR = + '' +const INCLUDEPICTURE_PARAGRAPH = + `${RPR}` + + `${RPR}\u9636\u8d70\u5230\u6cb3` + + `${RPR} ` + + `${RPR}` + + `${RPR} INCLUDEPICTURE "/tmp/x.jpeg" \\* MERGEFORMATINET ` + + `${RPR}` + + `${RPR}\u5cb8\u8fb9` + const PAGE_FIELD_PARAGRAPH = '' + ' PAGE ' + @@ -56,6 +69,33 @@ describe('field paragraph display model', () => { expect(doc.blocks[0].runs?.[0]).toMatchObject({ text: '- 8 -', instrField: 'PAGE' }) }) + it('a resultless INCLUDEPICTURE text field keeps spaces and run metrics (public issue #118)', async () => { + const doc = await parseDocx(await buildDocx({ bodyXml: INCLUDEPICTURE_PARAGRAPH })) + const block = doc.blocks[0] + expect(block.type).toBe('passthrough') + expect(block.fieldDisplay).toMatchObject({ + kind: 'text', + left: '\u9636\u8d70\u5230\u6cb3 \u5cb8\u8fb9', + szHalfPoints: 24, + fontFamily: '\u5b8b\u4f53', + align: 'left', + }) + }) + + it('a text field with an explicit line multiple carries the spacing', async () => { + const xml = INCLUDEPICTURE_PARAGRAPH.replace( + '', + '', + ) + const doc = await parseDocx(await buildDocx({ bodyXml: xml })) + expect(doc.blocks[0].fieldDisplay).toMatchObject({ + kind: 'text', + lineRule: 'auto', + lineRawTwips: 360, + lineSpacing: 1.5, + }) + }) + it('TOC entry carries its hyperlink anchor for click-to-jump', async () => { const doc = await parseDocx(await buildDocx({ bodyXml: TOC_ENTRY_PARAGRAPH })) expect(doc.blocks[0].fieldDisplay?.anchor).toBe('_Toc1') @@ -341,3 +381,51 @@ describe('FORMCHECKBOX form fields', () => { expect(doc.blocks[0].type).toBe('passthrough') }) }) + +// Mail-merge label/business-card layout: the visible text lives entirely in +// field results inside table cells (complex MERGEFIELD runs and fldSimple). +const MERGE_CELL_PARAGRAPH = + '' + + '' + + ' MERGEFIELD Vorname ' + + '' + + 'Erika' + + '' + + ' ' + + '' + + 'Mustermann' + + '' + + '' + +const NEXT_FIELD_PARAGRAPH = + '' + + ' NEXT ' + + '' + +function cellTable(content: string): string { + return ( + '' + + '' + + '' + + content + + '' + ) +} + +describe('field cached results in table cells', () => { + it('complex MERGEFIELD and fldSimple results stay visible as cell runs', async () => { + const doc = await parseDocx(await buildDocx({ bodyXml: cellTable(MERGE_CELL_PARAGRAPH) })) + const cell = doc.blocks[0].table!.rows[0][0] + const text = (cell.richParas?.[0]?.runs ?? []).map((r) => r.text).join('') + expect(text).toBe('Erika Mustermann') + // instruction text must not leak into the visible runs + expect(text).not.toContain('MERGEFIELD') + }) + + it('a resultless field (NEXT) contributes no text', async () => { + const doc = await parseDocx(await buildDocx({ bodyXml: cellTable(NEXT_FIELD_PARAGRAPH) })) + const cell = doc.blocks[0].table!.rows[0][0] + const text = (cell.richParas?.[0]?.runs ?? []).map((r) => r.text).join('') + expect(text).toBe('') + }) +}) diff --git a/packages/docx-engine/tests/hostile-input.test.ts b/packages/docx-engine/tests/hostile-input.test.ts index c39cf42..e76f0a8 100644 --- a/packages/docx-engine/tests/hostile-input.test.ts +++ b/packages/docx-engine/tests/hostile-input.test.ts @@ -1,7 +1,7 @@ import JSZip from 'jszip' import { describe, expect, it } from 'vitest' import { parseDocx, saveDocx } from '../src/index' -import { buildDocx } from './helpers/build-docx' +import { buildDocx, IMAGE_PARAGRAPH_XML } from './helpers/build-docx' /** overwrite the declared uncompressed size of every central-directory record */ function patchCentralSizes(bytes: Uint8Array, size: number): Uint8Array { @@ -95,3 +95,21 @@ describe('malformed XML degrades instead of failing the document', () => { expect(doc.blocks[0].runs?.[0]?.text).toBe('hello') }) }) + +describe('DOCTYPE prologue in a relationships part', () => { + it('opens the document and still resolves rels (external entity never fetched)', async () => { + const zip = await JSZip.loadAsync( + await buildDocx({ bodyXml: IMAGE_PARAGRAPH_XML + PLAIN_PARA, withImage: true }), + ) + const rels = await zip.file('word/_rels/document.xml.rels')!.async('string') + const poisoned = rels + .replace('?>', '?>\n\n]>') + .replace(' { } }) + it('floats a wide bothSides picture right when its center passes mid-body (public issue #118)', async () => { + // left edge before the midline, but the 3in-wide body fills the right half + const xml = ANCHOR_SQUARE_RIGHT_XML.replace( + 'right', + '2407073', + ).replace('', '') + const doc = await parseDocx(await buildDocx({ bodyXml: xml, withImage: true })) + expect(doc.blocks[0].imageWrap).toBe('square-right') + expect(doc.blocks[0].imageOffsetXEmu).toBe(2407073) + expect(doc.blocks[0].imageWrapDistTopEmu).toBe(0) + expect(doc.blocks[0].imageWrapDistBottomEmu).toBe(0) + expect(doc.blocks[0].imageWrapDistLeftEmu).toBe(114300) + expect(doc.blocks[0].imageWrapDistRightEmu).toBe(114300) + }) + + it('keeps leading spaces and first-line indent on an atomic picture block (public issue #118)', async () => { + const xml = IMAGE_PARAGRAPH_XML.replace( + '', + '' + + '' + + ' ', + ) + const doc = await parseDocx(await buildDocx({ bodyXml: xml, withImage: true })) + const block = doc.blocks[0] + expect(block.type).toBe('image') + expect(block.imageLeadingText).toBe(' ') + expect(block.imageLeadingFont).toBe('SimSun') + expect(block.imageLeadingImplicitSpaceCount).toBe(4) + expect(block.imageLeadingExplicitSpaceWidthPx).toBeUndefined() + expect(block.imageParagraphIndentFirstLine).toBe(420) + expect(block.imageWidthPx).toBe(96) + + const explicit = xml.replace( + ' ', + '' + ' ', + ) + const explicitBlock = (await parseDocx(await buildDocx({ bodyXml: explicit, withImage: true }))) + .blocks[0] + expect(explicitBlock.imageLeadingExplicitSpaceWidthPx).toBe(32) + expect(explicitBlock.imageLeadingImplicitSpaceCount).toBeUndefined() + }) + it('converts inline -> square anchor with position and wrap elements', () => { const out = applyImageWrap(IMAGE_PARAGRAPH_XML, 'square-left') expect(out).toContain(' { expect(doc.blocks[0].imageDataUrl).toMatch(/^data:image\/png;base64,/) }) }) + +describe('one run carrying several inline drawings', () => { + const GEN_CTX: GenerateContext = { + headingStyleIds: new Map(), + allocateHyperlinkRel: () => 'rId999', + } + + const inlineDrawing = (cx: number, cy: number): string => + `` + + '' + + '' + + '' + + it('splits the run so every drawing survives as its own image run', async () => { + const bodyXml = `${inlineDrawing(914400, 914400)}${inlineDrawing(457200, 457200)}` + const doc = await parseDocx(await buildDocx({ bodyXml, withImage: true })) + const block = doc.blocks[0] + expect(block.type).toBe('paragraph') + const imageRuns = block.runs!.filter((r) => r.image) + expect(imageRuns).toHaveLength(2) + expect(imageRuns.map((r) => r.image?.widthPx)).toEqual([96, 48]) + // an edit-triggered rebuild must write both pictures back + const regen = generateParagraphXml({ type: 'paragraph', runs: block.runs! }, GEN_CTX) + expect(regen.match(//g)).toHaveLength(2) + expect(regen.match(/r:embed="rId10"/g)).toHaveLength(2) + }) + + it('keeps text between the drawings in document order', async () => { + const bodyXml = + `${inlineDrawing(914400, 914400)}` + + `mid${inlineDrawing(457200, 457200)}` + const doc = await parseDocx(await buildDocx({ bodyXml, withImage: true })) + const runs = doc.blocks[0].runs! + expect(runs.filter((r) => r.image)).toHaveLength(2) + expect(runs.map((r) => r.text).join('')).toBe('mid') + const regen = generateParagraphXml({ type: 'paragraph', runs }, GEN_CTX) + expect(regen.indexOf('mid')).toBeGreaterThan(regen.indexOf('')) + expect(regen.indexOf('mid')).toBeLessThan(regen.lastIndexOf('')) + }) +}) diff --git a/packages/docx-engine/tests/layout-fidelity.test.ts b/packages/docx-engine/tests/layout-fidelity.test.ts index 2a88922..26c3e2a 100644 --- a/packages/docx-engine/tests/layout-fidelity.test.ts +++ b/packages/docx-engine/tests/layout-fidelity.test.ts @@ -157,3 +157,52 @@ describe('settings.xml layout flags', () => { expect(doc.defaultTabStopTwips).toBe(709) }) }) + +describe('w:suppressAutoHyphens', () => { + const stylesWith = (docDefaultsPPr: string, styleExtra = '') => + XML_DECL + + '' + + `${docDefaultsPPr}` + + styleExtra + + '' + + it('pPrDefault w:suppressAutoHyphens lands on docDefaults', async () => { + const doc = await parseDocx( + await buildDocx({ + bodyXml: 'x', + extraParts: [settingsPart('')], + stylesXml: stylesWith(''), + }), + ) + expect(doc.autoHyphenation).toBe(true) + expect(doc.docDefaults?.suppressAutoHyphens).toBe(true) + }) + + it('explicit off at pPrDefault stays unset', async () => { + const doc = await parseDocx( + await buildDocx({ + bodyXml: 'x', + stylesXml: stylesWith(''), + }), + ) + expect(doc.docDefaults?.suppressAutoHyphens).toBeUndefined() + }) + + it('style-level value is tri-state and survives the basedOn merge', async () => { + const doc = await parseDocx( + await buildDocx({ + bodyXml: 'x', + stylesXml: stylesWith( + '', + '' + + '' + + '' + + '' + + '', + ), + }), + ) + expect(doc.styles.get('NoHyph')?.display?.suppressAutoHyphens).toBe(true) + expect(doc.styles.get('ReHyph')?.display?.suppressAutoHyphens).toBe(false) + }) +}) diff --git a/packages/docx-engine/tests/notes.test.ts b/packages/docx-engine/tests/notes.test.ts index 7ae8f59..61e856b 100644 --- a/packages/docx-engine/tests/notes.test.ts +++ b/packages/docx-engine/tests/notes.test.ts @@ -248,7 +248,12 @@ describe('captions', () => { const doc = await parseDocx(await buildDocx({ bodyXml: generateCaptionXml('图', 1, '测试') })) const p = doc.blocks[0] expect(p.type).toBe('passthrough') - expect(p.fieldDisplay).toEqual({ kind: 'text', left: '图 1 测试' }) + expect(p.fieldDisplay).toEqual({ + kind: 'text', + left: '图 1 测试', + szHalfPoints: 18, + align: 'center', + }) }) }) diff --git a/packages/docx-engine/tests/ooxml-normalize.test.ts b/packages/docx-engine/tests/ooxml-normalize.test.ts new file mode 100644 index 0000000..68f6895 --- /dev/null +++ b/packages/docx-engine/tests/ooxml-normalize.test.ts @@ -0,0 +1,246 @@ +import JSZip from 'jszip' +import { describe, expect, it } from 'vitest' +import { needsOoxmlNormalization, normalizeOoxmlXml } from '../src/ooxml-normalize' +import { loadDocxZip } from '../src/zip-load' +import { parseDocx } from '../src/index' + +const XML_DECL = '' +const W_TRANS = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main' +const W_STRICT = 'http://purl.oclc.org/ooxml/wordprocessingml/main' +const R_STRICT = 'http://purl.oclc.org/ooxml/officeDocument/relationships' + +async function buildDocxBytes(documentXml: string, relsXml?: string): Promise { + const zip = new JSZip() + zip.file( + '[Content_Types].xml', + `${XML_DECL}` + + '' + + '' + + '' + + '', + ) + zip.file( + '_rels/.rels', + `${XML_DECL}` + + '' + + '', + ) + if (relsXml) zip.file('word/_rels/document.xml.rels', relsXml) + zip.file('word/document.xml', documentXml) + return zip.generateAsync({ type: 'uint8array' }) +} + +describe('needsOoxmlNormalization', () => { + it('is false for canonical transitional parts', () => { + expect(needsOoxmlNormalization(``)).toBe( + false, + ) + }) + + it('detects strict URIs and non-canonical prefixes', () => { + expect(needsOoxmlNormalization(``)).toBe(true) + expect(needsOoxmlNormalization(``)).toBe(true) + expect(needsOoxmlNormalization(``)).toBe(false) + }) +}) + +describe('non-canonical prefixes bound to known URIs', () => { + it('renames the prefix and its declaration to the canonical one', () => { + const out = normalizeOoxmlXml( + `` + + 'Hi' + + '', + ) + expect(out).toContain(``) + expect(out).toContain('Hi') + expect(out).not.toContain('fake:') + }) + + it('leaves unrelated namespaces alone even when local names collide', () => { + const src = `` + expect(normalizeOoxmlXml(src)).toBe(src) + }) + + it('skips the rename when the canonical prefix is taken by another namespace', () => { + const src = + `` + + '' + const out = normalizeOoxmlXml(src) + expect(out).toContain('') + }) + + it('frees a canonical prefix that is itself renamed away', () => { + const a = 'http://schemas.openxmlformats.org/drawingml/2006/main' + const out = normalizeOoxmlXml( + `` + + '', + ) + expect(out).toContain(``) + expect(out).toContain('') + }) + + it('frees a canonical prefix regardless of declaration order', () => { + const a = 'http://schemas.openxmlformats.org/drawingml/2006/main' + const out = normalizeOoxmlXml( + `` + + '', + ) + expect(out).toContain(``) + expect(out).toContain('') + }) + + it('drops a duplicate declaration when the canonical binding already exists', () => { + const out = normalizeOoxmlXml( + `` + + 'x', + ) + expect(out).toContain('x') + expect(out).not.toContain('ns3') + }) + + it('prefixes elements in a default wordprocessingml namespace', () => { + const out = normalizeOoxmlXml( + `

    x

    `, + ) + expect(out).toContain(`xmlns:w="${W_TRANS}"`) + expect(out).toContain('x') + }) +}) + +describe('strict OOXML normalization', () => { + it('maps strict namespace URIs to transitional and drops w:conformance', () => { + const out = normalizeOoxmlXml( + `` + + '', + ) + expect(out).toContain(`xmlns:w="${W_TRANS}"`) + expect(out).toContain( + 'xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"', + ) + expect(out).not.toContain('conformance') + expect(out).toContain('') + }) + + it('maps strict relationship types, including divergent tails', () => { + const out = normalizeOoxmlXml( + `${XML_DECL}` + + `` + + `` + + '', + ) + expect(out).toContain( + 'Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"', + ) + expect(out).toContain( + 'Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"', + ) + }) + + it('converts universal measures to the native unit of each attribute', () => { + const out = normalizeOoxmlXml( + `` + + '' + + '' + + '' + + '' + + '' + + '' + + '', + ) + expect(out).toContain('') + expect(out).toContain('') + expect(out).toContain('') + expect(out).toContain('') + expect(out).toContain('') + expect(out).toContain('') + expect(out).toContain('w:header="720" w:footer="720" w:gutter="0"') + }) + + it('leaves plain numeric values and non-w namespaces untouched', () => { + const src = + `` + + '' + + '' + const out = normalizeOoxmlXml(src) + expect(out).toContain('') + expect(out).toContain('') + }) + + it('shifts wordprocessingShape children written in the wp namespace into wps', () => { + const out = normalizeOoxmlXml( + `` + + '' + + '' + + '' + + '' + + '', + ) + expect(out).toContain( + '', + ) + expect(out).toContain('') + expect(out).toContain('') + // the anchor level stays in the wp namespace + expect(out).toContain('') + expect(out).toContain('xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"') + }) + + it('keeps mc:Ignorable prefix lists aligned with renames', () => { + const out = normalizeOoxmlXml( + `', + ) + expect(out).toContain('mc:Ignorable="w14"') + expect(out).toContain('') + }) + + it('remaps Ignorable tokens when the mc namespace itself uses another prefix', () => { + const out = normalizeOoxmlXml( + `', + ) + expect(out).toContain('mc:Ignorable="w14"') + expect(out).toContain('xmlns:mc=') + }) +}) + +describe('parseDocx over normalized packages', () => { + it('parses a document with a non-standard prefix for the main namespace', async () => { + const bytes = await buildDocxBytes( + `${XML_DECL}` + + 'Body text survives' + + '', + ) + const doc = await parseDocx(bytes) + expect( + doc.blocks + .flatMap((b) => b.runs ?? []) + .map((r) => r.text) + .join(''), + ).toContain('Body text survives') + }) + + it('parses a strict document with landscape geometry in pt units', async () => { + const bytes = await buildDocxBytes( + `${XML_DECL}` + + 'Strict text' + + '' + + '', + ) + const doc = await parseDocx(bytes) + expect( + doc.blocks + .flatMap((b) => b.runs ?? []) + .map((r) => r.text) + .join(''), + ).toContain('Strict text') + const zip = await loadDocxZip(bytes) + const normalized = await zip.file('word/document.xml')!.async('string') + expect(normalized).toContain('') + expect(normalized).not.toContain('conformance') + }) +}) diff --git a/packages/docx-engine/tests/page-anchored-boxes.test.ts b/packages/docx-engine/tests/page-anchored-boxes.test.ts index aec64af..f0d9242 100644 --- a/packages/docx-engine/tests/page-anchored-boxes.test.ts +++ b/packages/docx-engine/tests/page-anchored-boxes.test.ts @@ -160,4 +160,35 @@ describe('page/margin-anchored textboxes outside the body column', () => { expect(box?.floating).toBe(true) expect(box?.offsetXEmu).toBe(Math.round((PAGE_W * 2000) / 100000) - MARGIN) }) + + it('centers a column-relative wp:align box in a single-column section', async () => { + const posH = `center` + const posV = `0` + const para = anchorPara( + posH, + posV, + textWsp({ offX: 0, offY: 0, cx: 600000, cy: 400000, text: 'boxed' }), + '', + ).replace('', '') + const doc = await parseDocx(await buildDocx({ bodyXml: para })) + const [box] = doc.blocks[0].textboxes! + expect(box.floating).toBe(true) + expect(box.offsetXEmu).toBe(Math.round((PAGE_W - 2 * MARGIN - 600000) / 2)) + }) + + it('keeps column-relative align on the flow placement in multi-column sections', async () => { + const posH = `center` + const posV = `0` + const para = anchorPara( + posH, + posV, + textWsp({ offX: 0, offY: 0, cx: 600000, cy: 400000, text: 'boxed' }), + '', + ).replace('', '') + const doc = await parseDocx( + await buildDocx({ bodyXml: para, sectPrExtra: '' }), + ) + const [box] = doc.blocks[0].textboxes! + expect(box.offsetXEmu).toBeUndefined() + }) }) diff --git a/packages/docx-engine/tests/parse.test.ts b/packages/docx-engine/tests/parse.test.ts index 766d0b3..6185ab4 100644 --- a/packages/docx-engine/tests/parse.test.ts +++ b/packages/docx-engine/tests/parse.test.ts @@ -213,3 +213,40 @@ describe('empty paragraph line size', () => { expect(doc.blocks[2].format?.emptyRunFontFamily).toBe('Arial') }) }) + +describe('paragraph-mark w:vanish', () => { + it('collapses an empty paragraph with a hidden mark to an invisible marker', async () => { + const bodyXml = + 'before' + + '' + + 'after' + const doc = await parseDocx(await buildDocx({ bodyXml })) + expect(doc.blocks[1].invisibleMarker).toBe(true) + expect(doc.blocks[1].originalXml).toContain('w:vanish') + }) + + it('keeps a paragraph with visible runs even when its mark is hidden', async () => { + const bodyXml = + 'shown' + const doc = await parseDocx(await buildDocx({ bodyXml })) + expect(doc.blocks[0].invisibleMarker).toBeUndefined() + expect(doc.blocks[0].runs?.[0]?.text).toBe('shown') + }) + + it('keeps a text-less paragraph whose run carries a page break or note mark', async () => { + const bodyXml = + '' + + '' + const doc = await parseDocx(await buildDocx({ bodyXml })) + expect(doc.blocks[0].invisibleMarker).toBeUndefined() + expect(doc.blocks[1].invisibleMarker).toBeUndefined() + }) + + it('still collapses when only pPr tab stops are present', async () => { + const bodyXml = + '' + + '' + const doc = await parseDocx(await buildDocx({ bodyXml })) + expect(doc.blocks[0].invisibleMarker).toBe(true) + }) +}) diff --git a/packages/docx-engine/tests/smartart-ole.test.ts b/packages/docx-engine/tests/smartart-ole.test.ts index 145f614..aff2640 100644 --- a/packages/docx-engine/tests/smartart-ole.test.ts +++ b/packages/docx-engine/tests/smartart-ole.test.ts @@ -235,3 +235,57 @@ describe('field-form OLE (EMBED/LINK field around w:object)', () => { expect(saved).toBe(bytes) }) }) + +describe('OLE preview inside a table cell', () => { + const CELL_TABLE = + '' + + '' + + '' + + `${OLE_OBJECT_RUN}` + + '' + + it('the preview picture becomes a run image at the declared size', async () => { + const parsed = await parseDocx(await buildDocx({ bodyXml: CELL_TABLE, withImage: true })) + const cell = parsed.blocks[0].table!.rows[0][0] + const imageRun = cell.richParas?.[0]?.runs.find((r) => r.image) + expect(imageRun?.image?.dataUrl).toMatch(/^data:image\/png;base64,/) + // declared v:shape size (207.75pt x 15.75pt) + expect(imageRun?.image?.widthPx).toBe(277) + expect(imageRun?.image?.heightPx).toBe(21) + }) +}) + +describe('OLE embed sharing a paragraph with an inline picture', () => { + const DRAWING_RUN = + '' + + '' + + '' + + '' + + it('keeps both the OLE preview and the picture as run images', async () => { + const bodyXml = `${OLE_OBJECT_RUN}${DRAWING_RUN}` + const parsed = await parseDocx(await buildDocx({ bodyXml, withImage: true })) + const block = parsed.blocks[0] + expect(block.type).toBe('paragraph') + const images = (block.runs ?? []).filter((r) => r.image) + expect(images).toHaveLength(2) + expect(images[0].image?.widthPx).toBe(277) + expect(images[1].image?.widthPx).toBe(96) + }) +}) + +describe('OLE run carrying an empty sibling w:pict', () => { + it('still resolves the object preview (first pict with a picture wins)', async () => { + const bodyXml = + '' + + OLE_OBJECT_RUN.replace('', '').replace('', '') + + 'bit map object' + + '' + const parsed = await parseDocx(await buildDocx({ bodyXml, withImage: true })) + const block = parsed.blocks[0] + expect(block.type).toBe('paragraph') + const imageRun = (block.runs ?? []).find((r) => r.image) + expect(imageRun?.image?.dataUrl).toMatch(/^data:image\/png;base64,/) + expect((block.runs ?? []).map((r) => r.text).join('')).toBe('bit map object') + }) +}) diff --git a/packages/docx-engine/tests/table-edit.test.ts b/packages/docx-engine/tests/table-edit.test.ts index 6e8ac4a..f865ed3 100644 --- a/packages/docx-engine/tests/table-edit.test.ts +++ b/packages/docx-engine/tests/table-edit.test.ts @@ -197,6 +197,96 @@ describe('generateTableModelXml', () => { '', ) }) + + it('round-trips AutoFit, cell margins, floating position, and table style options', async () => { + const xml = generateTableModelXml({ + rows: [[{ paras: ['a'] }, { paras: ['b'] }]], + autoFit: 'window', + cellMarTwips: { top: 80, right: 160, bottom: 90, left: 170 }, + floatSide: 'left', + floatPos: { + xTwips: 720, + yTwips: 360, + horzAnchor: 'margin', + vertAnchor: 'text', + distanceTwips: { top: 60, right: 120, bottom: 70, left: 130 }, + }, + tableLook: { + firstRow: true, + lastRow: true, + firstColumn: false, + lastColumn: true, + bandedRows: false, + bandedColumns: true, + }, + }) + expect(xml).toContain('') + expect(xml).toContain('') + expect(xml).toContain('w:tblpX="720"') + expect(xml).toContain('w:tblpY="360"') + expect(xml).toContain('w:rightFromText="120"') + expect(xml).toContain('') + expect(xml).toContain('w:lastRow="1"') + expect(xml).toContain('w:noHBand="1"') + expect(xml).toContain('w:noVBand="0"') + + const parsed = await parseDocx(await buildDocx({ bodyXml: xml })) + const table = parsed.blocks[0].table! + expect(table.autoFit).toBe('window') + expect(table.cellMarTwips).toEqual({ top: 80, left: 170, bottom: 90, right: 160 }) + expect(table.floatSide).toBe('left') + expect(table.floatPos).toMatchObject({ + xTwips: 720, + yTwips: 360, + horzAnchor: 'margin', + vertAnchor: 'text', + distanceTwips: { top: 60, right: 120, bottom: 70, left: 130 }, + }) + expect(table.tableLook).toMatchObject({ + firstRow: true, + lastRow: true, + firstColumn: false, + lastColumn: true, + bandedRows: false, + bandedColumns: true, + }) + }) + + it('patches imported table properties and repeat-header rows explicitly', () => { + const template = + '' + + '' + + '' + + '' + + 'Old' + + '' + const out = generateTableModelXml( + { + rows: [[{ paras: ['New'] }]], + autoFit: 'contents', + cellMarTwips: { top: 0, right: 108, bottom: 0, left: 108 }, + floatSide: null, + repeatHeaderRows: [false], + }, + template, + ) + expect(out).toContain('') + expect(out).toContain('') + expect(out).not.toContain('w:tblpPr') + expect(out).not.toContain('w:tblOverlap') + expect(out).not.toContain('w:tblHeader') + expect(out).toContain('') + }) + + it('writes repeating header rows without disturbing other trPr children', () => { + const xml = generateTableModelXml({ + rows: [[{ paras: ['Header'] }], [{ paras: ['Body'] }]], + rawTrPrs: ['', null], + repeatHeaderRows: [true, false], + }) + expect(xml).toContain('') + expect(xml.match(//g)).toHaveLength(1) + }) }) describe('tcPr/trPr fidelity and new attributes', () => { diff --git a/packages/docx-engine/tests/table-grid-reconcile.test.ts b/packages/docx-engine/tests/table-grid-reconcile.test.ts new file mode 100644 index 0000000..af8e29d --- /dev/null +++ b/packages/docx-engine/tests/table-grid-reconcile.test.ts @@ -0,0 +1,189 @@ +import { describe, expect, it } from 'vitest' +import { generateTableModelXml, parseDocx } from '../src/index' +import { buildDocx } from './helpers/build-docx' + +function tc(text: string, tcPr = ''): string { + const pr = tcPr ? `${tcPr}` : '' + return `${pr}${text}` +} + +function gridXml(widths: number[]): string { + return `${widths.map((w) => ``).join('')}` +} + +async function tableOf(bodyXml: string) { + const doc = await parseDocx(await buildDocx({ bodyXml })) + const block = doc.blocks[0] + expect(block.type).toBe('table') + return block.table! +} + +/** twips width a cell spans, from the resolved grid */ +function cellWidth(widths: number[], row: { colSpan?: number }[], index: number): number { + let col = 0 + for (let i = 0; i < index; i++) col += row[i].colSpan ?? 1 + return widths.slice(col, col + (row[index].colSpan ?? 1)).reduce((a, b) => a + b, 0) +} + +describe('grid reconciliation from tcW boundaries', () => { + it('rebuilds the grid when row spans under-cover it (govdocs roster shape)', async () => { + // 7-column grid, but every row only spans 5 columns; the second cell would + // otherwise land on the 28-twip sliver and collapse to one char per line + const table = await tableOf( + '' + + gridXml([79, 489, 4472, 810, 28, 3780, 34]) + + '' + + tc('Crew Name:', '') + + tc('Host Unit/Address:', '') + + '' + + '' + + tc('a', '') + + tc('b', '') + + tc('c', '') + + tc('d', '') + + '' + + '', + ) + const widths = table.colWidthsTwips! + expect(cellWidth(widths, table.rows[0], 0)).toBe(5799) + expect(cellWidth(widths, table.rows[0], 1)).toBe(3780) + expect(cellWidth(widths, table.rows[1], 3)).toBe(3842) + // no column may be a sliver that a text cell sits on alone + expect( + Math.min(...table.rows.flatMap((r) => r.map((_c, ci) => cellWidth(widths, r, ci)))), + ).toBeGreaterThan(500) + }) + + it('re-aligns rows that dropped a gridSpan (govdocs workload shape)', async () => { + // 8-column grid; the second data row lost the gridSpan on its label cell, + // so its 7 cells drifted one column left and the label collapsed to 5 twips + const auto = '' + const dataCells = tc('1,582', auto) + tc('1,537', auto) + tc('1,139', auto) + tc('1,093', auto) + const table = await tableOf( + '' + + gridXml([5, 3282, 1125, 1116, 1116, 1116, 1123, 1232]) + + '' + + tc('Labels', '') + + tc('FY', '') + + dataCells + + tc('Est.', '') + + '' + + '' + + tc('Pending', '') + + tc('260', '') + + dataCells + + tc('89', '') + + '' + + '', + ) + const widths = table.colWidthsTwips! + // both label cells resolve to the same wide column(s) + expect(cellWidth(widths, table.rows[0], 0)).toBe(3283) + expect(cellWidth(widths, table.rows[1], 0)).toBe(3283) + // both rows cover the same total width (no one-column drift / empty tail) + const total = (r: { colSpan?: number }[]) => + r.reduce((sum, _c, i) => sum + cellWidth(widths, r, i), 0) + expect(total(table.rows[1])).toBe(total(table.rows[0])) + }) + + it('leaves consistent grids untouched', async () => { + const table = await tableOf( + '' + + gridXml([2000, 4000, 2000]) + + '' + + tc('a', '') + + tc('bc', '') + + '' + + '' + + tc('a', '') + + tc('b', '') + + tc('c', '') + + '' + + '', + ) + expect(table.colWidthsTwips).toEqual([2000, 4000, 2000]) + expect(table.rows[0][1].colSpan).toBe(2) + expect(table.rows[1].every((c) => (c.colSpan ?? 1) === 1)).toBe(true) + }) +}) + +describe('gridBefore/gridAfter', () => { + const STAGGERED = + '' + + gridXml([1000, 1000, 1000, 1000]) + + '' + + tc('a1', '') + + tc('a2', '') + + tc('a3', '') + + '' + + '' + + tc('b1', '') + + tc('b2', '') + + '' + + '' + + it('inserts borderless placeholder cells for the skipped columns', async () => { + const table = await tableOf(STAGGERED) + expect(table.colWidthsTwips).toEqual([1000, 1000, 1000, 1000]) + const [row1, row2] = table.rows + expect(row1.map((c) => c.gridGap ?? false)).toEqual([false, false, false, true]) + expect(row2.map((c) => c.gridGap ?? false)).toEqual([true, false, false]) + expect(row2[0].colSpan).toBe(2) + // offset row's first real cell starts at grid column 2 + expect(row2[1].paras[0]).toBe('b1') + // rawTcPr still lands on the real cells despite the inserted placeholders + expect(row2[0].rawTcPr).toBeUndefined() + expect(row2[1].rawTcPr).toContain(' { + const table = await tableOf(STAGGERED) + const xml = generateTableModelXml(table) + expect(xml.match(//g)).toHaveLength(5) + // rawTrPr passthrough keeps the original offsets exactly once + expect(xml.match(/') + expect(regen).toContain('') + expect(regen.match(//g)).toHaveLength(5) + }) +}) + +describe('cell spacing and shading patterns', () => { + it('parses w:tblCellSpacing and table-level shading', async () => { + const table = await tableOf( + '' + + '' + + '' + + gridXml([2000, 2000]) + + '' + + tc('a', '') + + tc('b', '') + + '' + + '', + ) + expect(table.cellSpacingTwips).toBe(142) + expect(table.fill).toBe('FFC000') + }) + + it('approximates pattern shading as blended fills', async () => { + const table = await tableOf( + '' + + gridXml([2000, 2000, 2000]) + + '' + + tc('gray', '') + + tc('stripe', '') + + tc('plain', '') + + '' + + '', + ) + const [gray, stripe, plain] = table.rows[0] + expect(gray.fill).toBe('F2F2F2') + expect(stripe.fill).toBe('B7D98D') + expect(plain.fill).toBeUndefined() + }) +}) diff --git a/packages/docx-engine/tests/themeless-shapes-external-txbx.test.ts b/packages/docx-engine/tests/themeless-shapes-external-txbx.test.ts new file mode 100644 index 0000000..a42fc48 --- /dev/null +++ b/packages/docx-engine/tests/themeless-shapes-external-txbx.test.ts @@ -0,0 +1,106 @@ +import { describe, expect, it } from 'vitest' +import { parseDocx } from '../src/parse' +import { buildDocx } from './helpers/build-docx' + +const WPS_NS = 'xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"' +const MC_NS = 'xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"' + +function anchorParagraph(wsp: string, drawingAttrs = ''): string { + return ( + `` + + '' + + '584200' + + '127000' + + '' + + '' + + '' + + `${wsp}` + + '
    ' + ) +} + +describe('themeless documents (no word/theme/theme1.xml)', () => { + it('resolves wps:style schemeClr against the built-in Office palette', async () => { + // docx4j 2010-glow-then-Alt / 2010-mcAlternateCo: a gallery ellipse whose + // only ink comes from fillRef/lnRef accent1 — colors:null dropped it + const wsp = + `` + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + const parsed = await parseDocx(await buildDocx({ bodyXml: anchorParagraph(wsp) })) + const box = parsed.blocks.find((b) => b.textboxes?.length)?.textboxes?.[0] + expect(box).toBeDefined() + expect(box?.prst).toBe('ellipse') + expect(box?.fill).toBe('4472C4') + // accent1 at 50% shade + expect(box?.borderColor).toBe('223962') + expect(parsed.themeColors?.accent1).toBe('4472C4') + }) +}) + +describe('w:drawing with attributes (mc:MustUnderstand)', () => { + it('extracts the shape from an attributed open tag', async () => { + const wsp = + `` + + '' + + '' + + '' + + '' + const parsed = await parseDocx( + await buildDocx({ + bodyXml: anchorParagraph(wsp, ` ${MC_NS} mc:MustUnderstand="wps"`), + }), + ) + const box = parsed.blocks.find((b) => b.textboxes?.length)?.textboxes?.[0] + expect(box).toBeDefined() + expect(box?.fill).toBe('FF0000') + expect(box?.floating).toBe(true) + }) +}) + +describe('external textbox part (wps:txbx r:txbx)', () => { + it('reads the referenced word/txbx1.xml paragraphs into the box, display-only', async () => { + // TestFiles mcdoc: the Choice textbox stores its content in a separate + // part whose w14:txbx root carries the w:p list directly + const wsp = + `` + + '' + + '' + + '' + + '' + + '' + const txbxPart = + '' + + '' + + 'hello' + const parsed = await parseDocx( + await buildDocx({ + bodyXml: anchorParagraph(wsp), + extraRels: + '', + extraParts: [ + { + path: 'word/txbx1.xml', + xml: txbxPart, + contentType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.txbx+xml', + }, + ], + }), + ) + const box = parsed.blocks.find((b) => b.textboxes?.length)?.textboxes?.[0] + expect(box).toBeDefined() + expect(box?.fill).toBe('FFFFFF') + expect(box?.borderColor).toBe('000000') + expect(box?.paras.map((p) => p.runs.map((r) => r.text).join(''))).toEqual(['hello']) + // the external content is not patchable in document.xml + expect(box?.readOnly).toBe(true) + expect(box?.txbxIndex).toBeUndefined() + }) +}) diff --git a/packages/docx-engine/tests/watermark-theme-sources.test.ts b/packages/docx-engine/tests/watermark-theme-sources.test.ts index 3de0e44..a8cb2d6 100644 --- a/packages/docx-engine/tests/watermark-theme-sources.test.ts +++ b/packages/docx-engine/tests/watermark-theme-sources.test.ts @@ -262,15 +262,17 @@ describe('w:themeColor resolution', () => { expect(doc.blocks[0].runs?.[1].color).toBe('335593') }) - it('w:val alone still wins when the doc has no theme part', async () => { + it('resolves against the built-in Office palette when the doc has no theme part', async () => { + // Word materializes the default theme for themeless documents: accent1 + // still beats the cached w:val const doc = await parseDocx( await buildDocx({ bodyXml: 'x', }), ) - expect(doc.themeColors).toBeNull() - expect(doc.blocks[0].runs?.[0].color).toBe('FF0000') + expect(doc.themeColors?.accent1).toBe('4472C4') + expect(doc.blocks[0].runs?.[0].color).toBe('4472C4') }) it('an untouched themed document still saves byte-identical', async () => { diff --git a/packages/docx-engine/tests/wordart-vml.test.ts b/packages/docx-engine/tests/wordart-vml.test.ts index 8e823a9..1183bfd 100644 --- a/packages/docx-engine/tests/wordart-vml.test.ts +++ b/packages/docx-engine/tests/wordart-vml.test.ts @@ -65,6 +65,94 @@ describe('VML WordArt (v:textpath) display', () => { expect(box?.textOutline).toBeUndefined() }) + it('keeps anchored DrawingML photos sharing the paragraph with the WordArt shape', async () => { + const anchoredPic = (blipXml: string, cx: number, cy: number): string => + '' + + '914400' + + '457200' + + `` + + '' + + `${blipXml}` + + // pic:spPr carries an EMPTY a:blip in its own blipFill (real-world Word + // output): media resolution must not trip over it + '' + + '' + + '' + const bodyXml = + '' + + `` + + '' + + '' + + anchoredPic('', 1418590, 1201420) + + // second blip carries both r:embed and r:link — the embedded part must resolve + anchoredPic('', 1448435, 1280160) + + '' + const doc = await parseDocx( + await buildDocx({ + bodyXml, + withImage: true, + extraRels: + '' + + '', + }), + ) + const block = doc.blocks[0] + expect(block.label).toBe('Text box') + const boxes = block.textboxes ?? [] + expect(boxes.some((b) => b.paras[0]?.runs[0]?.text === 'Moon Landing')).toBe(true) + const photos = boxes.filter((b) => b.fillImageDataUrl) + expect(photos).toHaveLength(2) + for (const photo of photos) { + expect(photo.fillImageDataUrl).toMatch(/^data:image\/png;base64,/) + } + expect(photos.map((p) => p.widthPx)).toEqual([149, 152]) + }) + + it('does not lift a picture nested in VML textbox content into a page-level box', async () => { + const innerDrawing = + '' + + '' + + '' + + '' + const bodyXml = + `` + + '' + + `caption${innerDrawing}` + + '' + const doc = await parseDocx(await buildDocx({ bodyXml, withImage: true })) + const boxes = doc.blocks[0].textboxes ?? [] + expect(boxes).toHaveLength(1) + expect(boxes[0].fillImageDataUrl).toBeUndefined() + expect(boxes[0].paras[0]?.runs[0]?.text).toBe('caption') + }) + + it('page-pins page-anchored wrapNone photos next to WordArt like the drawing branch', async () => { + const pageAnchoredPic = (x: number, y: number): string => + '' + + `${x}` + + `${y}` + + '' + + '' + + '' + + '' + const bodyXml = + 'logo line above' + + `` + + '' + + pageAnchoredPic(914400, 1828800) + + pageAnchoredPic(2743200, 1828800) + + '' + const doc = await parseDocx(await buildDocx({ bodyXml, withImage: true })) + const photos = (doc.blocks[1].textboxes ?? []).filter((b) => b.fillImageDataUrl) + expect(photos).toHaveLength(2) + // raw page coordinates, pinned to the page box (not the paragraph origin) + expect(photos.map((p) => p.pagePinned)).toEqual([true, true]) + expect(photos.map((p) => p.offsetXEmu)).toEqual([914400, 2743200]) + expect(photos.map((p) => p.offsetYEmu)).toEqual([1828800, 1828800]) + }) + it('ignores string-less v:textpath (shapetype template / watermark furniture)', async () => { const doc = await parseDocx( await buildDocx({ diff --git a/packages/docx-engine/tests/write-protection.test.ts b/packages/docx-engine/tests/write-protection.test.ts index 0acd049..6f60f8a 100644 --- a/packages/docx-engine/tests/write-protection.test.ts +++ b/packages/docx-engine/tests/write-protection.test.ts @@ -246,9 +246,10 @@ describe('removePersonalInformation', () => { const read = (path: string) => zip.file(path)!.async('string') const documentXml = await read('word/document.xml') - expect(documentXml).toContain("x:author='Author'") - expect(documentXml).toContain("x:id='body-rev'") - expect(documentXml).toContain("x:date='2024-02-01T02:03:04Z'") + // load-time namespace normalization renames the x binding to the canonical w + expect(documentXml).toContain('w:author="Author"') + expect(documentXml).toContain('w:id="body-rev"') + expect(documentXml).toContain('w:date="2024-02-01T02:03:04Z"') expect(documentXml).toContain('Body Person remains as content') expect(documentXml).toContain('author="Visible Person"') expect(documentXml).toContain("w:author='Visible Qualified'") @@ -282,10 +283,10 @@ describe('removePersonalInformation', () => { expect(xml).toContain(date) } const comments = await read('word/comments.xml') - expect(comments).toContain("c:author='Author'") - expect(comments).toContain("c:initials='A'") - expect(comments).toContain("c:id='comment-7'") - expect(comments).toContain("c:date='2024-03-01T02:03:04Z'") + expect(comments).toContain('w:author="Author"') + expect(comments).toContain('w:initials="A"') + expect(comments).toContain('w:id="comment-7"') + expect(comments).toContain('w:date="2024-03-01T02:03:04Z"') const core = await read('docProps/core.xml') expect(core).toContain("") @@ -299,8 +300,8 @@ describe('removePersonalInformation', () => { expect(app).toContain('Preserved App') const people = await read('word/people.xml') - expect(people).toContain(' + /** Dependency part bytes (base64, keyed by absolute path), so the paste target can be a different deck */ + parts?: Readonly> + /** ContentType per dependency: keyed by lower-case extension or by part path */ + contentTypes?: Readonly> } const RID_ATTR_RE = /\br:(?:embed|link|id)="(rId\d+)"/g @@ -3614,6 +3620,8 @@ export function copyElementData( const xml = patchedElementXml(el) const slideRels = opened.archive.readRels(slide.path) const rels: ElementClipboardItem['rels'] = [] + const parts: Record = {} + const contentTypes: Record = {} const seen = new Set() for (const m of xml.matchAll(RID_ATTR_RE)) { const rid = m[1]! @@ -3622,14 +3630,20 @@ export function copyElementData( const rel = slideRels.get(rid) if (!rel) continue const external = rel.targetMode === 'External' + const target = external ? rel.target : resolveTarget(slide.path, rel.target) + // Slide-jump hyperlinks reference a whole slide; bundling it would drag in its + // layout/master/theme graph, so keep those as path-only references + if (!external && !rel.type.endsWith('/slide')) { + collectPart(opened.archive, target, parts, contentTypes) + } rels.push({ rid, type: rel.type, - target: external ? rel.target : resolveTarget(slide.path, rel.target), + target, ...(external ? { external: true } : {}), }) } - return { xml, rels } + return { xml, rels, ...(Object.keys(parts).length ? { parts, contentTypes } : {}) } } /** 'ppt/media/image1.png' → the relative Target '../media/image1.png' in the slide rels */ @@ -3638,10 +3652,11 @@ function relTargetFromSlide(absTarget: string): string { } /** - * Paste: rels surgery (reuse the rId when the target slide already has a - * relationship with the same type+target, otherwise create one pointing at the - * same part — media bytes are not copied), renumber cNvPr ids, offset the whole - * thing, append, and reparse. + * Paste: land bundled dependency parts (byte-identical ones already in the + * target are shared, so a same-deck paste copies no media), rels surgery + * (reuse the rId when the target slide already has a relationship with the + * same type+target), renumber cNvPr ids, offset the whole thing, append, and + * reparse. */ export function pasteElements( opened: OpenedPptx, @@ -3667,15 +3682,27 @@ export function pasteElements( byKey.set(`${rel.type} ${abs}`, rel.id) } + // Land dependency parts the target deck is missing (all items at once, so shared media imports once) + const bundledParts: Record = {} + const bundledCT: Record = {} + for (const item of items) { + Object.assign(bundledParts, item.parts ?? {}) + Object.assign(bundledCT, item.contentTypes ?? {}) + } + const pathMap = Object.keys(bundledParts).length + ? importClipboardParts(archive, bundledParts, bundledCT) + : null + let nextId = nextCNvPrId(slide) const xmls = items.map((item) => { let xml = item.xml for (const rel of item.rels) { - const key = `${rel.type} ${rel.target}` + const landed = rel.external ? rel.target : (pathMap?.get(rel.target) ?? rel.target) + const key = `${rel.type} ${landed}` let rid = byKey.get(key) if (!rid) { rid = `rId${++maxRid}` - const target = rel.external ? rel.target : relTargetFromSlide(rel.target) + const target = rel.external ? rel.target : relTargetFromSlide(landed) const mode = rel.external ? ' TargetMode="External"' : '' relsXml = relsXml.replace( '', diff --git a/packages/pptx-engine/src/parse.ts b/packages/pptx-engine/src/parse.ts index c4a23ec..85afdd1 100644 --- a/packages/pptx-engine/src/parse.ts +++ b/packages/pptx-engine/src/parse.ts @@ -410,6 +410,7 @@ function parseSpShape( let glow = parseGlow(spPr, ctx) const reflection = parseReflection(spPr) const scene3d = parseScene3D(spPr, ctx) + const softEdgeRad = spPr?.['a:effectLst']?.['a:softEdge']?.['@_rad'] // holds a second fill element directly (a:gradFill/…), so parseFill reads it like an spPr const overlayNode = spPr?.['a:effectLst']?.['a:fillOverlay'] const fillOverlay = overlayNode ? parseFill(overlayNode, ctx) : undefined @@ -493,6 +494,7 @@ function parseSpShape( ...(glow ? { glow } : {}), ...(reflection ? { reflection } : {}), ...(scene3d ? { scene3d } : {}), + ...(softEdgeRad != null ? { softEdge: intOr(softEdgeRad, 0) } : {}), text, } return el diff --git a/packages/pptx-engine/src/slide-transfer.ts b/packages/pptx-engine/src/slide-transfer.ts index dab1cec..4392f66 100644 --- a/packages/pptx-engine/src/slide-transfer.ts +++ b/packages/pptx-engine/src/slide-transfer.ts @@ -66,7 +66,7 @@ function defaultContentType(ct: string | null, ext: string): string | undefined } /** Walks a part's rel graph, collecting bytes + content types for everything reachable. */ -function collectPart( +export function collectPart( archive: PackageArchive, partPath: string, parts: Record, @@ -219,12 +219,17 @@ function writeParts( archive: PackageArchive, parts: Readonly>, contentTypes: Readonly>, + reusable?: ReadonlySet, ): Map { // rels parts follow their owner, so map the owners first const pathMap = new Map() const taken = new Set() for (const sourcePath of Object.keys(parts)) { if (sourcePath.includes('/_rels/')) continue + if (reusable?.has(sourcePath)) { + pathMap.set(sourcePath, sourcePath) + continue + } const dest = archive.has(sourcePath) || taken.has(sourcePath) ? freePath(archive, sourcePath, taken) @@ -234,7 +239,7 @@ function writeParts( } for (const [sourcePath, base64] of Object.entries(parts)) { - if (sourcePath.includes('/_rels/')) continue + if (sourcePath.includes('/_rels/') || reusable?.has(sourcePath)) continue const targetPath = pathMap.get(sourcePath) if (!targetPath) continue archive.entries.set(targetPath, new Uint8Array(Buffer.from(base64, 'base64'))) @@ -256,6 +261,65 @@ function writeParts( return pathMap } +/** + * Parts the target archive already holds byte-identically (same path, same + * bytes, same rels, dependencies transitively identical too) — those can be + * shared instead of re-imported, so a same-deck paste never duplicates media. + */ +function reusableParts( + archive: PackageArchive, + parts: Readonly>, +): Set { + const memo = new Map() + const identical = (sourcePath: string): boolean => { + const cached = memo.get(sourcePath) + if (cached !== undefined) return cached + memo.set(sourcePath, false) // cycle guard + let ok = false + const existing = archive.readBytes(sourcePath) + if (existing && Buffer.from(existing).equals(Buffer.from(parts[sourcePath]!, 'base64'))) { + const relsPath = relsPathFor(sourcePath) + const bundledRels = parts[relsPath] + const existingRels = archive.readText(relsPath) + if (bundledRels == null) ok = existingRels == null + else if (existingRels != null) { + ok = Buffer.from(bundledRels, 'base64').toString('utf8') === existingRels + if (ok) { + for (const rel of archive.readRels(sourcePath).values()) { + if (rel.targetMode === 'External') continue + const dep = resolveTarget(sourcePath, rel.target) + if (parts[dep] != null && !identical(dep)) { + ok = false + break + } + } + } + } + } + memo.set(sourcePath, ok) + return ok + } + const reusable = new Set() + for (const sourcePath of Object.keys(parts)) { + if (!sourcePath.includes('/_rels/') && identical(sourcePath)) reusable.add(sourcePath) + } + return reusable +} + +/** + * Lands an element-clipboard's dependency parts in `archive` (paste target). + * Byte-identical parts already present are shared — a same-deck paste writes + * nothing; a cross-deck paste imports media/charts under non-conflicting names. + * Returns the source→destination path map. + */ +export function importClipboardParts( + archive: PackageArchive, + parts: Readonly>, + contentTypes: Readonly>, +): Map { + return writeParts(archive, parts, contentTypes, reusableParts(archive, parts)) +} + export function materializeSlideBundle( archive: PackageArchive, bundle: SlideBundle, diff --git a/packages/pptx-engine/src/types.ts b/packages/pptx-engine/src/types.ts index 4b51025..cd015e9 100644 --- a/packages/pptx-engine/src/types.ts +++ b/packages/pptx-engine/src/types.ts @@ -441,6 +441,8 @@ export interface TextElement extends ElementBase { glow?: GlowEffect reflection?: ReflectionEffect scene3d?: Scene3D + /** Soft edges (EMU feather radius) */ + softEdge?: number text?: TextBody } diff --git a/packages/pptx-engine/tests/clipboard-table-transition.test.ts b/packages/pptx-engine/tests/clipboard-table-transition.test.ts index 5688f8e..560151b 100644 --- a/packages/pptx-engine/tests/clipboard-table-transition.test.ts +++ b/packages/pptx-engine/tests/clipboard-table-transition.test.ts @@ -17,6 +17,7 @@ import { openPptx, pasteElements, reorderElement, + setElementLink, savePptx, setSlideAnimations, setSlideTransition, @@ -37,6 +38,14 @@ const PNG_1PX = Uint8Array.from( ), ) +// 1x1 blue PNG (distinct bytes from PNG_1PX) +const PNG_BLUE = Uint8Array.from( + Buffer.from( + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNgYPgPAAEDAQAIicLsAAAAAElFTkSuQmCC', + 'base64', + ), +) + describe('copy/paste elements', () => { it('pastes a textbox onto the same slide with shifted offset', async () => { const opened = await openPptx(fx('01_standard_business.pptx')) @@ -100,6 +109,74 @@ describe('copy/paste elements', () => { expect(mediaParts.filter((p) => p === pic.mediaRef).length).toBe(1) }) + it('pastes a picture into a different deck (media bytes travel with the clipboard)', async () => { + const source = await openPptx(fx('01_standard_business.pptx')) + const slide0 = source.deck.slides[0]! + const pic = addPicture(source, slide0, { bytes: PNG_1PX, ext: 'png', offset: { ...OFF } })! + const clip = copyElementData(source, slide0, pic) + expect(clip.parts?.[pic.mediaRef!]).toBeDefined() + + const target = await openPptx(fx('01_standard_business.pptx')) + const before = target.deck.slides[0]!.elements.length + const r = pasteElements(target, 0, [clip], SHIFT) + expect(r).not.toBeNull() + expect(r!.slide.elements.length).toBe(before + 1) + const pasted = r!.slide.elements.at(-1) as PictureElement + expect(pasted.type).toBe('picture') + expect(Buffer.from(target.archive.readBytes(pasted.mediaRef!)!)).toEqual(Buffer.from(PNG_1PX)) + + const reopened = await openPptx(await savePptx(target)) + const el2 = reopened.deck.slides[0]!.elements.at(-1) as PictureElement + expect(el2.type).toBe('picture') + expect(Buffer.from(reopened.archive.readBytes(el2.mediaRef!)!)).toEqual(Buffer.from(PNG_1PX)) + }) + + it('cross-deck paste keeps a different image already at the same media path', async () => { + const source = await openPptx(fx('01_standard_business.pptx')) + const srcPic = addPicture(source, source.deck.slides[0]!, { + bytes: PNG_1PX, + ext: 'png', + offset: { ...OFF }, + })! + const clip = copyElementData(source, source.deck.slides[0]!, srcPic) + + // The target deck holds different bytes at the very same media path + const target = await openPptx(fx('01_standard_business.pptx')) + const tgtPic = addPicture(target, target.deck.slides[0]!, { + bytes: PNG_BLUE, + ext: 'png', + offset: { ...OFF }, + })! + expect(tgtPic.mediaRef).toBe(srcPic.mediaRef) + + const r = pasteElements(target, 0, [clip], SHIFT)! + const pasted = r.slide.elements.at(-1) as PictureElement + expect(pasted.mediaRef).not.toBe(tgtPic.mediaRef) + expect(Buffer.from(target.archive.readBytes(pasted.mediaRef!)!)).toEqual(Buffer.from(PNG_1PX)) + expect(Buffer.from(target.archive.readBytes(tgtPic.mediaRef!)!)).toEqual(Buffer.from(PNG_BLUE)) + }) + + it('slide-jump hyperlink does not drag the target slide graph into the clipboard', async () => { + const source = await openPptx(fx('01_standard_business.pptx')) + const slide0 = source.deck.slides[0]! + const el = addElement(slide0, { + kind: 'rect', + offset: { ...OFF }, + paragraphs: [{ runs: [{ text: 'go to 2' }] }], + }) + const linked = setElementLink(source, 0, el.id, { kind: 'slide', slideIndex: 1 })! + const clip = copyElementData(source, linked, linked.elements.at(-1)!) + expect(clip.rels.some((r) => r.target === source.deck.slides[1]!.path)).toBe(true) + expect(Object.keys(clip.parts ?? {})).toEqual([]) + + // Cross-deck paste imports nothing extra; the link resolves by path in the target + const target = await openPptx(fx('01_standard_business.pptx')) + const partsBefore = target.archive.entries.size + const r = pasteElements(target, 0, [clip], SHIFT) + expect(r).not.toBeNull() + expect(target.archive.entries.size).toBe(partsBefore) + }) + it('pending text edits are baked into the copied xml', async () => { const opened = await openPptx(fx('01_standard_business.pptx')) const slide = opened.deck.slides[0]! diff --git a/packages/pptx-engine/tests/color-preset.test.ts b/packages/pptx-engine/tests/color-preset.test.ts index cb4f858..18ae1fa 100644 --- a/packages/pptx-engine/tests/color-preset.test.ts +++ b/packages/pptx-engine/tests/color-preset.test.ts @@ -18,10 +18,65 @@ describe('resolveColorNode a:prstClr', () => { 'a:alpha': { '@_val': '40000' }, }, } - expect(resolveColorNode(node, undefined)).toBe('#FF4D4D66') + // lumOff in HSL luminance: red L 0.5 → 0.8 with saturation kept = #FF9999 + // (matches Office's published tint table, e.g. 4472C4 lumMod40/lumOff60 → B4C7E7) + expect(resolveColorNode(node, undefined)).toBe('#FF999966') }) it('unknown preset name resolves to undefined', () => { expect(resolveColorNode({ 'a:prstClr': { '@_val': 'nope' } }, undefined)).toBeUndefined() }) }) + +describe('HSL modifiers', () => { + it('hueOff rotates hue (SmartArt colorful cycle)', () => { + // red + 120° hue offset (val in 1/60000 deg) → green, saturation/luminance kept + const node = { + 'a:srgbClr': { '@_val': 'FF0000', 'a:hueOff': { '@_val': String(120 * 60000) } }, + } + expect(resolveColorNode(node, undefined)).toBe('#00FF00') + }) + + it('satOff shifts saturation', () => { + // 50%-saturation red minus 25% saturation + const node = { + 'a:srgbClr': { '@_val': 'BF4040', 'a:satOff': { '@_val': '-25000' } }, + } + expect(resolveColorNode(node, undefined)).toBe('#9F6060') + }) + + it('lumMod/lumOff operate on HSL luminance with saturation kept', () => { + // Per-channel ±1/255: the HSL round-trip rounds differently across platforms + const expectHexClose = (got: string | undefined, want: string) => { + expect(got).toBeTruthy() + for (let i = 1; i < 7; i += 2) { + const g = parseInt(got!.slice(i, i + 2), 16) + const w = parseInt(want.slice(i, i + 2), 16) + expect(Math.abs(g - w)).toBeLessThanOrEqual(1) + } + } + // Office tint table: 4472C4 "Lighter 60%" (lumMod 40 + lumOff 60) = B4C7E7 + const lighter = { + 'a:srgbClr': { + '@_val': '4472C4', + 'a:lumMod': { '@_val': '40000' }, + 'a:lumOff': { '@_val': '60000' }, + }, + } + expectHexClose(resolveColorNode(lighter, undefined), '#B4C7E7') + // Office tint table: 4472C4 "Darker 25%" (lumMod 75) = 2F5496 + const darker = { + 'a:srgbClr': { '@_val': '4472C4', 'a:lumMod': { '@_val': '75000' } }, + } + expectHexClose(resolveColorNode(darker, undefined), '#2F5496') + // Dark saturated accent + lighten stays vivid (prod_011 org chart: 7F0000 → #FF6666) + const vivid = { + 'a:srgbClr': { + '@_val': '7F0000', + 'a:lumMod': { '@_val': '40000' }, + 'a:lumOff': { '@_val': '60000' }, + }, + } + expectHexClose(resolveColorNode(vivid, undefined), '#FF6666') + }) +}) diff --git a/packages/pptx-render/src/build-slide.ts b/packages/pptx-render/src/build-slide.ts index 1f8cf64..64242bc 100644 --- a/packages/pptx-render/src/build-slide.ts +++ b/packages/pptx-render/src/build-slide.ts @@ -382,6 +382,7 @@ function buildShape( ...(el.placeholder ? { placeholder: el.placeholder } : {}), ...(el.txBox ? { txBox: true } : {}), ...(el.presetGeometry ? { presetGeometry: el.presetGeometry } : {}), + ...(el.softEdge ? { softEdgePx: emuToPx(el.softEdge, vp.scale) } : {}), } // Raw avLst values ride along for the edit layer (yellow adjust handles) if (el.adjust) node.adjust = { ...el.adjust } diff --git a/packages/pptx-render/src/preset-geometry.ts b/packages/pptx-render/src/preset-geometry.ts index 754b69e..ce01b02 100644 --- a/packages/pptx-render/src/preset-geometry.ts +++ b/packages/pptx-render/src/preset-geometry.ts @@ -1324,6 +1324,34 @@ export function presetPath( .Z() return { path: b.d() } } + case 'swooshArrow': { + // ECMA-376 swooshArrow: rising swoosh body (two quad beziers) with an arrowhead at top-right + const a1 = Math.min(Math.max(adjust?.adj1 ?? 25000, 1), 75000) / 100000 + const maxAdj2 = (70000 * w) / ss + const a2 = Math.min(Math.max(adjust?.adj2 ?? 16667, 0), maxAdj2) + const ad1 = h * a1 + const ad2 = (ss * a2) / 100000 + const ssd8 = ss / 8 + const tanAlfa = Math.tan(Math.PI / 2 / 14) + const xB = w - ad2 + const yB = ssd8 + const xC = xB - ssd8 * tanAlfa + const yF = yB + ad1 + const xF = xB + ad1 * tanAlfa + const xE = xF + ssd8 * tanAlfa + const yE = yF + ssd8 + const yD = yE / 2 + h / 20 + const b = new PathB() + b.M(0, h) + .Q(w / 6, h / 3, xB, yB) + .L(xC, 0) + .L(w, yD) + .L(xE, yE) + .L(xF, yF) + .Q(w / 4, yF + h / 12, 0, h) + .Z() + return { path: b.d() } + } case 'wedgeRoundRectCallout': { const r = ss * frac('adj3', 16667) const tipX = cx + w * adjRaw(adjust, 'adj1', -20833) diff --git a/packages/pptx-render/src/render-tree.ts b/packages/pptx-render/src/render-tree.ts index 1b5869e..4d973d6 100644 --- a/packages/pptx-render/src/render-tree.ts +++ b/packages/pptx-render/src/render-tree.ts @@ -294,6 +294,8 @@ export interface ShapeRenderNode extends RenderNodeBase { fill: RenderFill /** : second fill drawn over the base with multiply blending */ fillOverlay?: RenderFill + /** Soft-edge feather radius (px) */ + softEdgePx?: number stroke?: RenderStroke shadow?: RenderShadow glow?: RenderGlow diff --git a/packages/pptx-render/tests/preset-geometry.test.ts b/packages/pptx-render/tests/preset-geometry.test.ts index 2ee7102..e688a92 100644 --- a/packages/pptx-render/tests/preset-geometry.test.ts +++ b/packages/pptx-render/tests/preset-geometry.test.ts @@ -364,3 +364,22 @@ describe('arrow callouts (tdf150789 SmartArt section arrows)', () => { expect(pts[9]).toBe(100) }) }) + +describe('swooshArrow (prod SmartArt rising-arrow diagrams)', () => { + it('returns a curved arrow path, not a rect fallback', () => { + const r = presetPath('swooshArrow', 788, 358, { adj1: 25000, adj2: 25000 }) + expect(r).toBeTruthy() + // Body sweep = two quad beziers; arrowhead = straight segments + expect((r!.path!.match(/Q /g) ?? []).length).toBe(2) + // Starts at bottom-left corner + expect(r!.path!.startsWith('M 0 358')).toBe(true) + // Arrowhead tip on the right edge, in the upper part (yD = yE/2 + h/20) + expect(r!.path).toContain('L 788 ') + }) + + it('clamps adj2 to maxAdj2 = 70000*w/ss', () => { + const r = presetPath('swooshArrow', 100, 100, { adj1: 25000, adj2: 999999 }) + // ad2 capped at 70% of ss → xB = w - 70 + expect(r!.path).toContain('Q 16.67 33.33 30 12.5') + }) +}) diff --git a/tools/fidelity-compare.mjs b/tools/fidelity-compare.mjs index f740f6f..1b1dc2d 100644 --- a/tools/fidelity-compare.mjs +++ b/tools/fidelity-compare.mjs @@ -119,8 +119,6 @@ function exportPdfViaPowerPoint(pptx, outPdf) { fs.mkdirSync(box, { recursive: true }) const src = path.join(box, 'in.pptx') const pdf = path.join(box, 'out.pdf') - fs.copyFileSync(pptx, src) - fs.rmSync(pdf, { force: true }) // Stale Office owner-lock files (~$in.pptx, left by a killed save) make every // subsequent open fail with -9074 until removed for (const f of fs.readdirSync(path.dirname(src))) { @@ -133,19 +131,39 @@ function exportPdfViaPowerPoint(pptx, outPdf) { save p in (POSIX file "${pdf}") as save as PDF close p saving no end tell` - try { - execFileSync('osascript', ['-e', script], { stdio: 'pipe', timeout: pptTimeout }) - } catch (e) { + const killPpt = () => { try { execFileSync('pkill', ['-x', 'Microsoft PowerPoint'], { stdio: 'ignore' }) } catch { /* no process to kill */ } - throw new Error('PowerPoint export failed: ' + (e.message ?? e), { cause: e }) - } finally { - fs.rmSync(src, { force: true }) } - if (!fs.existsSync(pdf)) throw new Error('PowerPoint produced no pdf') + const attempt = () => { + fs.copyFileSync(pptx, src) + fs.rmSync(pdf, { force: true }) + try { + execFileSync('osascript', ['-e', script], { stdio: 'pipe', timeout: pptTimeout }) + } catch (e) { + throw new Error('PowerPoint export failed: ' + (e.message ?? e), { cause: e }) + } finally { + fs.rmSync(src, { force: true }) + } + // A poison deck can leave PowerPoint alive but window-less; every later open + // then fails with -9074, so any failure mode must kill before the next deck. + if (!fs.existsSync(pdf)) throw new Error('PowerPoint produced no pdf') + } + try { + attempt() + } catch { + killPpt() + execFileSync('sleep', ['3']) + try { + attempt() + } catch (e) { + killPpt() + throw e + } + } fs.renameSync(pdf, outPdf) } diff --git a/tools/gen-third-party-notices.mjs b/tools/gen-third-party-notices.mjs index dfb368f..eb2f490 100644 --- a/tools/gen-third-party-notices.mjs +++ b/tools/gen-third-party-notices.mjs @@ -322,14 +322,19 @@ const FONTS = [ '© Adobe / Google. This bundle ships a subset of the original fonts (reduced glyph coverage for size);\nno other modifications were made.', ], [ - 'GenOffice Sans KR / GenOffice Serif KR (Noto Sans/Serif CJK KR derivative)', + 'GenOffice Sans KR (Noto Sans CJK KR derivative)', 'SIL OFL 1.1', - '© Adobe / Google, original Reserved Font Name "Noto". Subset with modified advance widths\nand horizontally transformed Noto outlines to match measured Korean Office-family metrics;\nrenamed per OFL 1.1. No Microsoft font outlines are included.', + 'Copyright 2014-2021 Adobe (http://www.adobe.com/), Google LLC, Reserved Font Name "Source".\nSubset with modified advance widths and horizontally transformed Noto CJK outlines to match measured\nKorean Office-family metrics; renamed per OFL 1.1. No Microsoft font outlines are included.', + ], + [ + 'GenOffice Serif KR (Noto Serif CJK KR derivative)', + 'SIL OFL 1.1', + 'Copyright 2017-2024 Adobe (http://www.adobe.com/), Reserved Font Name "Source".\nSubset with modified advance widths and horizontally transformed Noto CJK outlines to match measured\nKorean Office-family metrics; renamed per OFL 1.1. No Microsoft font outlines are included.', ], [ 'GenOffice Che Latin KR (Noto Sans CJK KR derivative)', 'SIL OFL 1.1', - '© Adobe / Google, original Reserved Font Name "Noto". ASCII subset with fixed 0.5em advances\nand horizontally transformed Noto outlines; Microsoft DotumChe is used only as a metric reference.\nRenamed per OFL 1.1. No Microsoft font outlines are included.', + 'Copyright 2014-2021 Adobe (http://www.adobe.com/), Google LLC, Reserved Font Name "Source".\nASCII subset with fixed 0.5em advances and horizontally transformed Noto CJK outlines; Microsoft\nDotumChe is used only as a metric reference. Renamed per OFL 1.1. No Microsoft outlines are included.', ], [ 'Noto Naskh Arabic / Noto Sans Arabic (subset)',