Skip to content

Commit cd55932

Browse files
fix: don't throw unhandled errors in TabstopManager when EditSession becomes undefined (#5193)
* fix: don't throw unhandled errors in TabstopManager when EditSession becomes * refactor snippets and hash_handler to use classes * fix detaching tabstrops after sessionChange * backwards compatibility for HashHandler.call --------- Co-authored-by: nightwing <[email protected]>
1 parent c90eaa1 commit cd55932

File tree

3 files changed

+304
-285
lines changed

3 files changed

+304
-285
lines changed

src/keyboard/hash_handler.js

+56-46
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,30 @@ var keyUtil = require("../lib/keys");
44
var useragent = require("../lib/useragent");
55
var KEY_MODS = keyUtil.KEY_MODS;
66

7-
function HashHandler(config, platform) {
8-
this.platform = platform || (useragent.isMac ? "mac" : "win");
9-
this.commands = {};
10-
this.commandKeyBinding = {};
11-
this.addCommands(config);
12-
this.$singleCommand = true;
13-
}
14-
15-
function MultiHashHandler(config, platform) {
16-
HashHandler.call(this, config, platform);
17-
this.$singleCommand = false;
18-
}
19-
20-
MultiHashHandler.prototype = HashHandler.prototype;
7+
class MultiHashHandler {
8+
constructor(config, platform) {
9+
this.$init(config, platform, false);
10+
}
2111

22-
(function() {
23-
12+
$init(config, platform, $singleCommand) {
13+
this.platform = platform || (useragent.isMac ? "mac" : "win");
14+
this.commands = {};
15+
this.commandKeyBinding = {};
16+
this.addCommands(config);
17+
this.$singleCommand = $singleCommand;
18+
}
2419

25-
this.addCommand = function(command) {
20+
addCommand(command) {
2621
if (this.commands[command.name])
2722
this.removeCommand(command);
2823

2924
this.commands[command.name] = command;
3025

3126
if (command.bindKey)
3227
this._buildKeyHash(command);
33-
};
28+
}
3429

35-
this.removeCommand = function(command, keepCommand) {
30+
removeCommand(command, keepCommand) {
3631
var name = command && (typeof command === 'string' ? command : command.name);
3732
command = this.commands[name];
3833
if (!keepCommand)
@@ -54,9 +49,9 @@ MultiHashHandler.prototype = HashHandler.prototype;
5449
}
5550
}
5651
}
57-
};
52+
}
5853

59-
this.bindKey = function(key, command, position) {
54+
bindKey(key, command, position) {
6055
if (typeof key == "object" && key) {
6156
if (position == undefined)
6257
position = key.position;
@@ -84,14 +79,9 @@ MultiHashHandler.prototype = HashHandler.prototype;
8479
var id = KEY_MODS[binding.hashId] + binding.key;
8580
this._addCommandToBinding(chain + id, command, position);
8681
}, this);
87-
};
88-
89-
function getPosition(command) {
90-
return typeof command == "object" && command.bindKey
91-
&& command.bindKey.position
92-
|| (command.isDefault ? -100 : 0);
9382
}
94-
this._addCommandToBinding = function(keyId, command, position) {
83+
84+
_addCommandToBinding(keyId, command, position) {
9585
var ckb = this.commandKeyBinding, i;
9686
if (!command) {
9787
delete ckb[keyId];
@@ -117,9 +107,9 @@ MultiHashHandler.prototype = HashHandler.prototype;
117107
}
118108
commands.splice(i, 0, command);
119109
}
120-
};
110+
}
121111

122-
this.addCommands = function(commands) {
112+
addCommands(commands) {
123113
commands && Object.keys(commands).forEach(function(name) {
124114
var command = commands[name];
125115
if (!command)
@@ -139,27 +129,27 @@ MultiHashHandler.prototype = HashHandler.prototype;
139129

140130
this.addCommand(command);
141131
}, this);
142-
};
132+
}
143133

144-
this.removeCommands = function(commands) {
134+
removeCommands(commands) {
145135
Object.keys(commands).forEach(function(name) {
146136
this.removeCommand(commands[name]);
147137
}, this);
148-
};
138+
}
149139

150-
this.bindKeys = function(keyList) {
140+
bindKeys(keyList) {
151141
Object.keys(keyList).forEach(function(key) {
152142
this.bindKey(key, keyList[key]);
153143
}, this);
154-
};
144+
}
155145

156-
this._buildKeyHash = function(command) {
146+
_buildKeyHash(command) {
157147
this.bindKey(command.bindKey, command);
158-
};
148+
}
159149

160150
// accepts keys in the form ctrl+Enter or ctrl-Enter
161151
// keys without modifiers or shift only
162-
this.parseKeys = function(keys) {
152+
parseKeys(keys) {
163153
var parts = keys.toLowerCase().split(/[\-\+]([\-\+])?/).filter(function(x){return x;});
164154
var key = parts.pop();
165155

@@ -182,14 +172,14 @@ MultiHashHandler.prototype = HashHandler.prototype;
182172
hashId |= modifier;
183173
}
184174
return {key: key, hashId: hashId};
185-
};
175+
}
186176

187-
this.findKeyCommand = function findKeyCommand(hashId, keyString) {
177+
findKeyCommand(hashId, keyString) {
188178
var key = KEY_MODS[hashId] + keyString;
189179
return this.commandKeyBinding[key];
190-
};
180+
}
191181

192-
this.handleKeyboard = function(data, hashId, keyString, keyCode) {
182+
handleKeyboard(data, hashId, keyString, keyCode) {
193183
if (keyCode < 0) return;
194184
var key = KEY_MODS[hashId] + keyString;
195185
var command = this.commandKeyBinding[key];
@@ -212,13 +202,33 @@ MultiHashHandler.prototype = HashHandler.prototype;
212202
data.$keyChain = ""; // reset keyChain
213203
}
214204
return {command: command};
215-
};
205+
}
216206

217-
this.getStatusText = function(editor, data) {
207+
getStatusText(editor, data) {
218208
return data.$keyChain || "";
219-
};
209+
}
210+
211+
}
212+
213+
function getPosition(command) {
214+
return typeof command == "object" && command.bindKey
215+
&& command.bindKey.position
216+
|| (command.isDefault ? -100 : 0);
217+
}
218+
219+
class HashHandler extends MultiHashHandler {
220+
constructor(config, platform) {
221+
super(config, platform);
222+
this.$singleCommand = true;
223+
}
224+
}
220225

221-
}).call(HashHandler.prototype);
226+
HashHandler.call = function(thisArg, config, platform) {
227+
MultiHashHandler.prototype.$init.call(thisArg, config, platform, true);
228+
};
229+
MultiHashHandler.call = function(thisArg, config, platform) {
230+
MultiHashHandler.prototype.$init.call(thisArg, config, platform, false);
231+
};
222232

223233
exports.HashHandler = HashHandler;
224234
exports.MultiHashHandler = MultiHashHandler;

0 commit comments

Comments
 (0)