Skip to content

Commit f39ddb6

Browse files
committed
feat(json-crdt-extensions): 🎸 update inline registry entries
1 parent 607fa77 commit f39ddb6

File tree

3 files changed

+67
-79
lines changed

3 files changed

+67
-79
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {CommonSliceType} from '../slice';
33
import type {PeritextMlElement} from '../block/types';
44
import type {NodeBuilder} from '../../../json-crdt-patch';
55
import type {JsonMlElement} from 'very-small-parser/lib/html/json-ml/types';
6-
import type {FromHtmlConverter, SliceTypeDefinition, ToHtmlConverter} from './types';
6+
import type {FromHtmlConverter, ToHtmlConverter} from './types';
77
import type {JsonNodeView} from '../../../json-crdt/nodes';
88
import type {SchemaToJsonNode} from '../../../json-crdt/schema/types';
99

@@ -88,7 +88,7 @@ export class SliceRegistry {
8888
private _fromHtml: Map<string, [entry: SliceRegistryEntry, converter: FromHtmlConverter][]> =
8989
new Map();
9090

91-
public add(entry: SliceRegistryEntry): void {
91+
public add(entry: SliceRegistryEntry<any, any, any>): void {
9292
const {tag, fromHtml} = entry;
9393
const _fromHtml = this._fromHtml;
9494
if (fromHtml) {
@@ -113,12 +113,12 @@ export class SliceRegistry {
113113
const tag = el[0] + '';
114114
const converters = this._fromHtml.get(tag);
115115
if (converters) {
116-
for (const [def, converter] of converters) {
116+
for (const [entry, converter] of converters) {
117117
const result = converter(el);
118118
if (result) {
119119
const attr = result[1] ?? (result[1] = {});
120-
attr.inline = def.inline ?? def.type < 0;
121-
attr.behavior = !attr.inline ? SliceBehavior.Marker : (def.behavior ?? SliceBehavior.Many);
120+
attr.inline = entry.isInline();
121+
attr.behavior = !attr.inline ? SliceBehavior.Marker : (entry.behavior ?? SliceBehavior.Many);
122122
return result;
123123
}
124124
}
Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,80 @@
11
import {s} from '../../../json-crdt-patch';
2+
import {SliceBehavior, SliceTypeCon} from '../slice/constants';
3+
import {SliceRegistry, SliceRegistryEntry, TagType} from './SliceRegistry';
24
import type {JsonNodeView} from '../../../json-crdt/nodes';
35
import type {SchemaToJsonNode} from '../../../json-crdt/schema/types';
4-
import {CommonSliceType} from '../slice';
5-
import {SliceBehavior} from '../slice/constants';
6-
import {SliceRegistry} from './SliceRegistry';
7-
8-
const undefSchema = s.con(undefined);
96

107
/**
118
* Default annotation type registry.
12-
*/
9+
*/
1310
export const registry = new SliceRegistry();
1411

15-
registry.def(CommonSliceType.i, undefSchema, SliceBehavior.One, true, {
16-
fromHtml: {
17-
i: () => [CommonSliceType.i, null],
18-
em: () => [CommonSliceType.i, null],
19-
},
20-
});
12+
const undefSchema = s.con(undefined);
2113

22-
registry.def(CommonSliceType.b, undefSchema, SliceBehavior.One, true, {
23-
fromHtml: {
24-
b: () => [CommonSliceType.b, null],
25-
strong: () => [CommonSliceType.b, null],
26-
},
27-
});
14+
// ----------------------------------------- Inline elements with "One" behavior
2815

29-
registry.def(CommonSliceType.s, undefSchema, SliceBehavior.One, true, {
30-
fromHtml: {
31-
s: () => [CommonSliceType.s, null],
32-
strike: () => [CommonSliceType.s, null],
33-
},
34-
});
16+
const inlineOne = <Tag extends TagType = TagType>(
17+
tag: Tag,
18+
fromHtml?: SliceRegistryEntry<SliceBehavior.One, Tag, typeof undefSchema>['fromHtml'],
19+
): void => {
20+
registry.add(
21+
new SliceRegistryEntry(
22+
SliceBehavior.One,
23+
tag,
24+
undefSchema,
25+
false,
26+
void 0,
27+
fromHtml,
28+
)
29+
);
30+
};
31+
32+
const inlineOne2 = <Tag extends TagType = TagType>(tag: Tag, htmlTags: string[]): void => {
33+
const fromHtml = {} as Record<any, any>;
34+
for (const htmlTag of htmlTags) fromHtml[htmlTag] = () => [tag, null];
35+
inlineOne(tag, fromHtml);
36+
};
37+
38+
inlineOne2(SliceTypeCon.i, ['i', 'em']);
39+
inlineOne2(SliceTypeCon.b, ['b', 'strong']);
40+
inlineOne2(SliceTypeCon.s, ['s', 'strike']);
41+
inlineOne(SliceTypeCon.u);
42+
inlineOne(SliceTypeCon.code);
43+
inlineOne(SliceTypeCon.mark);
44+
inlineOne(SliceTypeCon.kbd);
45+
inlineOne(SliceTypeCon.del);
46+
inlineOne(SliceTypeCon.ins);
47+
inlineOne(SliceTypeCon.sup);
48+
inlineOne(SliceTypeCon.sub);
49+
inlineOne(SliceTypeCon.math);
3550

36-
registry.def(CommonSliceType.u, undefSchema, SliceBehavior.One, true);
37-
registry.def(CommonSliceType.code, undefSchema, SliceBehavior.One, true);
38-
registry.def(CommonSliceType.mark, undefSchema, SliceBehavior.One, true);
39-
registry.def(CommonSliceType.kbd, undefSchema, SliceBehavior.One, true);
40-
registry.def(CommonSliceType.del, undefSchema, SliceBehavior.One, true);
41-
registry.def(CommonSliceType.ins, undefSchema, SliceBehavior.One, true);
42-
registry.def(CommonSliceType.sup, undefSchema, SliceBehavior.One, true);
43-
registry.def(CommonSliceType.sub, undefSchema, SliceBehavior.One, true);
44-
registry.def(CommonSliceType.math, undefSchema, SliceBehavior.One, true);
51+
// ---------------------------------------- Inline elements with "Many" behavior
4552

4653
const aSchema = s.obj({
4754
href: s.str<string>(''),
4855
title: s.str<string>(''),
4956
});
50-
registry.def(CommonSliceType.a, aSchema, SliceBehavior.Many, true, {
51-
fromHtml: {
52-
a: (jsonml) => {
53-
const attr = jsonml[1] || {};
54-
const data: JsonNodeView<SchemaToJsonNode<typeof aSchema>> = {
55-
href: attr.href ?? '',
56-
title: attr.title ?? '',
57-
};
58-
return [CommonSliceType.a, {data, inline: true}];
57+
registry.add(
58+
new SliceRegistryEntry(
59+
SliceBehavior.Many,
60+
SliceTypeCon.a,
61+
aSchema,
62+
false,
63+
void 0,
64+
{
65+
a: (jsonml) => {
66+
const attr = jsonml[1] || {};
67+
const data: JsonNodeView<SchemaToJsonNode<typeof aSchema>> = {
68+
href: attr.href ?? '',
69+
title: attr.title ?? '',
70+
};
71+
return [SliceTypeCon.a, {data, inline: true}];
72+
},
5973
},
60-
},
61-
});
74+
)
75+
);
6276

63-
// TODO: add more default annotations
77+
// TODO: add more default annotations with "Many" behavior
6478
// comment = SliceTypeCon.comment,
6579
// font = SliceTypeCon.font,
6680
// col = SliceTypeCon.col,
@@ -72,4 +86,6 @@ registry.def(CommonSliceType.a, aSchema, SliceBehavior.Many, true, {
7286
// iembed = SliceTypeCon.iembed,
7387
// bookmark = SliceTypeCon.bookmark,
7488

75-
registry.def(CommonSliceType.blockquote, undefSchema, SliceBehavior.Marker, false);
89+
// --------------------------------------- Block elements with "Marker" behavior
90+
91+
// registry.def(CommonSliceType.blockquote, undefSchema, SliceBehavior.Marker, false);

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,5 @@
1-
import type {NodeBuilder} from '../../../json-crdt-patch';
2-
import type {JsonNodeView} from '../../../json-crdt/nodes';
3-
import type {SchemaToJsonNode} from '../../../json-crdt/schema/types';
41
import type {PeritextMlElement} from '../block/types';
52
import type {JsonMlElement} from 'very-small-parser/lib/html/json-ml/types';
6-
import type {SliceBehavior} from '../slice/constants';
7-
8-
export interface SliceTypeDefinition<
9-
Type extends number | string = number | string,
10-
Schema extends NodeBuilder = NodeBuilder,
11-
Inline extends boolean = true,
12-
> {
13-
type: Type;
14-
schema: Schema;
15-
behavior?: SliceBehavior;
16-
inline?: boolean;
17-
18-
/**
19-
* For block element slices (block splits), the `container` property specifies
20-
* whether the type is a container for other block elements. For example, a
21-
* `blockquote` is a container for `paragraph` elements, however, a `paragraph`
22-
* is not a container (it can only contain inline elements).
23-
*/
24-
container?: boolean;
25-
26-
toHtml?: ToHtmlConverter<PeritextMlElement<Type, JsonNodeView<SchemaToJsonNode<Schema>>, Inline>>;
27-
fromHtml?: {
28-
[htmlTag: string]: FromHtmlConverter<PeritextMlElement<Type, JsonNodeView<SchemaToJsonNode<Schema>>, Inline>>;
29-
};
30-
}
313

324
export type ToHtmlConverter<
335
El extends PeritextMlElement<any, any, any> = PeritextMlElement<string | number, unknown, boolean>,

0 commit comments

Comments
 (0)