-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditor.tsx
226 lines (202 loc) · 6.77 KB
/
Editor.tsx
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
import React, { useMemo } from 'react'
import styled from 'styled-components'
import { ELEMENT_MEDIA_EMBED, ELEMENT_TABLE, ELEMENT_LINK, withProps } from '@udecode/plate'
import { useDebouncedCallback } from 'use-debounce'
import { LinkElement, MediaEmbedElement, TableWrapper } from '@mexit/shared'
import TagWrapper from './TagWrapper'
import BallonMarkToolbarButtons from './BalloonToolbar/EditorBalloonToolbar'
import { ELEMENT_ILINK, ELEMENT_INLINE_BLOCK, ELEMENT_TAG, ELEMENT_TODO_LI, NodeEditorContent } from '@mexit/core'
import Todo from '../Todo'
import { useEditorChange } from '@mexit/shared'
import { EditorStyles } from '@mexit/shared'
import { useDataStore } from '../../Stores/useDataStore'
import { ComboboxKey } from '../../Editor/Types/Combobox'
import MexEditor, { MexEditorOptions } from '../../Editor/MexEditor'
import { ComboboxConfig, ComboboxItem, ComboboxType, ComboConfigData } from '../../Editor/Types/MultiCombobox'
import { useRouting } from '../../Hooks/useRouting'
import { useSnippets } from '../../Hooks/useSnippets'
import { CategoryType, QuickLinkType } from '../../Editor/constants'
import { SlashComboboxItem } from '../../Editor/Components/SlashCommands/SlashComboboxItem'
import { TagComboboxItem } from '../../Editor/Components/Tags/TagComboboxItem'
import { QuickLinkComboboxItem } from '../../Editor/Components/QuickLink/QuickLinkComboboxItem'
import components from '../../Editor/Components/EditorPreviewComponents'
import { useNewNodes } from '../../Hooks/useNewNodes'
const EditorWrapper = styled(EditorStyles)`
flex: 1;
max-width: 800px;
margin: 1rem;
padding: 1rem;
`
interface EditorProps {
content: any[] // eslint-disable-line @typescript-eslint/no-explicit-any
nodePath?: string
nodeUID: string
readOnly?: boolean
onChange?: any // eslint-disable-line @typescript-eslint/no-explicit-any
autoFocus?: boolean
onAutoSave?: (content: NodeEditorContent) => void
}
const Editor: React.FC<EditorProps> = ({ nodeUID, nodePath, content, readOnly, onChange, autoFocus, onAutoSave }) => {
const tags = useDataStore((store) => store.tags)
const addTag = useDataStore((store) => store.addTag)
const ilinks = useDataStore((store) => store.ilinks)
const addILink = useDataStore((store) => store.addILink)
const slashCommands = useDataStore((store) => store.slashCommands)
const { getSnippetConfigs } = useSnippets()
const snippetConfigs = getSnippetConfigs()
const { params, location } = useRouting()
const ilinksForCurrentNode = useMemo(() => {
if (params.snippetid) return ilinks
return ilinks.filter((item) => item.nodeid !== nodeUID)
}, [nodeUID, ilinks])
const slashInternals = useMemo(() => {
const snippetName = (location?.state as any)?.title
if (params.snippetid && snippetName) {
return slashCommands.internal.filter((item) => snippetName !== item.text)
}
return slashCommands.internal
}, [slashCommands.internal])
const { addNodeOrNodes } = useNewNodes()
const internals: ComboboxItem[] = [
...ilinksForCurrentNode.map((l) => ({
...l,
value: l.nodeid,
text: l.path,
icon: l.icon ?? 'ri:file-list-2-line',
type: QuickLinkType.backlink
})),
...slashInternals.map((l) => ({ ...l, value: l.command, text: l.text, type: l.type }))
]
const comboOnKeyDownConfig: ComboConfigData = {
keys: {
inline_block: {
slateElementType: ELEMENT_INLINE_BLOCK,
newItemHandler: (newItem, parentId?) => {
const link = addILink({ ilink: newItem, parentId })
return link.nodeid
},
renderElement: QuickLinkComboboxItem
},
tag: {
slateElementType: ELEMENT_TAG,
newItemHandler: (newItem) => {
addTag(newItem)
return newItem
},
renderElement: TagComboboxItem
},
slash_command: {
slateElementType: 'slash_command',
newItemHandler: () => undefined,
renderElement: SlashComboboxItem
},
internal: {
slateElementType: 'internal',
newItemHandler: async (newItem, parentId?) => {
const node = await addNodeOrNodes(newItem, true, parentId)
return node.id
},
renderElement: SlashComboboxItem
}
},
internal: {
ilink: {
slateElementType: ELEMENT_ILINK,
newItemHandler: (newItem, parentId?) => {
return addNodeOrNodes(newItem, true, parentId).then((node) => {
return node.id
})
},
renderElement: QuickLinkComboboxItem
},
commands: {
...snippetConfigs
}
},
slashCommands: {
webem: {
slateElementType: ELEMENT_MEDIA_EMBED,
command: 'webem',
options: {
url: 'http://example.com/'
}
},
table: {
slateElementType: ELEMENT_TABLE,
command: 'table'
}
}
}
const comboOnChangeConfig: Record<string, ComboboxType> = {
internal: {
cbKey: ComboboxKey.INTERNAL,
trigger: '[[',
blockTrigger: ':',
data: internals,
icon: 'ri:file-list-2-line'
},
tag: {
cbKey: ComboboxKey.TAG,
trigger: '#',
data: tags.map((t) => ({ ...t, text: t.value })),
icon: 'ri:hashtag'
},
slash_command: {
cbKey: ComboboxKey.SLASH_COMMAND,
trigger: '/',
icon: 'ri:flask-line',
data: slashCommands.default.map((l) => ({ ...l, value: l.command, type: CategoryType.action, text: l.text }))
}
}
useEditorChange(nodeUID, content)
const editorOptions: MexEditorOptions = {
editableProps: {
readOnly: readOnly,
// placeholder: "Let's try something here...",
autoFocus: true
},
focusOptions: {
edge: 'start',
focus: true
},
withDraggable: false,
withBalloonToolbar: true
}
const onDelayPerform = useDebouncedCallback((value) => {
const f = !readOnly && typeof onChange === 'function' ? onChange : () => undefined
f(value)
}, 400)
const saveAfterDelay = useDebouncedCallback(
typeof onAutoSave === 'function' ? onAutoSave : () => undefined,
30 * 1000 // After 30 seconds
)
const onChangeContent = (val: NodeEditorContent) => {
onDelayPerform(val)
if (onAutoSave) {
saveAfterDelay.cancel()
saveAfterDelay(val)
}
}
const comboboxConfig: ComboboxConfig = {
onChangeConfig: comboOnChangeConfig,
onKeyDownConfig: comboOnKeyDownConfig
}
return (
<EditorWrapper>
<MexEditor
comboboxConfig={comboboxConfig}
components={components}
meta={{
path: nodePath
}}
BalloonMarkToolbarButtons={<BallonMarkToolbarButtons />}
// debug
onChange={onChangeContent}
options={editorOptions}
editorId={nodeUID}
value={content}
/>
</EditorWrapper>
)
}
export default Editor