Skip to content

Commit f4a6fc8

Browse files
committed
chore(json-crdt-peritext-ui): 🤖 progress on cursor calculation
1 parent 89640a9 commit f4a6fc8

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/json-crdt-peritext-ui/plugins/cursor/RenderCaret.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ export const RenderCaret: React.FC<RenderCaretProps> = ({italic, point, children
7474
const {dom} = usePeritext();
7575
const focus = useSyncStoreOpt(dom?.cursor.focus) || false;
7676
const plugin = useCursorPlugin();
77+
React.useLayoutEffect(() => {
78+
if (point.anchor === Anchor.After) {
79+
if (point.isAbs()) return;
80+
const rect = ctx.dom.getCharRect(point.id);
81+
if (!rect) return;
82+
const nextPoint = point.copy(p => p.refBefore());
83+
if (nextPoint.isAbs()) return;
84+
console.log(nextPoint.id);
85+
const rect2 = ctx.dom.getCharRect(nextPoint.id);
86+
console.log('rect', rect, rect2);
87+
}
88+
}, [point]);
7789

7890
const anchorForward = point.anchor === Anchor.Before;
7991

src/json-crdt-peritext-ui/plugins/cursor/state.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ValueSyncStore} from '../../../util/events/sync-store';
2-
import {ChangeDetail} from '../../events/types';
2+
import type {ChangeDetail} from '../../events/types';
33
import type {PeritextSurfaceState, UiLifeCycles} from '../../web';
44

55
export class CursorState implements UiLifeCycles {
@@ -16,11 +16,12 @@ export class CursorState implements UiLifeCycles {
1616
public readonly ctx: PeritextSurfaceState,
1717
) {}
1818

19-
/* --------------------------------------------------- {@link UiLifeCycles} */
19+
/** -------------------------------------------------- {@link UiLifeCycles} */
2020
public start(): () => void {
2121
const dom = this.ctx.dom;
2222
let lastNow: number = 0;
23-
const listener = (event: CustomEvent<ChangeDetail>) => {
23+
24+
const onChange = (event: CustomEvent<ChangeDetail>) => {
2425
const now = Date.now();
2526
const timeDiff = now - lastNow;
2627
let delta = 0;
@@ -41,9 +42,10 @@ export class CursorState implements UiLifeCycles {
4142
this.scoreDelta.next(delta);
4243
lastNow = now;
4344
};
44-
dom.et.addEventListener('change', listener);
45+
46+
dom.et.addEventListener('change', onChange);
4547
return () => {
46-
dom.et.removeEventListener('change', listener);
48+
dom.et.removeEventListener('change', onChange);
4749
};
4850
}
4951
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export class DomController implements UiLifeCycles, Printable, PeritextUiApi {
133133
const end = txt.point(id, Anchor.After);
134134
const charRange = txt.range(start, end);
135135
const span = this.findSpanContaining(charRange);
136+
console.log('span', span);
136137
if (!span) return;
137138
const inline = (span as any)[ElementAttr.InlineOffset] as Inline | undefined;
138139
if (!inline) return;

0 commit comments

Comments
 (0)