-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseTransform.ts
300 lines (260 loc) · 9.23 KB
/
useTransform.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { useOpenToast } from '@components/toast/showOpenToasts'
import { useCreateNewNote } from '@hooks/useCreateNewNote'
import { useSnippets } from '@hooks/useSnippets'
import { useUpdater } from '@hooks/useUpdater'
import {
deleteText, getNodeEntries, getPath, getSelectionText,
insertNodes, removeNodes, TEditor, withoutNormalizing
} from '@udecode/plate'
import { convertValueToTasks } from '@utils/lib/contentConvertTask'
import genereateName from 'project-name-generator'
import { SEPARATOR } from '../../../../components/mex/Sidebar/treeUtils'
import { defaultContent } from '../../../../data/Defaults/baseData'
import { generateSnippetId, generateTempId } from '../../../../data/Defaults/idPrefixes'
import { useEditorStore } from '../../../../store/useEditorStore'
import { NodeEditorContent } from '../../../../types/Types'
import { getSlug, NODE_PATH_CHAR_LENGTH, NODE_PATH_SPACER } from '../../../../utils/lib/strings'
import { convertContentToRawText } from '../../../../utils/search/parseData'
import { ELEMENT_ILINK } from '../../ilink/defaults'
import { ILinkNode } from '../../ilink/types'
import { ELEMENT_QA_BLOCK } from '../../QABlock/createQAPlugin'
import { ELEMENT_SYNC_BLOCK } from '../../SyncBlock'
export const useTransform = () => {
// const addSnippet = useSnippetStore((s) => s.addSnippet)
// const node = useEditorStore((s) => s.node)
const { openNoteToast, openSnippetToast } = useOpenToast()
const { updateSnippet } = useSnippets()
const { createNewNote } = useCreateNewNote()
const { updater } = useUpdater()
// const { toast } = useToast()
// Checks whether a node is a flowblock
const isFlowBlock = (node: any): boolean => {
if (node.type === ELEMENT_SYNC_BLOCK) return true
if (node.children) {
if (node.children.length > 0)
return node.children.map(isFlowBlock).reduce((p: boolean, c: boolean) => p || c, false)
}
return false
}
// Checks whether current editor selection can be converted
const addQABlock = (editor: TEditor, block: { question: string; questionId: string }): boolean => {
if (!editor) return false
if (!editor?.selection) return false
const { question, questionId } = block
deleteText(editor)
// If editor selection has flowblock it is not convertable
insertNodes<any>(editor, [{ type: ELEMENT_QA_BLOCK, question, questionId, id: generateTempId(), children: [] }], {
at: editor.selection
})
}
const convertSelectionToQABlock = (editor: TEditor) => {
try {
// const selectionPath = getPath(editor, editor.selection)
const val = selectionToValue(editor)
const valText = convertContentToRawText(val)
// mog('replaceSelectionWithLink selPath', { selectionPath })
removeNodes(editor, { at: editor.selection, hanging: false })
// Transforms.liftNodes(editor, { at: editor.selection, mode: 'lowest' })
// mog('replaceSelectionWithQA ', { selectionPath, val, valText })
//
addQABlock(editor, { question: valText, questionId: generateSnippetId() })
} catch (e) {
console.error(e)
return e
}
}
const replaceSelectionWithTask = (editor: TEditor, todoVal: NodeEditorContent) => {
try {
removeNodes(editor, { at: editor.selection, mode: 'highest' })
const convertedVal = convertValueToTasks(todoVal)
// mog('replaceSelectionWithTask ', { todoVal, convertedVal })
insertNodes<any>(editor, convertedVal, {
at: editor.selection
})
// addQABlock(editor, { question: valText, questionId: generateSnippetId() })
} catch (e) {
console.error(e)
return e
}
}
// Checks whether current editor selection can be converted
const isConvertable = (editor: TEditor): boolean => {
if (!editor) return false
if (!editor?.selection) return false
// If editor selection has flowblock it is not convertable
return !Array.from(
getNodeEntries(editor, {
block: true
})
).reduce((p: boolean, [node, _path]: any) => {
// mog('isConvertable', { editor, p, node, ifb: isFlowBlock(node) })
return p || isFlowBlock(node)
}, false)
}
const replaceSelectionWithLink = (editor: TEditor, ilink: string, inline: boolean) => {
try {
const selectionPath = getPath(editor, editor.selection)
// mog('replaceSelectionWithLink selPath', { selectionPath })
if (inline) deleteText(editor)
else removeNodes(editor, { at: editor.selection, hanging: false })
// Transforms.liftNodes(editor, { at: editor.selection, mode: 'lowest' })
// mog('replaceSelectionWithLink detFrag', { selectionPath })
insertNodes<ILinkNode>(editor, [{ type: ELEMENT_ILINK, value: ilink, children: [] }], {
at: editor.selection
})
// mog('replaceSelectionWithLink insNode', { sel: editor.selection })
} catch (e) {
console.error(e)
return e
}
}
/**
* Converts selection to Value
* @param editor
*/
const selectionToValue = (editor: TEditor) => {
if (!editor.selection) return
if (!isConvertable(editor)) return
const nodes = Array.from(
getNodeEntries(editor, {
mode: 'highest',
block: true,
at: editor.selection
})
)
const value = nodes.map(([node, _path]) => {
return node
})
return value
}
/**
* Converts selection to new node
* Inserts the link of new node in place of the selection
* @param editor
*/
const selectionToNode = (editor: TEditor, title?: string) => {
if (!editor.selection) return
if (!isConvertable(editor)) return
withoutNormalizing(editor, () => {
// const selectionPath = getPath(editor, editor.selection)
const nodes = Array.from(
getNodeEntries(editor, {
mode: 'highest',
block: true,
at: editor.selection
})
)
const lowest = Array.from(
getNodeEntries(editor, {
mode: 'lowest',
block: true,
at: editor.selection
})
)
const selText = getSelectionText(editor)
const value = nodes.map(([node, _path]) => {
return node
})
const isInline = lowest.length === 1
const putContent = selText.length > NODE_PATH_CHAR_LENGTH && title !== undefined
const text = convertContentToRawText(value, NODE_PATH_SPACER)
const parentPath = useEditorStore.getState().node.path
const namespace = useEditorStore.getState().node.namespace
const childTitle = title ?? (isInline ? getSlug(selText) : getSlug(text))
const path = parentPath + SEPARATOR + childTitle
const note = createNewNote({
path,
noteContent: putContent ? value : defaultContent.content,
namespace: namespace,
noRedirect: true
})
replaceSelectionWithLink(editor, note?.nodeid, isInline)
if (note) {
openNoteToast(note.nodeid, note.path)
}
// mog('Replace Selection with node We are here', {
// lowest,
// selText,
// esl: editor.selection,
// selectionPath,
// nodes,
// value,
// text,
// path
// })
// saveData()
// mog('We are here', { esl: editor.selection, selectionPath, nodes, value, text, path })
})
}
/**
* Converts selection to new Task
* @param editor
*/
const selectionToTask = (editor: TEditor) => {
if (!editor.selection) return
if (!isConvertable(editor)) return
withoutNormalizing(editor, () => {
// const selectionPath = getPath(editor, editor.selection)
const nodes = Array.from(
getNodeEntries(editor, {
mode: 'highest',
block: true,
at: editor.selection
})
)
const value = nodes.map(([node, _path]) => {
return node
})
replaceSelectionWithTask(editor, value)
// mog('We are here', { esl: editor.selection, selectionPath, nodes, value, text, path })
})
}
/**
* Converts selection to new snippet
* Shows notification of snippet creation
* @param editor
*/
const selectionToSnippet = (editor: TEditor, title?: string) => {
if (!editor.selection) return
if (!isConvertable(editor)) return
withoutNormalizing(editor, () => {
const selectionPath = getPath(editor, editor.selection)
const nodes = Array.from(
getNodeEntries(editor, {
mode: 'highest',
block: true,
at: editor.selection
})
)
const value = nodes.map(([node, _path]) => {
return node
})
const snippetId = generateSnippetId()
const snippetTitle = title ?? genereateName().dashed
const newSnippet = {
id: snippetId,
title: snippetTitle,
content: value,
icon: 'ri:quill-pen-line'
}
updateSnippet(newSnippet)
updater()
// addSnippet()
// mog('We are here', { esl: editor.selection, selectionPath, nodes, value })
//
openSnippetToast(snippetId, snippetTitle)
// setContent(nodeid, value)
// saveData()
// mog('We are here', { esl: editor.selection, selectionPath, nodes, value, text, path })
})
}
return {
selectionToNode,
convertSelectionToQABlock,
isConvertable,
isFlowBlock,
selectionToSnippet,
selectionToTask,
selectionToValue
}
}