-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializer.ts
173 lines (152 loc) · 4.07 KB
/
serializer.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
import {
directKeys,
DirectProperties,
directPropertyKeys,
ElementHighlightMetadata2,
extractMetadata,
generateTempId,
mappedKeys,
PartialBy
} from '@mexit/core'
import { useAuthStore } from '../Stores/useAuth'
// From content to api
export const serializeContent = (content: any[], nodeid: string, elementMetadata?: ElementHighlightMetadata2) => {
return content.map((el) => {
if (Object.keys(serializeSpecial).includes(el.type)) {
return serializeSpecial[el.type](el, nodeid)
}
const nl: any = {}
const directProperties: DirectProperties = {}
if (el.id) {
nl.id = el.id
} else {
nl.id = generateTempId()
}
if (elementMetadata && el?.highlight) {
nl.elementMetadata = elementMetadata
delete el['highlight']
} else if (el?.metadata) {
Object.keys(el.metadata).forEach((k) => {
nl[k] = el.metadata[k]
})
}
if (el.type) {
if (el.type !== 'paragraph') {
nl.elementType = el.type
}
}
Object.keys(el).forEach((k) => {
if (directPropertyKeys.includes(k)) {
directProperties[k] = el[k]
} else if (directKeys.includes(k)) {
nl[k] = el[k]
} else if (mappedKeys[k] !== undefined) {
nl[mappedKeys[k]] = el[k]
}
})
const nlproperties = {
...directProperties,
...el.properties
}
if (Object.keys(nlproperties).length > 0) {
nl.properties = nlproperties
}
if (el.children) {
nl.children = serializeContent(el.children, nodeid, elementMetadata)
}
return nl
})
}
export const serializeSpecial: { [elementType: string]: (element: any, nodeid: string) => any } = {
ilink: (el: any, nodeid: string) => {
const workspaceDetails = useAuthStore.getState().workspaceDetails
if (el.blockId)
return {
elementType: 'blockILink',
blockID: el.blockId,
blockAlias: el.blockValue,
nodeID: el.value,
id: el.id ?? generateTempId(),
workspaceID: workspaceDetails.id
}
else
return {
elementType: 'nodeILink',
nodeID: el.value,
id: el.id ?? generateTempId(),
workspaceID: workspaceDetails.id
}
},
a: (el: any, nodeid: string) => {
return {
elementType: 'webLink',
url: el.url,
id: el.id ?? generateTempId(),
children: serializeContent(el.children ?? [], nodeid)
}
}
}
export const deserializeSpecial: { [elementType: string]: (element: any) => any } = {
nodeILink: (el: any) => {
return {
type: 'ilink',
value: el.nodeID,
id: el.id,
children: [{ text: '', id: generateTempId() }]
}
},
blockILink: (el: any) => {
return {
type: 'ilink',
value: el.nodeID,
blockId: el.blockID,
blockValue: el.blockAlias,
id: el.id,
children: [{ text: '', id: generateTempId() }]
}
},
webLink: (el: any) => {
return {
type: 'a',
url: el.url,
id: el.id,
children: deserializeContent(el.children) ?? [{ text: '', id: generateTempId() }]
}
}
}
// From API to content
export const deserializeContent = (sanatizedContent: any[]) => {
return sanatizedContent?.map((el) => {
if (Object.keys(deserializeSpecial).includes(el.elementType)) {
const dEl = deserializeSpecial[el.elementType](el)
return dEl
}
const nl: any = {}
if (el.elementType !== 'paragraph' && el.elementType !== undefined) {
nl.type = el.elementType
}
if (el.id !== undefined) {
nl.id = el.id
}
nl.metadata = extractMetadata(el)
// Properties
if (el.properties) {
const elProps = { ...el.properties }
Object.keys(el.properties).forEach((k) => {
if (directPropertyKeys.includes(k)) {
nl[k] = el.properties[k]
delete elProps[k]
}
})
if (Object.keys(elProps).length > 0) {
nl.properties = elProps
}
}
if (el.children && el.children.length > 0) {
nl.children = deserializeContent(el.children ?? [])
} else {
nl.text = el.content
}
return nl
})
}