@tidbcloud/codemirror-extension-save-helper
A codemirror extension listens the editor doc changes, if changes, it will call the save handler in delay time, or, you can press the defined hotkey (default is Mod-s
) to call save handler immediately.
Installation
npm install @tidbcloud/codemirror-extension-save-helper
You need to install its peer dependencies as well:
npm install @codemirror/view @codemirror/state
Usage
import { EditorView } from '@codemirror/view'
import { saveHelper } from '@tidbcloud/codemirror-extension-save-helper'
// add extensions to your editor created by getting started
const extensions = [
// ...other extensions,
saveHelper({
save: (view: EditorView) => {
// call save file api
// saveFile(view.state.doc.toString())
console.log(view.state.doc.toString())
}
})
]
API
type SaveHelperOptions = {
// in milliseconds, default 5000
delay?: number
// default is true
auto?: boolean
// default is Mod-s
hotkey?: string
/* save handler */
save: (view: EditorView) => void
}
function saveHelper(options: SaveHelperOptions): Extension
Try it
You can try all the extensions playground at here & the save helper extension at here.