11import { s } from '../../../json-crdt-patch' ;
2+ import { SliceBehavior , SliceTypeCon } from '../slice/constants' ;
3+ import { SliceRegistry , SliceRegistryEntry , TagType } from './SliceRegistry' ;
24import type { JsonNodeView } from '../../../json-crdt/nodes' ;
35import 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+ */
1310export 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
4653const 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);
0 commit comments