@taufik-nurrohman/text-editor.source 2.2.8 → 2.2.10

Sign up to get free protection for your applications and to get access to all the features.
package/index.js CHANGED
@@ -37,12 +37,12 @@
37
37
  return 'undefined' !== typeof x;
38
38
  };
39
39
  var isInstance = function isInstance(x, of) {
40
- return x && isSet$1(of) && x instanceof of ;
40
+ return x && isSet(of) && x instanceof of ;
41
41
  };
42
42
  var isNull = function isNull(x) {
43
43
  return null === x;
44
44
  };
45
- var isSet$1 = function isSet(x) {
45
+ var isSet = function isSet(x) {
46
46
  return isDefined(x) && !isNull(x);
47
47
  };
48
48
  var isString = function isString(x) {
@@ -75,7 +75,7 @@
75
75
  }
76
76
  // No need to escape `/` in the pattern string
77
77
  pattern = pattern.replace(/\//g, '\\/');
78
- return new RegExp(pattern, isSet$1(opt) ? opt : 'g');
78
+ return new RegExp(pattern, isSet(opt) ? opt : 'g');
79
79
  };
80
80
  var pairs = {
81
81
  '`': '`',
@@ -141,11 +141,11 @@
141
141
  var CTRL_PREFIX = 'Control-';
142
142
  var SHIFT_PREFIX = 'Shift-';
143
143
 
144
- function canKeyDown(map, that) {
144
+ function canKeyDown(map, of) {
145
145
  var charAfter,
146
146
  charBefore,
147
- charIndent = that.state.source.tab || that.state.tab || '\t',
148
- charPairs = that.state.source.pairs || {},
147
+ charIndent = of.state.source.tab || of.state.tab || '\t',
148
+ charPairs = of.state.source.pairs || {},
149
149
  charPairsValues = toObjectValues(charPairs),
150
150
  key = map.key,
151
151
  queue = map.queue,
@@ -155,42 +155,42 @@
155
155
  return true;
156
156
  }
157
157
  if (' ' === keyValue) {
158
- var _that$$ = that.$(),
159
- _after = _that$$.after,
160
- _before = _that$$.before,
161
- _value = _that$$.value;
158
+ var _of$$ = of.$(),
159
+ _after = _of$$.after,
160
+ _before = _of$$.before,
161
+ _value = _of$$.value;
162
162
  charAfter = charPairs[charBefore = _before.slice(-1)];
163
163
  if (!_value && charAfter && charBefore && charAfter === _after[0]) {
164
- that.wrap(' ', ' ');
164
+ of.wrap(' ', ' ');
165
165
  return false;
166
166
  }
167
167
  return true;
168
168
  }
169
169
  if ('Enter' === keyValue) {
170
- var _that$$2 = that.$(),
171
- _after2 = _that$$2.after,
172
- _before2 = _that$$2.before,
173
- _value2 = _that$$2.value,
170
+ var _of$$2 = of.$(),
171
+ _after2 = _of$$2.after,
172
+ _before2 = _of$$2.before,
173
+ _value2 = _of$$2.value,
174
174
  lineBefore = _before2.split('\n').pop(),
175
175
  lineMatch = lineBefore.match(/^(\s+)/),
176
176
  lineMatchIndent = lineMatch && lineMatch[1] || "";
177
177
  if (!_value2) {
178
178
  if (_after2 && _before2 && (charAfter = charPairs[charBefore = _before2.slice(-1)]) && charAfter === _after2[0]) {
179
- that.wrap('\n' + lineMatchIndent + (charBefore !== charAfter ? charIndent : ""), '\n' + lineMatchIndent).record();
179
+ of.wrap('\n' + lineMatchIndent + (charBefore !== charAfter ? charIndent : ""), '\n' + lineMatchIndent).record();
180
180
  return false;
181
181
  }
182
182
  if (lineMatchIndent) {
183
- that.insert('\n' + lineMatchIndent, -1).record();
183
+ of.insert('\n' + lineMatchIndent, -1).record();
184
184
  return false;
185
185
  }
186
186
  }
187
187
  return true;
188
188
  }
189
189
  if ('Backspace' === keyValue) {
190
- var _that$$3 = that.$(),
191
- _after3 = _that$$3.after,
192
- _before3 = _that$$3.before,
193
- _value3 = _that$$3.value;
190
+ var _of$$3 = of.$(),
191
+ _after3 = _of$$3.after,
192
+ _before3 = _of$$3.before,
193
+ _value3 = _of$$3.value;
194
194
  _after3.split('\n')[0];
195
195
  var _lineBefore = _before3.split('\n').pop(),
196
196
  _lineMatch = _lineBefore.match(/^(\s+)/),
@@ -202,7 +202,7 @@
202
202
  }
203
203
  if (_value3) {
204
204
  if (_after3 && _before3 && charAfter && charAfter === _after3[0] && !_before3.endsWith('\\' + charBefore)) {
205
- that.record().peel(charBefore, charAfter).record();
205
+ of.record().peel(charBefore, charAfter).record();
206
206
  return false;
207
207
  }
208
208
  return true;
@@ -211,29 +211,29 @@
211
211
  if (charAfter && charBefore) {
212
212
  if (_after3.startsWith(' ' + charAfter) && _before3.endsWith(charBefore + ' ') || _after3.startsWith('\n' + _lineMatchIndent + charAfter) && _before3.endsWith(charBefore + '\n' + _lineMatchIndent)) {
213
213
  // Collapse bracket(s)
214
- that.trim("", "").record();
214
+ of.trim("", "").record();
215
215
  return false;
216
216
  }
217
217
  }
218
218
  // Outdent
219
219
  if (_lineBefore.endsWith(charIndent)) {
220
- that.pull(charIndent).record();
220
+ of.pull(charIndent).record();
221
221
  return false;
222
222
  }
223
223
  if (_after3 && _before3 && !_before3.endsWith('\\' + charBefore)) {
224
224
  if (charAfter === _after3[0] && charBefore === _before3.slice(-1)) {
225
225
  // Peel pair
226
- that.peel(charBefore, charAfter).record();
226
+ of.peel(charBefore, charAfter).record();
227
227
  return false;
228
228
  }
229
229
  }
230
230
  return true;
231
231
  }
232
- var _that$$4 = that.$(),
233
- after = _that$$4.after,
234
- before = _that$$4.before,
235
- start = _that$$4.start,
236
- value = _that$$4.value;
232
+ var _of$$4 = of.$(),
233
+ after = _of$$4.after,
234
+ before = _of$$4.before,
235
+ start = _of$$4.start,
236
+ value = _of$$4.value;
237
237
  // Do nothing on escape
238
238
  if ('\\' === (charBefore = before.slice(-1))) {
239
239
  return true;
@@ -243,7 +243,7 @@
243
243
  if (!value && after && before && charAfter && key === charAfter) {
244
244
  // Move to the next character
245
245
  // `}|`
246
- that.select(start + 1).record();
246
+ of.select(start + 1).record();
247
247
  return false;
248
248
  }
249
249
  for (charBefore in charPairs) {
@@ -252,7 +252,7 @@
252
252
  if (key === charBefore && charAfter) {
253
253
  // Wrap pair or selection
254
254
  // `{|}` `{|aaa|}`
255
- that.wrap(charBefore, charAfter).record();
255
+ of.wrap(charBefore, charAfter).record();
256
256
  return false;
257
257
  }
258
258
  // `|}`
@@ -260,7 +260,7 @@
260
260
  if (value) {
261
261
  // Wrap selection
262
262
  // `{|aaa|}`
263
- that.record().wrap(charBefore, charAfter).record();
263
+ of.record().wrap(charBefore, charAfter).record();
264
264
  return false;
265
265
  }
266
266
  break;
@@ -269,34 +269,34 @@
269
269
  return true;
270
270
  }
271
271
 
272
- function canKeyDownDent(map, that) {
273
- var charIndent = that.state.source.tab || that.state.tab || '\t';
272
+ function canKeyDownDent(map, of) {
273
+ var charIndent = of.state.source.tab || of.state.tab || '\t';
274
274
  map.key;
275
275
  map.queue;
276
276
  var keyValue = map + "";
277
277
  // Indent with `⎈]`
278
278
  if (CTRL_PREFIX + ']' === keyValue) {
279
- that.push(charIndent).record();
279
+ of.push(charIndent).record();
280
280
  return false;
281
281
  }
282
282
  // Outdent with `⎈[`
283
283
  if (CTRL_PREFIX + '[' === keyValue) {
284
- that.pull(charIndent).record();
284
+ of.pull(charIndent).record();
285
285
  return false;
286
286
  }
287
287
  return true;
288
288
  }
289
289
 
290
- function canKeyDownEnter(map, that) {
290
+ function canKeyDownEnter(map, of) {
291
291
  map.key;
292
292
  var queue = map.queue;
293
293
  if (queue.Control && queue.Enter) {
294
- var _that$$5 = that.$(),
295
- after = _that$$5.after,
296
- before = _that$$5.before,
297
- end = _that$$5.end,
298
- start = _that$$5.start,
299
- value = _that$$5.value,
294
+ var _of$$5 = of.$(),
295
+ after = _of$$5.after,
296
+ before = _of$$5.before,
297
+ end = _of$$5.end,
298
+ start = _of$$5.start,
299
+ value = _of$$5.value,
300
300
  lineAfter = after.split('\n').shift(),
301
301
  lineBefore = before.split('\n').pop(),
302
302
  lineMatch = lineBefore.match(/^(\s+)/),
@@ -304,44 +304,44 @@
304
304
  if (before || after) {
305
305
  if (queue.Shift) {
306
306
  // Insert line over with `⎈⇧↵`
307
- return that.select(start - toCount(lineBefore)).wrap(lineMatchIndent, '\n').insert(value).record(), false;
307
+ return of.select(start - toCount(lineBefore)).wrap(lineMatchIndent, '\n').insert(value).record(), false;
308
308
  }
309
309
  // Insert line below with `⎈↵`
310
- return that.select(end + toCount(lineAfter)).wrap('\n' + lineMatchIndent, "").insert(value).record(), false;
310
+ return of.select(end + toCount(lineAfter)).wrap('\n' + lineMatchIndent, "").insert(value).record(), false;
311
311
  }
312
312
  }
313
313
  return true;
314
314
  }
315
315
 
316
- function canKeyDownHistory(map, that) {
316
+ function canKeyDownHistory(map, of) {
317
317
  var keyValue = map + "";
318
318
  // Redo with `⎈y`
319
319
  if (CTRL_PREFIX + 'y' === keyValue) {
320
- return that.redo(), false;
320
+ return of.redo(), false;
321
321
  }
322
322
  // Undo with `⎈z`
323
323
  if (CTRL_PREFIX + 'z' === keyValue) {
324
- return that.undo(), false;
324
+ return of.undo(), false;
325
325
  }
326
326
  return true;
327
327
  }
328
328
 
329
- function canKeyDownMove(map, that) {
329
+ function canKeyDownMove(map, of) {
330
330
  map.key;
331
331
  var queue = map.queue,
332
332
  keyValue = map + "";
333
333
  if (!queue.Control) {
334
334
  return true;
335
335
  }
336
- var _that$$6 = that.$(),
337
- after = _that$$6.after,
338
- before = _that$$6.before,
339
- end = _that$$6.end,
340
- start = _that$$6.start,
341
- value = _that$$6.value,
336
+ var _of$$6 = of.$(),
337
+ after = _of$$6.after,
338
+ before = _of$$6.before,
339
+ end = _of$$6.end,
340
+ start = _of$$6.start,
341
+ value = _of$$6.value,
342
342
  charPair,
343
343
  charPairValue,
344
- charPairs = that.state.source.pairs || {},
344
+ charPairs = of.state.source.pairs || {},
345
345
  boundaries = [],
346
346
  m;
347
347
  if (value) {
@@ -356,17 +356,17 @@
356
356
  boundaries.push('[\\s\\S]'); // Last try!
357
357
  if (CTRL_PREFIX + 'ArrowLeft' === keyValue) {
358
358
  if (m = before.match(toPattern('(' + boundaries.join('|') + ')$', ""))) {
359
- that.insert("").select(start - toCount(m[0])).insert(value);
360
- return that.record(), false;
359
+ of.insert("").select(start - toCount(m[0])).insert(value);
360
+ return of.record(), false;
361
361
  }
362
- return that.select(), false;
362
+ return of.select(), false;
363
363
  }
364
364
  if (CTRL_PREFIX + 'ArrowRight' === keyValue) {
365
365
  if (m = after.match(toPattern('^(' + boundaries.join('|') + ')', ""))) {
366
- that.insert("").select(end + toCount(m[0]) - toCount(value)).insert(value);
367
- return that.record(), false;
366
+ of.insert("").select(end + toCount(m[0]) - toCount(value)).insert(value);
367
+ return of.record(), false;
368
368
  }
369
- return that.select(), false;
369
+ return of.select(), false;
370
370
  }
371
371
  }
372
372
  var lineAfter = after.split('\n').shift(),
@@ -379,57 +379,57 @@
379
379
  value = lineBefore + value + lineAfter;
380
380
  if (CTRL_PREFIX + 'ArrowUp' === keyValue) {
381
381
  if (!hasValue('\n', before)) {
382
- return that.select(), false;
382
+ return of.select(), false;
383
383
  }
384
- that.insert("");
385
- that.replace(/^([^\n]*?)(\n|$)/, '$2', 1);
386
- that.replace(/(^|\n)([^\n]*?)$/, "", -1);
387
- var $ = that.$();
384
+ of.insert("");
385
+ of.replace(/^([^\n]*?)(\n|$)/, '$2', 1);
386
+ of.replace(/(^|\n)([^\n]*?)$/, "", -1);
387
+ var $ = of.$();
388
388
  before = $.before;
389
389
  start = $.start;
390
390
  lineBefore = before.split('\n').pop();
391
- that.select(start = start - toCount(lineBefore)).wrap(value, '\n');
392
- that.select(start, start + toCount(value));
393
- return that.record(), false;
391
+ of.select(start = start - toCount(lineBefore)).wrap(value, '\n');
392
+ of.select(start, start + toCount(value));
393
+ return of.record(), false;
394
394
  }
395
395
  if (CTRL_PREFIX + 'ArrowDown' === keyValue) {
396
396
  if (!hasValue('\n', after)) {
397
- return that.select(), false;
397
+ return of.select(), false;
398
398
  }
399
- that.insert("");
400
- that.replace(/^([^\n]*?)(\n|$)/, "", 1);
401
- that.replace(/(^|\n)([^\n]*?)$/, '$1', -1);
402
- var _$ = that.$();
399
+ of.insert("");
400
+ of.replace(/^([^\n]*?)(\n|$)/, "", 1);
401
+ of.replace(/(^|\n)([^\n]*?)$/, '$1', -1);
402
+ var _$ = of.$();
403
403
  after = _$.after;
404
404
  end = _$.end;
405
405
  lineAfter = after.split('\n').shift();
406
- that.select(end = end + toCount(lineAfter)).wrap('\n', value);
406
+ of.select(end = end + toCount(lineAfter)).wrap('\n', value);
407
407
  end += 1;
408
- that.select(end, end + toCount(value));
409
- return that.record(), false;
408
+ of.select(end, end + toCount(value));
409
+ return of.record(), false;
410
410
  }
411
411
  return true;
412
412
  }
413
413
 
414
- function canKeyDownTab(map, that) {
415
- var charIndent = that.state.source.tab || that.state.tab || '\t',
414
+ function canKeyDownTab(map, of) {
415
+ var charIndent = of.state.source.tab || of.state.tab || '\t',
416
416
  keyValue = map + "";
417
417
  // Indent with `⇥`
418
418
  if ('Tab' === keyValue) {
419
- return that.push(charIndent).record(), false;
419
+ return of.push(charIndent).record(), false;
420
420
  }
421
421
  // Outdent with `⇧+⇥`
422
422
  if (SHIFT_PREFIX + 'Tab' === keyValue) {
423
- return that.pull(charIndent).record(), false;
423
+ return of.pull(charIndent).record(), false;
424
424
  }
425
425
  return true;
426
426
  }
427
- var bounce = debounce(function (that) {
428
- return that.record();
427
+ var bounce = debounce(function (of) {
428
+ return of.record();
429
429
  }, 100);
430
430
 
431
- function canKeyUp(map, that) {
432
- return bounce(that), true;
431
+ function canKeyUp(map, of) {
432
+ return bounce(of), true;
433
433
  }
434
434
  var state = defaults;
435
435
  exports.canKeyDown = canKeyDown;
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?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(((e="undefined"!=typeof globalThis?globalThis:e||self).TE=e.TE||{},e.TE.Source={}))}(this,(function(e){"use strict";var r=function(e,r){return-1!==r.indexOf(e)},t=function(e){return function(e){return void 0!==e}(e)&&!function(e){return null===e}(e)},n=function(e){return e.length},i=window,o=function(e){return n=RegExp,(r=e)&&t(n)&&r instanceof n;var r,n},a=function(e,r){return o(e)?e:(e=e.replace(/\//g,"\\/"),RegExp(e,t(r)?r:"g"))};function s(e,r){return new Promise((function(t,n){var o=i[e].apply(i,r);return o?t(o):n(o)}))}var u={source:{pairs:{"`":"`","(":")","{":"}","[":"]",'"':'"',"'":"'","<":">"},type:null}};["alert","confirm","prompt"].forEach((function(e){u.source[e]=function(){for(var r=arguments.length,t=Array(r),n=0;n<r;n++)t[n]=arguments[n];return s(e,t)}}));var c={};c.toggle=function(e,r,t,i){void 0===i&&(i=!1),r||""===r||(r=e);var o=this,a=o.$(),s=a.after,u=a.before,c=a.value,f=n(r),l=n(e);return t&&r===c.slice(-f)&&e===c.slice(0,l)||r===s.slice(0,f)&&e===u.slice(-l)?o.peel(e,r,t):(!1!==i&&("string"==typeof i?i=[i,i]:function(e){return Array.isArray(e)}(i)||(i=["",""]),isSet(i[1])||(i[1]=i[0]),o.trim(i[0],i[1])),o.wrap(e,r,t))};var f="Control-";var l,p,d,v=(l=function(e){return e.record()},p=100,function(){var e=arguments,r=this;d&&clearTimeout(d),d=setTimeout((function(){return l.apply(r,e)}),p)});var h=u;e.canKeyDown=function(e,t){var n,i,o=t.state.source.tab||t.state.tab||"\t",a=t.state.source.pairs||{},s=Object.values(a),u=e.key,c=e.queue,f=e+"";if(c.Alt||c.Control)return!0;if(" "===f){var l=t.$(),p=l.after,d=l.before,v=l.value;return n=a[i=d.slice(-1)],!(!v&&n&&i&&n===p[0])||(t.wrap(" "," "),!1)}if("Enter"===f){var h=t.$(),y=h.after,w=h.before,b=h.value,m=w.split("\n").pop().match(/^(\s+)/),$=m&&m[1]||"";if(!b){if(y&&w&&(n=a[i=w.slice(-1)])&&n===y[0])return t.wrap("\n"+$+(i!==n?o:""),"\n"+$).record(),!1;if($)return t.insert("\n"+$,-1).record(),!1}return!0}if("Backspace"===f){var g=t.$(),T=g.after,E=g.before,A=g.value;T.split("\n")[0];var D=E.split("\n").pop(),K=D.match(/^(\s+)/),W=K&&K[1]||"";return n=a[i=E.slice(-1)],"\\"===i?!0:A?!(T&&E&&n&&n===T[0]&&!E.endsWith("\\"+i))||(t.record().peel(i,n).record(),!1):(n=a[i=E.trim().slice(-1)])&&i&&(T.startsWith(" "+n)&&E.endsWith(i+" ")||T.startsWith("\n"+W+n)&&E.endsWith(i+"\n"+W))?(t.trim("","").record(),!1):D.endsWith(o)?(t.pull(o).record(),!1):!(T&&E&&!E.endsWith("\\"+i)&&n===T[0]&&i===E.slice(-1))||(t.peel(i,n).record(),!1)}var k=t.$(),x=k.after,j=k.before,S=k.start,q=k.value;if("\\"===(i=j.slice(-1)))return!0;if(n=r(x[0],s)?x[0]:a[i],!q&&x&&j&&n&&u===n)return t.select(S+1).record(),!1;for(i in a){if(n=a[i],u===i&&n)return t.wrap(i,n).record(),!1;if(u===n){if(q)return t.record().wrap(i,n).record(),!1;break}}return!0},e.canKeyDownDent=function(e,r){var t=r.state.source.tab||r.state.tab||"\t";e.key,e.queue;var n=e+"";return f+"]"===n?(r.push(t).record(),!1):f+"["!==n||(r.pull(t).record(),!1)},e.canKeyDownEnter=function(e,r){e.key;var t=e.queue;if(t.Control&&t.Enter){var i=r.$(),o=i.after,a=i.before,s=i.end,u=i.start,c=i.value,f=o.split("\n").shift(),l=a.split("\n").pop(),p=l.match(/^(\s+)/),d=p&&p[1]||"";if(a||o)return t.Shift?(r.select(u-n(l)).wrap(d,"\n").insert(c).record(),!1):(r.select(s+n(f)).wrap("\n"+d,"").insert(c).record(),!1)}return!0},e.canKeyDownHistory=function(e,r){var t=e+"";return f+"y"===t?(r.redo(),!1):f+"z"!==t||(r.undo(),!1)},e.canKeyDownMove=function(e,t){e.key;var i=e+"";if(!e.queue.Control)return!0;var o,s,u,c=t.$(),l=c.after,p=c.before,d=c.end,v=c.start,h=c.value,y=t.state.source.pairs||{},w=[];if(h){for(o in y)(s=y[o])&&w.push("(?:\\"+o+"(?:\\\\.|[^\\"+o+(s!==o?"\\"+s:"")+"])*\\"+s+")");if(w.push("\\w+"),w.push("\\s+"),w.push("[\\s\\S]"),f+"ArrowLeft"===i)return(u=p.match(a("("+w.join("|")+")$","")))?(t.insert("").select(v-n(u[0])).insert(h),t.record(),!1):(t.select(),!1);if(f+"ArrowRight"===i)return(u=l.match(a("^("+w.join("|")+")","")))?(t.insert("").select(d+n(u[0])-n(h)).insert(h),t.record(),!1):(t.select(),!1)}var b=l.split("\n").shift(),m=p.split("\n").pop(),$=m.match(/^(\s+)/);if($&&$[1],d+=n(b),v-=n(m),h=m+h+b,f+"ArrowUp"===i){if(!r("\n",p))return t.select(),!1;t.insert(""),t.replace(/^([^\n]*?)(\n|$)/,"$2",1),t.replace(/(^|\n)([^\n]*?)$/,"",-1);var g=t.$();return p=g.before,v=g.start,m=p.split("\n").pop(),t.select(v-=n(m)).wrap(h,"\n"),t.select(v,v+n(h)),t.record(),!1}if(f+"ArrowDown"===i){if(!r("\n",l))return t.select(),!1;t.insert(""),t.replace(/^([^\n]*?)(\n|$)/,"",1),t.replace(/(^|\n)([^\n]*?)$/,"$1",-1);var T=t.$();return l=T.after,d=T.end,b=l.split("\n").shift(),t.select(d+=n(b)).wrap("\n",h),d+=1,t.select(d,d+n(h)),t.record(),!1}return!0},e.canKeyDownTab=function(e,r){var t=r.state.source.tab||r.state.tab||"\t",n=e+"";return"Tab"===n?(r.push(t).record(),!1):"Shift-Tab"!==n||(r.pull(t).record(),!1)},e.canKeyUp=function(e,r){return v(r),!0},e.state=h,e.that=c,Object.defineProperty(e,"__esModule",{value:!0})}));
26
+ !function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(((e="undefined"!=typeof globalThis?globalThis:e||self).TE=e.TE||{},e.TE.Source={}))}(this,(function(e){"use strict";var r=function(e,r){return-1!==r.indexOf(e)},t=function(e){return function(e){return void 0!==e}(e)&&!function(e){return null===e}(e)},n=function(e){return e.length},i=window,o=function(e){return n=RegExp,(r=e)&&t(n)&&r instanceof n;var r,n},a=function(e,r){return o(e)?e:(e=e.replace(/\//g,"\\/"),RegExp(e,t(r)?r:"g"))};function u(e,r){return new Promise((function(t,n){var o=i[e].apply(i,r);return o?t(o):n(o)}))}var s={source:{pairs:{"`":"`","(":")","{":"}","[":"]",'"':'"',"'":"'","<":">"},type:null}};["alert","confirm","prompt"].forEach((function(e){s.source[e]=function(){for(var r=arguments.length,t=Array(r),n=0;n<r;n++)t[n]=arguments[n];return u(e,t)}}));var c={};c.toggle=function(e,r,i,o){void 0===o&&(o=!1),r||""===r||(r=e);var a=this,u=a.$(),s=u.after,c=u.before,f=u.value,l=n(r),p=n(e);return i&&r===f.slice(-l)&&e===f.slice(0,p)||r===s.slice(0,l)&&e===c.slice(-p)?a.peel(e,r,i):(!1!==o&&("string"==typeof o?o=[o,o]:function(e){return Array.isArray(e)}(o)||(o=["",""]),t(o[1])||(o[1]=o[0]),a.trim(o[0],o[1])),a.wrap(e,r,i))};var f="Control-";var l,p,d,v=(l=function(e){return e.record()},p=100,function(){var e=arguments,r=this;d&&clearTimeout(d),d=setTimeout((function(){return l.apply(r,e)}),p)});var h=s;e.canKeyDown=function(e,t){var n,i,o=t.state.source.tab||t.state.tab||"\t",a=t.state.source.pairs||{},u=Object.values(a),s=e.key,c=e.queue,f=e+"";if(c.Alt||c.Control)return!0;if(" "===f){var l=t.$(),p=l.after,d=l.before,v=l.value;return n=a[i=d.slice(-1)],!(!v&&n&&i&&n===p[0])||(t.wrap(" "," "),!1)}if("Enter"===f){var h=t.$(),y=h.after,w=h.before,b=h.value,m=w.split("\n").pop().match(/^(\s+)/),$=m&&m[1]||"";if(!b){if(y&&w&&(n=a[i=w.slice(-1)])&&n===y[0])return t.wrap("\n"+$+(i!==n?o:""),"\n"+$).record(),!1;if($)return t.insert("\n"+$,-1).record(),!1}return!0}if("Backspace"===f){var g=t.$(),T=g.after,E=g.before,A=g.value;T.split("\n")[0];var D=E.split("\n").pop(),K=D.match(/^(\s+)/),W=K&&K[1]||"";return n=a[i=E.slice(-1)],"\\"===i?!0:A?!(T&&E&&n&&n===T[0]&&!E.endsWith("\\"+i))||(t.record().peel(i,n).record(),!1):(n=a[i=E.trim().slice(-1)])&&i&&(T.startsWith(" "+n)&&E.endsWith(i+" ")||T.startsWith("\n"+W+n)&&E.endsWith(i+"\n"+W))?(t.trim("","").record(),!1):D.endsWith(o)?(t.pull(o).record(),!1):!(T&&E&&!E.endsWith("\\"+i)&&n===T[0]&&i===E.slice(-1))||(t.peel(i,n).record(),!1)}var k=t.$(),x=k.after,j=k.before,q=k.start,C=k.value;if("\\"===(i=j.slice(-1)))return!0;if(n=r(x[0],u)?x[0]:a[i],!C&&x&&j&&n&&s===n)return t.select(q+1).record(),!1;for(i in a){if(n=a[i],s===i&&n)return t.wrap(i,n).record(),!1;if(s===n){if(C)return t.record().wrap(i,n).record(),!1;break}}return!0},e.canKeyDownDent=function(e,r){var t=r.state.source.tab||r.state.tab||"\t";e.key,e.queue;var n=e+"";return f+"]"===n?(r.push(t).record(),!1):f+"["!==n||(r.pull(t).record(),!1)},e.canKeyDownEnter=function(e,r){e.key;var t=e.queue;if(t.Control&&t.Enter){var i=r.$(),o=i.after,a=i.before,u=i.end,s=i.start,c=i.value,f=o.split("\n").shift(),l=a.split("\n").pop(),p=l.match(/^(\s+)/),d=p&&p[1]||"";if(a||o)return t.Shift?(r.select(s-n(l)).wrap(d,"\n").insert(c).record(),!1):(r.select(u+n(f)).wrap("\n"+d,"").insert(c).record(),!1)}return!0},e.canKeyDownHistory=function(e,r){var t=e+"";return f+"y"===t?(r.redo(),!1):f+"z"!==t||(r.undo(),!1)},e.canKeyDownMove=function(e,t){e.key;var i=e+"";if(!e.queue.Control)return!0;var o,u,s,c=t.$(),l=c.after,p=c.before,d=c.end,v=c.start,h=c.value,y=t.state.source.pairs||{},w=[];if(h){for(o in y)(u=y[o])&&w.push("(?:\\"+o+"(?:\\\\.|[^\\"+o+(u!==o?"\\"+u:"")+"])*\\"+u+")");if(w.push("\\w+"),w.push("\\s+"),w.push("[\\s\\S]"),f+"ArrowLeft"===i)return(s=p.match(a("("+w.join("|")+")$","")))?(t.insert("").select(v-n(s[0])).insert(h),t.record(),!1):(t.select(),!1);if(f+"ArrowRight"===i)return(s=l.match(a("^("+w.join("|")+")","")))?(t.insert("").select(d+n(s[0])-n(h)).insert(h),t.record(),!1):(t.select(),!1)}var b=l.split("\n").shift(),m=p.split("\n").pop(),$=m.match(/^(\s+)/);if($&&$[1],d+=n(b),v-=n(m),h=m+h+b,f+"ArrowUp"===i){if(!r("\n",p))return t.select(),!1;t.insert(""),t.replace(/^([^\n]*?)(\n|$)/,"$2",1),t.replace(/(^|\n)([^\n]*?)$/,"",-1);var g=t.$();return p=g.before,v=g.start,m=p.split("\n").pop(),t.select(v-=n(m)).wrap(h,"\n"),t.select(v,v+n(h)),t.record(),!1}if(f+"ArrowDown"===i){if(!r("\n",l))return t.select(),!1;t.insert(""),t.replace(/^([^\n]*?)(\n|$)/,"",1),t.replace(/(^|\n)([^\n]*?)$/,"$1",-1);var T=t.$();return l=T.after,d=T.end,b=l.split("\n").shift(),t.select(d+=n(b)).wrap("\n",h),d+=1,t.select(d,d+n(h)),t.record(),!1}return!0},e.canKeyDownTab=function(e,r){var t=r.state.source.tab||r.state.tab||"\t",n=e+"";return"Tab"===n?(r.push(t).record(),!1):"Shift-Tab"!==n||(r.pull(t).record(),!1)},e.canKeyUp=function(e,r){return v(r),!0},e.state=h,e.that=c,Object.defineProperty(e,"__esModule",{value:!0})}));
package/index.mjs CHANGED
@@ -2,7 +2,7 @@ import {W} from '@taufik-nurrohman/document';
2
2
  import {debounce} from '@taufik-nurrohman/tick';
3
3
  import {esc, escChar, toPattern} from '@taufik-nurrohman/pattern';
4
4
  import {hasValue} from '@taufik-nurrohman/has';
5
- import {isArray, isString} from '@taufik-nurrohman/is';
5
+ import {isArray, isSet, isString} from '@taufik-nurrohman/is';
6
6
  import {toCount, toObjectValues} from '@taufik-nurrohman/to';
7
7
 
8
8
  const pairs = {
@@ -67,11 +67,11 @@ const ALT_PREFIX = 'Alt-';
67
67
  const CTRL_PREFIX = 'Control-';
68
68
  const SHIFT_PREFIX = 'Shift-';
69
69
 
70
- export function canKeyDown(map, that) {
70
+ export function canKeyDown(map, of) {
71
71
  let charAfter,
72
72
  charBefore,
73
- charIndent = that.state.source.tab || that.state.tab || '\t',
74
- charPairs = that.state.source.pairs || {},
73
+ charIndent = of.state.source.tab || of.state.tab || '\t',
74
+ charPairs = of.state.source.pairs || {},
75
75
  charPairsValues = toObjectValues(charPairs),
76
76
  {key, queue} = map,
77
77
  keyValue = map + "";
@@ -80,33 +80,33 @@ export function canKeyDown(map, that) {
80
80
  return true;
81
81
  }
82
82
  if (' ' === keyValue) {
83
- let {after, before, value} = that.$();
83
+ let {after, before, value} = of.$();
84
84
  charAfter = charPairs[charBefore = before.slice(-1)];
85
85
  if (!value && charAfter && charBefore && charAfter === after[0]) {
86
- that.wrap(' ', ' ');
86
+ of.wrap(' ', ' ');
87
87
  return false;
88
88
  }
89
89
  return true;
90
90
  }
91
91
  if ('Enter' === keyValue) {
92
- let {after, before, value} = that.$(),
92
+ let {after, before, value} = of.$(),
93
93
  lineBefore = before.split('\n').pop(),
94
94
  lineMatch = lineBefore.match(/^(\s+)/),
95
95
  lineMatchIndent = lineMatch && lineMatch[1] || "";
96
96
  if (!value) {
97
97
  if (after && before && (charAfter = charPairs[charBefore = before.slice(-1)]) && charAfter === after[0]) {
98
- that.wrap('\n' + lineMatchIndent + (charBefore !== charAfter ? charIndent : ""), '\n' + lineMatchIndent).record();
98
+ of.wrap('\n' + lineMatchIndent + (charBefore !== charAfter ? charIndent : ""), '\n' + lineMatchIndent).record();
99
99
  return false;
100
100
  }
101
101
  if (lineMatchIndent) {
102
- that.insert('\n' + lineMatchIndent, -1).record();
102
+ of.insert('\n' + lineMatchIndent, -1).record();
103
103
  return false;
104
104
  }
105
105
  }
106
106
  return true;
107
107
  }
108
108
  if ('Backspace' === keyValue) {
109
- let {after, before, value} = that.$(),
109
+ let {after, before, value} = of.$(),
110
110
  lineAfter = after.split('\n')[0],
111
111
  lineBefore = before.split('\n').pop(),
112
112
  lineMatch = lineBefore.match(/^(\s+)/),
@@ -118,7 +118,7 @@ export function canKeyDown(map, that) {
118
118
  }
119
119
  if (value) {
120
120
  if (after && before && charAfter && charAfter === after[0] && !before.endsWith('\\' + charBefore)) {
121
- that.record().peel(charBefore, charAfter).record();
121
+ of.record().peel(charBefore, charAfter).record();
122
122
  return false;
123
123
  }
124
124
  return true;
@@ -130,25 +130,25 @@ export function canKeyDown(map, that) {
130
130
  after.startsWith('\n' + lineMatchIndent + charAfter) && before.endsWith(charBefore + '\n' + lineMatchIndent)
131
131
  ) {
132
132
  // Collapse bracket(s)
133
- that.trim("", "").record();
133
+ of.trim("", "").record();
134
134
  return false;
135
135
  }
136
136
  }
137
137
  // Outdent
138
138
  if (lineBefore.endsWith(charIndent)) {
139
- that.pull(charIndent).record();
139
+ of.pull(charIndent).record();
140
140
  return false;
141
141
  }
142
142
  if (after && before && !before.endsWith('\\' + charBefore)) {
143
143
  if (charAfter === after[0] && charBefore === before.slice(-1)) {
144
144
  // Peel pair
145
- that.peel(charBefore, charAfter).record();
145
+ of.peel(charBefore, charAfter).record();
146
146
  return false;
147
147
  }
148
148
  }
149
149
  return true;
150
150
  }
151
- let {after, before, start, value} = that.$();
151
+ let {after, before, start, value} = of.$();
152
152
  // Do nothing on escape
153
153
  if ('\\' === (charBefore = before.slice(-1))) {
154
154
  return true;
@@ -158,7 +158,7 @@ export function canKeyDown(map, that) {
158
158
  if (!value && after && before && charAfter && key === charAfter) {
159
159
  // Move to the next character
160
160
  // `}|`
161
- that.select(start + 1).record();
161
+ of.select(start + 1).record();
162
162
  return false;
163
163
  }
164
164
  for (charBefore in charPairs) {
@@ -167,7 +167,7 @@ export function canKeyDown(map, that) {
167
167
  if (key === charBefore && charAfter) {
168
168
  // Wrap pair or selection
169
169
  // `{|}` `{|aaa|}`
170
- that.wrap(charBefore, charAfter).record();
170
+ of.wrap(charBefore, charAfter).record();
171
171
  return false;
172
172
  }
173
173
  // `|}`
@@ -175,7 +175,7 @@ export function canKeyDown(map, that) {
175
175
  if (value) {
176
176
  // Wrap selection
177
177
  // `{|aaa|}`
178
- that.record().wrap(charBefore, charAfter).record();
178
+ of.record().wrap(charBefore, charAfter).record();
179
179
  return false;
180
180
  }
181
181
  break;
@@ -184,27 +184,27 @@ export function canKeyDown(map, that) {
184
184
  return true;
185
185
  }
186
186
 
187
- export function canKeyDownDent(map, that) {
188
- let charIndent = that.state.source.tab || that.state.tab || '\t',
187
+ export function canKeyDownDent(map, of) {
188
+ let charIndent = of.state.source.tab || of.state.tab || '\t',
189
189
  {key, queue} = map,
190
190
  keyValue = map + "";
191
191
  // Indent with `⎈]`
192
192
  if (CTRL_PREFIX + ']' === keyValue) {
193
- that.push(charIndent).record();
193
+ of.push(charIndent).record();
194
194
  return false;
195
195
  }
196
196
  // Outdent with `⎈[`
197
197
  if (CTRL_PREFIX + '[' === keyValue) {
198
- that.pull(charIndent).record();
198
+ of.pull(charIndent).record();
199
199
  return false;
200
200
  }
201
201
  return true;
202
202
  }
203
203
 
204
- export function canKeyDownEnter(map, that) {
204
+ export function canKeyDownEnter(map, of) {
205
205
  let {key, queue} = map;
206
206
  if (queue.Control && queue.Enter) {
207
- let {after, before, end, start, value} = that.$(),
207
+ let {after, before, end, start, value} = of.$(),
208
208
  lineAfter = after.split('\n').shift(),
209
209
  lineBefore = before.split('\n').pop(),
210
210
  lineMatch = lineBefore.match(/^(\s+)/),
@@ -212,37 +212,37 @@ export function canKeyDownEnter(map, that) {
212
212
  if (before || after) {
213
213
  if (queue.Shift) {
214
214
  // Insert line over with `⎈⇧↵`
215
- return that.select(start - toCount(lineBefore)).wrap(lineMatchIndent, '\n').insert(value).record(), false;
215
+ return of.select(start - toCount(lineBefore)).wrap(lineMatchIndent, '\n').insert(value).record(), false;
216
216
  }
217
217
  // Insert line below with `⎈↵`
218
- return that.select(end + toCount(lineAfter)).wrap('\n' + lineMatchIndent, "").insert(value).record(), false;
218
+ return of.select(end + toCount(lineAfter)).wrap('\n' + lineMatchIndent, "").insert(value).record(), false;
219
219
  }
220
220
  }
221
221
  return true;
222
222
  }
223
223
 
224
- export function canKeyDownHistory(map, that) {
224
+ export function canKeyDownHistory(map, of) {
225
225
  let keyValue = map + "";
226
226
  // Redo with `⎈y`
227
227
  if (CTRL_PREFIX + 'y' === keyValue) {
228
- return that.redo(), false;
228
+ return of.redo(), false;
229
229
  }
230
230
  // Undo with `⎈z`
231
231
  if (CTRL_PREFIX + 'z' === keyValue) {
232
- return that.undo(), false;
232
+ return of.undo(), false;
233
233
  }
234
234
  return true;
235
235
  }
236
236
 
237
- export function canKeyDownMove(map, that) {
237
+ export function canKeyDownMove(map, of) {
238
238
  let {key, queue} = map,
239
239
  keyValue = map + "";
240
240
  if (!queue.Control) {
241
241
  return true;
242
242
  }
243
- let {after, before, end, start, value} = that.$(),
243
+ let {after, before, end, start, value} = of.$(),
244
244
  charPair, charPairValue,
245
- charPairs = that.state.source.pairs || {},
245
+ charPairs = of.state.source.pairs || {},
246
246
  boundaries = [], m;
247
247
  if (value) {
248
248
  for (charPair in charPairs) {
@@ -256,17 +256,17 @@ export function canKeyDownMove(map, that) {
256
256
  boundaries.push('[\\s\\S]'); // Last try!
257
257
  if (CTRL_PREFIX + 'ArrowLeft' === keyValue) {
258
258
  if (m = before.match(toPattern('(' + boundaries.join('|') + ')$', ""))) {
259
- that.insert("").select(start - toCount(m[0])).insert(value);
260
- return that.record(), false;
259
+ of.insert("").select(start - toCount(m[0])).insert(value);
260
+ return of.record(), false;
261
261
  }
262
- return that.select(), false;
262
+ return of.select(), false;
263
263
  }
264
264
  if (CTRL_PREFIX + 'ArrowRight' === keyValue) {
265
265
  if (m = after.match(toPattern('^(' + boundaries.join('|') + ')', ""))) {
266
- that.insert("").select(end + toCount(m[0]) - toCount(value)).insert(value);
267
- return that.record(), false;
266
+ of.insert("").select(end + toCount(m[0]) - toCount(value)).insert(value);
267
+ return of.record(), false;
268
268
  }
269
- return that.select(), false;
269
+ return of.select(), false;
270
270
  }
271
271
  }
272
272
  let lineAfter = after.split('\n').shift(),
@@ -279,56 +279,56 @@ export function canKeyDownMove(map, that) {
279
279
  value = lineBefore + value + lineAfter;
280
280
  if (CTRL_PREFIX + 'ArrowUp' === keyValue) {
281
281
  if (!hasValue('\n', before)) {
282
- return that.select(), false;
282
+ return of.select(), false;
283
283
  }
284
- that.insert("");
285
- that.replace(/^([^\n]*?)(\n|$)/, '$2', 1);
286
- that.replace(/(^|\n)([^\n]*?)$/, "", -1);
287
- let $ = that.$();
284
+ of.insert("");
285
+ of.replace(/^([^\n]*?)(\n|$)/, '$2', 1);
286
+ of.replace(/(^|\n)([^\n]*?)$/, "", -1);
287
+ let $ = of.$();
288
288
  before = $.before;
289
289
  start = $.start;
290
290
  lineBefore = before.split('\n').pop();
291
- that.select(start = start - toCount(lineBefore)).wrap(value, '\n');
292
- that.select(start, start + toCount(value));
293
- return that.record(), false;
291
+ of.select(start = start - toCount(lineBefore)).wrap(value, '\n');
292
+ of.select(start, start + toCount(value));
293
+ return of.record(), false;
294
294
  }
295
295
  if (CTRL_PREFIX + 'ArrowDown' === keyValue) {
296
296
  if (!hasValue('\n', after)) {
297
- return that.select(), false;
297
+ return of.select(), false;
298
298
  }
299
- that.insert("");
300
- that.replace(/^([^\n]*?)(\n|$)/, "", 1);
301
- that.replace(/(^|\n)([^\n]*?)$/, '$1', -1);
302
- let $ = that.$();
299
+ of.insert("");
300
+ of.replace(/^([^\n]*?)(\n|$)/, "", 1);
301
+ of.replace(/(^|\n)([^\n]*?)$/, '$1', -1);
302
+ let $ = of.$();
303
303
  after = $.after;
304
304
  end = $.end;
305
305
  lineAfter = after.split('\n').shift();
306
- that.select(end = end + toCount(lineAfter)).wrap('\n', value);
306
+ of.select(end = end + toCount(lineAfter)).wrap('\n', value);
307
307
  end += 1;
308
- that.select(end, end + toCount(value));
309
- return that.record(), false;
308
+ of.select(end, end + toCount(value));
309
+ return of.record(), false;
310
310
  }
311
311
  return true;
312
312
  }
313
313
 
314
- export function canKeyDownTab(map, that) {
315
- let charIndent = that.state.source.tab || that.state.tab || '\t',
314
+ export function canKeyDownTab(map, of) {
315
+ let charIndent = of.state.source.tab || of.state.tab || '\t',
316
316
  keyValue = map + "";
317
317
  // Indent with `⇥`
318
318
  if ('Tab' === keyValue) {
319
- return that.push(charIndent).record(), false;
319
+ return of.push(charIndent).record(), false;
320
320
  }
321
321
  // Outdent with `⇧+⇥`
322
322
  if (SHIFT_PREFIX + 'Tab' === keyValue) {
323
- return that.pull(charIndent).record(), false;
323
+ return of.pull(charIndent).record(), false;
324
324
  }
325
325
  return true;
326
326
  }
327
327
 
328
- let bounce = debounce(that => that.record(), 100);
328
+ let bounce = debounce(of => of.record(), 100);
329
329
 
330
- export function canKeyUp(map, that) {
331
- return bounce(that), true;
330
+ export function canKeyUp(map, of) {
331
+ return bounce(of), true;
332
332
  }
333
333
 
334
334
  export const state = defaults;
package/package.json CHANGED
@@ -49,5 +49,5 @@
49
49
  "scripts": {
50
50
  "pack": "pack --clean=false --from=.github/factory --js-format=umd --js-name=TE.Source --js-top='%(js.license)' --mjs=true --to=."
51
51
  },
52
- "version": "2.2.8"
52
+ "version": "2.2.10"
53
53
  }