Store editor selection on a per-user rather than per-project basis (#16246)

* Store editor selection on a per-user rather than per-project basis

* set code editor if rich_text=false

* format fix

GitOrigin-RevId: 8efc33b682de211162e674839e6b891ec04e542e
This commit is contained in:
Domagoj Kriskovic 2023-12-18 14:52:13 +01:00 committed by Copybot
parent b0028a2789
commit da2f7ff153
3 changed files with 38 additions and 12 deletions

View file

@ -1,4 +1,5 @@
import { ReactScopeValueStore } from '@/features/ide-react/scope-value-store/react-scope-value-store'
import customLocalStorage from '@/infrastructure/local-storage'
export function populateEditorScope(
store: ReactScopeValueStore,
@ -23,8 +24,25 @@ export function populateEditorScope(
newSourceEditor: true,
error_state: false,
})
store.persisted('editor.showVisual', false, `editor.mode.${projectId}`, {
toPersisted: showVisual => (showVisual ? 'rich-text' : 'source'),
fromPersisted: mode => mode === 'rich-text',
})
store.persisted(
'editor.showVisual',
showVisualFallbackValue(projectId),
`editor.lastUsedMode`,
{
toPersisted: showVisual => (showVisual ? 'visual' : 'code'),
fromPersisted: mode => mode === 'visual',
}
)
}
function showVisualFallbackValue(projectId: string) {
const editorModeKey = `editor.mode.${projectId}`
const editorModeVal = customLocalStorage.getItem(editorModeKey)
if (editorModeVal) {
// clean up the old key
customLocalStorage.removeItem(editorModeKey)
}
return editorModeVal === 'rich-text'
}

View file

@ -22,6 +22,7 @@ import './controllers/CompileButton'
import './controllers/SwitchToPDFButton'
import '../metadata/services/metadata'
import { debugConsole } from '@/utils/debugging'
import customLocalStorage from '@/infrastructure/local-storage'
let EditorManager
@ -161,10 +162,20 @@ export default EditorManager = (function () {
}
showVisual() {
return (
this.localStorage(`editor.mode.${this.$scope.project_id}`) ===
'rich-text'
)
const editorModeKey = `editor.mode.${this.$scope.project_id}`
const editorModeVal = this.localStorage(editorModeKey)
if (editorModeVal) {
// clean up the old key
customLocalStorage.removeItem(editorModeKey)
}
const lastUsedMode = this.localStorage(`editor.lastUsedMode`)
if (lastUsedMode) {
return lastUsedMode === 'visual'
} else {
return editorModeVal === 'rich-text'
}
}
autoOpenDoc() {

View file

@ -5,10 +5,7 @@ App.controller('EditorLoaderController', [
'localStorage',
function ($scope, localStorage) {
$scope.$watch('editor.showVisual', function (val) {
localStorage(
`editor.mode.${$scope.project_id}`,
val === true ? 'rich-text' : 'source'
)
localStorage(`editor.lastUsedMode`, val === true ? 'visual' : 'code')
})
},
])