overleaf/services/web/frontend/js/features/source-editor/extensions/auto-pair.ts
Alf Eaton 359255b820 [cm6] Use a modified fork of the closeBrackets extension (#12573)
GitOrigin-RevId: a24020ed216cb10defff989f5876666c29889de2
2023-04-17 08:04:43 +00:00

29 lines
801 B
TypeScript

import { keymap } from '@codemirror/view'
import { Compartment, Prec, TransactionSpec } from '@codemirror/state'
import { closeBrackets, closeBracketsKeymap } from './close-brackets'
const autoPairConf = new Compartment()
export const autoPair = ({
autoPairDelimiters,
}: {
autoPairDelimiters: boolean
}) => autoPairConf.of(createAutoPair(autoPairDelimiters))
export const setAutoPair = (autoPairDelimiters: boolean): TransactionSpec => {
return {
effects: autoPairConf.reconfigure(createAutoPair(autoPairDelimiters)),
}
}
const createAutoPair = (enabled: boolean) => {
if (!enabled) {
return []
}
return [
closeBrackets(),
// NOTE: using Prec.highest as this needs to run before the default Backspace handler
Prec.highest(keymap.of(closeBracketsKeymap)),
]
}