angular中怎么使用monaco-editor?下面本篇文章記錄下最近的一次業(yè)務中用到的 monaco-editor 在 angular 中的使用,希望對大家有所幫助!
在 angular12 及之前你可以選擇
這是沒有問題的 但是如果你使用了更高版本的 angular 在使用 npm 安裝 ngx-monaco-editor 時 會報錯?!鞠嚓P教程推薦:《angularjs視頻教程》】
因為原作者似乎已經(jīng)停止了對這個庫的維護 最終的支持停留在了 angular12 版本
當然 你選擇可以選擇正如提示那樣 用 --force 或者 --legacy-peer-deps 來解決問題
但是為了 消除/避免 隱藏的一些問題 我在原作者的基礎上將框架的 angular 支持提升到了 14 并且會一直更新
@rickzhou/ngx-monaco-editor
github github.com/rick-chou/n…
當然 你也可以選擇將作者的源代碼移入自己的本地代碼中 這也是完全沒有問題的
你只需要移動 lib 目錄下的六個文件 然后把它們當成自己項目中的一個 module 去使用就好了
其實所有的 api 都可以在 editor.api.d.ts 這個文件中找到
// 在這個editor下就可以找到所有TS類型 import { editor } from 'monaco-editor';
下面記錄一下常用的
1、設置
// 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, };
2、獲取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 }
3、獲取當前光標位置
editor.getPosition()
4、獲取當前鼠標選中的文本
editor.getModel().getValueInRange(editor.getSelection());
5、修改光標位置
editor.setPosition({ column: 1, lineNumber: 1, });
6、滾動指定行到可視區(qū)中間
editor.revealLineInCenter(1);
7、綁定事件
editor.onMouseDown(event => { // do something }); editor.onKeyDown(event => { // do something });
8、保存/恢復快照
const snapshot = editor.saveViewState(); editor.restoreViewState(snapshot);
9、阻止某個事件
// 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(); } });
更多編程相關知識,請訪問:編程視頻??!
以上就是淺析angular中怎么使用monaco-editor的詳細內容,更多請關注php中文網(wǎng)其它相關文章!
每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺進程會占用資源并降低性能。幸運的是,許多工具可以讓 Windows 保持平穩(wěn)運行。
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號