Files
overleaf-cep/services/web/frontend/js/features/source-editor/extensions/autocomplete-on-paste.ts
T
Alf Eaton 18b2621855 Cite an article by pasting the DOI (#29994)
GitOrigin-RevId: a68af023b24fcf87bbc608395f2898df8e9b41a5
2026-01-09 09:06:51 +00:00

27 lines
822 B
TypeScript

import { EditorView } from '@codemirror/view'
import { startCompletion } from '@codemirror/autocomplete'
import { ancestorNodeOfType } from '@/features/source-editor/utils/tree-operations/ancestors'
import { containsDOI } from '../utils/doi'
// open autocomplete when a DOI is pasted into a \cite argument
export const autocompleteOnPaste = EditorView.domEventHandlers({
paste(event, view) {
const text = event.clipboardData?.getData('text/plain')
if (text && containsDOI(text)) {
const node = ancestorNodeOfType(
view.state,
view.state.selection.main.from,
'BibKeyArgument',
-1
)
if (node) {
// allow time for the pasted content to appear in the state
window.setTimeout(() => {
startCompletion(view)
})
}
}
},
})