領先一步
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 的一篇部落格文章實作了一個更實際的插件,該文章描述了如何'偵測布林陷阱'。 該插件的原始碼在這裡。
請參閱 wiki 以取得有關插件開發的更多資訊。
最後但並非最不重要的一點是,我們正在建立在 Orion 中的主題支援之上,現在有一個淺色背景深色文字的編輯器主題。 目前無法配置個別顏色,但我們仍然覺得已設定的預設顏色看起來非常漂亮:
npm install -g scripted
scr foo.js
readme 描述了其他安裝選項,它作為獨立的 zip 檔案提供。 許多使用者很高興遵循 master 並每週/每天更新。