third-party-extensions.ts 1.2 KB

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