当前位置:   article > 正文

vue.js AEC代码编辑器_vue集成js脚本编辑器

vue集成js脚本编辑器

AEC代码编辑器

安装

npm install ace-builds --save-dev
  • 1

组件

  <div class="ace-container">
    <div class="event-panel">
      <div>
        <div class="_li">
          <label class="title">语言</label>
          <el-select class="_li_val" v-model="modePath" @change="handleModelPathChange" size="mini" value-key="name">
            <el-option v-for="mode in modeArray" :key="mode.name" :label="mode.name" :value="mode.path">
            </el-option>
          </el-select>
        </div>
        <div class="_li">
          <label class="title">参数</label>
          <el-input class="_li_val" placeholder="请输入内容" size="mini" v-model="inputDa" clearable>
          </el-input>
        </div>
      </div>
    </div>
    <div class="ace-editor" ref="ace"></div>
    <div class="_li result ma-t-20">
      <el-button class="ma-r-20" type="primary" size="mini" plain @click="debEvel">调试</el-button>
      <label class="title">结果:</label>
      <span class="item-li" :class="isValidate ? 'item-c' : 'item-warning'">{{ resultDa }}</span>
    </div>
  </div>
</template>
<script>
import ace from "ace-builds";
import "ace-builds/src-noconflict/snippets/javascript";
import "ace-builds/src-noconflict/snippets/html";
import "ace-builds/src-noconflict/snippets/css";
import "ace-builds/src-noconflict/snippets/scss";
import "ace-builds/src-noconflict/snippets/json";
import "ace-builds/src-noconflict/snippets/java";
import "ace-builds/src-noconflict/snippets/text";


import "ace-builds/src-noconflict/theme-xcode";
import "ace-builds/src-noconflict/mode-javascript";
import "ace-builds/src-noconflict/mode-json"
import "ace-builds/src-noconflict/theme-monokai";
import "ace-builds/src-noconflict/ext-language_tools";
// 解决build打包时包多大的问题(webpack-resolver原始包里引入所以的js文件,这里只引入了我们需要的js)
ace.config.setModuleUrl('ace/mode/javascript_worker', require('file-loader?esModule=false!ace-builds/src-noconflict/worker-javascript.js'))
ace.config.setModuleUrl('ace/theme/xcode', require('file-loader?esModule=false!ace-builds/src-noconflict/theme-xcode.js'))
ace.config.setModuleUrl('ace/ext/language_tools', require('file-loader?esModule=false!ace-builds/src-noconflict/ext-language_tools.js'))
ace.config.setModuleUrl('ace/snippets/javascript', require('file-loader?esModule=false!ace-builds/src-noconflict/snippets/javascript.js'))
ace.config.setModuleUrl('ace/mode/json_worker', require('file-loader?esModule=false!ace-builds/src-noconflict/worker-json.js'))


const modeArray = [
  {
    name: "JavaScript",
    path: "ace/mode/javascript",
  },
  {
    name: "Json",
    path: "ace/mode/json",
  },
  {
    name: "Text",
    path: "ace/mode/text",
  },
];

export default {
  props: {
    value: String,
  },
  mounted() {
    this.aceEditor = ace.edit(this.$refs.ace, {
      maxLines: 20,
      minLines: 10,
      fontSize: 14,
      value: this.value ? this.value : "",
      theme: this.themePath,
      mode: this.modePath,
      // wrap: this.wrap,
      tabSize: 4,
    });
    // 激活自动提示
    this.aceEditor.setOptions({
      enableSnippets: true,
      enableLiveAutocompletion: true,
      enableBasicAutocompletion: true,
    });
    // // 事件监听
    this.aceEditor.getSession().on("change", this.change);
    // 默认换行
    this.aceEditor.getSession().setUseWrapMode(true);
    // 默认代码
    this.aceEditor.getSession().setValue(this.modelAce);
  },
  data() {
    return {
      aceEditor: null,
      toggle: true,
      wrap: true,
      themePath: "ace/theme/monokai",
      modePath: "ace/mode/javascript",
      modeArray: modeArray,
      resultDa: "",
      inputDa: '',
      modelAce: '\nfunction hanlde(data){\n\t//请输入处理脚本\n\t\n\treturn data;\n}',
      isValidate: true,
    };
  },
  methods: {
    toggleConfigPanel() {
      this.toggle = !this.toggle;
    },
    change() {
      let das = this.aceEditor.getSession().getValue()
      console.log(das, '监听输入')
    },
    // 语言
    handleModelPathChange(modelPath) {
      this.aceEditor.getSession().setMode(modelPath);
    },
    // 调试 debugger
    debEvel() {
      try {
        this.isValidate = true;
        const func = this.aceEditor.getSession().getValue();
        const funs = '(' + func + ')(' + JSON.stringify(this.inputDa) + ')';
        this.resultDa = eval(funs);

      } catch {
        this.isValidate = false;
        this.resultDa = "执行异常!请检查执行代码块!";
      }
    },

  },
};
</script>
   
<style lang='scss' scoped>
.ma-t-20 {
  margin-top: 20px;
}

.ma-l-20 {
  margin-left: 20px;
}

.ma-r-20 {
  margin-right: 20px;
}

.ace-editor {
  // height: 300px !important;
  width: 600px;
}
.item-warning {
		color: red;
	}

	.item-c {
		color: #333;
	}

.ace-container {
  .event-panel {
    ._li {
      display: flex;
      margin: 20px 10px;

      ._li_val {
        width: 200px;
      }

      .title {
        margin: 0 10px;
        font-size: 14px;
      }
    }
  }
}
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180

效果

在这里插入图片描述

效果地址

在线效果:https://www.w3cschool.cn/tryrun/runcode?lang=javascript

测试:https://ace.c9.io/build/kitchen-sink.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/415403
推荐阅读
相关标签
  

闽ICP备14008679号