@taufik-nurrohman/text-editor.source 3.0.1 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
package/index.js CHANGED
@@ -33,19 +33,19 @@
33
33
  var isArray = function isArray(x) {
34
34
  return Array.isArray(x);
35
35
  };
36
- var isDefined = function isDefined(x) {
36
+ var isDefined$1 = function isDefined(x) {
37
37
  return 'undefined' !== typeof x;
38
38
  };
39
39
  var isFunction = function isFunction(x) {
40
40
  return 'function' === typeof x;
41
41
  };
42
- var isInstance = function isInstance(x, of) {
43
- return x && isSet(of) && x instanceof of ;
42
+ var isInstance$1 = function isInstance(x, of) {
43
+ return x && isSet$1(of) && x instanceof of ;
44
44
  };
45
45
  var isInteger = function isInteger(x) {
46
46
  return isNumber(x) && 0 === x % 1;
47
47
  };
48
- var isNull = function isNull(x) {
48
+ var isNull$1 = function isNull(x) {
49
49
  return null === x;
50
50
  };
51
51
  var isNumber = function isNumber(x) {
@@ -58,10 +58,10 @@
58
58
  if ('object' !== typeof x) {
59
59
  return false;
60
60
  }
61
- return isPlain ? isInstance(x, Object) : true;
61
+ return isPlain ? isInstance$1(x, Object) : true;
62
62
  };
63
- var isSet = function isSet(x) {
64
- return isDefined(x) && !isNull(x);
63
+ var isSet$1 = function isSet(x) {
64
+ return isDefined$1(x) && !isNull$1(x);
65
65
  };
66
66
  var toCount = function toCount(x) {
67
67
  return x.length;
@@ -77,7 +77,7 @@
77
77
  for (var i = 0, j = toCount(lot); i < j; ++i) {
78
78
  for (var k in lot[i]) {
79
79
  // Assign value
80
- if (!isSet(out[k])) {
80
+ if (!isSet$1(out[k])) {
81
81
  out[k] = lot[i][k];
82
82
  continue;
83
83
  }
@@ -116,6 +116,18 @@
116
116
  var offEventDefault = function offEventDefault(e) {
117
117
  return e && e.preventDefault();
118
118
  };
119
+ var isDefined = function isDefined(x) {
120
+ return 'undefined' !== typeof x;
121
+ };
122
+ var isInstance = function isInstance(x, of) {
123
+ return x && isSet(of) && x instanceof of ;
124
+ };
125
+ var isNull = function isNull(x) {
126
+ return null === x;
127
+ };
128
+ var isSet = function isSet(x) {
129
+ return isDefined(x) && !isNull(x);
130
+ };
119
131
  var isPattern = function isPattern(pattern) {
120
132
  return isInstance(pattern, RegExp);
121
133
  };
@@ -131,9 +143,9 @@
131
143
  var bounce = debounce(function ($) {
132
144
  return $.record();
133
145
  }, 10);
146
+ var name = 'TextEditor.Source';
134
147
 
135
148
  function onKeyDown(e) {
136
- var _$$state$source, _$$state$source2;
137
149
  var $ = this,
138
150
  key = $.k(false).pop(),
139
151
  // Capture the last key
@@ -144,8 +156,8 @@
144
156
  }
145
157
  var charAfter,
146
158
  charBefore,
147
- charIndent = ((_$$state$source = $.state.source) == null ? void 0 : _$$state$source.tab) || $.state.tab || '\t',
148
- charPairs = ((_$$state$source2 = $.state.source) == null ? void 0 : _$$state$source2.pairs) || {},
159
+ charIndent = $.state.tab || '\t',
160
+ charPairs = $.state.pairs || {},
149
161
  charPairsValues = toObjectValues(charPairs);
150
162
  if (isInteger(charIndent)) {
151
163
  charIndent = ' '.repeat(charIndent);
@@ -339,27 +351,27 @@
339
351
 
340
352
  function attach() {
341
353
  var $ = this;
354
+ var $$ = $.constructor.prototype;
342
355
  $.state = fromStates({
343
- source: {
344
- pairs: {
345
- '`': '`',
346
- '(': ')',
347
- '{': '}',
348
- '[': ']',
349
- '"': '"',
350
- "'": "'",
351
- '<': '>'
352
- }
356
+ pairs: {
357
+ '`': '`',
358
+ '(': ')',
359
+ '{': '}',
360
+ '[': ']',
361
+ '"': '"',
362
+ "'": "'",
363
+ '<': '>'
353
364
  }
354
365
  }, $.state);
355
- $.alert = function (hint, then) {
366
+ !isFunction($$.alert) && ($$.alert = function (hint, then) {
356
367
  W.alert && W.alert(hint);
357
- return isFunction(then) && then.call($, true);
358
- };
359
- $.confirm = function (hint, then) {
360
- return isFunction(then) && then.call($, W.confirm && W.confirm(hint));
361
- };
362
- $.insertBlock = function (value, mode) {
368
+ return isFunction(then) && then.call(this, true);
369
+ });
370
+ !isFunction($$.confirm) && ($$.confirm = function (hint, then) {
371
+ return isFunction(then) && then.call(this, W.confirm && W.confirm(hint));
372
+ });
373
+ !isFunction($$.insertBlock) && ($$.insertBlock = function (value, mode) {
374
+ var $ = this;
363
375
  var _$$$2 = $.$(),
364
376
  after = _$$$2.after,
365
377
  before = _$$$2.before,
@@ -378,8 +390,9 @@
378
390
  return $.select(end + lineAfterCount).insert('\n', -1).push(lineMatchIndent).insert(value, 1, false);
379
391
  }
380
392
  return $.select(start - lineBeforeCount, end + lineAfterCount).insert(value, mode, true).wrap(lineMatchIndent, "");
381
- };
382
- $.peelBlock = function (open, close, wrap) {
393
+ });
394
+ !isFunction($$.peelBlock) && ($$.peelBlock = function (open, close, wrap) {
395
+ var $ = this;
383
396
  var _$$$3 = $.$(),
384
397
  after = _$$$3.after,
385
398
  before = _$$$3.before,
@@ -396,23 +409,50 @@
396
409
  return $.select(start - lineBeforeCount + (wrap ? 0 : openCount), end + lineAfterCount - (wrap ? 0 : closeCount)).peel(open, close, wrap);
397
410
  }
398
411
  return $.select(start, end);
399
- };
400
- $.prompt = function (hint, value, then) {
401
- return isFunction(then) && then.call($, W.prompt ? W.prompt(hint, value) : false);
402
- };
403
- $.selectBlock = function () {
412
+ });
413
+ !isFunction($$.prompt) && ($$.prompt = function (hint, value, then) {
414
+ return isFunction(then) && then.call(this, W.prompt ? W.prompt(hint, value) : false);
415
+ });
416
+ !isFunction($$.selectBlock) && ($$.selectBlock = function (withSpaces) {
417
+ if (withSpaces === void 0) {
418
+ withSpaces = true;
419
+ }
420
+ var $ = this;
404
421
  var _$$$4 = $.$(),
405
422
  after = _$$$4.after,
406
423
  before = _$$$4.before,
407
424
  end = _$$$4.end,
408
425
  start = _$$$4.start,
426
+ value = _$$$4.value,
409
427
  lineAfter = after.split('\n').shift(),
410
428
  lineAfterCount = toCount(lineAfter),
411
429
  lineBefore = before.split('\n').pop(),
412
430
  lineBeforeCount = toCount(lineBefore);
413
- return $.select(start - lineBeforeCount, end + lineAfterCount);
414
- };
415
- $.toggle = function (open, close, wrap) {
431
+ if (!withSpaces) {
432
+ var lineAfterSpaces = /\s+$/.exec(lineAfter),
433
+ lineBeforeSpaces = /^\s+/.exec(lineBefore);
434
+ if (lineAfterSpaces) {
435
+ lineAfterCount -= toCount(lineAfterSpaces[0]);
436
+ }
437
+ if (lineBeforeSpaces) {
438
+ lineBeforeCount -= toCount(lineBeforeSpaces[0]);
439
+ }
440
+ }
441
+ $.select(start - lineBeforeCount, end + lineAfterCount);
442
+ if (!withSpaces) {
443
+ var s = $.$(),
444
+ m;
445
+ end = s.end;
446
+ start = s.start;
447
+ value = s.value;
448
+ if (m = /^(\s+)?[\s\S]+?(\s+)?$/.exec(value)) {
449
+ return $.select(start + toCount(m[1] || ""), end - toCount(m[2] || ""));
450
+ }
451
+ }
452
+ return $;
453
+ });
454
+ !isFunction($$.toggle) && ($$.toggle = function (open, close, wrap) {
455
+ var $ = this;
416
456
  var _$$$5 = $.$(),
417
457
  after = _$$$5.after,
418
458
  before = _$$$5.before,
@@ -423,8 +463,9 @@
423
463
  return $.peel(open, close, wrap);
424
464
  }
425
465
  return $.wrap(open, close, wrap);
426
- };
427
- $.toggleBlock = function (open, close, wrap) {
466
+ });
467
+ !isFunction($$.toggleBlock) && ($$.toggleBlock = function (open, close, wrap) {
468
+ var $ = this;
428
469
  var _$$$6 = $.$(),
429
470
  after = _$$$6.after,
430
471
  before = _$$$6.before,
@@ -437,8 +478,9 @@
437
478
  return $.peelBlock(open, close, wrap);
438
479
  }
439
480
  return $.wrapBlock(open, close, wrap);
440
- };
441
- $.wrapBlock = function (open, close, wrap) {
481
+ });
482
+ !isFunction($$.wrapBlock) && ($$.wrapBlock = function (open, close, wrap) {
483
+ var $ = this;
442
484
  var _$$$7 = $.$(),
443
485
  after = _$$$7.after,
444
486
  before = _$$$7.before,
@@ -449,7 +491,7 @@
449
491
  lineBefore = before.split('\n').pop(),
450
492
  lineBeforeCount = toCount(lineBefore);
451
493
  return $.select(start - lineBeforeCount, end + lineAfterCount).wrap(open, close, wrap);
452
- };
494
+ });
453
495
  return $.on('key.down', onKeyDown).record();
454
496
  }
455
497
 
@@ -458,7 +500,8 @@
458
500
  }
459
501
  var index_js = {
460
502
  attach: attach,
461
- detach: detach
503
+ detach: detach,
504
+ name: name
462
505
  };
463
506
  return index_js;
464
507
  }));
package/index.min.js CHANGED
@@ -23,4 +23,4 @@
23
23
  * SOFTWARE.
24
24
  *
25
25
  */
26
- !function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):((e="undefined"!=typeof globalThis?globalThis:e||self).TextEditor=e.TextEditor||{},e.TextEditor.Source=r())}(this,(function(){"use strict";var e,r,t,n=function(e,r){return-1!==r.indexOf(e)},i=function(e){return Array.isArray(e)},o=function(e){return"function"==typeof e},s=function(e,r){return e&&u(r)&&e instanceof r},c=function(e){return l(e)&&0==e%1},l=function(e){return"number"==typeof e},f=function(e,r){return void 0===r&&(r=!0),"object"==typeof e&&(!r||s(e,Object))},u=function(e){return function(e){return void 0!==e}(e)&&!function(e){return null===e}(e)},a=function(e){return e.length},p=function(e){return Object.values(e)},d=function e(){for(var r=arguments.length,t=Array(r),o=0;o<r;o++)t[o]=arguments[o];for(var s=t.shift(),c=0,l=a(t);c<l;++c)for(var p in t[c])if(u(s[p]))if(i(s[p])&&i(t[c][p])){s[p]=[].concat(s[p]);for(var d=0,h=a(t[c][p]);d<h;++d)n(t[c][p][d],s[p])||s[p].push(t[c][p][d])}else f(s[p])&&f(t[c][p])?s[p]=e({},s[p],t[c][p]):s[p]=t[c][p];else s[p]=t[c][p];return s},h=window,v=function(e){return e&&e.preventDefault()},w=function(e,r){return function(e){return s(e,RegExp)}(e)?e:RegExp(e,u(r)?r:"g")},b="Alt-",$="Control-",m="Shift-",k=(e=function(e){return e.record()},r=10,function(){var n=arguments,i=this;t&&clearTimeout(t),t=setTimeout((function(){return e.apply(i,n)}),r)});function y(e){var r,t,i=this,o=i.k(!1).pop(),s=i.k();if(k(i),!e.defaultPrevented&&!i.keys[s]){var l,f,u=(null==(r=i.state.source)?void 0:r.tab)||i.state.tab||"\t",d=(null==(t=i.state.source)?void 0:t.pairs)||{},h=p(d);c(u)&&(u=" ".repeat(u));var y=i.$(),g=y.after,x=y.before,E=y.end,A=y.start,B=y.value,T=g.split("\n").shift(),W=x.split("\n").pop(),j=/^\s+/.exec(W),D=j&&j[0]||"";if($+m+"Enter"===s)return x||g?(v(e),i.select(A-a(W)).wrap(D,"\n").insert(B).record(),!1):void 0;if($+"Enter"===s&&(x||g))return v(e),i.select(E+a(T)).wrap("\n"+D,"").insert(B).record(),!1;if(b!==s+"-"&&$!==s+"-"){if(" "===s)return l=d[f=x.slice(-1)],!B&&l&&f&&l===g[0]?(v(e),i.wrap(" "," ")):void 0;if("Backspace"===s||"Delete"===s){if(l=d[f=x.slice(-1)],"\\"===f)return;return B?g&&x&&l&&l===g[0]&&!x.endsWith("\\"+f)?(v(e),i.record().peel(f,l).record()):void 0:(l=d[f=x.trim().slice(-1)])&&f&&(g.startsWith(" "+l)&&x.endsWith(f+" ")||g.startsWith("\n"+D+l)&&x.endsWith(f+"\n"+D))?(v(e),i.trim("","").record()):"Delete"!==s&&W.endsWith(u)?(v(e),i.pull(u).record()):g&&x&&!x.endsWith("\\"+f)&&l===g[0]&&f===x.slice(-1)?(v(e),i.peel(f,l).record()):void 0}if("Enter"!==s&&m+"Enter"!==s){if("\\"!==(f=x.slice(-1))){if(l=n(g[0],h)?g[0]:d[f],!B&&g&&x&&l&&o===l)return v(e),i.select(A+1).record();for(f in d){if(l=d[f],o===f&&l)return v(e),i.wrap(f,l).record();if(o===l){if(B)return v(e),i.record().wrap(f,l).record();break}}var O,R,S,C=[];if(B){for(O in d)(R=d[O])&&C.push("(?:\\"+O+"(?:\\\\.|[^\\"+O+(R!==O?"\\"+R:"")+"])*\\"+R+")");if(C.push("\\w+"),C.push("\\s+"),C.push("[\\s\\S]"),$+"ArrowLeft"===s)return v(e),(S=w("("+C.join("|")+")$","").exec(x))?i.insert("").select(A-a(S[0])).insert(B).record():i.select();if($+"ArrowRight"===s)return v(e),(S=g.match(w("^("+C.join("|")+")","")))?i.insert("").select(E+a(S[0])-a(B)).insert(B).record():i.select()}if(E+=a(T),A-=a(W),B=W+B+T,$+"ArrowUp"===s){if(v(e),!n("\n",x))return i.select();i.insert(""),i.replace(/^([^\n]*?)(\n|$)/,"$2",1),i.replace(/(^|\n)([^\n]*?)$/,"",-1);var L=i.$();return x=L.before,A=L.start,W=x.split("\n").pop(),i.select(A-=a(W)).wrap(B,"\n"),i.select(A,A+a(B)),i.record()}if($+"ArrowDown"===s){if(v(e),!n("\n",g))return i.select();i.insert(""),i.replace(/^([^\n]*?)(\n|$)/,"",1),i.replace(/(^|\n)([^\n]*?)$/,"$1",-1);var P=i.$();return g=P.after,E=P.end,T=g.split("\n").shift(),i.select(E+=a(T)).wrap("\n",B),E+=1,i.select(E,E+a(B)),i.record()}}}else if(!B){if(g&&x&&(l=d[f=x.slice(-1)])&&l===g[0])return v(e),i.wrap("\n"+D+(f!==l?u:""),"\n"+D).record();if(D)return v(e),i.insert("\n"+D,-1).record()}}else v(e)}}var g={attach:function(){var e=this;return e.state=d({source:{pairs:{"`":"`","(":")","{":"}","[":"]",'"':'"',"'":"'","<":">"}}},e.state),e.alert=function(r,t){return h.alert&&h.alert(r),o(t)&&t.call(e,!0)},e.confirm=function(r,t){return o(t)&&t.call(e,h.confirm&&h.confirm(r))},e.insertBlock=function(r,t){var n=e.$(),i=n.after,o=n.before,s=n.end,c=n.start,l=i.split("\n").shift(),f=a(l),u=o.split("\n").pop(),p=a(u),d=/^\s+/.exec(u),h=d&&d[0]||"";return-1===t?e.select(c-p).insert("\n",1).push(h).insert(r,1,!1):1===t?e.select(s+f).insert("\n",-1).push(h).insert(r,1,!1):e.select(c-p,s+f).insert(r,t,!0).wrap(h,"")},e.peelBlock=function(r,t,n){var i=e.$(),o=i.after,s=i.before,c=i.end,l=i.start,f=i.value,u=a(t),p=o.split("\n").shift(),d=a(p),h=s.split("\n").pop(),v=a(h),w=a(r);return n&&t===f.slice(-u)&&r===f.slice(0,w)||t===p.slice(-u)&&r===h.slice(0,w)?e.select(l-v+(n?0:w),c+d-(n?0:u)).peel(r,t,n):e.select(l,c)},e.prompt=function(r,t,n){return o(n)&&n.call(e,!!h.prompt&&h.prompt(r,t))},e.selectBlock=function(){var r=e.$(),t=r.after,n=r.before,i=r.end,o=r.start,s=t.split("\n").shift(),c=a(s),l=n.split("\n").pop(),f=a(l);return e.select(o-f,i+c)},e.toggle=function(r,t,n){var i=e.$(),o=i.after,s=i.before,c=i.value,l=a(t),f=a(r);return n&&t===c.slice(-l)&&r===c.slice(0,f)||t===o.slice(0,l)&&r===s.slice(-f)?e.peel(r,t,n):e.wrap(r,t,n)},e.toggleBlock=function(r,t,n){var i=e.$(),o=i.after,s=i.before,c=i.value,l=a(t),f=o.split("\n").shift(),u=s.split("\n").pop(),p=a(r);return n&&t===c.slice(-l)&&r===c.slice(0,p)||t===f.slice(-l)&&r===u.slice(0,p)?e.peelBlock(r,t,n):e.wrapBlock(r,t,n)},e.wrapBlock=function(r,t,n){var i=e.$(),o=i.after,s=i.before,c=i.end,l=i.start,f=o.split("\n").shift(),u=a(f),p=s.split("\n").pop(),d=a(p);return e.select(l-d,c+u).wrap(r,t,n)},e.on("key.down",y).record()},detach:function(){return this.off("key.down",y)}};return g}));
26
+ !function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):((e="undefined"!=typeof globalThis?globalThis:e||self).TextEditor=e.TextEditor||{},e.TextEditor.Source=r())}(this,(function(){"use strict";var e,r,t,n=function(e,r){return-1!==r.indexOf(e)},i=function(e){return Array.isArray(e)},o=function(e){return"function"==typeof e},s=function(e){return"number"==typeof e},c=function(e,r){return void 0===r&&(r=!0),"object"==typeof e&&(!r||function(e,r){return e&&l(r)&&e instanceof r}(e,Object))},l=function(e){return function(e){return void 0!==e}(e)&&!function(e){return null===e}(e)},f=function(e){return e.length},u=function e(){for(var r=arguments.length,t=Array(r),o=0;o<r;o++)t[o]=arguments[o];for(var s=t.shift(),u=0,a=f(t);u<a;++u)for(var p in t[u])if(l(s[p]))if(i(s[p])&&i(t[u][p])){s[p]=[].concat(s[p]);for(var d=0,h=f(t[u][p]);d<h;++d)n(t[u][p][d],s[p])||s[p].push(t[u][p][d])}else c(s[p])&&c(t[u][p])?s[p]=e({},s[p],t[u][p]):s[p]=t[u][p];else s[p]=t[u][p];return s},a=window,p=function(e){return e&&e.preventDefault()},d=function(e){return function(e){return void 0!==e}(e)&&!function(e){return null===e}(e)},h=function(e){return t=RegExp,(r=e)&&d(t)&&r instanceof t;var r,t},v=function(e,r){return h(e)?e:RegExp(e,d(r)?r:"g")},w="Control-",k="Shift-",$=(e=function(e){return e.record()},r=10,function(){var n=arguments,i=this;t&&clearTimeout(t),t=setTimeout((function(){return e.apply(i,n)}),r)});function m(e){var r=this,t=r.k(!1).pop(),i=r.k();if($(r),!e.defaultPrevented&&!r.keys[i]){var o,c,l=r.state.tab||"\t",u=r.state.pairs||{},a=Object.values(u);(function(e){return s(e)&&0==e%1})(l)&&(l=" ".repeat(l));var d=r.$(),h=d.after,m=d.before,b=d.end,g=d.start,x=d.value,y=h.split("\n").shift(),B=m.split("\n").pop(),E=/^\s+/.exec(B),A=E&&E[0]||"";if(w+k+"Enter"===i)return m||h?(p(e),r.select(g-f(B)).wrap(A,"\n").insert(x).record(),!1):void 0;if(w+"Enter"===i&&(m||h))return p(e),r.select(b+f(y)).wrap("\n"+A,"").insert(x).record(),!1;if("Alt-"!=i+"-"&&w!==i+"-"){if(" "===i)return o=u[c=m.slice(-1)],!x&&o&&c&&o===h[0]?(p(e),r.wrap(" "," ")):void 0;if("Backspace"===i||"Delete"===i){if(o=u[c=m.slice(-1)],"\\"===c)return;return x?h&&m&&o&&o===h[0]&&!m.endsWith("\\"+c)?(p(e),r.record().peel(c,o).record()):void 0:(o=u[c=m.trim().slice(-1)])&&c&&(h.startsWith(" "+o)&&m.endsWith(c+" ")||h.startsWith("\n"+A+o)&&m.endsWith(c+"\n"+A))?(p(e),r.trim("","").record()):"Delete"!==i&&B.endsWith(l)?(p(e),r.pull(l).record()):h&&m&&!m.endsWith("\\"+c)&&o===h[0]&&c===m.slice(-1)?(p(e),r.peel(c,o).record()):void 0}if("Enter"!==i&&k+"Enter"!==i){if("\\"!==(c=m.slice(-1))){if(o=n(h[0],a)?h[0]:u[c],!x&&h&&m&&o&&t===o)return p(e),r.select(g+1).record();for(c in u){if(o=u[c],t===c&&o)return p(e),r.wrap(c,o).record();if(t===o){if(x)return p(e),r.record().wrap(c,o).record();break}}var T,W,j,S=[];if(x){for(T in u)(W=u[T])&&S.push("(?:\\"+T+"(?:\\\\.|[^\\"+T+(W!==T?"\\"+W:"")+"])*\\"+W+")");if(S.push("\\w+"),S.push("\\s+"),S.push("[\\s\\S]"),w+"ArrowLeft"===i)return p(e),(j=v("("+S.join("|")+")$","").exec(m))?r.insert("").select(g-f(j[0])).insert(x).record():r.select();if(w+"ArrowRight"===i)return p(e),(j=h.match(v("^("+S.join("|")+")","")))?r.insert("").select(b+f(j[0])-f(x)).insert(x).record():r.select()}if(b+=f(y),g-=f(B),x=B+x+y,w+"ArrowUp"===i){if(p(e),!n("\n",m))return r.select();r.insert(""),r.replace(/^([^\n]*?)(\n|$)/,"$2",1),r.replace(/(^|\n)([^\n]*?)$/,"",-1);var D=r.$();return m=D.before,g=D.start,B=m.split("\n").pop(),r.select(g-=f(B)).wrap(x,"\n"),r.select(g,g+f(x)),r.record()}if(w+"ArrowDown"===i){if(p(e),!n("\n",h))return r.select();r.insert(""),r.replace(/^([^\n]*?)(\n|$)/,"",1),r.replace(/(^|\n)([^\n]*?)$/,"$1",-1);var O=r.$();return h=O.after,b=O.end,y=h.split("\n").shift(),r.select(b+=f(y)).wrap("\n",x),b+=1,r.select(b,b+f(x)),r.record()}}}else if(!x){if(h&&m&&(o=u[c=m.slice(-1)])&&o===h[0])return p(e),r.wrap("\n"+A+(c!==o?l:""),"\n"+A).record();if(A)return p(e),r.insert("\n"+A,-1).record()}}else p(e)}}var b={attach:function(){var e=this,r=e.constructor.prototype;return e.state=u({pairs:{"`":"`","(":")","{":"}","[":"]",'"':'"',"'":"'","<":">"}},e.state),!o(r.alert)&&(r.alert=function(e,r){return a.alert&&a.alert(e),o(r)&&r.call(this,!0)}),!o(r.confirm)&&(r.confirm=function(e,r){return o(r)&&r.call(this,a.confirm&&a.confirm(e))}),!o(r.insertBlock)&&(r.insertBlock=function(e,r){var t=this,n=t.$(),i=n.after,o=n.before,s=n.end,c=n.start,l=i.split("\n").shift(),u=f(l),a=o.split("\n").pop(),p=f(a),d=/^\s+/.exec(a),h=d&&d[0]||"";return-1===r?t.select(c-p).insert("\n",1).push(h).insert(e,1,!1):1===r?t.select(s+u).insert("\n",-1).push(h).insert(e,1,!1):t.select(c-p,s+u).insert(e,r,!0).wrap(h,"")}),!o(r.peelBlock)&&(r.peelBlock=function(e,r,t){var n=this,i=n.$(),o=i.after,s=i.before,c=i.end,l=i.start,u=i.value,a=f(r),p=o.split("\n").shift(),d=f(p),h=s.split("\n").pop(),v=f(h),w=f(e);return t&&r===u.slice(-a)&&e===u.slice(0,w)||r===p.slice(-a)&&e===h.slice(0,w)?n.select(l-v+(t?0:w),c+d-(t?0:a)).peel(e,r,t):n.select(l,c)}),!o(r.prompt)&&(r.prompt=function(e,r,t){return o(t)&&t.call(this,!!a.prompt&&a.prompt(e,r))}),!o(r.selectBlock)&&(r.selectBlock=function(e){void 0===e&&(e=!0);var r=this,t=r.$(),n=t.after,i=t.before,o=t.end,s=t.start,c=t.value,l=n.split("\n").shift(),u=f(l),a=i.split("\n").pop(),p=f(a);if(!e){var d=/\s+$/.exec(l),h=/^\s+/.exec(a);d&&(u-=f(d[0])),h&&(p-=f(h[0]))}if(r.select(s-p,o+u),!e){var v,w=r.$();if(o=w.end,s=w.start,c=w.value,v=/^(\s+)?[\s\S]+?(\s+)?$/.exec(c))return r.select(s+f(v[1]||""),o-f(v[2]||""))}return r}),!o(r.toggle)&&(r.toggle=function(e,r,t){var n=this,i=n.$(),o=i.after,s=i.before,c=i.value,l=f(r),u=f(e);return t&&r===c.slice(-l)&&e===c.slice(0,u)||r===o.slice(0,l)&&e===s.slice(-u)?n.peel(e,r,t):n.wrap(e,r,t)}),!o(r.toggleBlock)&&(r.toggleBlock=function(e,r,t){var n=this,i=n.$(),o=i.after,s=i.before,c=i.value,l=f(r),u=o.split("\n").shift(),a=s.split("\n").pop(),p=f(e);return t&&r===c.slice(-l)&&e===c.slice(0,p)||r===u.slice(-l)&&e===a.slice(0,p)?n.peelBlock(e,r,t):n.wrapBlock(e,r,t)}),!o(r.wrapBlock)&&(r.wrapBlock=function(e,r,t){var n=this.$(),i=n.after,o=n.before,s=n.end,c=n.start,l=i.split("\n").shift(),u=f(l),a=o.split("\n").pop(),p=f(a);return this.select(c-p,s+u).wrap(e,r,t)}),e.on("key.down",m).record()},detach:function(){return this.off("key.down",m)},name:"TextEditor.Source"};return b}));
package/index.mjs CHANGED
@@ -12,6 +12,7 @@ const CTRL_PREFIX = 'Control-';
12
12
  const SHIFT_PREFIX = 'Shift-';
13
13
 
14
14
  const bounce = debounce($ => $.record(), 10);
15
+ const name = 'TextEditor.Source';
15
16
 
16
17
  function onKeyDown(e) {
17
18
  let $ = this,
@@ -23,8 +24,8 @@ function onKeyDown(e) {
23
24
  }
24
25
  let charAfter,
25
26
  charBefore,
26
- charIndent = $.state.source?.tab || $.state.tab || '\t',
27
- charPairs = $.state.source?.pairs || {},
27
+ charIndent = $.state.tab || '\t',
28
+ charPairs = $.state.pairs || {},
28
29
  charPairsValues = toObjectValues(charPairs);
29
30
  if (isInteger(charIndent)) {
30
31
  charIndent = ' '.repeat(charIndent);
@@ -214,28 +215,28 @@ function onKeyDown(e) {
214
215
  }
215
216
 
216
217
  function attach() {
217
- let $ = this;
218
+ const $ = this;
219
+ const $$ = $.constructor.prototype;
218
220
  $.state = fromStates({
219
- source: {
220
- pairs: {
221
- '`': '`',
222
- '(': ')',
223
- '{': '}',
224
- '[': ']',
225
- '"': '"',
226
- "'": "'",
227
- '<': '>'
228
- }
221
+ pairs: {
222
+ '`': '`',
223
+ '(': ')',
224
+ '{': '}',
225
+ '[': ']',
226
+ '"': '"',
227
+ "'": "'",
228
+ '<': '>'
229
229
  }
230
230
  }, $.state);
231
- $.alert = (hint, then) => {
231
+ !isFunction($$.alert) && ($$.alert = function (hint, then) {
232
232
  W.alert && W.alert(hint);
233
- return isFunction(then) && then.call($, true);
234
- };
235
- $.confirm = (hint, then) => {
236
- return isFunction(then) && then.call($, W.confirm && W.confirm(hint));
237
- };
238
- $.insertBlock = (value, mode) => {
233
+ return isFunction(then) && then.call(this, true);
234
+ });
235
+ !isFunction($$.confirm) && ($$.confirm = function (hint, then) {
236
+ return isFunction(then) && then.call(this, W.confirm && W.confirm(hint));
237
+ });
238
+ !isFunction($$.insertBlock) && ($$.insertBlock = function (value, mode) {
239
+ let $ = this;
239
240
  let {after, before, end, start} = $.$(),
240
241
  lineAfter = after.split('\n').shift(),
241
242
  lineAfterCount = toCount(lineAfter),
@@ -250,8 +251,9 @@ function attach() {
250
251
  return $.select(end + lineAfterCount).insert('\n', -1).push(lineMatchIndent).insert(value, 1, false);
251
252
  }
252
253
  return $.select(start - lineBeforeCount, end + lineAfterCount).insert(value, mode, true).wrap(lineMatchIndent, "");
253
- };
254
- $.peelBlock = (open, close, wrap) => {
254
+ });
255
+ !isFunction($$.peelBlock) && ($$.peelBlock = function (open, close, wrap) {
256
+ let $ = this;
255
257
  let {after, before, end, start, value} = $.$(),
256
258
  closeCount = toCount(close),
257
259
  lineAfter = after.split('\n').shift(),
@@ -266,19 +268,41 @@ function attach() {
266
268
  return $.select(start - lineBeforeCount + (wrap ? 0 : openCount), end + lineAfterCount - (wrap ? 0 : closeCount)).peel(open, close, wrap);
267
269
  }
268
270
  return $.select(start, end);
269
- };
270
- $.prompt = (hint, value, then) => {
271
- return isFunction(then) && then.call($, W.prompt ? W.prompt(hint, value) : false);
272
- };
273
- $.selectBlock = () => {
274
- let {after, before, end, start} = $.$(),
271
+ });
272
+ !isFunction($$.prompt) && ($$.prompt = function (hint, value, then) {
273
+ return isFunction(then) && then.call(this, W.prompt ? W.prompt(hint, value) : false);
274
+ });
275
+ !isFunction($$.selectBlock) && ($$.selectBlock = function (withSpaces = true) {
276
+ let $ = this;
277
+ let {after, before, end, start, value} = $.$(),
275
278
  lineAfter = after.split('\n').shift(),
276
279
  lineAfterCount = toCount(lineAfter),
277
280
  lineBefore = before.split('\n').pop(),
278
281
  lineBeforeCount = toCount(lineBefore);
279
- return $.select(start - lineBeforeCount, end + lineAfterCount);
280
- };
281
- $.toggle = (open, close, wrap) => {
282
+ if (!withSpaces) {
283
+ let lineAfterSpaces = /\s+$/.exec(lineAfter),
284
+ lineBeforeSpaces = /^\s+/.exec(lineBefore);
285
+ if (lineAfterSpaces) {
286
+ lineAfterCount -= toCount(lineAfterSpaces[0]);
287
+ }
288
+ if (lineBeforeSpaces) {
289
+ lineBeforeCount -= toCount(lineBeforeSpaces[0]);
290
+ }
291
+ }
292
+ $.select(start - lineBeforeCount, end + lineAfterCount);
293
+ if (!withSpaces) {
294
+ let s = $.$(), m;
295
+ end = s.end;
296
+ start = s.start;
297
+ value = s.value;
298
+ if (m = /^(\s+)?[\s\S]+?(\s+)?$/.exec(value)) {
299
+ return $.select(start + toCount(m[1] || ""), end - toCount(m[2] || ""));
300
+ }
301
+ }
302
+ return $;
303
+ });
304
+ !isFunction($$.toggle) && ($$.toggle = function (open, close, wrap) {
305
+ let $ = this;
282
306
  let {after, before, value} = $.$(),
283
307
  closeCount = toCount(close),
284
308
  openCount = toCount(open);
@@ -289,8 +313,9 @@ function attach() {
289
313
  return $.peel(open, close, wrap);
290
314
  }
291
315
  return $.wrap(open, close, wrap);
292
- };
293
- $.toggleBlock = (open, close, wrap) => {
316
+ });
317
+ !isFunction($$.toggleBlock) && ($$.toggleBlock = function (open, close, wrap) {
318
+ let $ = this;
294
319
  let {after, before, value} = $.$(),
295
320
  closeCount = toCount(close),
296
321
  lineAfter = after.split('\n').shift(),
@@ -303,15 +328,16 @@ function attach() {
303
328
  return $.peelBlock(open, close, wrap);
304
329
  }
305
330
  return $.wrapBlock(open, close, wrap);
306
- };
307
- $.wrapBlock = (open, close, wrap) => {
331
+ });
332
+ !isFunction($$.wrapBlock) && ($$.wrapBlock = function (open, close, wrap) {
333
+ let $ = this;
308
334
  let {after, before, end, start} = $.$(),
309
335
  lineAfter = after.split('\n').shift(),
310
336
  lineAfterCount = toCount(lineAfter),
311
337
  lineBefore = before.split('\n').pop(),
312
338
  lineBeforeCount = toCount(lineBefore);
313
339
  return $.select(start - lineBeforeCount, end + lineAfterCount).wrap(open, close, wrap);
314
- };
340
+ });
315
341
  return $.on('key.down', onKeyDown).record();
316
342
  }
317
343
 
@@ -319,4 +345,4 @@ function detach() {
319
345
  return this.off('key.down', onKeyDown);
320
346
  }
321
347
 
322
- export default {attach, detach};
348
+ export default {attach, detach, name};
package/package.json CHANGED
@@ -53,5 +53,5 @@
53
53
  "scripts": {
54
54
  "pack": "pack --clean=false --from=.factory --js-format=umd --js-name=TextEditor.Source --js-top='%(js.license)' --mjs=true --to=."
55
55
  },
56
- "version": "3.0.1"
56
+ "version": "3.0.3"
57
57
  }