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

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
@@ -36,8 +36,12 @@
36
36
  var isFunction = function isFunction(x) {
37
37
  return 'function' === typeof x;
38
38
  };
39
- var isInstance = function isInstance(x, of) {
40
- return x && isSet(of) && x instanceof of ;
39
+ var isInstance = function isInstance(x, of, exact) {
40
+ if (!x || 'object' !== typeof x) {
41
+ return false;
42
+ } {
43
+ return isSet(of) && isSet(x.constructor) && of === x.constructor;
44
+ }
41
45
  };
42
46
  var isNull = function isNull(x) {
43
47
  return null === x;
@@ -46,7 +50,7 @@
46
50
  if (isPlain === void 0) {
47
51
  isPlain = true;
48
52
  }
49
- if ('object' !== typeof x) {
53
+ if (!x || 'object' !== typeof x) {
50
54
  return false;
51
55
  }
52
56
  return isPlain ? isInstance(x, Object) : true;
@@ -57,55 +61,14 @@
57
61
  var isString = function isString(x) {
58
62
  return 'string' === typeof x;
59
63
  };
60
- var hasValue = function hasValue(x, data) {
61
- return -1 !== data.indexOf(x);
62
- };
63
- var _fromStates = function fromStates() {
64
- for (var _len = arguments.length, lot = new Array(_len), _key = 0; _key < _len; _key++) {
65
- lot[_key] = arguments[_key];
66
- }
67
- var out = lot.shift();
68
- for (var i = 0, j = toCount(lot); i < j; ++i) {
69
- for (var k in lot[i]) {
70
- // Assign value
71
- if (!isSet(out[k])) {
72
- out[k] = lot[i][k];
73
- continue;
74
- }
75
- // Merge array
76
- if (isArray(out[k]) && isArray(lot[i][k])) {
77
- out[k] = [ /* Clone! */ ].concat(out[k]);
78
- for (var ii = 0, jj = toCount(lot[i][k]); ii < jj; ++ii) {
79
- if (!hasValue(lot[i][k][ii], out[k])) {
80
- out[k].push(lot[i][k][ii]);
81
- }
82
- }
83
- // Merge object recursive
84
- } else if (isObject(out[k]) && isObject(lot[i][k])) {
85
- out[k] = _fromStates({
86
- /* Clone! */ }, out[k], lot[i][k]);
87
- // Replace value
88
- } else {
89
- out[k] = lot[i][k];
90
- }
91
- }
92
- }
93
- return out;
94
- };
95
- var toCount = function toCount(x) {
96
- return x.length;
97
- };
98
- var toObjectKeys = function toObjectKeys(x) {
99
- return Object.keys(x);
100
- };
101
64
 
102
65
  function Key(self) {
103
66
  var $ = this;
104
67
  $.commands = {};
105
68
  $.key = null;
106
69
  $.keys = {};
107
- $.queue = {};
108
70
  $.self = self || $;
71
+ $.set = new Set();
109
72
  return $;
110
73
  }
111
74
  var $$ = Key.prototype;
@@ -141,16 +104,19 @@
141
104
  var $ = this;
142
105
  $.key = null;
143
106
  if (!isSet(key)) {
144
- return $.queue = {}, $;
107
+ return $.set = new Set(), $;
145
108
  }
146
- return delete $.queue[key], $;
109
+ return $.set.delete(key), $;
147
110
  };
148
111
  $$.push = function (key) {
149
112
  var $ = this;
150
- return $.queue[$.key = key] = 1, $;
113
+ return $.set.add($.key = key, 1), $;
114
+ };
115
+ $$.toArray = function () {
116
+ return Array.from(this.set);
151
117
  };
152
118
  $$.toString = function () {
153
- return toObjectKeys(this.queue).join('-');
119
+ return this.toArray().join('-');
154
120
  };
155
121
  Object.defineProperty(Key, 'name', {
156
122
  value: 'Key'
@@ -166,14 +132,57 @@
166
132
  }, time);
167
133
  };
168
134
  };
135
+ var hasValue = function hasValue(x, data) {
136
+ return -1 !== data.indexOf(x);
137
+ };
138
+ var toCount = function toCount(x) {
139
+ return x.length;
140
+ };
141
+ var _fromStates = function fromStates() {
142
+ for (var _len = arguments.length, lot = new Array(_len), _key = 0; _key < _len; _key++) {
143
+ lot[_key] = arguments[_key];
144
+ }
145
+ var out = lot.shift();
146
+ for (var i = 0, j = toCount(lot); i < j; ++i) {
147
+ for (var k in lot[i]) {
148
+ // Assign value
149
+ if (!isSet(out[k])) {
150
+ out[k] = lot[i][k];
151
+ continue;
152
+ }
153
+ // Merge array
154
+ if (isArray(out[k]) && isArray(lot[i][k])) {
155
+ out[k] = [ /* Clone! */ ].concat(out[k]);
156
+ for (var ii = 0, jj = toCount(lot[i][k]); ii < jj; ++ii) {
157
+ if (!hasValue(lot[i][k][ii], out[k])) {
158
+ out[k].push(lot[i][k][ii]);
159
+ }
160
+ }
161
+ // Merge object recursive
162
+ } else if (isObject(out[k]) && isObject(lot[i][k])) {
163
+ out[k] = _fromStates({
164
+ /* Clone! */ }, out[k], lot[i][k]);
165
+ // Replace value
166
+ } else {
167
+ out[k] = lot[i][k];
168
+ }
169
+ }
170
+ }
171
+ return out;
172
+ };
169
173
  var offEventDefault = function offEventDefault(e) {
170
174
  return e && e.preventDefault();
171
175
  };
172
176
  var offEventPropagation = function offEventPropagation(e) {
173
177
  return e && e.stopPropagation();
174
178
  };
175
- var bounce = debounce(function (map) {
176
- return map.pull();
179
+ var bounce = debounce(function (map, e) {
180
+ // Remove all keys
181
+ 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).
183
+ e.altKey && map.push('Alt');
184
+ e.ctrlKey && map.push('Control');
185
+ e.shiftKey && map.push('Shift');
177
186
  }, 1000);
178
187
  var name = 'TextEditor.Key';
179
188
  var references = new WeakMap();
@@ -202,7 +211,12 @@
202
211
  var command,
203
212
  map = getReference($),
204
213
  v;
205
- map.push(e.key); // Add current key to the queue
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
+ map[e.altKey ? 'push' : 'pull']('Alt');
216
+ map[e.ctrlKey ? 'push' : 'pull']('Control');
217
+ map[e.shiftKey ? 'push' : 'pull']('Shift');
218
+ // 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);
206
220
  $._event = e;
207
221
  if (command = map.command()) {
208
222
  v = map.fire(command);
@@ -213,14 +227,14 @@
213
227
  console.warn('Unknown command: `' + command + '`');
214
228
  }
215
229
  }
216
- bounce(map); // Reset all key(s) after 1 second idle
230
+ bounce(map, e); // Reset all key(s) after 1 second idle.
217
231
  }
218
232
 
219
233
  function onKeyUp(e) {
220
234
  var $ = this,
221
235
  map = getReference($);
222
236
  $._event = e;
223
- map.pull(e.key); // Reset current key
237
+ map.pull(e.key); // Reset current key.
224
238
  }
225
239
 
226
240
  function setReference(key, value) {
@@ -240,21 +254,8 @@
240
254
  !isFunction($$.k) && ($$.k = function (join) {
241
255
  var $ = this,
242
256
  map = getReference($),
243
- key = map + "",
244
- keys;
245
- if (isSet(join) && '-' !== join) {
246
- keys = "" !== key ? key.split(/-(?!$)/) : [];
247
- if (false !== join) {
248
- return keys.join(join);
249
- }
250
- }
251
- if (false === join) {
252
- if ('-' === key) {
253
- return [key];
254
- }
255
- return keys;
256
- }
257
- return key;
257
+ keys = map.toArray();
258
+ return false === join ? keys : keys.join(join || '-');
258
259
  });
259
260
  !isFunction($$.key) && ($$.key = function (key, of) {
260
261
  var $ = this;
package/index.min.js CHANGED
@@ -23,4 +23,4 @@
23
23
  * SOFTWARE.
24
24
  *
25
25
  */
26
- !function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):((n="undefined"!=typeof globalThis?globalThis:n||self).TextEditor=n.TextEditor||{},n.TextEditor.Key=t())}(this,(function(){"use strict";var n=function(n){return Array.isArray(n)},t=function(n){return"function"==typeof n},e=function(n,t){return void 0===t&&(t=!0),"object"==typeof n&&(!t||function(n,t){return n&&r(t)&&n instanceof t}(n,Object))},r=function(n){return function(n){return void 0!==n}(n)&&!function(n){return null===n}(n)},o=function(n){return"string"==typeof n},u=function(){for(var t=arguments.length,o=Array(t),f=0;f<t;f++)o[f]=arguments[f];for(var c,s=o.shift(),a=0,l=i(o);a<l;++a)for(var y in o[a])if(r(s[y]))if(n(s[y])&&n(o[a][y])){s[y]=[].concat(s[y]);for(var d=0,p=i(o[a][y]);d<p;++d)c=o[a][y][d],-1===s[y].indexOf(c)&&s[y].push(o[a][y][d])}else e(s[y])&&e(o[a][y])?s[y]=u({},s[y],o[a][y]):s[y]=o[a][y];else s[y]=o[a][y];return s},i=function(n){return n.length};function f(n){var t=this;return t.commands={},t.key=null,t.keys={},t.queue={},t.self=n||t,t}var c=f.prototype;c.command=function(n){var t=this;if(o(n))return n===t.toString();var e=t.keys[t.toString()];return!!r(e)&&e},c.fire=function(e){var u,i,f=this,c=f.self||f;if(t(e))u=e.call(c),i=!0;else if(o(e)&&(e=f.commands[e]))u=e.call(c),i=!0;else if(n(e)){var s=e[1]||[];(e=f.commands[e[0]])&&(u=e.apply(c,s),i=!0)}return i?!r(u)||u:null},c.pull=function(n){var t=this;return t.key=null,r(n)?(delete t.queue[n],t):(t.queue={},t)},c.push=function(n){var t=this;return t.queue[t.key=n]=1,t},c.toString=function(){return(n=this.queue,Object.keys(n)).join("-");var n},Object.defineProperty(f,"name",{value:"Key"});var s,a,l,y=(s=function(n){return n.pull()},a=1e3,function(){var n=arguments,t=this;l&&clearTimeout(l),l=setTimeout((function(){return s.apply(t,n)}),a)}),d=new WeakMap;function p(n){return d.get(n)||null}function m(n){var t=p(this);this._event=n,t.pull()}function h(n){m.call(this,n)}function v(n){var t,e,r=p(this);r.push(n.key),this._event=n,(t=r.command())&&(!1===(e=r.fire(t))?(function(n){n&&n.preventDefault()}(n),function(n){n&&n.stopPropagation()}(n)):null===e&&console.warn("Unknown command: `"+t+"`")),y(r)}function k(n){var t=p(this);this._event=n,t.pull(n.key)}var g={attach:function(){var n,e,o=this,i=o.constructor.prototype,c=new f(o);return o.commands=u(o.commands=c.commands,o.state.commands||{}),o.keys=u(o.keys=c.keys,o.state.keys||{}),!t(i.command)&&(i.command=function(n,t){return this.commands[n]=t,this}),!t(i.k)&&(i.k=function(n){var t,e=p(this)+"";return r(n)&&"-"!==n&&(t=""!==e?e.split(/-(?!$)/):[],!1!==n)?t.join(n):!1===n?"-"===e?[e]:t:e}),!t(i.key)&&(i.key=function(n,t){return this.keys[n]=t,this}),o.on("blur",m),o.on("input",h),o.on("key.down",v),o.on("key.up",k),n=o,e=c,d.set(n,e),o},detach:function(){var n,t=this;return p(t).pull(),t.off("blur",m),t.off("input",h),t.off("key.down",v),t.off("key.up",k),n=t,d.delete(n),t},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){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}));
package/index.mjs CHANGED
@@ -4,7 +4,15 @@ import {fromStates} from '@taufik-nurrohman/from';
4
4
  import {isFunction, isSet} from '@taufik-nurrohman/is';
5
5
  import {offEventDefault, offEventPropagation} from '@taufik-nurrohman/event';
6
6
 
7
- const bounce = debounce(map => map.pull(), 1000);
7
+ const bounce = debounce((map, e) => {
8
+ // Remove all keys
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).
11
+ e.altKey && map.push('Alt');
12
+ e.ctrlKey && map.push('Control');
13
+ e.shiftKey && map.push('Shift');
14
+ }, 1000);
15
+
8
16
  const name = 'TextEditor.Key';
9
17
  const references = new WeakMap;
10
18
 
@@ -30,7 +38,12 @@ function onInput(e) {
30
38
  function onKeyDown(e) {
31
39
  let $ = this;
32
40
  let command, map = getReference($), v;
33
- map.push(e.key); // Add current key to the queue
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
+ map[e.altKey ? 'push' : 'pull']('Alt');
43
+ map[e.ctrlKey ? 'push' : 'pull']('Control');
44
+ map[e.shiftKey ? 'push' : 'pull']('Shift');
45
+ // 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);
34
47
  $._event = e;
35
48
  if (command = map.command()) {
36
49
  v = map.fire(command);
@@ -41,14 +54,14 @@ function onKeyDown(e) {
41
54
  console.warn('Unknown command: `' + command + '`');
42
55
  }
43
56
  }
44
- bounce(map); // Reset all key(s) after 1 second idle
57
+ bounce(map, e); // Reset all key(s) after 1 second idle.
45
58
  }
46
59
 
47
60
  function onKeyUp(e) {
48
61
  let $ = this,
49
62
  map = getReference($);
50
63
  $._event = e;
51
- map.pull(e.key); // Reset current key
64
+ map.pull(e.key); // Reset current key.
52
65
  }
53
66
 
54
67
  function setReference(key, value) {
@@ -68,20 +81,8 @@ function attach() {
68
81
  !isFunction($$.k) && ($$.k = function (join) {
69
82
  let $ = this,
70
83
  map = getReference($),
71
- key = map + "", keys;
72
- if (isSet(join) && '-' !== join) {
73
- keys = "" !== key ? key.split(/-(?!$)/) : [];
74
- if (false !== join) {
75
- return keys.join(join);
76
- }
77
- }
78
- if (false === join) {
79
- if ('-' === key) {
80
- return [key];
81
- }
82
- return keys;
83
- }
84
- return key;
84
+ keys = map.toArray();
85
+ return false === join ? keys : keys.join(join || '-');
85
86
  });
86
87
  !isFunction($$.key) && ($$.key = function (key, of) {
87
88
  let $ = this;
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.8"
49
+ "version": "1.0.9"
50
50
  }