editor-manager-context-adapter.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. open_doc_id: string | null
  9. open_doc_name: string | null
  10. opening: boolean
  11. trackChanges: boolean
  12. wantTrackChanges: boolean
  13. showVisual: boolean
  14. error_state: boolean
  15. }
  16. export function populateEditorScope(
  17. store: ReactScopeValueStore,
  18. projectId: string
  19. ) {
  20. store.set('project.name', null)
  21. const editor: Omit<EditorScopeValue, 'showVisual'> = {
  22. showSymbolPalette: false,
  23. toggleSymbolPalette: () => {},
  24. sharejs_doc: null,
  25. open_doc_id: null,
  26. open_doc_name: null,
  27. opening: true,
  28. trackChanges: false,
  29. wantTrackChanges: false,
  30. error_state: false,
  31. }
  32. store.set('editor', editor)
  33. store.persisted(
  34. 'editor.showVisual',
  35. showVisualFallbackValue(projectId),
  36. `editor.lastUsedMode`,
  37. {
  38. toPersisted: showVisual => (showVisual ? 'visual' : 'code'),
  39. fromPersisted: mode => mode === 'visual',
  40. }
  41. )
  42. }
  43. function showVisualFallbackValue(projectId: string) {
  44. const editorModeKey = `editor.mode.${projectId}`
  45. const editorModeVal = customLocalStorage.getItem(editorModeKey)
  46. if (editorModeVal) {
  47. // clean up the old key
  48. customLocalStorage.removeItem(editorModeKey)
  49. }
  50. return editorModeVal === 'rich-text'
  51. }