Commit f42183fd authored by Denys Mishunov's avatar Denys Mishunov Committed by Alexander Turinske

Switching the custom editor to Source Editor

Instead of a custom Monaco implementation we
switch to the unified Source Editor
parent f7648c58
---
title: 'Policy Editor: switching the custom editor to Source Editor'
merge_request: 57508
author:
type: changed
<script> <script>
import { editor as monacoEditor } from 'monaco-editor'; import EditorLite from '~/vue_shared/components/editor_lite.vue';
export default { export default {
components: {
EditorLite,
},
props: { props: {
value: { value: {
type: String, type: String,
...@@ -12,56 +15,38 @@ export default { ...@@ -12,56 +15,38 @@ export default {
required: false, required: false,
default: true, default: true,
}, },
height: {
type: Number,
required: false,
default: 300,
},
},
data() {
return { editor: null };
},
watch: {
value(val) {
if (val === this.editor.getValue()) return;
this.editor.setValue(val);
},
}, },
beforeDestroy() { computed: {
this.editor.dispose(); editorOptions() {
}, return {
mounted() {
if (!this.editor) {
this.setupEditor();
}
},
methods: {
setupEditor() {
this.editor = monacoEditor.create(this.$refs.editor, {
value: this.value,
language: 'yaml',
lineNumbers: 'off', lineNumbers: 'off',
minimap: { enabled: false }, minimap: { enabled: false },
folding: false, folding: false,
renderIndentGuides: false, renderIndentGuides: false,
renderWhitespace: 'boundary', renderWhitespace: 'boundary',
renderLineHighlight: 'none', renderLineHighlight: 'none',
glyphMargin: false,
lineDecorationsWidth: 0, lineDecorationsWidth: 0,
lineNumbersMinChars: 0, lineNumbersMinChars: 0,
occurrencesHighlight: false, occurrencesHighlight: false,
hideCursorInOverviewRuler: true, hideCursorInOverviewRuler: true,
overviewRulerBorder: false, overviewRulerBorder: false,
readOnly: this.readOnly, readOnly: this.readOnly,
}); };
this.editor.onDidChangeModelContent(() => { },
this.$emit('input', this.editor.getValue()); },
}); methods: {
notifyAboutUpdates(val) {
this.$emit('input', val);
}, },
}, },
}; };
</script> </script>
<template> <template>
<div ref="editor" class="gl-overflow-hidden" :style="{ height: `${height}px` }"></div> <editor-lite
:value="value"
file-name="*.yaml"
:editor-options="editorOptions"
@input="notifyAboutUpdates"
/>
</template> </template>
...@@ -365,7 +365,6 @@ export default { ...@@ -365,7 +365,6 @@ export default {
> >
{{ s__('NetworkPolicies|YAML editor') }} {{ s__('NetworkPolicies|YAML editor') }}
</h5> </h5>
<div class="gl-p-4">
<network-policy-editor <network-policy-editor
data-testid="network-policy-editor" data-testid="network-policy-editor"
:value="yamlEditorValue" :value="yamlEditorValue"
...@@ -376,7 +375,6 @@ export default { ...@@ -376,7 +375,6 @@ export default {
</div> </div>
</div> </div>
</div> </div>
</div>
<hr /> <hr />
<div class="row"> <div class="row">
<div class="col-md-auto"> <div class="col-md-auto">
......
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import NetworkPolicyEditor from 'ee/threat_monitoring/components/network_policy_editor.vue'; import NetworkPolicyEditor from 'ee/threat_monitoring/components/network_policy_editor.vue';
import EditorLite from '~/vue_shared/components/editor_lite.vue';
describe('NetworkPolicyEditor component', () => { describe('NetworkPolicyEditor component', () => {
let wrapper; let wrapper;
...@@ -10,6 +11,9 @@ describe('NetworkPolicyEditor component', () => { ...@@ -10,6 +11,9 @@ describe('NetworkPolicyEditor component', () => {
value: 'foo', value: 'foo',
...propsData, ...propsData,
}, },
stubs: {
EditorLite,
},
}); });
}; };
...@@ -23,22 +27,19 @@ describe('NetworkPolicyEditor component', () => { ...@@ -23,22 +27,19 @@ describe('NetworkPolicyEditor component', () => {
}); });
it('renders container element', () => { it('renders container element', () => {
expect(wrapper.find({ ref: 'editor' }).exists()).toBe(true); expect(wrapper.findComponent(EditorLite).exists()).toBe(true);
}); });
it('initializes monaco editor with yaml language and provided value', () => { it('initializes monaco editor with yaml language and provided value', () => {
const { const editor = wrapper.findComponent(EditorLite).vm.getEditor();
vm: { editor },
} = wrapper;
expect(editor).not.toBe(null);
expect(editor.getModel().getModeId()).toBe('yaml'); expect(editor.getModel().getModeId()).toBe('yaml');
expect(editor.getValue()).toBe('foo'); expect(editor.getValue()).toBe('foo');
}); });
it('emits input event on file changes', () => { it("emits input event on editor's input", async () => {
wrapper.vm.editor.setValue('bar'); const editor = wrapper.findComponent(EditorLite);
editor.vm.$emit('input');
await wrapper.vm.$nextTick();
expect(wrapper.emitted().input).toBeTruthy(); expect(wrapper.emitted().input).toBeTruthy();
expect(wrapper.emitted().input.length).toBe(1);
expect(wrapper.emitted().input[0]).toEqual(['bar']);
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment