-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditor.tsx
193 lines (168 loc) · 5.89 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
import React, { useEffect, useMemo } from 'react'
import { useGlobalListener } from '@hooks/useGlobalListener'
import useMultipleEditors from '@store/useEditorsStore'
import useSuggestionStore from '@store/useSuggestionStore'
import { getPlateEditorRef, Plate, selectEditor, usePlateEditorRef } from '@udecode/plate'
import { debounce } from 'lodash'
import { useContextMenu } from 'react-contexify'
import { DndProvider } from 'react-dnd'
import { HTML5Backend } from 'react-dnd-html5-backend'
import { useDebouncedCallback } from 'use-debounce'
import { useEditorChange } from '../hooks/useEditorActions'
import { useGraphStore } from '../store/useGraphStore'
import { EditorStyles } from '../style/Editor'
import { NodeEditorContent } from '../types/Types'
import { mog } from '../utils/lib/helper'
import { useBlockHighlightStore, useFocusBlock } from './Actions/useFocusBlock'
import BallonMarkToolbarButtons from './Components/EditorBalloonToolbar'
import { MENU_ID } from './Components/EditorContextMenu'
import { ComboboxOptions } from './Components/combobox/components/Combobox.types'
import components from './Components/components'
import { MultiComboboxContainer } from './Components/multi-combobox/multiComboboxContainer'
import generatePlugins, { PluginOptionType } from './Plugins/plugins'
import useEditorPluginConfig from './Plugins/useEditorPluginConfig'
interface EditorProps {
content: any[] // eslint-disable-line @typescript-eslint/no-explicit-any
editorId: string
readOnly?: boolean
onChange?: any
autoFocus?: boolean
comboboxOptions?: ComboboxOptions
focusAtBeginning?: boolean
showBalloonToolbar?: boolean
onAutoSave?: (content: NodeEditorContent) => void
padding?: string
options?: PluginOptionType
getSuggestions?: any
}
// High performance guaranteed
export const Editor = ({
content,
editorId,
autoFocus = true,
options,
readOnly = false,
onChange,
onAutoSave,
padding = '32px',
focusAtBeginning = true,
comboboxOptions = {
showPreview: true
},
showBalloonToolbar = false,
getSuggestions
}: EditorProps) => {
const editableProps = {
spellCheck: false,
autoFocus,
style: {
padding
},
readOnly
}
const setNodePreview = useGraphStore((store) => store.setNodePreview)
const headingQASearch = useSuggestionStore((store) => store.headingQASearch)
const isEmpty = useMultipleEditors((store) => store.isEmpty)
const editorRef = usePlateEditorRef()
const { show } = useContextMenu({ id: MENU_ID })
const { focusBlock } = useFocusBlock()
const clearHighlights = useBlockHighlightStore((store) => store.clearAllHighlightedBlockIds)
const hightlightedBlockIds = useBlockHighlightStore((store) => store.hightlighted.editor)
useEffect(() => {
const hightlightedBlockIds = useBlockHighlightStore.getState().hightlighted.editor
if (editorRef && hightlightedBlockIds.length > 0) {
focusBlock(hightlightedBlockIds[hightlightedBlockIds.length - 1], editorId)
return
}
if (editorRef && focusAtBeginning) {
selectEditor(editorRef, { edge: 'start', focus: true })
}
}, [editorRef, editorId, focusAtBeginning]) // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
// Clear highlights when highlightedBlockIds present
if (hightlightedBlockIds.length > 0) {
focusBlock(hightlightedBlockIds[hightlightedBlockIds.length - 1], editorId)
const clearHighlightTimeoutId = setTimeout(() => {
// mog('clearing highlights')
if (!readOnly) clearHighlights()
}, 2000)
return () => clearTimeout(clearHighlightTimeoutId)
}
}, [hightlightedBlockIds, editorId, editorRef])
const { pluginConfigs, comboConfigData } = useEditorPluginConfig(editorId, options)
const prePlugins = useMemo(() => generatePlugins(components, options), [])
const plugins = [
...prePlugins,
{
key: 'MULTI_COMBOBOX',
handlers: {
onContextMenu: () => (ev) => {
show(ev)
},
onChange: pluginConfigs.combobox.onChange,
onKeyDown: pluginConfigs.combobox.onKeyDown
}
}
]
useEditorChange(editorId, content, onChange)
const onDelayPerform = debounce(!readOnly && typeof onChange === 'function' ? onChange : () => undefined, 200)
// * Get suggestions after 1.5 secs
const getDebouncedSuggestions = useDebouncedCallback(
typeof getSuggestions === 'function' ? getSuggestions : () => undefined,
1500
)
const saveAfterDelay = useDebouncedCallback(
typeof onAutoSave === 'function' ? onAutoSave : () => undefined,
30 * 1000 // After 30 secons
)
const delayedHighlightRemoval = debounce(clearHighlights, 2000)
const onChangeContent = (val: any[]) => {
onDelayPerform(val)
if (getSuggestions && !headingQASearch) {
getDebouncedSuggestions.cancel()
getDebouncedSuggestions(val)
}
if (onAutoSave) {
saveAfterDelay.cancel()
saveAfterDelay(val)
}
}
return (
<>
<EditorStyles
readOnly={readOnly}
onClick={(ev) => {
ev.stopPropagation()
setNodePreview(false)
}}
data-tour="mex-onboarding-draft-editor"
>
<Plate
id={editorId}
editableProps={editableProps}
initialValue={content}
plugins={plugins}
onChange={onChangeContent}
>
{showBalloonToolbar && <BallonMarkToolbarButtons />}
{isEmpty && <MultiComboboxContainer options={comboboxOptions} config={comboConfigData} />}
<GlobalEditorListener />
</Plate>
</EditorStyles>
</>
)
}
const withDndProvider = (Component: any) => {
const DndDefaultEditor = (props: EditorProps) => (
<DndProvider backend={HTML5Backend}>
<Component {...props} />
</DndProvider>
)
return DndDefaultEditor
}
const GlobalEditorListener = () => {
useGlobalListener()
return null
}
withDndProvider.displayName = 'DefaultEditor'
export default withDndProvider(Editor)