-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.html
751 lines (518 loc) · 15.9 KB
/
index.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Does MrDoob Approve?</title>
</head>
<body>
<style>
body {
font-family: "Helvetica Neue", Helvetica, sans-serif;
padding: 0.5em;
background: #111;
color: #ccc;
transition: 1s;
text-shadow: #111 0px 1px;
}
.code {
display: block;
width: 100%;
height: 350px;
}
#code {
width: 100%;
height: 100%;
font-family: monospace;
font-size: 20px;
}
.CodeMirror {
border: 1px solid #bbb;
font-size: 20px;
}
.cm-trailingspace {
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAACCAYAAAB/qH1jAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3QUXCToH00Y1UgAAACFJREFUCNdjPMDBUc/AwNDAAAFMTAwMDA0OP34wQgX/AQBYgwYEx4f9lQAAAABJRU5ErkJggg==);
background-position: bottom left;
background-repeat: repeat-x;
}
h1 {
font-weight: 300;
font-size: 4em;
margin: 0.1em;
}
h2 {
display: block;
}
button {
padding: 0.5em;
margin-top: 0.5em;
font-size: 1em;
background: #bbb;
}
pre {
}
span {
}
#answer {
transition: opacity 2s ease-out;
opacity: 0;
}
.yes {
color: green;
}
.no {
color: red;
}
.hints {
color: #ddd;
text-shadow: #333 1px 1px;
}
.maybe {
color: orange;
}
.show {
display: block;
}
.hide {
display: none;
}
a {
text-decoration: none;
font-style: normal;
}
a:hover {
text-decoration: underline;
/*text-decoration: none;*/
/*font-weight: bolder;*/
/*font-style: italic;*/
}
.preset {
font-size: 1em;
padding: 0.2em;
background: transparent;
width: 300px;
border-radius: 0;
/*border: 0;
border-bottom: 1px solid #333;*/
-webkit-outline: 0;
color: #fff;
}
</style>
<h1>Does <select class="preset" onchange="setPreset(this.value)">
<option value="mdcs">mrdoob</option>
<!--<option>airbnb</option>
<option>crockford</option>
<option>google</option>
<option>grunt</option>
<option>jquery</option>
<option>mdcs</option>
<option>node-style-guide</option>
<option>wikimedia</option>
<option>wordpress</option>
<option>yandex</option>-->
</select> approve your code style?
</h1>
</div>
<div id="merge" class="code">
<textarea id="code" placeholder="paste code here">
/* Mr Doob Dislikes */
for (var j=0;j<100;j++) {
fail();
}
if(moo) {
ho();
}else{
nah
}
/* Mr Doob Approves */
async function mrdoob() {
for ( var i = 0; i < 100; i ++ ) {
hearts();
}
}
var es_function = () => {}
</textarea>
</div>
<div id="loading">Loading</div>
<script src="build/mdcs_bundle.js"></script>
<script src="mdcs_eslint.js"></script>
<script src="build/eslint.js"></script>
<br/>
<span id="hints"></span>
<br />
<button style="margin-right: 20px" onclick="fix()">Auto Format All The Code!</button>
<span style="display:none;">
<label>Auto</label> <input type="checkbox" id="autocheck" checked onclick="checkbutton.disabled = this.checked" />
<button id="checkbutton" onclick="check()" disabled="true">Check</button>
</span>
<button onclick="toggleMergePanels()">Toggle Diff Pane</button>
<button onclick="openFile()">Open File</button>
<button onclick="saveFile()">Save File</button>
<h2><span id="answer" /></h2>
<p>
Here is a editor that validates + formats javascript codestyle primarily to mrdoob's liking..
</p>
<i>Disclaimer: the name of this tool is also used without the permission of <a href="https://twitter.com/mrdoob">mrdoob</a>.</i>
<script>
var eslint = new eslint.Linter();
var textarea = document.getElementById( 'code' );
var answer = document.getElementById( 'answer' );
var hints = document.getElementById( 'hints' );
var autocheck = document.getElementById( 'autocheck' );
var merge = document.getElementById( 'merge' );
var presets = document.querySelector('.preset');
Object.keys(eslint_configs).forEach(k => {
presets.options[presets.options.length] = new Option(k);
});
var deferredValidator;
var cmOptions = {
value: textarea.value,
mode: 'javascript',
indentUnit: 4,
indentWithTabs: true,
lineNumbers: true,
matchBrackets: true,
autoCloseBrackets: true,
showTrailingSpace: true,
gutters: [ 'CodeMirror-lint-markers' ],
lint: {
async: true,
getAnnotations: CodeMirror.asyncValidator,
requestValidation: function( text, validator ) {
deferredValidator = validator;
if ( autocheck.checked ) validator( text );
},
checker: function(text) {
// ESLint Hook
return processESLintRules(text);
}
},
// merge plugin - for our case "orginal" means our "formatted" version.
origRight: '',
// connect: 'align',
collapseIdentical: true,
allowEditingOriginals: false,
showDifferences: true,
revertButtons: true
};
var checker, codeEditor, visibleError;
var loading = document.getElementById( 'loading' );
var pannelsOpened = false;
var _mdcs_eslint = mdcs_eslint;
loading.style.display = 'none';
const severity_map = {
1: 'warning',
2: 'error'
};
function getPos(error, from) {
var line = error.line-1, ch = from ? error.column : error.column+1;
if (error.node && error.node.loc) {
line = from ? error.node.loc.start.line -1 : error.node.loc.end.line -1;
ch = from ? error.node.loc.start.column : error.node.loc.end.column;
}
return CodeMirror.Pos(line, ch);
}
function mrdoobEsLint(text) {
return eslint.verify(text, eslint_configs[presets.value] || _mdcs_eslint);
}
function processESLintRules(text) {
const messages = mrdoobEsLint(text);
const errors = messages.filter( m => m.severity === 2);
setError(messages.filter(m => m.fix)[0]);
if ( text.trim().length === 0 ) {
setAnswer( 'Maybe... :)', 'maybe' );
return;
}
if ( errors.length === 0 ) {
setAnswer( 'Possibly \\o/*\\o', 'yes' );
} else {
setAnswer( 'No!!! :(', 'no' );
}
const lints = messages.map(m => {
return {
message: m.message,
severity: severity_map[m.severity] || 'error',
from: getPos(m, true),
to: getPos(m, false)
}
});
// console.log(lints);
return lints;
}
//
function init() {
/*
* Init Code CodeMirror
*/
textarea.className = 'hide';
mergeView = CodeMirror.MergeView( merge, cmOptions );
codeEditor = mergeView.editor();
formatEditor = mergeView.rightOriginal();
closeMergePanels( true );
}
init();
function setPreset( preset ) {
const v = eslint_configs[presets.value];
if (v) {
rules.value = JSON.stringify(v, null, '\t');
}
check();
}
function hideAnswer() {
answer.style.opacity = 0;
}
function setAnswer( comment, style ) {
answer.style.opacity = 1;
answer.textContent = comment;
answer.className = style;
document.body.style.background = style === 'no' ? 'rgba(150, 0, 0, 0.7)' : 'rgba(0, 150, 0, 0.7)';
setTimeout( hideAnswer, 3000 );
}
// checking code - check + lint errors..
// if split spane open, auto-format that
// auto-formating coding - fix up style
// if split pane open, close autofix panel
function check() {
var code = codeEditor.getValue();
if ( deferredValidator ) deferredValidator( code );
if ( pannelsOpened ) format();
}
function setError( error ) {
visibleError = error;
if ( visibleError ) {
hints.innerHTML = `
<a href="#" class="hints" onclick="return autofix();">Fix!</a>
<a href="#" class="hints" onclick="return gotoError();" >[${visibleError.line}:${visibleError.column}]</a>
\t
Error:
<i>${visibleError.message}</i>
`;
//
} else {
hints.textContent = '';
}
}
function autofix() {
if ( visibleError ) {
let text = codeEditor.getValue();
const v = visibleError.fix;
text = text.substring(0, v.range[0]) + v.text + text.substring(v.range[1]);
var target = pannelsOpened ? formatEditor : codeEditor;
target.setValue( text );
}
}
function gotoError() {
if ( visibleError ) {
var line = visibleError.line - 1;
var scrollInfo = codeEditor.getScrollInfo();
var coords = codeEditor.charCoords( { line: line, ch: visibleError.column }, "local" );
codeEditor.setCursor( line, visibleError.column, { scroll: false } );
if ( scrollInfo.top > coords.bottom || scrollInfo.top + scrollInfo.clientHeight < coords.top ) {
// out of view
codeEditor.scrollTo( null, coords.bottom - scrollInfo.clientHeight / 2 );
}
codeEditor.focus();
}
}
function fix() {
format();
if ( pannelsOpened ) {
mergeAll();
}
}
function preprocess( codeWrapper ) {
var code = codeWrapper.code;
// TODO refactor into more generic rule engine
// TODO make it work with checking too
// pass - leading indentation
var lead = /(\s*)\S/.exec( code );
if ( ! lead ) return code;
var leads = lead[ 1 ].split( '\n' );
var indentation = leads.pop();
// console.log( 'indentation', JSON.stringify( indentation ), leads.length );
codeWrapper.code = code
.split( '\n' )
.map( removeIfPrefixed.bind( indentation ) )
.join( '\n' );
codeWrapper.leadingLines = leads.length;
codeWrapper.indentation = indentation;
}
function removeIfPrefixed( string ) {
if ( string.indexOf( this ) !== 0 ) return string;
return string.substring( this.length );
}
function postprocess( wrapper ) {
var code = wrapper.code;
// pass restore leading indentation
var lines = code.split( '\n' );
lines = lines.map( function( line ) {
return wrapper.indentation + line;
} )
// fix trailing spaces
wrapper.code = lines.map( function( x ) {
return x.replace( /\s*$/, '' )
} ).join( '\n' );
}
/*
* Support Auto-correcting
*/
function format() {
var code = codeEditor.getValue();
var codeWrapper = {
code: code
};
preprocess( codeWrapper );
let errors = false;
let fixes;
try {
console.time( 'formating' );
var fixed = codeWrapper.code;
const messages = mrdoobEsLint( fixed );
fixes = messages
.map(e => e.fix)
.filter(e => !!e)
.sort( (a, b) => { return b.range[0] - a.range[0] } );
fixes
.forEach( v => {
fixed = fixed.substring(0, v.range[0]) + v.text + fixed.substring(v.range[1]);
})
codeWrapper.code = fixed;
postprocess( codeWrapper )
var target = pannelsOpened ? formatEditor : codeEditor;
target.setValue( codeWrapper.code );
console.log( 'errors', fixes );
} catch ( e ) {
console.log( e );
errors = e;
} finally {
if ( errors ) {
console.log( 'oops, there is something that is not fixed...' )
}
console.timeEnd( 'formating' );
}
}
function openMergePanels() {
mergeView.wrapEdit.style.width = ''; // 47%
mergeView.wrapRight.style.display = '';
mergeView.right.gap.style.display = '';
// mergeView.wrapEdit.style.transition = 'width 0.2s ease-out';
mergeView.setShowDifferences( true );
pannelsOpened = true;
}
function closeMergePanels( init ) {
mergeView.wrapRight.style.display = 'none';
mergeView.right.gap.style.display = 'none';
mergeView.wrapEdit.style.width = '100%';
if ( init ) {
setTimeout( function() {
mergeView.wrapEdit.style.transition = 'width 0.35s ease-out';
} );
}
mergeView.setShowDifferences( false );
pannelsOpened = false;
}
function toggleMergePanels() {
if ( ! pannelsOpened ) {
openMergePanels();
format();
} else {
closeMergePanels();
}
}
function mergeAll() {
codeEditor.setValue( formatEditor.getValue() );
closeMergePanels();
}
/*
* Open file
*/
var input, openCallback;
function openFile() {
openAs( function( text ) {
codeEditor.setValue( text );
}, document.body );
}
function handleFileSelect( evt ) {
var files = evt.target.files; // FileList object
var f = files[ 0 ];
if ( ! f ) return;
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = function( e ) {
var data = e.target.result;
openCallback( data );
};
reader.readAsText( f );
input.value = '';
}
function openAs( callback, target ) {
console.log( 'openfile...' );
openCallback = callback;
if ( ! input ) {
input = document.createElement( 'input' );
input.style.display = 'none';
input.type = 'file';
input.addEventListener( 'change', handleFileSelect );
target = target || document.body;
target.appendChild( input );
}
var e = document.createEvent( 'MouseEvents' );
e.initMouseEvent(
'click', true, false, window, 0, 0, 0, 0, 0,
false, false, false, false, 0, null
);
input.dispatchEvent( e );
}
function saveFile() {
var text = codeEditor.getValue();
if ( !! navigator.userAgent.match( /Version\/[\d\.]+.*Safari/ ) ) {
// safari exception
return window.open( 'data:attachment/js;charset=utf-8,' + encodeURIComponent( text ) );
;
}
var filename = 'mdcs.js'
filename = prompt( 'Save as filename', filename );
if ( ! filename ) return;
var blob = new Blob( [ text ], { type: 'text/plain' } );
var objectURL = URL.createObjectURL( blob );
var link = document.createElement( 'a' );
link.href = objectURL;
link.download = filename;
link.target = '_blank';
var event = document.createEvent( 'MouseEvents' );
event.initMouseEvent(
'click', true, false, window, 0, 0, 0, 0, 0
, false, false, false, false, 0, null
);
link.dispatchEvent( event );
}
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7549263-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<a href="https://github.com/zz85/mrdoobapproves" class="github-corner" aria-label="View source on Github"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
<p>
<pre onclick="rules.style.display = rules.style.display !=='block' ? 'block' : 'none'">Toggle Rules</pre>
<textarea style="width: 80%; height: 400px; display: none;" id="rules"></textarea>
<script>
rules.value = JSON.stringify(mdcs_eslint, 0, '\t');
rules.onkeyup = () => {
console.log('changed');
try {
_mdcs_eslint = JSON.parse(rules.value);
console.log('ok')
}
catch (e) {
console.log('failed to parse rules', e)
}
}
</script>
</p>
</body>
</html>