-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patheditor.ts
More file actions
111 lines (92 loc) · 2.35 KB
/
editor.ts
File metadata and controls
111 lines (92 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { createStore } from "zustand";
import { Algorithm, AlgorithmParamDefinitions } from "~/core/simulator/algorithm";
import { Graph } from "~/core/simulator/graph";
import { Step } from "~/core/simulator/step";
import { Project, ProjectMetadata } from "~/types/project";
type Mode =
| {
type: "IDLE";
}
| {
type: "SIMULATION";
steps: Step[];
};
export type EditorState = {
metadata: ProjectMetadata;
mode: Mode;
setMode: (mode: Mode) => void;
code: string;
setCode: (code: string) => void;
graph: Graph;
setGraph: (graph: Graph) => void;
algorithm: Algorithm<object> | undefined;
setAlgorithm: (algorithm?: Algorithm<object>) => void;
algorithmParams: AlgorithmParamDefinitions<object>;
setAlgorithmParams: (algorithmParams: AlgorithmParamDefinitions<object>) => void;
};
export type CreateEditorStoreOpts = {
project: Project;
};
const initialCode = `graph {
# This creates a node with ID "a"
a [cost = 100];
# This creates a node with cost of 100.
b [cost = 200];
# Semicolons are optional
c [cost = 300]
# This creates a path between a, b, c, d and e
# in which every edge has cost of 20.
a -- b -- c -- d -- e -- a [cost=20]
# You can create nodes while creating paths
a -- d [cost=10]
# You don't have to specify the cost
b -- e
# Use -> to represent a directed edge.
a0 -- b0 -> c0 -> d0 -- e0 [cost=12.4];
a -- a0 [cost=0.5];
b -- b0 [cost=0.8];
c -- c0 [cost=0.3];
d -- d0 [cost=0.3];
e -- e0 [cost=0.8];
# You can also give edges explicit names
x -- z [name="Connection"]
}
`;
const initialGraph: Graph = {
edges: {},
vertices: {},
};
export function createEditorStore({ project }: CreateEditorStoreOpts) {
return createStore<EditorState>()((set) => ({
metadata: project.metadata,
// mode
mode: {
type: "IDLE",
},
setMode: (mode) => {
set({
mode,
});
},
// Code
code: initialCode,
setCode: (code) => {
set({ code });
},
// Graph
graph: initialGraph,
setGraph: (graph) => {
set({ graph });
},
// Algorithm
algorithm: undefined,
setAlgorithm(algorithm) {
set({ algorithm });
},
algorithmParams: {},
setAlgorithmParams(algorithmParams) {
set({ algorithmParams });
},
}));
}
export type EditorStore = ReturnType<typeof createEditorStore>;