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'
|
2024-08-16 07:15:42 -04:00
|
|
|
import { isDesktop } from 'lib/isDesktop'
|
2024-04-17 20:18:07 -07:00
|
|
|
import toast from 'react-hot-toast'
|
2024-04-19 14:24:40 -07:00
|
|
|
import { editorManager } from 'lib/singletons'
|
2024-07-03 21:28:51 -07:00
|
|
|
import { Annotation, Transaction } from '@codemirror/state'
|
|
|
|
import { KeyBinding } from '@codemirror/view'
|
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-07-08 21:07:15 -07:00
|
|
|
const codeManagerUpdateAnnotation = Annotation.define<boolean>()
|
|
|
|
export const codeManagerUpdateEvent = codeManagerUpdateAnnotation.of(true)
|
2024-06-29 18:10:07 -07:00
|
|
|
|
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-10-21 16:07:20 -04:00
|
|
|
private timeoutWriter: ReturnType<typeof setTimeout> | undefined = undefined
|
2024-04-17 20:18:07 -07:00
|
|
|
|
2024-10-25 18:07:50 -04:00
|
|
|
public writeCausedByAppCheckedInFileTreeFileSystemWatcher = false
|
|
|
|
|
2024-04-17 20:18:07 -07:00
|
|
|
constructor() {
|
2024-08-16 07:15:42 -04:00
|
|
|
if (isDesktop()) {
|
2024-04-17 20:18:07 -07:00
|
|
|
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
|
2024-08-16 07:15:42 -04:00
|
|
|
// short term migration, shouldn't make a difference for desktop app users
|
2024-04-17 20:18:07 -07:00
|
|
|
// 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-07-25 20:18:11 -04:00
|
|
|
preventDefault: true,
|
2024-05-20 20:52:33 -07:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
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() {
|
2024-08-16 07:15:42 -04:00
|
|
|
if (isDesktop()) {
|
2024-10-21 16:07:20 -04:00
|
|
|
// Only write our buffer contents to file once per second. Any faster
|
|
|
|
// and file-system watchers which read, will receive empty data during
|
|
|
|
// writes.
|
2024-10-31 21:42:52 -04:00
|
|
|
|
2024-10-21 16:07:20 -04:00
|
|
|
clearTimeout(this.timeoutWriter)
|
2024-10-25 18:07:50 -04:00
|
|
|
this.writeCausedByAppCheckedInFileTreeFileSystemWatcher = true
|
2024-10-31 21:42:52 -04:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.timeoutWriter = setTimeout(() => {
|
|
|
|
if (!this._currentFilePath)
|
|
|
|
return reject(new Error('currentFilePath not set'))
|
|
|
|
|
|
|
|
// Wait one event loop to give a chance for params to be set
|
|
|
|
// Save the file to disk
|
2024-08-16 07:15:42 -04:00
|
|
|
window.electron
|
|
|
|
.writeFile(this._currentFilePath, this.code ?? '')
|
2024-10-31 21:42:52 -04:00
|
|
|
.then(resolve)
|
2024-08-16 07:15:42 -04:00
|
|
|
.catch((err: Error) => {
|
|
|
|
// 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')
|
2024-10-31 21:42:52 -04:00
|
|
|
reject(err)
|
2024-08-16 07:15:42 -04:00
|
|
|
})
|
2024-10-31 21:42:52 -04:00
|
|
|
}, 1000)
|
|
|
|
})
|
2024-04-17 20:18:07 -07:00
|
|
|
} 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)
|
|
|
|
}
|