/
DomToModelSettings.ts
150 lines (131 loc) · 4.72 KB
/
DomToModelSettings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import type { ContentModelSegmentFormat } from '../format/ContentModelSegmentFormat';
import type { ContentModelFormatBase } from '../format/ContentModelFormatBase';
import type { ContentModelFormatMap } from '../format/ContentModelFormatMap';
import type { DomToModelContext } from './DomToModelContext';
import type { ElementProcessor } from './ElementProcessor';
import type { FormatHandlerTypeMap, FormatKey } from '../format/FormatHandlerTypeMap';
/**
* A type of Default style map, from tag name string (in upper case) to a static style object
*/
export type DefaultStyleMap = {
[key in keyof HTMLElementDeprecatedTagNameMap]?: Readonly<Partial<CSSStyleDeclaration>>;
} &
{
[key in keyof HTMLElementTagNameMap]?: Readonly<Partial<CSSStyleDeclaration>>;
} & {
// Workaround typescript 4.4.4 which does not have these elements in its declaration file
center?: Partial<CSSStyleDeclaration>;
strike?: Partial<CSSStyleDeclaration>;
};
/**
* Parse format from the given HTML element and default style
* @param format The format object to parse into
* @param element The HTML element to parse format from
* @param context The context object that provide related context information
* @param defaultStyle Default CSS style of the given HTML element
*/
export type FormatParser<TFormat extends ContentModelFormatBase> = (
format: TFormat,
element: HTMLElement,
context: DomToModelContext,
defaultStyle: Readonly<Partial<CSSStyleDeclaration>>
) => void;
/**
* Parse format from the given text node
* @param format The format object to parse into
* @param textNode The text node to parse format from
* @param context The context object that provide related context information
*/
export type TextFormatParser<
TFormat extends ContentModelSegmentFormat = ContentModelSegmentFormat
> = (format: TFormat, textNode: Text, context: DomToModelContext) => void;
/**
* All format parsers
*/
export type FormatParsers = {
[Key in FormatKey]: FormatParser<FormatHandlerTypeMap[Key]> | null;
};
/**
* A map from format parser category name to an array of parsers. This is for HTML Element only
*/
export type ElementFormatParserPerCategory = {
[Key in keyof ContentModelFormatMap]: (FormatParser<ContentModelFormatMap[Key]> | null)[];
};
/**
* A map from format parser category name to an array of parsers
*/
export type FormatParsersPerCategory = ElementFormatParserPerCategory & {
text: TextFormatParser[];
};
/**
* A map from element processor name to its processor type
*/
export type ElementProcessorMap = {
[key in keyof HTMLElementDeprecatedTagNameMap]?: ElementProcessor<
HTMLElementDeprecatedTagNameMap[key]
>;
} &
{
[key in keyof HTMLElementTagNameMap]?: ElementProcessor<HTMLElementTagNameMap[key]>;
} & {
/**
* Processors for all other HTML elements
*/
'*': ElementProcessor<HTMLElement>;
/**
* Processor for text node
*/
'#text': ElementProcessor<Text>;
/**
* Processor for text node with selection.
* This is an internal processor used by #text processor
*/
textWithSelection: ElementProcessor<Text>;
/**
* Processor for entity
*/
entity: ElementProcessor<HTMLElement>;
/**
* Common processor dispatch for all elements
*/
element: ElementProcessor<HTMLElement>;
/**
* Common processor for child nodes of a given element
*/
child: ElementProcessor<ParentNode>;
/**
* Workaround for typescript 4.4.4 that doesn't have element "strike" in its element type
*/
strike?: ElementProcessor<HTMLElement>;
/**
* Workaround for typescript 4.4.4 that doesn't have element "center" in its element type
*/
center?: ElementProcessor<HTMLElement>;
/**
* Processor for Inline Readonly Delimiters
*/
delimiter?: ElementProcessor<Node>;
};
/**
* Represents settings to customize DOM to Content Model conversion
*/
export interface DomToModelSettings {
/**
* Map of element processors
*/
elementProcessors: ElementProcessorMap;
/**
* Map of format parsers
*/
formatParsers: FormatParsersPerCategory;
/**
* Default DOM to Content Model processors before overriding.
* This provides a way to call original processor from an overridden processor function
*/
defaultElementProcessors: Readonly<ElementProcessorMap>;
/**
* Default format parsers before overriding.
* This provides a way to call original format parser from an overridden parser function
*/
defaultFormatParsers: Readonly<FormatParsers>;
}