UNPKG

4.89 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.RawKeybinding = exports.Keybinding = void 0;
19const types_1 = require("./types");
20var Keybinding;
21(function (Keybinding) {
22 /**
23 * Compares two keybindings for equality.
24 * Can optionally ignore the keybinding and/or args property in the comparison.
25 * @param a The first Keybinding in the comparison
26 * @param b The second Keybinding in the comparison
27 * @param ignoreKeybinding Ignore the 'keybinding' property in the comparison
28 * @param ignoreArgs Ignore the 'args' property in the comparison
29 */
30 function equals(a, b, ignoreKeybinding = false, ignoreArgs = false) {
31 if (a.command === b.command &&
32 (a.context || '') === (b.context || '') &&
33 (a.when || '') === (b.when || '') &&
34 (ignoreKeybinding || a.keybinding === b.keybinding) &&
35 (ignoreArgs || (a.args || '') === (b.args || ''))) {
36 return true;
37 }
38 return false;
39 }
40 Keybinding.equals = equals;
41 /**
42 * Returns a new object only containing properties which
43 * are described on the `Keybinding` API.
44 *
45 * @param binding the binding to create an API object for.
46 */
47 function apiObjectify(binding) {
48 return {
49 command: binding.command,
50 keybinding: retrieveKeybinding(binding),
51 context: binding.context,
52 when: binding.when,
53 args: binding.args
54 };
55 }
56 Keybinding.apiObjectify = apiObjectify;
57 function retrieveKeybinding(binding) {
58 var _a, _b;
59 return (_b = (_a = binding.keybinding) !== null && _a !== void 0 ? _a : binding.key) !== null && _b !== void 0 ? _b : '';
60 }
61 Keybinding.retrieveKeybinding = retrieveKeybinding;
62 /**
63 * Returns with the string representation of the binding.
64 * Any additional properties which are not described on
65 * the `Keybinding` API will be ignored.
66 *
67 * @param binding the binding to stringify.
68 */
69 function stringify(binding) {
70 return JSON.stringify(apiObjectify(binding));
71 }
72 Keybinding.stringify = stringify;
73 /* Determine whether object is a KeyBinding */
74 function is(arg) {
75 return (0, types_1.isObject)(arg) && 'command' in arg && 'keybinding' in arg;
76 }
77 Keybinding.is = is;
78 function replaceKeybinding(keybindings, oldKeybinding, newKeybinding) {
79 const indexOld = keybindings.findIndex(keybinding => Keybinding.equals(keybinding, oldKeybinding, false, true));
80 if (indexOld >= 0) {
81 const indexNew = keybindings.findIndex(keybinding => Keybinding.equals(keybinding, newKeybinding, false, true));
82 if (indexNew >= 0 && indexNew !== indexOld) {
83 // if keybindings already contain the new keybinding, remove the old keybinding and update the new one
84 keybindings.splice(indexOld, 1);
85 keybindings[indexNew] = newKeybinding;
86 }
87 else {
88 keybindings[indexOld] = newKeybinding;
89 }
90 return true;
91 }
92 return false;
93 }
94 Keybinding.replaceKeybinding = replaceKeybinding;
95 function addKeybinding(keybindings, newKeybinding) {
96 const index = keybindings.findIndex(keybinding => Keybinding.equals(keybinding, newKeybinding, false, true));
97 if (index >= 0) {
98 // if keybindings already contain the new keybinding, update it
99 keybindings[index] = newKeybinding;
100 }
101 else {
102 keybindings.push(newKeybinding);
103 }
104 }
105 Keybinding.addKeybinding = addKeybinding;
106})(Keybinding = exports.Keybinding || (exports.Keybinding = {}));
107var RawKeybinding;
108(function (RawKeybinding) {
109 function is(candidate) {
110 return (0, types_1.isObject)(candidate) && 'command' in candidate && 'key' in candidate;
111 }
112 RawKeybinding.is = is;
113})(RawKeybinding = exports.RawKeybinding || (exports.RawKeybinding = {}));
114//# sourceMappingURL=keybinding.js.map
\No newline at end of file