<template>
    <div class="monacoCode" ref="main" >

    </div>
</template>

<script setup>
import { ref, onMounted, watch, shallowRef } from "vue";
import * as monaco from "monaco-editor";
import store from '@/stores/blockly'

const monacoEditor = shallowRef(null);
const main = ref(null);
const storePythonCode = store.useyPythonCodeStore()

const props = defineProps({
    pythonCode: {
        type: String,
        require: true,
        default: ""
    },
    codeMirrorShow: {
        type: Boolean,
        require: true,
        default: true
    }
});

onMounted(() => {
    initmonaco();
});

const initmonaco = () => {
    monacoEditor.value = monaco.editor.create(main.value, {
        theme: "dark", // 主题
        value: props.pythonCode, // 默认显示的值
        language: "python", // 语言
        folding: true, // 是否折叠
        foldingHighlight: true, // 折叠等高线
        foldingStrategy: "auto", // 折叠方式  auto | indentation
        showFoldingControls: "always", // 是否一直显示折叠 always | mouseover
        disableLayerHinting: true, // 等宽优化
        emptySelectionClipboard: false, // 空选择剪切板
        selectionClipboard: false, // 选择剪切板
        automaticLayout: true, // 自动布局
        codeLens: false, // 代码镜头
        scrollBeyondLastLine: false, // 滚动完最后一行后再滚动一屏幕
        colorDecorators: true, // 颜色装饰器
        accessibilitySupport: "auto", // 辅助功能支持  "auto" | "off" | "on"
        lineNumbers: "on", // 行号 取值： "on" | "off" | "relative" | "interval" | function
        lineNumbersMinChars: 5, // 行号最小字符   number
        enableSplitViewResizing: false,
        readOnly: props.codeShow, //是否只读  取值 true | false
        minimap: { // 关闭小地图
            enabled: false,
        },
    });

    monacoEditor.value.onDidChangeModelContent(() => {
        // console.log(monacoEditor.value.getValue(), 11111111111);
        storePythonCode.pythonCode = monacoEditor.value.getValue()
    });
};


watch(() => [props.pythonCode, props.codeMirrorShow], ([newPythonCode, newCodemirrorShow]) => {
    if (monacoEditor.value) {
        monacoEditor.value.setValue(newPythonCode);
        monacoEditor.value.updateOptions({
            readOnly: newCodemirrorShow
        });
    }
});
</script>

<style lang="scss">
.monacoCode {
    width: 100%;
    height: calc(100% - 50px);

    .monaco-editor {
        width: 100% !important;
        height: 100% !important;

        .overflow-guard {
            width: 100% !important;
            height: 100% !important;

            // .slider-mouseover {
            //     display: none !important;
            // }
        }
    }
}
</style>