pages235/src/optionsStorage.ts
Vitaly 0c3aaa420b feat: Chat commands completions! tab complete + auto complete for singleplayer & servers < 1.13
feat: Add chat history for mobile! (e.g. arrows on IOS)
fix: remove annoying popup on slash in Edge
fix: text cursor positioning after text update in Safari
fix: preserve currently typed text in navigation history (like in most REPLs)!
2023-09-23 05:48:01 +03:00

76 lines
2 KiB
TypeScript

// todo implement async options storage
import { proxy, subscribe } from 'valtio/vanilla'
// weird webpack configuration bug: it cant import valtio/utils in this file
import { subscribeKey } from 'valtio/utils'
const mergeAny: <T>(arg1: T, arg2: any) => T = Object.assign
const defaultOptions = {
renderDistance: 4,
closeConfirmation: true,
autoFullScreen: false,
mouseRawInput: false,
autoExitFullscreen: false,
localUsername: 'wanderer',
mouseSensX: 50,
mouseSensY: 50 as number | true,
// mouseInvertX: false,
chatWidth: 320,
chatHeight: 180,
chatScale: 100,
volume: 50,
// fov: 70,
fov: 75,
guiScale: 3,
autoRequestCompletions: true,
frameLimit: false as number | false,
alwaysBackupWorldBeforeLoading: undefined as boolean | undefined | null,
alwaysShowMobileControls: false,
maxMultiplayerRenderDistance: 6,
excludeCommunicationDebugEvents: [],
preventDevReloadWhilePlaying: false,
numWorkers: 4,
localServerOptions: {},
preferLoadReadonly: false,
disableLoadPrompts: false,
guestUsername: 'guest',
askGuestName: true
}
export const options = proxy(
mergeAny(defaultOptions, JSON.parse(localStorage.options || '{}'))
)
window.options = window.settings = options
subscribe(options, () => {
localStorage.options = JSON.stringify(options)
})
type WatchValue = <T extends Record<string, any>>(proxy: T, callback: (p: T) => void) => void
export const watchValue: WatchValue = (proxy, callback) => {
const watchedProps = new Set<string>()
callback(new Proxy(proxy, {
get(target, p, receiver) {
watchedProps.add(p.toString())
return Reflect.get(target, p, receiver)
},
}))
watchedProps.forEach(prop => {
subscribeKey(proxy, prop, () => {
callback(proxy)
})
})
}
watchValue(options, o => {
globalThis.excludeCommunicationDebugEvents = o.excludeCommunicationDebugEvents
})
export const useOptionValue = (setting, valueCallback) => {
valueCallback(setting)
subscribe(setting, valueCallback)
}