<template>
  <div class="codeEditorAlertBox" v-if="showCurrentTips || showDefaultTips">
    <el-alert
      :title="
        tip ||
        `// 当前输入语言为 ${
          language === 'get' || language === 'post' ? 'javascript' : language
        } , 请注意书写规范。`
      "
      v-if="showCurrentTips"
    />
    <el-alert :title="defaultTips" v-if="showDefaultTips" />
  </div>
  <div id="codeEditBox" :style="`height:${height ? height : '500px'}`"></div>
</template>

<script>
import * as monaco from "monaco-editor";
import { toRaw } from "vue";
export default {
  props: {
    height: {
      type: String,
      default: "500px",
    },
    tip: {
      type: String,
      default: () => "",
    },
    code: {
      type: String,
      default: () => "",
    },
    showCurrentTips: {
      type: Boolean,
      default: () => true,
    },
    showDefaultTips: {
      type: Boolean,
      default: () => true,
    },
    language: {
      type: String,
      default: "javascript",
    },
  },
  data() {
    return {
      editorValue: "",
      monacoEditor: null,
      defaultTips:
        "// 每一种类型的事件都携带 2 个内置变量，变量名称为 [e 和 target] ，分别表示 鼠标原生event对象 和 精简后的组件对象(包含事件发生位置在页面内的坐标与触发事件的组件)，如若需要，可自行直接使用其名称来进行代码书写。",
      activePen: {},
    };
  },
  mounted() {
    this.editorValue = this.code || "";
    this.init();
  },
  unmounted() {
    this.getEditorValue();
  },
  methods: {
    init() {
      // 使用 - 创建 monacoEditor 对象
      const language =
        this.language === "get" || this.language === "post"
          ? "javascript"
          : this.language;
      this.monacoEditor = monaco.editor.create(
        document.getElementById("codeEditBox"),
        {
          theme: "vs-dark", // 主题
          value: this.editorValue, // 默认显示的值
          language: language || "javascript",
          folding: true, // 是否折叠
          foldingHighlight: true, // 折叠等高线
          foldingStrategy: "indentation", // 折叠方式  auto | indentation
          showFoldingControls: "always", // 是否一直显示折叠 always | mouseover
          disableLayerHinting: true, // 等宽优化
          emptySelectionClipboard: false, // 空选择剪切板
          selectionClipboard: false, // 选择剪切板
          automaticLayout: true, // 自动布局
          codeLens: false, // 代码镜头
          scrollBeyondLastLine: false, // 滚动完最后一行后再滚动一屏幕
          colorDecorators: true, // 颜色装饰器
          accessibilitySupport: "off", // 辅助功能支持  "auto" | "off" | "on"
          lineNumbers: "on", // 行号 取值： "on" | "off" | "relative" | "interval" | function
          lineNumbersMinChars: 5, // 行号最小字符   number
          enableSplitViewResizing: false,
          readOnly: false, //是否只读  取值 true | false
        }
      );

        this.monacoEditor.onDidBlurEditorText(() =>{
            this.getEditorValue()
        })
      
    },

    getEditorValue() {
      this.$emit("change", toRaw(this.monacoEditor).getValue());
    },

    setEditorValue(newValue) {
      toRaw(this.monacoEditor).setValue(newValue);
      this.getEditorValue();
    },
  },

  watch: {
    code(value) {
      this.editorValue = value;
      toRaw(this.monacoEditor).setValue(this.editorValue);
    },
  },
};
</script>

<style lang="less" scoped>
#codeEditBox {
  width: 100%;
}

.codeEditorAlertBox {
  .el-alert {
    font-family: Consolas, "Courier New", monospace;
    font-weight: normal;
    font-size: 14px;
    font-feature-settings: "liga" 0, "calt" 0;
    line-height: 19px;
    width: 100%;
    background: #1e1e1e;
    color: #608b4e;
    text-indent: 4em;
    user-select: none;
    padding: 0 0 8px 0;
    border-radius: 0;
  }

  .el-alert.danger {
    color: #f25656;
    font-weight: 700;
    user-select: auto;
  }

  .el-alert:first-child {
    padding-top: 8px;
  }
}
</style>

<style lang="less">
.codeEditorAlertBox {
  .el-alert {
    .el-alert__content {
      width: calc(100% - 16px);
    }

    .el-alert__title {
      width: 98%;
      display: flex;
    }
  }
}
</style>