overleaf/services/web/frontend/js/features/source-editor/extensions/auto-pair.ts
Alf Eaton e025088065 Merge pull request #13241 from overleaf/ae-extensions-documentation
Add documentation for CodeMirror extensions

GitOrigin-RevId: e5f07084173f201919272f9d46dcdaef4b817874
2023-07-17 10:28:53 +00:00

26 lines
800 B
TypeScript

import { keymap } from '@codemirror/view'
import { Compartment, Prec, TransactionSpec } from '@codemirror/state'
import { closeBrackets, closeBracketsKeymap } from '@codemirror/autocomplete'
const autoPairConf = new Compartment()
export const autoPair = ({
autoPairDelimiters,
}: {
autoPairDelimiters: boolean
}) => autoPairConf.of(autoPairDelimiters ? extension : [])
export const setAutoPair = (autoPairDelimiters: boolean): TransactionSpec => {
return {
effects: autoPairConf.reconfigure(autoPairDelimiters ? extension : []),
}
}
/**
* The built-in closeBrackets extension and closeBrackets keymap.
*/
const extension = [
closeBrackets(),
// NOTE: using Prec.highest as this needs to run before the default Backspace handler
Prec.highest(keymap.of(closeBracketsKeymap)),
]