monaco editor性能优化

适用场景

一个项目使用多个editor

优化

1.单例模式:定义editor类,对实例进行缓存,提高复用率
2.通过append实例dom,尽量减少editor的重复创建

代码

import Editor from './MonacoEditor'
import Vue from 'vue'

let editorCache = {}

class Editor_ {
  constructor () {
    this.instance = new Vue({
      name: 'EditorInstance',
      data () {
        return {
          value: '',
          language: '',
          readOnly: false,
          minimap: false,
          suggestions: []
        }
      },
      watch: {
        value (value) {
          this.$emit('value-change', value)
        }
      },
      render (h) {
        return h(Editor, {
          props: {
            value: this.value,
            language: this.language,
            readOnly: this.readOnly,
            minimap: this.minimap,
            suggestions: this.suggestions
          },
          on: {
            'update': (value) => {
              this.value = value
            }
          }
        })
      }
    })
    this.instance.$mount()
  }

  /**
   * 初始化渲染editor
   */
  init ($container, props) {
    Object.assign(this.instance.$data, props)
    $container.appendChild(this.instance.$el)
  }
  /**
   * 设置data
   */
  setData (key, value) {
    this.instance[key] = value
  }
  /**
   * 获取data
   */
  getData (key) {
    return this.instance[key]
  }
  /**
   * 删除实例
   */
  destroy () {
    this.instance && this.instance.$destroy()
  }
}
/**
 * 注册
 */
Editor.register = function (name) {
  if (editorCache[name] === undefined) {
    editorCache[name] = new Editor_()
  }
  return editorCache[name]
}

export default Editor

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 关于前端性能优化问题详解 出处:http://segmentfault.com/blogs 前端性能优化指南 AJ...
    bennnnn阅读 1,613评论 2 4
  • 一. Java基础部分.................................................
    wy_sure阅读 3,836评论 0 11
  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,538评论 1 11
  • AJax 优化 缓存 Ajax 请求尽量使用GET, 仅取决于cookie数量 Cookie 优化 减少Cooki...
    KeKeMars阅读 9,377评论 5 89
  • 我从黑夜醒来 注定要承受所有的月光 迷茫也好 孤寂也罢 都是我无怨无悔的选择 我从白昼出发 注定要背负所有的依赖 ...
    在月亮上跳舞_5c7b阅读 180评论 0 0