-
Notifications
You must be signed in to change notification settings - Fork 504
/
playground.ts
293 lines (270 loc) · 7.52 KB
/
playground.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import { EditorContent, SESSION_KEY } from "../../playground/utils";
import { PLAYGROUND } from "../../telemetry/constants";
const LIVE_SAMPLE_PARTS = ["html", "css", "js"];
const SECTION_RE = /h[1-6]/i;
function partOfSection(heading: Element, element: Element) {
if (
SECTION_RE.test(element.tagName) &&
element.tagName.toLowerCase() <= heading.tagName.toLowerCase()
) {
return false;
}
return true;
}
function sectionForHeading(heading: Element | null): Element[] {
const nodes: Element[] = [];
if (heading === null) {
return [];
}
if (!SECTION_RE.test(heading.tagName)) {
return [...heading.children];
}
let next = heading.nextElementSibling;
while (next && partOfSection(heading, next)) {
nodes.push(next);
if (next.nextElementSibling === null) {
next = next.parentElement?.nextElementSibling?.firstElementChild || null;
} else {
next = next.nextElementSibling;
}
}
return nodes;
}
function closestHeading(element: Element) {
let prev = element;
while (prev.parentElement?.firstElementChild) {
if (SECTION_RE.test(prev.parentElement.firstElementChild.tagName)) {
return prev.parentElement.firstElementChild;
}
prev = prev.parentElement;
}
return null;
}
function prevHeading(heading: Element) {
let prev = heading;
while (prev.parentElement?.previousElementSibling?.firstElementChild) {
prev = prev.parentElement.previousElementSibling.firstElementChild;
if (
SECTION_RE.test(prev.tagName) &&
prev.tagName.toLowerCase() < heading.tagName.toLowerCase()
) {
return prev;
}
}
return null;
}
export function addBreakoutButton(
element: Element | null,
id: string,
code: EditorContent,
locale: string
) {
if (!element || !element.querySelector("play-sample")) {
element?.classList.add("play-sample");
} else {
return;
}
if (!element || element.querySelector(".play-button")) return;
const button = document.createElement("button");
button.textContent = "play";
button.setAttribute("class", "play-button");
button.type = "button";
button.dataset.glean = `${PLAYGROUND}: breakout->${id}`;
button.title = "Open in Playground";
element.appendChild(button);
button.addEventListener("click", (e) => {
sessionStorage.setItem(SESSION_KEY, JSON.stringify(code));
const url = new URL(window?.location.href);
url.pathname = `/${locale}/play`;
url.hash = "";
url.search = "";
if (e.shiftKey === true) {
window.location.href = url.href;
} else {
window.open(url, "_blank");
}
});
}
export function collectCode(input?: HTMLInputElement): EditorContent {
const selected = [
...document.querySelectorAll(".example-header.play-collect > div > input"),
]
.filter((i) => (i as HTMLInputElement).checked || i === input)
.map((e) => e.parentElement?.parentElement?.nextElementSibling)
.filter(Boolean);
if (selected.length > 1 && !input?.checked) {
input?.click?.();
}
return {
js: selected
.map((pre) =>
pre?.classList.contains("js") || pre?.classList.contains("javascript")
? pre.textContent
: null
)
.filter(Boolean)
.join("\n"),
css: selected
.map((pre) => (pre?.classList.contains("css") ? pre.textContent : null))
.filter(Boolean)
.join("\n"),
html: selected
.map((pre) => (pre?.classList.contains("html") ? pre.textContent : null))
.filter(Boolean)
.join("\n"),
};
}
export function highlight(
header: Element,
highlightedQueueExample: string | null
) {
// Highlight the header if it's the one we're mouse-overing
// on the corresponding item in the queue.
const id = header.querySelector('[type="checkbox"]')?.id;
if (highlightedQueueExample === id) {
header.classList.add("active");
} else {
header.classList.remove("active");
}
}
export function addCollectButton(
element: Element | null,
id: string,
locale: string
) {
if (!element || !element.querySelector("play-collect")) {
element?.classList.add("play-collect");
} else {
return;
}
if (!element || element.querySelector(".play-button")) return;
if (
[...(element.nextElementSibling?.classList.values() || [])].filter((c) =>
["js", "javascript", "css", "html"].includes(c)
).length === 0
) {
return;
}
const checkId = crypto.randomUUID();
const playlist = document.createElement("div");
playlist.classList.add("playlist");
const checkLabel = document.createElement("label");
checkLabel.htmlFor = checkId;
checkLabel.textContent = "queue";
checkLabel.title = "Add to Playground Queue";
const check = document.createElement("input");
check.addEventListener("change", (e) => {
const ct = e.currentTarget as HTMLInputElement | null;
if (ct?.dataset) {
checkLabel.textContent = ct.checked ? "queued" : "queue";
checkLabel.title = ct.checked
? "Remove from Playground Queue"
: "Add to Playground Queue";
window["playQueue"]?.();
}
});
check.type = "checkbox";
check.id = checkId;
check.dataset.glean = `${PLAYGROUND}: queue->${id}`;
const button = document.createElement("button");
button.textContent = "play";
button.classList.add("play-button");
button.type = "button";
button.setAttribute("data-play", id);
button.dataset.glean = `${PLAYGROUND}: breakout->${id}`;
button.title = "Open in Playground";
playlist.appendChild(check);
playlist.appendChild(checkLabel);
playlist.appendChild(button);
element.appendChild(playlist);
button.addEventListener("click", (e) => {
const code = collectCode(check);
sessionStorage.setItem(SESSION_KEY, JSON.stringify(code));
const url = new URL(window?.location.href);
url.pathname = `/${locale}/play`;
url.hash = "";
url.search = "";
window.open(url, "_blank");
});
}
function codeForHeading(
heading: Element,
src: string
): { code: EditorContent; nodes: Element[] } | null {
const section = sectionForHeading(heading);
if (!section.length) {
return null;
}
const code: EditorContent = {
css: "",
html: "",
js: "",
src,
};
const nodes: Element[] = [];
for (const part of LIVE_SAMPLE_PARTS) {
const src = section
.flatMap((e) => [...e?.querySelectorAll(`pre.${part}`)])
.map((e) => {
nodes.push(e);
return e.textContent;
})
.join("\n");
if (src) {
code[part] += src;
}
}
return nodes.length ? { code, nodes } : null;
}
function getLanguage(node: Element): string | null {
for (const part of LIVE_SAMPLE_PARTS) {
if (node.classList.contains(part)) {
return part;
}
}
return null;
}
export function getCodeAndNodesForIframeBySampleClass(
cls: string,
src: string
) {
const code: EditorContent = {
css: "",
html: "",
js: "",
src,
};
let empty = true;
const nodes: Element[] = [];
[...document.getElementsByClassName(`live-sample___${cls}`)].forEach(
(pre) => {
let lang = getLanguage(pre);
if (lang === null) {
return;
}
empty = false;
nodes.push(pre);
code[lang] += pre.textContent;
}
);
return empty ? null : { code, nodes };
}
export function getCodeAndNodesForIframe(
id: string,
iframe: Element,
src: string
) {
let heading = document.getElementById(id) || closestHeading(iframe);
if (!heading) {
return null;
}
let r = codeForHeading(heading, src);
while (r === null) {
heading = prevHeading(heading);
if (heading === null) {
return null;
}
r = codeForHeading(heading, src);
}
return r;
}