前端编辑器JSON HTML等,vue2-ace-editor,vue3-ace-editor

与框架无关

vue2-ace-editor有问题,ace拿不到(brace)

一些组件都是基于ace-builds或者brace包装的

不如直接用下面的,不如直接使用下面的

javascript 复制代码
<template>
  <div ref="editor" class="json-editor"></div>
</template>

<script>
import ace from 'ace-builds';
import 'ace-builds/webpack-resolver'; // 确保 Webpack 正确解析模块
import 'ace-builds/src-noconflict/mode-json'; // 引入 JSON 模式
import 'ace-builds/src-noconflict/theme-monokai'; // 引入 Monokai 主题

export default {
  name: 'JsonEditor',
  props: {
    value: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      editor: null,
    };
  },
  watch: {
    value(newValue) {
      if (newValue !== this.editor.getValue()) {
        this.editor.setValue(newValue, 1); // 1 表示不触发 change 事件
      }
    },
  },
  mounted() {
    this.initEditor();
  },
  beforeDestroy() {
    if (this.editor) {
      this.editor.destroy();
      this.editor = null;
    }
  },
  methods: {
    initEditor() {
      this.editor = ace.edit(this.$refs.editor, {
        mode: 'ace/mode/json',
        theme: 'ace/theme/monokai',
        tabSize: 2,
        useWorker: false, // 禁用 worker 以避免 JSON 解析错误时的警告
        minLines: 10,
        maxLines: 30,
        fontSize: '14px',
        showPrintMargin: false,
      });

      this.editor.setValue(this.value, 1); // 初始化值

      this.editor.getSession().on('change', () => {
        this.$emit('input', this.editor.getValue());
      });
    },
  },
};
</script>

<style scoped>
.json-editor {
  width: 100%;
  height: 400px;
}
</style>

解决光标错位

css 复制代码
.ace_editor,
.ace_editor * {
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Droid Sans Mono', 'Consolas', monospace !important;
  font-size: 12px !important;
  font-weight: 400 !important;
  letter-spacing: 0 !important;
}
相关推荐
用户059540174468 分钟前
把AI对话记忆测试从手动比对到Pytest+Redis自动化,验证效率提升了10倍,还顺带揪出3个隐蔽bug
前端·css
我星期八休息12 分钟前
网络编程—网络层
开发语言·前端·网络·人工智能·智能路由器
A242073493035 分钟前
React中请求拦截与响应拦截的统一处理方案
前端·javascript·react.js
IT_陈寒1 小时前
为什么我的Java Stream流操作会吃掉内存?
前端·人工智能·后端
嘟嘟07172 小时前
React 受控组件与非受控组件:表单数据到底归谁管?
前端·javascript·react.js
嘟嘟07172 小时前
React 表单管理进阶:从单字段到带校验的完整登录表单
前端·javascript·react.js
光影少年2 小时前
react navite原生事件监听、全局事件通知
前端·react native·react.js
lvv2 小时前
不会被渲染的组件,为什么让我的首屏白屏了?(一次前端问题总结)
前端·webpack·性能优化
码上成长3 小时前
微前端 Invalid hook call?先把「window 注入 + externals」整明白
前端·react.js·前端框架
宿6743 小时前
vue3-DOM树
前端·javascript·vue.js