editor-manager-context-adapter.ts 2.3 KB

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