@taufik-nurrohman/text-editor.source 3.0.2 → 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 +85 -114
- package/index.min.js +1 -1
- package/index.mjs +49 -80
- 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,13 +134,19 @@
|
|
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);
|
146
147
|
var name = 'TextEditor.Source';
|
147
148
|
|
148
149
|
function onKeyDown(e) {
|
149
|
-
var _$$state$source, _$$state$source2;
|
150
150
|
var $ = this,
|
151
151
|
key = $.k(false).pop(),
|
152
152
|
// Capture the last key
|
@@ -157,8 +157,8 @@
|
|
157
157
|
}
|
158
158
|
var charAfter,
|
159
159
|
charBefore,
|
160
|
-
charIndent =
|
161
|
-
charPairs =
|
160
|
+
charIndent = $.state.tab || '\t',
|
161
|
+
charPairs = $.state.pairs || {},
|
162
162
|
charPairsValues = toObjectValues(charPairs);
|
163
163
|
if (isInteger(charIndent)) {
|
164
164
|
charIndent = ' '.repeat(charIndent);
|
@@ -173,7 +173,7 @@
|
|
173
173
|
lineBefore = before.split('\n').pop(),
|
174
174
|
lineMatch = /^\s+/.exec(lineBefore),
|
175
175
|
lineMatchIndent = lineMatch && lineMatch[0] || "";
|
176
|
-
if (CTRL_PREFIX + SHIFT_PREFIX +
|
176
|
+
if (CTRL_PREFIX + SHIFT_PREFIX + KEY_ENTER === keys) {
|
177
177
|
if (before || after) {
|
178
178
|
// Insert line above with `⎈⇧↵`
|
179
179
|
offEventDefault(e);
|
@@ -181,7 +181,7 @@
|
|
181
181
|
}
|
182
182
|
return;
|
183
183
|
}
|
184
|
-
if (CTRL_PREFIX +
|
184
|
+
if (CTRL_PREFIX + KEY_ENTER === keys) {
|
185
185
|
if (before || after) {
|
186
186
|
// Insert line below with `⎈↵`
|
187
187
|
offEventDefault(e);
|
@@ -201,7 +201,7 @@
|
|
201
201
|
}
|
202
202
|
return;
|
203
203
|
}
|
204
|
-
if (
|
204
|
+
if (KEY_DELETE_LEFT === keys || KEY_DELETE_RIGHT === keys) {
|
205
205
|
charAfter = charPairs[charBefore = before.slice(-1)];
|
206
206
|
// Do nothing on escape
|
207
207
|
if ('\\' === charBefore) {
|
@@ -223,7 +223,7 @@
|
|
223
223
|
}
|
224
224
|
}
|
225
225
|
// Outdent
|
226
|
-
if (
|
226
|
+
if (KEY_DELETE_RIGHT !== keys && lineBefore.endsWith(charIndent)) {
|
227
227
|
offEventDefault(e);
|
228
228
|
return $.pull(charIndent).record();
|
229
229
|
}
|
@@ -236,7 +236,7 @@
|
|
236
236
|
}
|
237
237
|
return;
|
238
238
|
}
|
239
|
-
if (
|
239
|
+
if (KEY_ENTER === keys || SHIFT_PREFIX + KEY_ENTER === keys) {
|
240
240
|
if (!value) {
|
241
241
|
if (after && before && (charAfter = charPairs[charBefore = before.slice(-1)]) && charAfter === after[0]) {
|
242
242
|
offEventDefault(e);
|
@@ -295,14 +295,14 @@
|
|
295
295
|
tokens.push('\\w+'); // Word(s)
|
296
296
|
tokens.push('\\s+'); // White-space(s)
|
297
297
|
tokens.push('[\\s\\S]'); // Last try!
|
298
|
-
if (CTRL_PREFIX +
|
298
|
+
if (CTRL_PREFIX + KEY_ARROW_LEFT === keys) {
|
299
299
|
offEventDefault(e);
|
300
300
|
if (m = toPattern('(' + tokens.join('|') + ')$', "").exec(before)) {
|
301
301
|
return $.insert("").select(start - toCount(m[0])).insert(value).record();
|
302
302
|
}
|
303
303
|
return $.select();
|
304
304
|
}
|
305
|
-
if (CTRL_PREFIX +
|
305
|
+
if (CTRL_PREFIX + KEY_ARROW_RIGHT === keys) {
|
306
306
|
offEventDefault(e);
|
307
307
|
if (m = after.match(toPattern('^(' + tokens.join('|') + ')', ""))) {
|
308
308
|
return $.insert("").select(end + toCount(m[0]) - toCount(value)).insert(value).record();
|
@@ -314,7 +314,7 @@
|
|
314
314
|
end += toCount(lineAfter);
|
315
315
|
start -= toCount(lineBefore);
|
316
316
|
value = lineBefore + value + lineAfter;
|
317
|
-
if (CTRL_PREFIX +
|
317
|
+
if (CTRL_PREFIX + KEY_ARROW_UP === keys) {
|
318
318
|
offEventDefault(e);
|
319
319
|
if (!hasValue('\n', before)) {
|
320
320
|
return $.select();
|
@@ -330,7 +330,7 @@
|
|
330
330
|
$.select(start, start + toCount(value));
|
331
331
|
return $.record();
|
332
332
|
}
|
333
|
-
if (CTRL_PREFIX +
|
333
|
+
if (CTRL_PREFIX + KEY_ARROW_DOWN === keys) {
|
334
334
|
offEventDefault(e);
|
335
335
|
if (!hasValue('\n', after)) {
|
336
336
|
return $.select();
|
@@ -352,18 +352,16 @@
|
|
352
352
|
|
353
353
|
function attach() {
|
354
354
|
var $ = this;
|
355
|
-
var $$ = $.constructor.
|
355
|
+
var $$ = $.constructor._;
|
356
356
|
$.state = fromStates({
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
'<': '>'
|
366
|
-
}
|
357
|
+
pairs: {
|
358
|
+
'`': '`',
|
359
|
+
'(': ')',
|
360
|
+
'{': '}',
|
361
|
+
'[': ']',
|
362
|
+
'"': '"',
|
363
|
+
"'": "'",
|
364
|
+
'<': '>'
|
367
365
|
}
|
368
366
|
}, $.state);
|
369
367
|
!isFunction($$.alert) && ($$.alert = function (hint, then) {
|
@@ -373,9 +371,9 @@
|
|
373
371
|
!isFunction($$.confirm) && ($$.confirm = function (hint, then) {
|
374
372
|
return isFunction(then) && then.call(this, W.confirm && W.confirm(hint));
|
375
373
|
});
|
376
|
-
!isFunction($$.
|
377
|
-
var $ = this
|
378
|
-
|
374
|
+
!isFunction($$.insertLine) && ($$.insertLine = function (value, mode) {
|
375
|
+
var $ = this,
|
376
|
+
_$$$2 = $.$(),
|
379
377
|
after = _$$$2.after,
|
380
378
|
before = _$$$2.before,
|
381
379
|
end = _$$$2.end,
|
@@ -394,69 +392,45 @@
|
|
394
392
|
}
|
395
393
|
return $.select(start - lineBeforeCount, end + lineAfterCount).insert(value, mode, true).wrap(lineMatchIndent, "");
|
396
394
|
});
|
397
|
-
!isFunction($$.
|
398
|
-
|
399
|
-
|
400
|
-
after = _$$$3.after,
|
401
|
-
before = _$$$3.before,
|
402
|
-
end = _$$$3.end,
|
403
|
-
start = _$$$3.start,
|
404
|
-
value = _$$$3.value,
|
405
|
-
closeCount = toCount(close),
|
406
|
-
lineAfter = after.split('\n').shift(),
|
407
|
-
lineAfterCount = toCount(lineAfter),
|
408
|
-
lineBefore = before.split('\n').pop(),
|
409
|
-
lineBeforeCount = toCount(lineBefore),
|
410
|
-
openCount = toCount(open);
|
411
|
-
if (wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount) || close === lineAfter.slice(-closeCount) && open === lineBefore.slice(0, openCount)) {
|
412
|
-
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;
|
413
398
|
}
|
414
|
-
return
|
399
|
+
return this.selectLine(withSpaces).peel(open, close, wrap);
|
415
400
|
});
|
416
401
|
!isFunction($$.prompt) && ($$.prompt = function (hint, value, then) {
|
417
402
|
return isFunction(then) && then.call(this, W.prompt ? W.prompt(hint, value) : false);
|
418
403
|
});
|
419
|
-
!isFunction($$.
|
404
|
+
!isFunction($$.selectLine) && ($$.selectLine = function (withSpaces) {
|
420
405
|
if (withSpaces === void 0) {
|
421
406
|
withSpaces = true;
|
422
407
|
}
|
423
|
-
var $ = this
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
408
|
+
var $ = this,
|
409
|
+
m,
|
410
|
+
_$$$3 = $.$(),
|
411
|
+
after = _$$$3.after,
|
412
|
+
before = _$$$3.before,
|
413
|
+
end = _$$$3.end,
|
414
|
+
start = _$$$3.start,
|
430
415
|
lineAfter = after.split('\n').shift(),
|
431
416
|
lineAfterCount = toCount(lineAfter),
|
432
417
|
lineBefore = before.split('\n').pop(),
|
433
418
|
lineBeforeCount = toCount(lineBefore);
|
434
|
-
if (!withSpaces) {
|
435
|
-
var lineAfterSpaces = /\s+$/.exec(lineAfter),
|
436
|
-
lineBeforeSpaces = /^\s+/.exec(lineBefore);
|
437
|
-
if (lineAfterSpaces) {
|
438
|
-
lineAfterCount -= toCount(lineAfterSpaces[0]);
|
439
|
-
}
|
440
|
-
if (lineBeforeSpaces) {
|
441
|
-
lineBeforeCount -= toCount(lineBeforeSpaces[0]);
|
442
|
-
}
|
443
|
-
}
|
444
419
|
$.select(start - lineBeforeCount, end + lineAfterCount);
|
445
420
|
if (!withSpaces) {
|
446
|
-
var
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
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] || ""));
|
453
427
|
}
|
454
428
|
}
|
455
429
|
return $;
|
456
430
|
});
|
457
431
|
!isFunction($$.toggle) && ($$.toggle = function (open, close, wrap) {
|
458
|
-
var $ = this
|
459
|
-
|
432
|
+
var $ = this,
|
433
|
+
_$$$5 = $.$(),
|
460
434
|
after = _$$$5.after,
|
461
435
|
before = _$$$5.before,
|
462
436
|
value = _$$$5.value,
|
@@ -467,33 +441,30 @@
|
|
467
441
|
}
|
468
442
|
return $.wrap(open, close, wrap);
|
469
443
|
});
|
470
|
-
!isFunction($$.
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
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,
|
476
453
|
closeCount = toCount(close),
|
477
|
-
lineAfter = after.split('\n').shift(),
|
478
|
-
lineBefore = before.split('\n').pop(),
|
479
454
|
openCount = toCount(open);
|
480
|
-
if (wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount)
|
481
|
-
|
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);
|
482
460
|
}
|
483
|
-
return $.
|
461
|
+
return $.toggle(open, close, wrap);
|
484
462
|
});
|
485
|
-
!isFunction($$.
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
end = _$$$7.end,
|
491
|
-
start = _$$$7.start,
|
492
|
-
lineAfter = after.split('\n').shift(),
|
493
|
-
lineAfterCount = toCount(lineAfter),
|
494
|
-
lineBefore = before.split('\n').pop(),
|
495
|
-
lineBeforeCount = toCount(lineBefore);
|
496
|
-
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);
|
497
468
|
});
|
498
469
|
return $.on('key.down', onKeyDown).record();
|
499
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
|
|
@@ -24,8 +32,8 @@ function onKeyDown(e) {
|
|
24
32
|
}
|
25
33
|
let charAfter,
|
26
34
|
charBefore,
|
27
|
-
charIndent = $.state.
|
28
|
-
charPairs = $.state.
|
35
|
+
charIndent = $.state.tab || '\t',
|
36
|
+
charPairs = $.state.pairs || {},
|
29
37
|
charPairsValues = toObjectValues(charPairs);
|
30
38
|
if (isInteger(charIndent)) {
|
31
39
|
charIndent = ' '.repeat(charIndent);
|
@@ -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,18 +224,16 @@ function onKeyDown(e) {
|
|
216
224
|
|
217
225
|
function attach() {
|
218
226
|
const $ = this;
|
219
|
-
const $$ = $.constructor.
|
227
|
+
const $$ = $.constructor._;
|
220
228
|
$.state = fromStates({
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
'<': '>'
|
230
|
-
}
|
229
|
+
pairs: {
|
230
|
+
'`': '`',
|
231
|
+
'(': ')',
|
232
|
+
'{': '}',
|
233
|
+
'[': ']',
|
234
|
+
'"': '"',
|
235
|
+
"'": "'",
|
236
|
+
'<': '>'
|
231
237
|
}
|
232
238
|
}, $.state);
|
233
239
|
!isFunction($$.alert) && ($$.alert = function (hint, then) {
|
@@ -237,9 +243,9 @@ function attach() {
|
|
237
243
|
!isFunction($$.confirm) && ($$.confirm = function (hint, then) {
|
238
244
|
return isFunction(then) && then.call(this, W.confirm && W.confirm(hint));
|
239
245
|
});
|
240
|
-
!isFunction($$.
|
241
|
-
let $ = this
|
242
|
-
|
246
|
+
!isFunction($$.insertLine) && ($$.insertLine = function (value, mode) {
|
247
|
+
let $ = this,
|
248
|
+
{after, before, end, start} = $.$(),
|
243
249
|
lineAfter = after.split('\n').shift(),
|
244
250
|
lineAfterCount = toCount(lineAfter),
|
245
251
|
lineBefore = before.split('\n').pop(),
|
@@ -254,58 +260,31 @@ function attach() {
|
|
254
260
|
}
|
255
261
|
return $.select(start - lineBeforeCount, end + lineAfterCount).insert(value, mode, true).wrap(lineMatchIndent, "");
|
256
262
|
});
|
257
|
-
!isFunction($$.
|
258
|
-
|
259
|
-
let {after, before, end, start, value} = $.$(),
|
260
|
-
closeCount = toCount(close),
|
261
|
-
lineAfter = after.split('\n').shift(),
|
262
|
-
lineAfterCount = toCount(lineAfter),
|
263
|
-
lineBefore = before.split('\n').pop(),
|
264
|
-
lineBeforeCount = toCount(lineBefore),
|
265
|
-
openCount = toCount(open);
|
266
|
-
if (
|
267
|
-
(wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount)) ||
|
268
|
-
(close === lineAfter.slice(-closeCount) && open === lineBefore.slice(0, openCount))
|
269
|
-
) {
|
270
|
-
return $.select(start - lineBeforeCount + (wrap ? 0 : openCount), end + lineAfterCount - (wrap ? 0 : closeCount)).peel(open, close, wrap);
|
271
|
-
}
|
272
|
-
return $.select(start, end);
|
263
|
+
!isFunction($$.peelLine) && ($$.peelLine = function (open, close, wrap, withSpaces = false) {
|
264
|
+
return this.selectLine(withSpaces).peel(open, close, wrap);
|
273
265
|
});
|
274
266
|
!isFunction($$.prompt) && ($$.prompt = function (hint, value, then) {
|
275
267
|
return isFunction(then) && then.call(this, W.prompt ? W.prompt(hint, value) : false);
|
276
268
|
});
|
277
|
-
!isFunction($$.
|
278
|
-
let $ = this
|
279
|
-
|
269
|
+
!isFunction($$.selectLine) && ($$.selectLine = function (withSpaces = true) {
|
270
|
+
let $ = this, m,
|
271
|
+
{after, before, end, start} = $.$(),
|
280
272
|
lineAfter = after.split('\n').shift(),
|
281
273
|
lineAfterCount = toCount(lineAfter),
|
282
274
|
lineBefore = before.split('\n').pop(),
|
283
275
|
lineBeforeCount = toCount(lineBefore);
|
284
|
-
if (!withSpaces) {
|
285
|
-
let lineAfterSpaces = /\s+$/.exec(lineAfter),
|
286
|
-
lineBeforeSpaces = /^\s+/.exec(lineBefore);
|
287
|
-
if (lineAfterSpaces) {
|
288
|
-
lineAfterCount -= toCount(lineAfterSpaces[0]);
|
289
|
-
}
|
290
|
-
if (lineBeforeSpaces) {
|
291
|
-
lineBeforeCount -= toCount(lineBeforeSpaces[0]);
|
292
|
-
}
|
293
|
-
}
|
294
276
|
$.select(start - lineBeforeCount, end + lineAfterCount);
|
295
277
|
if (!withSpaces) {
|
296
|
-
let
|
297
|
-
|
298
|
-
start = s.start;
|
299
|
-
value = s.value;
|
300
|
-
if (m = /^(\s+)?[\s\S]+?(\s+)?$/.exec(value)) {
|
278
|
+
let {end, start, value} = $.$();
|
279
|
+
if (m = /^(\s+)?[\s\S]*?(\s+)?$/.exec(value)) {
|
301
280
|
return $.select(start + toCount(m[1] || ""), end - toCount(m[2] || ""));
|
302
281
|
}
|
303
282
|
}
|
304
283
|
return $;
|
305
284
|
});
|
306
285
|
!isFunction($$.toggle) && ($$.toggle = function (open, close, wrap) {
|
307
|
-
let $ = this
|
308
|
-
|
286
|
+
let $ = this,
|
287
|
+
{after, before, value} = $.$(),
|
309
288
|
closeCount = toCount(close),
|
310
289
|
openCount = toCount(open);
|
311
290
|
if (
|
@@ -316,29 +295,19 @@ function attach() {
|
|
316
295
|
}
|
317
296
|
return $.wrap(open, close, wrap);
|
318
297
|
});
|
319
|
-
!isFunction($$.
|
320
|
-
let $ = this
|
321
|
-
|
298
|
+
!isFunction($$.toggleLine) && ($$.toggleLine = function (open, close, wrap, withSpaces = false) {
|
299
|
+
let $ = this.selectLine(withSpaces),
|
300
|
+
{after, before, value} = $.$(),
|
322
301
|
closeCount = toCount(close),
|
323
|
-
lineAfter = after.split('\n').shift(),
|
324
|
-
lineBefore = before.split('\n').pop(),
|
325
302
|
openCount = toCount(open);
|
326
|
-
if (
|
327
|
-
|
328
|
-
(
|
329
|
-
) {
|
330
|
-
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);
|
331
306
|
}
|
332
|
-
return $.
|
307
|
+
return $.toggle(open, close, wrap);
|
333
308
|
});
|
334
|
-
!isFunction($$.
|
335
|
-
|
336
|
-
let {after, before, end, start} = $.$(),
|
337
|
-
lineAfter = after.split('\n').shift(),
|
338
|
-
lineAfterCount = toCount(lineAfter),
|
339
|
-
lineBefore = before.split('\n').pop(),
|
340
|
-
lineBeforeCount = toCount(lineBefore);
|
341
|
-
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);
|
342
311
|
});
|
343
312
|
return $.on('key.down', onKeyDown).record();
|
344
313
|
}
|
package/package.json
CHANGED