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

Sign up to get free protection for your applications and to get access to all the features.
package/index.js CHANGED
@@ -211,19 +211,41 @@
211
211
  onBlur.call(this, e);
212
212
  }
213
213
 
214
- function onKeyDown(e) {
215
- var $ = this,
214
+ function onKeyDownOrPutDown(e) {
215
+ var map = getReference(this),
216
216
  command,
217
217
  v,
218
+ data = e.data,
219
+ inputType = e.inputType,
218
220
  key = e.key,
219
- map = getReference($);
220
- // 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).
221
- map[e.altKey ? 'push' : 'pull']('Alt');
222
- map[e.ctrlKey ? 'push' : 'pull']('Control');
223
- map[e.metaKey ? 'push' : 'pull']('Meta');
224
- map[e.shiftKey ? 'push' : 'pull']('Shift');
225
- // Add the actual key to the queue. Don’t worry, this will not mistakenly add a key that already exists in the queue.
226
- 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
+ }
227
249
  if (command = map.command()) {
228
250
  v = map.fire(command);
229
251
  if (false === v) {
@@ -236,24 +258,32 @@
236
258
  bounce(map, e); // Reset all key(s) after 1 second idle.
237
259
  }
238
260
 
239
- function onKeyUp(e) {
240
- var $ = this,
261
+ function onKeyUpOrPutUp(e) {
262
+ var map = getReference(this),
263
+ data = e.data,
264
+ inputType = e.inputType,
241
265
  key = e.key,
242
- map = getReference($);
243
- key && map.pull(key); // Reset current key.
244
- }
245
- // Partial mobile support
246
- function onPutDown(e) {
247
- var $ = this,
248
- key = e.data,
249
- map = getReference($);
250
- if (isString(key) && 1 === toCount(key)) {
251
- // Having 1 printable character to put will discard the other(s)
252
- map.toArray().forEach(function (k) {
253
- return isString(k) && 1 === toCount(k) && map.pull(k);
254
- });
255
- // Put the current printable character to the list
256
- map.push(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
+ }
257
287
  }
258
288
  }
259
289
 
@@ -283,9 +313,10 @@
283
313
  });
284
314
  $.on('blur', onBlur);
285
315
  $.on('focus', onFocus);
286
- $.on('key.down', onKeyDown);
287
- $.on('key.up', onKeyUp);
288
- $.on('put.down', onPutDown);
316
+ $.on('key.down', onKeyDownOrPutDown);
317
+ $.on('key.up', onKeyUpOrPutUp);
318
+ $.on('put.down', onKeyDownOrPutDown);
319
+ $.on('put.up', onKeyUpOrPutUp);
289
320
  return setReference($, map), $;
290
321
  }
291
322
 
@@ -295,9 +326,10 @@
295
326
  map.pull();
296
327
  $.off('blur', onBlur);
297
328
  $.off('focus', onFocus);
298
- $.off('key.down', onKeyDown);
299
- $.off('key.up', onKeyUp);
300
- $.off('put.down', onPutDown);
329
+ $.off('key.down', onKeyDownOrPutDown);
330
+ $.off('key.up', onKeyUpOrPutUp);
331
+ $.off('put.down', onKeyDownOrPutDown);
332
+ $.off('put.up', onKeyUpOrPutUp);
301
333
  return letReference($), $;
302
334
  }
303
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,c=s.self||s;return u=u||[],n(e)||o(e)&&(e=s.commands[e])?(i=e.apply(c,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(c,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,c,a=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,c=a(o);s<c;++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=a(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;c&&clearTimeout(c),c=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){d.call(this,t)}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)}function K(t){var n=t.data,e=h(this);o(n)&&1===a(n)&&(e.toArray().forEach((function(t){return o(t)&&1===a(t)&&e.pull(t)})),e.push(n))}var g={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("focus",m),r.on("key.down",v),r.on("key.up",k),r.on("put.down",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("focus",m),n.off("key.down",v),n.off("key.up",k),n.off("put.down",K),t=n,p.delete(t),n},name:"TextEditor.Key"};return g}));
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
@@ -37,17 +37,34 @@ function onFocus(e) {
37
37
  onBlur.call(this, e);
38
38
  }
39
39
 
40
- function onKeyDown(e) {
41
- let $ = this, command, v,
42
- key = e.key,
43
- map = getReference($);
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);
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
+ }
51
68
  if (command = map.command()) {
52
69
  v = map.fire(command);
53
70
  if (false === v) {
@@ -60,23 +77,29 @@ function onKeyDown(e) {
60
77
  bounce(map, e); // Reset all key(s) after 1 second idle.
61
78
  }
62
79
 
63
- function onKeyUp(e) {
64
- let $ = this,
65
- key = e.key,
66
- map = getReference($);
67
- key && map.pull(key); // Reset current key.
68
- }
69
-
70
- // Partial mobile support
71
- function onPutDown(e) {
72
- let $ = this,
73
- key = e.data,
74
- map = getReference($);
75
- if (isString(key) && 1 === toCount(key)) {
76
- // Having 1 printable character to put will discard the other(s)
77
- map.toArray().forEach(k => isString(k) && 1 === toCount(k) && map.pull(k));
78
- // Put the current printable character to the list
79
- map.push(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
+ }
80
103
  }
81
104
  }
82
105
 
@@ -106,9 +129,10 @@ function attach() {
106
129
  });
107
130
  $.on('blur', onBlur);
108
131
  $.on('focus', onFocus);
109
- $.on('key.down', onKeyDown);
110
- $.on('key.up', onKeyUp);
111
- $.on('put.down', onPutDown);
132
+ $.on('key.down', onKeyDownOrPutDown);
133
+ $.on('key.up', onKeyUpOrPutUp);
134
+ $.on('put.down', onKeyDownOrPutDown);
135
+ $.on('put.up', onKeyUpOrPutUp);
112
136
  return setReference($, map), $;
113
137
  }
114
138
 
@@ -118,9 +142,10 @@ function detach() {
118
142
  map.pull();
119
143
  $.off('blur', onBlur);
120
144
  $.off('focus', onFocus);
121
- $.off('key.down', onKeyDown);
122
- $.off('key.up', onKeyUp);
123
- $.off('put.down', onPutDown);
145
+ $.off('key.down', onKeyDownOrPutDown);
146
+ $.off('key.up', onKeyUpOrPutUp);
147
+ $.off('put.down', onKeyDownOrPutDown);
148
+ $.off('put.up', onKeyUpOrPutUp);
124
149
  return letReference($), $;
125
150
  }
126
151
 
package/package.json CHANGED
@@ -47,5 +47,5 @@
47
47
  "scripts": {
48
48
  "pack": "pack --clean=false --from=.factory --js-format=umd --js-name=TextEditor.Key --js-top='%(js.license)' --mjs=true --to=."
49
49
  },
50
- "version": "1.0.11"
50
+ "version": "1.0.12"
51
51
  }