Skip to content

Commit 5196bd6

Browse files
committed
feat(json-crdt-extensions): 🎸 populate registry with inline slice types
1 parent a05b09b commit 5196bd6

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {PeritextMlElement} from '../block/types';
22
import {NodeBuilder} from '../../../json-crdt-patch';
3+
import {SliceBehavior} from '../slice/constants';
34
import type {JsonMlElement} from 'very-small-parser/lib/html/json-ml/types';
45
import type {FromHtmlConverter, SliceTypeDefinition, ToHtmlConverter} from './types';
5-
import {SliceBehavior} from '../slice/constants';
66

77
export class SliceRegistry {
88
private map: Map<string | number, SliceTypeDefinition<any, any, any>> = new Map();
@@ -24,6 +24,16 @@ export class SliceRegistry {
2424
}
2525
}
2626

27+
public def<Type extends number | string, Schema extends NodeBuilder, Inline extends boolean = true>(
28+
type: Type,
29+
schema: Schema,
30+
behavior: SliceBehavior,
31+
inline: boolean,
32+
rest: Omit<SliceTypeDefinition<Type, Schema, Inline>, 'type' | 'schema' | 'behavior' | 'inline'> = {},
33+
): void {
34+
this.add({type, schema, behavior, inline, ...rest});
35+
}
36+
2737
public toHtml(el: PeritextMlElement): ReturnType<ToHtmlConverter<any>> | undefined {
2838
const converter = this.toHtmlMap.get(el[0]);
2939
return converter ? converter(el) : undefined;

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

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,43 @@ import {CommonSliceType} from "../slice";
55
import {SliceBehavior} from '../slice/constants';
66
import {SliceRegistry} from "./SliceRegistry";
77

8+
const undefSchema = s.con(undefined);
9+
810
/**
911
* Default annotation type registry.
1012
*/
1113
export const registry = new SliceRegistry();
1214

15+
registry.def(CommonSliceType.i, undefSchema, SliceBehavior.One, true, {
16+
fromHtml: {
17+
i: () => [CommonSliceType.i, null],
18+
em: () => [CommonSliceType.i, null],
19+
},
20+
});
21+
22+
registry.def(CommonSliceType.b, undefSchema, SliceBehavior.One, true, {
23+
fromHtml: {
24+
b: () => [CommonSliceType.b, null],
25+
strong: () => [CommonSliceType.b, null],
26+
},
27+
});
28+
29+
registry.def(CommonSliceType.u, undefSchema, SliceBehavior.One, true);
30+
registry.def(CommonSliceType.s, undefSchema, SliceBehavior.One, true);
31+
registry.def(CommonSliceType.code, undefSchema, SliceBehavior.One, true);
32+
registry.def(CommonSliceType.mark, undefSchema, SliceBehavior.One, true);
33+
registry.def(CommonSliceType.kbd, undefSchema, SliceBehavior.One, true);
34+
registry.def(CommonSliceType.del, undefSchema, SliceBehavior.One, true);
35+
registry.def(CommonSliceType.ins, undefSchema, SliceBehavior.One, true);
36+
registry.def(CommonSliceType.sup, undefSchema, SliceBehavior.One, true);
37+
registry.def(CommonSliceType.sub, undefSchema, SliceBehavior.One, true);
38+
registry.def(CommonSliceType.math, undefSchema, SliceBehavior.One, true);
39+
1340
const aSchema = s.obj({
1441
href: s.str<string>(''),
1542
title: s.str<string>(''),
1643
});
17-
18-
registry.add({
19-
type: CommonSliceType.a,
20-
schema: aSchema,
21-
// toHtml: (el) => ['a', {...el[1]?.data}],
44+
registry.def(CommonSliceType.a, aSchema, SliceBehavior.Many, true, {
2245
fromHtml: {
2346
a: (jsonml) => {
2447
const attr = jsonml[1] || {};
@@ -31,12 +54,14 @@ registry.add({
3154
},
3255
});
3356

34-
registry.add({
35-
type: CommonSliceType.i,
36-
behavior: SliceBehavior.One,
37-
schema: s.con(undefined),
38-
fromHtml: {
39-
em: () => [CommonSliceType.i, null],
40-
i: () => [CommonSliceType.i, null],
41-
},
42-
});
57+
// TODO: add more default annotations
58+
// comment = SliceTypeCon.comment,
59+
// font = SliceTypeCon.font,
60+
// col = SliceTypeCon.col,
61+
// bg = SliceTypeCon.bg,
62+
// hidden = SliceTypeCon.hidden,
63+
// footnote = SliceTypeCon.footnote,
64+
// ref = SliceTypeCon.ref,
65+
// iaside = SliceTypeCon.iaside,
66+
// iembed = SliceTypeCon.iembed,
67+
// bookmark = SliceTypeCon.bookmark,

0 commit comments

Comments
 (0)