Skip to content

Commit 4a35ac7

Browse files
committed
feat(json-crdt-peritext-ui): 🎸 improve boundary element mapping
1 parent 9917b37 commit 4a35ac7

File tree

4 files changed

+16
-31
lines changed

4 files changed

+16
-31
lines changed

src/json-crdt-peritext-ui/web/dom/DomController.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {printTree, type Printable} from 'tree-dump';
2+
import {AvlMap} from "sonic-forest/lib/avl/AvlMap";
23
import {InputController} from './InputController';
34
import {CursorController} from './CursorController';
45
import {RichTextController} from './RichTextController';
@@ -8,12 +9,12 @@ import {AnnalsController} from './annals/AnnalsController';
89
import {ElementAttr} from '../constants';
910
import {Anchor} from '../../../json-crdt-extensions/peritext/rga/constants';
1011
import {UiHandle} from '../../events/defaults/ui/UiHandle';
11-
import type {ITimestampStruct} from '../../../json-crdt-patch';
12+
import {compare, type ITimestampStruct} from '../../../json-crdt-patch';
1213
import type {PeritextEventDefaults} from '../../events/defaults/PeritextEventDefaults';
1314
import type {PeritextEventTarget} from '../../events/PeritextEventTarget';
1415
import type {Rect, UiLifeCycles} from '../types';
1516
import type {Log} from '../../../json-crdt/log/Log';
16-
import type {Inline} from '../../../json-crdt-extensions';
17+
import type {Inline, Peritext} from '../../../json-crdt-extensions';
1718
import type {Range} from '../../../json-crdt-extensions/peritext/rga/Range';
1819
import type {PeritextUiApi} from '../../events/defaults/ui/types';
1920

@@ -24,17 +25,20 @@ export interface DomControllerOpts {
2425
}
2526

2627
export class DomController implements UiLifeCycles, Printable, PeritextUiApi {
28+
public readonly txt: Peritext;
2729
public readonly et: PeritextEventTarget;
2830
public readonly keys: KeyController;
2931
public readonly comp: CompositionController;
3032
public readonly input: InputController;
3133
public readonly cursor: CursorController;
3234
public readonly richText: RichTextController;
3335
public readonly annals: AnnalsController;
36+
public readonly boundaries = new AvlMap<ITimestampStruct, Element>(compare);
3437

3538
constructor(public readonly opts: DomControllerOpts) {
3639
const {source, events, log} = opts;
3740
const {txt} = events;
41+
this.txt = txt;
3842
const et = (this.et = opts.events.et);
3943
const keys = (this.keys = new KeyController({source}));
4044
const comp = (this.comp = new CompositionController({et, source, txt}));
@@ -77,6 +81,12 @@ export class DomController implements UiLifeCycles, Printable, PeritextUiApi {
7781
}
7882

7983
protected findSpanContaining(range: Range): [span: HTMLSpanElement, inline: Inline] | undefined {
84+
const start = range.start;
85+
const marker = this.txt.overlay.getOrNextLowerMarker(start);
86+
const markerId = marker?.id;
87+
const el = this.boundaries.get(markerId);
88+
// console.log('markerId', markerId);
89+
8090
const spans = this.getSpans();
8191
const length = spans.length;
8292
for (let i = 0; i < length; i++) {

src/json-crdt-peritext-ui/web/react/BlockView.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface BlockViewProps {
1919
export const BlockView: React.FC<BlockViewProps> = React.memo(
2020
(props) => {
2121
const {block, el} = props;
22-
const {plugins, mapping} = usePeritext();
22+
const {plugins, dom} = usePeritext();
2323
const elements: React.ReactNode[] = [];
2424
if (block instanceof LeafBlock) {
2525
for (const inline of block.texts()) {
@@ -78,8 +78,9 @@ export const BlockView: React.FC<BlockViewProps> = React.memo(
7878
<span ref={(element) => {
7979
el?.(element);
8080
if (block instanceof LeafBlock) {
81-
if (element) mapping.setBlock(block, element);
82-
else mapping.delBlock(block);
81+
const blockId = block.start.id;
82+
if (element) dom.boundaries.set(blockId, element);
83+
else dom.boundaries.del(blockId);
8384
}
8485
}} style={{position: 'relative', display: 'block'}}>
8586
{elements.length ? elements : Char.ZeroLengthSpace}

src/json-crdt-peritext-ui/web/state/ElementMapping.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/json-crdt-peritext-ui/web/state/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {Log} from '../../../json-crdt/log/Log';
22
import {DomController} from '../dom/DomController';
33
import {ValueSyncStore} from '../../../util/events/sync-store';
4-
import {ElementMapping} from './ElementMapping';
54
import type {PeritextPlugin} from '../react/types';
65
import type {Peritext} from '../../../json-crdt-extensions/peritext/Peritext';
76
import type {PeritextEventDefaults} from '../../events/defaults/PeritextEventDefaults';
@@ -12,7 +11,6 @@ export class PeritextSurfaceState implements UiLifeCycles {
1211
public readonly dom: DomController;
1312
public readonly log: Log;
1413
public readonly render = new ValueSyncStore<number>(0);
15-
public readonly mapping: ElementMapping = new ElementMapping();;
1614

1715
public readonly rerender = (): void => {
1816
const {peritext, render} = this;

0 commit comments

Comments
 (0)