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

Sign up to get free protection for your applications and to get access to all the features.
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,36 @@
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
210
  function onInput(e) {
206
- onBlur.call(this, e);
211
+ var $ = this,
212
+ key = e.data,
213
+ map = getReference($);
214
+ key && map.pull(key);
207
215
  }
208
216
 
209
217
  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).
218
+ var $ = this,
219
+ command,
220
+ v,
221
+ 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).
215
224
  map[e.altKey ? 'push' : 'pull']('Alt');
216
225
  map[e.ctrlKey ? 'push' : 'pull']('Control');
226
+ map[e.metaKey ? 'push' : 'pull']('Meta');
217
227
  map[e.shiftKey ? 'push' : 'pull']('Shift');
218
228
  // 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;
229
+ key && map.push(key);
221
230
  if (command = map.command()) {
222
231
  v = map.fire(command);
223
232
  if (false === v) {
224
233
  offEventDefault(e);
225
234
  offEventPropagation(e);
226
235
  } else if (null === v) {
227
- console.warn('Unknown command: `' + command + '`');
236
+ console.warn('Unknown command:', command);
228
237
  }
229
238
  }
230
239
  bounce(map, e); // Reset all key(s) after 1 second idle.
@@ -232,9 +241,9 @@
232
241
 
233
242
  function onKeyUp(e) {
234
243
  var $ = this,
244
+ key = e.key,
235
245
  map = getReference($);
236
- $._event = e;
237
- map.pull(e.key); // Reset current key.
246
+ key && map.pull(key); // Reset current key.
238
247
  }
239
248
 
240
249
  function setReference(key, value) {
@@ -243,7 +252,7 @@
243
252
 
244
253
  function attach() {
245
254
  var $ = this;
246
- var $$ = $.constructor.prototype;
255
+ var $$ = $.constructor._;
247
256
  var map = new Key($);
248
257
  $.commands = _fromStates($.commands = map.commands, $.state.commands || {});
249
258
  $.keys = _fromStates($.keys = map.keys, $.state.keys || {});
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,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}));
package/index.mjs CHANGED
@@ -5,11 +5,12 @@ import {isFunction, isSet} from '@taufik-nurrohman/is';
5
5
  import {offEventDefault, offEventPropagation} from '@taufik-nurrohman/event';
6
6
 
7
7
  const bounce = debounce((map, e) => {
8
- // Remove all keys
8
+ // Remove all key(s)
9
9
  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).
10
+ // 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
11
  e.altKey && map.push('Alt');
12
12
  e.ctrlKey && map.push('Control');
13
+ e.metaKey && map.push('Meta');
13
14
  e.shiftKey && map.push('Shift');
14
15
  }, 1000);
15
16
 
@@ -27,31 +28,34 @@ function letReference(key) {
27
28
  function onBlur(e) {
28
29
  let $ = this,
29
30
  map = getReference($);
30
- $._event = e;
31
31
  map.pull(); // Reset all key(s)
32
32
  }
33
33
 
34
34
  function onInput(e) {
35
- onBlur.call(this, e);
35
+ let $ = this,
36
+ key = e.data,
37
+ map = getReference($);
38
+ key && map.pull(key);
36
39
  }
37
40
 
38
41
  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).
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).
42
46
  map[e.altKey ? 'push' : 'pull']('Alt');
43
47
  map[e.ctrlKey ? 'push' : 'pull']('Control');
48
+ map[e.metaKey ? 'push' : 'pull']('Meta');
44
49
  map[e.shiftKey ? 'push' : 'pull']('Shift');
45
50
  // 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;
51
+ key && map.push(key);
48
52
  if (command = map.command()) {
49
53
  v = map.fire(command);
50
54
  if (false === v) {
51
55
  offEventDefault(e);
52
56
  offEventPropagation(e);
53
57
  } else if (null === v) {
54
- console.warn('Unknown command: `' + command + '`');
58
+ console.warn('Unknown command:', command);
55
59
  }
56
60
  }
57
61
  bounce(map, e); // Reset all key(s) after 1 second idle.
@@ -59,9 +63,9 @@ function onKeyDown(e) {
59
63
 
60
64
  function onKeyUp(e) {
61
65
  let $ = this,
66
+ key = e.key,
62
67
  map = getReference($);
63
- $._event = e;
64
- map.pull(e.key); // Reset current key.
68
+ key && map.pull(key); // Reset current key.
65
69
  }
66
70
 
67
71
  function setReference(key, value) {
@@ -70,7 +74,7 @@ function setReference(key, value) {
70
74
 
71
75
  function attach() {
72
76
  const $ = this;
73
- const $$ = $.constructor.prototype;
77
+ const $$ = $.constructor._;
74
78
  const map = new Key($);
75
79
  $.commands = fromStates($.commands = map.commands, $.state.commands || {});
76
80
  $.keys = fromStates($.keys = map.keys, $.state.keys || {});
package/package.json CHANGED
@@ -46,5 +46,5 @@
46
46
  "scripts": {
47
47
  "pack": "pack --clean=false --from=.factory --js-format=umd --js-name=TextEditor.Key --js-top='%(js.license)' --mjs=true --to=."
48
48
  },
49
- "version": "1.0.9"
49
+ "version": "1.0.10"
50
50
  }