更上一层楼
VMware 提供培训和认证,以加速您的进步。
了解更多本周我们发布了 JavaScript 代码编辑器 0.4 版本。您可以阅读 Scripted 的背景信息 这里。
0.4 版本的完整发布说明 在这里,但在本文中我将重点介绍一些更有趣的更改。
Ctrl/Cmd+Space
在选择第一个模板完成项后,编辑器内容变为
我们想到的一个关键用例是让您编写一个插件,该插件向编辑器贡献新的注释(这些注释出现在左侧标尺中并允许对编辑器文本进行样式设置)。 这是一个非常简单的插件。 它只是在您的代码中找到水果的名称并为它们添加注释。 也许不是最有用的插件,但它应该显示插件的关键部分是什么,而不是陷入注释计算中。
define(function (require) {
// Grab the editor API
var editorApi = require('scripted/api/editor-extensions');
var pathExp = new RegExp('.*\\.js$');
// Register the new annotation type
editorApi.registerAnnotationType('example.message');
// Load the styling for our annotation (very simple bit of css)
editorApi.loadCss(require('text!./styling.css'));
// Register an 'annotation computer'.
// The return value of the function is the new set of annotations
// which replace any added on previous calls to the function.
editorApi.addAnnotationComputer(function (editor) {
// Only interested in .js files
var path = editor.getFilePath();
if (!path || !pathExp.test(path)) {
return;
}
var text = editor.getText();
var fruits = ['apple', 'banana', 'kiwi', 'orange'];
var annotations=[];
for (var f=0; f<fruits.length; f++) {
var fruit = fruits[f];
var index = text.indexOf(fruit);
while (index!=-1) {
// Create the annotation: needs type/start/end/text
annotations.push({type:'example.message', start:index,
end:index+fruit.length, text:'Found '+fruit });
index = text.indexOf(fruit,index+1);
}
}
return annotations;
});
console.log('Annotation adding sample plugin');
});
此插件的另外两个部分是样式 css(抱歉内联图像数据,只是为了方便重用我们从 Eclipse Orion 继承的一些图像)
/* Styling for the text in the editor */
.annotationRange.message {
/* the icon for a 'squiggly' underline */
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAYAAAC09K7GAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9sJFhQXEbhTg7YAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAAMklEQVQI12NkgIIvJ3QXMjAwdDN+OaEbysDA4MPAwNDNwMCwiOHLCd1zX07o6kBVGQEAKBANtobskNMAAAAASUVORK5CYII=");
background-color: cyan;
color: blue;
}
/* Styling for the right hand overview ruler */
.annotationOverview.message {
background-color: grey;
border: 1px solid #000080;
}
/* Styling for the left hand annotation ruler */
.annotationHTML.message {
/* the icon for a 'flashlight' */
background-image: url("data:image/gif;base64,R0lGODlhEAAQANUAALClrLu1ubOpsKqdp6eapKufqMTAw7attLSrsrGnr62jq8C7v765vaebpb22vLmyuMbCxsnGycfEx8G+wcrIysTBxUltof//yf///v70jergpPvws+nWc/npqvrpqvrpq/raffffnvXVkfTVkvXUkd+9f+SiOemvV+uyXa2OX7mYZqeIXKuNX/ClO7KQYqiIXJ59Vp19VpFvTo9uTZBvTpNyUJNyUf///////wAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAADgALAAAAAAQABAAAAZ4QJxwSCwajS2aS1U6DlunzcagcuKgG4sn5HJiLZ2QiHbEbj6hEapVTKVYr3OItG5TIhVGLF0npigUEAsPAjV9Q24pEhMBCAoybEUmGRcrDgcAAzNGkxcYNzAJBQSbRJ0YqBc2DaVEHJ6pGTStRBqfGBcZILRWvThBADs=");
}
和一个 plugin.json(.json 文件对于完全简单的插件来说不是必需的)。
{
"name": "annotation-adding-plugin",
"version": "0.1",
"description": "Scripted plugin to add markers in the editor",
"main": "annotation-adding-plugin",
"scripted": {
"plugin": true
}
}
所有这些部分都在这里在 git 仓库中。 激活后,添加注释时样式将如下所示
基于此模型,我们根据 Ariya Hidayat 的一篇博客文章实现了一个更实际的插件,该文章描述了如何'检测布尔陷阱'。 该插件的源代码在这里。
有关插件开发的更多信息,请参阅维基。
最后但并非最不重要的是,我们在 Orion 中的主题支持基础上构建,现在为编辑器提供了一种深色主题。 目前无法配置单个颜色,但我们仍然认为设置的默认颜色看起来非常不错:
npm install -g scripted
scr foo.js
readme描述了其他安装选项,它可作为独立 zip 文件使用。 许多用户很高兴关注 master 并每周/每天更新。