@sobree/core 0.1.24 → 0.1.25

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.
@@ -159,6 +159,20 @@ export declare class Editor {
159
159
  * that would clobber the caret, while still repainting on undo/remote.
160
160
  */
161
161
  private pendingLiveFrameEdit;
162
+ /**
163
+ * The editing context the caret was last in — a frame id, or `"body"`.
164
+ * When it changes, the undo-capture group is closed so each box's edit
165
+ * is a distinct undo step. `null` until the first selection.
166
+ */
167
+ private lastEditContext;
168
+ /**
169
+ * Selection captured at `beforeinput` (pre-DOM-mutation) for the open
170
+ * undo group — restored on undo so the caret lands where the edit began.
171
+ * `hasPendingPreEdit` distinguishes "nothing stashed" from a stashed
172
+ * `null` (a body `Selection` can legitimately be `null`).
173
+ */
174
+ private pendingPreEdit;
175
+ private hasPendingPreEdit;
162
176
  /**
163
177
  * Kernel seam handed to the behaviour modules (`ops/*`, `query`). Built
164
178
  * once in the constructor; closes over this instance's privates so the
@@ -487,6 +501,11 @@ export declare class Editor {
487
501
  * its body directly into it), so `serializeHostsToDocument([el])` yields
488
502
  * the same `Block[]` shape as a body host. Matched to the AST frame by
489
503
  * its stable `data-anchor-id`. Pure body swap — geometry/anchor untouched.
504
+ *
505
+ * `captureRunDefaults` promotes each paragraph's rendered base font to
506
+ * `runDefaults`, so a frame's text keeps its size/family even when a
507
+ * keystroke (or a select-all-retype) strips every run's inline styling —
508
+ * the heading no longer collapses to the default font on the next repaint.
490
509
  */
491
510
  private syncFramesFromDom;
492
511
  /**
@@ -495,6 +514,50 @@ export declare class Editor {
495
514
  * `input` event to the frame read-back instead of the body read-back.
496
515
  */
497
516
  private editedFrameId;
517
+ /** The editable textbox frame element the caret is inside, or null. */
518
+ private focusedFrameEl;
519
+ /** The (freshly-painted) frame element with this `data-anchor-id`, or null. */
520
+ private frameElById;
521
+ /**
522
+ * Capture the live selection for an undo step. A selection inside an
523
+ * editable textbox frame becomes a `FrameSelection` — the body
524
+ * `Selection` model is keyed on registry blocks and can't address frame
525
+ * content — so undo can restore it the same way it restores a body
526
+ * selection. Everything else is an ordinary body selection.
527
+ */
528
+ private captureSelectionForHistory;
529
+ /**
530
+ * The selection BEFORE the edit that opened the current undo group,
531
+ * stashed by `onBeforeInput` (which fires before the DOM mutates). Falls
532
+ * back to the live selection for edits that bypass `beforeinput`
533
+ * (programmatic mutations, `setDocument`).
534
+ */
535
+ private capturePreEditSelection;
536
+ /**
537
+ * Stash the pre-edit selection on the first input of a new undo group,
538
+ * so undo can land the caret where the edit began. `beforeinput` fires
539
+ * before the browser mutates the DOM, so the live selection here is the
540
+ * pre-edit position. Only the FIRST input of a group stashes; coalesced
541
+ * inputs leave the group's `before` intact (`History.onGroupSettled`
542
+ * clears the stash once a step has captured).
543
+ *
544
+ * Scoped to textbox frames. The body already restores its caret through
545
+ * the proven `applySelectionToDom` path on `stack-item-popped`; we leave
546
+ * its behaviour byte-for-byte unchanged (no pre-edit stash → `before`
547
+ * falls back to the post-edit selection, same as `after`). Frames had no
548
+ * working restore at all, so they get the full pre-/post-edit treatment.
549
+ */
550
+ private onBeforeInput;
551
+ /** Drop the pending pre-edit stash — a step has captured (or extended) it. */
552
+ private clearPendingPreEditSelection;
553
+ /**
554
+ * Restore an undo step's selection. Fires on `stack-item-popped`, which
555
+ * runs AFTER the change handler has already repainted the frame overlay
556
+ * (`adoptYDocState` calls `emitChangeNow` synchronously), so a captured
557
+ * frame selection lands on the fresh frame element and sticks — the same
558
+ * lifecycle the body selection restore relies on.
559
+ */
560
+ private restoreCapturedSelection;
498
561
  /**
499
562
  * Toggle a mark on the caret inside an editable textbox frame, natively
500
563
  * (`document.execCommand`), so the body-selection mark path doesn't have
@@ -546,6 +609,14 @@ export declare class Editor {
546
609
  * even when no subscribers exist (the early-return keeps it cheap).
547
610
  */
548
611
  private fireSelection;
612
+ /**
613
+ * When the caret moves to a different editing context — another textbox
614
+ * frame, or between a frame and the body — close the undo-capture group
615
+ * so the next edit there is its own undo step. Without this, two edits
616
+ * to different boxes within `captureTimeout` coalesce and a single undo
617
+ * reverts both, unlike Word (where each box is a distinct action).
618
+ */
619
+ private breakUndoOnContextChange;
549
620
  private fireKeyDown;
550
621
  }
551
622
  export { countBlocks };
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Selection <-> character-offset helpers for editable textbox frames.
3
+ *
4
+ * A frame's body is a contentEditable island whose text isn't addressable
5
+ * by the body's block-registry `Selection` model. Undo/redo still needs to
6
+ * capture the caret (or selection) on a frame edit and put it back, so we
7
+ * model a frame selection as a `{ start, end }` character span across the
8
+ * frame's text nodes — stable enough to survive the AST round-trip and
9
+ * clamp cleanly to the (possibly shorter) post-undo text. A collapsed
10
+ * caret is `start === end`.
11
+ */
12
+ export interface FrameOffsets {
13
+ /** Character offset of the selection start within the frame. */
14
+ start: number;
15
+ /** Character offset of the selection end (=== start for a caret). */
16
+ end: number;
17
+ }
18
+ /**
19
+ * The current selection's `{ start, end }` character offsets within `root`,
20
+ * or `null` when the selection isn't anchored inside `root`. Normalised so
21
+ * `start <= end` regardless of selection direction.
22
+ */
23
+ export declare function frameSelectionOffsets(root: HTMLElement, doc: Document): FrameOffsets | null;
24
+ /**
25
+ * Character offset of the collapsed caret within `root` (selection start),
26
+ * or `null` when the selection isn't inside `root`. Thin wrapper over
27
+ * {@link frameSelectionOffsets} for caret-only callers.
28
+ */
29
+ export declare function caretCharOffset(root: HTMLElement, doc: Document): number | null;
30
+ /**
31
+ * Select `start..end` characters within `root` (a collapsed caret when
32
+ * equal), clamping each end to the available text — an undo can revert to
33
+ * shorter text. No-op-safe when `root` has no text nodes.
34
+ */
35
+ export declare function applyFrameSelection(root: HTMLElement, offsets: FrameOffsets, doc: Document): void;
36
+ /** Place a collapsed caret `offset` chars into `root` (clamped). */
37
+ export declare function placeCaretAtOffset(root: HTMLElement, offset: number, doc: Document): void;
@@ -10,5 +10,20 @@ export interface BlockSerializeContext {
10
10
  numId: number;
11
11
  ordered: boolean;
12
12
  } | null;
13
+ /**
14
+ * Capture each paragraph's effective base run style (from the rendered
15
+ * `<p>`'s inline font) into `ParagraphProperties.runDefaults`.
16
+ *
17
+ * Used for textbox-frame read-back only. A frame's text carries its font
18
+ * on the runs, with no named style to fall back on; so when a keystroke
19
+ * lands in a bare text node — or a select-all-retype replaces every
20
+ * styled span with one unstyled node — the runs lose their font and a
21
+ * repaint renders the whole line at the default tiny size. The `<p>`
22
+ * element keeps its inline font through these DOM edits, so promoting it
23
+ * to a paragraph-level default makes the font survive run-level loss.
24
+ * Body flow leaves this off: its runs legitimately inherit from named
25
+ * styles, which must stay style-linked across edits.
26
+ */
27
+ captureRunDefaults?: boolean;
13
28
  }
14
29
  export declare function blocksFromNodes(nodes: readonly Node[], ctx: BlockSerializeContext): Block[];
@@ -5,4 +5,13 @@ import { SobreeDocument } from '../../../doc/types';
5
5
  * produced here — the Sobree façade injects those from its current page
6
6
  * setup state before handing the document off to the exporter.
7
7
  */
8
- export declare function serializeHostsToDocument(hosts: readonly HTMLElement[]): SobreeDocument;
8
+ export interface SerializeHostsOptions {
9
+ /**
10
+ * Capture each paragraph's effective base run style into
11
+ * `ParagraphProperties.runDefaults`. Set for textbox-frame read-back so a
12
+ * frame's font survives run-level styling loss; left off for body flow,
13
+ * which stays style-linked. See `BlockSerializeContext.captureRunDefaults`.
14
+ */
15
+ captureRunDefaults?: boolean;
16
+ }
17
+ export declare function serializeHostsToDocument(hosts: readonly HTMLElement[], options?: SerializeHostsOptions): SobreeDocument;
@@ -1,4 +1,4 @@
1
- import { InlineRun } from '../../../doc/types';
1
+ import { InlineRun, RunProperties } from '../../../doc/types';
2
2
  /**
3
3
  * Serialise DOM children of `el` into a flat `InlineRun[]`. Nested
4
4
  * formatting wrappers (`<strong><em>...`) are flattened — each leaf text
@@ -9,3 +9,4 @@ import { InlineRun } from '../../../doc/types';
9
9
  * children (their own flat run list).
10
10
  */
11
11
  export declare function serializeInlineChildren(el: HTMLElement): InlineRun[];
12
+ export declare function mergeStyleAttribute(base: RunProperties, styleAttr: string | null): RunProperties;
@@ -24,6 +24,9 @@ export interface EditorDomHooks {
24
24
  trackedInput: TrackedInput;
25
25
  /** Tracked-changes authoring mode is on right now. */
26
26
  isTrackedEnabled: () => boolean;
27
+ /** A real edit is about to mutate the DOM (`beforeinput`, before the
28
+ * mutation). Lets the editor stash the pre-edit selection for undo. */
29
+ onBeforeInput: () => void;
27
30
  /** Mark the DOM dirty + schedule a debounced change. */
28
31
  onInput: () => void;
29
32
  /** Fire the editor's `selection` event. */
@@ -1,5 +1,5 @@
1
- import { Selection, Selection as PublicSelection } from '../doc/api';
2
- import { HistoryConfig, HistoryDepth } from './types';
1
+ import { CapturedSelection, HistoryConfig, HistoryDepth } from './types';
2
+ import { Selection as PublicSelection } from '../doc/api';
3
3
  /**
4
4
  * Undo / redo for the Sobree editor — backed by `Y.UndoManager`.
5
5
  *
@@ -51,22 +51,43 @@ export interface HistoryOptions extends Partial<HistoryConfig> {
51
51
  * local writes. UndoManager only tracks operations whose origin
52
52
  * is in this set. Defaults to `"local"`. */
53
53
  localOrigin?: unknown;
54
- /** Capture the *current* live selection — called as a stack item
55
- * is being added so we can stash it for restore on undo. */
56
- captureSelection: () => Selection;
54
+ /** Capture the *current* (post-edit) live selection — called as a
55
+ * stack item is added/updated, stashed as the step's `after`. Returns
56
+ * a body `Selection` or a frame selection; `History` keeps it opaque. */
57
+ captureSelection: () => CapturedSelection;
58
+ /** Capture the selection as it was BEFORE the edit that opened the
59
+ * current undo step — stashed as the step's `before` and restored on
60
+ * undo. Defaults to {@link captureSelection} when not provided (callers
61
+ * without a pre-edit hook, e.g. headless, get post-edit on both). */
62
+ capturePreEditSelection?: () => CapturedSelection;
57
63
  /** Restore a previously-captured selection to the live DOM /
58
64
  * EditorSelection. Called on undo / redo after the Y.Doc has been
59
65
  * re-projected and re-rendered. */
60
- restoreSelection: (sel: Selection) => void;
66
+ restoreSelection: (sel: CapturedSelection) => void;
67
+ /** Notify the caller that the current undo group has captured (or
68
+ * extended) its selection, so it can drop any pending pre-edit stash.
69
+ * Fires on every `stack-item-added` / `stack-item-updated`. */
70
+ onGroupSettled?: () => void;
61
71
  }
62
72
  export declare class History {
63
73
  private readonly mgr;
64
74
  private readonly listeners;
65
75
  private readonly captureSelection;
76
+ private readonly capturePreEditSelection;
66
77
  private readonly restoreSelection;
78
+ private readonly onGroupSettled;
67
79
  constructor(opts: HistoryOptions);
68
80
  undo(): boolean;
69
81
  redo(): boolean;
82
+ /**
83
+ * Close the current undo-capture group so the NEXT local edit starts a
84
+ * fresh undo step instead of coalescing into the previous one (within
85
+ * `captureTimeout`). The editor calls this when the editing context
86
+ * changes — e.g. the caret moves to a different textbox frame, or
87
+ * between a frame and the body — so two distinct edits don't collapse
88
+ * into a single undo. No-op when there's nothing pending to capture.
89
+ */
90
+ stopCapturing(): void;
70
91
  canUndo(): boolean;
71
92
  canRedo(): boolean;
72
93
  clear(): void;
@@ -1,8 +1,37 @@
1
+ import { Selection } from '../doc/api';
1
2
  /**
2
- * History config + depth shape. Phase 1b.6+ backed by Y.UndoManager;
3
- * the snapshot-specific types (HistoryEntry, SnapshotPosition,
4
- * SnapshotSelection) that lived here in Phase 1a are no longer needed.
3
+ * Selection inside an editable textbox frame. Frame bodies aren't body
4
+ * registry blocks, so the public `Selection` model can't address them —
5
+ * but undo/redo still needs to put the caret (or range) back. The editor
6
+ * captures this on a frame edit and restores it on undo, exactly as it
7
+ * does the body `Selection`; `History` treats the stashed value as opaque.
8
+ * A collapsed caret has `start === end`.
5
9
  */
10
+ export interface FrameSelection {
11
+ kind: "frame-selection";
12
+ /** `data-anchor-id` of the frame the selection was in. */
13
+ frameId: string;
14
+ /** Character offset of the selection start across the frame's text. */
15
+ start: number;
16
+ /** Character offset of the selection end (=== start for a caret). */
17
+ end: number;
18
+ }
19
+ /**
20
+ * What the editor stashes for an undo step: a body `Selection` (incl.
21
+ * `null` when focus is outside) or a {@link FrameSelection}. Restored
22
+ * verbatim on undo/redo so the cursor lands where the body's would.
23
+ */
24
+ export type CapturedSelection = Selection | FrameSelection;
25
+ /**
26
+ * Both ends of an undo step's cursor: where it sat BEFORE the edit and
27
+ * AFTER it. Undo restores `before` (you land where you started the edit);
28
+ * redo restores `after` (you land where the edit left you) — Word/Docs
29
+ * behaviour. Stashed on each `Y.UndoManager` stack item's meta.
30
+ */
31
+ export interface UndoSelections {
32
+ before: CapturedSelection;
33
+ after: CapturedSelection;
34
+ }
6
35
  export interface HistoryConfig {
7
36
  /** Hard cap on entry count (per stack). UndoManager doesn't expose
8
37
  * a max-depth knob directly — kept here for forward-compat in case