Sync snapshot (2026-08-26) (#159)

- Sheets: CSV export and save support, Excel-matching data validation
- Docs: image, shape, WordArt, and chart rendering fixes
- Slides: downloadable font catalog with a missing-font banner
- Slides: better AI text layout and a speaker-notes tool

Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
This commit is contained in:
merrick-2002
2026-08-26 17:34:55 +08:00
committed by GitHub
co-authored by GenOffice
parent 9711a4507c
commit 0a2c25d483
201 changed files with 16950 additions and 1271 deletions
+23 -9
View File
@@ -241,6 +241,19 @@ export function applyColorMods(hex: string, mods: XmlNode | undefined): string {
const lumOff = pct('a:lumOff')
const shade = pct('a:shade')
const tint = pct('a:tint')
// SmartArt colorful cycles rotate node colors via HSL offsets (hueOff 1/60000 deg, satOff 1/1000 %)
const hueOffV = asXmlNode(mods?.['a:hueOff'])['@_val']
const hueOff = hueOffV != null ? (parseInt(String(hueOffV), 10) || 0) / 60000 : undefined
const satOff = pct('a:satOff')
if ((hueOff != null && hueOff !== 0) || (satOff != null && satOff !== 0)) {
const { h, s, l } = rgbToHsl(r, g, b)
const h2 = (((h + (hueOff ?? 0) / 360) % 1) + 1) % 1
const s2 = Math.max(0, Math.min(1, s + (satOff ?? 0)))
const rgb2 = hslToRgb(h2, s2, l)
r = rgb2.r
g = rgb2.g
b = rgb2.b
}
// satMod (frequent in theme gradient templates): HSL saturation multiplier
const satMod = pct('a:satMod')
if (satMod != null) {
@@ -250,15 +263,16 @@ export function applyColorMods(hex: string, mods: XmlNode | undefined): string {
g = rgb2.g
b = rgb2.b
}
if (lumMod != null) {
r *= lumMod
g *= lumMod
b *= lumMod
}
if (lumOff != null) {
r += 255 * lumOff
g += 255 * lumOff
b += 255 * lumOff
// lumMod/lumOff scale/offset HSL luminance with saturation preserved (Office semantics:
// accent2 7F0000 + lumMod40/lumOff60 renders #FF6666, and 4472C4 + lumMod75 renders
// #2F5496 — both match the HSL formula exactly, while an RGB multiply/add desaturates)
if (lumMod != null || lumOff != null) {
const { h, s, l } = rgbToHsl(r, g, b)
const l2 = Math.max(0, Math.min(1, l * (lumMod ?? 1) + (lumOff ?? 0)))
const rgb2 = hslToRgb(h, s, l2)
r = rgb2.r
g = rgb2.g
b = rgb2.b
}
// tint/shade blend in linear-gamma space (PowerPoint semantics: black + 55% tint
// renders ≈#B1, not #73; verified against PowerPoint for Mac gradient stops)
+35 -8
View File
@@ -71,7 +71,9 @@ import { buildChartSpaceXml, type NewChartKind, type NewChartOptions } from './c
import { parseLayoutPlaceholders, placeholderSpXml, prepareInsertSlideWithLayout } from './layout'
import {
chooseLayout,
collectPart,
collectSlideBundle,
importClipboardParts,
importSourceLayout,
materializeSlideBundle,
type SlideBundle,
@@ -3601,6 +3603,10 @@ export interface ElementClipboardItem {
xml: string
/** rId referenced by the xml → absolute part path (External relationships keep the target verbatim) */
rels: Array<{ rid: string; type: string; target: string; external?: boolean }>
/** Dependency part bytes (base64, keyed by absolute path), so the paste target can be a different deck */
parts?: Readonly<Record<string, string>>
/** ContentType per dependency: keyed by lower-case extension or by part path */
contentTypes?: Readonly<Record<string, string>>
}
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<string, string> = {}
const contentTypes: Record<string, string> = {}
const seen = new Set<string>()
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<string, string> = {}
const bundledCT: Record<string, string> = {}
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(
'</Relationships>',
+2
View File
@@ -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']
// <a:fillOverlay> 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
+66 -2
View File
@@ -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<string, string>,
@@ -219,12 +219,17 @@ function writeParts(
archive: PackageArchive,
parts: Readonly<Record<string, string>>,
contentTypes: Readonly<Record<string, string>>,
reusable?: ReadonlySet<string>,
): Map<string, string> {
// rels parts follow their owner, so map the owners first
const pathMap = new Map<string, string>()
const taken = new Set<string>()
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<Record<string, string>>,
): Set<string> {
const memo = new Map<string, boolean>()
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<string>()
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<Record<string, string>>,
contentTypes: Readonly<Record<string, string>>,
): Map<string, string> {
return writeParts(archive, parts, contentTypes, reusableParts(archive, parts))
}
export function materializeSlideBundle(
archive: PackageArchive,
bundle: SlideBundle,
+2
View File
@@ -441,6 +441,8 @@ export interface TextElement extends ElementBase {
glow?: GlowEffect
reflection?: ReflectionEffect
scene3d?: Scene3D
/** Soft edges <a:softEdge rad> (EMU feather radius) */
softEdge?: number
text?: TextBody
}
@@ -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]!
@@ -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')
})
})