Skip to content

Commit 7a44072

Browse files
committed
update
1 parent c4f4f99 commit 7a44072

File tree

14 files changed

+318
-221
lines changed

14 files changed

+318
-221
lines changed

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@emotion/styled": "^11.14.1",
1919
"@mui/icons-material": "^7.3.1",
2020
"@mui/material": "^7.3.1",
21+
"debounce": "^2.2.0",
2122
"es-toolkit": "^1.39.10",
2223
"eventemitter3": "^5.0.1",
2324
"memoize-one": "^6.0.0",

src/pages/edit/Editor/renderer/Connection.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useStore } from "../../../../store/react";
66
import ensureStoreItem from "../../../../store/react/error";
77
import { useNode } from "../../../../store/react/selectors";
88
import { useComponentEditorStore } from "../store";
9+
import { stringifySimulationValue } from "../store/slices/core/index";
910
import getCCComponentEditorRendererNodeGeometry from "./Node.geometry";
1011

1112
export type CCComponentEditorRendererConnectionCoreProps = {
@@ -106,8 +107,6 @@ const CCComponentEditorRendererConnection = ensureStoreItem(
106107

107108
const [isHovered, setIsHovered] = useState(false);
108109

109-
// const fromNodePinValue = componentEditorState.getNodePinValue(fromNodePin.id);
110-
111110
return (
112111
<>
113112
<CCComponentEditorRendererConnectionCore
@@ -128,7 +127,9 @@ const CCComponentEditorRendererConnection = ensureStoreItem(
128127
startOffset="50%"
129128
textAnchor="middle"
130129
>
131-
hoge
130+
{stringifySimulationValue(
131+
nullthrows(componentEditorState.getNodePinValue(toNodePin.id)),
132+
)}
132133
</textPath>
133134
</text>
134135
)}

src/pages/edit/Editor/renderer/Node.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const CCComponentEditorRendererNode = ensureStoreItem(
7979
fill={theme.palette.textPrimary}
8080
x={geometry.x}
8181
y={geometry.y - 5}
82-
textAnchor="end"
82+
textAnchor="start"
8383
fontSize={12}
8484
>
8585
{component.name}

src/pages/home/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
} from "@mui/material";
1818
import { useRef, useState } from "react";
1919
import { ComponentPropertyDialog } from "../../components/ComponentPropertyDialog";
20-
import type { CCStorePropsFromJson } from "../../store";
2120
import { type CCComponentId, CCComponentStore } from "../../store/component";
2221
import { useStore } from "../../store/react";
2322
import { useComponents } from "../../store/react/selectors";
@@ -49,9 +48,7 @@ export default function HomePage({ onComponentSelected }: HomePageProps) {
4948
if (!file) return;
5049
const reader = new FileReader();
5150
reader.onload = () => {
52-
const storeJSON = reader.result as string;
53-
const storeData = JSON.parse(storeJSON);
54-
resetStore(storeData as CCStorePropsFromJson);
51+
resetStore(reader.result as string);
5552
};
5653
reader.readAsText(file);
5754
};

src/store/autoSaver.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import debounce from "debounce";
2+
import type CCStore from ".";
3+
import { ccComponentStoreChangeEventTypes } from "./component";
4+
import { ccComponentPinStoreChangeEventTypes } from "./componentPin";
5+
import { ccConnectionStoreChangeEventTypes } from "./connection";
6+
import { ccNodeStoreChangeEventTypes } from "./node";
7+
import { ccNodePinStoreChangeEventTypes } from "./nodePin";
8+
9+
export class CCStoreAutoSaver {
10+
#store: CCStore;
11+
12+
static localStorageKey = "ccStoreAutoSaverState";
13+
14+
constructor(store: CCStore) {
15+
this.#store = store;
16+
}
17+
18+
watch() {
19+
const save = debounce(this.save.bind(this), 1000);
20+
for (const eventType of ccComponentStoreChangeEventTypes) {
21+
this.#store.components.addListener(eventType, save);
22+
}
23+
for (const eventType of ccComponentPinStoreChangeEventTypes) {
24+
this.#store.componentPins.addListener(eventType, save);
25+
}
26+
for (const eventType of ccNodeStoreChangeEventTypes) {
27+
this.#store.nodes.addListener(eventType, save);
28+
}
29+
for (const eventType of ccNodePinStoreChangeEventTypes) {
30+
this.#store.nodePins.addListener(eventType, save);
31+
}
32+
for (const eventType of ccConnectionStoreChangeEventTypes) {
33+
this.#store.connections.addListener(eventType, save);
34+
}
35+
}
36+
37+
save(): void {
38+
window.localStorage.setItem(
39+
CCStoreAutoSaver.localStorageKey,
40+
this.#store.toJSON(),
41+
);
42+
}
43+
44+
tryRestore(): boolean {
45+
const json = window.localStorage.getItem(CCStoreAutoSaver.localStorageKey);
46+
if (json) {
47+
this.#store.importJson(json);
48+
return true;
49+
} else {
50+
return false;
51+
}
52+
}
53+
}

src/store/component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export type CCComponentStoreEvents = {
2020
didUnregister(component: CCComponent): void;
2121
didUpdate(component: CCComponent): void;
2222
};
23+
export const ccComponentStoreChangeEventTypes: (keyof CCComponentStoreEvents)[] =
24+
["didRegister", "didUnregister", "didUpdate"];
2325

2426
/**
2527
* Store of components

0 commit comments

Comments
 (0)