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

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
@@ -80,19 +80,24 @@
80
80
  var command = $.keys[$.toString()];
81
81
  return isSet(command) ? command : false;
82
82
  };
83
- $$.fire = function (command) {
83
+ $$.fire = function (command, data) {
84
84
  var $ = this;
85
85
  var self = $.self || $,
86
86
  value,
87
87
  exist;
88
+ data = data || [];
88
89
  if (isFunction(command)) {
89
- value = command.call(self);
90
+ value = command.apply(self, data);
90
91
  exist = true;
91
92
  } else if (isString(command) && (command = $.commands[command])) {
92
- value = command.call(self);
93
+ value = command.apply(self, data);
93
94
  exist = true;
94
95
  } else if (isArray(command)) {
95
- var data = command[1] || [];
96
+ if (isArray(command[1])) {
97
+ command[1].forEach(function (v, k) {
98
+ return isSet(v) && (data[k] = v);
99
+ });
100
+ }
96
101
  if (command = $.commands[command[0]]) {
97
102
  value = command.apply(self, data);
98
103
  exist = true;
@@ -177,11 +182,12 @@
177
182
  return e && e.stopPropagation();
178
183
  };
179
184
  var bounce = debounce(function (map, e) {
180
- // Remove all keys
185
+ // Remove all key(s)
181
186
  map.pull();
182
- // Make the `Alt`, `Control`, and `Shift` keys sticky (does not require the user to release all keys first to repeat or change the current key combination).
187
+ // 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).
183
188
  e.altKey && map.push('Alt');
184
189
  e.ctrlKey && map.push('Control');
190
+ e.metaKey && map.push('Meta');
185
191
  e.shiftKey && map.push('Shift');
186
192
  }, 1000);
187
193
  var name = 'TextEditor.Key';
@@ -198,33 +204,33 @@
198
204
  function onBlur(e) {
199
205
  var $ = this,
200
206
  map = getReference($);
201
- $._event = e;
202
207
  map.pull(); // Reset all key(s)
203
208
  }
204
209
 
205
- function onInput(e) {
210
+ function onFocus(e) {
206
211
  onBlur.call(this, e);
207
212
  }
208
213
 
209
214
  function onKeyDown(e) {
210
- var $ = this;
211
- var command,
212
- map = getReference($),
213
- v;
214
- // Make the `Alt`, `Control`, and `Shift` keys sticky (does not require the user to release all keys first to repeat or change the current key combination).
215
+ var $ = this,
216
+ command,
217
+ v,
218
+ 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).
215
221
  map[e.altKey ? 'push' : 'pull']('Alt');
216
222
  map[e.ctrlKey ? 'push' : 'pull']('Control');
223
+ map[e.metaKey ? 'push' : 'pull']('Meta');
217
224
  map[e.shiftKey ? 'push' : 'pull']('Shift');
218
225
  // Add the actual key to the queue. Don’t worry, this will not mistakenly add a key that already exists in the queue.
219
- map.push(e.key);
220
- $._event = e;
226
+ key && map.push(key);
221
227
  if (command = map.command()) {
222
228
  v = map.fire(command);
223
229
  if (false === v) {
224
230
  offEventDefault(e);
225
231
  offEventPropagation(e);
226
232
  } else if (null === v) {
227
- console.warn('Unknown command: `' + command + '`');
233
+ console.warn('Unknown command:', command);
228
234
  }
229
235
  }
230
236
  bounce(map, e); // Reset all key(s) after 1 second idle.
@@ -232,9 +238,23 @@
232
238
 
233
239
  function onKeyUp(e) {
234
240
  var $ = this,
241
+ key = e.key,
235
242
  map = getReference($);
236
- $._event = e;
237
- map.pull(e.key); // Reset current key.
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);
257
+ }
238
258
  }
239
259
 
240
260
  function setReference(key, value) {
@@ -243,7 +263,7 @@
243
263
 
244
264
  function attach() {
245
265
  var $ = this;
246
- var $$ = $.constructor.prototype;
266
+ var $$ = $.constructor._;
247
267
  var map = new Key($);
248
268
  $.commands = _fromStates($.commands = map.commands, $.state.commands || {});
249
269
  $.keys = _fromStates($.keys = map.keys, $.state.keys || {});
@@ -262,9 +282,10 @@
262
282
  return $.keys[key] = of, $;
263
283
  });
264
284
  $.on('blur', onBlur);
265
- $.on('input', onInput);
285
+ $.on('focus', onFocus);
266
286
  $.on('key.down', onKeyDown);
267
287
  $.on('key.up', onKeyUp);
288
+ $.on('put.down', onPutDown);
268
289
  return setReference($, map), $;
269
290
  }
270
291
 
@@ -273,9 +294,10 @@
273
294
  map = getReference($);
274
295
  map.pull();
275
296
  $.off('blur', onBlur);
276
- $.off('input', onInput);
297
+ $.off('focus', onFocus);
277
298
  $.off('key.down', onKeyDown);
278
299
  $.off('key.up', onKeyUp);
300
+ $.off('put.down', onPutDown);
279
301
  return letReference($), $;
280
302
  }
281
303
  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){var u,i,f=this,s=f.self||f;if(n(e))u=e.call(s),i=!0;else if(o(e)&&(e=f.commands[e]))u=e.call(s),i=!0;else if(t(e)){var c=e[1]||[];(e=f.commands[e[0]])&&(u=e.apply(s,c),i=!0)}return i?!r(u)||u: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.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){var n=h(this);this._event=t,n.pull()}function m(t){d.call(this,t)}function v(t){var n,e,r=h(this);r[t.altKey?"push":"pull"]("Alt"),r[t.ctrlKey?"push":"pull"]("Control"),r[t.shiftKey?"push":"pull"]("Shift"),r.push(t.key),this._event=t,(n=r.command())&&(!1===(e=r.fire(n))?(function(t){t&&t.preventDefault()}(t),function(t){t&&t.stopPropagation()}(t)):null===e&&console.warn("Unknown command: `"+n+"`")),y(r,t)}function k(t){var n=h(this);this._event=t,n.pull(t.key)}var g={attach:function(){var t,e,r=this,o=r.constructor.prototype,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 g}));
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}));
package/index.mjs CHANGED
@@ -1,15 +1,18 @@
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
- // Remove all keys
10
+ // Remove all key(s)
9
11
  map.pull();
10
- // Make the `Alt`, `Control`, and `Shift` keys sticky (does not require the user to release all keys first to repeat or change the current key combination).
12
+ // 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).
11
13
  e.altKey && map.push('Alt');
12
14
  e.ctrlKey && map.push('Control');
15
+ e.metaKey && map.push('Meta');
13
16
  e.shiftKey && map.push('Shift');
14
17
  }, 1000);
15
18
 
@@ -27,31 +30,31 @@ function letReference(key) {
27
30
  function onBlur(e) {
28
31
  let $ = this,
29
32
  map = getReference($);
30
- $._event = e;
31
33
  map.pull(); // Reset all key(s)
32
34
  }
33
35
 
34
- function onInput(e) {
36
+ function onFocus(e) {
35
37
  onBlur.call(this, e);
36
38
  }
37
39
 
38
40
  function onKeyDown(e) {
39
- let $ = this;
40
- let command, map = getReference($), v;
41
- // Make the `Alt`, `Control`, and `Shift` keys sticky (does not require the user to release all keys first to repeat or change the current key combination).
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).
42
45
  map[e.altKey ? 'push' : 'pull']('Alt');
43
46
  map[e.ctrlKey ? 'push' : 'pull']('Control');
47
+ map[e.metaKey ? 'push' : 'pull']('Meta');
44
48
  map[e.shiftKey ? 'push' : 'pull']('Shift');
45
49
  // Add the actual key to the queue. Don’t worry, this will not mistakenly add a key that already exists in the queue.
46
- map.push(e.key);
47
- $._event = e;
50
+ key && map.push(key);
48
51
  if (command = map.command()) {
49
52
  v = map.fire(command);
50
53
  if (false === v) {
51
54
  offEventDefault(e);
52
55
  offEventPropagation(e);
53
56
  } else if (null === v) {
54
- console.warn('Unknown command: `' + command + '`');
57
+ console.warn('Unknown command:', command);
55
58
  }
56
59
  }
57
60
  bounce(map, e); // Reset all key(s) after 1 second idle.
@@ -59,9 +62,22 @@ function onKeyDown(e) {
59
62
 
60
63
  function onKeyUp(e) {
61
64
  let $ = this,
65
+ key = e.key,
62
66
  map = getReference($);
63
- $._event = e;
64
- map.pull(e.key); // Reset current key.
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
+ }
65
81
  }
66
82
 
67
83
  function setReference(key, value) {
@@ -70,7 +86,7 @@ function setReference(key, value) {
70
86
 
71
87
  function attach() {
72
88
  const $ = this;
73
- const $$ = $.constructor.prototype;
89
+ const $$ = $.constructor._;
74
90
  const map = new Key($);
75
91
  $.commands = fromStates($.commands = map.commands, $.state.commands || {});
76
92
  $.keys = fromStates($.keys = map.keys, $.state.keys || {});
@@ -89,9 +105,10 @@ function attach() {
89
105
  return ($.keys[key] = of), $;
90
106
  });
91
107
  $.on('blur', onBlur);
92
- $.on('input', onInput);
108
+ $.on('focus', onFocus);
93
109
  $.on('key.down', onKeyDown);
94
110
  $.on('key.up', onKeyUp);
111
+ $.on('put.down', onPutDown);
95
112
  return setReference($, map), $;
96
113
  }
97
114
 
@@ -100,9 +117,10 @@ function detach() {
100
117
  map = getReference($);
101
118
  map.pull();
102
119
  $.off('blur', onBlur);
103
- $.off('input', onInput);
120
+ $.off('focus', onFocus);
104
121
  $.off('key.down', onKeyDown);
105
122
  $.off('key.up', onKeyUp);
123
+ $.off('put.down', onPutDown);
106
124
  return letReference($), $;
107
125
  }
108
126
 
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.9"
50
+ "version": "1.0.11"
50
51
  }