Commit 8ada13ae authored by Miguel Rincon's avatar Miguel Rincon

Merge branch 'threat-monitoring-policy-editor-source-editor' into 'master'

Policy Editor: switching the custom editor to Source Editor

See merge request gitlab-org/gitlab!57508
parents daa6dab5 350fd251
<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,35 @@ export default { ...@@ -12,56 +15,35 @@ 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() {
this.editor.dispose();
}, },
mounted() { computed: {
if (!this.editor) { editorOptions() {
this.setupEditor(); return {
}
},
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,
// Investigate the necessity of `glyphMargin` with #326746
glyphMargin: 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: {
onInput(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="onInput" />
</template> </template>
...@@ -369,7 +369,6 @@ export default { ...@@ -369,7 +369,6 @@ export default {
<network-policy-editor <network-policy-editor
data-testid="network-policy-editor" data-testid="network-policy-editor"
:value="yamlEditorValue" :value="yamlEditorValue"
:height="400"
:read-only="false" :read-only="false"
@input="loadYaml" @input="loadYaml"
/> />
......
---
title: 'Policy Editor: switching the custom editor to Source Editor'
merge_request: 57508
author:
type: changed
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;
const findEditor = () => wrapper.findComponent(EditorLite);
const factory = ({ propsData } = {}) => { const factory = ({ propsData } = {}) => {
wrapper = shallowMount(NetworkPolicyEditor, { wrapper = shallowMount(NetworkPolicyEditor, {
propsData: { propsData: {
value: 'foo', value: 'foo',
...propsData, ...propsData,
}, },
stubs: {
EditorLite,
},
}); });
}; };
...@@ -23,22 +29,20 @@ describe('NetworkPolicyEditor component', () => { ...@@ -23,22 +29,20 @@ describe('NetworkPolicyEditor component', () => {
}); });
it('renders container element', () => { it('renders container element', () => {
expect(wrapper.find({ ref: 'editor' }).exists()).toBe(true); expect(findEditor().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 editorComponent = findEditor();
vm: { editor }, expect(editorComponent.props('value')).toBe('foo');
} = wrapper; const editor = editorComponent.vm.getEditor();
expect(editor).not.toBe(null);
expect(editor.getModel().getModeId()).toBe('yaml'); expect(editor.getModel().getModeId()).toBe('yaml');
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 = findEditor();
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']);
}); });
}); });
...@@ -24,7 +24,6 @@ exports[`PolicyEditorApp component given .yaml editor mode is enabled renders ya ...@@ -24,7 +24,6 @@ exports[`PolicyEditorApp component given .yaml editor mode is enabled renders ya
> >
<networkpolicyeditor-stub <networkpolicyeditor-stub
data-testid="network-policy-editor" data-testid="network-policy-editor"
height="400"
value="" value=""
/> />
</div> </div>
......
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