docxkeun 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -140,7 +140,7 @@ export function diffParagraphs(before, after) {
140
140
  for (let i = k; i < match; i++)
141
141
  deleted.push({ at: para, text: oldGap[i] });
142
142
  const d = diffText(oldGap[match], text);
143
- changes.push({ para, kind: 'changed', ranges: d.ranges, cuts: d.cuts, before: oldGap[match] });
143
+ changes.push({ para, kind: 'changed', ranges: d.ranges, cuts: d.cuts, before: oldGap[match], from: p + ai + match });
144
144
  k = match + 1;
145
145
  }
146
146
  else {
@@ -173,17 +173,41 @@ function still(prefix) {
173
173
  `${prefix}.docxkeun-ai-caret{transition:none;animation:none}`,
174
174
  `${prefix}.docxkeun-ai-ghost,${prefix}.docxkeun-ai-cut{animation:docxkeun-ai-fade 2.4s both}`,
175
175
  `${prefix}.docxkeun-ai-bar,${prefix}.docxkeun-ai-spark{animation:none}`,
176
+ // The other working styles, held still.
177
+ `${prefix}.docxkeun-ai-words{animation:none;background-image:linear-gradient(90deg,var(--dk-ai-1),var(--dk-ai-2))}`,
178
+ `${prefix}.docxkeun-ai-underline{animation:none;background-image:linear-gradient(90deg,var(--dk-ai-1),var(--dk-ai-2));background-size:100% 100%}`,
179
+ `${prefix}.docxkeun-ai-edge{animation:none;box-shadow:0 0 0 1.5px color-mix(in srgb,var(--dk-ai-1) 70%,transparent),0 0 10px color-mix(in srgb,var(--dk-ai-1) 30%,transparent)}`,
180
+ `${prefix}.docxkeun-ai-spot,${prefix}.docxkeun-ai-pulse,${prefix}.docxkeun-ai-intro{animation:none}`,
181
+ `${prefix}.docxkeun-ai-sparkle{animation:none;opacity:.75}`,
176
182
  ].join('\n');
177
183
  }
178
184
  const CSS = `
179
185
  .docxkeun-viewer{--dk-ai-1:var(--docxkeun-ai-color-1,#7c5cff);--dk-ai-2:var(--docxkeun-ai-color-2,var(--docxkeun-ai-color-1,#d97757));--dk-ai-text:var(--docxkeun-ai-text,#fff);--docxkeun-ai-page:#fff}
180
- .docxkeun-ai-layer{position:absolute;left:0;top:0;width:0;height:0;pointer-events:none;z-index:5}
186
+ .docxkeun-ai-layer{position:absolute;left:0;top:0;width:0;height:0;pointer-events:none}
181
187
  .docxkeun-ai-layer>*{position:absolute;pointer-events:none}
188
+ .docxkeun-ai-words{border-radius:2px;background-image:linear-gradient(100deg,#000 30%,var(--dk-ai-1) 44%,var(--dk-ai-2) 56%,#000 70%);background-size:300% 100%;mix-blend-mode:lighten;animation:docxkeun-ai-shimmer 1.8s linear infinite}
189
+ .docxkeun-ai-underline{border-radius:1px;background-color:color-mix(in srgb,var(--dk-ai-1) 18%,transparent);background-image:linear-gradient(90deg,transparent,var(--dk-ai-1),var(--dk-ai-2),transparent);background-size:35% 100%;background-repeat:no-repeat;animation:docxkeun-ai-wave 1.4s ease-in-out infinite}
190
+ @keyframes docxkeun-ai-wave{from{background-position:-40% 0}to{background-position:140% 0}}
191
+ .docxkeun-ai-edge{border-radius:6px;animation:docxkeun-ai-aura 2.2s ease-in-out infinite}
192
+ @keyframes docxkeun-ai-aura{0%,100%{box-shadow:0 0 0 1.5px color-mix(in srgb,var(--dk-ai-1) 70%,transparent),0 0 12px color-mix(in srgb,var(--dk-ai-1) 35%,transparent)}50%{box-shadow:0 0 0 1.5px color-mix(in srgb,var(--dk-ai-2) 70%,transparent),0 0 16px color-mix(in srgb,var(--dk-ai-2) 40%,transparent)}}
193
+ .docxkeun-ai-spot{border-radius:4px;box-shadow:0 0 0 4000px rgba(255,255,255,.55);animation:docxkeun-ai-in .4s ease-out both}
194
+ .docxkeun-ai-pulse{width:3px;border-radius:2px;background:linear-gradient(var(--dk-ai-1),var(--dk-ai-2));animation:docxkeun-ai-pulse 1.2s ease-in-out infinite}
195
+ @keyframes docxkeun-ai-pulse{0%,100%{opacity:.35;transform:scaleY(.85)}50%{opacity:1;transform:none}}
196
+ .docxkeun-ai-sparkle{width:12px;height:12px;margin:-6px 0 0 -6px;font:11px/12px system-ui,sans-serif;text-align:center;animation:docxkeun-ai-float 2.4s ease-in-out var(--t) infinite both}
197
+ @keyframes docxkeun-ai-float{0%{opacity:0;transform:translateY(4px) scale(.4) rotate(-40deg)}30%{opacity:1;transform:translateY(-2px) scale(1)}to{opacity:0;transform:translateY(-14px) scale(.6) rotate(40deg)}}
198
+ .docxkeun-ai-mark-tint{border-radius:3px;background:linear-gradient(90deg,color-mix(in srgb,var(--dk-ai-1) 14%,transparent),color-mix(in srgb,var(--dk-ai-2) 14%,transparent));mix-blend-mode:multiply}
199
+ .docxkeun-ai-mark-halo{border-radius:6px;background:linear-gradient(90deg,color-mix(in srgb,var(--dk-ai-1) 24%,transparent),color-mix(in srgb,var(--dk-ai-2) 24%,transparent));filter:blur(4px);mix-blend-mode:multiply}
200
+ .docxkeun-ai-mark-words{border-radius:2px;background:linear-gradient(90deg,var(--dk-ai-1),var(--dk-ai-2));mix-blend-mode:lighten}
201
+ .docxkeun-ai-mark-underline{border-radius:1px;background:linear-gradient(90deg,var(--dk-ai-1),var(--dk-ai-2))}
202
+ .docxkeun-ai-intro{animation:docxkeun-ai-in .9s ease-out var(--t,0ms) both}
203
+ .docxkeun-ai-mark-underline.docxkeun-ai-intro{transform-origin:left;animation-name:docxkeun-ai-draw}
204
+ @keyframes docxkeun-ai-in{from{opacity:0}}
205
+ @keyframes docxkeun-ai-draw{from{transform:scaleX(0)}}
182
206
  .docxkeun-ai-shimmer{border-radius:3px;background-color:color-mix(in srgb,var(--dk-ai-1) 7%,transparent);background-image:linear-gradient(100deg,color-mix(in srgb,var(--dk-ai-1) 0%,transparent) 25%,color-mix(in srgb,var(--dk-ai-1) 30%,transparent) 42%,color-mix(in srgb,var(--dk-ai-2) 30%,transparent) 58%,color-mix(in srgb,var(--dk-ai-2) 0%,transparent) 75%);background-size:300% 100%;mix-blend-mode:multiply;animation:docxkeun-ai-shimmer 1.6s ease-in-out infinite}
183
207
  @keyframes docxkeun-ai-shimmer{from{background-position:100% 0}to{background-position:0 0}}
184
208
  .docxkeun-ai-scan{height:46px;background:linear-gradient(180deg,color-mix(in srgb,var(--dk-ai-1) 0%,transparent),color-mix(in srgb,var(--dk-ai-1) 16%,transparent) 60%,color-mix(in srgb,var(--dk-ai-2) 45%,transparent) 96%,color-mix(in srgb,var(--dk-ai-2) 0%,transparent));mix-blend-mode:multiply;animation:docxkeun-ai-scan 1.3s cubic-bezier(.4,0,.2,1) forwards}
185
209
  @keyframes docxkeun-ai-scan{from{transform:translateY(0);opacity:0}12%{opacity:1}85%{opacity:1}to{transform:translateY(var(--travel));opacity:0}}
186
- .docxkeun-ai-caret{width:2px;border-radius:1px;background:linear-gradient(var(--dk-ai-1),var(--dk-ai-2));transition:left .45s cubic-bezier(.3,.7,.2,1),top .45s cubic-bezier(.3,.7,.2,1),height .2s,opacity .4s;box-shadow:0 0 6px color-mix(in srgb,var(--dk-ai-1) 60%,transparent)}
210
+ .docxkeun-ai-caret{z-index:2;width:2px;border-radius:1px;background:linear-gradient(var(--dk-ai-1),var(--dk-ai-2));transition:left .45s cubic-bezier(.3,.7,.2,1),top .45s cubic-bezier(.3,.7,.2,1),height .2s,opacity .4s;box-shadow:0 0 6px color-mix(in srgb,var(--dk-ai-1) 60%,transparent)}
187
211
  .docxkeun-ai-caret.working{animation:docxkeun-ai-blink 1s steps(2,jump-none) infinite}
188
212
  @keyframes docxkeun-ai-blink{50%{opacity:.35}}
189
213
  .docxkeun-ai-caret span{position:absolute;left:0;bottom:100%;margin-bottom:2px;white-space:nowrap;font:600 11px/1 system-ui,sans-serif;color:var(--dk-ai-text);padding:3px 6px;border-radius:4px 4px 4px 0;background:linear-gradient(90deg,var(--dk-ai-1),var(--dk-ai-2));box-shadow:0 1px 3px rgba(0,0,0,.2)}
@@ -261,13 +285,43 @@ function labelText(colors, host) {
261
285
  const onDark = Math.min(...lums.map((l) => (l + 0.05) / (darkL + 0.05)));
262
286
  return onWhite < 3 && onDark > onWhite ? DARK_TEXT : undefined;
263
287
  }
288
+ /** The bounding box of some lines on each page they are on. */
289
+ function boxesByPage(lines) {
290
+ const byPage = new Map();
291
+ for (const l of lines)
292
+ byPage.set(l.page, [...(byPage.get(l.page) ?? []), l]);
293
+ return [...byPage].map(([page, ls]) => {
294
+ const x = Math.min(...ls.map((l) => l.x));
295
+ const y = Math.min(...ls.map((l) => l.y));
296
+ return { page, x, y, w: Math.max(...ls.map((l) => l.x + l.w)) - x, h: Math.max(...ls.map((l) => l.y + l.h)) - y };
297
+ });
298
+ }
299
+ /** A fixed, even-looking spread in [0.1, 0.9) for the i-th item (no randomness: redraws match). */
300
+ function spread(i, axis) {
301
+ const golden = axis === 1 ? 0.6180339887 : 0.7548776662;
302
+ return 0.1 + (((i + 1) * golden) % 1) * 0.8;
303
+ }
264
304
  const reducedMotion = () => typeof matchMedia !== 'undefined' && matchMedia('(prefers-reduced-motion: reduce)').matches;
265
305
  const nextFrame = () => new Promise((ok) => requestAnimationFrame(() => requestAnimationFrame(() => ok())));
306
+ /** The styles, with a label and a line to show in a picker, in the order to show them. */
307
+ export const ACTIVITY_STYLES = [
308
+ { style: 'highlight', label: 'Highlight band', description: 'A tinted band sweeps over the lines; the changed paragraph keeps a soft tint.' },
309
+ { style: 'words', label: 'Word shimmer', description: 'The letters themselves shimmer; the changed words keep a glow.' },
310
+ { style: 'underline', label: 'Underline wave', description: 'A streak runs under each line; the changed words stay underlined.' },
311
+ { style: 'edge', label: 'Edge glow', description: 'A soft glow breathes around the paragraph.' },
312
+ { style: 'spotlight', label: 'Spotlight', description: 'The rest of the page dims while the paragraph is changed.' },
313
+ { style: 'quiet', label: 'Quiet', description: 'Only the named caret and a pulsing bar in the margin.' },
314
+ { style: 'sparkles', label: 'Sparkles', description: 'Small ✦ drift over the paragraph.' },
315
+ ];
316
+ /** Styles whose own mark stays on the changed text; the others leave the ✦ margin bar. */
317
+ const OWN_MARK = new Set(['highlight', 'words', 'underline']);
318
+ const isStyle = (value) => ACTIVITY_STYLES.some((s) => s.style === value);
266
319
  /** The AI's presence on the pages: see the module comment. Get it from `viewer.activity`. */
267
320
  export class AiActivity {
268
321
  viewer;
269
322
  aiName;
270
323
  aiColor;
324
+ aiStyle = 'highlight';
271
325
  layer;
272
326
  map;
273
327
  caret;
@@ -305,6 +359,8 @@ export class AiActivity {
305
359
  this.layer.appendChild(this.caret);
306
360
  if (options.color !== undefined)
307
361
  this.color = options.color;
362
+ if (options.style !== undefined)
363
+ this.style = options.style;
308
364
  // Positions depend on the layout and zoom: redraw what lasts.
309
365
  this.cleanup.push(viewer.on('layout', () => requestAnimationFrame(() => this.redraw())));
310
366
  const onScroll = () => this.drawMap();
@@ -321,7 +377,7 @@ export class AiActivity {
321
377
  this.label.textContent = this.working ? this.workingText(this.working) : name;
322
378
  // The change marks' tooltips name the AI too.
323
379
  if (this.marks.length)
324
- this.drawMarks(false);
380
+ this.drawMarks();
325
381
  }
326
382
  /**
327
383
  * Colour of the caret, shimmer, glow and change marks: one CSS colour, or two for a gradient.
@@ -354,6 +410,22 @@ export class AiActivity {
354
410
  else
355
411
  style.removeProperty('--docxkeun-ai-text');
356
412
  }
413
+ /**
414
+ * How the AI's work shows, and so which mark stays after a change (see `ActivityOptions.style`).
415
+ * Changing it redraws the work in progress and the marks. Throws a TypeError for an unknown style.
416
+ */
417
+ get style() {
418
+ return this.aiStyle;
419
+ }
420
+ set style(style) {
421
+ if (!isStyle(style))
422
+ throw new TypeError(`Unknown AI effect style "${String(style)}" (use ${ACTIVITY_STYLES.map((s) => s.style).join(', ')})`);
423
+ this.aiStyle = style;
424
+ if (this.working)
425
+ this.drawWorking(false);
426
+ if (this.marks.length)
427
+ this.drawMarks();
428
+ }
357
429
  /** Show the AI's activity at all. Turning it off clears everything on screen. */
358
430
  get enabled() {
359
431
  return this.on;
@@ -386,7 +458,7 @@ export class AiActivity {
386
458
  }
387
459
  set marksShown(on) {
388
460
  this.keepMarks = on;
389
- this.drawMarks(false);
461
+ this.drawMarks();
390
462
  }
391
463
  // ---- geometry ----
392
464
  paragraphs() {
@@ -528,22 +600,10 @@ export class AiActivity {
528
600
  if (!w)
529
601
  return;
530
602
  const all = this.paragraphs();
531
- let first;
532
- let line = 0;
533
- for (const n of w.paras) {
534
- const para = all[n - 1];
535
- if (!para)
536
- continue;
537
- for (const r of this.lineRects(para.el)) {
538
- first ??= r;
539
- const el = this.add('docxkeun-ai-shimmer', r, 1.5);
540
- if (el) {
541
- // Ripple down the paragraph.
542
- el.style.animationDelay = `${(line++ % 12) * 70}ms`;
543
- this.shimmers.push(el);
544
- }
545
- }
546
- }
603
+ // The lines of each paragraph being worked on.
604
+ const groups = w.paras.map((n) => all[n - 1]).filter((p) => !!p).map((p) => this.lineRects(p.el));
605
+ const first = groups.flat()[0];
606
+ this.drawStyle(groups, first);
547
607
  this.label.textContent = this.workingText(w);
548
608
  if (first) {
549
609
  this.moveCaret(first.page, first.x, first.y, first.h, true);
@@ -553,6 +613,69 @@ export class AiActivity {
553
613
  else if (w.mode === 'read')
554
614
  this.parkCaret();
555
615
  }
616
+ /** The working effect of the current style over each paragraph's lines. */
617
+ drawStyle(groups, first) {
618
+ const keep = (el) => {
619
+ if (el)
620
+ this.shimmers.push(el);
621
+ return el;
622
+ };
623
+ // Ripple down the lines.
624
+ let line = 0;
625
+ const ripple = (el) => {
626
+ if (el)
627
+ el.style.animationDelay = `${(line++ % 12) * 70}ms`;
628
+ };
629
+ const lines = groups.flat();
630
+ switch (this.aiStyle) {
631
+ case 'highlight':
632
+ for (const r of lines)
633
+ ripple(keep(this.add('docxkeun-ai-shimmer', r, 1.5)));
634
+ break;
635
+ case 'words':
636
+ // A band blended with "lighten": white stays white, only the letters take its colours.
637
+ for (const r of lines)
638
+ ripple(keep(this.add('docxkeun-ai-words', r, 0.5)));
639
+ break;
640
+ case 'underline':
641
+ for (const r of lines)
642
+ ripple(keep(this.add('docxkeun-ai-underline', { ...r, y: r.y + r.h - 0.4, h: 1.6 })));
643
+ break;
644
+ case 'edge':
645
+ for (const g of groups)
646
+ for (const box of boxesByPage(g))
647
+ keep(this.add('docxkeun-ai-edge', box, 3));
648
+ break;
649
+ case 'spotlight': {
650
+ // One clear window around the lines on the caret's page; the rest of the view dims.
651
+ const box = first ? boxesByPage(lines).find((b) => b.page === first.page) : undefined;
652
+ if (box)
653
+ keep(this.add('docxkeun-ai-spot', box, 4));
654
+ break;
655
+ }
656
+ case 'quiet':
657
+ for (const g of groups) {
658
+ for (const box of boxesByPage(g))
659
+ keep(this.add('docxkeun-ai-pulse', { page: box.page, x: Math.max(4, box.page.content.x - 12), y: box.y, w: 3, h: box.h }));
660
+ }
661
+ break;
662
+ case 'sparkles': {
663
+ let i = 0;
664
+ for (const r of lines) {
665
+ for (let k = 0, n = Math.min(6, Math.max(1, Math.round(r.w / 70))); k < n && i < 40; k++, i++) {
666
+ const el = keep(this.add('docxkeun-ai-sparkle', { page: r.page, x: r.x + spread(i, 1) * r.w, y: r.y + spread(i, 2) * r.h, w: 0, h: 0 }));
667
+ if (!el)
668
+ continue;
669
+ el.textContent = '✦';
670
+ el.style.width = el.style.height = '12px';
671
+ el.style.color = i % 2 ? 'var(--dk-ai-1)' : 'var(--dk-ai-2)';
672
+ el.style.setProperty('--t', `${(i * 370) % 2400}ms`);
673
+ }
674
+ }
675
+ break;
676
+ }
677
+ }
678
+ }
556
679
  workingText(w) {
557
680
  const verb = w.label ?? (w.mode === 'read' ? 'reading' : 'editing');
558
681
  return `${this.aiName} is ${verb}…`;
@@ -622,7 +745,7 @@ export class AiActivity {
622
745
  if (old.formats.length) {
623
746
  for (const [o, n] of diff.same) {
624
747
  if (old.formats[o] !== undefined && old.formats[o] !== formatSignature(paras[n]))
625
- diff.changes.push({ para: n, kind: 'changed', ranges: [], cuts: [], formatting: true });
748
+ diff.changes.push({ para: n, kind: 'changed', ranges: [], cuts: [], formatting: true, before: old.texts[o], from: o });
626
749
  }
627
750
  diff.changes.sort((x, y) => x.para - y.para);
628
751
  }
@@ -632,8 +755,11 @@ export class AiActivity {
632
755
  for (const el of this.shimmers)
633
756
  el.remove();
634
757
  this.shimmers = [];
635
- this.marks = diff.changes.map((c) => ({ para: c.para, ...(c.before !== undefined ? { before: c.before } : {}), ...(c.formatting ? { formatting: true } : {}) }));
636
- this.drawMarks(true);
758
+ // The marks of earlier changes stay (following their paragraphs); this change's join them.
759
+ this.marks = this.mergeMarks(diff, after);
760
+ this.drawMarks(new Set(diff.changes.map((c) => c.para)));
761
+ // A style with its own mark lets the mark fade in; the others get the passing glow.
762
+ const glow = !(OWN_MARK.has(this.aiStyle) && this.keepMarks);
637
763
  // Ink in the new text, line after line, as if written (about 2.5 s at most).
638
764
  const pieces = [];
639
765
  for (const c of diff.changes) {
@@ -653,7 +779,8 @@ export class AiActivity {
653
779
  for (const { r } of pieces) {
654
780
  const d = Math.min(900, Math.max(140, (r.w / speed) * 1000));
655
781
  const vars = { '--d': `${d}ms`, '--t': `${t}ms`, '--w': `${r.w * this.viewer.scale}px` };
656
- this.add('docxkeun-ai-glow', r, 1, vars, t + d + 2600);
782
+ if (glow)
783
+ this.add('docxkeun-ai-glow', r, 1, vars, t + d + 2600);
657
784
  this.add('docxkeun-ai-ink', r, 0.5, vars, t + d);
658
785
  const pen = this.add('docxkeun-ai-pen', { ...r, w: 0, h: 0, y: r.y + r.h / 2 }, 0, vars, t + d);
659
786
  if (pen)
@@ -703,21 +830,100 @@ export class AiActivity {
703
830
  this.hideTimer = setTimeout(() => (this.caret.style.opacity = '0'), Math.min(t, 2400) + 2500);
704
831
  return diff;
705
832
  }
706
- /** Remove the change bars and the change map. */
833
+ /**
834
+ * Remove every mark and the change map. Marks build up over a run of changes (each follows its
835
+ * paragraph as others are added or removed) until this is called, e.g. when a new request starts.
836
+ */
707
837
  clearMarks() {
708
838
  this.marks = [];
709
- this.drawMarks(false);
839
+ this.drawMarks();
710
840
  }
711
- drawMarks(animate) {
841
+ /**
842
+ * The marks after a change: earlier marks follow their paragraphs (and go with deleted ones), a
843
+ * paragraph changed again is marked against its text before the first change, and new ones join.
844
+ */
845
+ mergeMarks(diff, after) {
846
+ const earlier = new Map(this.marks.map((m) => [m.para, m]));
847
+ const out = new Map();
848
+ for (const [o, n] of diff.same) {
849
+ const m = earlier.get(o);
850
+ if (m)
851
+ out.set(n, { ...m, para: n });
852
+ }
853
+ for (const c of diff.changes) {
854
+ const prev = c.from !== undefined ? earlier.get(c.from) : undefined;
855
+ if (!prev) {
856
+ out.set(c.para, { para: c.para, ranges: c.ranges, ...(c.before !== undefined ? { before: c.before } : {}), ...(c.formatting ? { formatting: true } : {}) });
857
+ continue;
858
+ }
859
+ const text = after[c.para] ?? '';
860
+ const formatting = !!(prev.formatting || c.formatting);
861
+ // A paragraph the AI added stays added; otherwise compare with the text before the first change.
862
+ const d = prev.before === undefined ? { ranges: text ? [[0, text.length]] : [], cuts: [] } : diffText(prev.before, text);
863
+ if (!d.ranges.length && !d.cuts.length && !formatting) {
864
+ // Changed back to how it was: no mark.
865
+ out.delete(c.para);
866
+ continue;
867
+ }
868
+ out.set(c.para, { para: c.para, ranges: d.ranges, ...(prev.before !== undefined ? { before: prev.before } : {}), ...(formatting ? { formatting: true } : {}) });
869
+ }
870
+ return [...out.values()].sort((a, b) => a.para - b.para);
871
+ }
872
+ /** What a mark's tooltip says. */
873
+ markTitle(m) {
874
+ if (m.formatting && !m.ranges.length)
875
+ return `Formatting changed by ${this.name}`;
876
+ if (m.before !== undefined)
877
+ return `Changed by ${this.name}. Before: ${m.before.replace(//g, '').slice(0, 300) || '(empty)'}`;
878
+ return `Added by ${this.name}`;
879
+ }
880
+ /** Draw the marks; those of the paragraphs in `fresh` (just changed) appear with a short animation. */
881
+ drawMarks(fresh = new Set()) {
712
882
  for (const el of this.markEls)
713
883
  el.remove();
714
884
  this.markEls = [];
715
885
  const paras = this.paragraphs();
886
+ let n = 0;
887
+ let animate = false;
888
+ // Marks of the style's own kind fade (or draw) in one after another.
889
+ const intro = (el) => {
890
+ if (!el)
891
+ return;
892
+ if (animate) {
893
+ el.classList.add('docxkeun-ai-intro');
894
+ el.style.setProperty('--t', `${Math.min(n++ * 90, 1500)}ms`);
895
+ }
896
+ this.markEls.push(el);
897
+ };
716
898
  for (const m of this.keepMarks ? this.marks : []) {
899
+ animate = fresh.has(m.para);
717
900
  const para = paras[m.para];
718
901
  if (!para)
719
902
  continue;
720
903
  const lines = this.lineRects(para.el);
904
+ const style = this.aiStyle;
905
+ if (style === 'highlight') {
906
+ // The changed paragraph keeps a still tint.
907
+ for (const r of lines)
908
+ intro(this.add('docxkeun-ai-mark-tint', r, 1.5));
909
+ continue;
910
+ }
911
+ if (style === 'words' || style === 'underline') {
912
+ // The changed words; the whole paragraph when only its formatting changed.
913
+ const rects = m.ranges.length ? m.ranges.flatMap(([from, to]) => this.charRects(para, from, to)) : m.formatting ? lines : [];
914
+ if (rects.length) {
915
+ for (const r of rects) {
916
+ if (style === 'words') {
917
+ intro(this.add('docxkeun-ai-mark-halo', r, 2));
918
+ intro(this.add('docxkeun-ai-mark-words', r, 0.5));
919
+ }
920
+ else
921
+ intro(this.add('docxkeun-ai-mark-underline', { ...r, y: r.y + r.h - 0.2, h: 1.4 }));
922
+ }
923
+ continue;
924
+ }
925
+ // Only deletions in this paragraph: nothing to colour, so the margin bar marks it.
926
+ }
721
927
  // One bar per page the paragraph is on, in the left margin.
722
928
  const byPage = new Map();
723
929
  for (const l of lines)
@@ -732,11 +938,7 @@ export class AiActivity {
732
938
  continue;
733
939
  if (!animate)
734
940
  bar.style.animation = 'none';
735
- bar.title = m.formatting
736
- ? `Formatting changed by ${this.name}`
737
- : m.before !== undefined
738
- ? `Changed by ${this.name}. Before: ${m.before.replace(//g, '').slice(0, 300) || '(empty)'}`
739
- : `Added by ${this.name}`;
941
+ bar.title = this.markTitle(m);
740
942
  this.markEls.push(bar);
741
943
  if (firstBar && animate) {
742
944
  const spark = this.add('docxkeun-ai-spark', { page, x: x + 1.5, y: top, w: 0, h: 0 });
@@ -769,18 +971,25 @@ export class AiActivity {
769
971
  continue;
770
972
  const tick = document.createElement('i');
771
973
  tick.style.top = `${(p.top / height) * root.clientHeight}px`;
772
- tick.title = `Changed by ${this.name}`;
974
+ tick.title = this.markTitle(m);
773
975
  tick.addEventListener('click', () => root.scrollTo({ top: Math.max(0, p.top - root.clientHeight / 3), behavior: reducedMotion() ? 'auto' : 'smooth' }));
774
976
  this.map.appendChild(tick);
775
977
  }
776
978
  }
979
+ /**
980
+ * The viewer rebuilt its page rows (zoom, the comment column's width): redraw at the new positions.
981
+ * @internal
982
+ */
983
+ rowsBuilt() {
984
+ requestAnimationFrame(() => this.redraw());
985
+ }
777
986
  redraw() {
778
987
  // Transient animations are tied to the old positions: drop them; redraw what lasts.
779
988
  for (const el of [...this.layer.children]) {
780
989
  if (el !== this.caret && !this.markEls.includes(el) && !this.shimmers.includes(el))
781
990
  el.remove();
782
991
  }
783
- this.drawMarks(false);
992
+ this.drawMarks();
784
993
  if (this.working)
785
994
  this.drawWorking(false);
786
995
  }
@@ -14,4 +14,4 @@ export { ToolbarController, FormatToolbar, type ToolbarOptions } from './toolbar
14
14
  export { InlineEditing, type CaretFormat, type EditStory, type EditingOptions, type Caret } from './editing.js';
15
15
  export { htmlToParagraphs, type PastedParagraph, type PastedRun } from './clipboard.js';
16
16
  export { selectionInfoFor, describeSelection, type SelectionInfo, type SelectedParagraph, type TextSpot } from '../api/selection.js';
17
- export { AiActivity, diffParagraphs, diffText, type ParagraphDiff, type ParagraphChange, type ParagraphSnapshot, type ActivityOptions } from './activity.js';
17
+ export { AiActivity, ACTIVITY_STYLES, diffParagraphs, diffText, type ActivityStyle, type ParagraphDiff, type ParagraphChange, type ParagraphSnapshot, type ActivityOptions } from './activity.js';
@@ -13,4 +13,4 @@ export { ToolbarController, FormatToolbar } from './toolbar.js';
13
13
  export { InlineEditing } from './editing.js';
14
14
  export { htmlToParagraphs } from './clipboard.js';
15
15
  export { selectionInfoFor, describeSelection } from '../api/selection.js';
16
- export { AiActivity, diffParagraphs, diffText } from './activity.js';
16
+ export { AiActivity, ACTIVITY_STYLES, diffParagraphs, diffText } from './activity.js';
@@ -455,6 +455,8 @@ export class DocxViewer {
455
455
  });
456
456
  this.root.scrollTop = scrollRatio * this.root.scrollHeight;
457
457
  this.commentsCtl.rowsBuilt();
458
+ // The AI's marks and effects sit at page positions: follow the new rows (zoom, column width).
459
+ this.activityLayer?.rowsBuilt();
458
460
  }
459
461
  onIntersect(entries) {
460
462
  for (const entry of entries) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "docxkeun",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Read, render, edit and convert .docx files in the browser with zero runtime dependencies: a DOCX viewer and editor (toolbar, comments, tracked changes, undo), a DOCX-to-PDF converter, and live effects for AI edits.",
5
5
  "author": "Kevin Jeremia (https://github.com/kevinkjs)",
6
6
  "license": "MIT",