1
1
import { SliceBehavior , type SliceTypeCon } from '../slice/constants' ;
2
2
import { CommonSliceType } from '../slice' ;
3
+ import { SliceRegistryEntry } from './SliceRegistryEntry' ;
4
+ import { printTree } from 'tree-dump/lib/printTree' ;
3
5
import type { PeritextMlElement } from '../block/types' ;
4
- import type { NodeBuilder } from '../../../json-crdt-patch' ;
5
6
import type { JsonMlElement } from 'very-small-parser/lib/html/json-ml/types' ;
6
7
import type { FromHtmlConverter , ToHtmlConverter } from './types' ;
7
- import type { JsonNodeView } from '../../../json-crdt/nodes' ;
8
- import type { SchemaToJsonNode } from '../../../json-crdt/schema/types' ;
8
+ import type { Printable } from 'tree-dump' ;
9
9
10
- export type TagType = SliceTypeCon | number | string ;
11
-
12
- const sliceCustomData = new WeakMap < SliceRegistryEntry < any , any , any > , Record < string , unknown > > ( ) ;
13
-
14
- export class SliceRegistryEntry <
15
- Behavior extends SliceBehavior = SliceBehavior ,
16
- Tag extends TagType = TagType ,
17
- Schema extends NodeBuilder = NodeBuilder ,
18
- > {
19
- public isInline ( ) : boolean {
20
- return this . behavior !== SliceBehavior . Marker ;
21
- }
22
-
23
- public data ( ) : Record < string , unknown > {
24
- const data = sliceCustomData . get ( this ) ;
25
- if ( data ) return data ;
26
- const newData = { } ;
27
- sliceCustomData . set ( this , newData ) ;
28
- return newData ;
29
- }
30
-
31
- constructor (
32
- /**
33
- * Specifies whether the slice is an inline or block element. And if it is
34
- * an inline element, whether multiple instances of the same tag are allowed
35
- * to be applied to a range of tex - "Many", or only one instance - "One".
36
- */
37
- public readonly behavior : Behavior ,
38
-
39
- /**
40
- * The tag name of this slice. The tag is one step in the type path of the
41
- * slice. For example, below is a type path composed of three steps:
42
- *
43
- * ```js
44
- * ['ul', 'li', 'p']
45
- * ```
46
- *
47
- * Tag types are normally numbers of type {@link SliceTypeCon}, however,
48
- * they can also be any arbitrary strings or numbers.
49
- */
50
- public readonly tag : Tag ,
51
-
52
- /**
53
- * Default expected schema of the slice data.
54
- */
55
- public readonly schema : Schema ,
56
-
57
- /**
58
- * This property is relevant only for block split markers. It specifies
59
- * whether the block split marker is a container for other block elements.
60
- *
61
- * For example, a `blockquote` is a container for `paragraph` elements,
62
- * however, a `paragraph` is not a container (it can only contain inline
63
- * elements).
64
- *
65
- * If the marker slice is of the container sort, they tag can appear in the
66
- * path steps of the type:
67
- *
68
- * ```
69
- *
70
- * ```
71
- */
72
- public readonly container : boolean = false ,
73
-
74
- /**
75
- * Converts a node of this type to HTML representation: returns the HTML tag
76
- * and attributes. The method receives {@link PeritextMlElement} as an
77
- * argument, which is a tuple of internal HTML-like representation of the
78
- * node.
79
- */
80
- public readonly toHtml :
81
- | ToHtmlConverter <
82
- PeritextMlElement <
83
- Tag ,
84
- JsonNodeView < SchemaToJsonNode < Schema > > ,
85
- Behavior extends SliceBehavior . Marker ? false : true
86
- >
87
- >
88
- | undefined = void 0 ,
89
-
90
- /**
91
- * Specifies a mapping of converters from HTML {@link JsonMlElement} to
92
- * {@link PeritextMlElement}. This way a slice type can specify multiple
93
- * HTML tags that are converted to the same slice type.
94
- *
95
- * For example, both, `<b>` and `<strong>` tags can be converted to the
96
- * {@link SliceTypeCon.b} slice type.
97
- */
98
- public readonly fromHtml ?: {
99
- [ htmlTag : string ] : FromHtmlConverter <
100
- PeritextMlElement <
101
- Tag ,
102
- JsonNodeView < SchemaToJsonNode < Schema > > ,
103
- Behavior extends SliceBehavior . Marker ? false : true
104
- >
105
- > ;
106
- } ,
107
- ) { }
108
- }
10
+ export type TypeTag = SliceTypeCon | number | string ;
109
11
110
12
/**
111
13
* Slice registry contains a record of possible inline an block formatting
@@ -115,10 +17,15 @@ export class SliceRegistryEntry<
115
17
* @todo Consider moving the registry under the `/transfer` directory. Or maybe
116
18
* `/slices` directory.
117
19
*/
118
- export class SliceRegistry {
119
- private map : Map < TagType , SliceRegistryEntry > = new Map ( ) ;
20
+ export class SliceRegistry implements Printable {
21
+ private map : Map < TypeTag , SliceRegistryEntry > = new Map ( ) ;
120
22
private _fromHtml : Map < string , [ entry : SliceRegistryEntry , converter : FromHtmlConverter ] [ ] > = new Map ( ) ;
121
23
24
+ public clear ( ) : void {
25
+ this . map . clear ( ) ;
26
+ this . _fromHtml . clear ( ) ;
27
+ }
28
+
122
29
public add ( entry : SliceRegistryEntry < any , any , any > ) : void {
123
30
const { tag, fromHtml} = entry ;
124
31
this . map . set ( tag , entry ) ;
@@ -135,11 +42,11 @@ export class SliceRegistry {
135
42
if ( tagStr && typeof tagStr === 'string' ) _fromHtml . set ( tagStr , [ [ entry , ( ) => [ tag , null ] ] ] ) ;
136
43
}
137
44
138
- public get ( tag : TagType ) : SliceRegistryEntry | undefined {
45
+ public get ( tag : TypeTag ) : SliceRegistryEntry | undefined {
139
46
return this . map . get ( tag ) ;
140
47
}
141
48
142
- public isContainer ( tag : TagType ) : boolean {
49
+ public isContainer ( tag : TypeTag ) : boolean {
143
50
const entry = this . map . get ( tag ) ;
144
51
return entry ?. container ?? false ;
145
52
}
@@ -167,4 +74,10 @@ export class SliceRegistry {
167
74
}
168
75
return ;
169
76
}
77
+
78
+ /** ----------------------------------------------------- {@link Printable} */
79
+
80
+ public toString ( tab : string = '' ) : string {
81
+ return `SliceRegistry` + printTree ( tab , [ ...this . map . values ( ) ] . map ( ( entry ) => tab => entry . toString ( tab ) ) ) ;
82
+ }
170
83
}
0 commit comments