editor-manager-context-adapter.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // This value is not used in the React code. It's just here to prevent errors
  9. // from EditorProvider
  10. store.set('state.loading', false)
  11. store.set('project.name', null)
  12. store.set('editor', {
  13. showSymbolPalette: false,
  14. toggleSymbolPalette: () => {},
  15. sharejs_doc: null,
  16. open_doc_id: null,
  17. open_doc_name: null,
  18. opening: true,
  19. trackChanges: false,
  20. wantTrackChanges: false,
  21. // No Ace here
  22. newSourceEditor: true,
  23. error_state: false,
  24. })
  25. store.persisted(
  26. 'editor.showVisual',
  27. getMeta('ol-usedLatex') === 'never' || showVisualFallbackValue(projectId),
  28. `editor.lastUsedMode`,
  29. {
  30. toPersisted: showVisual => (showVisual ? 'visual' : 'code'),
  31. fromPersisted: mode => mode === 'visual',
  32. }
  33. )
  34. store.persisted(
  35. 'editor.codeEditorOpened',
  36. codeEditorOpenedFallbackValue(),
  37. 'editor.codeEditorOpened'
  38. )
  39. store.watch('editor.showVisual', showVisual => {
  40. if (store.get('editor.codeEditorOpened') !== true && showVisual === false) {
  41. store.set('editor.codeEditorOpened', true)
  42. }
  43. })
  44. }
  45. function showVisualFallbackValue(projectId: string) {
  46. const editorModeKey = `editor.mode.${projectId}`
  47. const editorModeVal = customLocalStorage.getItem(editorModeKey)
  48. if (editorModeVal) {
  49. // clean up the old key
  50. customLocalStorage.removeItem(editorModeKey)
  51. }
  52. return editorModeVal === 'rich-text'
  53. }
  54. function codeEditorOpenedFallbackValue() {
  55. const signUpDate = getMeta('ol-user').signUpDate
  56. if (
  57. typeof signUpDate === 'string' &&
  58. new Date(signUpDate) < new Date('2024-08-02')
  59. ) {
  60. // if signUpDate is before releasing "codeEditorOpened" value
  61. // it is assumed that the user has opened the code editor at some point
  62. return true
  63. }
  64. return false
  65. }