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

Sign up to get free protection for your applications and to get access to all the features.
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;
@@ -117,19 +80,24 @@
117
80
  var command = $.keys[$.toString()];
118
81
  return isSet(command) ? command : false;
119
82
  };
120
- $$.fire = function (command) {
83
+ $$.fire = function (command, data) {
121
84
  var $ = this;
122
85
  var self = $.self || $,
123
86
  value,
124
87
  exist;
88
+ data = data || [];
125
89
  if (isFunction(command)) {
126
- value = command.call(self);
90
+ value = command.apply(self, data);
127
91
  exist = true;
128
92
  } else if (isString(command) && (command = $.commands[command])) {
129
- value = command.call(self);
93
+ value = command.apply(self, data);
130
94
  exist = true;
131
95
  } else if (isArray(command)) {
132
- 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
+ }
133
101
  if (command = $.commands[command[0]]) {
134
102
  value = command.apply(self, data);
135
103
  exist = true;
@@ -141,16 +109,19 @@
141
109
  var $ = this;
142
110
  $.key = null;
143
111
  if (!isSet(key)) {
144
- return $.queue = {}, $;
112
+ return $.set = new Set(), $;
145
113
  }
146
- return delete $.queue[key], $;
114
+ return $.set.delete(key), $;
147
115
  };
148
116
  $$.push = function (key) {
149
117
  var $ = this;
150
- return $.queue[$.key = key] = 1, $;
118
+ return $.set.add($.key = key, 1), $;
119
+ };
120
+ $$.toArray = function () {
121
+ return Array.from(this.set);
151
122
  };
152
123
  $$.toString = function () {
153
- return toObjectKeys(this.queue).join('-');
124
+ return this.toArray().join('-');
154
125
  };
155
126
  Object.defineProperty(Key, 'name', {
156
127
  value: 'Key'
@@ -166,14 +137,58 @@
166
137
  }, time);
167
138
  };
168
139
  };
140
+ var hasValue = function hasValue(x, data) {
141
+ return -1 !== data.indexOf(x);
142
+ };
143
+ var toCount = function toCount(x) {
144
+ return x.length;
145
+ };
146
+ var _fromStates = function fromStates() {
147
+ for (var _len = arguments.length, lot = new Array(_len), _key = 0; _key < _len; _key++) {
148
+ lot[_key] = arguments[_key];
149
+ }
150
+ var out = lot.shift();
151
+ for (var i = 0, j = toCount(lot); i < j; ++i) {
152
+ for (var k in lot[i]) {
153
+ // Assign value
154
+ if (!isSet(out[k])) {
155
+ out[k] = lot[i][k];
156
+ continue;
157
+ }
158
+ // Merge array
159
+ if (isArray(out[k]) && isArray(lot[i][k])) {
160
+ out[k] = [ /* Clone! */ ].concat(out[k]);
161
+ for (var ii = 0, jj = toCount(lot[i][k]); ii < jj; ++ii) {
162
+ if (!hasValue(lot[i][k][ii], out[k])) {
163
+ out[k].push(lot[i][k][ii]);
164
+ }
165
+ }
166
+ // Merge object recursive
167
+ } else if (isObject(out[k]) && isObject(lot[i][k])) {
168
+ out[k] = _fromStates({
169
+ /* Clone! */ }, out[k], lot[i][k]);
170
+ // Replace value
171
+ } else {
172
+ out[k] = lot[i][k];
173
+ }
174
+ }
175
+ }
176
+ return out;
177
+ };
169
178
  var offEventDefault = function offEventDefault(e) {
170
179
  return e && e.preventDefault();
171
180
  };
172
181
  var offEventPropagation = function offEventPropagation(e) {
173
182
  return e && e.stopPropagation();
174
183
  };
175
- var bounce = debounce(function (map) {
176
- return map.pull();
184
+ var bounce = debounce(function (map, e) {
185
+ // Remove all key(s)
186
+ map.pull();
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).
188
+ e.altKey && map.push('Alt');
189
+ e.ctrlKey && map.push('Control');
190
+ e.metaKey && map.push('Meta');
191
+ e.shiftKey && map.push('Shift');
177
192
  }, 1000);
178
193
  var name = 'TextEditor.Key';
179
194
  var references = new WeakMap();
@@ -189,38 +204,46 @@
189
204
  function onBlur(e) {
190
205
  var $ = this,
191
206
  map = getReference($);
192
- $._event = e;
193
207
  map.pull(); // Reset all key(s)
194
208
  }
195
209
 
196
210
  function onInput(e) {
197
- onBlur.call(this, e);
211
+ var $ = this,
212
+ key = e.data,
213
+ map = getReference($);
214
+ key && map.pull(key);
198
215
  }
199
216
 
200
217
  function onKeyDown(e) {
201
- var $ = this;
202
- var command,
203
- map = getReference($),
204
- v;
205
- map.push(e.key); // Add current key to the queue
206
- $._event = e;
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).
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);
207
230
  if (command = map.command()) {
208
231
  v = map.fire(command);
209
232
  if (false === v) {
210
233
  offEventDefault(e);
211
234
  offEventPropagation(e);
212
235
  } else if (null === v) {
213
- console.warn('Unknown command: `' + command + '`');
236
+ console.warn('Unknown command:', command);
214
237
  }
215
238
  }
216
- bounce(map); // Reset all key(s) after 1 second idle
239
+ bounce(map, e); // Reset all key(s) after 1 second idle.
217
240
  }
218
241
 
219
242
  function onKeyUp(e) {
220
243
  var $ = this,
244
+ key = e.key,
221
245
  map = getReference($);
222
- $._event = e;
223
- map.pull(e.key); // Reset current key
246
+ key && map.pull(key); // Reset current key.
224
247
  }
225
248
 
226
249
  function setReference(key, value) {
@@ -229,7 +252,7 @@
229
252
 
230
253
  function attach() {
231
254
  var $ = this;
232
- var $$ = $.constructor.prototype;
255
+ var $$ = $.constructor._;
233
256
  var map = new Key($);
234
257
  $.commands = _fromStates($.commands = map.commands, $.state.commands || {});
235
258
  $.keys = _fromStates($.keys = map.keys, $.state.keys || {});
@@ -240,21 +263,8 @@
240
263
  !isFunction($$.k) && ($$.k = function (join) {
241
264
  var $ = this,
242
265
  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;
266
+ keys = map.toArray();
267
+ return false === join ? keys : keys.join(join || '-');
258
268
  });
259
269
  !isFunction($$.key) && ($$.key = function (key, of) {
260
270
  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,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
@@ -4,7 +4,16 @@ 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 key(s)
9
+ map.pull();
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
+ e.altKey && map.push('Alt');
12
+ e.ctrlKey && map.push('Control');
13
+ e.metaKey && map.push('Meta');
14
+ e.shiftKey && map.push('Shift');
15
+ }, 1000);
16
+
8
17
  const name = 'TextEditor.Key';
9
18
  const references = new WeakMap;
10
19
 
@@ -19,36 +28,44 @@ function letReference(key) {
19
28
  function onBlur(e) {
20
29
  let $ = this,
21
30
  map = getReference($);
22
- $._event = e;
23
31
  map.pull(); // Reset all key(s)
24
32
  }
25
33
 
26
34
  function onInput(e) {
27
- onBlur.call(this, e);
35
+ let $ = this,
36
+ key = e.data,
37
+ map = getReference($);
38
+ key && map.pull(key);
28
39
  }
29
40
 
30
41
  function onKeyDown(e) {
31
- let $ = this;
32
- let command, map = getReference($), v;
33
- map.push(e.key); // Add current key to the queue
34
- $._event = 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);
35
52
  if (command = map.command()) {
36
53
  v = map.fire(command);
37
54
  if (false === v) {
38
55
  offEventDefault(e);
39
56
  offEventPropagation(e);
40
57
  } else if (null === v) {
41
- console.warn('Unknown command: `' + command + '`');
58
+ console.warn('Unknown command:', command);
42
59
  }
43
60
  }
44
- bounce(map); // Reset all key(s) after 1 second idle
61
+ bounce(map, e); // Reset all key(s) after 1 second idle.
45
62
  }
46
63
 
47
64
  function onKeyUp(e) {
48
65
  let $ = this,
66
+ key = e.key,
49
67
  map = getReference($);
50
- $._event = e;
51
- map.pull(e.key); // Reset current key
68
+ key && map.pull(key); // Reset current key.
52
69
  }
53
70
 
54
71
  function setReference(key, value) {
@@ -57,7 +74,7 @@ function setReference(key, value) {
57
74
 
58
75
  function attach() {
59
76
  const $ = this;
60
- const $$ = $.constructor.prototype;
77
+ const $$ = $.constructor._;
61
78
  const map = new Key($);
62
79
  $.commands = fromStates($.commands = map.commands, $.state.commands || {});
63
80
  $.keys = fromStates($.keys = map.keys, $.state.keys || {});
@@ -68,20 +85,8 @@ function attach() {
68
85
  !isFunction($$.k) && ($$.k = function (join) {
69
86
  let $ = this,
70
87
  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;
88
+ keys = map.toArray();
89
+ return false === join ? keys : keys.join(join || '-');
85
90
  });
86
91
  !isFunction($$.key) && ($$.key = function (key, of) {
87
92
  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.10"
50
50
  }