@taufik-nurrohman/text-editor.key 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2024 Taufik Nurrohman <https://github.com/taufik-nurrohman>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the “Software”), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ Text Editor Key
2
+ ================
3
+
4
+ > Key extension for [Text Editor](https://github.com/taufik-nurrohman/text-editor).
5
+
6
+ Provides a feature to easily interact with the keyboard keys.
package/index.js ADDED
@@ -0,0 +1,241 @@
1
+ /*!
2
+ *
3
+ * The MIT License (MIT)
4
+ *
5
+ * Copyright © 2024 Taufik Nurrohman <https://github.com/taufik-nurrohman>
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the “Software”), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ *
25
+ */
26
+ (function (g, f) {
27
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = f() : typeof define === 'function' && define.amd ? define(f) : (g = typeof globalThis !== 'undefined' ? globalThis : g || self, (g.TextEditor = g.TextEditor || {}, g.TextEditor.Key = f()));
28
+ })(this, (function () {
29
+ 'use strict';
30
+ var isArray = function isArray(x) {
31
+ return Array.isArray(x);
32
+ };
33
+ var isDefined = function isDefined(x) {
34
+ return 'undefined' !== typeof x;
35
+ };
36
+ var isFunction = function isFunction(x) {
37
+ return 'function' === typeof x;
38
+ };
39
+ var isInstance = function isInstance(x, of) {
40
+ return x && isSet(of) && x instanceof of ;
41
+ };
42
+ var isNull = function isNull(x) {
43
+ return null === x;
44
+ };
45
+ var isObject = function isObject(x, isPlain) {
46
+ if (isPlain === void 0) {
47
+ isPlain = true;
48
+ }
49
+ if ('object' !== typeof x) {
50
+ return false;
51
+ }
52
+ return isPlain ? isInstance(x, Object) : true;
53
+ };
54
+ var isSet = function isSet(x) {
55
+ return isDefined(x) && !isNull(x);
56
+ };
57
+ var isString = function isString(x) {
58
+ return 'string' === typeof x;
59
+ };
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
+
102
+ function Key(self) {
103
+ var $ = this;
104
+ var queue = {};
105
+ $.command = function (v) {
106
+ if (isString(v)) {
107
+ return v === $.toString();
108
+ }
109
+ var command = $.keys[$.toString()];
110
+ return isSet(command) ? command : false;
111
+ };
112
+ $.commands = {};
113
+ $.fire = function (command) {
114
+ var self = $.self || $,
115
+ value,
116
+ exist;
117
+ if (isFunction(command)) {
118
+ value = command.call(self);
119
+ exist = true;
120
+ } else if (isString(command) && (command = $.commands[command])) {
121
+ value = command.call(self);
122
+ exist = true;
123
+ } else if (isArray(command)) {
124
+ var data = command[1] || [];
125
+ if (command = $.commands[command[0]]) {
126
+ value = command.apply(self, data);
127
+ exist = true;
128
+ }
129
+ }
130
+ return exist ? isSet(value) ? value : true : null;
131
+ };
132
+ $.key = null;
133
+ $.keys = {};
134
+ $.pull = function (key) {
135
+ $.key = null;
136
+ if (!isSet(key)) {
137
+ return queue = {}, $;
138
+ }
139
+ return delete queue[key], $;
140
+ };
141
+ $.push = function (key) {
142
+ return queue[$.key = key] = 1, $;
143
+ };
144
+ $.self = self;
145
+ $.toString = function () {
146
+ return toObjectKeys(queue).join('-');
147
+ };
148
+ return $;
149
+ }
150
+ var debounce = function debounce(then, time) {
151
+ var timer;
152
+ return function () {
153
+ var _arguments = arguments,
154
+ _this = this;
155
+ timer && clearTimeout(timer);
156
+ timer = setTimeout(function () {
157
+ return then.apply(_this, _arguments);
158
+ }, time);
159
+ };
160
+ };
161
+ var offEvent = function offEvent(name, node, then) {
162
+ node.removeEventListener(name, then);
163
+ };
164
+ var offEventDefault = function offEventDefault(e) {
165
+ return e && e.preventDefault();
166
+ };
167
+ var onEvent = function onEvent(name, node, then, options) {
168
+ if (options === void 0) {
169
+ options = false;
170
+ }
171
+ node.addEventListener(name, then, options);
172
+ };
173
+ var bounce = debounce(function (map) {
174
+ return map.pull();
175
+ }, 1000);
176
+
177
+ function onBlur(e) {
178
+ this.Key.pull(); // Reset all key(s)
179
+ }
180
+
181
+ function onInput(e) {
182
+ this.Key.pull(); // Reset all key(s)
183
+ }
184
+
185
+ function onKeyDown(e) {
186
+ var command,
187
+ map = this.Key,
188
+ v;
189
+ map.push(e.key); // Add current key to the queue
190
+ if (command = map.command()) {
191
+ v = map.fire(command);
192
+ if (false === v) {
193
+ offEventDefault(e);
194
+ } else if (null === v) {
195
+ console.warn('Unknown command: `' + command + '`');
196
+ }
197
+ }
198
+ bounce(map); // Reset all key(s) after 1 second idle
199
+ }
200
+
201
+ function onKeyUp(e) {
202
+ this.Key.pull(e.key); // Reset current key
203
+ }
204
+
205
+ function attach(self) {
206
+ var $ = this;
207
+ var map = new Key($);
208
+ $.command = function (command, of) {
209
+ return $.commands[command] = of, $;
210
+ };
211
+ $.commands = fromStates(map.commands, $.state.commands || {});
212
+ $.k = function () {
213
+ return map + "";
214
+ };
215
+ $.key = function (key, of) {
216
+ return $.keys[key] = of, $;
217
+ };
218
+ $.keys = fromStates(map.keys, $.state.keys || {});
219
+ onEvent('blur', self, onBlur);
220
+ onEvent('input', self, onInput);
221
+ onEvent('keydown', self, onKeyDown);
222
+ onEvent('keyup', self, onKeyUp);
223
+ self.Key = map;
224
+ }
225
+
226
+ function detach(self) {
227
+ var $ = this;
228
+ delete $.commands;
229
+ delete $.keys;
230
+ delete self.Key;
231
+ offEvent('blur', self, onBlur);
232
+ offEvent('input', self, onInput);
233
+ offEvent('keydown', self, onKeyDown);
234
+ offEvent('keyup', self, onKeyUp);
235
+ }
236
+ var index_js = {
237
+ attach: attach,
238
+ detach: detach
239
+ };
240
+ return index_js;
241
+ }));
package/index.min.js ADDED
@@ -0,0 +1,26 @@
1
+ /*!
2
+ *
3
+ * The MIT License (MIT)
4
+ *
5
+ * Copyright © 2024 Taufik Nurrohman <https://github.com/taufik-nurrohman>
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the “Software”), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ *
25
+ */
26
+ !function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):((n="undefined"!=typeof globalThis?globalThis:n||self).TextEditor=n.TextEditor||{},n.TextEditor.Key=e())}(this,(function(){"use strict";var n=function(n){return Array.isArray(n)},e=function(n){return"function"==typeof n},t=function(n,e){return void 0===e&&(e=!0),"object"==typeof n&&(!e||function(n,e){return n&&r(e)&&n instanceof e}(n,Object))},r=function(n){return function(n){return void 0!==n}(n)&&!function(n){return null===n}(n)},u=function(n){return"string"==typeof n},o=function e(){for(var u=arguments.length,o=Array(u),f=0;f<u;f++)o[f]=arguments[f];for(var c,s=o.shift(),l=0,a=i(o);l<a;++l)for(var d in o[l])if(r(s[d]))if(n(s[d])&&n(o[l][d])){s[d]=[].concat(s[d]);for(var y=0,m=i(o[l][d]);y<m;++y)c=o[l][d][y],-1===s[d].indexOf(c)&&s[d].push(o[l][d][y])}else t(s[d])&&t(o[l][d])?s[d]=e({},s[d],o[l][d]):s[d]=o[l][d];else s[d]=o[l][d];return s},i=function(n){return n.length},f=function(n){return Object.keys(n)};function c(t){var o=this,i={};return o.command=function(n){if(u(n))return n===o.toString();var e=o.keys[o.toString()];return!!r(e)&&e},o.commands={},o.fire=function(t){var i,f,c=o.self||o;if(e(t))i=t.call(c),f=!0;else if(u(t)&&(t=o.commands[t]))i=t.call(c),f=!0;else if(n(t)){var s=t[1]||[];(t=o.commands[t[0]])&&(i=t.apply(c,s),f=!0)}return f?!r(i)||i:null},o.key=null,o.keys={},o.pull=function(n){return o.key=null,r(n)?(delete i[n],o):(i={},o)},o.push=function(n){return i[o.key=n]=1,o},o.self=t,o.toString=function(){return f(i).join("-")},o}var s,l,a,d=function(n,e,t){e.removeEventListener(n,t)},y=function(n){return n&&n.preventDefault()},m=function(n,e,t,r){void 0===r&&(r=!1),e.addEventListener(n,t,r)},p=(s=function(n){return n.pull()},l=1e3,function(){var n=arguments,e=this;a&&clearTimeout(a),a=setTimeout((function(){return s.apply(e,n)}),l)});function h(n){this.Key.pull()}function k(n){this.Key.pull()}function v(n){var e,t,r=this.Key;r.push(n.key),(e=r.command())&&(!1===(t=r.fire(e))?y(n):null===t&&console.warn("Unknown command: `"+e+"`")),p(r)}function b(n){this.Key.pull(n.key)}return{attach:function(n){var e=this,t=new c(e);e.command=function(n,t){return e.commands[n]=t,e},e.commands=o(t.commands,e.state.commands||{}),e.k=function(){return t+""},e.key=function(n,t){return e.keys[n]=t,e},e.keys=o(t.keys,e.state.keys||{}),m("blur",n,h),m("input",n,k),m("keydown",n,v),m("keyup",n,b),n.Key=t},detach:function(n){delete this.commands,delete this.keys,delete n.Key,d("blur",n,h),d("input",n,k),d("keydown",n,v),d("keyup",n,b)}}}));
package/index.mjs ADDED
@@ -0,0 +1,60 @@
1
+ import Key from '@taufik-nurrohman/key';
2
+ import {debounce} from '@taufik-nurrohman/tick';
3
+ import {fromStates} from '@taufik-nurrohman/from';
4
+ import {onEvent, offEvent, offEventDefault} from '@taufik-nurrohman/event';
5
+
6
+ const bounce = debounce(map => map.pull(), 1000);
7
+
8
+ function onBlur(e) {
9
+ this.Key.pull(); // Reset all key(s)
10
+ }
11
+
12
+ function onInput(e) {
13
+ this.Key.pull(); // Reset all key(s)
14
+ }
15
+
16
+ function onKeyDown(e) {
17
+ let command, map = this.Key, v;
18
+ map.push(e.key); // Add current key to the queue
19
+ if (command = map.command()) {
20
+ v = map.fire(command);
21
+ if (false === v) {
22
+ offEventDefault(e);
23
+ } else if (null === v) {
24
+ console.warn('Unknown command: `' + command + '`');
25
+ }
26
+ }
27
+ bounce(map); // Reset all key(s) after 1 second idle
28
+ }
29
+
30
+ function onKeyUp(e) {
31
+ this.Key.pull(e.key); // Reset current key
32
+ }
33
+
34
+ function attach(self) {
35
+ let $ = this;
36
+ let map = new Key($);
37
+ $.command = (command, of) => (($.commands[command] = of), $);
38
+ $.commands = fromStates(map.commands, $.state.commands || {});
39
+ $.k = () => map + "";
40
+ $.key = (key, of) => (($.keys[key] = of), $);
41
+ $.keys = fromStates(map.keys, $.state.keys || {});
42
+ onEvent('blur', self, onBlur);
43
+ onEvent('input', self, onInput);
44
+ onEvent('keydown', self, onKeyDown);
45
+ onEvent('keyup', self, onKeyUp);
46
+ self.Key = map;
47
+ }
48
+
49
+ function detach(self) {
50
+ let $ = this;
51
+ delete $.commands;
52
+ delete $.keys;
53
+ delete self.Key;
54
+ offEvent('blur', self, onBlur);
55
+ offEvent('input', self, onInput);
56
+ offEvent('keydown', self, onKeyDown);
57
+ offEvent('keyup', self, onKeyUp);
58
+ }
59
+
60
+ export default {attach, detach};
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "author": "Taufik Nurrohman",
3
+ "browser": "index.min.js",
4
+ "bugs": "https://github.com/taufik-nurrohman/text-editor.key/issues",
5
+ "dependencies": {
6
+ "@taufik-nurrohman/event": "*",
7
+ "@taufik-nurrohman/from": "*",
8
+ "@taufik-nurrohman/key": "*",
9
+ "@taufik-nurrohman/text-editor": "*",
10
+ "@taufik-nurrohman/tick": "*"
11
+ },
12
+ "description": "Provides a feature to easily interact with the keyboard keys.",
13
+ "devDependencies": {
14
+ "@taufik-nurrohman/factory": "*"
15
+ },
16
+ "exports": {
17
+ "import": "./index.mjs",
18
+ "require": "./index.js"
19
+ },
20
+ "files": [
21
+ "index.js",
22
+ "index.min.js",
23
+ "index.mjs"
24
+ ],
25
+ "funding": {
26
+ "url": "https://paypal.me/tatautaufik"
27
+ },
28
+ "homepage": "https://taufik-nurrohman.js.org/text-editor.key",
29
+ "keywords": [
30
+ "command",
31
+ "editor",
32
+ "extension",
33
+ "key",
34
+ "keyboard",
35
+ "text"
36
+ ],
37
+ "license": "MIT",
38
+ "main": "index.js",
39
+ "module": "index.mjs",
40
+ "name": "@taufik-nurrohman/text-editor.key",
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/taufik-nurrohman/text-editor.key.git"
44
+ },
45
+ "scripts": {
46
+ "pack": "pack --clean=false --from=.factory --js-format=umd --js-name=TextEditor.Key --js-top='%(js.license)' --mjs=true --to=."
47
+ },
48
+ "version": "1.0.0"
49
+ }