@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 +87 -87
- package/index.min.js +1 -1
- package/index.mjs +61 -61
- package/package.json +1 -1
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
|
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
|
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
|
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,
|
144
|
+
function canKeyDown(map, of) {
|
145
145
|
var charAfter,
|
146
146
|
charBefore,
|
147
|
-
charIndent =
|
148
|
-
charPairs =
|
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
|
159
|
-
_after =
|
160
|
-
_before =
|
161
|
-
_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
|
-
|
164
|
+
of.wrap(' ', ' ');
|
165
165
|
return false;
|
166
166
|
}
|
167
167
|
return true;
|
168
168
|
}
|
169
169
|
if ('Enter' === keyValue) {
|
170
|
-
var
|
171
|
-
_after2 =
|
172
|
-
_before2 =
|
173
|
-
_value2 =
|
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
|
-
|
179
|
+
of.wrap('\n' + lineMatchIndent + (charBefore !== charAfter ? charIndent : ""), '\n' + lineMatchIndent).record();
|
180
180
|
return false;
|
181
181
|
}
|
182
182
|
if (lineMatchIndent) {
|
183
|
-
|
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
|
191
|
-
_after3 =
|
192
|
-
_before3 =
|
193
|
-
_value3 =
|
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
|
-
|
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
|
-
|
214
|
+
of.trim("", "").record();
|
215
215
|
return false;
|
216
216
|
}
|
217
217
|
}
|
218
218
|
// Outdent
|
219
219
|
if (_lineBefore.endsWith(charIndent)) {
|
220
|
-
|
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
|
-
|
226
|
+
of.peel(charBefore, charAfter).record();
|
227
227
|
return false;
|
228
228
|
}
|
229
229
|
}
|
230
230
|
return true;
|
231
231
|
}
|
232
|
-
var
|
233
|
-
after =
|
234
|
-
before =
|
235
|
-
start =
|
236
|
-
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
|
-
|
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
|
-
|
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
|
-
|
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,
|
273
|
-
var charIndent =
|
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
|
-
|
279
|
+
of.push(charIndent).record();
|
280
280
|
return false;
|
281
281
|
}
|
282
282
|
// Outdent with `⎈[`
|
283
283
|
if (CTRL_PREFIX + '[' === keyValue) {
|
284
|
-
|
284
|
+
of.pull(charIndent).record();
|
285
285
|
return false;
|
286
286
|
}
|
287
287
|
return true;
|
288
288
|
}
|
289
289
|
|
290
|
-
function canKeyDownEnter(map,
|
290
|
+
function canKeyDownEnter(map, of) {
|
291
291
|
map.key;
|
292
292
|
var queue = map.queue;
|
293
293
|
if (queue.Control && queue.Enter) {
|
294
|
-
var
|
295
|
-
after =
|
296
|
-
before =
|
297
|
-
end =
|
298
|
-
start =
|
299
|
-
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
|
307
|
+
return of.select(start - toCount(lineBefore)).wrap(lineMatchIndent, '\n').insert(value).record(), false;
|
308
308
|
}
|
309
309
|
// Insert line below with `⎈↵`
|
310
|
-
return
|
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,
|
316
|
+
function canKeyDownHistory(map, of) {
|
317
317
|
var keyValue = map + "";
|
318
318
|
// Redo with `⎈y`
|
319
319
|
if (CTRL_PREFIX + 'y' === keyValue) {
|
320
|
-
return
|
320
|
+
return of.redo(), false;
|
321
321
|
}
|
322
322
|
// Undo with `⎈z`
|
323
323
|
if (CTRL_PREFIX + 'z' === keyValue) {
|
324
|
-
return
|
324
|
+
return of.undo(), false;
|
325
325
|
}
|
326
326
|
return true;
|
327
327
|
}
|
328
328
|
|
329
|
-
function canKeyDownMove(map,
|
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
|
337
|
-
after =
|
338
|
-
before =
|
339
|
-
end =
|
340
|
-
start =
|
341
|
-
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 =
|
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
|
-
|
360
|
-
return
|
359
|
+
of.insert("").select(start - toCount(m[0])).insert(value);
|
360
|
+
return of.record(), false;
|
361
361
|
}
|
362
|
-
return
|
362
|
+
return of.select(), false;
|
363
363
|
}
|
364
364
|
if (CTRL_PREFIX + 'ArrowRight' === keyValue) {
|
365
365
|
if (m = after.match(toPattern('^(' + boundaries.join('|') + ')', ""))) {
|
366
|
-
|
367
|
-
return
|
366
|
+
of.insert("").select(end + toCount(m[0]) - toCount(value)).insert(value);
|
367
|
+
return of.record(), false;
|
368
368
|
}
|
369
|
-
return
|
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
|
382
|
+
return of.select(), false;
|
383
383
|
}
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
var $ =
|
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
|
-
|
392
|
-
|
393
|
-
return
|
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
|
397
|
+
return of.select(), false;
|
398
398
|
}
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
var _$ =
|
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
|
-
|
406
|
+
of.select(end = end + toCount(lineAfter)).wrap('\n', value);
|
407
407
|
end += 1;
|
408
|
-
|
409
|
-
return
|
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,
|
415
|
-
var charIndent =
|
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
|
419
|
+
return of.push(charIndent).record(), false;
|
420
420
|
}
|
421
421
|
// Outdent with `⇧+⇥`
|
422
422
|
if (SHIFT_PREFIX + 'Tab' === keyValue) {
|
423
|
-
return
|
423
|
+
return of.pull(charIndent).record(), false;
|
424
424
|
}
|
425
425
|
return true;
|
426
426
|
}
|
427
|
-
var bounce = debounce(function (
|
428
|
-
return
|
427
|
+
var bounce = debounce(function (of) {
|
428
|
+
return of.record();
|
429
429
|
}, 100);
|
430
430
|
|
431
|
-
function canKeyUp(map,
|
432
|
-
return bounce(
|
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
|
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,
|
70
|
+
export function canKeyDown(map, of) {
|
71
71
|
let charAfter,
|
72
72
|
charBefore,
|
73
|
-
charIndent =
|
74
|
-
charPairs =
|
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} =
|
83
|
+
let {after, before, value} = of.$();
|
84
84
|
charAfter = charPairs[charBefore = before.slice(-1)];
|
85
85
|
if (!value && charAfter && charBefore && charAfter === after[0]) {
|
86
|
-
|
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} =
|
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
|
-
|
98
|
+
of.wrap('\n' + lineMatchIndent + (charBefore !== charAfter ? charIndent : ""), '\n' + lineMatchIndent).record();
|
99
99
|
return false;
|
100
100
|
}
|
101
101
|
if (lineMatchIndent) {
|
102
|
-
|
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} =
|
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
|
-
|
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
|
-
|
133
|
+
of.trim("", "").record();
|
134
134
|
return false;
|
135
135
|
}
|
136
136
|
}
|
137
137
|
// Outdent
|
138
138
|
if (lineBefore.endsWith(charIndent)) {
|
139
|
-
|
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
|
-
|
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} =
|
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
|
-
|
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
|
-
|
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
|
-
|
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,
|
188
|
-
let charIndent =
|
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
|
-
|
193
|
+
of.push(charIndent).record();
|
194
194
|
return false;
|
195
195
|
}
|
196
196
|
// Outdent with `⎈[`
|
197
197
|
if (CTRL_PREFIX + '[' === keyValue) {
|
198
|
-
|
198
|
+
of.pull(charIndent).record();
|
199
199
|
return false;
|
200
200
|
}
|
201
201
|
return true;
|
202
202
|
}
|
203
203
|
|
204
|
-
export function canKeyDownEnter(map,
|
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} =
|
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
|
215
|
+
return of.select(start - toCount(lineBefore)).wrap(lineMatchIndent, '\n').insert(value).record(), false;
|
216
216
|
}
|
217
217
|
// Insert line below with `⎈↵`
|
218
|
-
return
|
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,
|
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
|
228
|
+
return of.redo(), false;
|
229
229
|
}
|
230
230
|
// Undo with `⎈z`
|
231
231
|
if (CTRL_PREFIX + 'z' === keyValue) {
|
232
|
-
return
|
232
|
+
return of.undo(), false;
|
233
233
|
}
|
234
234
|
return true;
|
235
235
|
}
|
236
236
|
|
237
|
-
export function canKeyDownMove(map,
|
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} =
|
243
|
+
let {after, before, end, start, value} = of.$(),
|
244
244
|
charPair, charPairValue,
|
245
|
-
charPairs =
|
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
|
-
|
260
|
-
return
|
259
|
+
of.insert("").select(start - toCount(m[0])).insert(value);
|
260
|
+
return of.record(), false;
|
261
261
|
}
|
262
|
-
return
|
262
|
+
return of.select(), false;
|
263
263
|
}
|
264
264
|
if (CTRL_PREFIX + 'ArrowRight' === keyValue) {
|
265
265
|
if (m = after.match(toPattern('^(' + boundaries.join('|') + ')', ""))) {
|
266
|
-
|
267
|
-
return
|
266
|
+
of.insert("").select(end + toCount(m[0]) - toCount(value)).insert(value);
|
267
|
+
return of.record(), false;
|
268
268
|
}
|
269
|
-
return
|
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
|
282
|
+
return of.select(), false;
|
283
283
|
}
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
let $ =
|
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
|
-
|
292
|
-
|
293
|
-
return
|
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
|
297
|
+
return of.select(), false;
|
298
298
|
}
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
let $ =
|
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
|
-
|
306
|
+
of.select(end = end + toCount(lineAfter)).wrap('\n', value);
|
307
307
|
end += 1;
|
308
|
-
|
309
|
-
return
|
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,
|
315
|
-
let charIndent =
|
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
|
319
|
+
return of.push(charIndent).record(), false;
|
320
320
|
}
|
321
321
|
// Outdent with `⇧+⇥`
|
322
322
|
if (SHIFT_PREFIX + 'Tab' === keyValue) {
|
323
|
-
return
|
323
|
+
return of.pull(charIndent).record(), false;
|
324
324
|
}
|
325
325
|
return true;
|
326
326
|
}
|
327
327
|
|
328
|
-
let bounce = debounce(
|
328
|
+
let bounce = debounce(of => of.record(), 100);
|
329
329
|
|
330
|
-
export function canKeyUp(map,
|
331
|
-
return bounce(
|
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