Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions e2e/docx-track.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,45 @@ describe('docx tracked changes', () => {
expect(show(ps.find((p) => show(p).startsWith('Hire'))!)).toBe('Hire the platform team')
expect(v.errors).toEqual([])
})

test('typed and struck out a second apart: one insertion and one deletion; struck-through text inserted keeps its line', async () => {
const v = await open('samples/sample.docx')
await v.btn('Track changes').click()
// the page's clock moves on a second at every reading: every key press is a second after the one before
const clock = (on: boolean) =>
v.page.evaluate((on) => {
const w = window as unknown as { Date: DateConstructor; __Date?: DateConstructor }
w.__Date ??= w.Date
const D = w.__Date
if (!on) return void (w.Date = D)
let t = D.now()
w.Date = class extends D {
constructor(...a: unknown[]) {
if (a.length) super(...(a as [number]))
else super((t += 1000))
}
static override now() {
return (t += 1000)
}
} as DateConstructor
}, on)
await v.selectText('Hire the platform team', 'end')
await clock(true)
await v.page.keyboard.type('xyz', { delay: 50 })
await v.selectText('Hire the platform', 'end')
await v.page.keyboard.press('Backspace')
await v.page.keyboard.press('Backspace')
await v.page.waitForTimeout(250)
await clock(false)
const ps = await docxParas((await v.save()).bytes)
expect(show(ps.find((p) => show(p).startsWith('Hire'))!)).toBe('Hire the platfo[-rm] team[+xyz]')

// the inserted text struck through (formatting is not tracked): both lines show, the change's and the text's own
await v.selectText('xyz')
await v.btn('Strikethrough').click()
const xyz = v.page.locator('.de-pages [data-tc="ins"]', { hasText: 'xyz' })
await v.page.waitForFunction(() => [...document.querySelectorAll('.de-pages [data-tc="ins"]')].some((el) => el.textContent === 'xyz' && getComputedStyle(el).textDecorationLine.includes('line-through')))
expect(await xyz.evaluate((el) => getComputedStyle(el).textDecorationLine)).toContain('underline')
expect(v.errors).toEqual([])
})
})
21 changes: 13 additions & 8 deletions src/react/docx/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,17 @@ export function authorColor(author: string | undefined): string {
return TRACK_COLORS[i % TRACK_COLORS.length]
}

/** inserted text underlined, deleted text struck through, in the author's colour */
function trackCss(tc: TrackMark): CSSProperties {
/**
* `run`'s style as a tracked change: inserted text underlined, deleted text struck through, in the author's colour,
* with the lines the text has of its own (struck-through text inserted is still struck through). One style for all
* the lines: solid, as the change's. (Longhands only, here and in `runCss`: React warns when a run's style changes
* and has both a shorthand and one of its longhands.)
*/
function trackCss(tc: TrackMark, run: CSSProperties): CSSProperties {
const color = authorColor(tc.author)
return tc.k === 'ins'
? { color, textDecoration: 'underline', textDecorationColor: color }
: { color, textDecoration: 'line-through', textDecorationColor: color }
const own = String(run.textDecorationLine ?? '').split(' ').filter((x) => x && x !== 'none')
const line = tc.k === 'ins' ? 'underline' : 'line-through'
return { ...run, color, textDecorationLine: [...new Set([...own, line])].join(' '), textDecorationColor: color, textDecorationStyle: 'solid' }
}

export function runCss(s: RunStyle | undefined): CSSProperties {
Expand All @@ -68,8 +73,8 @@ export function runCss(s: RunStyle | undefined): CSSProperties {
else if (s.i === false) css.fontStyle = 'normal'
const u = s.u && s.u !== 'none'
const deco = [u ? 'underline' : '', s.strike || s.dstrike ? 'line-through' : ''].filter(Boolean).join(' ')
if (deco) css.textDecoration = deco
else if (s.u === 'none' || s.strike === false) css.textDecoration = 'none'
if (deco) css.textDecorationLine = deco
else if (s.u === 'none' || s.strike === false) css.textDecorationLine = 'none'
if (u && UNDERLINE_STYLE[s.u!]) css.textDecorationStyle = UNDERLINE_STYLE[s.u!]
if (s.dstrike) css.textDecorationStyle = 'double'
// colors of the model come from the file, a peer or the clipboard: `background` would load a url() in one
Expand Down Expand Up @@ -244,7 +249,7 @@ function InlineView({ x, ctx, o, pieces, tabs }: { x: Inline; ctx: RenderCtx; o:
const span = (
<span
data-o={o}
style={tc ? { ...runCss(x.s), ...trackCss(tc) } : runCss(x.s)}
style={tc ? trackCss(tc, runCss(x.s)) : runCss(x.s)}
data-cm={x.cm?.join(' ')}
data-tc={tc?.k}
title={tc ? tcTitle(ctx, tc, true) : undefined}
Expand Down