@taufik-nurrohman/text-editor.source 3.0.3 → 3.0.4
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -2
- package/index.js +75 -101
- package/index.min.js +1 -1
- package/index.mjs +39 -68
- package/package.json +1 -1
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
|
-
|
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
|
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
|
43
|
-
|
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
|
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
|
67
|
+
return isPlain ? isInstance(x, Object, 1) : true;
|
62
68
|
};
|
63
|
-
var isSet
|
64
|
-
return isDefined
|
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
|
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 +
|
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 +
|
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 (
|
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 (
|
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 (
|
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 +
|
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 +
|
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 +
|
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 +
|
333
|
+
if (CTRL_PREFIX + KEY_ARROW_DOWN === keys) {
|
333
334
|
offEventDefault(e);
|
334
335
|
if (!hasValue('\n', after)) {
|
335
336
|
return $.select();
|
@@ -351,7 +352,7 @@
|
|
351
352
|
|
352
353
|
function attach() {
|
353
354
|
var $ = this;
|
354
|
-
var $$ = $.constructor.
|
355
|
+
var $$ = $.constructor._;
|
355
356
|
$.state = fromStates({
|
356
357
|
pairs: {
|
357
358
|
'`': '`',
|
@@ -370,9 +371,9 @@
|
|
370
371
|
!isFunction($$.confirm) && ($$.confirm = function (hint, then) {
|
371
372
|
return isFunction(then) && then.call(this, W.confirm && W.confirm(hint));
|
372
373
|
});
|
373
|
-
!isFunction($$.
|
374
|
-
var $ = this
|
375
|
-
|
374
|
+
!isFunction($$.insertLine) && ($$.insertLine = function (value, mode) {
|
375
|
+
var $ = this,
|
376
|
+
_$$$2 = $.$(),
|
376
377
|
after = _$$$2.after,
|
377
378
|
before = _$$$2.before,
|
378
379
|
end = _$$$2.end,
|
@@ -391,69 +392,45 @@
|
|
391
392
|
}
|
392
393
|
return $.select(start - lineBeforeCount, end + lineAfterCount).insert(value, mode, true).wrap(lineMatchIndent, "");
|
393
394
|
});
|
394
|
-
!isFunction($$.
|
395
|
-
|
396
|
-
|
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);
|
395
|
+
!isFunction($$.peelLine) && ($$.peelLine = function (open, close, wrap, withSpaces) {
|
396
|
+
if (withSpaces === void 0) {
|
397
|
+
withSpaces = false;
|
410
398
|
}
|
411
|
-
return
|
399
|
+
return this.selectLine(withSpaces).peel(open, close, wrap);
|
412
400
|
});
|
413
401
|
!isFunction($$.prompt) && ($$.prompt = function (hint, value, then) {
|
414
402
|
return isFunction(then) && then.call(this, W.prompt ? W.prompt(hint, value) : false);
|
415
403
|
});
|
416
|
-
!isFunction($$.
|
404
|
+
!isFunction($$.selectLine) && ($$.selectLine = function (withSpaces) {
|
417
405
|
if (withSpaces === void 0) {
|
418
406
|
withSpaces = true;
|
419
407
|
}
|
420
|
-
var $ = this
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
408
|
+
var $ = this,
|
409
|
+
m,
|
410
|
+
_$$$3 = $.$(),
|
411
|
+
after = _$$$3.after,
|
412
|
+
before = _$$$3.before,
|
413
|
+
end = _$$$3.end,
|
414
|
+
start = _$$$3.start,
|
427
415
|
lineAfter = after.split('\n').shift(),
|
428
416
|
lineAfterCount = toCount(lineAfter),
|
429
417
|
lineBefore = before.split('\n').pop(),
|
430
418
|
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
419
|
$.select(start - lineBeforeCount, end + lineAfterCount);
|
442
420
|
if (!withSpaces) {
|
443
|
-
var
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
return $.select(start + toCount(m[1] || ""), end - toCount(m[2] || ""));
|
421
|
+
var _$$$4 = $.$(),
|
422
|
+
_end = _$$$4.end,
|
423
|
+
_start = _$$$4.start,
|
424
|
+
value = _$$$4.value;
|
425
|
+
if (m = /^(\s+)?[\s\S]*?(\s+)?$/.exec(value)) {
|
426
|
+
return $.select(_start + toCount(m[1] || ""), _end - toCount(m[2] || ""));
|
450
427
|
}
|
451
428
|
}
|
452
429
|
return $;
|
453
430
|
});
|
454
431
|
!isFunction($$.toggle) && ($$.toggle = function (open, close, wrap) {
|
455
|
-
var $ = this
|
456
|
-
|
432
|
+
var $ = this,
|
433
|
+
_$$$5 = $.$(),
|
457
434
|
after = _$$$5.after,
|
458
435
|
before = _$$$5.before,
|
459
436
|
value = _$$$5.value,
|
@@ -464,33 +441,30 @@
|
|
464
441
|
}
|
465
442
|
return $.wrap(open, close, wrap);
|
466
443
|
});
|
467
|
-
!isFunction($$.
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
444
|
+
!isFunction($$.toggleLine) && ($$.toggleLine = function (open, close, wrap, withSpaces) {
|
445
|
+
if (withSpaces === void 0) {
|
446
|
+
withSpaces = false;
|
447
|
+
}
|
448
|
+
var $ = this.selectLine(withSpaces),
|
449
|
+
_$$$6 = $.$();
|
450
|
+
_$$$6.after;
|
451
|
+
_$$$6.before;
|
452
|
+
var value = _$$$6.value,
|
473
453
|
closeCount = toCount(close),
|
474
|
-
lineAfter = after.split('\n').shift(),
|
475
|
-
lineBefore = before.split('\n').pop(),
|
476
454
|
openCount = toCount(open);
|
477
|
-
if (wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount)
|
478
|
-
|
455
|
+
if (!wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount)) {
|
456
|
+
var _$$$7 = $.$(),
|
457
|
+
end = _$$$7.end,
|
458
|
+
start = _$$$7.start;
|
459
|
+
$.select(start + openCount, end - closeCount);
|
479
460
|
}
|
480
|
-
return $.
|
461
|
+
return $.toggle(open, close, wrap);
|
481
462
|
});
|
482
|
-
!isFunction($$.
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
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);
|
463
|
+
!isFunction($$.wrapLine) && ($$.wrapLine = function (open, close, wrap, withSpaces) {
|
464
|
+
if (withSpaces === void 0) {
|
465
|
+
withSpaces = false;
|
466
|
+
}
|
467
|
+
return this.selectLine(withSpaces).wrap(open, close, wrap);
|
494
468
|
});
|
495
469
|
return $.on('key.down', onKeyDown).record();
|
496
470
|
}
|
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},
|
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)}}var y={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).record()},detach:function(){return this.off("key.down",b)},name:"TextEditor.Source"};return y}));
|
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 +
|
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 +
|
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 (
|
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 (
|
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 (
|
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 +
|
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 +
|
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 +
|
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 +
|
205
|
+
if (CTRL_PREFIX + KEY_ARROW_DOWN === keys) {
|
198
206
|
offEventDefault(e);
|
199
207
|
if (!hasValue('\n', after)) {
|
200
208
|
return $.select();
|
@@ -216,7 +224,7 @@ function onKeyDown(e) {
|
|
216
224
|
|
217
225
|
function attach() {
|
218
226
|
const $ = this;
|
219
|
-
const $$ = $.constructor.
|
227
|
+
const $$ = $.constructor._;
|
220
228
|
$.state = fromStates({
|
221
229
|
pairs: {
|
222
230
|
'`': '`',
|
@@ -235,9 +243,9 @@ function attach() {
|
|
235
243
|
!isFunction($$.confirm) && ($$.confirm = function (hint, then) {
|
236
244
|
return isFunction(then) && then.call(this, W.confirm && W.confirm(hint));
|
237
245
|
});
|
238
|
-
!isFunction($$.
|
239
|
-
let $ = this
|
240
|
-
|
246
|
+
!isFunction($$.insertLine) && ($$.insertLine = function (value, mode) {
|
247
|
+
let $ = this,
|
248
|
+
{after, before, end, start} = $.$(),
|
241
249
|
lineAfter = after.split('\n').shift(),
|
242
250
|
lineAfterCount = toCount(lineAfter),
|
243
251
|
lineBefore = before.split('\n').pop(),
|
@@ -252,58 +260,31 @@ function attach() {
|
|
252
260
|
}
|
253
261
|
return $.select(start - lineBeforeCount, end + lineAfterCount).insert(value, mode, true).wrap(lineMatchIndent, "");
|
254
262
|
});
|
255
|
-
!isFunction($$.
|
256
|
-
|
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);
|
263
|
+
!isFunction($$.peelLine) && ($$.peelLine = function (open, close, wrap, withSpaces = false) {
|
264
|
+
return this.selectLine(withSpaces).peel(open, close, wrap);
|
271
265
|
});
|
272
266
|
!isFunction($$.prompt) && ($$.prompt = function (hint, value, then) {
|
273
267
|
return isFunction(then) && then.call(this, W.prompt ? W.prompt(hint, value) : false);
|
274
268
|
});
|
275
|
-
!isFunction($$.
|
276
|
-
let $ = this
|
277
|
-
|
269
|
+
!isFunction($$.selectLine) && ($$.selectLine = function (withSpaces = true) {
|
270
|
+
let $ = this, m,
|
271
|
+
{after, before, end, start} = $.$(),
|
278
272
|
lineAfter = after.split('\n').shift(),
|
279
273
|
lineAfterCount = toCount(lineAfter),
|
280
274
|
lineBefore = before.split('\n').pop(),
|
281
275
|
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
276
|
$.select(start - lineBeforeCount, end + lineAfterCount);
|
293
277
|
if (!withSpaces) {
|
294
|
-
let
|
295
|
-
|
296
|
-
start = s.start;
|
297
|
-
value = s.value;
|
298
|
-
if (m = /^(\s+)?[\s\S]+?(\s+)?$/.exec(value)) {
|
278
|
+
let {end, start, value} = $.$();
|
279
|
+
if (m = /^(\s+)?[\s\S]*?(\s+)?$/.exec(value)) {
|
299
280
|
return $.select(start + toCount(m[1] || ""), end - toCount(m[2] || ""));
|
300
281
|
}
|
301
282
|
}
|
302
283
|
return $;
|
303
284
|
});
|
304
285
|
!isFunction($$.toggle) && ($$.toggle = function (open, close, wrap) {
|
305
|
-
let $ = this
|
306
|
-
|
286
|
+
let $ = this,
|
287
|
+
{after, before, value} = $.$(),
|
307
288
|
closeCount = toCount(close),
|
308
289
|
openCount = toCount(open);
|
309
290
|
if (
|
@@ -314,29 +295,19 @@ function attach() {
|
|
314
295
|
}
|
315
296
|
return $.wrap(open, close, wrap);
|
316
297
|
});
|
317
|
-
!isFunction($$.
|
318
|
-
let $ = this
|
319
|
-
|
298
|
+
!isFunction($$.toggleLine) && ($$.toggleLine = function (open, close, wrap, withSpaces = false) {
|
299
|
+
let $ = this.selectLine(withSpaces),
|
300
|
+
{after, before, value} = $.$(),
|
320
301
|
closeCount = toCount(close),
|
321
|
-
lineAfter = after.split('\n').shift(),
|
322
|
-
lineBefore = before.split('\n').pop(),
|
323
302
|
openCount = toCount(open);
|
324
|
-
if (
|
325
|
-
|
326
|
-
(
|
327
|
-
) {
|
328
|
-
return $.peelBlock(open, close, wrap);
|
303
|
+
if (!wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount)) {
|
304
|
+
let {end, start} = $.$();
|
305
|
+
$.select(start + openCount, end - closeCount);
|
329
306
|
}
|
330
|
-
return $.
|
307
|
+
return $.toggle(open, close, wrap);
|
331
308
|
});
|
332
|
-
!isFunction($$.
|
333
|
-
|
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);
|
309
|
+
!isFunction($$.wrapLine) && ($$.wrapLine = function (open, close, wrap, withSpaces = false) {
|
310
|
+
return this.selectLine(withSpaces).wrap(open, close, wrap);
|
340
311
|
});
|
341
312
|
return $.on('key.down', onKeyDown).record();
|
342
313
|
}
|
package/package.json
CHANGED