<template>
  <div class="yaml-editor">
    <textarea ref="textarea"/>
  </div>
</template>

<script>
import CodeMirror from 'codemirror'
import 'codemirror/addon/lint/lint.css'
import 'codemirror/lib/codemirror.css'
import 'codemirror/theme/rubyblue.css'

export default {
  name: 'YamlEditor',
  /* eslint-disable vue/require-prop-types */
  props: ['value'],
  data() {
    return {
      yamlEditor: false
    }
  },
  watch: {
    value(value) {
      const editor_value = this.yamlEditor.getValue()
      if (value !== editor_value) {
        this.yamlEditor.setValue(this.value)
      }
    }
  },
  mounted() {
    this.yamlEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
      lineNumbers: true,
      mode: 'text/x-yaml',
      gutters: ['CodeMirror-lint-markers'],
      theme: 'rubyblue',
      lint: true
    })

    this.yamlEditor.setValue(this.value)
    this.yamlEditor.on('change', cm => {
      this.$emit('changed', cm.getValue())
      this.$emit('input', cm.getValue())
    })
  },
  methods: {
    getValue() {
      return this.yamlEditor.getValue()
    }
  }
}
</script>

<style>
  .yaml-editor {
    height: 100%;
    position: relative;
  }

  /*.json-editor >>> .CodeMirror {*/
    /*height: 400px;*/
    /*min-height: 300px;*/
  /*}*/

  .yaml-editor >>> .CodeMirror-scroll {
    min-height: 300px;
  }

  .yaml-editor >>> .cm-s-rubyblue span.cm-string {
    color: #F08047;
  }
</style>
