@@ -6,6 +6,7 @@ var fs = require("fs");
6
6
var execSync = require ( "child_process" ) . execSync ;
7
7
var parser = require ( "../src/parser" ) ;
8
8
var utils = require ( "./utils" ) ;
9
+ const examplesAlias = require ( "./examplesAlias" ) ;
9
10
10
11
var files = fs . readdirSync ( "../examples/" ) . filter ( x => x . endsWith ( ".wy" ) ) ;
11
12
var prgms = { } ;
@@ -18,9 +19,6 @@ for (var i = 0; i < files.length; i++) {
18
19
var lib = utils . loadlib ( ) ;
19
20
20
21
function main ( ) {
21
- var ed = newEditor ( prgms [ "mandelbrot" ] ) ;
22
- // var ln = newLineNo(ed);
23
-
24
22
let highlighted = true ;
25
23
let currentHighlightTimeout ;
26
24
const highlightCode = ( ) => {
@@ -30,17 +28,46 @@ function main() {
30
28
console . timeEnd ( "highlight" ) ;
31
29
} ;
32
30
31
+ var makeTitle = ( example ) => {
32
+ return ( examplesAlias [ example ] || example ) + " - wenyan-lang Online IDE" ;
33
+ } ;
34
+
33
35
var sel = document . getElementById ( "pick-example" ) ;
34
36
for ( var k in prgms ) {
35
37
var opt = document . createElement ( "option" ) ;
36
38
opt . value = k ;
37
- opt . innerHTML = k ;
39
+ opt . text = examplesAlias [ k ] || k ;
38
40
sel . appendChild ( opt ) ;
39
41
}
40
- sel . value = "mandelbrot" ;
42
+ var match = location . search . match ( / (?: ^ \? | & ) e x a m p l e = ( [ ^ & ] + ) / ) ;
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 ) ;
41
49
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 ] ;
43
57
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
+ }
44
71
} ;
45
72
var autohl = document . getElementById ( "auto-hl" ) ;
46
73
autohl . onchange = function ( ) {
@@ -126,6 +153,7 @@ function main() {
126
153
var html = `<!--GENERATED FILE, DO NOT READ-->
127
154
<head>
128
155
<meta charset="UTF-8">
156
+ <title>wenyan-lang Online IDE</title>
129
157
<style>
130
158
[contenteditable="true"]:focus {outline: none;}
131
159
pre{tab-size: 4;}
@@ -141,6 +169,7 @@ pre{tab-size: 4;}
141
169
<table><tr><td><select id="pick-example"></select><button id="run">Run</button> <input type="checkbox" id="auto-hl"/><small>Auto Highlight</small> <input type="checkbox" id="hide-std"/><small>Hide Imported Code</small> <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>
142
170
<script>var STDLIB = ${ JSON . stringify ( lib ) } ;</script>
143
171
<script>var prgms = ${ JSON . stringify ( prgms ) } ;</script>
172
+ <script>var examplesAlias = ${ JSON . stringify ( examplesAlias ) } ;</script>
144
173
<script>${ main . toString ( ) } ;main();</script>
145
174
</body>
146
175
` ;
0 commit comments