forked from ghinda/jotted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jotted.js
1946 lines (1548 loc) · 53.5 KB
/
jotted.js
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.Jotted = factory());
}(this, (function () { 'use strict';
/* util
*/
function extend() {
var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var defaults = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var extended = {};
// clone object
Object.keys(obj).forEach(function (key) {
extended[key] = obj[key];
});
// copy default keys where undefined
Object.keys(defaults).forEach(function (key) {
if (typeof extended[key] !== 'undefined') {
extended[key] = obj[key];
} else {
extended[key] = defaults[key];
}
});
return extended;
}
function fetch(url, callback) {
var xhr = new window.XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'text';
xhr.onload = function () {
if (xhr.status === 200) {
callback(null, xhr.responseText);
} else {
callback(url, xhr);
}
};
xhr.onerror = function (err) {
callback(err);
};
xhr.send();
}
function runCallback(index, params, arr, errors, callback) {
return function (err, res) {
if (err) {
errors.push(err);
}
index++;
if (index < arr.length) {
seqRunner(index, res, arr, errors, callback);
} else {
callback(errors, res);
}
};
}
function seqRunner(index, params, arr, errors, callback) {
// async
arr[index](params, runCallback.apply(this, arguments));
}
function seq(arr, params) {
var callback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () {};
var errors = [];
if (!arr.length) {
return callback(errors, params);
}
seqRunner(0, params, arr, errors, callback);
}
function debounce(fn, delay) {
var cooldown = null;
var multiple = null;
return function () {
var _this = this,
_arguments = arguments;
if (cooldown) {
multiple = true;
} else {
fn.apply(this, arguments);
}
clearTimeout(cooldown);
cooldown = setTimeout(function () {
if (multiple) {
fn.apply(_this, _arguments);
}
cooldown = null;
multiple = null;
}, delay);
};
}
function hasClass(node, className) {
if (!node.className) {
return false;
}
var tempClass = ' ' + node.className + ' ';
className = ' ' + className + ' ';
if (tempClass.indexOf(className) !== -1) {
return true;
}
return false;
}
function addClass(node, className) {
// class is already added
if (hasClass(node, className)) {
return node.className;
}
if (node.className) {
className = ' ' + className;
}
node.className += className;
return node.className;
}
function removeClass(node, className) {
var spaceBefore = ' ' + className;
var spaceAfter = className + ' ';
if (node.className.indexOf(spaceBefore) !== -1) {
node.className = node.className.replace(spaceBefore, '');
} else if (node.className.indexOf(spaceAfter) !== -1) {
node.className = node.className.replace(spaceAfter, '');
} else {
node.className = node.className.replace(className, '');
}
return node.className;
}
function data(node, attr) {
return node.getAttribute('data-' + attr);
}
// mode detection based on content type and file extension
var defaultModemap = {
'html': 'html',
'css': 'css',
'js': 'javascript',
'less': 'less',
'styl': 'stylus',
'coffee': 'coffeescript'
};
function getMode() {
var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var file = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var customModemap = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var modemap = extend(customModemap, defaultModemap);
// try the file extension
for (var key in modemap) {
var keyLength = key.length;
if (file.slice(-keyLength++) === '.' + key) {
return modemap[key];
}
}
// try the file type (html/css/js)
for (var _key in modemap) {
if (type === _key) {
return modemap[_key];
}
}
return type;
}
/* template
*/
function container() {
return '\n <ul class="jotted-nav">\n <li class="jotted-nav-item jotted-nav-item-result">\n <a href="#" data-jotted-type="result">\n Result\n </a>\n </li>\n <li class="jotted-nav-item jotted-nav-item-html">\n <a href="#" data-jotted-type="html">\n HTML\n </a>\n </li>\n <li class="jotted-nav-item jotted-nav-item-css">\n <a href="#" data-jotted-type="css">\n CSS\n </a>\n </li>\n <li class="jotted-nav-item jotted-nav-item-js">\n <a href="#" data-jotted-type="js">\n JavaScript\n </a>\n </li>\n </ul>\n <div class="jotted-pane jotted-pane-result"><iframe></iframe></div>\n <div class="jotted-pane jotted-pane-html"></div>\n <div class="jotted-pane jotted-pane-css"></div>\n <div class="jotted-pane jotted-pane-js"></div>\n ';
}
function paneActiveClass(type) {
return 'jotted-pane-active-' + type;
}
function containerClass() {
return 'jotted';
}
function hasFileClass(type) {
return 'jotted-has-' + type;
}
function editorClass(type) {
return 'jotted-editor jotted-editor-' + type;
}
function editorContent(type) {
var fileUrl = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
return '\n <textarea data-jotted-type="' + type + '" data-jotted-file="' + fileUrl + '"></textarea>\n <div class="jotted-status"></div>\n ';
}
function statusMessage(err) {
return '\n <p>' + err + '</p>\n ';
}
function statusClass(type) {
return 'jotted-status-' + type;
}
function statusActiveClass(type) {
return 'jotted-status-active-' + type;
}
function pluginClass(name) {
return 'jotted-plugin-' + name;
}
function statusLoading(url) {
return 'Loading <strong>' + url + '</strong>..';
}
function statusFetchError(url) {
return 'There was an error loading <strong>' + url + '</strong>.';
}
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
var asyncGenerator = function () {
function AwaitValue(value) {
this.value = value;
}
function AsyncGenerator(gen) {
var front, back;
function send(key, arg) {
return new Promise(function (resolve, reject) {
var request = {
key: key,
arg: arg,
resolve: resolve,
reject: reject,
next: null
};
if (back) {
back = back.next = request;
} else {
front = back = request;
resume(key, arg);
}
});
}
function resume(key, arg) {
try {
var result = gen[key](arg);
var value = result.value;
if (value instanceof AwaitValue) {
Promise.resolve(value.value).then(function (arg) {
resume("next", arg);
}, function (arg) {
resume("throw", arg);
});
} else {
settle(result.done ? "return" : "normal", result.value);
}
} catch (err) {
settle("throw", err);
}
}
function settle(type, value) {
switch (type) {
case "return":
front.resolve({
value: value,
done: true
});
break;
case "throw":
front.reject(value);
break;
default:
front.resolve({
value: value,
done: false
});
break;
}
front = front.next;
if (front) {
resume(front.key, front.arg);
} else {
back = null;
}
}
this._invoke = send;
if (typeof gen.return !== "function") {
this.return = undefined;
}
}
if (typeof Symbol === "function" && Symbol.asyncIterator) {
AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
return this;
};
}
AsyncGenerator.prototype.next = function (arg) {
return this._invoke("next", arg);
};
AsyncGenerator.prototype.throw = function (arg) {
return this._invoke("throw", arg);
};
AsyncGenerator.prototype.return = function (arg) {
return this._invoke("return", arg);
};
return {
wrap: function (fn) {
return function () {
return new AsyncGenerator(fn.apply(this, arguments));
};
},
await: function (value) {
return new AwaitValue(value);
}
};
}();
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var get = function get(object, property, receiver) {
if (object === null) object = Function.prototype;
var desc = Object.getOwnPropertyDescriptor(object, property);
if (desc === undefined) {
var parent = Object.getPrototypeOf(object);
if (parent === null) {
return undefined;
} else {
return get(parent, property, receiver);
}
} else if ("value" in desc) {
return desc.value;
} else {
var getter = desc.get;
if (getter === undefined) {
return undefined;
}
return getter.call(receiver);
}
};
var set = function set(object, property, value, receiver) {
var desc = Object.getOwnPropertyDescriptor(object, property);
if (desc === undefined) {
var parent = Object.getPrototypeOf(object);
if (parent !== null) {
set(parent, property, value, receiver);
}
} else if ("value" in desc && desc.writable) {
desc.value = value;
} else {
var setter = desc.set;
if (setter !== undefined) {
setter.call(receiver, value);
}
}
return value;
};
/* plugin
*/
var plugins = [];
function find$1(id) {
for (var pluginIndex in plugins) {
var plugin = plugins[pluginIndex];
if (plugin._id === id) {
return plugin;
}
}
// can't find plugin
throw new Error('Plugin ' + id + ' is not registered.');
}
function register(id, plugin) {
// private properties
plugin._id = id;
plugins.push(plugin);
}
// create a new instance of each plugin, on the jotted instance
function init() {
var _this = this;
this._get('options').plugins.forEach(function (plugin) {
// check if plugin definition is string or object
var Plugin = void 0;
var pluginName = void 0;
var pluginOptions = {};
if (typeof plugin === 'string') {
pluginName = plugin;
} else if ((typeof plugin === 'undefined' ? 'undefined' : _typeof(plugin)) === 'object') {
pluginName = plugin.name;
pluginOptions = plugin.options || {};
}
Plugin = find$1(pluginName);
_this._get('plugins')[plugin] = new Plugin(_this, pluginOptions);
addClass(_this._get('$container'), pluginClass(pluginName));
});
}
/* pubsoup
*/
var PubSoup = function () {
function PubSoup() {
classCallCheck(this, PubSoup);
this.topics = {};
this.callbacks = {};
}
createClass(PubSoup, [{
key: 'find',
value: function find(query) {
this.topics[query] = this.topics[query] || [];
return this.topics[query];
}
}, {
key: 'subscribe',
value: function subscribe(topic, subscriber) {
var priority = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 90;
var foundTopic = this.find(topic);
subscriber._priority = priority;
foundTopic.push(subscriber);
// sort subscribers on priority
foundTopic.sort(function (a, b) {
return a._priority > b._priority ? 1 : b._priority > a._priority ? -1 : 0;
});
}
// removes a function from an array
}, {
key: 'remover',
value: function remover(arr, fn) {
arr.forEach(function () {
// if no fn is specified
// clean-up the array
if (!fn) {
arr.length = 0;
return;
}
// find the fn in the arr
var index = [].indexOf.call(arr, fn);
// we didn't find it in the array
if (index === -1) {
return;
}
arr.splice(index, 1);
});
}
}, {
key: 'unsubscribe',
value: function unsubscribe(topic, subscriber) {
// remove from subscribers
var foundTopic = this.find(topic);
this.remover(foundTopic, subscriber);
// remove from callbacks
this.callbacks[topic] = this.callbacks[topic] || [];
this.remover(this.callbacks[topic], subscriber);
}
// sequentially runs a method on all plugins
}, {
key: 'publish',
value: function publish(topic) {
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var foundTopic = this.find(topic);
var runList = [];
foundTopic.forEach(function (subscriber) {
runList.push(subscriber);
});
seq(runList, params, this.runCallbacks(topic));
}
// parallel run all .done callbacks
}, {
key: 'runCallbacks',
value: function runCallbacks(topic) {
var _this = this;
return function (err, params) {
_this.callbacks[topic] = _this.callbacks[topic] || [];
_this.callbacks[topic].forEach(function (c) {
c(err, params);
});
};
}
// attach a callback when a publish[topic] is done
}, {
key: 'done',
value: function done(topic) {
var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
this.callbacks[topic] = this.callbacks[topic] || [];
this.callbacks[topic].push(callback);
}
}]);
return PubSoup;
}();
/* render plugin
* renders the iframe
*/
var PluginRender = function () {
function PluginRender(jotted, options) {
classCallCheck(this, PluginRender);
options = extend(options, {});
// iframe srcdoc support
var supportSrcdoc = !!('srcdoc' in document.createElement('iframe'));
var $resultFrame = jotted.$container.querySelector('.jotted-pane-result iframe');
var frameContent = '';
// cached content
var content = {
html: '',
css: '',
js: ''
};
// catch domready events from the iframe
window.addEventListener('message', this.domready.bind(this));
// render on each change
jotted.on('change', this.change.bind(this), 100);
// public
this.supportSrcdoc = supportSrcdoc;
this.content = content;
this.frameContent = frameContent;
this.$resultFrame = $resultFrame;
this.callbacks = [];
this.index = 0;
this.lastCallback = function () {};
}
createClass(PluginRender, [{
key: 'template',
value: function template() {
var style = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var script = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
return '\n <!doctype html>\n <html>\n <head>\n <script>\n (function () {\n window.addEventListener(\'DOMContentLoaded\', function () {\n window.parent.postMessage(JSON.stringify({\n type: \'jotted-dom-ready\'\n }), \'*\')\n })\n }())\n </script>\n\n <style>' + style + '</style>\n </head>\n <body>\n ' + body + '\n\n <!--\n Jotted:\n Empty script tag prevents malformed HTML from breaking the next script.\n -->\n <script></script>\n <script>' + script + '</script>\n </body>\n </html>\n ';
}
}, {
key: 'change',
value: function change(params, callback) {
var _this = this;
// cache manipulated content
this.content[params.type] = params.content;
// check existing and to-be-rendered content
var oldFrameContent = this.frameContent;
this.frameContent = this.template(this.content['css'], this.content['html'], this.content['js']);
// cache the current callback as global,
// so we can call it from the message callback.
this.lastCallback = function () {
_this.lastCallback = function () {};
callback(null, params);
};
// don't render if previous and new frame content are the same.
// mostly for the `play` plugin,
// so we don't re-render the same content on each change.
// unless we set forceRender.
if (params.forceRender !== true && this.frameContent === oldFrameContent) {
callback(null, params);
return;
}
if (this.supportSrcdoc) {
// srcdoc in unreliable in Chrome.
// https://github.com/ghinda/jotted/issues/23
this.$resultFrame.contentWindow.document.open();
this.$resultFrame.contentWindow.document.write(this.frameContent);
this.$resultFrame.contentWindow.document.close();
} else {
// older browsers without iframe srcset support (IE9).
this.$resultFrame.setAttribute('data-srcdoc', this.frameContent);
// tips from https://github.com/jugglinmike/srcdoc-polyfill
// Copyright (c) 2012 Mike Pennisi
// Licensed under the MIT license.
var jsUrl = 'javascript:window.frameElement.getAttribute("data-srcdoc");';
this.$resultFrame.setAttribute('src', jsUrl);
// Explicitly set the iFrame's window.location for
// compatibility with IE9, which does not react to changes in
// the `src` attribute when it is a `javascript:` URL.
if (this.$resultFrame.contentWindow) {
this.$resultFrame.contentWindow.location = jsUrl;
}
}
}
}, {
key: 'domready',
value: function domready(e) {
// only catch messages from the iframe
if (e.source !== this.$resultFrame.contentWindow) {
return;
}
var data$$1 = {};
try {
data$$1 = JSON.parse(e.data);
} catch (e) {}
if (data$$1.type === 'jotted-dom-ready') {
this.lastCallback();
}
}
}]);
return PluginRender;
}();
/* scriptless plugin
* removes script tags from html content
*/
var PluginScriptless = function () {
function PluginScriptless(jotted, options) {
classCallCheck(this, PluginScriptless);
options = extend(options, {});
// https://html.spec.whatwg.org/multipage/scripting.html
var runScriptTypes = ['application/javascript', 'application/ecmascript', 'application/x-ecmascript', 'application/x-javascript', 'text/ecmascript', 'text/javascript', 'text/javascript1.0', 'text/javascript1.1', 'text/javascript1.2', 'text/javascript1.3', 'text/javascript1.4', 'text/javascript1.5', 'text/jscript', 'text/livescript', 'text/x-ecmascript', 'text/x-javascript'];
// remove script tags on each change
jotted.on('change', this.change.bind(this));
// public
this.runScriptTypes = runScriptTypes;
}
createClass(PluginScriptless, [{
key: 'change',
value: function change(params, callback) {
if (params.type !== 'html') {
return callback(null, params);
}
// for IE9 support, remove the script tags from HTML content.
// when we stop supporting IE9, we can use the sandbox attribute.
var fragment = document.createElement('div');
fragment.innerHTML = params.content;
var typeAttr = null;
// remove all script tags
var $scripts = fragment.querySelectorAll('script');
for (var i = 0; i < $scripts.length; i++) {
typeAttr = $scripts[i].getAttribute('type');
// only remove script tags without the type attribute
// or with a javascript mime attribute value.
// the ones that are run by the browser.
if (!typeAttr || this.runScriptTypes.indexOf(typeAttr) !== -1) {
$scripts[i].parentNode.removeChild($scripts[i]);
}
}
params.content = fragment.innerHTML;
callback(null, params);
}
}]);
return PluginScriptless;
}();
/* ace plugin
*/
var PluginAce = function () {
function PluginAce(jotted, options) {
classCallCheck(this, PluginAce);
var priority = 1;
var i;
this.editor = {};
this.jotted = jotted;
options = extend(options, {});
// check if Ace is loaded
if (typeof window.ace === 'undefined') {
return;
}
var $editors = jotted.$container.querySelectorAll('.jotted-editor');
for (i = 0; i < $editors.length; i++) {
var $textarea = $editors[i].querySelector('textarea');
var type = data($textarea, 'jotted-type');
var file = data($textarea, 'jotted-file');
var $aceContainer = document.createElement('div');
$editors[i].appendChild($aceContainer);
this.editor[type] = window.ace.edit($aceContainer);
var editor = this.editor[type];
var editorOptions = extend(options);
editor.getSession().setMode('ace/mode/' + getMode(type, file));
editor.getSession().setOptions(editorOptions);
editor.$blockScrolling = Infinity;
}
jotted.on('change', this.change.bind(this), priority);
}
createClass(PluginAce, [{
key: 'editorChange',
value: function editorChange(params) {
var _this = this;
return function () {
_this.jotted.trigger('change', params);
};
}
}, {
key: 'change',
value: function change(params, callback) {
var editor = this.editor[params.type];
// if the event is not started by the ace change.
// triggered only once per editor,
// when the textarea is populated/file is loaded.
if (!params.aceEditor) {
editor.getSession().setValue(params.content);
// attach the event only after the file is loaded
params.aceEditor = editor;
editor.on('change', this.editorChange(params));
}
// manipulate the params and pass them on
params.content = editor.getValue();
callback(null, params);
}
}]);
return PluginAce;
}();
/* coremirror plugin
*/
var PluginCodeMirror = function () {
function PluginCodeMirror(jotted, options) {
classCallCheck(this, PluginCodeMirror);
var priority = 1;
var i;
this.editor = {};
this.jotted = jotted;
// custom modemap for codemirror
var modemap = {
'html': 'htmlmixed'
};
options = extend(options, {
lineNumbers: true
});
// check if CodeMirror is loaded
if (typeof window.CodeMirror === 'undefined') {
return;
}
var $editors = jotted.$container.querySelectorAll('.jotted-editor');
for (i = 0; i < $editors.length; i++) {
var $textarea = $editors[i].querySelector('textarea');
var type = data($textarea, 'jotted-type');
var file = data($textarea, 'jotted-file');
this.editor[type] = window.CodeMirror.fromTextArea($textarea, options);
this.editor[type].setOption('mode', getMode(type, file, modemap));
}
jotted.on('change', this.change.bind(this), priority);
}
createClass(PluginCodeMirror, [{
key: 'editorChange',
value: function editorChange(params) {
var _this = this;
return function () {
// trigger a change event
_this.jotted.trigger('change', params);
};
}
}, {
key: 'change',
value: function change(params, callback) {
var editor = this.editor[params.type];
// if the event is not started by the codemirror change.
// triggered only once per editor,
// when the textarea is populated/file is loaded.
if (!params.cmEditor) {
editor.setValue(params.content);
// attach the event only after the file is loaded
params.cmEditor = editor;
editor.on('change', this.editorChange(params));
}
// manipulate the params and pass them on
params.content = editor.getValue();
callback(null, params);
}
}]);
return PluginCodeMirror;
}();
/* less plugin
*/
var PluginLess = function () {
function PluginLess(jotted, options) {
classCallCheck(this, PluginLess);
var priority = 20;
options = extend(options, {});
// check if less is loaded
if (typeof window.less === 'undefined') {
return;
}
// change CSS link label to Less
jotted.$container.querySelector('a[data-jotted-type="css"]').innerHTML = 'Less';
jotted.on('change', this.change.bind(this), priority);
}
createClass(PluginLess, [{
key: 'isLess',
value: function isLess(params) {
if (params.type !== 'css') {
return false;
}
return params.file.indexOf('.less') !== -1 || params.file === '';
}
}, {
key: 'change',
value: function change(params, callback) {
// only parse .less and blank files
if (this.isLess(params)) {
window.less.render(params.content, this.options, function (err, res) {
if (err) {
return callback(err, params);
} else {
// replace the content with the parsed less
params.content = res.css;