前端编辑器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;
}
相关推荐
运维咖啡吧1 分钟前
周一才上线的网站,单单今天已经超过1000访问了
前端·程序员·ai编程
世界哪有真情4 分钟前
用虚拟IP扩容端口池:解决高并发WebSocket端口耗尽问题
前端·后端·websocket
前端世界5 分钟前
打造一个可维护、可复用的前端权限控制方案(含完整Demo)
前端
LeQi10 分钟前
当!important成为代码毒瘤:你的项目是不是也中了招?
前端·css·程序员
玲小珑12 分钟前
Next.js 教程系列(九)增量静态再生 (ISR):动态更新的静态内容
前端·next.js
Mintopia20 分钟前
B 样条曲线:计算机图形学里的 “曲线魔术师”
前端·javascript·计算机图形学
前端小巷子23 分钟前
跨域问题解决方案:CORS(跨域资源共享)
前端·网络协议·面试
大大。24 分钟前
van-tabbar-item选中active数据变了,图标没变
java·服务器·前端
Mintopia26 分钟前
Three.js 3D 世界中的噪声运动:当数学与像素共舞
前端·javascript·three.js
nc_kai27 分钟前
Flutter 之 每日翻译 PreferredSizeWidget
java·前端·flutter