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

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  [Text Editor](https://github.com/taufik-nurrohman/text-editor) » Source
2
2
  =============================================================================
3
3
 
4
- Source extension for [Text Editor](https://github.com/taufik-nurrohman/text-editor). This extension provides a set of
5
- key strokes to generate responses like in a typical source code editor.
4
+ This extension provides a set of key strokes to generate responses like in a typical source code editor.
6
5
 
7
6
  ![index.js](https://img.shields.io/github/size/taufik-nurrohman/text-editor.source/index.js?branch=main&color=%23f1e05a&label=index.js&labelColor=%231f2328&style=flat-square)
8
7
  ![index.min.js](https://img.shields.io/github/size/taufik-nurrohman/text-editor.source/index.min.js?branch=main&color=%23f1e05a&label=index.min.js&labelColor=%231f2328&style=flat-square)
package/index.js CHANGED
@@ -33,19 +33,25 @@
33
33
  var isArray = function isArray(x) {
34
34
  return Array.isArray(x);
35
35
  };
36
- var isDefined$1 = function isDefined(x) {
36
+ var isDefined = 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$1 = function isInstance(x, of) {
43
- return x && isSet$1(of) && x instanceof of ;
42
+ var isInstance = function isInstance(x, of, exact) {
43
+ if (!x || 'object' !== typeof x) {
44
+ return false;
45
+ }
46
+ if (exact) {
47
+ return isSet(of) && isSet(x.constructor) && of === x.constructor;
48
+ }
49
+ return isSet(of) && x instanceof of ;
44
50
  };
45
51
  var isInteger = function isInteger(x) {
46
52
  return isNumber(x) && 0 === x % 1;
47
53
  };
48
- var isNull$1 = function isNull(x) {
54
+ var isNull = function isNull(x) {
49
55
  return null === x;
50
56
  };
51
57
  var isNumber = function isNumber(x) {
@@ -55,13 +61,13 @@
55
61
  if (isPlain === void 0) {
56
62
  isPlain = true;
57
63
  }
58
- if ('object' !== typeof x) {
64
+ if (!x || 'object' !== typeof x) {
59
65
  return false;
60
66
  }
61
- return isPlain ? isInstance$1(x, Object) : true;
67
+ return isPlain ? isInstance(x, Object, 1) : true;
62
68
  };
63
- var isSet$1 = function isSet(x) {
64
- return isDefined$1(x) && !isNull$1(x);
69
+ var isSet = function isSet(x) {
70
+ return isDefined(x) && !isNull(x);
65
71
  };
66
72
  var toCount = function toCount(x) {
67
73
  return x.length;
@@ -77,7 +83,7 @@
77
83
  for (var i = 0, j = toCount(lot); i < j; ++i) {
78
84
  for (var k in lot[i]) {
79
85
  // Assign value
80
- if (!isSet$1(out[k])) {
86
+ if (!isSet(out[k])) {
81
87
  out[k] = lot[i][k];
82
88
  continue;
83
89
  }
@@ -116,18 +122,6 @@
116
122
  var offEventDefault = function offEventDefault(e) {
117
123
  return e && e.preventDefault();
118
124
  };
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
- };
131
125
  var isPattern = function isPattern(pattern) {
132
126
  return isInstance(pattern, RegExp);
133
127
  };
@@ -140,6 +134,13 @@
140
134
  var ALT_PREFIX = 'Alt-';
141
135
  var CTRL_PREFIX = 'Control-';
142
136
  var SHIFT_PREFIX = 'Shift-';
137
+ var KEY_ARROW_DOWN = 'ArrowDown';
138
+ var KEY_ARROW_LEFT = 'ArrowLeft';
139
+ var KEY_ARROW_RIGHT = 'ArrowRight';
140
+ var KEY_ARROW_UP = 'ArrowUp';
141
+ var KEY_DELETE_LEFT = 'Backspace';
142
+ var KEY_DELETE_RIGHT = 'Delete';
143
+ var KEY_ENTER = 'Enter';
143
144
  var bounce = debounce(function ($) {
144
145
  return $.record();
145
146
  }, 10);
@@ -172,7 +173,7 @@
172
173
  lineBefore = before.split('\n').pop(),
173
174
  lineMatch = /^\s+/.exec(lineBefore),
174
175
  lineMatchIndent = lineMatch && lineMatch[0] || "";
175
- if (CTRL_PREFIX + SHIFT_PREFIX + 'Enter' === keys) {
176
+ if (CTRL_PREFIX + SHIFT_PREFIX + KEY_ENTER === keys) {
176
177
  if (before || after) {
177
178
  // Insert line above with `⎈⇧↵`
178
179
  offEventDefault(e);
@@ -180,7 +181,7 @@
180
181
  }
181
182
  return;
182
183
  }
183
- if (CTRL_PREFIX + 'Enter' === keys) {
184
+ if (CTRL_PREFIX + KEY_ENTER === keys) {
184
185
  if (before || after) {
185
186
  // Insert line below with `⎈↵`
186
187
  offEventDefault(e);
@@ -200,7 +201,7 @@
200
201
  }
201
202
  return;
202
203
  }
203
- if ('Backspace' === keys || 'Delete' === keys) {
204
+ if (KEY_DELETE_LEFT === keys || KEY_DELETE_RIGHT === keys) {
204
205
  charAfter = charPairs[charBefore = before.slice(-1)];
205
206
  // Do nothing on escape
206
207
  if ('\\' === charBefore) {
@@ -222,7 +223,7 @@
222
223
  }
223
224
  }
224
225
  // Outdent
225
- if ('Delete' !== keys && lineBefore.endsWith(charIndent)) {
226
+ if (KEY_DELETE_RIGHT !== keys && lineBefore.endsWith(charIndent)) {
226
227
  offEventDefault(e);
227
228
  return $.pull(charIndent).record();
228
229
  }
@@ -235,7 +236,7 @@
235
236
  }
236
237
  return;
237
238
  }
238
- if ('Enter' === keys || SHIFT_PREFIX + 'Enter' === keys) {
239
+ if (KEY_ENTER === keys || SHIFT_PREFIX + KEY_ENTER === keys) {
239
240
  if (!value) {
240
241
  if (after && before && (charAfter = charPairs[charBefore = before.slice(-1)]) && charAfter === after[0]) {
241
242
  offEventDefault(e);
@@ -294,14 +295,14 @@
294
295
  tokens.push('\\w+'); // Word(s)
295
296
  tokens.push('\\s+'); // White-space(s)
296
297
  tokens.push('[\\s\\S]'); // Last try!
297
- if (CTRL_PREFIX + 'ArrowLeft' === keys) {
298
+ if (CTRL_PREFIX + KEY_ARROW_LEFT === keys) {
298
299
  offEventDefault(e);
299
300
  if (m = toPattern('(' + tokens.join('|') + ')$', "").exec(before)) {
300
301
  return $.insert("").select(start - toCount(m[0])).insert(value).record();
301
302
  }
302
303
  return $.select();
303
304
  }
304
- if (CTRL_PREFIX + 'ArrowRight' === keys) {
305
+ if (CTRL_PREFIX + KEY_ARROW_RIGHT === keys) {
305
306
  offEventDefault(e);
306
307
  if (m = after.match(toPattern('^(' + tokens.join('|') + ')', ""))) {
307
308
  return $.insert("").select(end + toCount(m[0]) - toCount(value)).insert(value).record();
@@ -313,7 +314,7 @@
313
314
  end += toCount(lineAfter);
314
315
  start -= toCount(lineBefore);
315
316
  value = lineBefore + value + lineAfter;
316
- if (CTRL_PREFIX + 'ArrowUp' === keys) {
317
+ if (CTRL_PREFIX + KEY_ARROW_UP === keys) {
317
318
  offEventDefault(e);
318
319
  if (!hasValue('\n', before)) {
319
320
  return $.select();
@@ -329,7 +330,7 @@
329
330
  $.select(start, start + toCount(value));
330
331
  return $.record();
331
332
  }
332
- if (CTRL_PREFIX + 'ArrowDown' === keys) {
333
+ if (CTRL_PREFIX + KEY_ARROW_DOWN === keys) {
333
334
  offEventDefault(e);
334
335
  if (!hasValue('\n', after)) {
335
336
  return $.select();
@@ -348,10 +349,14 @@
348
349
  }
349
350
  return;
350
351
  }
352
+ // Partial mobile support
353
+ function onPutDown(e) {
354
+ onKeyDown.call(this, e);
355
+ }
351
356
 
352
357
  function attach() {
353
358
  var $ = this;
354
- var $$ = $.constructor.prototype;
359
+ var $$ = $.constructor._;
355
360
  $.state = fromStates({
356
361
  pairs: {
357
362
  '`': '`',
@@ -370,9 +375,9 @@
370
375
  !isFunction($$.confirm) && ($$.confirm = function (hint, then) {
371
376
  return isFunction(then) && then.call(this, W.confirm && W.confirm(hint));
372
377
  });
373
- !isFunction($$.insertBlock) && ($$.insertBlock = function (value, mode) {
374
- var $ = this;
375
- var _$$$2 = $.$(),
378
+ !isFunction($$.insertLine) && ($$.insertLine = function (value, mode) {
379
+ var $ = this,
380
+ _$$$2 = $.$(),
376
381
  after = _$$$2.after,
377
382
  before = _$$$2.before,
378
383
  end = _$$$2.end,
@@ -391,69 +396,45 @@
391
396
  }
392
397
  return $.select(start - lineBeforeCount, end + lineAfterCount).insert(value, mode, true).wrap(lineMatchIndent, "");
393
398
  });
394
- !isFunction($$.peelBlock) && ($$.peelBlock = function (open, close, wrap) {
395
- var $ = this;
396
- var _$$$3 = $.$(),
397
- after = _$$$3.after,
398
- before = _$$$3.before,
399
- end = _$$$3.end,
400
- start = _$$$3.start,
401
- value = _$$$3.value,
402
- closeCount = toCount(close),
403
- lineAfter = after.split('\n').shift(),
404
- lineAfterCount = toCount(lineAfter),
405
- lineBefore = before.split('\n').pop(),
406
- lineBeforeCount = toCount(lineBefore),
407
- openCount = toCount(open);
408
- if (wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount) || close === lineAfter.slice(-closeCount) && open === lineBefore.slice(0, openCount)) {
409
- return $.select(start - lineBeforeCount + (wrap ? 0 : openCount), end + lineAfterCount - (wrap ? 0 : closeCount)).peel(open, close, wrap);
399
+ !isFunction($$.peelLine) && ($$.peelLine = function (open, close, wrap, withSpaces) {
400
+ if (withSpaces === void 0) {
401
+ withSpaces = false;
410
402
  }
411
- return $.select(start, end);
403
+ return this.selectLine(withSpaces).peel(open, close, wrap);
412
404
  });
413
405
  !isFunction($$.prompt) && ($$.prompt = function (hint, value, then) {
414
406
  return isFunction(then) && then.call(this, W.prompt ? W.prompt(hint, value) : false);
415
407
  });
416
- !isFunction($$.selectBlock) && ($$.selectBlock = function (withSpaces) {
408
+ !isFunction($$.selectLine) && ($$.selectLine = function (withSpaces) {
417
409
  if (withSpaces === void 0) {
418
410
  withSpaces = true;
419
411
  }
420
- var $ = this;
421
- var _$$$4 = $.$(),
422
- after = _$$$4.after,
423
- before = _$$$4.before,
424
- end = _$$$4.end,
425
- start = _$$$4.start,
426
- value = _$$$4.value,
412
+ var $ = this,
413
+ m,
414
+ _$$$3 = $.$(),
415
+ after = _$$$3.after,
416
+ before = _$$$3.before,
417
+ end = _$$$3.end,
418
+ start = _$$$3.start,
427
419
  lineAfter = after.split('\n').shift(),
428
420
  lineAfterCount = toCount(lineAfter),
429
421
  lineBefore = before.split('\n').pop(),
430
422
  lineBeforeCount = toCount(lineBefore);
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
423
  $.select(start - lineBeforeCount, end + lineAfterCount);
442
424
  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] || ""));
425
+ var _$$$4 = $.$(),
426
+ _end = _$$$4.end,
427
+ _start = _$$$4.start,
428
+ value = _$$$4.value;
429
+ if (m = /^(\s+)?[\s\S]*?(\s+)?$/.exec(value)) {
430
+ return $.select(_start + toCount(m[1] || ""), _end - toCount(m[2] || ""));
450
431
  }
451
432
  }
452
433
  return $;
453
434
  });
454
435
  !isFunction($$.toggle) && ($$.toggle = function (open, close, wrap) {
455
- var $ = this;
456
- var _$$$5 = $.$(),
436
+ var $ = this,
437
+ _$$$5 = $.$(),
457
438
  after = _$$$5.after,
458
439
  before = _$$$5.before,
459
440
  value = _$$$5.value,
@@ -464,39 +445,36 @@
464
445
  }
465
446
  return $.wrap(open, close, wrap);
466
447
  });
467
- !isFunction($$.toggleBlock) && ($$.toggleBlock = function (open, close, wrap) {
468
- var $ = this;
469
- var _$$$6 = $.$(),
470
- after = _$$$6.after,
471
- before = _$$$6.before,
472
- value = _$$$6.value,
448
+ !isFunction($$.toggleLine) && ($$.toggleLine = function (open, close, wrap, withSpaces) {
449
+ if (withSpaces === void 0) {
450
+ withSpaces = false;
451
+ }
452
+ var $ = this.selectLine(withSpaces),
453
+ _$$$6 = $.$();
454
+ _$$$6.after;
455
+ _$$$6.before;
456
+ var value = _$$$6.value,
473
457
  closeCount = toCount(close),
474
- lineAfter = after.split('\n').shift(),
475
- lineBefore = before.split('\n').pop(),
476
458
  openCount = toCount(open);
477
- if (wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount) || close === lineAfter.slice(-closeCount) && open === lineBefore.slice(0, openCount)) {
478
- return $.peelBlock(open, close, wrap);
459
+ if (!wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount)) {
460
+ var _$$$7 = $.$(),
461
+ end = _$$$7.end,
462
+ start = _$$$7.start;
463
+ $.select(start + openCount, end - closeCount);
479
464
  }
480
- return $.wrapBlock(open, close, wrap);
465
+ return $.toggle(open, close, wrap);
481
466
  });
482
- !isFunction($$.wrapBlock) && ($$.wrapBlock = function (open, close, wrap) {
483
- var $ = this;
484
- var _$$$7 = $.$(),
485
- after = _$$$7.after,
486
- before = _$$$7.before,
487
- end = _$$$7.end,
488
- start = _$$$7.start,
489
- lineAfter = after.split('\n').shift(),
490
- lineAfterCount = toCount(lineAfter),
491
- lineBefore = before.split('\n').pop(),
492
- lineBeforeCount = toCount(lineBefore);
493
- return $.select(start - lineBeforeCount, end + lineAfterCount).wrap(open, close, wrap);
467
+ !isFunction($$.wrapLine) && ($$.wrapLine = function (open, close, wrap, withSpaces) {
468
+ if (withSpaces === void 0) {
469
+ withSpaces = false;
470
+ }
471
+ return this.selectLine(withSpaces).wrap(open, close, wrap);
494
472
  });
495
- return $.on('key.down', onKeyDown).record();
473
+ return $.on('key.down', onKeyDown).on('put.down', onPutDown).record();
496
474
  }
497
475
 
498
476
  function detach() {
499
- return this.off('key.down', onKeyDown);
477
+ return this.off('key.down', onKeyDown).off('put.down', onPutDown);
500
478
  }
501
479
  var index_js = {
502
480
  attach: attach,
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){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}));
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,t){return!(!e||"object"!=typeof e)&&(t?u(r)&&u(e.constructor)&&r===e.constructor:u(r)&&e instanceof r)},c=function(e){return"number"==typeof e},f=function(e,r){return void 0===r&&(r=!0),!(!e||"object"!=typeof e)&&(!r||s(e,Object,1))},u=function(e){return function(e){return void 0!==e}(e)&&!function(e){return null===e}(e)},l=function(e){return e.length},a=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,a=l(t);c<a;++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=l(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},p=window,d=function(e){return e&&e.preventDefault()},h=function(e,r){return function(e){return s(e,RegExp)}(e)?e:RegExp(e,u(r)?r:"g")},v="Control-",w="Shift-",g="Delete",m="Enter",$=(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=this,t=r.k(!1).pop(),i=r.k();if($(r),!e.defaultPrevented&&!r.keys[i]){var o,s,f=r.state.tab||"\t",u=r.state.pairs||{},a=Object.values(u);(function(e){return c(e)&&0==e%1})(f)&&(f=" ".repeat(f));var p=r.$(),b=p.after,y=p.before,L=p.end,x=p.start,A=p.value,T=b.split("\n").shift(),j=y.split("\n").pop(),k=/^\s+/.exec(j),E=k&&k[0]||"";if(v+w+m===i)return y||b?(d(e),r.select(x-l(j)).wrap(E,"\n").insert(A).record(),!1):void 0;if(v+m===i&&(y||b))return d(e),r.select(L+l(T)).wrap("\n"+E,"").insert(A).record(),!1;if("Alt-"!=i+"-"&&v!==i+"-"){if(" "===i)return o=u[s=y.slice(-1)],!A&&o&&s&&o===b[0]?(d(e),r.wrap(" "," ")):void 0;if("Backspace"===i||g===i){if(o=u[s=y.slice(-1)],"\\"===s)return;return A?b&&y&&o&&o===b[0]&&!y.endsWith("\\"+s)?(d(e),r.record().peel(s,o).record()):void 0:(o=u[s=y.trim().slice(-1)])&&s&&(b.startsWith(" "+o)&&y.endsWith(s+" ")||b.startsWith("\n"+E+o)&&y.endsWith(s+"\n"+E))?(d(e),r.trim("","").record()):g!==i&&j.endsWith(f)?(d(e),r.pull(f).record()):b&&y&&!y.endsWith("\\"+s)&&o===b[0]&&s===y.slice(-1)?(d(e),r.peel(s,o).record()):void 0}if(m!==i&&w+m!==i){if("\\"!==(s=y.slice(-1))){if(o=n(b[0],a)?b[0]:u[s],!A&&b&&y&&o&&t===o)return d(e),r.select(x+1).record();for(s in u){if(o=u[s],t===s&&o)return d(e),r.wrap(s,o).record();if(t===o){if(A)return d(e),r.record().wrap(s,o).record();break}}var W,S,D,O=[];if(A){for(W in u)(S=u[W])&&O.push("(?:\\"+W+"(?:\\\\.|[^\\"+W+(S!==W?"\\"+S:"")+"])*\\"+S+")");if(O.push("\\w+"),O.push("\\s+"),O.push("[\\s\\S]"),v+"ArrowLeft"===i)return d(e),(D=h("("+O.join("|")+")$","").exec(y))?r.insert("").select(x-l(D[0])).insert(A).record():r.select();if(v+"ArrowRight"===i)return d(e),(D=b.match(h("^("+O.join("|")+")","")))?r.insert("").select(L+l(D[0])-l(A)).insert(A).record():r.select()}if(L+=l(T),x-=l(j),A=j+A+T,v+"ArrowUp"===i){if(d(e),!n("\n",y))return r.select();r.insert(""),r.replace(/^([^\n]*?)(\n|$)/,"$2",1),r.replace(/(^|\n)([^\n]*?)$/,"",-1);var R=r.$();return y=R.before,x=R.start,j=y.split("\n").pop(),r.select(x-=l(j)).wrap(A,"\n"),r.select(x,x+l(A)),r.record()}if(v+"ArrowDown"===i){if(d(e),!n("\n",b))return r.select();r.insert(""),r.replace(/^([^\n]*?)(\n|$)/,"",1),r.replace(/(^|\n)([^\n]*?)$/,"$1",-1);var B=r.$();return b=B.after,L=B.end,T=b.split("\n").shift(),r.select(L+=l(T)).wrap("\n",A),L+=1,r.select(L,L+l(A)),r.record()}}}else if(!A){if(b&&y&&(o=u[s=y.slice(-1)])&&o===b[0])return d(e),r.wrap("\n"+E+(s!==o?f:""),"\n"+E).record();if(E)return d(e),r.insert("\n"+E,-1).record()}}else d(e)}}function y(e){b.call(this,e)}var L={attach:function(){var e=this,r=e.constructor._;return e.state=a({pairs:{"`":"`","(":")","{":"}","[":"]",'"':'"',"'":"'","<":">"}},e.state),!o(r.alert)&&(r.alert=function(e,r){return p.alert&&p.alert(e),o(r)&&r.call(this,!0)}),!o(r.confirm)&&(r.confirm=function(e,r){return o(r)&&r.call(this,p.confirm&&p.confirm(e))}),!o(r.insertLine)&&(r.insertLine=function(e,r){var t=this,n=t.$(),i=n.after,o=n.before,s=n.end,c=n.start,f=i.split("\n").shift(),u=l(f),a=o.split("\n").pop(),p=l(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.peelLine)&&(r.peelLine=function(e,r,t,n){return void 0===n&&(n=!1),this.selectLine(n).peel(e,r,t)}),!o(r.prompt)&&(r.prompt=function(e,r,t){return o(t)&&t.call(this,!!p.prompt&&p.prompt(e,r))}),!o(r.selectLine)&&(r.selectLine=function(e){void 0===e&&(e=!0);var r,t=this,n=t.$(),i=n.after,o=n.before,s=n.end,c=n.start,f=i.split("\n").shift(),u=l(f),a=o.split("\n").pop(),p=l(a);if(t.select(c-p,s+u),!e){var d=t.$(),h=d.end,v=d.start,w=d.value;if(r=/^(\s+)?[\s\S]*?(\s+)?$/.exec(w))return t.select(v+l(r[1]||""),h-l(r[2]||""))}return t}),!o(r.toggle)&&(r.toggle=function(e,r,t){var n=this,i=n.$(),o=i.after,s=i.before,c=i.value,f=l(r),u=l(e);return t&&r===c.slice(-f)&&e===c.slice(0,u)||r===o.slice(0,f)&&e===s.slice(-u)?n.peel(e,r,t):n.wrap(e,r,t)}),!o(r.toggleLine)&&(r.toggleLine=function(e,r,t,n){void 0===n&&(n=!1);var i=this.selectLine(n),o=i.$();o.after,o.before;var s=o.value,c=l(r),f=l(e);if(!t&&r===s.slice(-c)&&e===s.slice(0,f)){var u=i.$(),a=u.end,p=u.start;i.select(p+f,a-c)}return i.toggle(e,r,t)}),!o(r.wrapLine)&&(r.wrapLine=function(e,r,t,n){return void 0===n&&(n=!1),this.selectLine(n).wrap(e,r,t)}),e.on("key.down",b).on("put.down",y).record()},detach:function(){return this.off("key.down",b).off("put.down",y)},name:"TextEditor.Source"};return L}));
package/index.mjs CHANGED
@@ -11,6 +11,14 @@ const ALT_PREFIX = 'Alt-';
11
11
  const CTRL_PREFIX = 'Control-';
12
12
  const SHIFT_PREFIX = 'Shift-';
13
13
 
14
+ const KEY_ARROW_DOWN = 'ArrowDown';
15
+ const KEY_ARROW_LEFT = 'ArrowLeft';
16
+ const KEY_ARROW_RIGHT = 'ArrowRight';
17
+ const KEY_ARROW_UP = 'ArrowUp';
18
+ const KEY_DELETE_LEFT = 'Backspace';
19
+ const KEY_DELETE_RIGHT = 'Delete';
20
+ const KEY_ENTER = 'Enter';
21
+
14
22
  const bounce = debounce($ => $.record(), 10);
15
23
  const name = 'TextEditor.Source';
16
24
 
@@ -35,7 +43,7 @@ function onKeyDown(e) {
35
43
  lineBefore = before.split('\n').pop(),
36
44
  lineMatch = /^\s+/.exec(lineBefore),
37
45
  lineMatchIndent = lineMatch && lineMatch[0] || "";
38
- if (CTRL_PREFIX + SHIFT_PREFIX + 'Enter' === keys) {
46
+ if (CTRL_PREFIX + SHIFT_PREFIX + KEY_ENTER === keys) {
39
47
  if (before || after) {
40
48
  // Insert line above with `⎈⇧↵`
41
49
  offEventDefault(e);
@@ -43,7 +51,7 @@ function onKeyDown(e) {
43
51
  }
44
52
  return;
45
53
  }
46
- if (CTRL_PREFIX + 'Enter' === keys) {
54
+ if (CTRL_PREFIX + KEY_ENTER === keys) {
47
55
  if (before || after) {
48
56
  // Insert line below with `⎈↵`
49
57
  offEventDefault(e);
@@ -63,7 +71,7 @@ function onKeyDown(e) {
63
71
  }
64
72
  return;
65
73
  }
66
- if ('Backspace' === keys || 'Delete' === keys) {
74
+ if (KEY_DELETE_LEFT === keys || KEY_DELETE_RIGHT === keys) {
67
75
  charAfter = charPairs[charBefore = before.slice(-1)];
68
76
  // Do nothing on escape
69
77
  if ('\\' === charBefore) {
@@ -88,7 +96,7 @@ function onKeyDown(e) {
88
96
  }
89
97
  }
90
98
  // Outdent
91
- if ('Delete' !== keys && lineBefore.endsWith(charIndent)) {
99
+ if (KEY_DELETE_RIGHT !== keys && lineBefore.endsWith(charIndent)) {
92
100
  offEventDefault(e);
93
101
  return $.pull(charIndent).record();
94
102
  }
@@ -101,7 +109,7 @@ function onKeyDown(e) {
101
109
  }
102
110
  return;
103
111
  }
104
- if ('Enter' === keys || SHIFT_PREFIX + 'Enter' === keys) {
112
+ if (KEY_ENTER === keys || SHIFT_PREFIX + KEY_ENTER === keys) {
105
113
  if (!value) {
106
114
  if (after && before && (charAfter = charPairs[charBefore = before.slice(-1)]) && charAfter === after[0]) {
107
115
  offEventDefault(e);
@@ -159,14 +167,14 @@ function onKeyDown(e) {
159
167
  tokens.push('\\w+'); // Word(s)
160
168
  tokens.push('\\s+'); // White-space(s)
161
169
  tokens.push('[\\s\\S]'); // Last try!
162
- if (CTRL_PREFIX + 'ArrowLeft' === keys) {
170
+ if (CTRL_PREFIX + KEY_ARROW_LEFT === keys) {
163
171
  offEventDefault(e);
164
172
  if (m = toPattern('(' + tokens.join('|') + ')$', "").exec(before)) {
165
173
  return $.insert("").select(start - toCount(m[0])).insert(value).record();
166
174
  }
167
175
  return $.select();
168
176
  }
169
- if (CTRL_PREFIX + 'ArrowRight' === keys) {
177
+ if (CTRL_PREFIX + KEY_ARROW_RIGHT === keys) {
170
178
  offEventDefault(e);
171
179
  if (m = after.match(toPattern('^(' + tokens.join('|') + ')', ""))) {
172
180
  return $.insert("").select(end + toCount(m[0]) - toCount(value)).insert(value).record();
@@ -178,7 +186,7 @@ function onKeyDown(e) {
178
186
  end += toCount(lineAfter);
179
187
  start -= toCount(lineBefore);
180
188
  value = lineBefore + value + lineAfter;
181
- if (CTRL_PREFIX + 'ArrowUp' === keys) {
189
+ if (CTRL_PREFIX + KEY_ARROW_UP === keys) {
182
190
  offEventDefault(e);
183
191
  if (!hasValue('\n', before)) {
184
192
  return $.select();
@@ -194,7 +202,7 @@ function onKeyDown(e) {
194
202
  $.select(start, start + toCount(value));
195
203
  return $.record();
196
204
  }
197
- if (CTRL_PREFIX + 'ArrowDown' === keys) {
205
+ if (CTRL_PREFIX + KEY_ARROW_DOWN === keys) {
198
206
  offEventDefault(e);
199
207
  if (!hasValue('\n', after)) {
200
208
  return $.select();
@@ -214,9 +222,14 @@ function onKeyDown(e) {
214
222
  return;
215
223
  }
216
224
 
225
+ // Partial mobile support
226
+ function onPutDown(e) {
227
+ onKeyDown.call(this, e);
228
+ }
229
+
217
230
  function attach() {
218
231
  const $ = this;
219
- const $$ = $.constructor.prototype;
232
+ const $$ = $.constructor._;
220
233
  $.state = fromStates({
221
234
  pairs: {
222
235
  '`': '`',
@@ -235,9 +248,9 @@ function attach() {
235
248
  !isFunction($$.confirm) && ($$.confirm = function (hint, then) {
236
249
  return isFunction(then) && then.call(this, W.confirm && W.confirm(hint));
237
250
  });
238
- !isFunction($$.insertBlock) && ($$.insertBlock = function (value, mode) {
239
- let $ = this;
240
- let {after, before, end, start} = $.$(),
251
+ !isFunction($$.insertLine) && ($$.insertLine = function (value, mode) {
252
+ let $ = this,
253
+ {after, before, end, start} = $.$(),
241
254
  lineAfter = after.split('\n').shift(),
242
255
  lineAfterCount = toCount(lineAfter),
243
256
  lineBefore = before.split('\n').pop(),
@@ -252,58 +265,31 @@ function attach() {
252
265
  }
253
266
  return $.select(start - lineBeforeCount, end + lineAfterCount).insert(value, mode, true).wrap(lineMatchIndent, "");
254
267
  });
255
- !isFunction($$.peelBlock) && ($$.peelBlock = function (open, close, wrap) {
256
- let $ = this;
257
- let {after, before, end, start, value} = $.$(),
258
- closeCount = toCount(close),
259
- lineAfter = after.split('\n').shift(),
260
- lineAfterCount = toCount(lineAfter),
261
- lineBefore = before.split('\n').pop(),
262
- lineBeforeCount = toCount(lineBefore),
263
- openCount = toCount(open);
264
- if (
265
- (wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount)) ||
266
- (close === lineAfter.slice(-closeCount) && open === lineBefore.slice(0, openCount))
267
- ) {
268
- return $.select(start - lineBeforeCount + (wrap ? 0 : openCount), end + lineAfterCount - (wrap ? 0 : closeCount)).peel(open, close, wrap);
269
- }
270
- return $.select(start, end);
268
+ !isFunction($$.peelLine) && ($$.peelLine = function (open, close, wrap, withSpaces = false) {
269
+ return this.selectLine(withSpaces).peel(open, close, wrap);
271
270
  });
272
271
  !isFunction($$.prompt) && ($$.prompt = function (hint, value, then) {
273
272
  return isFunction(then) && then.call(this, W.prompt ? W.prompt(hint, value) : false);
274
273
  });
275
- !isFunction($$.selectBlock) && ($$.selectBlock = function (withSpaces = true) {
276
- let $ = this;
277
- let {after, before, end, start, value} = $.$(),
274
+ !isFunction($$.selectLine) && ($$.selectLine = function (withSpaces = true) {
275
+ let $ = this, m,
276
+ {after, before, end, start} = $.$(),
278
277
  lineAfter = after.split('\n').shift(),
279
278
  lineAfterCount = toCount(lineAfter),
280
279
  lineBefore = before.split('\n').pop(),
281
280
  lineBeforeCount = toCount(lineBefore);
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
281
  $.select(start - lineBeforeCount, end + lineAfterCount);
293
282
  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)) {
283
+ let {end, start, value} = $.$();
284
+ if (m = /^(\s+)?[\s\S]*?(\s+)?$/.exec(value)) {
299
285
  return $.select(start + toCount(m[1] || ""), end - toCount(m[2] || ""));
300
286
  }
301
287
  }
302
288
  return $;
303
289
  });
304
290
  !isFunction($$.toggle) && ($$.toggle = function (open, close, wrap) {
305
- let $ = this;
306
- let {after, before, value} = $.$(),
291
+ let $ = this,
292
+ {after, before, value} = $.$(),
307
293
  closeCount = toCount(close),
308
294
  openCount = toCount(open);
309
295
  if (
@@ -314,35 +300,25 @@ function attach() {
314
300
  }
315
301
  return $.wrap(open, close, wrap);
316
302
  });
317
- !isFunction($$.toggleBlock) && ($$.toggleBlock = function (open, close, wrap) {
318
- let $ = this;
319
- let {after, before, value} = $.$(),
303
+ !isFunction($$.toggleLine) && ($$.toggleLine = function (open, close, wrap, withSpaces = false) {
304
+ let $ = this.selectLine(withSpaces),
305
+ {after, before, value} = $.$(),
320
306
  closeCount = toCount(close),
321
- lineAfter = after.split('\n').shift(),
322
- lineBefore = before.split('\n').pop(),
323
307
  openCount = toCount(open);
324
- if (
325
- (wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount)) ||
326
- (close === lineAfter.slice(-closeCount) && open === lineBefore.slice(0, openCount))
327
- ) {
328
- return $.peelBlock(open, close, wrap);
308
+ if (!wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount)) {
309
+ let {end, start} = $.$();
310
+ $.select(start + openCount, end - closeCount);
329
311
  }
330
- return $.wrapBlock(open, close, wrap);
312
+ return $.toggle(open, close, wrap);
331
313
  });
332
- !isFunction($$.wrapBlock) && ($$.wrapBlock = function (open, close, wrap) {
333
- let $ = this;
334
- let {after, before, end, start} = $.$(),
335
- lineAfter = after.split('\n').shift(),
336
- lineAfterCount = toCount(lineAfter),
337
- lineBefore = before.split('\n').pop(),
338
- lineBeforeCount = toCount(lineBefore);
339
- return $.select(start - lineBeforeCount, end + lineAfterCount).wrap(open, close, wrap);
314
+ !isFunction($$.wrapLine) && ($$.wrapLine = function (open, close, wrap, withSpaces = false) {
315
+ return this.selectLine(withSpaces).wrap(open, close, wrap);
340
316
  });
341
- return $.on('key.down', onKeyDown).record();
317
+ return $.on('key.down', onKeyDown).on('put.down', onPutDown).record();
342
318
  }
343
319
 
344
320
  function detach() {
345
- return this.off('key.down', onKeyDown);
321
+ return this.off('key.down', onKeyDown).off('put.down', onPutDown);
346
322
  }
347
323
 
348
324
  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.3"
56
+ "version": "3.0.5"
57
57
  }