monaco?editor在Angular的使用詳解
正文
本篇文章主要記錄下最近的一次業(yè)務(wù)中用到的 monaco-editor 在 angular 中的使用
安裝依賴
在 angular12 及之前你可以選擇
- monaco-editor
- ngx-monaco-editor
這是沒有問題的 但是如果你使用了更高版本的 angular 在使用 npm 安裝 ngx-monaco-editor 時 會報錯
因為原作者似乎已經(jīng)停止了對這個庫的維護 最終的支持停留在了 angular12 版本

當(dāng)然 你選擇可以選擇正如提示那樣 用 --force 或者 --legacy-peer-deps 來解決問題
但是為了 消除/避免 隱藏的一些問題 我在原作者的基礎(chǔ)上將框架的 angular 支持提升到了 14 并且會一直更新
@rickzhou/ngx-monaco-editor
github github.com/rick-chou/n…
npm www.npmjs.com/package/@ri…
當(dāng)然 你也可以選擇將作者的源代碼移入自己的本地代碼中 這也是完全沒有問題的
- base-editor.ts 引入 monaco-editor
- config.ts
- diff-editor.component.ts
- editor.component.ts
- editor.module.ts
- types.ts

你只需要移動 lib 目錄下的六個文件 然后把它們當(dāng)成自己項目中的一個 module 去使用就好了
使用
其實所有的 api 都可以在 editor.api.d.ts 這個文件中找到
// 在這個editor下就可以找到所有TS類型
import { editor } from 'monaco-editor';
下面記錄一下常用的
- 設(shè)置
// eg
export const READ_EDITOR_OPTIONS: editor.IEditorOptions = {
readOnly: true,
automaticLayout: false,
minimap: {
enabled: false,
},
renderFinalNewline: false,
scrollbar: {
vertical: 'visible',
},
mouseWheelZoom: true,
contextmenu: false,
fontSize: 16,
scrollBeyondLastLine: false,
smoothScrolling: true,
cursorWidth: 0,
renderValidationDecorations: 'off',
colorDecorators: false,
hideCursorInOverviewRuler: true,
overviewRulerLanes: 0,
overviewRulerBorder: false,
};
- 獲取editor實例
<ngx-monaco-editor
[options]="readEditorOptions"
[(ngModel)]="originLogVal"
(onInit)="initViewEditor($event, false)">
</ngx-monaco-editor>
public initViewEditor(editor: editor.ICodeEditor): void {
// 這個editor就是實例
// 下面方法中的editor就是這里的editor
this.editor = editor
}
- 獲取當(dāng)前光標(biāo)位置
editor.getPosition()
- 獲取當(dāng)前鼠標(biāo)選中的文本
editor.getModel().getValueInRange(editor.getSelection());
- 修改光標(biāo)位置
editor.setPosition({
column: 1,
lineNumber: 1,
});
- 滾動指定行到可視區(qū)中間
editor.revealLineInCenter(1);
- 綁定事件
editor.onMouseDown(event => {
// do something
});
editor.onKeyDown(event => {
// do something
});
- 保存/恢復(fù)快照
const snapshot = editor.saveViewState(); editor.restoreViewState(snapshot);
- 阻止某個事件
// eg 組件默認的搜索快捷鍵
function isMac() {
return /macintosh|mac os x/i.test(navigator.userAgent);
}
editor.onKeyDown(event => {
if (
(isMac() && event.browserEvent.key === 'f' && event.metaKey) ||
(!isMac() && event.browserEvent.key === 'f' && event.ctrlKey)
) {
event.preventDefault();
event.stopPropagation();
}
});以上就是monaco editor在Angular的使用詳解的詳細內(nèi)容,更多關(guān)于Angular使用monaco editor的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
AngularJS中關(guān)于ng-class指令的幾種實現(xiàn)方式詳解
這篇文章給大家介紹了angularJS中ng-class指令的三種實現(xiàn)方式,其中包括通過數(shù)據(jù)的雙向綁定、通過對象數(shù)組和通過key/value這三種方式,有需要的朋友們可以參考學(xué)習(xí),下面來一起看看吧。2016-09-09
AngularJS利用Controller完成URL跳轉(zhuǎn)
本文的主要內(nèi)容是介紹在AngularJS中怎樣利用Controller實現(xiàn)URL跳轉(zhuǎn),本文給出了實例代碼,簡單明了,有需要的可以參考學(xué)習(xí)。2016-08-08
Angular ui.bootstrap.pagination分頁
這篇文章主要為大家詳細介紹了Angular ui.bootstrap.pagination 分頁的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
AngularJS基于provider實現(xiàn)全局變量的讀取和賦值方法
這篇文章主要介紹了AngularJS基于provider實現(xiàn)全局變量的讀取和賦值方法,結(jié)合實例形式分析了AngularJS全局變量的聲明、賦值、讀取等相關(guān)使用技巧,需要的朋友可以參考下2017-06-06
Angular使用操作事件指令ng-click傳多個參數(shù)示例
這篇文章主要介紹了Angular使用操作事件指令ng-click傳多個參數(shù),結(jié)合實例形式分析了AngularJS事件指令及相關(guān)的響應(yīng)、處理操作技巧,需要的朋友可以參考下2018-03-03
angular2 ng build部署后base文件路徑問題詳細解答
本篇文章主要介紹了angular2 ng build部署后base文件路徑問題詳細解答,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07

