Skip to content

Commit 5021984

Browse files
authored
feat: Adds a nifty HTML page to create a JSFiddle of any sample. (#887)
1 parent 963f02f commit 5021984

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

samples/create-fiddle.html

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Create JSFiddle</title>
5+
<style>
6+
body { font-family: sans-serif; display: grid; place-content: center; text-align: left; height: 100vh; margin: 0; }
7+
button { font-size: 1.5em; padding: 0.5em 1em; cursor: pointer; }
8+
#error { color: red; font-weight: bold; font-size: smaller; margin-top: 5px; }
9+
#sample-name-input { font-size: 1.5em; padding: 0.5em 1em; margin-bottom: 5px;}
10+
</style>
11+
</head>
12+
<body>
13+
14+
<h1 id="title">Create a JSFiddle!!!</h1>
15+
<p>Create a JSFiddle from any sample in a local branch of js-api-samples.</p>
16+
<ol>
17+
<li>Run <code>npm run build --workspace=sample-name</code> to build the needed sample.</li>
18+
<li>Enter the sample name and click <b>Create Fiddle!</b></li>
19+
<li>A fiddle will open in a new browser tab.</li>
20+
<li>Update the default name and save the fiddle.</li>
21+
<li>Copy the URL to send to collaborators!</li>
22+
</ol>
23+
24+
<input id="sample-name-input" placeholder="Enter a sample name..." autocomplete="off"></input>
25+
<button id="create-fiddle-btn">Create Fiddle!</button>
26+
<div id="error"></div>
27+
28+
<!-- This form will be populated and submitted by the script -->
29+
<form id="fiddle-form" action="https://jsfiddle.net/api/post/library/pure/" method="post" target="_blank" style="display: none;">
30+
<textarea name="html"></textarea>
31+
<textarea name="css"></textarea>
32+
<textarea name="js"></textarea>
33+
<input type="text" name="resources" />
34+
<input type="text" name="title" />
35+
</form>
36+
37+
<script>
38+
const sampleInput = document.querySelector('#sample-name-input');
39+
const button = document.getElementById('create-fiddle-btn');
40+
const titleEl = document.getElementById('title');
41+
const errorEl = document.getElementById('error');
42+
43+
44+
button.addEventListener('click', createFiddle);
45+
46+
async function createFiddle() {
47+
errorEl.textContent = '';
48+
titleEl.textContent = 'Creating Fiddle...';
49+
50+
const sampleName = sampleInput.value;
51+
52+
const form = document.getElementById('fiddle-form');
53+
const basePath = `../dist/samples/${sampleName}/jsfiddle`;
54+
55+
try {
56+
// 1. Fetch the file contents from the dist directory
57+
const [htmlResponse, cssResponse, jsResponse] = await Promise.all([
58+
fetch(`${basePath}/demo.html`),
59+
fetch(`${basePath}/demo.css`),
60+
fetch(`${basePath}/demo.js`)
61+
]);
62+
63+
if (!htmlResponse.ok || !cssResponse.ok || !jsResponse.ok) {
64+
throw new Error(`Could not find files in ${basePath}. Double-check the sample name.`);
65+
}
66+
67+
const htmlContent = await htmlResponse.text();
68+
const cssContent = await cssResponse.text();
69+
const jsContent = await jsResponse.text();
70+
71+
// 2. Prepare the code for JSFiddle.
72+
const bodyContent = htmlContent.match(/<html>([\s\S]*)<\/html>/i)[1] || '';
73+
74+
// 3. Populate the form fields
75+
form.querySelector('textarea[name="html"]').value = bodyContent.trim();
76+
form.querySelector('textarea[name="css"]').value = cssContent;
77+
form.querySelector('textarea[name="js"]').value = jsContent.trim();
78+
form.querySelector('input[name="title"]').value = `Maps JS API Sample: ${sampleName}`;
79+
80+
// 4. Submit the form to create the fiddle
81+
form.submit();
82+
} catch (e) {
83+
errorEl.textContent = e.message;
84+
}
85+
}
86+
</script>
87+
88+
</body>
89+
</html>

0 commit comments

Comments
 (0)