commands.js
来自「在线编辑器」· JavaScript 代码 · 共 859 行 · 第 1/2 页
JS
859 行
}});// ** {{{Command: closefile}}} **bespin.cmd.commands.add({ name: 'closefile', takes: ['filename', 'project'], preview: 'close the file (may lose edits)', completeText: 'add the filename to close (defaults to this file).<br>also, optional project name.', execute: function(self, args) { bespin.publish("bespin:editor:closefile", args); }});// ** {{{Command: dashboard}}} **bespin.cmd.commands.add({ name: 'dashboard', preview: 'navigate to the file', execute: function(self) { bespin.util.navigate.dashboard(); }});// ** {{{Command: version}}} **bespin.cmd.commands.add({ name: 'version', takes: ['command'], preview: 'show the version for Bespin or a command', completeText: 'optionally, a command name', execute: function(self, command) { var bespinVersion = 'Your Bespin is at version ' + bespin.versionNumber + ', Code name: "' + bespin.versionCodename + '"'; var version; if (command) { var theCommand = self.commands[command]; if (!theCommand) { version = "It appears that there is no command named '" + command + "', but " + bespinVersion; } else { version = (theCommand.version) ? "The command named '" + command + "' is at version " + theCommand.version : "The command named '" + command + "' is a core command in Bespin version " + bespin.versionNumber; } } else { version = bespinVersion; } self.showInfo(version); }});// ** {{{Command: clear}}} **bespin.cmd.commands.add({ name: 'clear', aliases: ['cls'], preview: 'clear the file', execute: function(self) { self.editor.model.clear(); }});// ** {{{Command: goto}}} **bespin.cmd.commands.add({ name: 'goto', takes: ['linenumber'], preview: 'move it! make the editor head to your line number.', completeText: 'add the line number to move to', execute: function(self, linenumber) { if (linenumber) { var linenumAsInt = parseInt(linenumber) - 1; self.editor.moveCursor({ row: linenumAsInt, col: 0 }); // If the line that we are moving to is off screen, center it, else just move in place if ( (linenumAsInt < self.editor.ui.firstVisibleRow) || (linenumAsInt >= self.editor.ui.firstVisibleRow+self.editor.ui.visibleRows) ) { bespin.publish("bespin:editor:doaction", { action: 'moveCursorRowToCenter' }); } } }});// ** {{{Command: replace}}} **bespin.cmd.commands.add({ name: 'replace', takes: ['search', 'replace'], preview: 's/foo/bar/g', completeText: 'add the search regex, and then the replacement text', execute: function(self, args) { self.editor.model.replace(args.search, args.replace); }});// ** {{{Command: login}}} **bespin.cmd.commands.add({ name: 'login', // aliases: ['user'], // takes: ['username', 'password'], hidden: true, takes: { order: ['username', 'password'], username: { "short": 'u', }, password: { "short": 'p', optional: true } }, preview: 'login to the service', completeText: 'pass in your username and password', execute: function(self, args) { if (!args) { // short circuit if no username self.executeCommand("status"); return; } _editSession.username = args.user; // TODO: normalize syncing _server.login(args.user, args.pass); }});// ** {{{Command: logout}}} **bespin.cmd.commands.add({ name: 'logout', preview: 'log out', execute: function(self) { delete _editSession.username; _server.logout(function() { window.location.href="/"; }); }});// ** {{{Command: bespin}}} **bespin.cmd.commands.add({ name: 'bespin', preview: 'has', hidden: true, messages: [ "really wants you to trick it out in some way.", "is your Web editor.", "would love to be like Emacs on the Web.", "is written on the Web platform, so you can tweak it." ], execute: function(self) { self.showInfo("Bespin " + this.messages[Math.floor(Math.random() * this.messages.length)]); }});// ** {{{Command: action}}} **bespin.cmd.commands.add({ name: 'action', takes: ['actionname'], preview: 'execute any editor action', hidden: true, execute: function(self, actionname) { bespin.publish("bespin:editor:doaction", { action: actionname }); }});// ** {{{Command: sort}}} **bespin.cmd.commands.add({ name: 'sort', takes: ['direction'], preview: 'sort the current buffer', completeText: 'optionally, sort descending', execute: function(self, direction) { var buffer = self.editor.model.getDocument().split(/\n/); buffer.sort(); if (direction && /^desc/.test(direction.toLowerCase())) buffer.reverse(); self.editor.model.insertDocument(buffer.join("\n")); }});// ** {{{Command: export}}} **bespin.cmd.commands.add({ name: 'export', takes: ['project', 'archivetype'], preview: 'export the given project with an archivetype of zip or tgz', completeText: 'project name, archivetype (zip | tgz, defaults to zip)', execute: function(self, args) { var project = args.project || _editSession.project; var type = args.archivetype; if (!bespin.util.include(['zip','tgz','tar.gz'], archivetype)) { type = 'zip'; } _server.exportProject(project, type); // try to do it via the iframe }});// ** {{{Command: import}}} **bespin.cmd.commands.add({ name: 'import', takes: ['url', 'project'], preview: 'import the given url as a project.<br>If a project name isn\'t given it will use the filename', completeText: 'url (to an archive zip | tgz), optional project name', usage: "[url of archive] [projectname]<br><br><em>(projectname optional. Will be taken from the URL if not provided)</em>", // ** {{{calculateProjectName}}} // // Given a URL, work out the project name as a default // For example, given http://foo.com/path/to/myproject.zip // return "myproject" calculateProjectName: function(url) { var split = url.split('/'); var projectMaker = split[split.length - 1].split("."); projectMaker.pop(); return projectMaker.join("_"); }, // ** {{{isURL}}} // // Test the given string to return if it is a URL. // In this context it has to be http(s) only isURL: function(url) { return (url && (/^http(:|s:)/.test(url))); }, // ** {{{execute}}} // // Can be called in three ways: // // * import http://foo.com/path/to/archive.zip // * import http://foo.com/path/to/archive.zip projectName // * import projectName http://foo.com/path/to/archive.zip execute: function(self, args) { // Fail fast. Nothing given? if (!args.url) { self.showUsage(this); return; // * checking - import http://foo.com/path/to/archive.zip } else if (!args.project && this.isURL(args.url)) { args.project = this.calculateProjectName(args.url); // * Oops, project and url are the wrong way around. That's fine } else if (this.isURL(args.project)) { var project = args.project; var url = args.url; args.project = url; args.url = project; // * Make sure that a URL came along at some point } else if (!this.isURL(args.url)) { self.showUsage(this); return; } var project = args.project; var url = args.url; self.showInfo("About to import " + project + " from:<br><br>" + url + "<br><br><em>It can take awhile to download the project, so be patient!</em>"); bespin.publish("bespin:project:import", { project: project, url: url }); }});// ** {{{Command: doctype}}} **bespin.cmd.commands.add({ name: 'doctype', takes: ['section'], // part on the Wiki preview: 'grab the doctype info for a section', completeText: 'can you give me the Doctype wiki section?', hidden: true, execute: function(self, section) { //TODO grab the HTML: http://code.google.com/p/doctype/wiki/SectionElement?show=content }});// ** {{{Command: trim}}} **bespin.cmd.commands.add({ name: 'trim', takes: ['side'], // left, right, both preview: 'trim trailing or leading whitespace', completeText: 'optionally, give a side of left, right, or both (defaults to right)', execute: function(self, side) { self.editor.model.changeEachRow(function(row) { if (!side) side = "right"; if (bespin.util.include(["left", "both"], side)) { while (row[0] == ' ') { row.shift(); } } if (bespin.util.include(["right", "both"], side)) { var i = row.length - 1; while (row[i] == ' ') { delete row[i]; i--; } } return bespin.util.shrinkArray(row); }); }});// ** {{{Command: bindkey}}} **bespin.cmd.commands.add({ name: 'bindkey', takes: ['modifiers', 'key', 'action'], preview: 'Bind a key to an action', completeText: 'give modifier(s), key, and action name', hidden: true, execute: function(self, args) { if (args.modifiers == "none") args.modifiers = ''; bespin.publish("bespin:editor:bindkey", args); }});// ** {{{Command: insert}}} **bespin.cmd.commands.add({ name: 'insert', takes: ['text'], preview: 'insert the given text at this point.', hidden: true, execute: function(self, text) { self.editor.model.insertChunk(self.editor.cursorPosition, text); }});// ** {{{Command: typingtest}}} **bespin.cmd.commands.add({ name: 'typingtest', preview: 'type in the alphabet a few times', hidden: true, execute: function(self) { var start = Date.now(); for (var i = 0; i < 3; i++) { dojo.forEach(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], function(c) { var args = { pos: bespin.editor.utils.copyPos(self.editor.cursorPosition) }; args.newchar = c; self.editor.ui.actions.insertCharacter(args); }); } var stop = Date.now(); self.showInfo("It took " + (stop - start) + " milliseconds to do this"); }});// ** {{{Command: template}}} **bespin.cmd.commands.add({ name: 'template', takes: ['type'], preview: 'insert templates', completeText: 'pass in the template name', templates: { 'in': "for (var key in object) {\n\n}"}, execute: function(cmdline, type) { cmdline.editor.model.insertChunk(cmdline.editor.cursorPosition, this.templates[type]); }});// ** {{{Command: alias}}} **bespin.cmd.commands.add({ name: 'alias', takes: ['alias', 'command'], preview: 'define and show aliases for commands', completeText: 'optionally, add your alias name, and then the command name', execute: function(self, args) { var output; if (!args.alias) { // -- show all output = "<u>Your Aliases</u><br/><br/>"; for (var x in self.aliases) { output += x + ": " + self.aliases[x] + "<br/>"; } } else { if (args.command == undefined) { // show it output = "<u>Your alias</u><br/><br/>"; var alias = self.aliases[args.alias]; if (alias) { output += args.alias + ": " + self.aliases[args.alias]; } else { output += "No alias set for " + args.alias; } } else { // save a new alias var key = args.alias; var value = args.command; var aliascmd = value.split(' ')[0]; output = "<u>Saving setting</u><br/><br/>"; if (self.commands[key]) { output += "Sorry, there is already a command with the name: " + key; } else if (self.commands[aliascmd]) { output += key + ": " + value; self.aliases[key] = value; } else if (self.aliases[aliascmd]) { // TODO: have the symlink to the alias not the end point output += key + ": " + self.aliases[value] + " (" + value + " was an alias itself)"; self.aliases[key] = value; } else { output += "Sorry, no command or alias with that name." } } } self.showInfo(output); },});// ** {{{Command: history}}} **bespin.cmd.commands.add({ name: 'history', preview: 'show history of the commands', execute: function(self) { self.showInfo('<u>Command History</u><br/><br/>' + self.commandLineHistory.history.join('<br/>')); }});// ** {{{Command: use}}} **bespin.cmd.commands.add({ name: 'use', takes: ['type'], preview: 'use patterns to bring in code', completeText: '"sound" will add sound support', libnames: { 'jquery': 'jquery.min.js' }, execute: function(self, type) { if (type == 'sound') { self.editor.model.insertChunk({ row: 3, col: 0 }, ' <script type="text/javascript" src="soundmanager2.js"></script>\n'); self.editor.model.insertChunk({ row: 4, col: 0 }, " <script>\n var sound; \n soundManager.onload = function() {\n sound = soundManager.createSound('mySound','/path/to/mysoundfile.mp3');\n }\n </script>\n"); } else if (type == 'js') { var jslib = 'http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js'; var script = '<script type="text/javascript" src="' + jslib + '"></script>\n'; self.editor.model.insertChunk({ row: 3, col: 0 }, script); } }});
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?