Skip to content

Commit 1a26a6d

Browse files
committed
fix now working on latest acode
1 parent 026f9ef commit 1a26a6d

7 files changed

Lines changed: 189 additions & 30 deletions

File tree

.vscode/settings.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"cSpell.words": [
3+
"Acode",
4+
"onvolumndown",
5+
"onvolumnup",
6+
"volumedownbutton",
7+
"volumeupbutton",
8+
"volumncursor"
9+
]
10+
}

dist/main.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
2-
"name": "acode-plugin",
2+
"name": "acode-plugin-volumncursor",
33
"version": "1.0.1",
4-
"description": "Template for Acode plugin",
4+
"description": "Plugin for acode editor to move cursor using volume keys",
55
"main": "dist/main.js",
66
"repository": "https://github.com/deadlyjack/acode-plugin.git",
77
"author": "Ajit <me@ajitkumar.dev>",
88
"license": "MIT",
99
"dependencies": {
10-
"html-tag-js": "^1.1.6",
1110
"terser": ">=5.14.2 "
1211
},
1312
"devDependencies": {
@@ -31,4 +30,4 @@
3130
"resolutions": {
3231
"terser": ">=5.14.2 "
3332
}
34-
}
33+
}

plugin.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"id": "acode.plugin.volumncursor",
3-
"name": "Volumn cursor",
3+
"name": "Volume cursor",
44
"type": "free",
55
"main": "dist/main.js",
6-
"version": "1.0.1",
6+
"version": "1.0.2",
77
"readme": "readme.md",
88
"icon": "icon.png",
99
"files": [],

src/keyboardEvent.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
const initKeyboardEventType = (function (event) {
2+
try {
3+
event.initKeyboardEvent(
4+
"keyup" // in DOMString typeArg
5+
, false // in boolean canBubbleArg
6+
, false // in boolean cancelableArg
7+
, window // in views::AbstractView viewArg
8+
, "+" // [test]in DOMString keyIdentifierArg | webkit event.keyIdentifier | IE9 event.key
9+
, 3 // [test]in unsigned long keyLocationArg | webkit event.keyIdentifier | IE9 event.location
10+
, true // [test]in boolean ctrlKeyArg | webkit event.shiftKey | old webkit event.ctrlKey | IE9 event.modifiersList
11+
, false // [test]shift | alt
12+
, true // [test]shift | alt
13+
, false // meta
14+
, false // altGraphKey
15+
);
16+
17+
return ((event["keyIdentifier"] || event["key"]) == "+" && (event["location"]) || event["keyLocation"] == 3) && (
18+
event.ctrlKey ?
19+
event.altKey ? // webkit
20+
1 :
21+
3 :
22+
event.shiftKey ?
23+
2 : // webkit
24+
4 // IE9
25+
) || 9; // FireFox|w3c
26+
} catch (error) {
27+
initKeyboardEventType = 0
28+
}
29+
})(document.createEvent("KeyboardEvent"));
30+
31+
const keyboardEventPropertiesDictionary = {
32+
"char": "",
33+
"key": "",
34+
"location": 0,
35+
"ctrlKey": false,
36+
"shiftKey": false,
37+
"altKey": false,
38+
"metaKey": false,
39+
"repeat": false,
40+
"locale": "",
41+
42+
"detail": 0,
43+
"bubbles": false,
44+
"cancelable": false,
45+
46+
//legacy properties
47+
"keyCode": 0,
48+
"charCode": 0,
49+
"which": 0
50+
};
51+
52+
const own = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
53+
54+
const ObjectDefineProperty = Object.defineProperty || function (obj, prop, val) {
55+
if ("value" in val) {
56+
obj[prop] = val["value"];
57+
}
58+
};
59+
60+
export default function KeyboardEvent(type, dict) {
61+
let event;
62+
if (initKeyboardEventType) {
63+
event = document.createEvent("KeyboardEvent");
64+
} else {
65+
event = document.createEvent("Event");
66+
}
67+
68+
let propName;
69+
let localDict = {};
70+
71+
for (propName in keyboardEventPropertiesDictionary)
72+
if (own(keyboardEventPropertiesDictionary, propName)) {
73+
localDict[propName] = (own(dict, propName) && dict || keyboardEventPropertiesDictionary)[propName];
74+
}
75+
76+
const ctrlKey = localDict["ctrlKey"];
77+
const shiftKey = localDict["shiftKey"];
78+
const altKey = localDict["altKey"];
79+
const metaKey = localDict["metaKey"];
80+
const altGraphKey = localDict["altGraphKey"];
81+
82+
const modifiersListArg = initKeyboardEventType > 3 ? (
83+
(ctrlKey ? "Control" : "") +
84+
(shiftKey ? " Shift" : "") +
85+
(altKey ? " Alt" : "") +
86+
(metaKey ? " Meta" : "") +
87+
(altGraphKey ? " AltGraph" : "")
88+
).trim() : null
89+
90+
const key = localDict["key"] + "";
91+
const char = localDict["char"] + "";
92+
const location = localDict["location"];
93+
const keyCode = localDict["keyCode"] || (localDict["keyCode"] = key && key.charCodeAt(0) || 0);
94+
const charCode = localDict["charCode"] || (localDict["charCode"] = char && char.charCodeAt(0) || 0);
95+
const bubbles = localDict["bubbles"];
96+
const cancelable = localDict["cancelable"];
97+
const repeat = localDict["repeat"];
98+
const locale = localDict["locale"];
99+
const view = window;
100+
101+
localDict["which"] || (localDict["which"] = localDict["keyCode"]);
102+
103+
if ("initKeyEvent" in event) { //FF
104+
//https://developer.mozilla.org/en/DOM/event.initKeyEvent
105+
event.initKeyEvent(type, bubbles, cancelable, view, ctrlKey, altKey, shiftKey, metaKey, keyCode, charCode);
106+
} else if (initKeyboardEventType && "initKeyboardEvent" in event) { //https://developer.mozilla.org/en/DOM/KeyboardEvent#initKeyboardEvent()
107+
if (initKeyboardEventType == 1) { // webkit
108+
//http://stackoverflow.com/a/8490774/1437207
109+
//https://bugs.webkit.org/show_bug.cgi?id=13368
110+
event.initKeyboardEvent(type, bubbles, cancelable, view, key, location, ctrlKey, shiftKey, altKey, metaKey, altGraphKey);
111+
} else if (initKeyboardEventType == 2) { // old webkit
112+
//http://code.google.com/p/chromium/issues/detail?id=52408
113+
event.initKeyboardEvent(type, bubbles, cancelable, view, ctrlKey, altKey, shiftKey, metaKey, keyCode, charCode);
114+
} else if (initKeyboardEventType == 3) { // webkit
115+
event.initKeyboardEvent(type, bubbles, cancelable, view, key, location, ctrlKey, altKey, shiftKey, metaKey, altGraphKey);
116+
} else if (initKeyboardEventType == 4) { // IE9
117+
//http://msdn.microsoft.com/en-us/library/ie/ff975297(v=vs.85).aspx
118+
event.initKeyboardEvent(type, bubbles, cancelable, view, key, location, modifiersListArg, repeat, locale);
119+
} else { // FireFox|w3c
120+
//http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent-initKeyboardEvent
121+
//https://developer.mozilla.org/en/DOM/KeyboardEvent#initKeyboardEvent()
122+
event.initKeyboardEvent(type, bubbles, cancelable, view, char, key, location, modifiersListArg, repeat, locale);
123+
}
124+
} else {
125+
event.initEvent(type, bubbles, cancelable)
126+
}
127+
128+
for (propName in keyboardEventPropertiesDictionary)
129+
if (own(keyboardEventPropertiesDictionary, propName)) {
130+
if (event[propName] != localDict[propName]) {
131+
try {
132+
delete event[propName];
133+
ObjectDefineProperty(event, propName, {
134+
writable: true,
135+
"value": localDict[propName]
136+
});
137+
} catch (error) {
138+
//Some properties is read-only
139+
}
140+
141+
}
142+
}
143+
144+
return event;
145+
}

src/main.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import tag from 'html-tag-js';
21
import plugin from '../plugin.json';
2+
import KeyboardEvent from './keyboardEvent';
33

44
const appSettings = acode.require('settings');
55

@@ -20,39 +20,44 @@ class AcodePlugin {
2020
KEY_UP = 38;
2121
KEY_RIGHT = 39;
2222
KEY_DOWN = 40;
23+
lastActiveElement = null;
2324

2425
constructor() {
2526
if (!appSettings[plugin.id]) {
2627
appSettings[plugin.id] = {
2728
direction: this.TO_RIGHT,
2829
};
30+
2931
}
3032
this.removeListeners = this.removeListeners.bind(this);
31-
this.addListners = this.addListners.bind(this);
33+
this.addListeners = this.addListeners.bind(this);
3234
this.onvolumndown = this.onvolumndown.bind(this);
3335
this.onvolumnup = this.onvolumnup.bind(this);
36+
this.update = this.update.bind(this);
3437
}
3538

3639
async init() {
37-
const { editor } = editorManager;
38-
editor.on('focus', this.addListners);
39-
editor.on('blur', this.removeListeners);
40+
document.addEventListener('focusin', this.update);
4041
}
4142

4243
async destroy() {
43-
const { editor } = editorManager;
44-
editor.off('focus', this.addListners);
45-
editor.off('blur', this.removeListeners);
46-
document.removeEventListener('volumeupbutton', this.onvolumnup);
47-
document.removeEventListener('volumedownbutton', this.onvolumndown);
44+
this.removeListeners();
45+
document.removeEventListener('focusin', this.update);
46+
}
47+
48+
update() {
49+
this.removeListeners();
50+
if (this.isEditable) {
51+
this.addListeners();
52+
}
4853
}
4954

5055
removeListeners() {
5156
document.removeEventListener('volumeupbutton', this.onvolumnup);
5257
document.removeEventListener('volumedownbutton', this.onvolumndown);
5358
}
5459

55-
addListners() {
60+
addListeners() {
5661
document.addEventListener('volumeupbutton', this.onvolumnup);
5762
document.addEventListener('volumedownbutton', this.onvolumndown);
5863
}
@@ -100,16 +105,21 @@ class AcodePlugin {
100105
}
101106

102107
dispatchKey(which) {
103-
const { editor } = editorManager;
104-
const shiftKey = tag.get('#shift-key')?.dataset.state === 'on'
105-
const $textarea = editor.textInput.getElement();
106-
const keyevent = window.createKeyboardEvent('keydown', {
108+
const keyEvent = KeyboardEvent('keydown', {
109+
type: 'keydown',
107110
key: keyMapping[which],
108111
keyCode: which,
109-
shiftKey,
112+
which,
110113
});
111114

112-
$textarea.dispatchEvent(keyevent);
115+
console.log(keyEvent);
116+
document.activeElement.dispatchEvent(keyEvent);
117+
}
118+
119+
get isEditable() {
120+
return document.activeElement instanceof HTMLTextAreaElement
121+
|| document.activeElement instanceof HTMLInputElement
122+
|| document.activeElement.isContentEditable;
113123
}
114124

115125
get settingsObj() {
@@ -155,4 +165,4 @@ if (window.acode) {
155165
acode.setPluginUnmount(plugin.id, () => {
156166
acodePlugin.destroy();
157167
});
158-
}
168+
}

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,11 +2046,6 @@ has@^1.0.3:
20462046
dependencies:
20472047
function-bind "^1.1.1"
20482048

2049-
html-tag-js@^1.1.6:
2050-
version "1.1.36"
2051-
resolved "https://registry.yarnpkg.com/html-tag-js/-/html-tag-js-1.1.36.tgz#39d8a722a92d4b77072e3438d1fac8d82b691968"
2052-
integrity sha512-8AqoJJNKIuuEn4si4sJKIBqeee/pdRtuRorYwHLlejHlqhY28ge7uyUGCU9qdbyQvtmpMnQV906BSBmrSdlr4Q==
2053-
20542049
http-auth@3.1.x:
20552050
version "3.1.3"
20562051
resolved "https://registry.yarnpkg.com/http-auth/-/http-auth-3.1.3.tgz#945cfadd66521eaf8f7c84913d377d7b15f24e31"

0 commit comments

Comments
 (0)