fix: Pane visibility settings will be persisted locally (#2888)
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
import { EditorFontSize, EditorLineHeight, EditorLineWidth } from '@standardnotes/models'
|
import { EditorFontSize, EditorLineHeight, EditorLineWidth } from '@standardnotes/models'
|
||||||
|
import { NativeFeatureIdentifier } from '@standardnotes/features'
|
||||||
|
|
||||||
export enum LocalPrefKey {
|
export enum LocalPrefKey {
|
||||||
|
ListPaneCollapsed = 'listPaneCollapsed',
|
||||||
|
NavigationPaneCollapsed = 'navigationPaneCollapsed',
|
||||||
ActiveThemes = 'activeThemes',
|
ActiveThemes = 'activeThemes',
|
||||||
UseSystemColorScheme = 'useSystemColorScheme',
|
UseSystemColorScheme = 'useSystemColorScheme',
|
||||||
UseTranslucentUI = 'useTranslucentUI',
|
UseTranslucentUI = 'useTranslucentUI',
|
||||||
@@ -14,6 +17,8 @@ export enum LocalPrefKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type LocalPrefValue = {
|
export type LocalPrefValue = {
|
||||||
|
[LocalPrefKey.ListPaneCollapsed]: boolean
|
||||||
|
[LocalPrefKey.NavigationPaneCollapsed]: boolean
|
||||||
[LocalPrefKey.ActiveThemes]: string[]
|
[LocalPrefKey.ActiveThemes]: string[]
|
||||||
[LocalPrefKey.UseSystemColorScheme]: boolean
|
[LocalPrefKey.UseSystemColorScheme]: boolean
|
||||||
[LocalPrefKey.UseTranslucentUI]: boolean
|
[LocalPrefKey.UseTranslucentUI]: boolean
|
||||||
@@ -25,3 +30,20 @@ export type LocalPrefValue = {
|
|||||||
[LocalPrefKey.EditorLineWidth]: EditorLineWidth
|
[LocalPrefKey.EditorLineWidth]: EditorLineWidth
|
||||||
[LocalPrefKey.EditorFontSize]: EditorFontSize
|
[LocalPrefKey.EditorFontSize]: EditorFontSize
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const LocalPrefDefaults = {
|
||||||
|
[LocalPrefKey.ListPaneCollapsed]: false,
|
||||||
|
[LocalPrefKey.NavigationPaneCollapsed]: false,
|
||||||
|
[LocalPrefKey.ActiveThemes]: [],
|
||||||
|
[LocalPrefKey.UseSystemColorScheme]: false,
|
||||||
|
[LocalPrefKey.UseTranslucentUI]: true,
|
||||||
|
[LocalPrefKey.AutoLightThemeIdentifier]: 'Default',
|
||||||
|
[LocalPrefKey.AutoDarkThemeIdentifier]: NativeFeatureIdentifier.TYPES.DarkTheme,
|
||||||
|
|
||||||
|
[LocalPrefKey.EditorMonospaceEnabled]: false,
|
||||||
|
[LocalPrefKey.EditorLineHeight]: EditorLineHeight.Normal,
|
||||||
|
[LocalPrefKey.EditorLineWidth]: EditorLineWidth.FullWidth,
|
||||||
|
[LocalPrefKey.EditorFontSize]: EditorFontSize.Normal,
|
||||||
|
} satisfies {
|
||||||
|
[key in LocalPrefKey]: LocalPrefValue[key]
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import { PanesForLayout } from './../../Application/UseCase/PanesForLayout'
|
|||||||
import {
|
import {
|
||||||
InternalEventHandlerInterface,
|
InternalEventHandlerInterface,
|
||||||
InternalEventInterface,
|
InternalEventInterface,
|
||||||
|
LocalPrefDefaults,
|
||||||
|
LocalPrefKey,
|
||||||
PreferenceServiceInterface,
|
PreferenceServiceInterface,
|
||||||
} from '@standardnotes/services'
|
} from '@standardnotes/services'
|
||||||
import {
|
import {
|
||||||
@@ -41,9 +43,16 @@ export class PaneController extends AbstractViewController implements InternalEv
|
|||||||
currentNavPanelWidth = 0
|
currentNavPanelWidth = 0
|
||||||
currentItemsPanelWidth = 0
|
currentItemsPanelWidth = 0
|
||||||
focusModeEnabled = false
|
focusModeEnabled = false
|
||||||
|
hasPaneInitializationLogicRun = false
|
||||||
|
|
||||||
listPaneExplicitelyCollapsed = false
|
listPaneExplicitelyCollapsed = this.preferences.getLocalValue(
|
||||||
navigationPaneExplicitelyCollapsed = false
|
LocalPrefKey.ListPaneCollapsed,
|
||||||
|
LocalPrefDefaults[LocalPrefKey.ListPaneCollapsed],
|
||||||
|
)
|
||||||
|
navigationPaneExplicitelyCollapsed = this.preferences.getLocalValue(
|
||||||
|
LocalPrefKey.NavigationPaneCollapsed,
|
||||||
|
LocalPrefDefaults[LocalPrefKey.NavigationPaneCollapsed],
|
||||||
|
)
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private preferences: PreferenceServiceInterface,
|
private preferences: PreferenceServiceInterface,
|
||||||
@@ -84,12 +93,6 @@ export class PaneController extends AbstractViewController implements InternalEv
|
|||||||
this.setCurrentNavPanelWidth(preferences.getValue(PrefKey.TagsPanelWidth, MinimumNavPanelWidth))
|
this.setCurrentNavPanelWidth(preferences.getValue(PrefKey.TagsPanelWidth, MinimumNavPanelWidth))
|
||||||
this.setCurrentItemsPanelWidth(preferences.getValue(PrefKey.NotesPanelWidth, MinimumNotesPanelWidth))
|
this.setCurrentItemsPanelWidth(preferences.getValue(PrefKey.NotesPanelWidth, MinimumNotesPanelWidth))
|
||||||
|
|
||||||
const screen = this._isTabletOrMobileScreen.execute().getValue()
|
|
||||||
|
|
||||||
this.panes = screen.isTabletOrMobile
|
|
||||||
? [AppPaneId.Navigation, AppPaneId.Items]
|
|
||||||
: [AppPaneId.Navigation, AppPaneId.Items, AppPaneId.Editor]
|
|
||||||
|
|
||||||
const mediaQuery = window.matchMedia(MediaQueryBreakpoints.md)
|
const mediaQuery = window.matchMedia(MediaQueryBreakpoints.md)
|
||||||
if (mediaQuery?.addEventListener != undefined) {
|
if (mediaQuery?.addEventListener != undefined) {
|
||||||
mediaQuery.addEventListener('change', this.mediumScreenMQHandler)
|
mediaQuery.addEventListener('change', this.mediumScreenMQHandler)
|
||||||
@@ -98,6 +101,7 @@ export class PaneController extends AbstractViewController implements InternalEv
|
|||||||
}
|
}
|
||||||
|
|
||||||
eventBus.addEventHandler(this, ApplicationEvent.PreferencesChanged)
|
eventBus.addEventHandler(this, ApplicationEvent.PreferencesChanged)
|
||||||
|
eventBus.addEventHandler(this, ApplicationEvent.LocalPreferencesChanged)
|
||||||
|
|
||||||
this.disposers.push(
|
this.disposers.push(
|
||||||
keyboardService.addCommandHandler({
|
keyboardService.addCommandHandler({
|
||||||
@@ -136,6 +140,34 @@ export class PaneController extends AbstractViewController implements InternalEv
|
|||||||
this.setCurrentNavPanelWidth(this.preferences.getValue(PrefKey.TagsPanelWidth, MinimumNavPanelWidth))
|
this.setCurrentNavPanelWidth(this.preferences.getValue(PrefKey.TagsPanelWidth, MinimumNavPanelWidth))
|
||||||
this.setCurrentItemsPanelWidth(this.preferences.getValue(PrefKey.NotesPanelWidth, MinimumNotesPanelWidth))
|
this.setCurrentItemsPanelWidth(this.preferences.getValue(PrefKey.NotesPanelWidth, MinimumNotesPanelWidth))
|
||||||
}
|
}
|
||||||
|
if (event.type === ApplicationEvent.LocalPreferencesChanged) {
|
||||||
|
this.listPaneExplicitelyCollapsed = this.preferences.getLocalValue(
|
||||||
|
LocalPrefKey.ListPaneCollapsed,
|
||||||
|
LocalPrefDefaults[LocalPrefKey.ListPaneCollapsed],
|
||||||
|
)
|
||||||
|
this.navigationPaneExplicitelyCollapsed = this.preferences.getLocalValue(
|
||||||
|
LocalPrefKey.NavigationPaneCollapsed,
|
||||||
|
LocalPrefDefaults[LocalPrefKey.NavigationPaneCollapsed],
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!this.hasPaneInitializationLogicRun) {
|
||||||
|
const screen = this._isTabletOrMobileScreen.execute().getValue()
|
||||||
|
if (screen.isTabletOrMobile) {
|
||||||
|
this.panes = [AppPaneId.Navigation, AppPaneId.Items]
|
||||||
|
} else {
|
||||||
|
if (!this.listPaneExplicitelyCollapsed && !this.navigationPaneExplicitelyCollapsed) {
|
||||||
|
this.panes = [AppPaneId.Navigation, AppPaneId.Items, AppPaneId.Editor]
|
||||||
|
} else if (this.listPaneExplicitelyCollapsed && this.navigationPaneExplicitelyCollapsed) {
|
||||||
|
this.panes = [AppPaneId.Editor]
|
||||||
|
} else if (this.listPaneExplicitelyCollapsed) {
|
||||||
|
this.panes = [AppPaneId.Navigation, AppPaneId.Editor]
|
||||||
|
} else {
|
||||||
|
this.panes = [AppPaneId.Items, AppPaneId.Editor]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.hasPaneInitializationLogicRun = true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setCurrentNavPanelWidth(width: number) {
|
setCurrentNavPanelWidth(width: number) {
|
||||||
@@ -250,24 +282,24 @@ export class PaneController extends AbstractViewController implements InternalEv
|
|||||||
toggleListPane = () => {
|
toggleListPane = () => {
|
||||||
if (this.panes.includes(AppPaneId.Items)) {
|
if (this.panes.includes(AppPaneId.Items)) {
|
||||||
this.removePane(AppPaneId.Items)
|
this.removePane(AppPaneId.Items)
|
||||||
this.listPaneExplicitelyCollapsed = true
|
this.preferences.setLocalValue(LocalPrefKey.ListPaneCollapsed, true)
|
||||||
} else {
|
} else {
|
||||||
if (this.panes.includes(AppPaneId.Navigation)) {
|
if (this.panes.includes(AppPaneId.Navigation)) {
|
||||||
this.insertPaneAtIndex(AppPaneId.Items, 1)
|
this.insertPaneAtIndex(AppPaneId.Items, 1)
|
||||||
} else {
|
} else {
|
||||||
this.insertPaneAtIndex(AppPaneId.Items, 0)
|
this.insertPaneAtIndex(AppPaneId.Items, 0)
|
||||||
}
|
}
|
||||||
this.listPaneExplicitelyCollapsed = false
|
this.preferences.setLocalValue(LocalPrefKey.ListPaneCollapsed, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleNavigationPane = () => {
|
toggleNavigationPane = () => {
|
||||||
if (this.panes.includes(AppPaneId.Navigation)) {
|
if (this.panes.includes(AppPaneId.Navigation)) {
|
||||||
this.removePane(AppPaneId.Navigation)
|
this.removePane(AppPaneId.Navigation)
|
||||||
this.navigationPaneExplicitelyCollapsed = true
|
this.preferences.setLocalValue(LocalPrefKey.NavigationPaneCollapsed, true)
|
||||||
} else {
|
} else {
|
||||||
this.insertPaneAtIndex(AppPaneId.Navigation, 0)
|
this.insertPaneAtIndex(AppPaneId.Navigation, 0)
|
||||||
this.navigationPaneExplicitelyCollapsed = false
|
this.preferences.setLocalValue(LocalPrefKey.NavigationPaneCollapsed, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
import { useApplication } from '@/Components/ApplicationProvider'
|
import { useApplication } from '@/Components/ApplicationProvider'
|
||||||
import { ApplicationEvent, PrefKey, PrefDefaults, LocalPrefKey, LocalPrefValue } from '@standardnotes/snjs'
|
import {
|
||||||
|
ApplicationEvent,
|
||||||
|
PrefKey,
|
||||||
|
PrefDefaults,
|
||||||
|
LocalPrefKey,
|
||||||
|
LocalPrefValue,
|
||||||
|
LocalPrefDefaults,
|
||||||
|
} from '@standardnotes/snjs'
|
||||||
import { useCallback, useEffect, useState } from 'react'
|
import { useCallback, useEffect, useState } from 'react'
|
||||||
|
|
||||||
export function useLocalPreference<Key extends LocalPrefKey>(preference: Key) {
|
export function useLocalPreference<Key extends LocalPrefKey>(preference: Key) {
|
||||||
const application = useApplication()
|
const application = useApplication()
|
||||||
|
|
||||||
const [value, setValue] = useState(application.preferences.getLocalValue(preference, PrefDefaults[preference]))
|
const [value, setValue] = useState(application.preferences.getLocalValue(preference, LocalPrefDefaults[preference]))
|
||||||
|
|
||||||
const setNewValue = useCallback(
|
const setNewValue = useCallback(
|
||||||
(newValue: LocalPrefValue[Key]) => {
|
(newValue: LocalPrefValue[Key]) => {
|
||||||
@@ -16,7 +23,7 @@ export function useLocalPreference<Key extends LocalPrefKey>(preference: Key) {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return application.addEventObserver(async () => {
|
return application.addEventObserver(async () => {
|
||||||
const latestValue = application.preferences.getLocalValue(preference, PrefDefaults[preference])
|
const latestValue = application.preferences.getLocalValue(preference, LocalPrefDefaults[preference])
|
||||||
|
|
||||||
setValue(latestValue)
|
setValue(latestValue)
|
||||||
}, ApplicationEvent.LocalPreferencesChanged)
|
}, ApplicationEvent.LocalPreferencesChanged)
|
||||||
|
|||||||
Reference in New Issue
Block a user