-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
getSchemaByResolvedExtensions.ts
204 lines (179 loc) · 6.78 KB
/
getSchemaByResolvedExtensions.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {
MarkSpec, NodeSpec, Schema, TagParseRule,
} from '@tiptap/pm/model'
import { Editor, MarkConfig, NodeConfig } from '../index.js'
import { AnyConfig, Extensions } from '../types.js'
import { callOrReturn } from '../utilities/callOrReturn.js'
import { isEmptyObject } from '../utilities/isEmptyObject.js'
import { getAttributesFromExtensions } from './getAttributesFromExtensions.js'
import { getExtensionField } from './getExtensionField.js'
import { getRenderedAttributes } from './getRenderedAttributes.js'
import { injectExtensionAttributesToParseRule } from './injectExtensionAttributesToParseRule.js'
import { splitExtensions } from './splitExtensions.js'
function cleanUpSchemaItem<T>(data: T) {
return Object.fromEntries(
// @ts-ignore
Object.entries(data).filter(([key, value]) => {
if (key === 'attrs' && isEmptyObject(value as {} | undefined)) {
return false
}
return value !== null && value !== undefined
}),
) as T
}
/**
* Creates a new Prosemirror schema based on the given extensions.
* @param extensions An array of Tiptap extensions
* @param editor The editor instance
* @returns A Prosemirror schema
*/
export function getSchemaByResolvedExtensions(extensions: Extensions, editor?: Editor): Schema {
const allAttributes = getAttributesFromExtensions(extensions)
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
const topNode = nodeExtensions.find(extension => getExtensionField(extension, 'topNode'))?.name
const nodes = Object.fromEntries(
nodeExtensions.map(extension => {
const extensionAttributes = allAttributes.filter(
attribute => attribute.type === extension.name,
)
const context = {
name: extension.name,
options: extension.options,
storage: extension.storage,
editor,
}
const extraNodeFields = extensions.reduce((fields, e) => {
const extendNodeSchema = getExtensionField<AnyConfig['extendNodeSchema']>(
e,
'extendNodeSchema',
context,
)
return {
...fields,
...(extendNodeSchema ? extendNodeSchema(extension) : {}),
}
}, {})
const schema: NodeSpec = cleanUpSchemaItem({
...extraNodeFields,
content: callOrReturn(
getExtensionField<NodeConfig['content']>(extension, 'content', context),
),
marks: callOrReturn(getExtensionField<NodeConfig['marks']>(extension, 'marks', context)),
group: callOrReturn(getExtensionField<NodeConfig['group']>(extension, 'group', context)),
inline: callOrReturn(getExtensionField<NodeConfig['inline']>(extension, 'inline', context)),
atom: callOrReturn(getExtensionField<NodeConfig['atom']>(extension, 'atom', context)),
selectable: callOrReturn(
getExtensionField<NodeConfig['selectable']>(extension, 'selectable', context),
),
draggable: callOrReturn(
getExtensionField<NodeConfig['draggable']>(extension, 'draggable', context),
),
code: callOrReturn(getExtensionField<NodeConfig['code']>(extension, 'code', context)),
whitespace: callOrReturn(getExtensionField<NodeConfig['whitespace']>(extension, 'whitespace', context)),
defining: callOrReturn(
getExtensionField<NodeConfig['defining']>(extension, 'defining', context),
),
isolating: callOrReturn(
getExtensionField<NodeConfig['isolating']>(extension, 'isolating', context),
),
attrs: Object.fromEntries(
extensionAttributes.map(extensionAttribute => {
return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }]
}),
),
})
const parseHTML = callOrReturn(
getExtensionField<NodeConfig['parseHTML']>(extension, 'parseHTML', context),
)
if (parseHTML) {
schema.parseDOM = parseHTML.map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes)) as TagParseRule[]
}
const renderHTML = getExtensionField<NodeConfig['renderHTML']>(
extension,
'renderHTML',
context,
)
if (renderHTML) {
schema.toDOM = node => renderHTML({
node,
HTMLAttributes: getRenderedAttributes(node, extensionAttributes),
})
}
const renderText = getExtensionField<NodeConfig['renderText']>(
extension,
'renderText',
context,
)
if (renderText) {
schema.toText = renderText
}
return [extension.name, schema]
}),
)
const marks = Object.fromEntries(
markExtensions.map(extension => {
const extensionAttributes = allAttributes.filter(
attribute => attribute.type === extension.name,
)
const context = {
name: extension.name,
options: extension.options,
storage: extension.storage,
editor,
}
const extraMarkFields = extensions.reduce((fields, e) => {
const extendMarkSchema = getExtensionField<AnyConfig['extendMarkSchema']>(
e,
'extendMarkSchema',
context,
)
return {
...fields,
...(extendMarkSchema ? extendMarkSchema(extension) : {}),
}
}, {})
const schema: MarkSpec = cleanUpSchemaItem({
...extraMarkFields,
inclusive: callOrReturn(
getExtensionField<MarkConfig['inclusive']>(extension, 'inclusive', context),
),
excludes: callOrReturn(
getExtensionField<MarkConfig['excludes']>(extension, 'excludes', context),
),
group: callOrReturn(getExtensionField<MarkConfig['group']>(extension, 'group', context)),
spanning: callOrReturn(
getExtensionField<MarkConfig['spanning']>(extension, 'spanning', context),
),
code: callOrReturn(getExtensionField<MarkConfig['code']>(extension, 'code', context)),
attrs: Object.fromEntries(
extensionAttributes.map(extensionAttribute => {
return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }]
}),
),
})
const parseHTML = callOrReturn(
getExtensionField<MarkConfig['parseHTML']>(extension, 'parseHTML', context),
)
if (parseHTML) {
schema.parseDOM = parseHTML.map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes))
}
const renderHTML = getExtensionField<MarkConfig['renderHTML']>(
extension,
'renderHTML',
context,
)
if (renderHTML) {
schema.toDOM = mark => renderHTML({
mark,
HTMLAttributes: getRenderedAttributes(mark, extensionAttributes),
})
}
return [extension.name, schema]
}),
)
return new Schema({
topNode,
nodes,
marks,
})
}