Skip to content

Commit 42fb2f3

Browse files
committed
Port autosave
1 parent 33aa569 commit 42fb2f3

File tree

2 files changed

+21
-40
lines changed

2 files changed

+21
-40
lines changed

src/components/EditorPane.tsx

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,14 @@ import { default as MonacoEditor } from "@monaco-editor/react";
22
import type * as monaco from "monaco-editor";
33
import { Component } from "react";
44

5-
import { saveLastSource } from "../config.js";
6-
import * as ls from "../dot-monaco/index.js";
7-
8-
type Monaco = typeof monaco;
9-
105
type EditorPaneProps = {
116
defaultValue?: string;
127
onChangeValue(value: string): void;
138
onValueError(err: monaco.editor.IMarkerData[]): void;
149
};
1510

16-
const SOURCE_SAVE_TIMEOUT = 5 * 1000; // 5 seconds
17-
1811
export default class EditorPane extends Component<EditorPaneProps, object> {
19-
#processor: ls.LanguageProcessor | undefined;
2012
#editor: monaco.editor.IStandaloneCodeEditor | undefined;
21-
#autoSaveTimeout: ReturnType<typeof setTimeout> | undefined = undefined;
2213

2314
state: object = {};
2415

@@ -29,36 +20,8 @@ export default class EditorPane extends Component<EditorPaneProps, object> {
2920
}
3021
}
3122

32-
#editorWillMount = (monaco: Monaco): void => {
33-
ls.registerService(monaco, ls.service);
34-
this.#processor = ls.service.processor;
35-
};
36-
37-
#onChange = (
38-
value: string | undefined,
39-
_event: monaco.editor.IModelContentChangedEvent,
40-
): void => {
41-
const p = this.#processor;
42-
const e = this.#editor;
43-
if (!p || !e) return;
44-
45-
if (typeof this.#autoSaveTimeout !== "undefined") {
46-
clearTimeout(this.#autoSaveTimeout);
47-
}
48-
this.#autoSaveTimeout = setTimeout(
49-
() => saveLastSource(value),
50-
SOURCE_SAVE_TIMEOUT,
51-
);
52-
};
53-
5423
render() {
5524
const defaultValue = this.props.defaultValue || "";
56-
return (
57-
<MonacoEditor
58-
defaultValue={defaultValue}
59-
onChange={this.#onChange}
60-
beforeMount={this.#editorWillMount}
61-
/>
62-
);
25+
return <MonacoEditor defaultValue={defaultValue} />;
6326
}
6427
}

src/index.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import "./index.scss";
88

99
import Navigation from "./components/Navigation.js";
1010
import SplitEditor from "./components/SplitEditor.js";
11-
import { getLastState, mergeStates, saveLastEngine } from "./config.js";
11+
import {
12+
getLastState,
13+
mergeStates,
14+
saveLastEngine,
15+
saveLastSource,
16+
} from "./config.js";
1217
import { FileSaver } from "./FileSaver.js";
1318
import { exportAs, type SupportedEngine, saveSource } from "./rendering.js";
1419
import { tutorial } from "./samples/index.js";
@@ -17,7 +22,7 @@ import {
1722
type ExportableFormat,
1823
sourceFormatExtension,
1924
supportedEngines,
20-
} from "./viz";
25+
} from "./viz/index.js";
2126

2227
const defaultEngine = supportedEngines[1];
2328

@@ -31,12 +36,16 @@ interface AppProps {
3136

3237
const defaultSource = tutorial;
3338

39+
const SOURCE_SAVE_TIMEOUT = 5 * 1000; // 5 seconds
40+
3441
class App extends Component<AppProps, AppState> {
3542
#currentSource: string | undefined = undefined;
3643
#saver: FileSaver = new FileSaver();
3744
#editorRef: RefObject<import("./components/SplitEditor").default | null> =
3845
createRef();
3946

47+
#autoSaveTimeout: ReturnType<typeof setTimeout> | undefined = undefined;
48+
4049
state: AppState;
4150

4251
constructor(p: AppProps) {
@@ -77,6 +86,15 @@ class App extends Component<AppProps, AppState> {
7786

7887
#sourceChanged = (source: string): void => {
7988
this.#currentSource = source;
89+
90+
if (typeof this.#autoSaveTimeout !== "undefined") {
91+
clearTimeout(this.#autoSaveTimeout);
92+
}
93+
94+
this.#autoSaveTimeout = setTimeout(
95+
() => saveLastSource(source),
96+
SOURCE_SAVE_TIMEOUT,
97+
);
8098
};
8199

82100
#share = (): boolean => {

0 commit comments

Comments
 (0)