commands.js
来自「在线编辑器」· JavaScript 代码 · 共 859 行 · 第 1/2 页
JS
859 行
/* ***** 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 ***** */dojo.provide("bespin.cmd.commands");// = Commands =//// This array stores all of the default commands.// ** {{{bespin.cmd.commands.store}}} **//// The core store to hold commands that others share.bespin.cmd.commands.store = {};// ** {{{bespin.cmd.commands.add}}} **//// Add the command to the store which has a name -> command hashbespin.cmd.commands.add = function(command) { bespin.cmd.commands.store[command.name] = command;};// ** {{{bespin.cmd.commands.get}}} **//// Return a command from the storebespin.cmd.commands.get = function(commandname) { return bespin.cmd.commands.store[commandname];};// == Start adding commands to the store ==//// ** {{{Command: help}}} **bespin.cmd.commands.add({ name: 'help', takes: ['search'], preview: 'show commands', description: 'The <u>help</u> gives you access to the various commands in the Bespin system.<br/><br/>You can narrow the search of a command by adding an optional search params.<br/><br/>If you pass in the magic <em>hidden</em> parameter, you will find subtle hidden commands.<br/><br/>Finally, pass in the full name of a command and you can get the full description, which you just did to see this!', completeText: 'optionally, narrow down the search', execute: function(self, extra) { var commands = []; if (self.commands[extra]) { // caught a real command commands.push("<u>Help for the command: <em>" + extra + "</em></u><br/>"); var command = self.commands[extra]; commands.push(command['description'] ? command.description : command.preview); } else { var showHidden = false; commands.push("<u>Commands Available</u><br/>"); if (extra) { if (extra == "hidden") { // sneaky, sneaky. extra = ""; showHidden = true; } commands.push("<em>(starting with</em> " + extra + " <em>)</em><br/>"); } var tobesorted = []; for (name in self.commands) { tobesorted.push(name); } var sorted = tobesorted.sort(); for (var i = 0; i < sorted.length; i++) { var name = sorted[i]; var command = self.commands[name]; if (!showHidden && command.hidden) continue; if (extra && name.indexOf(extra) != 0) continue; var arguments = (command.takes) ? ' [' + command.takes.order.join('] [') + ']' : ''; commands.push('<b>' + name + arguments + '</b>: ' + command.preview); } } self.showInfo("<div style='font-size: 0.80em'>" + commands.join("<br/>") + "</div>"); }}); // ** {{{Command: set}}} **bespin.cmd.commands.add({ name: 'set', takes: ['key', 'value'], preview: 'define and show settings', completeText: 'optionally, add a key and/or a value, else you will see all settings', // complete: function(self, value) { // console.log(self); // console.log(value); // return value; // }, execute: function(self, setting) { var output; if (!setting.key) { // -- show all var settings = self.settings.list(); output = "<u>Your Settings</u><br/><br/>"; for (var x = 0; x < settings.length; x++) { if (settings[x].key[0] != '_') { output += settings[x].key + ": " + settings[x].value + "<br/>"; } } } else { var key = setting.key; if (setting.value == undefined) { // show it var value = self.settings.get(key); if (value) { output = "<u>Your setting</u><br/><br/>"; output += key + ": " + value; } else { output = "You do not have a setting for <em>" + key + "</em>"; } } else { output = "<u>Saving setting</u><br/><br/>"; output += key + ": " + setting.value; self.settings.set(key, setting.value); } } self.showInfo(output); }});// ** {{{Command: files (ls, list)}}} **bespin.cmd.commands.add({ name: 'files', aliases: ['ls', 'list'], takes: ['project'], preview: 'show files', completeText: 'optionally, add the project name of your choice', execute: function(self, project) { if (!project && (typeof _editSession != "undefined") ) { project = _editSession.projectForDisplay(); } if (!project) { self.showInfo("You need to pass in a project"); return; } self.files.fileNames(project, function(fileNames) { var files = "<u>Files in project: " + project + "</u><br/><br/>"; for (var x = 0; x < fileNames.length; x++) { files += fileNames[x].name + "<br/>"; } self.showInfo(files); }); }});// ** {{{Command: status}}} **bespin.cmd.commands.add({ name: 'status', preview: 'get info on the current project and file', execute: function(self) { var file = _editSession.path || 'a new scratch file'; self.showInfo('Hey ' + _editSession.username + ', you are editing ' + file + ' in project ' + _editSession.projectForDisplay()); }});// ** {{{Command: project}}} **bespin.cmd.commands.add({ name: 'project', takes: ['projectname'], preview: 'show the current project, or set to a new one', completeText: 'optionally, add the project name to change to that project', execute: function(self, projectname) { if (projectname) { bespin.publish("bespin:editor:project:set", { project: projectname }); } else { self.executeCommand('status'); } }});// ** {{{Command: projects}}} **bespin.cmd.commands.add({ name: 'projects', preview: 'show projects', execute: function(self, extra) { self.files.projects(function(projectNames) { var projects = "<u>Your projects</u><br/><br/>"; for (var x = 0; x < projectNames.length; x++) { projects += projectNames[x].name + "<br/>"; } self.showInfo(projects); }); }});// ** {{{Command: createproject}}} **bespin.cmd.commands.add({ name: 'createproject', takes: ['projectname'], preview: 'create a new project', usage: '[newprojectname]', execute: function(self, projectname) { if (!projectname) { self.showUsage(this); return; } bespin.publish("bespin:project:create", { project: projectname }); }});// ** {{{Command: createproject}}} **bespin.cmd.commands.add({ name: 'deleteproject', takes: ['projectname'], preview: 'delete a new project', usage: '[newprojectname]', execute: function(self, projectname) { if (!projectname) { self.showUsage(this); return; } bespin.publish("bespin:project:delete", { project: projectname }); }});// ** {{{Command: renameproject}}} **bespin.cmd.commands.add({ name: 'renameproject', takes: ['currentProject', 'newProject'], preview: 'rename a project', usage: '[currentProject], [newProject]', execute: function(self, args) { if (!args.currentProject || !args.newProject) { self.showUsage(this); return; } bespin.publish("bespin:project:rename", { currentProject: args.currentProject, newProject: args.newProject }); }});// ** {{{Command: mkdir}}} **bespin.cmd.commands.add({ name: 'mkdir', takes: ['path', 'projectname'], preview: 'create a new directory in the given project', usage: '[path] [projectname]', execute: function(self, args) { if (!args.path) { self.showUsage(this); return; } var opts = { path: args.path }; if (args.projectname) opts.project = args.projectname; bespin.publish("bespin:directory:create", opts); }});// ** {{{Command: save}}} **bespin.cmd.commands.add({ name: 'save', takes: ['filename'], preview: 'save the current contents', completeText: 'add the filename to save as, or use the current file', withKey: "APPLE S", execute: function(self, filename) { bespin.publish("bespin:editor:savefile", { filename: filename }); }});// ** {{{Command: load (open)}}} **bespin.cmd.commands.add({ name: 'load', aliases: ['open'], takes: ['filename'], preview: 'load up the contents of the file', completeText: 'add the filename to open', execute: function(self, filename) { bespin.publish("bespin:editor:openfile", { filename: filename }); }});// ** {{{Command: preview}}} **bespin.cmd.commands.add({ name: 'preview', takes: ['filename'], preview: 'view the file in a new browser window', completeText: 'add the filename to view or use the current file', execute: function(self, filename) { bespin.publish("bespin:editor:preview", { filename: filename }); }});// ** {{{Command: editconfig}}} **bespin.cmd.commands.add({ name: 'editconfig', aliases: ['config'], preview: 'load up the config file', execute: function(self) { bespin.publish("bespin:editor:config:edit"); }});// ** {{{Command: runconfig}}} **bespin.cmd.commands.add({ name: 'runconfig', preview: 'run your config file', execute: function(self) { bespin.publish("bespin:editor:config:run"); }});// ** {{{Command: cmdload}}} **bespin.cmd.commands.add({ name: 'cmdload', takes: ['commandname'], preview: 'load up a new command', completeText: 'command name to load (required)', usage: '[commandname]: Command name required.', execute: function(self, commandname) { if (!commandname) { self.showUsage(this); return; } bespin.publish("bespin:commands:load", { commandname: commandname }); }});// ** {{{Command: cmdedit}}} **bespin.cmd.commands.add({ name: 'cmdedit', takes: ['commandname'], aliases: ['cmdadd'], preview: 'edit the given command (force if doesn\'t exist', completeText: 'command name to edit (required)', usage: '[commandname]: Command name required.', execute: function(self, commandname) { if (!commandname) { self.showUsage(this); return; } bespin.publish("bespin:commands:edit", { commandname: commandname }); }});// ** {{{Command: cmdlist}}} **bespin.cmd.commands.add({ name: 'cmdlist', preview: 'list my custom commands', execute: function(self) { bespin.publish("bespin:commands:list"); }});// ** {{{Command: cmdrm}}} **bespin.cmd.commands.add({ name: 'cmdrm', takes: ['commandname'], preview: 'delete a custom command', completeText: 'command name to delete (required)', usage: '[commandname]: Command name required.', execute: function(self, commandname) { if (!commandname) { self.showUsage(this); return; } bespin.publish("bespin:commands:delete", { commandname: commandname }); }});// ** {{{Command: newfile}}} **bespin.cmd.commands.add({ name: 'newfile', //aliases: ['new'], takes: ['filename', 'project'], preview: 'create a new buffer for file', completeText: 'optionally, name the new filename', withKey: "CTRL SHIFT N", execute: function(self, args) { if (args.filename) { args.newfilename = args.filename; delete args.filename; } bespin.publish("bespin:editor:newfile", args || {}); }});// ** {{{Command: rm (remove, del)}}} **bespin.cmd.commands.add({ name: 'rm', aliases: ['remove', 'del'], takes: ['filename'], preview: 'remove the file', completeText: 'add the filename to remove', execute: function(self, filename) { if (!filename) { self.showInfo("give me a filename or directory to delete"); return; } self.files.removeFile(_editSession.project, filename, function() { if (_editSession.checkSameFile(_editSession.project, filename)) self.editor.model.clear(); // only clear if deleting the same file self.showInfo('Removed file: ' + filename, true); }, function(xhr) { self.showInfo("Wasn't able to remove the file <b>" + filename + "</b><br/><em>Error</em> (probably doesn't exist): " + xhr.responseText); });
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?