国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Monaco Editor安裝,vue3中使用,自定義高亮,自定義提示,附完整代碼

這篇具有很好參考價值的文章主要介紹了Monaco Editor安裝,vue3中使用,自定義高亮,自定義提示,附完整代碼。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

一、安裝

	yarn add monaco-editor
	或
	npm install monaco-editor --save

二、使用

<div ref="editorContainer" style="height:100%;"></div>
import * as monaco from 'monaco-editor';
const editorContainer = ref<any>(null)
const editor = ref<any>(null)
onMounted(() => {
  editor.value = monaco.editor.create(editorContainer.value,{
    value: "test",
    language:"javascript",
    folding: true, // 是否折疊
    foldingHighlight: true, // 折疊等高線
    foldingStrategy: "indentation", // 折疊方式  auto | indentation
    showFoldingControls: "always", // 是否一直顯示折疊 always | mouseover
    disableLayerHinting: true, // 等寬優(yōu)化
    emptySelectionClipboard: false, // 空選擇剪切板
    selectionClipboard: false, // 選擇剪切板
    automaticLayout: true, // 自動布局
    codeLens: false, // 代碼鏡頭
    scrollBeyondLastLine: false, // 滾動完最后一行后再滾動一屏幕
    colorDecorators: true, // 顏色裝飾器
    accessibilitySupport: "off", // 輔助功能支持  "auto" | "off" | "on"
    lineNumbers: "on", // 行號 取值: "on" | "off" | "relative" | "interval" | function
    lineNumbersMinChars: 5, // 行號最小字符   number
    readOnly: false, //是否只讀  取值 true | false
  })
})

三、自定義高亮

monaco.languages.register({ id: 'mylanguages' })
monaco.languages.setMonarchTokensProvider('mylanguages', {
  ignoreCase: true, // 忽略大小寫
  tokenizer: {
    root:[
          [/curl/, {token: "string.escape"}],
          [/-X|-H|-d/, {token: "keyword"}],
          [/POST|GET|DELETE|PATCH|PUT/, {token: "comment.doc"}],
    ],
  }
})

root中為高亮規(guī)則。[/curl/, {token: “string.escape”}]:表示 ‘curl’ 的高亮顏色為粉色
高亮顏色參考:https://microsoft.github.io/monaco-editor/monarch.html
效果:
monaco editor 代碼提示,javascript,開發(fā)語言,ecmascript

四、自定義提示

monaco.languages.registerHoverProvider('mylanguages', { // 光標移入提示功能
    provideHover: function (model, position, token) {
      const lineword = model.getLineContent(position.lineNumber) // 獲取光標懸停所在行的所有內(nèi)容
      const word = model.getWordAtPosition(position)?.word // 獲取光標懸停的單詞
        if (word === "name") {
          return {
            contents: [
              { value: `${word}` },
              {
                value: [
                  "這是name的一些介紹",
                  "這是name的一些介紹",
                ].join("\n\n"),
              },
            ],
          };
        } else if(undefined !== word){
          return {
            contents: [
              { value: `${word}` },
              {
                value: [
                  lineword
                ].join("\n\n"),
              },
            ],
          }
        }
    },
  });

效果:
monaco editor 代碼提示,javascript,開發(fā)語言,ecmascript

monaco editor 代碼提示,javascript,開發(fā)語言,ecmascript

五、完整代碼

1、父組件:HomeView.vue
父組件中傳給子組件所需的組件高度、初始內(nèi)容、高亮類型、是否只讀
子組件通過editorChange方法給父組件實時傳值

<template>
  <div>
      <monaco
        ref="monacoEdit"
        :value="data"
        :readonly="false"
        type="curl"
        :height="300"
        @editorChange="editorChange"
      ></monaco>
  </div>
</template>
<script setup lang="ts">
import monaco from '../components/MonacoView.vue'
import { ref, toRefs, reactive } from "vue"
 const data = ref('test')
function editorChange(val: string) {
//val:子組件實時傳過來的值
  console.log(val)
}
</script>
 
<style scoped>
  
</style>

2、子組件:MonacoView.vue

<template>
  <div class="editorapp">
    <div ref="editorContainer" :style="{height:editor_height}"></div>
  </div>
  
</template>

<script setup lang="ts">
import { onMounted, ref, toRaw, watchEffect } from "vue"
// 引入方法一
import * as monaco from 'monaco-editor';

// 引入方法二( 這種引入方法體積小但沒有鼠標懸停方法registerHoverProvider)
// import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js';
// import 'monaco-editor/esm/vs/basic-languages/javascript/javascript.contribution'

const emit = defineEmits(['contentChange'])
const props = defineProps({
  value: {
    type: String,
    default: '',
  },
  height: { // 編輯器height
    type: [String, Number],
    default: 500,
  },
  readonly: { // 是否只讀
    type: Boolean,
    default: false,
  },
  type: { // 高亮類型(javascript、curl等,javascript:自帶的,curl:自定義的高亮規(guī)則)
    type: String,
    default: 'javascript',
  }
})
const editorContainer = ref<any>(null)
const editor = ref<any>(null)
const data = ref(props.value)
const editor_height = ref(`${props.height}px`)
onMounted(() => {
   // 初始化編輯器,確保dom已經(jīng)渲染
  if(props.type === 'curl'){ // 如果是curl 類型則重新定義高亮規(guī)則,否則使用自帶的高亮語言
    monaco.languages.register({ id: props.type })
    monaco.languages.setMonarchTokensProvider(props.type, {
      ignoreCase: true,
      tokenizer: {
        root:[
              [/curl/, {token: "string.escape"}],
              [/-X|-H|-d/, {token: "keyword"}],
              [/POST|GET|DELETE|PATCH|PUT/, {token: "comment.doc"}],
        ],
      }
    })
  }
  monaco.languages.registerHoverProvider(props.type, { // 光標移入提示功能
    provideHover: function (model, position, token) {
      const lineword = model.getLineContent(position.lineNumber) // 獲取光標懸停所在行的所有內(nèi)容
      const word = model.getWordAtPosition(position)?.word // 獲取光標懸停的單詞
        if (word === "name") {
          return {
            contents: [
              { value: `${word}` },
              {
                value: [
                  "這是name的一些介紹",
                  "這是name的一些介紹",
                ].join("\n\n"),
              },
            ],
          };
        } else if(undefined !== word){
          return {
            contents: [
              { value: `${word}` },
              {
                value: [
                  lineword
                ].join("\n\n"),
              },
            ],
          }
        }
    },
  });
  editor.value = monaco.editor.create(editorContainer.value,{
    value: data.value,
    language:props.type,
    folding: true, // 是否折疊
    foldingHighlight: true, // 折疊等高線
    foldingStrategy: "indentation", // 折疊方式  auto | indentation
    showFoldingControls: "always", // 是否一直顯示折疊 always | mouseover
    disableLayerHinting: true, // 等寬優(yōu)化
    emptySelectionClipboard: false, // 空選擇剪切板
    selectionClipboard: false, // 選擇剪切板
    automaticLayout: true, // 自動布局
    codeLens: false, // 代碼鏡頭
    scrollBeyondLastLine: false, // 滾動完最后一行后再滾動一屏幕
    colorDecorators: true, // 顏色裝飾器
    accessibilitySupport: "off", // 輔助功能支持  "auto" | "off" | "on"
    lineNumbers: "on", // 行號 取值: "on" | "off" | "relative" | "interval" | function
    lineNumbersMinChars: 5, // 行號最小字符   number
    readOnly: props.readonly, //是否只讀  取值 true | false

  })
  editor.value.onDidChangeModelContent((val: any) => {
    //內(nèi)容改變時給父組件實時返回值
    emit('editorChange', toRaw(editor.value).getValue())
  })
})
watchEffect(()=>{ // 監(jiān)聽父組件值的變化,重新賦值給編輯器
  if(editor.value)
    toRaw(editor.value).setValue(props.value)
})
</script>

<style scoped>
.editorapp {
  height: 100%;
  width: 100vh;
}
</style>

效果:
monaco editor 代碼提示,javascript,開發(fā)語言,ecmascript
monaco editor 代碼提示,javascript,開發(fā)語言,ecmascript

六、實際問題

沒有實現(xiàn)雙向綁定。子組件給父組件傳值會比較麻煩,如果是很多頁面都要使用的話要重復寫很多接收參數(shù)的方法,并且重復定義很多額外的參數(shù)來接收子組件的傳值

父組件中定義額外的參數(shù)來接收子組件的傳值的原因:
monaco editor 代碼提示,javascript,開發(fā)語言,ecmascript
修改:
父組件中:改為v-model,不再需要editorChange方法了

<monaco-editor ref="monacoEdit" v-model="data" :readonly="false" main="javascript" bgcolor="vs-dark'" />

子組件中:(只顯示修改的部分)文章來源地址http://www.zghlxwxcb.cn/news/detail-602245.html

// 編輯器避免重復賦值
watchEffect(() => {
  if (editor.value && toRaw(editor.value).getValue() !== props.modelValue)
    toRaw(editor.value).setValue(props.modelValue)
})
const emit = defineEmits<{
  (e: 'update:modelValue', value: string): void
}>()
// 監(jiān)聽值的變化
  editor.value.onDidChangeModelContent((val: any) => {
    // 給父組件實時返回最新文本
    emit('update:modelValue', toRaw(editor.value).getValue())
  })

到了這里,關(guān)于Monaco Editor安裝,vue3中使用,自定義高亮,自定義提示,附完整代碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • Monaco Editor教程(二十):在編輯器的某個特定位置插入自定義的dom內(nèi)容,圖片,表單,表格,視頻

    哇咔咔,這是我的第20篇Monaco教程,寫完這一篇會暫時休息一段時間,練練字,存存稿,讀讀書,順便修修文章。 目前全網(wǎng)成系統(tǒng)的monaco中文專欄應該只有我這一個,歡迎評論區(qū)打臉。自結(jié)束了GitLab CI/CD的專欄后,我就一直在利用業(yè)余時間學習Monaco相關(guān)的知識,一是為了彌補

    2023年04月16日
    瀏覽(78)
  • Monaco-editor使用指南,遇到問題詳解

    Monaco-editor使用指南,遇到問題詳解

    初始化對象 這里需要引入你需要的語言包 complete.js如下 kind為枚舉類型,可根據(jù)自定義提示類型選擇,可選值有 Function、Keyword、Text、Method、Event、Operator、Unit、Value、Constant、Enum、Color等 QQ錄屏20230215171945 我這里是在vs-dark的基礎上定義的主題配色 效果圖如下 圖上是json的效果

    2024年02月06日
    瀏覽(24)
  • 使用 monaco-editor-nls 漢化 右鍵菜單漢化部分失敗原因

    使用 monaco-editor-nls 漢化 右鍵菜單漢化部分失敗原因

    首先使用npm或者其他包管理工具安裝依賴插件: 如果右鍵菜單漢化一部分失敗,首先去項目下看node_modules/monaco-editor-nls/locale/zh-hans中搜vs/editor/contrib/format/看一下是否有路徑有brower字段,再去對應的node_modules/monaco-editor/esm/vs/editor/contrib/format/formatActions看是否有brower文件夾 如果

    2024年02月15日
    瀏覽(21)
  • monaco,monaco-editor,monaco-editor-webpack-plugin,

    \\\"Monaco\\\"是包含了Monaco Editor和Monaco Language Server兩個項目的總稱,而\\\"Monaco Editor\\\"是Monaco項目中的一個部分,它是一款基于Web技術(shù)的高性能代碼編輯器。 Monaco Language Server是一個支持多種語言的語言服務器,可以提供語法分析、代碼補全、錯誤檢查、重構(gòu)等功能。Monaco Editor可以與

    2024年02月14日
    瀏覽(44)
  • react-app框架——使用monaco editor實現(xiàn)online編輯html代碼編輯器

    react-app框架——使用monaco editor實現(xiàn)online編輯html代碼編輯器

    大家好,我是yma16,本文分享關(guān)于 react-app框架——使用monaco editor實現(xiàn)online編輯html代碼編輯器。 monaco editor 編輯器 Monaco Editor是一款功能強大的Web編輯器,由微軟開發(fā)并使用在多個項目中。它是基于VS Code編輯器的核心組件,具有類似的功能和用戶體驗。 Monaco Editor具有以下特點

    2024年01月20日
    瀏覽(40)
  • vue3 wangeditor/editor富文本使用和編輯

    vue3 wangeditor/editor富文本使用和編輯

    第一步:安裝 第二步:使用 最終效果圖: 第四:工具欄配置 進入官方demo:https://www.wangeditor.com/demo/index.html 打開之后,按F12在控制臺輸入 toolbar.getConfig()? 查看工具欄的默認配置。 ?如果有不需要的工具欄項,可以在? toolbarConfig.excludeKeys 中配置

    2024年01月21日
    瀏覽(22)
  • uniapp小程序,H5,Editor組件封裝、使用及回顯(vue3)

    uniapp小程序,H5,Editor組件封裝、使用及回顯(vue3)

    官網(wǎng)鏈接:editor | uni-app官網(wǎng) (dcloud.net.cn)

    2024年02月01日
    瀏覽(24)
  • 代碼編輯器之monaco editor

    代碼編輯器之monaco editor

    (一)簡介 底層vscode開發(fā)的一款編輯器,各方面的樣式功能基本與vscode一致。 (二)官方文檔 Monaco Editor (microsoft.github.io) (三)安裝 安裝時兩者版本要對應,對應表在后面 (四)屬性 以下是較為常見的屬性 (五) 方法 挑選出使用頻次較高的 monaco.editor.setTheme(‘主題色名字

    2024年02月21日
    瀏覽(21)
  • Monaco Editor教程(二二):monaco編輯器完整配置翻譯及重點配置解析

    本篇文章講解一下創(chuàng)建Monaco編輯器時所有完整配置,算是一個比較淺顯的入門文章。 但由于一個Monaco的配置項多達150個,整篇文章耗費了我5天的下班時間,請讀者自行點贊收藏。這里結(jié)合實際的項目業(yè)務場景介紹一些常用的,有可能修改的默認配置參數(shù)。Monaco已經(jīng)默認了很

    2023年04月25日
    瀏覽(46)
  • 解決Vue3 使用Element-Plus導航刷新active高亮消失

    解決Vue3 使用Element-Plus導航刷新active高亮消失

    啟用路由模式會在激活導航時以 index 作為 path 進行路由跳轉(zhuǎn) 使用 default-active 來設置加載時的激活項。 接下來打印一下選中項index和index路徑, 刷新也是沒有任何問題的,active不會消失,整體代碼如下:

    2024年02月14日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包