visual-editor.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
  2. import { isValidTeXFile } from '../../../main/is-valid-tex-file'
  3. const visualEditorProviders = importOverleafModules('visualEditorProviders')
  4. const cmVisualEditorProviders = importOverleafModules(
  5. 'sourceEditorVisualExtensions'
  6. )
  7. /**
  8. * This currently covers LaTeX and Markdown. Other file
  9. * types (e.g. .bib) use module-provided visual editors that render their own
  10. * component instead, so they must NOT enable the CodeMirror visual extensions.
  11. */
  12. export function isCmVisualEditorAvailable(filename: string): boolean {
  13. if (isValidTeXFile(filename)) {
  14. return true
  15. }
  16. for (const provider of cmVisualEditorProviders) {
  17. if (provider.import.isCmVisualEditorFile?.(filename)) {
  18. return true
  19. }
  20. }
  21. return false
  22. }
  23. /**
  24. * Whether any visual editor exists for the file, including module-provided
  25. * ones. Drives the editor toggle UI and the default editor mode for a file.
  26. */
  27. export function isVisualEditorAvailable(filename: string): boolean {
  28. if (isCmVisualEditorAvailable(filename)) {
  29. return true
  30. }
  31. // Visual editors provided by modules
  32. for (const provider of visualEditorProviders) {
  33. if (provider.import.isVisualEditorAvailable(filename)) {
  34. return true
  35. }
  36. }
  37. return false
  38. }
  39. export function getVisualEditorComponent(filename: string) {
  40. for (const provider of visualEditorProviders) {
  41. const component = provider.import.getVisualEditorComponent(filename)
  42. if (component != null) {
  43. return component
  44. }
  45. }
  46. return null
  47. }
  48. export function getVisualEditorStorageKey(filename: string): string {
  49. for (const provider of visualEditorProviders) {
  50. if (provider.import.isVisualEditorAvailable(filename)) {
  51. const id = provider.import.id
  52. return id != null ? `editor.lastUsedMode.${id}` : 'editor.lastUsedMode'
  53. }
  54. }
  55. return 'editor.lastUsedMode'
  56. }