![Maven Central Adds Sigstore Signature Validation](https://cdn.sanity.io/images/cgdhsj6q/production/7da3bc8a946cfb5df15d7fcf49767faedc72b483-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
x-codemirror-vue3
Advanced tools
CodeMirror 的 vue3 实现
详细可以查看源码中的示例代码
直接用 script 引入 use cdn
<link rel="stylesheet" href="codemirror/lib/codemirror.css">
<script type="text/javascript" src="codemirror.js"></script>
<script type="text/javascript" src="vue.min.js"></script>
<script type="text/javascript" src="lib/xcodemirror.umd.js"></script>
<script type="text/javascript" src="codemirror/mode/..."></script>
NPM
npm install codemirror
npm install x-codemirror-vue3
如果使用 merge 模式,需要
npm install diff-match-patch
全局引用
在main.js中引入 import in main.js
import XCodeMirror from 'x-codemirror-vue3';
vue.use(XCodeMirror);
或者 在组件中引用
import { XCodeMirror } from 'x-codemirror-vue3';
组件中使用
载入主题样式和对应的语言模型 import theme and mode which you want use first
import 'codemirror/lib/codemirror.css' // 主样式
import 'codemirror/theme/[主题].css' // 主题样式,对应 optins 里的 theme
import 'codemirror/mode/[语言].js' // 对应 optins 里的 mode
如果是合并对比模式 if merge type
import 'codemirror/addon/merge/merge.js'
import 'codemirror/addon/merge/merge.css'
import DiffMatchPatch from 'diff-match-patch' // google DiffMatchPatch,用于代码对比
window.diff_match_patch = DiffMatchPatch
window.DIFF_DELETE = -1
window.DIFF_INSERT = 1
window.DIFF_EQUAL = 0
组件 component
<x-code-mirror
:placeholder="" // 提示文描 String 类型,需提取载入 import 'codemirror/addon/display/placeholder.js'
:options="options" // 初始配置,具体配置如下 Object
:events="events" // 需要监听的事件 Array 类型
:code="code" // 初始代码 String 类型
:is-merge="isMerge" 是否为代码对比合并,合并的 options 见下方的 mergeOptions
@change="handleCodeMirror"
/>
options
options: {
mode: 'text/javascript', // 当前编辑器对应的语言
theme: 'eclipse', // 主题必须提取载入 例如:import 'codemirror/theme/eclipse.css',
tabSize: 2,
lineNumbers: true,
line: true,
}
TIPS:options被动态监听,选项的变化,会触发组件的更新,可以动态更改mode来切换不同语言。
events
组件支持的监听的事件,[事件说明 events manual]
可把需要被监听的事件传入组件,也可不传,不传时传空数组[]。
[
'focus',
'blur',
'update'
'scroll',
'changes',
'beforeChange',
'cursorActivity',
'keyHandled',
'inputRead',
'electricInput',
'beforeSelectionChange',
'viewportChange',
'swapDoc',
'gutterClick',
'gutterContextMenu',
'refresh',
'optionChange',
'scrollCursorIntoView',
]
handleCodeMirror(对应emit的方法名,可自定义)
组件里事件返回
handleCodeMirror(emitData) {
const { type, data } = emitData;
switch (type) {
case 'ready': // CodeMirror初始完成,返回的CodeMirror实例对象
this.codeMirrorIns = data;
break;
case 'value':
// data 为当前的 textarea 内容
break;
case ...events: // 事件列表中的事件
// 支持监听事件的返回
console.log(data);
break;
}
}
对比的 options [对比demo]
option: {
value: '<p>hello</p>', // 对比的代码
origLeft: null,
orig: '<p>hello world</p>', //被对比的代码
connect: 'align',
mode: 'text/html',
lineNumbers: true,
collapseIdentical: false,
highlightDifferences: true
},
代码提示 [hint配置]
import "codemirror/addon/hint/show-hint.css"
import "codemirror/addon/hint/show-hint.js"
import "codemirror/addon/hint/[当前mode]-hint.js"
options里配置
options: {
hintOptions: {
completeSingle: false
},
}
TIPS: 这个completeSingle的作用是当只敲击了一个单个的字母时,在不打开提示框的情况下,是否认为这个字母是提示语的一部分。默认为true。 如果是true,那么如果我们只输入了S,输入结束的时候会变成Select。如果为False,那么就不会这样,而是正常的和其他代码编辑器一样单个处理。 所以我们将其设置为 false。
语言模型载入的方式 language mode types
Codemirror language mode have string | object types.
// name
mode: {
name: 'javascript',
json: true
}
// MIME types
mode: 'text/javascript'
// ext
mode: {
ext: 'js'
}
// mime
mode: {
mime: 'text/javascript'
}
// filename
mode: {
filename: 'index.js'
}
FAQs
> CodeMirror 的 vue3 实现 > > 详细可以查看源码中的示例代码
The npm package x-codemirror-vue3 receives a total of 0 weekly downloads. As such, x-codemirror-vue3 popularity was classified as not popular.
We found that x-codemirror-vue3 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.