-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinit.js
137 lines (110 loc) · 4.26 KB
/
init.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
import { delegate } from "./delegate.js";
import { defaultProg } from "./defaultProg.js";
const listenBody = delegate(document.body);
let audioChunks = [];
export async function init(args, state) {
dispatch("RENDER");
dispatch("TOGGLE_COLOR_MODE", { mode: state.colorMode })
listenBody("click", ".trigger-play", () => {
dispatch("PLAY");
})
listenBody("keydown", "", (e) => {
let key = event.key;
const cm = document.querySelector("#cm");
const prog = cm.view.state.doc.toString();
window.localStorage.setItem("muse-prog", prog);
if (key === "Enter" && event.shiftKey) {
event.preventDefault();
dispatch("PLAY");
}
if (e.target.getAttribute("role") === "textbox") return;
if (key in state.keyBindings) {
state.keyBindings[key]();
}
})
listenBody("mousedown", "", (e) => {
if (!e.target.matches(".examples > *, .examples")) {
dispatch("EXAMPLES", { show: false });
}
if (!e.target.matches(".shared-confirmation")) {
state.showShared = false;
dispatch("RENDER");
}
})
const savedSamples = window.localStorage.getItem("muse-samples");
if (savedSamples) state.samples = JSON.parse(savedSamples);
// load program
const url = new URL(window.location.href);
const search = window.location.search;
const file = new URLSearchParams(search).get("file");
if (file) {
let file_url = file;
if (file.startsWith("rec")) { // fetch from airtable
// fetch rec with that ID
const url = `https://api2.hackclub.com/v0.1/Saved Projects/Muse Projects/?select={"filterByFormula": "RECORD_ID()='${file}'"}`;
(async () => {
const json = await fetch(url, {mode: 'cors'}).then(r => r.json())
document.querySelector("#cm").view.dispatch({
changes: {from: 0, insert: json[0].fields["Content"] }
});
})()
} else { // fetch from another page
if (!file.startsWith("http")) file_url = `examples/${file}`;
(async () => {
const file = await fetch(file_url, {mode: 'cors'});
const txt = await file.text();
document.querySelector("#cm").view.dispatch({
changes: {from: 0, insert: txt}
});
})()
}
} else { // if no parameter and empty load cached program else load default program
const saved = window.localStorage.getItem("muse-prog")
document.querySelector("#cm").view.dispatch({
changes: { from: 0, insert: !saved ? defaultProg.trim() : saved }
});
}
dispatch("RENDER")
const stream = await navigator
.mediaDevices
.getUserMedia({ audio: true })
state.recordingStatus = "ready";
dispatch("RENDER");
state.rec = new MediaRecorder(stream)
state.rec.ondataavailable = (e) => {
audioChunks.push(e.data)
if (state.rec.state == "inactive") {
let blob = new Blob(audioChunks, { type: "audio/mpeg-3" })
sendData(blob)
}
}
(async () => {
const examplesURL = `https://api2.hackclub.com/v0.1/Saved Projects/Muse Projects/?select={"filterByFormula": "{Public}=TRUE()"}`;
const json = await fetch(examplesURL, { mode: "cors" }).then(res => res.json());
state.examples = json.map(x => x.fields);
})()
}
function makeId(length) {
var result = '';
var characters = 'abcdefghijklmnopqrstuvwxyz';
var numbers = "0123456789"
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
async function sendData(data) {
const body = new FormData()
body.append('sound',data)
const upload = await fetch('https://sound-sampler.maxwofford.repl.co/upload-sound', {
method: 'POST',
body
}).then(r => r.json())
const url = 'https://sound-sampler.maxwofford.repl.co/' + upload.data.filename
const sampleObj = {}
const name = makeId(4);
dispatch("ADD_SAMPLE", { name, url, provided: false})
// sampleObj[name] = {url, provided: false}
// setSample(sampleObj)
}