@taufik-nurrohman/text-editor.key 1.0.6 → 1.0.8
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/README.md +1 -2
- package/index.js +34 -15
- package/index.min.js +1 -1
- package/index.mjs +29 -12
- 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
|

|
|
8
7
|

|
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,20 +176,31 @@
|
|
|
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
|
-
|
|
190
|
+
var $ = this,
|
|
191
|
+
map = getReference($);
|
|
192
|
+
$._event = e;
|
|
193
|
+
map.pull(); // Reset all key(s)
|
|
183
194
|
}
|
|
184
195
|
|
|
185
196
|
function onInput(e) {
|
|
186
|
-
onBlur.call(this);
|
|
197
|
+
onBlur.call(this, e);
|
|
187
198
|
}
|
|
188
199
|
|
|
189
200
|
function onKeyDown(e) {
|
|
190
201
|
var $ = this;
|
|
191
202
|
var command,
|
|
192
|
-
map = $
|
|
203
|
+
map = getReference($),
|
|
193
204
|
v;
|
|
194
205
|
map.push(e.key); // Add current key to the queue
|
|
195
206
|
$._event = e;
|
|
@@ -206,22 +217,30 @@
|
|
|
206
217
|
}
|
|
207
218
|
|
|
208
219
|
function onKeyUp(e) {
|
|
209
|
-
|
|
220
|
+
var $ = this,
|
|
221
|
+
map = getReference($);
|
|
222
|
+
$._event = e;
|
|
223
|
+
map.pull(e.key); // Reset current key
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function setReference(key, value) {
|
|
227
|
+
return references.set(key, value);
|
|
210
228
|
}
|
|
211
229
|
|
|
212
230
|
function attach() {
|
|
213
231
|
var $ = this;
|
|
214
232
|
var $$ = $.constructor.prototype;
|
|
215
233
|
var map = new Key($);
|
|
216
|
-
$.commands =
|
|
217
|
-
$.keys =
|
|
234
|
+
$.commands = _fromStates($.commands = map.commands, $.state.commands || {});
|
|
235
|
+
$.keys = _fromStates($.keys = map.keys, $.state.keys || {});
|
|
218
236
|
!isFunction($$.command) && ($$.command = function (command, of) {
|
|
219
237
|
var $ = this;
|
|
220
238
|
return $.commands[command] = of, $;
|
|
221
239
|
});
|
|
222
240
|
!isFunction($$.k) && ($$.k = function (join) {
|
|
223
241
|
var $ = this,
|
|
224
|
-
|
|
242
|
+
map = getReference($),
|
|
243
|
+
key = map + "",
|
|
225
244
|
keys;
|
|
226
245
|
if (isSet(join) && '-' !== join) {
|
|
227
246
|
keys = "" !== key ? key.split(/-(?!$)/) : [];
|
|
@@ -245,18 +264,18 @@
|
|
|
245
264
|
$.on('input', onInput);
|
|
246
265
|
$.on('key.down', onKeyDown);
|
|
247
266
|
$.on('key.up', onKeyUp);
|
|
248
|
-
return
|
|
267
|
+
return setReference($, map), $;
|
|
249
268
|
}
|
|
250
269
|
|
|
251
270
|
function detach() {
|
|
252
|
-
var $ = this
|
|
253
|
-
|
|
271
|
+
var $ = this,
|
|
272
|
+
map = getReference($);
|
|
273
|
+
map.pull();
|
|
254
274
|
$.off('blur', onBlur);
|
|
255
275
|
$.off('input', onInput);
|
|
256
276
|
$.off('key.down', onKeyDown);
|
|
257
277
|
$.off('key.up', onKeyUp);
|
|
258
|
-
|
|
259
|
-
return $;
|
|
278
|
+
return letReference($), $;
|
|
260
279
|
}
|
|
261
280
|
var index_js = {
|
|
262
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,20 +6,30 @@ 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
|
-
|
|
20
|
+
let $ = this,
|
|
21
|
+
map = getReference($);
|
|
22
|
+
$._event = e;
|
|
23
|
+
map.pull(); // Reset all key(s)
|
|
14
24
|
}
|
|
15
25
|
|
|
16
26
|
function onInput(e) {
|
|
17
|
-
onBlur.call(this);
|
|
27
|
+
onBlur.call(this, e);
|
|
18
28
|
}
|
|
19
29
|
|
|
20
30
|
function onKeyDown(e) {
|
|
21
31
|
let $ = this;
|
|
22
|
-
let command, map = $
|
|
32
|
+
let command, map = getReference($), v;
|
|
23
33
|
map.push(e.key); // Add current key to the queue
|
|
24
34
|
$._event = e;
|
|
25
35
|
if (command = map.command()) {
|
|
@@ -35,7 +45,14 @@ function onKeyDown(e) {
|
|
|
35
45
|
}
|
|
36
46
|
|
|
37
47
|
function onKeyUp(e) {
|
|
38
|
-
|
|
48
|
+
let $ = this,
|
|
49
|
+
map = getReference($);
|
|
50
|
+
$._event = e;
|
|
51
|
+
map.pull(e.key); // Reset current key
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function setReference(key, value) {
|
|
55
|
+
return references.set(key, value);
|
|
39
56
|
}
|
|
40
57
|
|
|
41
58
|
function attach() {
|
|
@@ -50,8 +67,8 @@ function attach() {
|
|
|
50
67
|
});
|
|
51
68
|
!isFunction($$.k) && ($$.k = function (join) {
|
|
52
69
|
let $ = this,
|
|
53
|
-
|
|
54
|
-
keys;
|
|
70
|
+
map = getReference($),
|
|
71
|
+
key = map + "", keys;
|
|
55
72
|
if (isSet(join) && '-' !== join) {
|
|
56
73
|
keys = "" !== key ? key.split(/-(?!$)/) : [];
|
|
57
74
|
if (false !== join) {
|
|
@@ -74,18 +91,18 @@ function attach() {
|
|
|
74
91
|
$.on('input', onInput);
|
|
75
92
|
$.on('key.down', onKeyDown);
|
|
76
93
|
$.on('key.up', onKeyUp);
|
|
77
|
-
return (
|
|
94
|
+
return setReference($, map), $;
|
|
78
95
|
}
|
|
79
96
|
|
|
80
97
|
function detach() {
|
|
81
|
-
let $ = this
|
|
82
|
-
|
|
98
|
+
let $ = this,
|
|
99
|
+
map = getReference($);
|
|
100
|
+
map.pull();
|
|
83
101
|
$.off('blur', onBlur);
|
|
84
102
|
$.off('input', onInput);
|
|
85
103
|
$.off('key.down', onKeyDown);
|
|
86
104
|
$.off('key.up', onKeyUp);
|
|
87
|
-
|
|
88
|
-
return $;
|
|
105
|
+
return letReference($), $;
|
|
89
106
|
}
|
|
90
107
|
|
|
91
108
|
export default {attach, detach, name};
|
package/package.json
CHANGED