@taufik-nurrohman/text-editor.key 1.0.7 → 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -2
- package/index.js +31 -16
- package/index.min.js +1 -1
- package/index.mjs +26 -13
- package/package.json +1 -1
package/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
[Text Editor](https://github.com/taufik-nurrohman/text-editor) » Key
|
2
2
|
==========================================================================
|
3
3
|
|
4
|
-
|
5
|
-
easily interact with the keyboard keys.
|
4
|
+
This extension provides a feature to easily interact with the keyboard keys.
|
6
5
|
|
7
6
|
![index.js](https://img.shields.io/github/size/taufik-nurrohman/text-editor.key/index.js?branch=main&color=%23f1e05a&label=index.js&labelColor=%231f2328&style=flat-square)
|
8
7
|
![index.min.js](https://img.shields.io/github/size/taufik-nurrohman/text-editor.key/index.min.js?branch=main&color=%23f1e05a&label=index.min.js&labelColor=%231f2328&style=flat-square)
|
package/index.js
CHANGED
@@ -60,7 +60,7 @@
|
|
60
60
|
var hasValue = function hasValue(x, data) {
|
61
61
|
return -1 !== data.indexOf(x);
|
62
62
|
};
|
63
|
-
var
|
63
|
+
var _fromStates = function fromStates() {
|
64
64
|
for (var _len = arguments.length, lot = new Array(_len), _key = 0; _key < _len; _key++) {
|
65
65
|
lot[_key] = arguments[_key];
|
66
66
|
}
|
@@ -82,7 +82,7 @@
|
|
82
82
|
}
|
83
83
|
// Merge object recursive
|
84
84
|
} else if (isObject(out[k]) && isObject(lot[i][k])) {
|
85
|
-
out[k] =
|
85
|
+
out[k] = _fromStates({
|
86
86
|
/* Clone! */ }, out[k], lot[i][k]);
|
87
87
|
// Replace value
|
88
88
|
} else {
|
@@ -176,12 +176,21 @@
|
|
176
176
|
return map.pull();
|
177
177
|
}, 1000);
|
178
178
|
var name = 'TextEditor.Key';
|
179
|
-
var
|
179
|
+
var references = new WeakMap();
|
180
|
+
|
181
|
+
function getReference(key) {
|
182
|
+
return references.get(key) || null;
|
183
|
+
}
|
184
|
+
|
185
|
+
function letReference(key) {
|
186
|
+
return references.delete(key);
|
187
|
+
}
|
180
188
|
|
181
189
|
function onBlur(e) {
|
182
|
-
var $ = this
|
190
|
+
var $ = this,
|
191
|
+
map = getReference($);
|
183
192
|
$._event = e;
|
184
|
-
|
193
|
+
map.pull(); // Reset all key(s)
|
185
194
|
}
|
186
195
|
|
187
196
|
function onInput(e) {
|
@@ -191,7 +200,7 @@
|
|
191
200
|
function onKeyDown(e) {
|
192
201
|
var $ = this;
|
193
202
|
var command,
|
194
|
-
map = $
|
203
|
+
map = getReference($),
|
195
204
|
v;
|
196
205
|
map.push(e.key); // Add current key to the queue
|
197
206
|
$._event = e;
|
@@ -208,24 +217,30 @@
|
|
208
217
|
}
|
209
218
|
|
210
219
|
function onKeyUp(e) {
|
211
|
-
var $ = this
|
220
|
+
var $ = this,
|
221
|
+
map = getReference($);
|
212
222
|
$._event = e;
|
213
|
-
|
223
|
+
map.pull(e.key); // Reset current key
|
224
|
+
}
|
225
|
+
|
226
|
+
function setReference(key, value) {
|
227
|
+
return references.set(key, value);
|
214
228
|
}
|
215
229
|
|
216
230
|
function attach() {
|
217
231
|
var $ = this;
|
218
232
|
var $$ = $.constructor.prototype;
|
219
233
|
var map = new Key($);
|
220
|
-
$.commands =
|
221
|
-
$.keys =
|
234
|
+
$.commands = _fromStates($.commands = map.commands, $.state.commands || {});
|
235
|
+
$.keys = _fromStates($.keys = map.keys, $.state.keys || {});
|
222
236
|
!isFunction($$.command) && ($$.command = function (command, of) {
|
223
237
|
var $ = this;
|
224
238
|
return $.commands[command] = of, $;
|
225
239
|
});
|
226
240
|
!isFunction($$.k) && ($$.k = function (join) {
|
227
241
|
var $ = this,
|
228
|
-
|
242
|
+
map = getReference($),
|
243
|
+
key = map + "",
|
229
244
|
keys;
|
230
245
|
if (isSet(join) && '-' !== join) {
|
231
246
|
keys = "" !== key ? key.split(/-(?!$)/) : [];
|
@@ -249,18 +264,18 @@
|
|
249
264
|
$.on('input', onInput);
|
250
265
|
$.on('key.down', onKeyDown);
|
251
266
|
$.on('key.up', onKeyUp);
|
252
|
-
return
|
267
|
+
return setReference($, map), $;
|
253
268
|
}
|
254
269
|
|
255
270
|
function detach() {
|
256
|
-
var $ = this
|
257
|
-
|
271
|
+
var $ = this,
|
272
|
+
map = getReference($);
|
273
|
+
map.pull();
|
258
274
|
$.off('blur', onBlur);
|
259
275
|
$.off('input', onInput);
|
260
276
|
$.off('key.down', onKeyDown);
|
261
277
|
$.off('key.up', onKeyUp);
|
262
|
-
|
263
|
-
return $;
|
278
|
+
return letReference($), $;
|
264
279
|
}
|
265
280
|
var index_js = {
|
266
281
|
attach: attach,
|
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
|
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}));
|
package/index.mjs
CHANGED
@@ -6,13 +6,21 @@ import {offEventDefault, offEventPropagation} from '@taufik-nurrohman/event';
|
|
6
6
|
|
7
7
|
const bounce = debounce(map => map.pull(), 1000);
|
8
8
|
const name = 'TextEditor.Key';
|
9
|
+
const references = new WeakMap;
|
9
10
|
|
10
|
-
|
11
|
+
function getReference(key) {
|
12
|
+
return references.get(key) || null;
|
13
|
+
}
|
14
|
+
|
15
|
+
function letReference(key) {
|
16
|
+
return references.delete(key);
|
17
|
+
}
|
11
18
|
|
12
19
|
function onBlur(e) {
|
13
|
-
let $ = this
|
20
|
+
let $ = this,
|
21
|
+
map = getReference($);
|
14
22
|
$._event = e;
|
15
|
-
|
23
|
+
map.pull(); // Reset all key(s)
|
16
24
|
}
|
17
25
|
|
18
26
|
function onInput(e) {
|
@@ -21,7 +29,7 @@ function onInput(e) {
|
|
21
29
|
|
22
30
|
function onKeyDown(e) {
|
23
31
|
let $ = this;
|
24
|
-
let command, map = $
|
32
|
+
let command, map = getReference($), v;
|
25
33
|
map.push(e.key); // Add current key to the queue
|
26
34
|
$._event = e;
|
27
35
|
if (command = map.command()) {
|
@@ -37,9 +45,14 @@ function onKeyDown(e) {
|
|
37
45
|
}
|
38
46
|
|
39
47
|
function onKeyUp(e) {
|
40
|
-
let $ = this
|
48
|
+
let $ = this,
|
49
|
+
map = getReference($);
|
41
50
|
$._event = e;
|
42
|
-
|
51
|
+
map.pull(e.key); // Reset current key
|
52
|
+
}
|
53
|
+
|
54
|
+
function setReference(key, value) {
|
55
|
+
return references.set(key, value);
|
43
56
|
}
|
44
57
|
|
45
58
|
function attach() {
|
@@ -54,8 +67,8 @@ function attach() {
|
|
54
67
|
});
|
55
68
|
!isFunction($$.k) && ($$.k = function (join) {
|
56
69
|
let $ = this,
|
57
|
-
|
58
|
-
keys;
|
70
|
+
map = getReference($),
|
71
|
+
key = map + "", keys;
|
59
72
|
if (isSet(join) && '-' !== join) {
|
60
73
|
keys = "" !== key ? key.split(/-(?!$)/) : [];
|
61
74
|
if (false !== join) {
|
@@ -78,18 +91,18 @@ function attach() {
|
|
78
91
|
$.on('input', onInput);
|
79
92
|
$.on('key.down', onKeyDown);
|
80
93
|
$.on('key.up', onKeyUp);
|
81
|
-
return (
|
94
|
+
return setReference($, map), $;
|
82
95
|
}
|
83
96
|
|
84
97
|
function detach() {
|
85
|
-
let $ = this
|
86
|
-
|
98
|
+
let $ = this,
|
99
|
+
map = getReference($);
|
100
|
+
map.pull();
|
87
101
|
$.off('blur', onBlur);
|
88
102
|
$.off('input', onInput);
|
89
103
|
$.off('key.down', onKeyDown);
|
90
104
|
$.off('key.up', onKeyUp);
|
91
|
-
|
92
|
-
return $;
|
105
|
+
return letReference($), $;
|
93
106
|
}
|
94
107
|
|
95
108
|
export default {attach, detach, name};
|
package/package.json
CHANGED