-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
174 lines (141 loc) · 4.01 KB
/
index.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { render } from "https://unpkg.com/[email protected]/lit-html.js";
import { delegate } from "./delegate.js";
import { view } from "./view.js";
import { initialSamples } from "./samples.js";
import { init } from "./init.js";
import { play } from "./play.js";
import "./test.js";
const listenBody = delegate(document.body);
const STATE = {
activeMuses: [],
samples: initialSamples(),
recordingStatus: "pre-permission",
sampleVolume: 0.35,
rec: undefined,
played: [],
keyBindings: {},
showExamples: false,
examples: [],
showShared: false,
colorMode: "light",
}
function copy(str) {
const inp = document.createElement('input');
document.body.appendChild(inp);
inp.value = str;
inp.select();
document.execCommand('copy', false);
inp.remove();
}
const ACTIONS = {
INIT: init,
RENDER: (args, state) => { render(view(STATE), document.body) },
TOGGLE_COLOR_MODE: (args, state) => {
const setProp = (name, val) => document.documentElement.style.setProperty(name, val);
let mode = args.mode;
if (!mode) {
if (state.colorMode === "dark") mode = "light";
else if (state.colorMode === "light") mode = "dark";
}
if (mode === "dark") {
setProp("--text-color", "white");
setProp("--background", "var(--dark-background)");
setProp("--background-2", "var(--dark-background-2)");
setProp("--cm-filter", "invert(1)");
} else if (mode === "light") {
setProp("--text-color", "black");
setProp("--background", "var(--light-background)");
setProp("--background-2", "var(--light-background-2)");
setProp("--cm-filter", "");
}
state.colorMode = mode;
},
ADD_ACTIVE_MUSE: ({ newMuse }, state) => {
state.activeMuses.push(newMuse);
},
DELETE_SAMPLE: ({index}, state) => {
state.samples[index].deleted = true;
dispatch("RENDER");
},
START_RECORDING: (args, state) => {
state.recordingStatus = "three"
dispatch("RENDER");
setTimeout(() => {
state.recordingStatus = "two"
dispatch("RENDER");
}, 1000);
setTimeout(() => {
state.recordingStatus = "one"
dispatch("RENDER");
}, 2000);
setTimeout(() => {
state.rec.start()
state.recordingStatus = "recording"
dispatch("RENDER");
}, 3000);
},
ADD_SAMPLE: (sample, state) => {
state.recordingStatus = "ready"
state.samples.push(sample)
dispatch("RENDER");
},
STOP_RECORDING: async (args, state) => {
state.recordingStatus = "loading"
dispatch("RENDER") // force re-render
await state.rec.stop()
},
ADD_PLAYED: ({symbol}, state) => {
state.played.push(symbol);
dispatch("RENDER");
const el = document.querySelector(".played-log");
el.scrollTop = el.scrollHeight - el.clientHeight;
},
CLEAR_PLAYED: (args, state) => {
state.played = [];
dispatch("RENDER");
const el = document.querySelector(".played-log");
el.scrollTop = el.scrollHeight - el.clientHeight;
},
STOP: (args, state) => {
state.activeMuses.forEach( muse => muse.stop() );
state.activeMuses = [];
},
EXAMPLES: ({ show }, state) => {
state.showExamples = show;
dispatch("RENDER");
},
LOAD: ({ txt }, state) => {
const cm = document.querySelector("#cm");
const prog = cm.view.state.doc.toString();
cm.view.dispatch({
changes: { from: 0, to: prog.length, insert: txt }
});
},
SHARE: (args, state) => {
const cm = document.querySelector("#cm");
const prog = cm.view.state.doc.toString();
const url = 'https://airbridge.hackclub.com/v0.2/Saved%20Projects/Muse%20Projects/?authKey=recgJS1YFxdq7ypts1634664978nkjcc8fdj4';
(async () => {
const res = await fetch(url, {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
"Content": prog
})
}).then(r => r.json())
copy(res.fields["Link"]);
state.showShared = true;
dispatch("RENDER");
})()
},
PLAY: (args, state) => {
play(state);
}
}
const dispatch = (action, args = {}) => {
if (action in ACTIONS) ACTIONS[action](args, STATE);
else console.error("Unrecongized action:", action);
}
window.dispatch = dispatch;
window.IS_MUSE_EDITOR = true;
dispatch("INIT");