前端编辑器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;
}
相关推荐
环境栈笔记34 分钟前
多账号浏览器选型检查清单:Profile、权限、Session 和任务日志怎么评估
前端·人工智能·后端·自动化
前端炒粉43 分钟前
手写promise
java·前端·javascript
LaughingZhu1 小时前
智能体经典范式构建:ReAct、Plan-and-Solve 与 Reflection
前端·react.js·前端框架
jsonbro1 小时前
手把手带你实现发布订阅模式
前端·vue.js·设计模式
一只猫的梦1 小时前
自动化模块 (Automation Module)
前端·chrome
熊猫钓鱼>_>1 小时前
Electron:当 Web 技术统治桌面
大数据·前端·javascript·人工智能·架构·electron·agent
德福危险1 小时前
从盲打xss到到模板注入:unix靶机渗透之Tempus_fugit4
前端·unix·xss
JavaGuide1 小时前
又一个画图 Skill 开源,再见手动画 draw.io!
前端·后端·github
诚信定制8392 小时前
使用 CLion 内置性能分析工具观察新特性开销
编辑器
一只猫的梦2 小时前
AI 适配器架构
前端·chrome