@taufik-nurrohman/text-editor.key 1.0.10 → 1.0.12

Sign up to get free protection for your applications and to get access to all the features.
package/index.js CHANGED
@@ -207,26 +207,45 @@
207
207
  map.pull(); // Reset all key(s)
208
208
  }
209
209
 
210
- function onInput(e) {
211
- var $ = this,
212
- key = e.data,
213
- map = getReference($);
214
- key && map.pull(key);
210
+ function onFocus(e) {
211
+ onBlur.call(this, e);
215
212
  }
216
213
 
217
- function onKeyDown(e) {
218
- var $ = this,
214
+ function onKeyDownOrPutDown(e) {
215
+ var map = getReference(this),
219
216
  command,
220
217
  v,
218
+ data = e.data,
219
+ inputType = e.inputType,
221
220
  key = e.key,
222
- map = getReference($);
223
- // Make the `Alt`, `Control`, `Meta`, and `Shift` key(s) sticky (does not require the user to release all key(s) first to repeat or change the current key combination).
224
- map[e.altKey ? 'push' : 'pull']('Alt');
225
- map[e.ctrlKey ? 'push' : 'pull']('Control');
226
- map[e.metaKey ? 'push' : 'pull']('Meta');
227
- map[e.shiftKey ? 'push' : 'pull']('Shift');
228
- // Add the actual key to the queue. Don’t worry, this will not mistakenly add a key that already exists in the queue.
229
- key && map.push(key);
221
+ type = e.type;
222
+ if ('keydown' === type) {
223
+ // Make the `Alt`, `Control`, `Meta`, and `Shift` key(s) sticky (does not require the user to release all key(s) first to repeat or change the current key combination).
224
+ map[e.altKey ? 'push' : 'pull']('Alt');
225
+ map[e.ctrlKey ? 'push' : 'pull']('Control');
226
+ map[e.metaKey ? 'push' : 'pull']('Meta');
227
+ map[e.shiftKey ? 'push' : 'pull']('Shift');
228
+ // Add the actual key to the queue. Don’t worry, this will not mistakenly add a key that already exists in the queue.
229
+ key && map.push(key);
230
+ } else {
231
+ if ('deleteContentBackward' === inputType) {
232
+ map.pull().push('Backspace'); // Simulate `Backspace` key
233
+ } else if ('deleteContentForward' === inputType) {
234
+ map.pull().push('Delete'); // Simulate `Delete` key
235
+ } else if ('deleteWordBackward' === inputType) {
236
+ map.pull().push('Control').push('Backspace'); // Simulate `Control-Backspace` keys
237
+ } else if ('deleteWordForward' === inputType) {
238
+ map.pull().push('Control').push('Delete'); // Simulate `Control-Delete` keys
239
+ } else if ('insertLineBreak' === inputType) {
240
+ map.pull().push('Enter'); // Simulate `Enter` key
241
+ } else if ('insertText' === inputType && data) {
242
+ // One character at a time
243
+ map.toArray().forEach(function (key) {
244
+ return 1 === toCount(key) && map.pull(key);
245
+ });
246
+ map.push(data);
247
+ }
248
+ }
230
249
  if (command = map.command()) {
231
250
  v = map.fire(command);
232
251
  if (false === v) {
@@ -239,11 +258,33 @@
239
258
  bounce(map, e); // Reset all key(s) after 1 second idle.
240
259
  }
241
260
 
242
- function onKeyUp(e) {
243
- var $ = this,
261
+ function onKeyUpOrPutUp(e) {
262
+ var map = getReference(this),
263
+ data = e.data,
264
+ inputType = e.inputType,
244
265
  key = e.key,
245
- map = getReference($);
246
- key && map.pull(key); // Reset current key.
266
+ type = e.type;
267
+ if ('keyup' === type) {
268
+ map[e.altKey ? 'push' : 'pull']('Alt');
269
+ map[e.ctrlKey ? 'push' : 'pull']('Control');
270
+ map[e.metaKey ? 'push' : 'pull']('Meta');
271
+ map[e.shiftKey ? 'push' : 'pull']('Shift');
272
+ key && map.pull(key);
273
+ } else {
274
+ if ('deleteContentBackward' === inputType) {
275
+ map.pull('Backspace');
276
+ } else if ('deleteContentForward' === inputType) {
277
+ map.pull('Delete');
278
+ } else if ('deleteWordBackward' === inputType) {
279
+ map.pull('Control').pull('Backspace');
280
+ } else if ('deleteWordForward' === inputType) {
281
+ map.pull('Control').pull('Delete');
282
+ } else if ('insertLineBreak' === inputType) {
283
+ map.pull('Enter');
284
+ } else if ('insertText' === inputType && data) {
285
+ map.pull(data);
286
+ }
287
+ }
247
288
  }
248
289
 
249
290
  function setReference(key, value) {
@@ -271,9 +312,11 @@
271
312
  return $.keys[key] = of, $;
272
313
  });
273
314
  $.on('blur', onBlur);
274
- $.on('input', onInput);
275
- $.on('key.down', onKeyDown);
276
- $.on('key.up', onKeyUp);
315
+ $.on('focus', onFocus);
316
+ $.on('key.down', onKeyDownOrPutDown);
317
+ $.on('key.up', onKeyUpOrPutUp);
318
+ $.on('put.down', onKeyDownOrPutDown);
319
+ $.on('put.up', onKeyUpOrPutUp);
277
320
  return setReference($, map), $;
278
321
  }
279
322
 
@@ -282,9 +325,11 @@
282
325
  map = getReference($);
283
326
  map.pull();
284
327
  $.off('blur', onBlur);
285
- $.off('input', onInput);
286
- $.off('key.down', onKeyDown);
287
- $.off('key.up', onKeyUp);
328
+ $.off('focus', onFocus);
329
+ $.off('key.down', onKeyDownOrPutDown);
330
+ $.off('key.up', onKeyUpOrPutUp);
331
+ $.off('put.down', onKeyDownOrPutDown);
332
+ $.off('put.up', onKeyUpOrPutUp);
288
333
  return letReference($), $;
289
334
  }
290
335
  var index_js = {
package/index.min.js CHANGED
@@ -23,4 +23,4 @@
23
23
  * SOFTWARE.
24
24
  *
25
25
  */
26
- !function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):((t="undefined"!=typeof globalThis?globalThis:t||self).TextEditor=t.TextEditor||{},t.TextEditor.Key=n())}(this,(function(){"use strict";var t=function(t){return Array.isArray(t)},n=function(t){return"function"==typeof t},e=function(t,n){return void 0===n&&(n=!0),!(!t||"object"!=typeof t)&&(!n||function(t,n){return!(!t||"object"!=typeof t)&&r(n)&&r(t.constructor)&&n===t.constructor}(t,Object))},r=function(t){return function(t){return void 0!==t}(t)&&!function(t){return null===t}(t)},o=function(t){return"string"==typeof t};function u(t){var n=this;return n.commands={},n.key=null,n.keys={},n.self=t||n,n.set=new Set,n}var i=u.prototype;i.command=function(t){var n=this;if(o(t))return t===n.toString();var e=n.keys[n.toString()];return!!r(e)&&e},i.fire=function(e,u){var i,f,s=this,a=s.self||s;return u=u||[],n(e)||o(e)&&(e=s.commands[e])?(i=e.apply(a,u),f=!0):t(e)&&(t(e[1])&&e[1].forEach((function(t,n){return r(t)&&(u[n]=t)})),(e=s.commands[e[0]])&&(i=e.apply(a,u),f=!0)),f?!r(i)||i:null},i.pull=function(t){var n=this;return n.key=null,r(t)?(n.set.delete(t),n):(n.set=new Set,n)},i.push=function(t){var n=this;return n.set.add(n.key=t,1),n},i.toArray=function(){return Array.from(this.set)},i.toString=function(){return this.toArray().join("-")},Object.defineProperty(u,"name",{value:"Key"});var f,s,a,c=function(t){return t.length},l=function(){for(var n=arguments.length,o=Array(n),u=0;u<n;u++)o[u]=arguments[u];for(var i,f=o.shift(),s=0,a=c(o);s<a;++s)for(var y in o[s])if(r(f[y]))if(t(f[y])&&t(o[s][y])){f[y]=[].concat(f[y]);for(var p=0,h=c(o[s][y]);p<h;++p)i=o[s][y][p],-1===f[y].indexOf(i)&&f[y].push(o[s][y][p])}else e(f[y])&&e(o[s][y])?f[y]=l({},f[y],o[s][y]):f[y]=o[s][y];else f[y]=o[s][y];return f},y=(f=function(t,n){t.pull(),n.altKey&&t.push("Alt"),n.ctrlKey&&t.push("Control"),n.metaKey&&t.push("Meta"),n.shiftKey&&t.push("Shift")},s=1e3,function(){var t=arguments,n=this;a&&clearTimeout(a),a=setTimeout((function(){return f.apply(n,t)}),s)}),p=new WeakMap;function h(t){return p.get(t)||null}function d(t){h(this).pull()}function m(t){var n=t.data,e=h(this);n&&e.pull(n)}function v(t){var n,e,r=t.key,o=h(this);o[t.altKey?"push":"pull"]("Alt"),o[t.ctrlKey?"push":"pull"]("Control"),o[t.metaKey?"push":"pull"]("Meta"),o[t.shiftKey?"push":"pull"]("Shift"),r&&o.push(r),(n=o.command())&&(!1===(e=o.fire(n))?(function(t){t&&t.preventDefault()}(t),function(t){t&&t.stopPropagation()}(t)):null===e&&console.warn("Unknown command:",n)),y(o,t)}function k(t){var n=t.key,e=h(this);n&&e.pull(n)}var K={attach:function(){var t,e,r=this,o=r.constructor._,i=new u(r);return r.commands=l(r.commands=i.commands,r.state.commands||{}),r.keys=l(r.keys=i.keys,r.state.keys||{}),!n(o.command)&&(o.command=function(t,n){return this.commands[t]=n,this}),!n(o.k)&&(o.k=function(t){var n=h(this).toArray();return!1===t?n:n.join(t||"-")}),!n(o.key)&&(o.key=function(t,n){return this.keys[t]=n,this}),r.on("blur",d),r.on("input",m),r.on("key.down",v),r.on("key.up",k),t=r,e=i,p.set(t,e),r},detach:function(){var t,n=this;return h(n).pull(),n.off("blur",d),n.off("input",m),n.off("key.down",v),n.off("key.up",k),t=n,p.delete(t),n},name:"TextEditor.Key"};return K}));
26
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):((t="undefined"!=typeof globalThis?globalThis:t||self).TextEditor=t.TextEditor||{},t.TextEditor.Key=e())}(this,(function(){"use strict";var t=function(t){return Array.isArray(t)},e=function(t){return"function"==typeof t},n=function(t,e){return void 0===e&&(e=!0),!(!t||"object"!=typeof t)&&(!e||function(t,e){return!(!t||"object"!=typeof t)&&r(e)&&r(t.constructor)&&e===t.constructor}(t,Object))},r=function(t){return function(t){return void 0!==t}(t)&&!function(t){return null===t}(t)},o=function(t){return"string"==typeof t};function u(t){var e=this;return e.commands={},e.key=null,e.keys={},e.self=t||e,e.set=new Set,e}var l=u.prototype;l.command=function(t){var e=this;if(o(t))return t===e.toString();var n=e.keys[e.toString()];return!!r(n)&&n},l.fire=function(n,u){var l,i,a=this,s=a.self||a;return u=u||[],e(n)||o(n)&&(n=a.commands[n])?(l=n.apply(s,u),i=!0):t(n)&&(t(n[1])&&n[1].forEach((function(t,e){return r(t)&&(u[e]=t)})),(n=a.commands[n[0]])&&(l=n.apply(s,u),i=!0)),i?!r(l)||l:null},l.pull=function(t){var e=this;return e.key=null,r(t)?(e.set.delete(t),e):(e.set=new Set,e)},l.push=function(t){var e=this;return e.set.add(e.key=t,1),e},l.toArray=function(){return Array.from(this.set)},l.toString=function(){return this.toArray().join("-")},Object.defineProperty(u,"name",{value:"Key"});var i,a,s,p=function(t){return t.length},f=function(){for(var e=arguments.length,o=Array(e),u=0;u<e;u++)o[u]=arguments[u];for(var l,i=o.shift(),a=0,s=p(o);a<s;++a)for(var c in o[a])if(r(i[c]))if(t(i[c])&&t(o[a][c])){i[c]=[].concat(i[c]);for(var d=0,y=p(o[a][c]);d<y;++d)l=o[a][c][d],-1===i[c].indexOf(l)&&i[c].push(o[a][c][d])}else n(i[c])&&n(o[a][c])?i[c]=f({},i[c],o[a][c]):i[c]=o[a][c];else i[c]=o[a][c];return i},c=(i=function(t,e){t.pull(),e.altKey&&t.push("Alt"),e.ctrlKey&&t.push("Control"),e.metaKey&&t.push("Meta"),e.shiftKey&&t.push("Shift")},a=1e3,function(){var t=arguments,e=this;s&&clearTimeout(s),s=setTimeout((function(){return i.apply(e,t)}),a)}),d=new WeakMap;function y(t){return d.get(t)||null}function h(t){y(this).pull()}function m(t){h.call(this,t)}function k(t){var e,n,r=y(this),o=t.data,u=t.inputType,l=t.key;"keydown"===t.type?(r[t.altKey?"push":"pull"]("Alt"),r[t.ctrlKey?"push":"pull"]("Control"),r[t.metaKey?"push":"pull"]("Meta"),r[t.shiftKey?"push":"pull"]("Shift"),l&&r.push(l)):"deleteContentBackward"===u?r.pull().push("Backspace"):"deleteContentForward"===u?r.pull().push("Delete"):"deleteWordBackward"===u?r.pull().push("Control").push("Backspace"):"deleteWordForward"===u?r.pull().push("Control").push("Delete"):"insertLineBreak"===u?r.pull().push("Enter"):"insertText"===u&&o&&(r.toArray().forEach((function(t){return 1===p(t)&&r.pull(t)})),r.push(o)),(e=r.command())&&(!1===(n=r.fire(e))?(function(t){t&&t.preventDefault()}(t),function(t){t&&t.stopPropagation()}(t)):null===n&&console.warn("Unknown command:",e)),c(r,t)}function v(t){var e=y(this),n=t.data,r=t.inputType,o=t.key;"keyup"===t.type?(e[t.altKey?"push":"pull"]("Alt"),e[t.ctrlKey?"push":"pull"]("Control"),e[t.metaKey?"push":"pull"]("Meta"),e[t.shiftKey?"push":"pull"]("Shift"),o&&e.pull(o)):"deleteContentBackward"===r?e.pull("Backspace"):"deleteContentForward"===r?e.pull("Delete"):"deleteWordBackward"===r?e.pull("Control").pull("Backspace"):"deleteWordForward"===r?e.pull("Control").pull("Delete"):"insertLineBreak"===r?e.pull("Enter"):"insertText"===r&&n&&e.pull(n)}var w={attach:function(){var t,n,r=this,o=r.constructor._,l=new u(r);return r.commands=f(r.commands=l.commands,r.state.commands||{}),r.keys=f(r.keys=l.keys,r.state.keys||{}),!e(o.command)&&(o.command=function(t,e){return this.commands[t]=e,this}),!e(o.k)&&(o.k=function(t){var e=y(this).toArray();return!1===t?e:e.join(t||"-")}),!e(o.key)&&(o.key=function(t,e){return this.keys[t]=e,this}),r.on("blur",h),r.on("focus",m),r.on("key.down",k),r.on("key.up",v),r.on("put.down",k),r.on("put.up",v),t=r,n=l,d.set(t,n),r},detach:function(){var t,e=this;return y(e).pull(),e.off("blur",h),e.off("focus",m),e.off("key.down",k),e.off("key.up",v),e.off("put.down",k),e.off("put.up",v),t=e,d.delete(t),e},name:"TextEditor.Key"};return w}));
package/index.mjs CHANGED
@@ -1,8 +1,10 @@
1
1
  import Key from '@taufik-nurrohman/key';
2
- import {debounce} from '@taufik-nurrohman/tick';
2
+
3
+ import {debounce, delay} from '@taufik-nurrohman/tick';
3
4
  import {fromStates} from '@taufik-nurrohman/from';
4
- import {isFunction, isSet} from '@taufik-nurrohman/is';
5
+ import {isFunction, isSet, isString} from '@taufik-nurrohman/is';
5
6
  import {offEventDefault, offEventPropagation} from '@taufik-nurrohman/event';
7
+ import {toCount} from '@taufik-nurrohman/to';
6
8
 
7
9
  const bounce = debounce((map, e) => {
8
10
  // Remove all key(s)
@@ -31,24 +33,38 @@ function onBlur(e) {
31
33
  map.pull(); // Reset all key(s)
32
34
  }
33
35
 
34
- function onInput(e) {
35
- let $ = this,
36
- key = e.data,
37
- map = getReference($);
38
- key && map.pull(key);
36
+ function onFocus(e) {
37
+ onBlur.call(this, e);
39
38
  }
40
39
 
41
- function onKeyDown(e) {
42
- let $ = this, command, v,
43
- key = e.key,
44
- map = getReference($);
45
- // Make the `Alt`, `Control`, `Meta`, and `Shift` key(s) sticky (does not require the user to release all key(s) first to repeat or change the current key combination).
46
- map[e.altKey ? 'push' : 'pull']('Alt');
47
- map[e.ctrlKey ? 'push' : 'pull']('Control');
48
- map[e.metaKey ? 'push' : 'pull']('Meta');
49
- map[e.shiftKey ? 'push' : 'pull']('Shift');
50
- // Add the actual key to the queue. Don’t worry, this will not mistakenly add a key that already exists in the queue.
51
- key && map.push(key);
40
+ function onKeyDownOrPutDown(e) {
41
+ let map = getReference(this), command, v,
42
+ {data, inputType, key, type} = e;
43
+ if ('keydown' === type) {
44
+ // Make the `Alt`, `Control`, `Meta`, and `Shift` key(s) sticky (does not require the user to release all key(s) first to repeat or change the current key combination).
45
+ map[e.altKey ? 'push' : 'pull']('Alt');
46
+ map[e.ctrlKey ? 'push' : 'pull']('Control');
47
+ map[e.metaKey ? 'push' : 'pull']('Meta');
48
+ map[e.shiftKey ? 'push' : 'pull']('Shift');
49
+ // Add the actual key to the queue. Don’t worry, this will not mistakenly add a key that already exists in the queue.
50
+ key && map.push(key);
51
+ } else {
52
+ if ('deleteContentBackward' === inputType) {
53
+ map.pull().push('Backspace'); // Simulate `Backspace` key
54
+ } else if ('deleteContentForward' === inputType) {
55
+ map.pull().push('Delete'); // Simulate `Delete` key
56
+ } else if ('deleteWordBackward' === inputType) {
57
+ map.pull().push('Control').push('Backspace'); // Simulate `Control-Backspace` keys
58
+ } else if ('deleteWordForward' === inputType) {
59
+ map.pull().push('Control').push('Delete'); // Simulate `Control-Delete` keys
60
+ } else if ('insertLineBreak' === inputType) {
61
+ map.pull().push('Enter'); // Simulate `Enter` key
62
+ } else if ('insertText' === inputType && data) {
63
+ // One character at a time
64
+ map.toArray().forEach(key => 1 === toCount(key) && map.pull(key));
65
+ map.push(data);
66
+ }
67
+ }
52
68
  if (command = map.command()) {
53
69
  v = map.fire(command);
54
70
  if (false === v) {
@@ -61,11 +77,30 @@ function onKeyDown(e) {
61
77
  bounce(map, e); // Reset all key(s) after 1 second idle.
62
78
  }
63
79
 
64
- function onKeyUp(e) {
65
- let $ = this,
66
- key = e.key,
67
- map = getReference($);
68
- key && map.pull(key); // Reset current key.
80
+ function onKeyUpOrPutUp(e) {
81
+ let map = getReference(this),
82
+ {data, inputType, key, type} = e;
83
+ if ('keyup' === type) {
84
+ map[e.altKey ? 'push' : 'pull']('Alt');
85
+ map[e.ctrlKey ? 'push' : 'pull']('Control');
86
+ map[e.metaKey ? 'push' : 'pull']('Meta');
87
+ map[e.shiftKey ? 'push' : 'pull']('Shift');
88
+ key && map.pull(key);
89
+ } else {
90
+ if ('deleteContentBackward' === inputType) {
91
+ map.pull('Backspace');
92
+ } else if ('deleteContentForward' === inputType) {
93
+ map.pull('Delete');
94
+ } else if ('deleteWordBackward' === inputType) {
95
+ map.pull('Control').pull('Backspace');
96
+ } else if ('deleteWordForward' === inputType) {
97
+ map.pull('Control').pull('Delete');
98
+ } else if ('insertLineBreak' === inputType) {
99
+ map.pull('Enter');
100
+ } else if ('insertText' === inputType && data) {
101
+ map.pull(data);
102
+ }
103
+ }
69
104
  }
70
105
 
71
106
  function setReference(key, value) {
@@ -93,9 +128,11 @@ function attach() {
93
128
  return ($.keys[key] = of), $;
94
129
  });
95
130
  $.on('blur', onBlur);
96
- $.on('input', onInput);
97
- $.on('key.down', onKeyDown);
98
- $.on('key.up', onKeyUp);
131
+ $.on('focus', onFocus);
132
+ $.on('key.down', onKeyDownOrPutDown);
133
+ $.on('key.up', onKeyUpOrPutUp);
134
+ $.on('put.down', onKeyDownOrPutDown);
135
+ $.on('put.up', onKeyUpOrPutUp);
99
136
  return setReference($, map), $;
100
137
  }
101
138
 
@@ -104,9 +141,11 @@ function detach() {
104
141
  map = getReference($);
105
142
  map.pull();
106
143
  $.off('blur', onBlur);
107
- $.off('input', onInput);
108
- $.off('key.down', onKeyDown);
109
- $.off('key.up', onKeyUp);
144
+ $.off('focus', onFocus);
145
+ $.off('key.down', onKeyDownOrPutDown);
146
+ $.off('key.up', onKeyUpOrPutUp);
147
+ $.off('put.down', onKeyDownOrPutDown);
148
+ $.off('put.up', onKeyUpOrPutUp);
110
149
  return letReference($), $;
111
150
  }
112
151
 
package/package.json CHANGED
@@ -8,7 +8,8 @@
8
8
  "@taufik-nurrohman/is": "*",
9
9
  "@taufik-nurrohman/key": "*",
10
10
  "@taufik-nurrohman/text-editor": "*",
11
- "@taufik-nurrohman/tick": "*"
11
+ "@taufik-nurrohman/tick": "*",
12
+ "@taufik-nurrohman/to": "*"
12
13
  },
13
14
  "description": "Provides a feature to easily interact with the keyboard keys.",
14
15
  "devDependencies": {
@@ -46,5 +47,5 @@
46
47
  "scripts": {
47
48
  "pack": "pack --clean=false --from=.factory --js-format=umd --js-name=TextEditor.Key --js-top='%(js.license)' --mjs=true --to=."
48
49
  },
49
- "version": "1.0.10"
50
+ "version": "1.0.12"
50
51
  }