最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

monaco?editor在Angular的使用詳解

 更新時間:2022年09月27日 15:51:59   作者:RickZhou  
這篇文章主要為大家介紹了monaco?editor在Angular的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

本篇文章主要記錄下最近的一次業(yè)務(wù)中用到的 monaco-editor 在 angular 中的使用

安裝依賴

在 angular12 及之前你可以選擇

  • monaco-editor
  • ngx-monaco-editor

這是沒有問題的 但是如果你使用了更高版本的 angular 在使用 npm 安裝 ngx-monaco-editor 時 會報錯

因?yàn)樵髡咚坪跻呀?jīng)停止了對這個庫的維護(hù) 最終的支持停留在了 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

github.com/rick-chou/n…

你只需要移動 lib 目錄下的六個文件 然后把它們當(dāng)成自己項(xiàng)目中的一個 module 去使用就好了

使用

其實(shí)所有的 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實(shí)例
<ngx-monaco-editor
  [options]="readEditorOptions"
  [(ngModel)]="originLogVal"
  (onInit)="initViewEditor($event, false)">
</ngx-monaco-editor>
public initViewEditor(editor: editor.ICodeEditor): void {
  // 這個editor就是實(shí)例
  // 下面方法中的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 組件默認(rèn)的搜索快捷鍵
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的使用詳解的詳細(xì)內(nèi)容,更多關(guān)于Angular使用monaco editor的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

尚志市| 玉溪市| 德阳市| 望都县| 金华市| 隆尧县| 彰化市| 桂林市| 故城县| 崇明县| 酉阳| 平阳县| 卓资县| 峨眉山市| 金平| 普定县| 宣恩县| 五河县| 紫云| 焦作市| 马山县| 乌什县| 锦州市| 资源县| 嘉荫县| 南岸区| 新乡市| 江北区| 沙雅县| 靖江市| 滁州市| 酉阳| 龙海市| 县级市| 高平市| 东乡县| 泾源县| 化德县| 睢宁县| 长寿区| 平果县|