mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-04 18:54:23 -05:00
e025088065
Add documentation for CodeMirror extensions GitOrigin-RevId: e5f07084173f201919272f9d46dcdaef4b817874
42 lines
1 KiB
TypeScript
42 lines
1 KiB
TypeScript
import { ViewPlugin } from '@codemirror/view'
|
|
import { EditorSelection } from '@codemirror/state'
|
|
|
|
/**
|
|
* A custom extension that listens for an `editor:insert-symbol` event and inserts the given content into the document.
|
|
*/
|
|
export const symbolPalette = () => {
|
|
return ViewPlugin.define(view => {
|
|
const listener = (event: Event) => {
|
|
const symbol = (event as CustomEvent<{ command: string }>).detail
|
|
|
|
view.focus()
|
|
|
|
const spec = view.state.changeByRange(range => {
|
|
const changeSet = view.state.changes([
|
|
{
|
|
from: range.from,
|
|
to: range.to,
|
|
insert: symbol.command,
|
|
},
|
|
])
|
|
|
|
return {
|
|
range: EditorSelection.cursor(changeSet.mapPos(range.to, 1)),
|
|
changes: changeSet,
|
|
}
|
|
})
|
|
|
|
view.dispatch(spec, {
|
|
scrollIntoView: true,
|
|
})
|
|
}
|
|
|
|
window.addEventListener('editor:insert-symbol', listener)
|
|
|
|
return {
|
|
destroy() {
|
|
window.removeEventListener('editor:insert-symbol', listener)
|
|
},
|
|
}
|
|
})
|
|
}
|