editor-manager-context-adapter.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { ReactScopeValueStore } from '@/features/ide-react/scope-value-store/react-scope-value-store'
  2. import customLocalStorage from '@/infrastructure/local-storage'
  3. import getMeta from '@/utils/meta'
  4. export function populateEditorScope(
  5. store: ReactScopeValueStore,
  6. projectId: string
  7. ) {
  8. store.set('project.name', null)
  9. store.set('editor', {
  10. showSymbolPalette: false,
  11. toggleSymbolPalette: () => {},
  12. sharejs_doc: null,
  13. open_doc_id: null,
  14. open_doc_name: null,
  15. opening: true,
  16. trackChanges: false,
  17. wantTrackChanges: false,
  18. // No Ace here
  19. newSourceEditor: true,
  20. error_state: false,
  21. })
  22. store.persisted(
  23. 'editor.showVisual',
  24. getMeta('ol-usedLatex') === 'never' || showVisualFallbackValue(projectId),
  25. `editor.lastUsedMode`,
  26. {
  27. toPersisted: showVisual => (showVisual ? 'visual' : 'code'),
  28. fromPersisted: mode => mode === 'visual',
  29. }
  30. )
  31. store.persisted(
  32. 'editor.codeEditorOpened',
  33. codeEditorOpenedFallbackValue(),
  34. 'editor.codeEditorOpened'
  35. )
  36. store.watch('editor.showVisual', showVisual => {
  37. if (store.get('editor.codeEditorOpened') !== true && showVisual === false) {
  38. store.set('editor.codeEditorOpened', true)
  39. }
  40. })
  41. }
  42. function showVisualFallbackValue(projectId: string) {
  43. const editorModeKey = `editor.mode.${projectId}`
  44. const editorModeVal = customLocalStorage.getItem(editorModeKey)
  45. if (editorModeVal) {
  46. // clean up the old key
  47. customLocalStorage.removeItem(editorModeKey)
  48. }
  49. return editorModeVal === 'rich-text'
  50. }
  51. function codeEditorOpenedFallbackValue() {
  52. const signUpDate = getMeta('ol-user').signUpDate
  53. if (
  54. typeof signUpDate === 'string' &&
  55. new Date(signUpDate) < new Date('2024-08-02')
  56. ) {
  57. // if signUpDate is before releasing "codeEditorOpened" value
  58. // it is assumed that the user has opened the code editor at some point
  59. return true
  60. }
  61. return false
  62. }