@@ -7,7 +7,7 @@ import { LinkElement, MediaEmbedElement, TableWrapper } from '@mexit/shared'
7
7
8
8
import TagWrapper from './TagWrapper'
9
9
import BallonMarkToolbarButtons from './BalloonToolbar/EditorBalloonToolbar'
10
- import { ELEMENT_ILINK , ELEMENT_INLINE_BLOCK , ELEMENT_TAG , ELEMENT_TODO_LI } from '@mexit/core'
10
+ import { ELEMENT_ILINK , ELEMENT_INLINE_BLOCK , ELEMENT_TAG , ELEMENT_TODO_LI , NodeEditorContent } from '@mexit/core'
11
11
import Todo from '../Todo'
12
12
import { useEditorChange } from '@mexit/shared'
13
13
import { EditorStyles } from '@mexit/shared'
@@ -38,9 +38,10 @@ interface EditorProps {
38
38
readOnly ?: boolean
39
39
onChange ?: any // eslint-disable-line @typescript-eslint/no-explicit-any
40
40
autoFocus ?: boolean
41
+ onAutoSave ?: ( content : NodeEditorContent ) => void
41
42
}
42
43
43
- const Editor : React . FC < EditorProps > = ( { nodeUID, nodePath, content, readOnly, onChange, autoFocus } ) => {
44
+ const Editor : React . FC < EditorProps > = ( { nodeUID, nodePath, content, readOnly, onChange, autoFocus, onAutoSave } ) => {
44
45
const tags = useDataStore ( ( store ) => store . tags )
45
46
const addTag = useDataStore ( ( store ) => store . addTag )
46
47
const ilinks = useDataStore ( ( store ) => store . ilinks )
@@ -152,7 +153,7 @@ const Editor: React.FC<EditorProps> = ({ nodeUID, nodePath, content, readOnly, o
152
153
tag : {
153
154
cbKey : ComboboxKey . TAG ,
154
155
trigger : '#' ,
155
- data : tags . map ( ( t ) => ( { ...t , value : t . text } ) ) ,
156
+ data : tags . map ( ( t ) => ( { ...t , text : t . value } ) ) ,
156
157
icon : 'ri:hashtag'
157
158
} ,
158
159
slash_command : {
@@ -179,10 +180,24 @@ const Editor: React.FC<EditorProps> = ({ nodeUID, nodePath, content, readOnly, o
179
180
withBalloonToolbar : true
180
181
}
181
182
182
- const debounced = useDebouncedCallback ( ( value ) => {
183
+ const onDelayPerform = useDebouncedCallback ( ( value ) => {
183
184
const f = ! readOnly && typeof onChange === 'function' ? onChange : ( ) => undefined
184
185
f ( value )
185
- } , 1000 )
186
+ } , 400 )
187
+
188
+ const saveAfterDelay = useDebouncedCallback (
189
+ typeof onAutoSave === 'function' ? onAutoSave : ( ) => undefined ,
190
+ 30 * 1000 // After 30 seconds
191
+ )
192
+
193
+ const onChangeContent = ( val : NodeEditorContent ) => {
194
+ onDelayPerform ( val )
195
+
196
+ if ( onAutoSave ) {
197
+ saveAfterDelay . cancel ( )
198
+ saveAfterDelay ( val )
199
+ }
200
+ }
186
201
187
202
const comboboxConfig : ComboboxConfig = {
188
203
onChangeConfig : comboOnChangeConfig ,
@@ -199,7 +214,7 @@ const Editor: React.FC<EditorProps> = ({ nodeUID, nodePath, content, readOnly, o
199
214
} }
200
215
BalloonMarkToolbarButtons = { < BallonMarkToolbarButtons /> }
201
216
// debug
202
- onChange = { debounced }
217
+ onChange = { onChangeContent }
203
218
options = { editorOptions }
204
219
editorId = { nodeUID }
205
220
value = { content }
0 commit comments