Skip to content

Commit 87c5a7a

Browse files
committed
feat(json-crdt-peritext-ui): 🎸 position caret at line end
1 parent f4a6fc8 commit 87c5a7a

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/json-crdt-peritext-ui/events/defaults/ui/UiHandle.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ export class UiHandle {
2727
*/
2828
public getPointRect(pos: number | Point<string>, right = true): Rect | undefined {
2929
const txt = this.txt;
30-
const point = typeof pos === 'number' ? txt.pointAt(pos) : pos;
31-
const char = right ? point.rightChar() : point.leftChar();
32-
if (!char) return;
33-
const id = tick(char.chunk.id, char.off);
34-
return this.api.getCharRect?.(id);
30+
const point = typeof pos === 'number' ? txt.pointAt(pos) : pos.clone();
31+
if (right) point.refBefore(); else point.refAfter();
32+
return this.api.getCharRect?.(point.id);
3533
}
3634

3735
public pointX(pos: number | Point<string>): [x: number, rect: Rect] | undefined {

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,27 @@ 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(() => {
77+
const ref = React.useRef<HTMLSpanElement>(null);
78+
React.useEffect(() => {
79+
const el = ref.current;
80+
if (!el) return;
81+
const style = el.style;
82+
style.top = '0px';
83+
style.left = '0px';
7884
if (point.anchor === Anchor.After) {
7985
if (point.isAbs()) return;
8086
const rect = ctx.dom.getCharRect(point.id);
8187
if (!rect) return;
8288
const nextPoint = point.copy(p => p.refBefore());
8389
if (nextPoint.isAbs()) return;
84-
console.log(nextPoint.id);
8590
const rect2 = ctx.dom.getCharRect(nextPoint.id);
86-
console.log('rect', rect, rect2);
91+
if (!rect2) return;
92+
if (rect.x > rect2.x) {
93+
const dx = rect.x + rect.width - rect2.x;
94+
const dy = rect.y - rect2.y;
95+
style.top = dy + 'px';
96+
style.left = dx + 'px';
97+
}
8798
}
8899
}, [point]);
89100

@@ -105,7 +116,7 @@ export const RenderCaret: React.FC<RenderCaretProps> = ({italic, point, children
105116
}
106117

107118
return (
108-
<span className={blockClass}>
119+
<span ref={ref} className={blockClass}>
109120
{children}
110121
{score !== plugin.lastVisScore.value && (
111122
<CaretScore

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ 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);
137136
if (!span) return;
138137
const inline = (span as any)[ElementAttr.InlineOffset] as Inline | undefined;
139138
if (!inline) return;

0 commit comments

Comments
 (0)