vue-codemirror实现代码编辑器

创新项目实训

任务

已经实现了大部分数据的可视化,现在有了新的需求,如何实现对图表的结构化数据进行在线编辑,并能够实时运行展示,并保存到数据库中,因此我们需要实现一个在线编辑器,并能够对图表进行实时更新。

vue-codemirror
  • 介绍,就不多说了,请看官网,下面介绍实现对JSON的在线编辑(与常规的vue-codemirror略有不同),可以参考文章
  • 引入各种样式,语言等信息
    // require styles
    import 'codemirror/lib/codemirror.css'
    // 引入语言
    import 'codemirror/mode/javascript/javascript.js'
    // 引入样式
    import 'codemirror/theme/juejin.css'
    import 'codemirror/theme/rubyblue.css'
    import 'codemirror/theme/idea.css'
    // JSON
    import 'codemirror/addon/lint/lint.css'
    import 'codemirror/addon/lint/json-lint'
    import CodeMirror from 'codemirror'
    
  • 定义容器
     <div class="json-editor">
        <textarea ref="textarea" />
      </div>
    
  • 容器初始化
        initEditor() {
        this.jsonEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
          lineNumbers: true,
          mode: 'application/json',
          gutters: ['CodeMirror-lint-markers'],
          theme: 'idea',
          lint: true,
        });
    
        // 设置编辑器值
        this.jsonEditor.setValue(this.value);
        console.log(this.value)
        this.jsonEditor.on('change', cm => {
          this.$emit('changed', cm.getValue());
          this.$emit('input', cm.getValue());
        });
        },
    
  • 定义出其他方法
    	getValue () {
    		//获得编辑器中的内容
          return this.jsonEditor.getValue()
        },
    
  • 实现效果
    在这里插入图片描述
图表的实时更新
  • 对图表的生命周期了解即可。如init,setOption,dispose等。
        //运行代码
        runCode(){
            let code = this.getValue()
            this.dataClean(code)
            console.log(this.chartType)
            this.handleOptionBeforeSet()
            this.chartInstance.setOption(this.decoration);
            //this.chartInstance.setOption(code)
        },
    
图表相关信息的保存
  • 常规操作,调用后端接口即可。
        submitUpdateChartData(){
            this.$confirm('此操作将永久修改图表配置, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
                updateChartData({data:{chartId:this.chartInfo.id,chartData:this.getValue()}}).then(response=>{
                    if(response.data===1){
                        this.$message({
                        type: 'success',
                        message: '修改成功!'
                    });
                    }else{
                        this.$message({
                        type: 'warning',
                        message: '修改失败!'
                    });
                    }
                })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消更新'
              });          
            });
        },
        submitUpdateChartInfo(){
            this.$confirm('此操作将永久修改图表信息, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
                updateChartInfo({data:{chartId:this.chartInfo.id,chartName:this.updateForm.name,genResult:this.updateForm.result}}).then(response=>{
                    if(response.data===1){
                        this.$message({
                        type: 'success',
                        message: '修改成功!'
                    });
                    this.dialogVisible=false
                    }else{
                        this.$message({
                        type: 'warning',
                        message: '修改失败!'
                    });
                    }
                })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消更新'
              });          
            });
        },
    
  • 页面展示
    在这里插入图片描述
  • 18
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue-codemirror是一个基于Vue.js代码编辑器组件,提供了一个用户友好的界面和丰富的功能来编辑各种编程语言的代码。引用中的内容是Vue-codemirror组件的一些属性和配置选项。 其中,value属性用于设置编辑器的初始内容,可以是代码字符串或者是从服务器加载的代码文件。这样可以在编辑器中显示已有的代码。 Mode属性用于设置编译器的编程语言关联内容,通过引入对应的模块或包,使得编辑器能够识别和高亮显示特定编程语言的语法。 Theme属性用于设置编辑器的主题,可以选择不同的主题样式来满足用户的个性化需求。 tabSize属性用于设置tab的空格宽度,可以根据用户的喜好和项目要求来调整。 lineNumbers属性用于决定是否在编辑器中显示行号,方便用户进行代码的定位和调试。 smartIndent属性用于控制自动缩进的开关,当该选项打开时,编辑器会根据上一行的缩进自动调整下一行的缩进。 indentUnit属性用于设置缩进的单位,可以是空格或者制表符,根据项目的要求来进行设置。 keyMap属性用于设置快捷键,可以自定义或选择预设的快捷键方案,以提高代码编辑的效率。 通过配置这些属性和选项,可以使得vue-codemirror代码编辑器满足不同开发需求,并提供良好的编码体验。<span class="em">1</span> #### 引用[.reference_title] - *1* [vue codemirror 代码编辑器](https://blog.csdn.net/qq_42181155/article/details/121253626)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值