autocomplete-on-paste.ts 822 B

1234567891011121314151617181920212223242526
  1. import { EditorView } from '@codemirror/view'
  2. import { startCompletion } from '@codemirror/autocomplete'
  3. import { ancestorNodeOfType } from '@/features/source-editor/utils/tree-operations/ancestors'
  4. import { containsDOI } from '../utils/doi'
  5. // open autocomplete when a DOI is pasted into a \cite argument
  6. export const autocompleteOnPaste = EditorView.domEventHandlers({
  7. paste(event, view) {
  8. const text = event.clipboardData?.getData('text/plain')
  9. if (text && containsDOI(text)) {
  10. const node = ancestorNodeOfType(
  11. view.state,
  12. view.state.selection.main.from,
  13. 'BibKeyArgument',
  14. -1
  15. )
  16. if (node) {
  17. // allow time for the pasted content to appear in the state
  18. window.setTimeout(() => {
  19. startCompletion(view)
  20. })
  21. }
  22. }
  23. },
  24. })