1
1
import * as React from 'react' ;
2
+ import { createElement as h } from 'react' ;
2
3
import { LeafBlock } from '../../../json-crdt-extensions/peritext/block/LeafBlock' ;
3
4
import { InlineView } from './InlineView' ;
4
5
import { Char } from '../constants' ;
5
6
import { usePeritext } from './context' ;
6
7
import { CommonSliceType } from '../../../json-crdt-extensions' ;
7
8
import { CaretView } from './cursor/CaretView' ;
8
9
import { FocusView } from './cursor/FocusView' ;
9
- import { InlineAttrEnd , InlineAttrPassing , InlineAttrStart } from '../../../json-crdt-extensions/peritext/block/Inline' ;
10
+ import { Inline , InlineAttrEnd , InlineAttrPassing , InlineAttrStart } from '../../../json-crdt-extensions/peritext/block/Inline' ;
10
11
import { AnchorView } from './cursor/AnchorView' ;
11
12
import type { Block } from '../../../json-crdt-extensions/peritext/block/Block' ;
12
13
@@ -22,8 +23,12 @@ export const BlockView: React.FC<BlockViewProps> = React.memo(
22
23
const { plugins, dom} = usePeritext ( ) ;
23
24
const elements : React . ReactNode [ ] = [ ] ;
24
25
if ( block instanceof LeafBlock ) {
25
- for ( const inline of block . texts ( ) ) {
26
+ let inline : Inline < string > | undefined ;
27
+ let prevInline : Inline < string > | undefined ;
28
+ for ( const iterator = block . texts0 ( ) ; ( inline = iterator ( ) ) ; prevInline = inline ) {
26
29
const hasCursor = inline . hasCursor ( ) ;
30
+
31
+ // Insert cursor before the inline text element.
27
32
if ( hasCursor ) {
28
33
const attr = inline . attr ( ) ;
29
34
const italic = attr [ CommonSliceType . i ] ?. [ 0 ] ;
@@ -32,17 +37,22 @@ export const BlockView: React.FC<BlockViewProps> = React.memo(
32
37
const key = cursorStart . start . key ( ) + '-a' ;
33
38
let element : React . ReactNode ;
34
39
if ( cursorStart . isStartFocused ( ) ) {
35
- if ( cursorStart . isCollapsed ( ) )
36
- element = < CaretView key = { key } italic = { ! ! italic } point = { cursorStart . start } cursor = { cursorStart } /> ;
37
- else {
40
+ if ( cursorStart . isCollapsed ( ) ) {
41
+ element = < CaretView key = { key } italic = { ! ! italic } point = { cursorStart . start } cursor = { cursorStart } fwd = { inline } bwd = { prevInline } /> ;
42
+ } else {
38
43
const isItalic = italic instanceof InlineAttrEnd || italic instanceof InlineAttrPassing ;
39
44
element = < FocusView key = { key } italic = { isItalic } cursor = { cursorStart } /> ;
40
45
}
41
46
} else element = < AnchorView key = { key } /> ;
42
47
elements . push ( element ) ;
43
48
}
44
49
}
45
- elements . push ( < InlineView key = { inline . key ( ) } inline = { inline } /> ) ;
50
+
51
+ // Insert the inline text element itself.
52
+ const currInlineProps = { key : inline . key ( ) , inline} ;
53
+ elements . push ( h ( InlineView , currInlineProps ) ) ;
54
+
55
+ // Insert cursor after the inline text element.
46
56
if ( hasCursor ) {
47
57
const cursorEnd = inline . cursorEnd ( ) ;
48
58
const attr = inline . attr ( ) ;
@@ -51,9 +61,9 @@ export const BlockView: React.FC<BlockViewProps> = React.memo(
51
61
const key = cursorEnd . end . key ( ) + '-b' ;
52
62
let element : React . ReactNode ;
53
63
if ( cursorEnd . isEndFocused ( ) ) {
54
- if ( cursorEnd . isCollapsed ( ) )
55
- element = < CaretView key = { key } italic = { ! ! italic } point = { cursorEnd . start } cursor = { cursorEnd } /> ;
56
- else
64
+ if ( cursorEnd . isCollapsed ( ) ) {
65
+ element = < CaretView key = { key } italic = { ! ! italic } point = { cursorEnd . start } cursor = { cursorEnd } bwd = { inline } /> ;
66
+ } else
57
67
element = (
58
68
< FocusView
59
69
key = { key }
0 commit comments