Commit 596d3f20 authored by Andrew Smith's avatar Andrew Smith

Hide markdown preview tab if editing non-markdown formats

Fixes https://gitlab.com/gitlab-org/gitlab/-/issues/228883

Changelog: added
Signed-off-by: default avatarAndrew Smith <espadav8@gmail.com>
parent 12007969
...@@ -495,6 +495,7 @@ export default { ...@@ -495,6 +495,7 @@ export default {
:textarea-value="content" :textarea-value="content"
:markdown-docs-path="pageInfo.markdownHelpPath" :markdown-docs-path="pageInfo.markdownHelpPath"
:uploads-path="pageInfo.uploadsPath" :uploads-path="pageInfo.uploadsPath"
:enable-preview="isMarkdownFormat"
class="bordered-box" class="bordered-box"
> >
<template #textarea> <template #textarea>
......
...@@ -48,6 +48,11 @@ export default { ...@@ -48,6 +48,11 @@ export default {
required: false, required: false,
default: '', default: '',
}, },
enablePreview: {
type: Boolean,
required: false,
default: true,
},
addSpacingClasses: { addSpacingClasses: {
type: Boolean, type: Boolean,
required: false, required: false,
...@@ -204,6 +209,14 @@ export default { ...@@ -204,6 +209,14 @@ export default {
} }
}, },
}, },
enablePreview: {
immediate: true,
handler(newVal) {
if (!newVal) {
this.showWriteTab();
}
},
},
}, },
mounted() { mounted() {
// GLForm class handles all the toolbar buttons // GLForm class handles all the toolbar buttons
...@@ -301,6 +314,7 @@ export default { ...@@ -301,6 +314,7 @@ export default {
:preview-markdown="previewMarkdown" :preview-markdown="previewMarkdown"
:line-content="lineContent" :line-content="lineContent"
:can-suggest="canSuggest" :can-suggest="canSuggest"
:enable-preview="enablePreview"
:show-suggest-popover="showSuggestPopover" :show-suggest-popover="showSuggestPopover"
:suggestion-start-index="suggestionsStartIndex" :suggestion-start-index="suggestionsStartIndex"
data-testid="markdownHeader" data-testid="markdownHeader"
......
...@@ -43,6 +43,11 @@ export default { ...@@ -43,6 +43,11 @@ export default {
required: false, required: false,
default: 0, default: 0,
}, },
enablePreview: {
type: Boolean,
required: false,
default: true,
},
}, },
data() { data() {
return { return {
...@@ -164,6 +169,7 @@ export default { ...@@ -164,6 +169,7 @@ export default {
@click="writeMarkdownTab($event)" @click="writeMarkdownTab($event)"
/> />
<gl-tab <gl-tab
v-if="enablePreview"
title-link-class="gl-pt-3 gl-px-3 js-md-preview-button" title-link-class="gl-pt-3 gl-px-3 js-md-preview-button"
:title="$options.i18n.previewTabTitle" :title="$options.i18n.previewTabTitle"
:active="previewMarkdown" :active="previewMarkdown"
......
...@@ -154,6 +154,20 @@ describe('WikiForm', () => { ...@@ -154,6 +154,20 @@ describe('WikiForm', () => {
expect(findContent().element.value).toBe(' My page content '); expect(findContent().element.value).toBe(' My page content ');
}); });
it.each`
format | enabled | action
${'markdown'} | ${true} | ${'displays'}
${'rdoc'} | ${false} | ${'hides'}
${'asciidoc'} | ${false} | ${'hides'}
${'org'} | ${false} | ${'hides'}
`('$action preview in the markdown field when format is $format', async ({ format, enabled }) => {
createWrapper();
await setFormat(format);
expect(findClassicEditor().props('enablePreview')).toBe(enabled);
});
it.each` it.each`
value | text value | text
${'markdown'} | ${'[Link Title](page-slug)'} ${'markdown'} | ${'[Link Title](page-slug)'}
......
...@@ -29,6 +29,7 @@ exports[`Snippet Description Edit component rendering matches the snapshot 1`] = ...@@ -29,6 +29,7 @@ exports[`Snippet Description Edit component rendering matches the snapshot 1`] =
> >
<markdown-header-stub <markdown-header-stub
data-testid="markdownHeader" data-testid="markdownHeader"
enablepreview="true"
linecontent="" linecontent=""
suggestionstartindex="0" suggestionstartindex="0"
/> />
......
...@@ -4,6 +4,7 @@ import $ from 'jquery'; ...@@ -4,6 +4,7 @@ import $ from 'jquery';
import { TEST_HOST, FIXTURES_PATH } from 'spec/test_constants'; import { TEST_HOST, FIXTURES_PATH } from 'spec/test_constants';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import MarkdownField from '~/vue_shared/components/markdown/field.vue'; import MarkdownField from '~/vue_shared/components/markdown/field.vue';
import MarkdownFieldHeader from '~/vue_shared/components/markdown/header.vue';
import { mountExtended } from 'helpers/vue_test_utils_helper'; import { mountExtended } from 'helpers/vue_test_utils_helper';
const markdownPreviewPath = `${TEST_HOST}/preview`; const markdownPreviewPath = `${TEST_HOST}/preview`;
...@@ -32,7 +33,7 @@ describe('Markdown field component', () => { ...@@ -32,7 +33,7 @@ describe('Markdown field component', () => {
axiosMock.restore(); axiosMock.restore();
}); });
function createSubject(lines = []) { function createSubject({ lines = [], enablePreview = true } = {}) {
// We actually mount a wrapper component so that we can force Vue to rerender classes in order to test a regression // We actually mount a wrapper component so that we can force Vue to rerender classes in order to test a regression
// caused by mixing Vanilla JS and Vue. // caused by mixing Vanilla JS and Vue.
subject = mountExtended( subject = mountExtended(
...@@ -61,6 +62,7 @@ describe('Markdown field component', () => { ...@@ -61,6 +62,7 @@ describe('Markdown field component', () => {
isSubmitting: false, isSubmitting: false,
textareaValue, textareaValue,
lines, lines,
enablePreview,
}, },
provide: { provide: {
glFeatures: { glFeatures: {
...@@ -272,11 +274,22 @@ describe('Markdown field component', () => { ...@@ -272,11 +274,22 @@ describe('Markdown field component', () => {
describe('suggestions', () => { describe('suggestions', () => {
it('escapes new line characters', () => { it('escapes new line characters', () => {
createSubject([{ rich_text: 'hello world\\n' }]); createSubject({ lines: [{ rich_text: 'hello world\\n' }] });
expect(subject.find('[data-testid="markdownHeader"]').props('lineContent')).toBe( expect(subject.find('[data-testid="markdownHeader"]').props('lineContent')).toBe(
'hello world%br', 'hello world%br',
); );
}); });
}); });
it('allows enabling and disabling Markdown Preview', () => {
createSubject({ enablePreview: false });
expect(subject.findComponent(MarkdownFieldHeader).props('enablePreview')).toBe(false);
subject.destroy();
createSubject({ enablePreview: true });
expect(subject.findComponent(MarkdownFieldHeader).props('enablePreview')).toBe(true);
});
}); });
...@@ -157,4 +157,12 @@ describe('Markdown field header component', () => { ...@@ -157,4 +157,12 @@ describe('Markdown field header component', () => {
expect(wrapper.find('.js-suggestion-btn').exists()).toBe(false); expect(wrapper.find('.js-suggestion-btn').exists()).toBe(false);
}); });
it('hides preview tab when previewMarkdown property is false', () => {
createWrapper({
enablePreview: false,
});
expect(wrapper.findByTestId('preview-tab').exists()).toBe(false);
});
}); });
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