Editor singleton to prevent re-renders (#2163)

* move editor data into a singleton

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* debounce on update

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* make select on extrude work

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* highlight range

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* highlight range

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* fix errors

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* almost forgot the error pane

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* loint

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* call out to codemirror

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* fix tauri;

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* more efficient

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* create the modals in the hook

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* Revert "create the modals in the hook"

This reverts commit bbeba85030763cf7235a09fa24247dbf120f2a64.

* change todo

Signed-off-by: Jess Frazelle <github@jessfraz.com>

---------

Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2024-04-19 14:24:40 -07:00
committed by GitHub
parent f08d955d40
commit 537d86c8ff
44 changed files with 584 additions and 415 deletions

View File

@ -5,7 +5,7 @@ import { bracket } from 'lib/exampleKcl'
import { isTauri } from 'lib/isTauri'
import { writeTextFile } from '@tauri-apps/plugin-fs'
import toast from 'react-hot-toast'
import { Params } from 'react-router-dom'
import { editorManager } from 'lib/singletons'
const PERSIST_CODE_TOKEN = 'persistCode'
@ -13,7 +13,7 @@ export default class CodeManager {
private _code: string = bracket
private _updateState: (arg: string) => void = () => {}
private _updateEditor: (arg: string) => void = () => {}
private _params: Params<string> = {}
private _currentFilePath: string | null = null
constructor() {
if (isTauri()) {
@ -45,19 +45,12 @@ export default class CodeManager {
return this._code
}
registerCallBacks({
setCode,
setEditorCode,
}: {
setCode: (arg: string) => void
setEditorCode: (arg: string) => void
}) {
registerCallBacks({ setCode }: { setCode: (arg: string) => void }) {
this._updateState = setCode
this._updateEditor = setEditorCode
}
setParams(params: Params<string>) {
this._params = params
updateCurrentFilePath(path: string) {
this._currentFilePath = path
}
// This updates the code state and calls the updateState function.
@ -70,11 +63,14 @@ export default class CodeManager {
// Update the code in the editor.
updateCodeEditor(code: string): void {
if (this._code !== code) {
this.code = code
this._updateEditor(code)
}
const lastCode = this._code
this.code = code
this._updateEditor(code)
if (editorManager.editorView) {
editorManager.editorView.dispatch({
changes: { from: 0, to: lastCode.length, insert: code },
})
}
}
// Update the code, state, and the code the code mirror editor sees.
@ -91,8 +87,8 @@ export default class CodeManager {
setTimeout(() => {
// Wait one event loop to give a chance for params to be set
// Save the file to disk
this._params.id &&
writeTextFile(this._params.id, this.code).catch((err) => {
this._currentFilePath &&
writeTextFile(this._currentFilePath, this.code).catch((err) => {
// 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')