Skip to content

Commit 2c491bb

Browse files
committed
feat(json-crdt-extensions): 🎸 register block slice types
1 parent 356387a commit 2c491bb

File tree

2 files changed

+97
-43
lines changed

2 files changed

+97
-43
lines changed

src/json-crdt-extensions/peritext/registry/registry.ts

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {s} from '../../../json-crdt-patch';
2-
import {SliceBehavior, SliceTypeCon} from '../slice/constants';
2+
import {SliceBehavior, SliceTypeCon as TAG} from '../slice/constants';
33
import {SliceRegistry, SliceRegistryEntry, TagType} from './SliceRegistry';
44
import type {JsonNodeView} from '../../../json-crdt/nodes';
55
import type {SchemaToJsonNode} from '../../../json-crdt/schema/types';
@@ -14,7 +14,7 @@ const undefSchema = s.con(undefined);
1414

1515
// ----------------------------------------- Inline elements with "One" behavior
1616

17-
const inlineOne = <Tag extends TagType = TagType>(
17+
const i0 = <Tag extends TagType = TagType>(
1818
tag: Tag,
1919
fromHtml?: SliceRegistryEntry<SliceBehavior.One, Tag, typeof undefSchema>['fromHtml'],
2020
): void => {
@@ -30,24 +30,24 @@ const inlineOne = <Tag extends TagType = TagType>(
3030
);
3131
};
3232

33-
const inlineOne2 = <Tag extends TagType = TagType>(tag: Tag, htmlTags: string[]): void => {
33+
const i1 = <Tag extends TagType = TagType>(tag: Tag, htmlTags: string[]): void => {
3434
const fromHtml = {} as Record<any, any>;
3535
for (const htmlTag of htmlTags) fromHtml[htmlTag] = () => [tag, null];
36-
inlineOne(tag, fromHtml);
36+
i0(tag, fromHtml);
3737
};
3838

39-
inlineOne2(SliceTypeCon.i, ['i', 'em']);
40-
inlineOne2(SliceTypeCon.b, ['b', 'strong']);
41-
inlineOne2(SliceTypeCon.s, ['s', 'strike']);
42-
inlineOne(SliceTypeCon.u);
43-
inlineOne(SliceTypeCon.code);
44-
inlineOne(SliceTypeCon.mark);
45-
inlineOne(SliceTypeCon.kbd);
46-
inlineOne(SliceTypeCon.del);
47-
inlineOne(SliceTypeCon.ins);
48-
inlineOne(SliceTypeCon.sup);
49-
inlineOne(SliceTypeCon.sub);
50-
inlineOne(SliceTypeCon.math);
39+
i1(TAG.i, ['i', 'em']);
40+
i1(TAG.b, ['b', 'strong']);
41+
i1(TAG.s, ['s', 'strike']);
42+
i0(TAG.u);
43+
i0(TAG.code);
44+
i0(TAG.mark);
45+
i0(TAG.kbd);
46+
i0(TAG.del);
47+
i0(TAG.ins);
48+
i0(TAG.sup);
49+
i0(TAG.sub);
50+
i0(TAG.math);
5151

5252
// ---------------------------------------- Inline elements with "Many" behavior
5353

@@ -58,7 +58,7 @@ const aSchema = s.obj({
5858
registry.add(
5959
new SliceRegistryEntry(
6060
SliceBehavior.Many,
61-
SliceTypeCon.a,
61+
TAG.a,
6262
aSchema,
6363
false,
6464
void 0,
@@ -69,7 +69,7 @@ registry.add(
6969
href: attr.href ?? '',
7070
title: attr.title ?? '',
7171
};
72-
return [SliceTypeCon.a, {data, inline: true}] as PeritextMlElement<SliceTypeCon.a, any, true>;
72+
return [TAG.a, {data, inline: true}] as PeritextMlElement<TAG.a, any, true>;
7373
},
7474
},
7575
)
@@ -89,4 +89,54 @@ registry.add(
8989

9090
// --------------------------------------- Block elements with "Marker" behavior
9191

92-
// registry.def(CommonSliceType.blockquote, undefSchema, SliceBehavior.Marker, false);
92+
const commonBlockSchema = s.obj({}, {
93+
indent: s.con(0),
94+
align: s.str<'left' | 'center' | 'right' | 'justify'>('left'),
95+
});
96+
97+
const b0 = <Tag extends TagType = TagType>(
98+
tag: Tag,
99+
container: boolean,
100+
) => {
101+
registry.add(
102+
new SliceRegistryEntry(
103+
SliceBehavior.Marker,
104+
tag,
105+
commonBlockSchema,
106+
container,
107+
)
108+
);
109+
};
110+
111+
b0(TAG.p, false);
112+
b0(TAG.blockquote, true);
113+
b0(TAG.codeblock, false);
114+
b0(TAG.pre, false);
115+
b0(TAG.ul, true);
116+
b0(TAG.ol, true);
117+
b0(TAG.tl, true);
118+
b0(TAG.ol, true);
119+
b0(TAG.li, true);
120+
b0(TAG.h1, false);
121+
b0(TAG.h2, false);
122+
b0(TAG.h3, false);
123+
b0(TAG.h4, false);
124+
b0(TAG.h5, false);
125+
b0(TAG.h6, false);
126+
b0(TAG.title, false);
127+
b0(TAG.subtitle, false);
128+
// b0(TAG.br, false);
129+
// b0(TAG.nl, false);
130+
// b0(TAG.hr, false);
131+
// b0(TAG.page, false);
132+
// b0(TAG.aside, true);
133+
// b0(TAG.embed, false);
134+
// b0(TAG.column, true);
135+
// b0(TAG.contents, true);
136+
// b0(TAG.table, true);
137+
// b0(TAG.row, true);
138+
// b0(TAG.cell, true);
139+
// b0(TAG.collapselist, true);
140+
// b0(TAG.collapse, true);
141+
// b0(TAG.note, true);
142+
// b0(TAG.mathblock, false);

src/json-crdt-extensions/peritext/slice/constants.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,34 @@ export const enum SliceTypeCon {
1717
pre = 3, // <pre>
1818
ul = 4, // <ul>
1919
ol = 5, // <ol>
20-
tasklist = 6, // - [ ] Task list
21-
h1 = 7, // <h1>
22-
h2 = 8, // <h2>
23-
h3 = 9, // <h3>
24-
h4 = 10, // <h4>
25-
h5 = 11, // <h5>
26-
h6 = 12, // <h6>
27-
title = 13, // <title>
28-
subtitle = 14, // <subtitle>
29-
br = 15, // <br>
30-
nl = 16, // \n
31-
hr = 17, // <hr>
32-
page = 18, // Page break
33-
aside = 19, // <aside>
34-
embed = 20, // <embed>, <iframe>, <object>, <video>, <audio>, etc.
35-
column = 21, // <div style="column-count: ..."> (represents 2 and 3 column layouts)
36-
contents = 22, // Table of contents
37-
table = 23, // <table>
38-
row = 24, // Table row
39-
cell = 25, // Table cell
40-
collapselist = 26, // Collapsible list - > List item
41-
collapse = 27, // Collapsible block
42-
note = 28, // Note block
43-
mathblock = 29, // <math> block
20+
tl = 6, // - [ ] Task list
21+
li = 7, // <li>
22+
h1 = 8, // <h1>
23+
h2 = 9, // <h2>
24+
h3 = 10, // <h3>
25+
h4 = 11, // <h4>
26+
h5 = 12, // <h5>
27+
h6 = 13, // <h6>
28+
title = 14, // <title>
29+
subtitle = 15, // <subtitle>
30+
br = 16, // <br>
31+
nl = 17, // \n
32+
hr = 18, // <hr>
33+
page = 19, // Page break
34+
aside = 20, // <aside>
35+
embed = 21, // <embed>, <iframe>, <object>, <video>, <audio>, etc.
36+
column = 22, // <div style="column-count: ..."> (represents 2 and 3 column layouts)
37+
contents = 23, // Table of contents
38+
table = 24, // <table>
39+
row = 25, // Table row
40+
cell = 26, // Table cell
41+
42+
// TODO: rename to `cl` (collapsible list)?
43+
collapselist = 27, // Collapsible list - > List item
44+
45+
collapse = 28, // Collapsible block
46+
note = 29, // Note block
47+
mathblock = 30, // <math> block
4448

4549
// ------------------------------------------------ inline slices (-64 to -1)
4650
Cursor = -1,

0 commit comments

Comments
 (0)