@taufik-nurrohman/text-editor.source 2.2.11 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  *
3
3
  * The MIT License (MIT)
4
4
  *
5
- * Copyright © 2023 Taufik Nurrohman <https://github.com/taufik-nurrohman>
5
+ * Copyright © 2024 Taufik Nurrohman <https://github.com/taufik-nurrohman>
6
6
  *
7
7
  * Permission is hereby granted, free of charge, to any person obtaining a copy
8
8
  * of this software and associated documentation files (the “Software”), to deal
@@ -24,8 +24,8 @@
24
24
  *
25
25
  */
26
26
  (function (g, f) {
27
- typeof exports === 'object' && typeof module !== 'undefined' ? f(exports) : typeof define === 'function' && define.amd ? define(['exports'], f) : (g = typeof globalThis !== 'undefined' ? globalThis : g || self, f((g.TE = g.TE || {}, g.TE.Source = {})));
28
- })(this, (function (exports) {
27
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = f() : typeof define === 'function' && define.amd ? define(f) : (g = typeof globalThis !== 'undefined' ? globalThis : g || self, (g.TextEditor = g.TextEditor || {}, g.TextEditor.Source = f()));
28
+ })(this, (function () {
29
29
  'use strict';
30
30
  var hasValue = function hasValue(x, data) {
31
31
  return -1 !== data.indexOf(x);
@@ -36,24 +36,71 @@
36
36
  var isDefined = function isDefined(x) {
37
37
  return 'undefined' !== typeof x;
38
38
  };
39
+ var isFunction = function isFunction(x) {
40
+ return 'function' === typeof x;
41
+ };
39
42
  var isInstance = function isInstance(x, of) {
40
43
  return x && isSet(of) && x instanceof of ;
41
44
  };
45
+ var isInteger = function isInteger(x) {
46
+ return isNumber(x) && 0 === x % 1;
47
+ };
42
48
  var isNull = function isNull(x) {
43
49
  return null === x;
44
50
  };
51
+ var isNumber = function isNumber(x) {
52
+ return 'number' === typeof x;
53
+ };
54
+ var isObject = function isObject(x, isPlain) {
55
+ if (isPlain === void 0) {
56
+ isPlain = true;
57
+ }
58
+ if ('object' !== typeof x) {
59
+ return false;
60
+ }
61
+ return isPlain ? isInstance(x, Object) : true;
62
+ };
45
63
  var isSet = function isSet(x) {
46
64
  return isDefined(x) && !isNull(x);
47
65
  };
48
- var isString = function isString(x) {
49
- return 'string' === typeof x;
50
- };
51
66
  var toCount = function toCount(x) {
52
67
  return x.length;
53
68
  };
54
69
  var toObjectValues = function toObjectValues(x) {
55
70
  return Object.values(x);
56
71
  };
72
+ var fromStates = function fromStates() {
73
+ for (var _len = arguments.length, lot = new Array(_len), _key = 0; _key < _len; _key++) {
74
+ lot[_key] = arguments[_key];
75
+ }
76
+ var out = lot.shift();
77
+ for (var i = 0, j = toCount(lot); i < j; ++i) {
78
+ for (var k in lot[i]) {
79
+ // Assign value
80
+ if (!isSet(out[k])) {
81
+ out[k] = lot[i][k];
82
+ continue;
83
+ }
84
+ // Merge array
85
+ if (isArray(out[k]) && isArray(lot[i][k])) {
86
+ out[k] = [ /* Clone! */ ].concat(out[k]);
87
+ for (var ii = 0, jj = toCount(lot[i][k]); ii < jj; ++ii) {
88
+ if (!hasValue(lot[i][k][ii], out[k])) {
89
+ out[k].push(lot[i][k][ii]);
90
+ }
91
+ }
92
+ // Merge object recursive
93
+ } else if (isObject(out[k]) && isObject(lot[i][k])) {
94
+ out[k] = fromStates({
95
+ /* Clone! */ }, out[k], lot[i][k]);
96
+ // Replace value
97
+ } else {
98
+ out[k] = lot[i][k];
99
+ }
100
+ }
101
+ }
102
+ return out;
103
+ };
57
104
  var W = window;
58
105
  var debounce = function debounce(then, time) {
59
106
  var timer;
@@ -66,6 +113,9 @@
66
113
  }, time);
67
114
  };
68
115
  };
116
+ var offEventDefault = function offEventDefault(e) {
117
+ return e && e.preventDefault();
118
+ };
69
119
  var isPattern = function isPattern(pattern) {
70
120
  return isInstance(pattern, RegExp);
71
121
  };
@@ -73,178 +123,130 @@
73
123
  if (isPattern(pattern)) {
74
124
  return pattern;
75
125
  }
76
- // No need to escape `/` in the pattern string
77
- pattern = pattern.replace(/\//g, '\\/');
78
126
  return new RegExp(pattern, isSet(opt) ? opt : 'g');
79
127
  };
80
- var pairs = {
81
- '`': '`',
82
- '(': ')',
83
- '{': '}',
84
- '[': ']',
85
- '"': '"',
86
- "'": "'",
87
- '<': '>'
88
- };
89
-
90
- function promisify(type, lot) {
91
- return new Promise(function (resolve, reject) {
92
- var r = W[type].apply(W, lot);
93
- return r ? resolve(r) : reject(r);
94
- });
95
- }
96
- var defaults = {
97
- source: {
98
- pairs: pairs,
99
- type: null
100
- }
101
- };
102
- ['alert', 'confirm', 'prompt'].forEach(function (type) {
103
- defaults.source[type] = function () {
104
- for (var _len = arguments.length, lot = new Array(_len), _key = 0; _key < _len; _key++) {
105
- lot[_key] = arguments[_key];
106
- }
107
- return promisify(type, lot);
108
- };
109
- });
110
- var that = {};
111
- that.toggle = function (open, close, wrap, tidy) {
112
- if (tidy === void 0) {
113
- tidy = false;
114
- }
115
- if (!close && "" !== close) {
116
- close = open;
117
- }
118
- var t = this,
119
- _t$$ = t.$(),
120
- after = _t$$.after,
121
- before = _t$$.before,
122
- value = _t$$.value,
123
- closeCount = toCount(close),
124
- openCount = toCount(open);
125
- if (wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount) || close === after.slice(0, closeCount) && open === before.slice(-openCount)) {
126
- return t.peel(open, close, wrap);
127
- }
128
- if (false !== tidy) {
129
- if (isString(tidy)) {
130
- tidy = [tidy, tidy];
131
- } else if (!isArray(tidy)) {
132
- tidy = ["", ""];
133
- }
134
- if (!isSet(tidy[1])) {
135
- tidy[1] = tidy[0];
136
- }
137
- t.trim(tidy[0], tidy[1]);
138
- }
139
- return t.wrap(open, close, wrap);
140
- };
128
+ var ALT_PREFIX = 'Alt-';
141
129
  var CTRL_PREFIX = 'Control-';
142
130
  var SHIFT_PREFIX = 'Shift-';
131
+ var bounce = debounce(function ($) {
132
+ return $.record();
133
+ }, 10);
143
134
 
144
- function canKeyDown(map, of) {
135
+ function onKeyDown(e) {
136
+ var _$$state$source, _$$state$source2;
137
+ var $ = this,
138
+ key = $.k(false).pop(),
139
+ // Capture the last key
140
+ keys = $.k();
141
+ bounce($);
142
+ if (e.defaultPrevented || $.keys[keys]) {
143
+ return;
144
+ }
145
145
  var charAfter,
146
146
  charBefore,
147
- charIndent = of.state.source.tab || of.state.tab || '\t',
148
- charPairs = of.state.source.pairs || {},
149
- charPairsValues = toObjectValues(charPairs),
150
- key = map.key,
151
- queue = map.queue,
152
- keyValue = map + "";
153
- // Do nothing
154
- if (queue.Alt || queue.Control) {
155
- return true;
147
+ charIndent = ((_$$state$source = $.state.source) == null ? void 0 : _$$state$source.tab) || $.state.tab || '\t',
148
+ charPairs = ((_$$state$source2 = $.state.source) == null ? void 0 : _$$state$source2.pairs) || {},
149
+ charPairsValues = toObjectValues(charPairs);
150
+ if (isInteger(charIndent)) {
151
+ charIndent = ' '.repeat(charIndent);
156
152
  }
157
- if (' ' === keyValue) {
158
- var _of$$ = of.$(),
159
- _after = _of$$.after,
160
- _before = _of$$.before,
161
- _value = _of$$.value;
162
- charAfter = charPairs[charBefore = _before.slice(-1)];
163
- if (!_value && charAfter && charBefore && charAfter === _after[0]) {
164
- of.wrap(' ', ' ');
165
- return false;
153
+ var _$$$ = $.$(),
154
+ after = _$$$.after,
155
+ before = _$$$.before,
156
+ end = _$$$.end,
157
+ start = _$$$.start,
158
+ value = _$$$.value,
159
+ lineAfter = after.split('\n').shift(),
160
+ lineBefore = before.split('\n').pop(),
161
+ lineMatch = /^\s+/.exec(lineBefore),
162
+ lineMatchIndent = lineMatch && lineMatch[0] || "";
163
+ if (CTRL_PREFIX + SHIFT_PREFIX + 'Enter' === keys) {
164
+ if (before || after) {
165
+ // Insert line above with `⎈⇧↵`
166
+ offEventDefault(e);
167
+ return $.select(start - toCount(lineBefore)).wrap(lineMatchIndent, '\n').insert(value).record(), false;
166
168
  }
167
- return true;
169
+ return;
168
170
  }
169
- if ('Enter' === keyValue || SHIFT_PREFIX + 'Enter' === keyValue) {
170
- var _of$$2 = of.$(),
171
- _after2 = _of$$2.after,
172
- _before2 = _of$$2.before,
173
- _value2 = _of$$2.value,
174
- lineBefore = _before2.split('\n').pop(),
175
- lineMatch = lineBefore.match(/^(\s+)/),
176
- lineMatchIndent = lineMatch && lineMatch[1] || "";
177
- if (!_value2) {
178
- if (_after2 && _before2 && (charAfter = charPairs[charBefore = _before2.slice(-1)]) && charAfter === _after2[0]) {
179
- of.wrap('\n' + lineMatchIndent + (charBefore !== charAfter ? charIndent : ""), '\n' + lineMatchIndent).record();
180
- return false;
181
- }
182
- if (lineMatchIndent) {
183
- of.insert('\n' + lineMatchIndent, -1).record();
184
- return false;
185
- }
171
+ if (CTRL_PREFIX + 'Enter' === keys) {
172
+ if (before || after) {
173
+ // Insert line below with `⎈↵`
174
+ offEventDefault(e);
175
+ return $.select(end + toCount(lineAfter)).wrap('\n' + lineMatchIndent, "").insert(value).record(), false;
186
176
  }
187
- return true;
188
177
  }
189
- if ('Backspace' === keyValue) {
190
- var _of$$3 = of.$(),
191
- _after3 = _of$$3.after,
192
- _before3 = _of$$3.before,
193
- _value3 = _of$$3.value;
194
- _after3.split('\n')[0];
195
- var _lineBefore = _before3.split('\n').pop(),
196
- _lineMatch = _lineBefore.match(/^(\s+)/),
197
- _lineMatchIndent = _lineMatch && _lineMatch[1] || "";
198
- charAfter = charPairs[charBefore = _before3.slice(-1)];
178
+ // Do nothing
179
+ if (ALT_PREFIX === keys + '-' || CTRL_PREFIX === keys + '-') {
180
+ offEventDefault(e);
181
+ return;
182
+ }
183
+ if (' ' === keys) {
184
+ charAfter = charPairs[charBefore = before.slice(-1)];
185
+ if (!value && charAfter && charBefore && charAfter === after[0]) {
186
+ offEventDefault(e);
187
+ return $.wrap(' ', ' ');
188
+ }
189
+ return;
190
+ }
191
+ if ('Backspace' === keys || 'Delete' === keys) {
192
+ charAfter = charPairs[charBefore = before.slice(-1)];
199
193
  // Do nothing on escape
200
194
  if ('\\' === charBefore) {
201
- return true;
195
+ return;
202
196
  }
203
- if (_value3) {
204
- if (_after3 && _before3 && charAfter && charAfter === _after3[0] && !_before3.endsWith('\\' + charBefore)) {
205
- of.record().peel(charBefore, charAfter).record();
206
- return false;
197
+ if (value) {
198
+ if (after && before && charAfter && charAfter === after[0] && !before.endsWith('\\' + charBefore)) {
199
+ offEventDefault(e);
200
+ return $.record().peel(charBefore, charAfter).record();
207
201
  }
208
- return true;
202
+ return;
209
203
  }
210
- charAfter = charPairs[charBefore = _before3.trim().slice(-1)];
204
+ charAfter = charPairs[charBefore = before.trim().slice(-1)];
211
205
  if (charAfter && charBefore) {
212
- if (_after3.startsWith(' ' + charAfter) && _before3.endsWith(charBefore + ' ') || _after3.startsWith('\n' + _lineMatchIndent + charAfter) && _before3.endsWith(charBefore + '\n' + _lineMatchIndent)) {
206
+ if (after.startsWith(' ' + charAfter) && before.endsWith(charBefore + ' ') || after.startsWith('\n' + lineMatchIndent + charAfter) && before.endsWith(charBefore + '\n' + lineMatchIndent)) {
213
207
  // Collapse bracket(s)
214
- of.trim("", "").record();
215
- return false;
208
+ offEventDefault(e);
209
+ return $.trim("", "").record();
216
210
  }
217
211
  }
218
212
  // Outdent
219
- if (_lineBefore.endsWith(charIndent)) {
220
- of.pull(charIndent).record();
221
- return false;
213
+ if ('Delete' !== keys && lineBefore.endsWith(charIndent)) {
214
+ offEventDefault(e);
215
+ return $.pull(charIndent).record();
222
216
  }
223
- if (_after3 && _before3 && !_before3.endsWith('\\' + charBefore)) {
224
- if (charAfter === _after3[0] && charBefore === _before3.slice(-1)) {
217
+ if (after && before && !before.endsWith('\\' + charBefore)) {
218
+ if (charAfter === after[0] && charBefore === before.slice(-1)) {
225
219
  // Peel pair
226
- of.peel(charBefore, charAfter).record();
227
- return false;
220
+ offEventDefault(e);
221
+ return $.peel(charBefore, charAfter).record();
222
+ }
223
+ }
224
+ return;
225
+ }
226
+ if ('Enter' === keys || SHIFT_PREFIX + 'Enter' === keys) {
227
+ if (!value) {
228
+ if (after && before && (charAfter = charPairs[charBefore = before.slice(-1)]) && charAfter === after[0]) {
229
+ offEventDefault(e);
230
+ return $.wrap('\n' + lineMatchIndent + (charBefore !== charAfter ? charIndent : ""), '\n' + lineMatchIndent).record();
231
+ }
232
+ if (lineMatchIndent) {
233
+ offEventDefault(e);
234
+ return $.insert('\n' + lineMatchIndent, -1).record();
228
235
  }
229
236
  }
230
- return true;
237
+ return;
231
238
  }
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
239
  // Do nothing on escape
238
240
  if ('\\' === (charBefore = before.slice(-1))) {
239
- return true;
241
+ return;
240
242
  }
241
243
  charAfter = hasValue(after[0], charPairsValues) ? after[0] : charPairs[charBefore];
242
244
  // `|}`
243
245
  if (!value && after && before && charAfter && key === charAfter) {
244
246
  // Move to the next character
245
247
  // `}|`
246
- of.select(start + 1).record();
247
- return false;
248
+ offEventDefault(e);
249
+ return $.select(start + 1).record();
248
250
  }
249
251
  for (charBefore in charPairs) {
250
252
  charAfter = charPairs[charBefore];
@@ -252,196 +254,211 @@
252
254
  if (key === charBefore && charAfter) {
253
255
  // Wrap pair or selection
254
256
  // `{|}` `{|aaa|}`
255
- of.wrap(charBefore, charAfter).record();
256
- return false;
257
+ offEventDefault(e);
258
+ return $.wrap(charBefore, charAfter).record();
257
259
  }
258
260
  // `|}`
259
261
  if (key === charAfter) {
260
262
  if (value) {
261
263
  // Wrap selection
262
264
  // `{|aaa|}`
263
- of.record().wrap(charBefore, charAfter).record();
264
- return false;
265
+ offEventDefault(e);
266
+ return $.record().wrap(charBefore, charAfter).record();
265
267
  }
266
268
  break;
267
269
  }
268
270
  }
269
- return true;
270
- }
271
-
272
- function canKeyDownDent(map, of) {
273
- var charIndent = of.state.source.tab || of.state.tab || '\t';
274
- map.key;
275
- map.queue;
276
- var keyValue = map + "";
277
- // Indent with `⎈]`
278
- if (CTRL_PREFIX + ']' === keyValue) {
279
- of.push(charIndent).record();
280
- return false;
281
- }
282
- // Outdent with `⎈[`
283
- if (CTRL_PREFIX + '[' === keyValue) {
284
- of.pull(charIndent).record();
285
- return false;
286
- }
287
- return true;
288
- }
289
-
290
- function canKeyDownEnter(map, of) {
291
- map.key;
292
- var queue = map.queue;
293
- if (queue.Control && queue.Enter) {
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
- lineAfter = after.split('\n').shift(),
301
- lineBefore = before.split('\n').pop(),
302
- lineMatch = lineBefore.match(/^(\s+)/),
303
- lineMatchIndent = lineMatch && lineMatch[1] || "";
304
- if (before || after) {
305
- if (queue.Shift) {
306
- // Insert line above with `⎈⇧↵`
307
- return of.select(start - toCount(lineBefore)).wrap(lineMatchIndent, '\n').insert(value).record(), false;
308
- }
309
- // Insert line below with `⎈↵`
310
- return of.select(end + toCount(lineAfter)).wrap('\n' + lineMatchIndent, "").insert(value).record(), false;
311
- }
312
- }
313
- return true;
314
- }
315
-
316
- function canKeyDownHistory(map, of) {
317
- var keyValue = map + "";
318
- // Redo with `⎈y`
319
- if (CTRL_PREFIX + 'y' === keyValue) {
320
- return of.redo(), false;
321
- }
322
- // Undo with `⎈z`
323
- if (CTRL_PREFIX + 'z' === keyValue) {
324
- return of.undo(), false;
325
- }
326
- return true;
327
- }
328
-
329
- function canKeyDownMove(map, of) {
330
- map.key;
331
- var queue = map.queue,
332
- keyValue = map + "";
333
- if (!queue.Control) {
334
- return true;
335
- }
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
- charPair,
271
+ var charPair,
343
272
  charPairValue,
344
- charPairs = of.state.source.pairs || {},
345
- boundaries = [],
346
- m;
273
+ m,
274
+ tokens = [];
347
275
  if (value) {
348
276
  for (charPair in charPairs) {
349
277
  if (!(charPairValue = charPairs[charPair])) {
350
278
  continue;
351
279
  }
352
- boundaries.push('(?:\\' + charPair + '(?:\\\\.|[^\\' + charPair + (charPairValue !== charPair ? '\\' + charPairValue : "") + '])*\\' + charPairValue + ')');
280
+ tokens.push('(?:\\' + charPair + '(?:\\\\.|[^\\' + charPair + (charPairValue !== charPair ? '\\' + charPairValue : "") + '])*\\' + charPairValue + ')');
353
281
  }
354
- boundaries.push('\\w+'); // Word(s)
355
- boundaries.push('\\s+'); // White-space(s)
356
- boundaries.push('[\\s\\S]'); // Last try!
357
- if (CTRL_PREFIX + 'ArrowLeft' === keyValue) {
358
- if (m = before.match(toPattern('(' + boundaries.join('|') + ')$', ""))) {
359
- of.insert("").select(start - toCount(m[0])).insert(value);
360
- return of.record(), false;
282
+ tokens.push('\\w+'); // Word(s)
283
+ tokens.push('\\s+'); // White-space(s)
284
+ tokens.push('[\\s\\S]'); // Last try!
285
+ if (CTRL_PREFIX + 'ArrowLeft' === keys) {
286
+ offEventDefault(e);
287
+ if (m = toPattern('(' + tokens.join('|') + ')$', "").exec(before)) {
288
+ return $.insert("").select(start - toCount(m[0])).insert(value).record();
361
289
  }
362
- return of.select(), false;
290
+ return $.select();
363
291
  }
364
- if (CTRL_PREFIX + 'ArrowRight' === keyValue) {
365
- if (m = after.match(toPattern('^(' + boundaries.join('|') + ')', ""))) {
366
- of.insert("").select(end + toCount(m[0]) - toCount(value)).insert(value);
367
- return of.record(), false;
292
+ if (CTRL_PREFIX + 'ArrowRight' === keys) {
293
+ offEventDefault(e);
294
+ if (m = after.match(toPattern('^(' + tokens.join('|') + ')', ""))) {
295
+ return $.insert("").select(end + toCount(m[0]) - toCount(value)).insert(value).record();
368
296
  }
369
- return of.select(), false;
297
+ return $.select();
370
298
  }
371
299
  }
372
- var lineAfter = after.split('\n').shift(),
373
- lineBefore = before.split('\n').pop(),
374
- lineMatch = lineBefore.match(/^(\s+)/);
375
- lineMatch && lineMatch[1] || "";
376
300
  // Force to select the current line if there is no selection
377
301
  end += toCount(lineAfter);
378
302
  start -= toCount(lineBefore);
379
303
  value = lineBefore + value + lineAfter;
380
- if (CTRL_PREFIX + 'ArrowUp' === keyValue) {
304
+ if (CTRL_PREFIX + 'ArrowUp' === keys) {
305
+ offEventDefault(e);
381
306
  if (!hasValue('\n', before)) {
382
- return of.select(), false;
307
+ return $.select();
383
308
  }
384
- of.insert("");
385
- of.replace(/^([^\n]*?)(\n|$)/, '$2', 1);
386
- of.replace(/(^|\n)([^\n]*?)$/, "", -1);
387
- var $ = of.$();
388
- before = $.before;
389
- start = $.start;
309
+ $.insert("");
310
+ $.replace(/^([^\n]*?)(\n|$)/, '$2', 1);
311
+ $.replace(/(^|\n)([^\n]*?)$/, "", -1);
312
+ var s = $.$();
313
+ before = s.before;
314
+ start = s.start;
390
315
  lineBefore = before.split('\n').pop();
391
- of.select(start = start - toCount(lineBefore)).wrap(value, '\n');
392
- of.select(start, start + toCount(value));
393
- return of.record(), false;
316
+ $.select(start = start - toCount(lineBefore)).wrap(value, '\n');
317
+ $.select(start, start + toCount(value));
318
+ return $.record();
394
319
  }
395
- if (CTRL_PREFIX + 'ArrowDown' === keyValue) {
320
+ if (CTRL_PREFIX + 'ArrowDown' === keys) {
321
+ offEventDefault(e);
396
322
  if (!hasValue('\n', after)) {
397
- return of.select(), false;
323
+ return $.select();
398
324
  }
399
- of.insert("");
400
- of.replace(/^([^\n]*?)(\n|$)/, "", 1);
401
- of.replace(/(^|\n)([^\n]*?)$/, '$1', -1);
402
- var _$ = of.$();
403
- after = _$.after;
404
- end = _$.end;
325
+ $.insert("");
326
+ $.replace(/^([^\n]*?)(\n|$)/, "", 1);
327
+ $.replace(/(^|\n)([^\n]*?)$/, '$1', -1);
328
+ var _s = $.$();
329
+ after = _s.after;
330
+ end = _s.end;
405
331
  lineAfter = after.split('\n').shift();
406
- of.select(end = end + toCount(lineAfter)).wrap('\n', value);
332
+ $.select(end = end + toCount(lineAfter)).wrap('\n', value);
407
333
  end += 1;
408
- of.select(end, end + toCount(value));
409
- return of.record(), false;
334
+ $.select(end, end + toCount(value));
335
+ return $.record();
410
336
  }
411
- return true;
337
+ return;
412
338
  }
413
339
 
414
- function canKeyDownTab(map, of) {
415
- var charIndent = of.state.source.tab || of.state.tab || '\t',
416
- keyValue = map + "";
417
- // Indent with `⇥`
418
- if ('Tab' === keyValue) {
419
- return of.push(charIndent).record(), false;
420
- }
421
- // Outdent with `⇧+⇥`
422
- if (SHIFT_PREFIX + 'Tab' === keyValue) {
423
- return of.pull(charIndent).record(), false;
424
- }
425
- return true;
340
+ function attach() {
341
+ var $ = this;
342
+ $.state = fromStates({
343
+ source: {
344
+ pairs: {
345
+ '`': '`',
346
+ '(': ')',
347
+ '{': '}',
348
+ '[': ']',
349
+ '"': '"',
350
+ "'": "'",
351
+ '<': '>'
352
+ }
353
+ }
354
+ }, $.state);
355
+ $.alert = function (hint, then) {
356
+ W.alert && W.alert(hint);
357
+ return isFunction(then) && then.call($, true);
358
+ };
359
+ $.confirm = function (hint, then) {
360
+ return isFunction(then) && then.call($, W.confirm && W.confirm(hint));
361
+ };
362
+ $.insertBlock = function (value, mode) {
363
+ var _$$$2 = $.$(),
364
+ after = _$$$2.after,
365
+ before = _$$$2.before,
366
+ end = _$$$2.end,
367
+ start = _$$$2.start,
368
+ lineAfter = after.split('\n').shift(),
369
+ lineAfterCount = toCount(lineAfter),
370
+ lineBefore = before.split('\n').pop(),
371
+ lineBeforeCount = toCount(lineBefore),
372
+ lineMatch = /^\s+/.exec(lineBefore),
373
+ lineMatchIndent = lineMatch && lineMatch[0] || "";
374
+ if (-1 === mode) {
375
+ return $.select(start - lineBeforeCount).insert('\n', 1).push(lineMatchIndent).insert(value, 1, false);
376
+ }
377
+ if (1 === mode) {
378
+ return $.select(end + lineAfterCount).insert('\n', -1).push(lineMatchIndent).insert(value, 1, false);
379
+ }
380
+ return $.select(start - lineBeforeCount, end + lineAfterCount).insert(value, mode, true).wrap(lineMatchIndent, "");
381
+ };
382
+ $.peelBlock = function (open, close, wrap) {
383
+ var _$$$3 = $.$(),
384
+ after = _$$$3.after,
385
+ before = _$$$3.before,
386
+ end = _$$$3.end,
387
+ start = _$$$3.start,
388
+ value = _$$$3.value,
389
+ closeCount = toCount(close),
390
+ lineAfter = after.split('\n').shift(),
391
+ lineAfterCount = toCount(lineAfter),
392
+ lineBefore = before.split('\n').pop(),
393
+ lineBeforeCount = toCount(lineBefore),
394
+ openCount = toCount(open);
395
+ if (wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount) || close === lineAfter.slice(-closeCount) && open === lineBefore.slice(0, openCount)) {
396
+ return $.select(start - lineBeforeCount + (wrap ? 0 : openCount), end + lineAfterCount - (wrap ? 0 : closeCount)).peel(open, close, wrap);
397
+ }
398
+ return $.select(start, end);
399
+ };
400
+ $.prompt = function (hint, value, then) {
401
+ return isFunction(then) && then.call($, W.prompt ? W.prompt(hint, value) : false);
402
+ };
403
+ $.selectBlock = function () {
404
+ var _$$$4 = $.$(),
405
+ after = _$$$4.after,
406
+ before = _$$$4.before,
407
+ end = _$$$4.end,
408
+ start = _$$$4.start,
409
+ lineAfter = after.split('\n').shift(),
410
+ lineAfterCount = toCount(lineAfter),
411
+ lineBefore = before.split('\n').pop(),
412
+ lineBeforeCount = toCount(lineBefore);
413
+ return $.select(start - lineBeforeCount, end + lineAfterCount);
414
+ };
415
+ $.toggle = function (open, close, wrap) {
416
+ var _$$$5 = $.$(),
417
+ after = _$$$5.after,
418
+ before = _$$$5.before,
419
+ value = _$$$5.value,
420
+ closeCount = toCount(close),
421
+ openCount = toCount(open);
422
+ if (wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount) || close === after.slice(0, closeCount) && open === before.slice(-openCount)) {
423
+ return $.peel(open, close, wrap);
424
+ }
425
+ return $.wrap(open, close, wrap);
426
+ };
427
+ $.toggleBlock = function (open, close, wrap) {
428
+ var _$$$6 = $.$(),
429
+ after = _$$$6.after,
430
+ before = _$$$6.before,
431
+ value = _$$$6.value,
432
+ closeCount = toCount(close),
433
+ lineAfter = after.split('\n').shift(),
434
+ lineBefore = before.split('\n').pop(),
435
+ openCount = toCount(open);
436
+ if (wrap && close === value.slice(-closeCount) && open === value.slice(0, openCount) || close === lineAfter.slice(-closeCount) && open === lineBefore.slice(0, openCount)) {
437
+ return $.peelBlock(open, close, wrap);
438
+ }
439
+ return $.wrapBlock(open, close, wrap);
440
+ };
441
+ $.wrapBlock = function (open, close, wrap) {
442
+ var _$$$7 = $.$(),
443
+ after = _$$$7.after,
444
+ before = _$$$7.before,
445
+ end = _$$$7.end,
446
+ start = _$$$7.start,
447
+ lineAfter = after.split('\n').shift(),
448
+ lineAfterCount = toCount(lineAfter),
449
+ lineBefore = before.split('\n').pop(),
450
+ lineBeforeCount = toCount(lineBefore);
451
+ return $.select(start - lineBeforeCount, end + lineAfterCount).wrap(open, close, wrap);
452
+ };
453
+ return $.on('key.down', onKeyDown).record();
426
454
  }
427
- var bounce = debounce(function (of) {
428
- return of.record();
429
- }, 100);
430
455
 
431
- function canKeyUp(map, of) {
432
- return bounce(of), true;
456
+ function detach() {
457
+ return this.off('key.down', onKeyDown);
433
458
  }
434
- var state = defaults;
435
- exports.canKeyDown = canKeyDown;
436
- exports.canKeyDownDent = canKeyDownDent;
437
- exports.canKeyDownEnter = canKeyDownEnter;
438
- exports.canKeyDownHistory = canKeyDownHistory;
439
- exports.canKeyDownMove = canKeyDownMove;
440
- exports.canKeyDownTab = canKeyDownTab;
441
- exports.canKeyUp = canKeyUp;
442
- exports.state = state;
443
- exports.that = that;
444
- Object.defineProperty(exports, '__esModule', {
445
- value: true
446
- });
459
+ var index_js = {
460
+ attach: attach,
461
+ detach: detach
462
+ };
463
+ return index_js;
447
464
  }));