-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheffects.ts
115 lines (95 loc) · 2.7 KB
/
effects.ts
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
112
113
114
115
import {
HistorySaveAction,
HistorySavedAction,
SetViewportSizeAction,
HistoryLoadedAction,
UndoAction,
HistoryLoadAction,
ApplyUpdateAction,
UpdateAction,
UpdateCompletedAction,
PublishAction,
SetLayoutAction,
} from "./actions";
import { reduce, dispatch, set } from "./state";
reduce(HistorySaveAction, async (state) => {
try {
const { history, snippet, input } = state;
await fetch("/components/" + state.componentId, {
method: "POST",
body: JSON.stringify({ history, snippet, input }),
});
} finally {
dispatch(new HistorySavedAction(null));
}
});
reduce(SetLayoutAction, (_, action) => set("layout", action.payload));
reduce(SetViewportSizeAction, (_, action) =>
set("viewportSize", action.payload)
);
reduce(HistorySaveAction, () => set("saving", true));
reduce(HistorySavedAction, () => set("saving", false));
reduce(HistoryLoadedAction, (state, action) => {
const { history = [], snippet = "", input = "" } = action.payload;
return Object.assign({}, state, { history, snippet, input });
});
reduce(PublishAction, async function (state) {
if (!confirm("Sure?")) {
return;
}
try {
set("publishing", true);
await fetch("/publish/" + state.componentId, { method: "POST" });
} finally {
set("publishing", false);
}
});
reduce(HistoryLoadAction, async function load(state) {
const history = await fetch("/components/" + state.componentId);
const json = await history.json();
dispatch(new HistoryLoadedAction(json));
});
reduce(UndoAction, function undo(state) {
const list = state.history.slice();
if (!list.length) {
return;
}
const previous = list.pop()!;
set("history", list);
set("snippet", previous.snippet);
set("input", previous.message);
set("running", false);
});
reduce(UpdateAction, async () => set("running", true));
reduce(UpdateCompletedAction, async () => set("running", false));
reduce(UpdateAction, async (state) => {
try {
const body = JSON.stringify({
history: state.history.map((h) => h.message),
code: state.snippet,
instruction: state.input,
});
const req = await fetch("/run", { method: "POST", body });
const code = await req.text();
dispatch(new ApplyUpdateAction(code));
} finally {
dispatch(new UpdateCompletedAction(null));
}
});
reduce(ApplyUpdateAction, (state, action) => {
if (action.payload) {
state.snippet = sanitize(action.payload);
state.history = state.history.concat({
snippet: state.snippet,
message: state.input,
});
}
state.input = "";
return { ...state };
});
function sanitize(code) {
if (code.trim().includes("```")) {
return code.replaceAll("```html", "").replaceAll("```", "");
}
return code;
}