组件简介
vue-code-view
是一个基于 vue 2.x
、轻量级的代码交互组件,在网页中实时编辑运行代码、预览成果的代码交互组件。
应用此组件, 不管 vue
页面还是 Markdown
文档中的示例代码,成果如下:
组件的由来
当我的项目中页面或者 Markdown
文档蕴含大量代码时,应用 highlight.js
进行代码高亮后极大的增大了浏览性,然而当咱们浏览时想要对以后代码进行编辑调试时,只能关上本地开发环境或者跳转至 codepen
codesandbox
等在线我的项目示例。即便是很简略的代码示例依然防止不了上述场景的繁琐步骤!如果遇到网络不好,或者本地开发环境没有装置配置的状况,那就很遗憾了!
目前大多开源我的项目的 Markdown
文档示例大多反对了示例代码的实时渲染,能够在文档页面中看到源码的运行成果,提供了在线我的项目的跳转性能。当须要调试代码时,还是须要反复上述步骤,体验不是太敌对。
那么能不能有这么一个组件能反对在页面中编辑代码,实时运行预览成果?在网络找了良久,没有找到 vue
版本,只看到了 react-code-view,受其启发,自已编写了一个 vue
版本组件 vue-code-view
!
组件性能
目前组件已实现的次要性能个性:
- 代码能够在线编辑,实时预览成果。
- 代码编辑器反对代码高亮、光标行背景高亮、括号/标签匹配主动敞开、代码折叠。
- 基于vue的
SFC
解析,反对<template>
<script>
<style>
代码逻辑。 - 反对
<style>
CSS 预处理,目前实现sass
。 - 反对
Markdown
示例实时渲染,须要自定义 loader 。
组件props
参数 | 阐明 | 类型 | 默认值 | |
---|---|---|---|---|
theme | theme mode,反对 light / dark | light \ | dark | dark |
showCode | 是否显示代码编辑器 | boolean | false | |
source | 示例代码 | string | - | |
renderToolbar | 自定义工具栏展现 | function | - | |
errorHandler | 谬误处理函数 | function | - | |
debounceDelay | 错误处理防抖提早(ms) | number | 300 |
我的项目资源列表
- Github我的项目地址:vue-code-view
- 我的项目网站及在线示例: vue-code-view
- NPM地址: vue-code-view
- CodeSandbox 在线我的项目示例:-
应用示例
装置
应用 npm
或 yarn
装置组件包。
npm i vue-code-view# oryarn add vue-code-view
Vue 配置
组件应用蕴含运行时编译器的 Vue 构建版本,所以须要独自配置下。
若应用 vue cli
,须要在vue.config.js
文件进行如下配置:
module.exports = { runtimeCompiler: true, // or chainWebpack: (config) => { config.resolve.alias .set("vue$", "vue/dist/vue.esm.js"); },};
组件引入
在我的项目的入口文件 main.js
中引入组件及款式,注册组件。
import Vue from "vue";import App from "./App.vue";import CodeView from "vue-code-view"; import "vue-code-view/lib/vue-code-viewer.css";...Vue.use(CodeView);...
组件应用
应用组件的source
属性传入示例代码。
示例代码格局反对 <template>
<script>
<style>
,<template>
不能为空;暂不反对JSX
语法。
<template> <div id="app"> <code-viewer :source="code_example"></code-viewer> </div></template><script>const code_source = ` <template> <div id="app"> <img alt="Vue logo" class="logo" src="https://cn.vuejs.org/images/logo.svg" /> <h1>Welcome to Vue.js {{version}} !</h1> </div></template><script>export default { data() { return { version: '2.x' }; }, };<\/script><style> .logo { width:66px;}</style> `, export default { data() { return { code_example: code_source }; }, };</script>
JSX
应用形式
组件 JSX
语法应用形式。
<script>const code_example = `<template> <div id="app"> <img alt="Vue logo" class="logo" src="https://cn.vuejs.org/images/logo.svg" /> <h1>Welcome to Vue.js !</h1> </div></template> `;export default { name: "demo", render() { return ( <div > <code-viewer source={code_example} showCode={false} ></code-viewer> </div> ); },};</script>
组件库混合应用
我的项目引入其余组件库后,组件的示例源代码中间接应用即可,实现预览调试性能。
错误处理
组件内置了谬误预处理,目前反对代码为空、代码格局谬误(<template>
内容不存在)等,以文字的模式显示在示例区域,也提供了自定义谬误形式 errorHandler
(应用 Notice
组件进行信息告知)。
render() { return ( <div > <code-viewer source={code_example} showCode={false} errorHandler={(errorMsg) => { this.$notify.error({ title: "Info", message: errorMsg, }); }} ></code-viewer> </div> )}
示例应用了antd vue
的 notify
组件进行音讯揭示,成果如下:
示例成果
具体示例成果详见 组件Markdown阐明文档
其余
后续性能继续迭代中!激情期待。