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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,6 +143,7 @@
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
149
  var _$$state$source, _$$state$source2;
@@ -339,6 +352,7 @@
339
352
 
340
353
  function attach() {
341
354
  var $ = this;
355
+ var $$ = $.constructor.prototype;
342
356
  $.state = fromStates({
343
357
  source: {
344
358
  pairs: {
@@ -352,14 +366,15 @@
352
366
  }
353
367
  }
354
368
  }, $.state);
355
- $.alert = function (hint, then) {
369
+ !isFunction($$.alert) && ($$.alert = function (hint, then) {
356
370
  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) {
371
+ return isFunction(then) && then.call(this, true);
372
+ });
373
+ !isFunction($$.confirm) && ($$.confirm = function (hint, then) {
374
+ return isFunction(then) && then.call(this, W.confirm && W.confirm(hint));
375
+ });
376
+ !isFunction($$.insertBlock) && ($$.insertBlock = function (value, mode) {
377
+ var $ = this;
363
378
  var _$$$2 = $.$(),
364
379
  after = _$$$2.after,
365
380
  before = _$$$2.before,
@@ -378,8 +393,9 @@
378
393
  return $.select(end + lineAfterCount).insert('\n', -1).push(lineMatchIndent).insert(value, 1, false);
379
394
  }
380
395
  return $.select(start - lineBeforeCount, end + lineAfterCount).insert(value, mode, true).wrap(lineMatchIndent, "");
381
- };
382
- $.peelBlock = function (open, close, wrap) {
396
+ });
397
+ !isFunction($$.peelBlock) && ($$.peelBlock = function (open, close, wrap) {
398
+ var $ = this;
383
399
  var _$$$3 = $.$(),
384
400
  after = _$$$3.after,
385
401
  before = _$$$3.before,
@@ -396,23 +412,50 @@
396
412
  return $.select(start - lineBeforeCount + (wrap ? 0 : openCount), end + lineAfterCount - (wrap ? 0 : closeCount)).peel(open, close, wrap);
397
413
  }
398
414
  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 () {
415
+ });
416
+ !isFunction($$.prompt) && ($$.prompt = function (hint, value, then) {
417
+ return isFunction(then) && then.call(this, W.prompt ? W.prompt(hint, value) : false);
418
+ });
419
+ !isFunction($$.selectBlock) && ($$.selectBlock = function (withSpaces) {
420
+ if (withSpaces === void 0) {
421
+ withSpaces = true;
422
+ }
423
+ var $ = this;
404
424
  var _$$$4 = $.$(),
405
425
  after = _$$$4.after,
406
426
  before = _$$$4.before,
407
427
  end = _$$$4.end,
408
428
  start = _$$$4.start,
429
+ value = _$$$4.value,
409
430
  lineAfter = after.split('\n').shift(),
410
431
  lineAfterCount = toCount(lineAfter),
411
432
  lineBefore = before.split('\n').pop(),
412
433
  lineBeforeCount = toCount(lineBefore);
413
- return $.select(start - lineBeforeCount, end + lineAfterCount);
414
- };
415
- $.toggle = function (open, close, wrap) {
434
+ if (!withSpaces) {
435
+ var lineAfterSpaces = /\s+$/.exec(lineAfter),
436
+ lineBeforeSpaces = /^\s+/.exec(lineBefore);
437
+ if (lineAfterSpaces) {
438
+ lineAfterCount -= toCount(lineAfterSpaces[0]);
439
+ }
440
+ if (lineBeforeSpaces) {
441
+ lineBeforeCount -= toCount(lineBeforeSpaces[0]);
442
+ }
443
+ }
444
+ $.select(start - lineBeforeCount, end + lineAfterCount);
445
+ if (!withSpaces) {
446
+ var s = $.$(),
447
+ m;
448
+ end = s.end;
449
+ start = s.start;
450
+ value = s.value;
451
+ if (m = /^(\s+)?[\s\S]+?(\s+)?$/.exec(value)) {
452
+ return $.select(start + toCount(m[1] || ""), end - toCount(m[2] || ""));
453
+ }
454
+ }
455
+ return $;
456
+ });
457
+ !isFunction($$.toggle) && ($$.toggle = function (open, close, wrap) {
458
+ var $ = this;
416
459
  var _$$$5 = $.$(),
417
460
  after = _$$$5.after,
418
461
  before = _$$$5.before,
@@ -423,8 +466,9 @@
423
466
  return $.peel(open, close, wrap);
424
467
  }
425
468
  return $.wrap(open, close, wrap);
426
- };
427
- $.toggleBlock = function (open, close, wrap) {
469
+ });
470
+ !isFunction($$.toggleBlock) && ($$.toggleBlock = function (open, close, wrap) {
471
+ var $ = this;
428
472
  var _$$$6 = $.$(),
429
473
  after = _$$$6.after,
430
474
  before = _$$$6.before,
@@ -437,8 +481,9 @@
437
481
  return $.peelBlock(open, close, wrap);
438
482
  }
439
483
  return $.wrapBlock(open, close, wrap);
440
- };
441
- $.wrapBlock = function (open, close, wrap) {
484
+ });
485
+ !isFunction($$.wrapBlock) && ($$.wrapBlock = function (open, close, wrap) {
486
+ var $ = this;
442
487
  var _$$$7 = $.$(),
443
488
  after = _$$$7.after,
444
489
  before = _$$$7.before,
@@ -449,7 +494,7 @@
449
494
  lineBefore = before.split('\n').pop(),
450
495
  lineBeforeCount = toCount(lineBefore);
451
496
  return $.select(start - lineBeforeCount, end + lineAfterCount).wrap(open, close, wrap);
452
- };
497
+ });
453
498
  return $.on('key.down', onKeyDown).record();
454
499
  }
455
500
 
@@ -458,7 +503,8 @@
458
503
  }
459
504
  var index_js = {
460
505
  attach: attach,
461
- detach: detach
506
+ detach: detach,
507
+ name: name
462
508
  };
463
509
  return index_js;
464
510
  }));
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 b(e){var r,t,i=this,o=i.k(!1).pop(),c=i.k();if($(i),!e.defaultPrevented&&!i.keys[c]){var l,u,a=(null==(r=i.state.source)?void 0:r.tab)||i.state.tab||"\t",d=(null==(t=i.state.source)?void 0:t.pairs)||{},h=Object.values(d);(function(e){return s(e)&&0==e%1})(a)&&(a=" ".repeat(a));var b=i.$(),m=b.after,g=b.before,x=b.end,y=b.start,B=b.value,E=m.split("\n").shift(),A=g.split("\n").pop(),T=/^\s+/.exec(A),W=T&&T[0]||"";if(w+k+"Enter"===c)return g||m?(p(e),i.select(y-f(A)).wrap(W,"\n").insert(B).record(),!1):void 0;if(w+"Enter"===c&&(g||m))return p(e),i.select(x+f(E)).wrap("\n"+W,"").insert(B).record(),!1;if("Alt-"!=c+"-"&&w!==c+"-"){if(" "===c)return l=d[u=g.slice(-1)],!B&&l&&u&&l===m[0]?(p(e),i.wrap(" "," ")):void 0;if("Backspace"===c||"Delete"===c){if(l=d[u=g.slice(-1)],"\\"===u)return;return B?m&&g&&l&&l===m[0]&&!g.endsWith("\\"+u)?(p(e),i.record().peel(u,l).record()):void 0:(l=d[u=g.trim().slice(-1)])&&u&&(m.startsWith(" "+l)&&g.endsWith(u+" ")||m.startsWith("\n"+W+l)&&g.endsWith(u+"\n"+W))?(p(e),i.trim("","").record()):"Delete"!==c&&A.endsWith(a)?(p(e),i.pull(a).record()):m&&g&&!g.endsWith("\\"+u)&&l===m[0]&&u===g.slice(-1)?(p(e),i.peel(u,l).record()):void 0}if("Enter"!==c&&k+"Enter"!==c){if("\\"!==(u=g.slice(-1))){if(l=n(m[0],h)?m[0]:d[u],!B&&m&&g&&l&&o===l)return p(e),i.select(y+1).record();for(u in d){if(l=d[u],o===u&&l)return p(e),i.wrap(u,l).record();if(o===l){if(B)return p(e),i.record().wrap(u,l).record();break}}var j,S,D,O=[];if(B){for(j in d)(S=d[j])&&O.push("(?:\\"+j+"(?:\\\\.|[^\\"+j+(S!==j?"\\"+S:"")+"])*\\"+S+")");if(O.push("\\w+"),O.push("\\s+"),O.push("[\\s\\S]"),w+"ArrowLeft"===c)return p(e),(D=v("("+O.join("|")+")$","").exec(g))?i.insert("").select(y-f(D[0])).insert(B).record():i.select();if(w+"ArrowRight"===c)return p(e),(D=m.match(v("^("+O.join("|")+")","")))?i.insert("").select(x+f(D[0])-f(B)).insert(B).record():i.select()}if(x+=f(E),y-=f(A),B=A+B+E,w+"ArrowUp"===c){if(p(e),!n("\n",g))return i.select();i.insert(""),i.replace(/^([^\n]*?)(\n|$)/,"$2",1),i.replace(/(^|\n)([^\n]*?)$/,"",-1);var R=i.$();return g=R.before,y=R.start,A=g.split("\n").pop(),i.select(y-=f(A)).wrap(B,"\n"),i.select(y,y+f(B)),i.record()}if(w+"ArrowDown"===c){if(p(e),!n("\n",m))return i.select();i.insert(""),i.replace(/^([^\n]*?)(\n|$)/,"",1),i.replace(/(^|\n)([^\n]*?)$/,"$1",-1);var C=i.$();return m=C.after,x=C.end,E=m.split("\n").shift(),i.select(x+=f(E)).wrap("\n",B),x+=1,i.select(x,x+f(B)),i.record()}}}else if(!B){if(m&&g&&(l=d[u=g.slice(-1)])&&l===m[0])return p(e),i.wrap("\n"+W+(u!==l?a:""),"\n"+W).record();if(W)return p(e),i.insert("\n"+W,-1).record()}}else p(e)}}var m={attach:function(){var e=this,r=e.constructor.prototype;return e.state=u({source:{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",b).record()},detach:function(){return this.off("key.down",b)},name:"TextEditor.Source"};return m}));
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,
@@ -214,7 +215,8 @@ 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
221
  source: {
220
222
  pairs: {
@@ -228,14 +230,15 @@ function attach() {
228
230
  }
229
231
  }
230
232
  }, $.state);
231
- $.alert = (hint, then) => {
233
+ !isFunction($$.alert) && ($$.alert = function (hint, then) {
232
234
  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) => {
235
+ return isFunction(then) && then.call(this, true);
236
+ });
237
+ !isFunction($$.confirm) && ($$.confirm = function (hint, then) {
238
+ return isFunction(then) && then.call(this, W.confirm && W.confirm(hint));
239
+ });
240
+ !isFunction($$.insertBlock) && ($$.insertBlock = function (value, mode) {
241
+ let $ = this;
239
242
  let {after, before, end, start} = $.$(),
240
243
  lineAfter = after.split('\n').shift(),
241
244
  lineAfterCount = toCount(lineAfter),
@@ -250,8 +253,9 @@ function attach() {
250
253
  return $.select(end + lineAfterCount).insert('\n', -1).push(lineMatchIndent).insert(value, 1, false);
251
254
  }
252
255
  return $.select(start - lineBeforeCount, end + lineAfterCount).insert(value, mode, true).wrap(lineMatchIndent, "");
253
- };
254
- $.peelBlock = (open, close, wrap) => {
256
+ });
257
+ !isFunction($$.peelBlock) && ($$.peelBlock = function (open, close, wrap) {
258
+ let $ = this;
255
259
  let {after, before, end, start, value} = $.$(),
256
260
  closeCount = toCount(close),
257
261
  lineAfter = after.split('\n').shift(),
@@ -266,19 +270,41 @@ function attach() {
266
270
  return $.select(start - lineBeforeCount + (wrap ? 0 : openCount), end + lineAfterCount - (wrap ? 0 : closeCount)).peel(open, close, wrap);
267
271
  }
268
272
  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} = $.$(),
273
+ });
274
+ !isFunction($$.prompt) && ($$.prompt = function (hint, value, then) {
275
+ return isFunction(then) && then.call(this, W.prompt ? W.prompt(hint, value) : false);
276
+ });
277
+ !isFunction($$.selectBlock) && ($$.selectBlock = function (withSpaces = true) {
278
+ let $ = this;
279
+ let {after, before, end, start, value} = $.$(),
275
280
  lineAfter = after.split('\n').shift(),
276
281
  lineAfterCount = toCount(lineAfter),
277
282
  lineBefore = before.split('\n').pop(),
278
283
  lineBeforeCount = toCount(lineBefore);
279
- return $.select(start - lineBeforeCount, end + lineAfterCount);
280
- };
281
- $.toggle = (open, close, wrap) => {
284
+ if (!withSpaces) {
285
+ let lineAfterSpaces = /\s+$/.exec(lineAfter),
286
+ lineBeforeSpaces = /^\s+/.exec(lineBefore);
287
+ if (lineAfterSpaces) {
288
+ lineAfterCount -= toCount(lineAfterSpaces[0]);
289
+ }
290
+ if (lineBeforeSpaces) {
291
+ lineBeforeCount -= toCount(lineBeforeSpaces[0]);
292
+ }
293
+ }
294
+ $.select(start - lineBeforeCount, end + lineAfterCount);
295
+ if (!withSpaces) {
296
+ let s = $.$(), m;
297
+ end = s.end;
298
+ start = s.start;
299
+ value = s.value;
300
+ if (m = /^(\s+)?[\s\S]+?(\s+)?$/.exec(value)) {
301
+ return $.select(start + toCount(m[1] || ""), end - toCount(m[2] || ""));
302
+ }
303
+ }
304
+ return $;
305
+ });
306
+ !isFunction($$.toggle) && ($$.toggle = function (open, close, wrap) {
307
+ let $ = this;
282
308
  let {after, before, value} = $.$(),
283
309
  closeCount = toCount(close),
284
310
  openCount = toCount(open);
@@ -289,8 +315,9 @@ function attach() {
289
315
  return $.peel(open, close, wrap);
290
316
  }
291
317
  return $.wrap(open, close, wrap);
292
- };
293
- $.toggleBlock = (open, close, wrap) => {
318
+ });
319
+ !isFunction($$.toggleBlock) && ($$.toggleBlock = function (open, close, wrap) {
320
+ let $ = this;
294
321
  let {after, before, value} = $.$(),
295
322
  closeCount = toCount(close),
296
323
  lineAfter = after.split('\n').shift(),
@@ -303,15 +330,16 @@ function attach() {
303
330
  return $.peelBlock(open, close, wrap);
304
331
  }
305
332
  return $.wrapBlock(open, close, wrap);
306
- };
307
- $.wrapBlock = (open, close, wrap) => {
333
+ });
334
+ !isFunction($$.wrapBlock) && ($$.wrapBlock = function (open, close, wrap) {
335
+ let $ = this;
308
336
  let {after, before, end, start} = $.$(),
309
337
  lineAfter = after.split('\n').shift(),
310
338
  lineAfterCount = toCount(lineAfter),
311
339
  lineBefore = before.split('\n').pop(),
312
340
  lineBeforeCount = toCount(lineBefore);
313
341
  return $.select(start - lineBeforeCount, end + lineAfterCount).wrap(open, close, wrap);
314
- };
342
+ });
315
343
  return $.on('key.down', onKeyDown).record();
316
344
  }
317
345
 
@@ -319,4 +347,4 @@ function detach() {
319
347
  return this.off('key.down', onKeyDown);
320
348
  }
321
349
 
322
- export default {attach, detach};
350
+ 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.2"
57
57
  }