2024-04-17 20:18:07 -07:00
|
|
|
// A little class for updating the code state when we need to and explicitly
|
|
|
|
// NOT updating the code state when we don't need to.
|
|
|
|
// This prevents re-renders of the codemirror editor, when typing.
|
|
|
|
import { bracket } from 'lib/exampleKcl'
|
|
|
|
import { isTauri } from 'lib/isTauri'
|
|
|
|
import { writeTextFile } from '@tauri-apps/plugin-fs'
|
|
|
|
import toast from 'react-hot-toast'
|
2024-04-19 14:24:40 -07:00
|
|
|
import { editorManager } from 'lib/singletons'
|
2024-06-29 18:10:07 -07:00
|
|
|
import { Annotation, KeyBinding, Transaction } from '@uiw/react-codemirror'
|
2024-04-17 20:18:07 -07:00
|
|
|
|
2024-07-02 17:16:27 +10:00
|
|
|
const PERSIST_CODE_KEY = 'persistCode'
|
2024-04-17 20:18:07 -07:00
|
|
|
|
2024-06-29 18:10:07 -07:00
|
|
|
const codeManagerUpdateAnnotation = Annotation.define<null>()
|
|
|
|
export const codeManagerUpdateEvent = codeManagerUpdateAnnotation.of(null)
|
|
|
|
|
2024-04-17 20:18:07 -07:00
|
|
|
export default class CodeManager {
|
|
|
|
private _code: string = bracket
|
2024-05-06 19:28:30 +10:00
|
|
|
#updateState: (arg: string) => void = () => {}
|
2024-04-19 14:24:40 -07:00
|
|
|
private _currentFilePath: string | null = null
|
2024-05-20 20:52:33 -07:00
|
|
|
private _hotkeys: { [key: string]: () => void } = {}
|
2024-04-17 20:18:07 -07:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
if (isTauri()) {
|
|
|
|
this.code = ''
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-02 17:16:27 +10:00
|
|
|
const storedCode = safeLSGetItem(PERSIST_CODE_KEY)
|
2024-04-17 20:18:07 -07:00
|
|
|
// TODO #819 remove zustand persistence logic in a few months
|
|
|
|
// short term migration, shouldn't make a difference for tauri app users
|
|
|
|
// anyway since that's filesystem based.
|
|
|
|
const zustandStore = JSON.parse(safeLSGetItem('store') || '{}')
|
|
|
|
if (storedCode === null && zustandStore?.state?.code) {
|
|
|
|
this.code = zustandStore.state.code
|
|
|
|
zustandStore.state.code = ''
|
|
|
|
safeLSSetItem('store', JSON.stringify(zustandStore))
|
|
|
|
} else if (storedCode === null) {
|
|
|
|
this.code = bracket
|
|
|
|
} else {
|
|
|
|
this.code = storedCode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set code(code: string) {
|
|
|
|
this._code = code
|
|
|
|
}
|
|
|
|
|
|
|
|
get code(): string {
|
|
|
|
return this._code
|
|
|
|
}
|
|
|
|
|
2024-04-19 14:24:40 -07:00
|
|
|
registerCallBacks({ setCode }: { setCode: (arg: string) => void }) {
|
2024-05-06 19:28:30 +10:00
|
|
|
this.#updateState = setCode
|
2024-04-17 20:18:07 -07:00
|
|
|
}
|
|
|
|
|
2024-05-20 20:52:33 -07:00
|
|
|
registerHotkey(hotkey: string, callback: () => void) {
|
|
|
|
this._hotkeys[hotkey] = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
getCodemirrorHotkeys(): KeyBinding[] {
|
|
|
|
return Object.keys(this._hotkeys).map((key) => ({
|
|
|
|
key,
|
|
|
|
run: () => {
|
|
|
|
this._hotkeys[key]()
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2024-04-19 14:24:40 -07:00
|
|
|
updateCurrentFilePath(path: string) {
|
|
|
|
this._currentFilePath = path
|
2024-04-17 20:18:07 -07:00
|
|
|
}
|
|
|
|
|
2024-06-20 21:39:01 -04:00
|
|
|
/**
|
|
|
|
* This updates the code state and calls the updateState function.
|
|
|
|
*/
|
2024-04-17 20:18:07 -07:00
|
|
|
updateCodeState(code: string): void {
|
|
|
|
if (this._code !== code) {
|
|
|
|
this.code = code
|
2024-05-06 19:28:30 +10:00
|
|
|
this.#updateState(code)
|
2024-04-17 20:18:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-20 21:39:01 -04:00
|
|
|
/**
|
|
|
|
* Update the code in the editor.
|
|
|
|
*/
|
2024-04-17 20:18:07 -07:00
|
|
|
updateCodeEditor(code: string): void {
|
2024-04-19 14:24:40 -07:00
|
|
|
this.code = code
|
|
|
|
if (editorManager.editorView) {
|
|
|
|
editorManager.editorView.dispatch({
|
2024-05-06 19:28:30 +10:00
|
|
|
changes: {
|
|
|
|
from: 0,
|
|
|
|
to: editorManager.editorView.state.doc.length,
|
|
|
|
insert: code,
|
|
|
|
},
|
2024-06-29 18:10:07 -07:00
|
|
|
annotations: [
|
|
|
|
codeManagerUpdateEvent,
|
|
|
|
Transaction.addToHistory.of(true),
|
|
|
|
],
|
2024-04-19 14:24:40 -07:00
|
|
|
})
|
|
|
|
}
|
2024-04-17 20:18:07 -07:00
|
|
|
}
|
|
|
|
|
2024-06-20 21:39:01 -04:00
|
|
|
/**
|
|
|
|
* Update the code, state, and the code the code mirror editor sees.
|
|
|
|
*/
|
2024-04-17 20:18:07 -07:00
|
|
|
updateCodeStateEditor(code: string): void {
|
|
|
|
if (this._code !== code) {
|
|
|
|
this.code = code
|
2024-05-06 19:28:30 +10:00
|
|
|
this.#updateState(code)
|
2024-05-10 15:30:40 -07:00
|
|
|
this.updateCodeEditor(code)
|
2024-04-17 20:18:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async writeToFile() {
|
|
|
|
if (isTauri()) {
|
|
|
|
setTimeout(() => {
|
|
|
|
// Wait one event loop to give a chance for params to be set
|
|
|
|
// Save the file to disk
|
2024-04-19 14:24:40 -07:00
|
|
|
this._currentFilePath &&
|
|
|
|
writeTextFile(this._currentFilePath, this.code).catch((err) => {
|
2024-04-17 20:18:07 -07:00
|
|
|
// TODO: add tracing per GH issue #254 (https://github.com/KittyCAD/modeling-app/issues/254)
|
|
|
|
console.error('error saving file', err)
|
|
|
|
toast.error('Error saving file, please check file permissions')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
2024-07-02 17:16:27 +10:00
|
|
|
safeLSSetItem(PERSIST_CODE_KEY, this.code)
|
2024-04-17 20:18:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function safeLSGetItem(key: string) {
|
|
|
|
if (typeof window === 'undefined') return null
|
|
|
|
return localStorage?.getItem(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
function safeLSSetItem(key: string, value: string) {
|
|
|
|
if (typeof window === 'undefined') return
|
|
|
|
localStorage?.setItem(key, value)
|
|
|
|
}
|