@kui-shell/plugin-core-support
Version:
Kui plugin offering core extensions such as help and screenshot commands
115 lines (114 loc) • 3.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
/*
* Copyright 2021 The Kubernetes Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P ? value : new P(function (resolve) {
resolve(value);
});
}
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e) {
reject(e);
}
}
function step(result) {
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
/**
* The command usage model
*
*/
const usage = {
command: 'tab edit toggle',
strict: 'tab edit toggle',
optional: [{
name: '--current-tab',
alias: '-c',
boolean: true,
docs: 'Toggle edit mode on current tab'
}]
};
function getTabIndex(argvNoOptions) {
const index = parseInt(argvNoOptions[argvNoOptions.length - 1], 10);
if (isNaN(index)) {
throw new Error(`4th argument is not a number. Expected type number`);
} else {
return index;
}
}
/** Command registration */
function _default(registrar) {
// register the `tab edit toggle` command
registrar.listen('/tab/edit/toggle', ({
tab,
argvNoOptions,
parsedOptions
}) => __awaiter(this, void 0, void 0, function* () {
const {
isOfflineClient,
isReadOnlyClient
} = yield Promise.resolve().then(() => require('@kui-shell/core/mdist/api/Client'));
if (isReadOnlyClient() || isOfflineClient()) {
throw new Error('Unsupported operation');
}
if (!parsedOptions.c && argvNoOptions.length < 4) {
throw new Error('Not enough arguments. Expected: tab edit toggle tabIndexNum');
}
if (argvNoOptions.length > 4) {
throw new Error('Too many arguments. Expected: tab edit toggle tabIndexNum');
}
const [{
getPrimaryTabId
}, {
eventBus
}] = yield Promise.all([Promise.resolve().then(() => require('@kui-shell/core/mdist/api/Tab')), Promise.resolve().then(() => require('@kui-shell/core/mdist/api/Events'))]);
if (parsedOptions.c) {
// we have the uuid
eventBus.emitWithTabId('/kui/tab/edit/toggle', getPrimaryTabId(tab));
} else {
// we only have the index, so we need to broadcast for help
// the event expects 0-indexed, our controller takes 1-indexed
eventBus.emit('/kui/tab/edit/toggle/index', getTabIndex(argvNoOptions) - 1);
}
return 'Successfully toggled edit mode';
}), {
usage,
flags: {
alias: {
'current-tab': ['c']
},
boolean: ['c']
}
});
}