editor-manager-context-adapter.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { ReactScopeValueStore } from '@/features/ide-react/scope-value-store/react-scope-value-store'
  2. import customLocalStorage from '@/infrastructure/local-storage'
  3. import { DocumentContainer } from '@/features/ide-react/editor/document-container'
  4. export type EditorScopeValue = {
  5. showSymbolPalette: false
  6. toggleSymbolPalette: () => void
  7. sharejs_doc: DocumentContainer | null
  8. opening: boolean
  9. trackChanges: boolean
  10. wantTrackChanges: boolean
  11. showVisual: boolean
  12. error_state: boolean
  13. }
  14. export function populateEditorScope(
  15. store: ReactScopeValueStore,
  16. projectId: string
  17. ) {
  18. store.set('project.name', null)
  19. const editor: Omit<EditorScopeValue, 'showVisual'> = {
  20. showSymbolPalette: false,
  21. toggleSymbolPalette: () => {},
  22. sharejs_doc: null,
  23. opening: true,
  24. trackChanges: false,
  25. wantTrackChanges: false,
  26. error_state: false,
  27. }
  28. store.set('editor', editor)
  29. store.persisted(
  30. 'editor.showVisual',
  31. showVisualFallbackValue(projectId),
  32. `editor.lastUsedMode`,
  33. {
  34. toPersisted: showVisual => (showVisual ? 'visual' : 'code'),
  35. fromPersisted: mode => mode === 'visual',
  36. }
  37. )
  38. }
  39. function showVisualFallbackValue(projectId: string) {
  40. const editorModeKey = `editor.mode.${projectId}`
  41. const editorModeVal = customLocalStorage.getItem(editorModeKey)
  42. if (editorModeVal) {
  43. // clean up the old key
  44. customLocalStorage.removeItem(editorModeKey)
  45. }
  46. return editorModeVal === 'rich-text'
  47. }