generated from cloud-cli/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.html
144 lines (122 loc) · 4.41 KB
/
editor.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
<link rel="stylesheet" href="/assets/wds/editor.css" />
</head>
<body class="wds-page">
<div class="wds-editor">
<div class="wds-editor__toolbar">
<span class="wds-editor__title">Tailwind Preset</span>
<button class="wds-btn wds-btn-primary" data-action="compile">
Compile
</button>
<button class="wds-btn wds-btn-secondary" data-action="updatePreview">
Preview
</button>
</div>
<div class="wds-editor__content" id="content">
<div id="editor" class="wds-editor__editor"></div>
<div id="playground" class="wds-editor__playground hidden"></div>
</div>
<div class="wds-editor__console" id="console"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.30.1/min/vs/loader.js"></script>
<script type="module">
import { encode, decode } from "https://yaml.jsfn.run/index.mjs";
const presetName = location.pathname.replace("/edit/", "");
function debounce(fn, wait) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn.apply(this, args), wait);
};
}
window.onEditorLoad = (editor, playground) => {
const output = document.getElementById("console");
const contentArea = document.getElementById("content");
const togglePlayground = (hidden) => {
playground.classList[hidden ? "add" : "remove"]("hidden");
contentArea.classList[hidden ? "remove" : "add"]("grid-cols-2");
};
const save = () =>
fetch("/preset/" + presetName, {
method: "POST",
body: editor.getValue(),
});
const compile = async () => {
output.innerText = "Generating assets...";
const req = await fetch("/compile/" + presetName, {
method: "POST",
});
const res = await req.json();
output.innerText = res.error || 'Done.';
updatePreview();
};
const updatePreview = async () => {
const snippets = (await decode(editor.getValue())).snippets;
if (!snippets) {
togglePlayground(true);
return;
}
playground.innerHTML = "";
togglePlayground(false);
const frame = document.createElement("iframe");
const heading = document.createElement("h2");
playground.appendChild(frame);
const fbody = frame.contentDocument.body;
fbody.innerHTML =
`<link rel="stylesheet" href="/assets/${presetName}.css" />` +
Object.entries(snippets).map(([key, value]) => `<div style="font-weight: bold">${key}</div>${value}`).join('');
};
const actions = {
save,
compile,
updatePreview,
};
document.querySelectorAll("[data-action]").forEach((c) => {
const action = c.dataset.action;
if (actions[action]) {
c.addEventListener("click", async (event) => {
c.disabled = true;
await actions[action](event);
c.disabled = false;
});
}
});
const debouncedSave = debounce(save, 900);
editor.onDidChangeModelContent(debouncedSave);
};
</script>
<script>
require.config({
paths: {
vs: "https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.30.1/min/vs",
},
});
function waitForEditor(editor, playground) {
const fn = () => {
if (window.onEditorLoad) {
window.onEditorLoad(editor, playground);
return;
}
setTimeout(fn, 500);
};
fn();
}
require(["vs/editor/editor.main"], async function () {
const name = location.pathname.replace("/edit/", "");
const source = await fetch("/preset/" + name);
const playground = document.getElementById("playground");
const editor = monaco.editor.create(document.getElementById("editor"), {
value: source.ok ? await source.text() : "",
language: "yaml",
automaticLayout: true,
});
waitForEditor(editor, playground);
});
</script>
</body>
</html>