third-party-extensions.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import type { Extension } from '@codemirror/state'
  2. import CodeMirror, { CodeMirrorVim } from './bundle'
  3. /**
  4. * A custom extension that allows additional CodeMirror extensions to be provided by external code,
  5. * e.g. browser extensions.
  6. */
  7. export const thirdPartyExtensions = (): Extension => {
  8. const extensions: Extension[] = []
  9. window.dispatchEvent(
  10. new CustomEvent('UNSTABLE_editor:extensions', {
  11. detail: { CodeMirror, CodeMirrorVim, extensions },
  12. })
  13. )
  14. Object.defineProperty(window, 'UNSTABLE_editorHelp', {
  15. writable: false,
  16. enumerable: true,
  17. value: `
  18. Listen for the UNSTABLE_editor:extensions event to add your CodeMirror 6
  19. extension(s) to the extensions array. Use the exported objects to avoid
  20. instanceof comparison errors.
  21. Open an issue on http://github.com/overleaf/overleaf if you think more
  22. should be exported.
  23. This API is **unsupported** and subject to change without warning.
  24. Example:
  25. window.addEventListener("UNSTABLE_editor:extensions", function(evt) {
  26. const { CodeMirror, extensions } = evt.detail;
  27. // CodeMirror contains exported objects from the CodeMirror instance
  28. const { EditorSelection, ViewPlugin } = CodeMirror;
  29. // ...
  30. // Any custom extensions should be pushed to the \`extensions\` array
  31. extensions.push(myCustomExtension)
  32. });`,
  33. })
  34. return extensions
  35. }