Skip to content

Commit 4b46129

Browse files
committed
Add url query support and history state support to online IDE #386
1 parent 96bd991 commit 4b46129

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

tools/examplesAlias.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module.exports = {
2+
"beer": "while",
3+
"collatz": "角谷猜想",
4+
"collatz2": "考拉兹猜想(角谷猜想)",
5+
"divination": "春秋古筮法",
6+
"eightqueens": "八皇后問題",
7+
"euclidean": "歐幾里得法",
8+
"factorial": "階乘",
9+
"fibonacci": "斐波那契",
10+
"fibonacci2": "斐氏列",
11+
"fizzbuzz": "Fizz buzz",
12+
"hanoi": "漢諾塔",
13+
"helloworld": "問天地好在",
14+
"helloworld+": "問天地好在+",
15+
"import": "導入",
16+
"mandelbrot": "曼德博集",
17+
"mergesort": "歸併排序",
18+
"misc": "雜項",
19+
"modinv": "大衍求一術",
20+
"multiplication_table": "乘算口訣",
21+
"nested_fun": "函数",
22+
"obj": "对象",
23+
"pi_leibniz": "萊布尼茲圓周率估算",
24+
"quicksort": "快速排序",
25+
"quicksort_inplace": "快速排序2",
26+
"quine": "自產生程式",
27+
"quine2": "自產生程式2",
28+
"selectionsort": "選擇排序",
29+
"sieve": "埃氏篩",
30+
"sqrt_newton": "牛頓求根法",
31+
"turing": "圖靈機",
32+
};

tools/make_ide.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var fs = require("fs");
66
var execSync = require("child_process").execSync;
77
var parser = require("../src/parser");
88
var utils = require("./utils");
9+
const examplesAlias = require("./examplesAlias");
910

1011
var files = fs.readdirSync("../examples/").filter(x => x.endsWith(".wy"));
1112
var prgms = {};
@@ -18,9 +19,6 @@ for (var i = 0; i < files.length; i++) {
1819
var lib = utils.loadlib();
1920

2021
function main() {
21-
var ed = newEditor(prgms["mandelbrot"]);
22-
// var ln = newLineNo(ed);
23-
2422
let highlighted = true;
2523
let currentHighlightTimeout;
2624
const highlightCode = () => {
@@ -30,17 +28,46 @@ function main() {
3028
console.timeEnd("highlight");
3129
};
3230

31+
var makeTitle = (example) => {
32+
return (examplesAlias[example] || example) + " - wenyan-lang Online IDE";
33+
};
34+
3335
var sel = document.getElementById("pick-example");
3436
for (var k in prgms) {
3537
var opt = document.createElement("option");
3638
opt.value = k;
37-
opt.innerHTML = k;
39+
opt.text = examplesAlias[k] || k;
3840
sel.appendChild(opt);
3941
}
40-
sel.value = "mandelbrot";
42+
var match = location.search.match(/(?:^\?|&)example=([^&]+)/);
43+
match = match && decodeURIComponent(match[1]);
44+
var example = match || "mandelbrot";
45+
var ed = newEditor(prgms[example]);
46+
// var ln = newLineNo(ed);
47+
sel.value = example;
48+
document.title = makeTitle(example);
4149
sel.onchange = function() {
42-
ed.innerText = prgms[sel.value];
50+
var example = sel.value;
51+
if (!prgms[example]) {
52+
return;
53+
}
54+
var title = makeTitle(example);
55+
document.title = title;
56+
ed.innerText = prgms[example];
4357
run();
58+
if (history.pushState) {
59+
var url = "?example=" + encodeURIComponent(example);
60+
history.pushState({ example: example }, title, url);
61+
}
62+
};
63+
window.onpopstate = function (event) {
64+
var example = event.state && event.state.example;
65+
if (example && prgms[example]) {
66+
sel.value = example;
67+
document.title = makeTitle(example);
68+
ed.innerText = prgms[example];
69+
run();
70+
}
4471
};
4572
var autohl = document.getElementById("auto-hl");
4673
autohl.onchange = function() {
@@ -126,6 +153,7 @@ function main() {
126153
var html = `<!--GENERATED FILE, DO NOT READ-->
127154
<head>
128155
<meta charset="UTF-8">
156+
<title>wenyan-lang Online IDE</title>
129157
<style>
130158
[contenteditable="true"]:focus {outline: none;}
131159
pre{tab-size: 4;}
@@ -141,6 +169,7 @@ pre{tab-size: 4;}
141169
<table><tr><td><select id="pick-example"></select><button id="run">Run</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="auto-hl"/><small>Auto Highlight</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="hide-std"/><small>Hide Imported Code</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<small>Romanization</small><select id="pick-roman"></select></td></tr><tr><td id="in" valign="top"><div class="tbar">EDITOR</div></td><td rowspan="2" valign="top"><div class="tbar">COMPILED JAVASCRIPT</div><pre id="js"></pre></td></tr><tr><td valign="top"><div class="tbar">OUTPUT</div><pre id="out"></pre></td></tr></table>
142170
<script>var STDLIB = ${JSON.stringify(lib)};</script>
143171
<script>var prgms = ${JSON.stringify(prgms)};</script>
172+
<script>var examplesAlias = ${JSON.stringify(examplesAlias)};</script>
144173
<script>${main.toString()};main();</script>
145174
</body>
146175
`;

0 commit comments

Comments
 (0)