2023-04-13 04:21:25 -04:00
|
|
|
import { keymap } from '@codemirror/view'
|
|
|
|
import { Compartment, Prec, TransactionSpec } from '@codemirror/state'
|
2023-05-03 04:24:55 -04:00
|
|
|
import { closeBrackets, closeBracketsKeymap } from '@codemirror/autocomplete'
|
2023-04-13 04:21:25 -04:00
|
|
|
|
|
|
|
const autoPairConf = new Compartment()
|
|
|
|
|
|
|
|
export const autoPair = ({
|
|
|
|
autoPairDelimiters,
|
|
|
|
}: {
|
|
|
|
autoPairDelimiters: boolean
|
2023-05-03 04:24:55 -04:00
|
|
|
}) => autoPairConf.of(autoPairDelimiters ? extension : [])
|
2023-04-13 04:21:25 -04:00
|
|
|
|
|
|
|
export const setAutoPair = (autoPairDelimiters: boolean): TransactionSpec => {
|
|
|
|
return {
|
2023-05-03 04:24:55 -04:00
|
|
|
effects: autoPairConf.reconfigure(autoPairDelimiters ? extension : []),
|
2023-04-13 04:21:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 04:35:51 -04:00
|
|
|
/**
|
|
|
|
* The built-in closeBrackets extension and closeBrackets keymap.
|
|
|
|
*/
|
2023-05-03 04:24:55 -04:00
|
|
|
const extension = [
|
|
|
|
closeBrackets(),
|
|
|
|
// NOTE: using Prec.highest as this needs to run before the default Backspace handler
|
|
|
|
Prec.highest(keymap.of(closeBracketsKeymap)),
|
|
|
|
]
|