commandline.js
来自「在线编辑器」· JavaScript 代码 · 共 502 行 · 第 1/2 页
JS
502 行
/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1 * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (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.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. * See the License for the specific language governing rights and * limitations under the License. * * The Original Code is Bespin. * * The Initial Developer of the Original Code is Mozilla. * Portions created by the Initial Developer are Copyright (C) 2009 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Bespin Team (bespin@mozilla.com) * * ***** END LICENSE BLOCK ***** */ // = Command Line =//// This command line module provides everything that the command line interface needs://// * {{{bespin.cmd.commandline.Interface}}} : The base class itself. The actually interface.// * {{{bespin.cmd.commandline.KeyBindings}}} : Handling the special key handling in the command line// * {{{bespin.cmd.commandline.History}}} : Handle command line history// * {{{bespin.cmd.commandline.SimpleHistoryStore}}} : Simple one session storage of history// * {{{bespin.cmd.commandline.Events}}} : The custom events that the command line needs to handledojo.provide("bespin.cmd.commandline");// ** {{{ bespin.cmd.commandline.Interface }}} **//// The core command line driver. It executes commands, stores them, and handles completiondojo.declare("bespin.cmd.commandline.Interface", null, { constructor: function(commandLine, initCommands) { this.commandLine = commandLine; if (window['_files']) this.files = _files; if (window['_settings']) this.settings = _settings; if (window['_editor']) this.editor = _editor; this.inCommandLine = false; this.commands = {}; this.aliases = {}; this.commandLineKeyBindings = new bespin.cmd.commandline.KeyBindings(this); this.commandLineHistory = new bespin.cmd.commandline.History(this); this.customEvents = new bespin.cmd.commandline.Events(this); if (initCommands) this.addCommands(initCommands); // initialize the commands for the cli }, executeCommand: function(value) { var data = value.split(/\s+/); var commandname = data.shift(); var command; var argstr = data.join(' '); if (this.commands[commandname]) { command = this.commands[commandname]; } else if (this.aliases[commandname]) { var alias = this.aliases[commandname].split(' '); var aliascmd = alias.shift(); if (alias.length > 0) { argstr = alias.join(' ') + ' ' + argstr; } command = this.commands[aliascmd]; } else { this.showInfo("Sorry, no command '" + commandname + "'. Maybe try to run » help", true); return; } bespin.publish("bespin:cmdline:executed", { command: command, args: argstr }); command.execute(this, this.getArgs(argstr.split(' '), command)); this.commandLine.value = ''; // clear after the command }, addCommand: function(command) { // -- Allow for the default [ ] takes style by expanding it to something bigger if (command.takes && dojo.isArray(command.takes)) { command = this.normalizeTakes(command); } // -- Add bindings if (command.withKey) { var args = bespin.util.keys.fillArguments(command.withKey); args.action = "bespin:cmdline:execute;name=" + command.name; bespin.publish("bespin:editor:bindkey", args); } this.commands[command.name] = command; if (command['aliases']) { dojo.forEach(command['aliases'], function(alias) { this.aliases[alias] = command.name; }, this); } }, addCommands: function(commands) { dojo.forEach(commands, dojo.hitch(this, function(command) { if (dojo.isString(command)) command = bespin.cmd.commands.get(command); this.addCommand(command); })); }, hasCommand: function(commandname) { if (this.commands[commandname]) { // yup, there she blows. shortcut return true; } for (command in this.commands) { // try the aliases if (this.commands[command]['aliases']) { if (bespin.util.include(this.commands[command]['aliases'], commandname)) { return true; } } } return false; }, showUsage: function(command, autohide) { var usage = command.usage || "no usage information found for " + command.name; this.showInfo("Usage: " + command.name + " " + usage, autohide); }, showInfo: function(html, autohide) { this.hideInfo(); dojo.byId('info').innerHTML = html; dojo.style('info', 'display', 'block'); dojo.connect(dojo.byId('info'), "onclick", this, "hideInfo"); if (autohide) { this.infoTimeout = setTimeout(dojo.hitch(this, function() { this.hideInfo(); }), 4600); } }, hideInfo: function() { dojo.style('info', 'display', 'none'); if (this.infoTimeout) clearTimeout(this.infoTimeout); }, findCompletions: function(value) { var matches = []; if (value.length > 0) { for (var command in this.commands) { if (command.indexOf(value) == 0) { matches.push(command); } } for (var alias in this.aliases) { if (alias.indexOf(value) == 0) { matches.push(alias); } } } return matches; }, complete: function(value) { var matches = this.findCompletions(value); if (matches.length == 1) { var commandLineValue = matches[0]; var command = this.commands[matches[0]]; if (command) { if (this.commandTakesArgs(command)) { commandLineValue += ' '; } if (command['completeText']) { this.showInfo(command['completeText']); } if (command['complete']) { this.showInfo(command.complete(this, value)); } } else { // an alias this.showInfo(commandLineValue + " is an alias for: " + this.aliases[commandLineValue]); commandLineValue += ' '; } this.commandLine.value = commandLineValue; } }, commandTakesArgs: function(command) { return command.takes != undefined; }, // ** {{{ getArgs }}} ** // // Calculate the args object to be passed into the command. // If it only takes one argument just send in that data, but if it wants more, split it all up for the command and send in an object. getArgs: function(fromUser, command) { if (!command.takes) return undefined; var args; var userString = fromUser.join(' '); if (command.takes && command.takes.order.length < 2) { // One argument, so just return that args = userString; } else { args = new bespin.util.TokenObject(userString, { params: command.takes.order.join(' ') }); args.rawinput = userString; } return args; }, normalizeTakes: function(command) { // TODO: handle shorts that are the same! :) var takes = command.takes; command.takes = { order: takes }; dojo.forEach(takes, function(item) { command.takes[item] = { "short": item[0] }; }); return command; }, handleCommandLineFocus: function(e) { if (this.inCommandLine) return true; // in the command line! if (e.keyChar == 'j' && e.ctrlKey) { // send to command line this.commandLine.focus(); dojo.stopEvent(e); return true;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?