Commit 3829ec38 authored by Jose Ivan Vargas's avatar Jose Ivan Vargas

Merge branch '250343-yaml-try-catch' into 'master'

Add catch during manual yaml front matter editing

See merge request gitlab-org/gitlab!44782
parents 78bd506a c06b9ecc
...@@ -50,12 +50,12 @@ export default { ...@@ -50,12 +50,12 @@ export default {
}, },
data() { data() {
return { return {
saveable: false,
parsedSource: parseSourceFile(this.preProcess(true, this.content)), parsedSource: parseSourceFile(this.preProcess(true, this.content)),
editorMode: EDITOR_TYPES.wysiwyg, editorMode: EDITOR_TYPES.wysiwyg,
isModified: false,
hasMatter: false, hasMatter: false,
isDrawerOpen: false, isDrawerOpen: false,
isModified: false,
isSaveable: false,
}; };
}, },
imageRepository: imageRepository(), imageRepository: imageRepository(),
...@@ -85,8 +85,11 @@ export default { ...@@ -85,8 +85,11 @@ export default {
return templatedContent; return templatedContent;
}, },
refreshEditHelpers() { refreshEditHelpers() {
this.isModified = this.parsedSource.isModified(); const { isModified, hasMatter, isMatterValid } = this.parsedSource;
this.hasMatter = this.parsedSource.hasMatter(); this.isModified = isModified();
this.hasMatter = hasMatter();
const hasValidMatter = this.hasMatter ? isMatterValid() : true;
this.isSaveable = this.isModified && hasValidMatter;
}, },
onDrawerOpen() { onDrawerOpen() {
this.isDrawerOpen = true; this.isDrawerOpen = true;
...@@ -142,12 +145,12 @@ export default { ...@@ -142,12 +145,12 @@ export default {
@input="onInputChange" @input="onInputChange"
@uploadImage="onUploadImage" @uploadImage="onUploadImage"
/> />
<unsaved-changes-confirm-dialog :modified="isModified" /> <unsaved-changes-confirm-dialog :modified="isSaveable" />
<publish-toolbar <publish-toolbar
class="gl-fixed gl-left-0 gl-bottom-0 gl-w-full" class="gl-fixed gl-left-0 gl-bottom-0 gl-w-full"
:has-settings="hasSettings" :has-settings="hasSettings"
:return-url="returnUrl" :return-url="returnUrl"
:saveable="isModified" :saveable="isSaveable"
:saving-changes="savingChanges" :saving-changes="savingChanges"
@editSettings="onDrawerOpen" @editSettings="onDrawerOpen"
@submit="onSubmit" @submit="onSubmit"
......
...@@ -16,6 +16,7 @@ export const frontMatterify = source => { ...@@ -16,6 +16,7 @@ export const frontMatterify = source => {
const NO_FRONTMATTER = { const NO_FRONTMATTER = {
source, source,
matter: null, matter: null,
hasMatter: false,
spacing: null, spacing: null,
content: source, content: source,
delimiter: null, delimiter: null,
...@@ -53,6 +54,7 @@ export const frontMatterify = source => { ...@@ -53,6 +54,7 @@ export const frontMatterify = source => {
return { return {
source, source,
matter, matter,
hasMatter: true,
spacing, spacing,
content, content,
delimiter, delimiter,
......
import { frontMatterify, stringify } from './front_matterify'; import { frontMatterify, stringify } from './front_matterify';
const parseSourceFile = raw => { const parseSourceFile = raw => {
const remake = source => frontMatterify(source); let editable;
let editable = remake(raw);
const syncContent = (newVal, isBody) => { const syncContent = (newVal, isBody) => {
if (isBody) { if (isBody) {
editable.content = newVal; editable.content = newVal;
} else { } else {
editable = remake(newVal); try {
editable = frontMatterify(newVal);
editable.isMatterValid = true;
} catch (e) {
editable.isMatterValid = false;
}
} }
}; };
...@@ -23,10 +26,15 @@ const parseSourceFile = raw => { ...@@ -23,10 +26,15 @@ const parseSourceFile = raw => {
const isModified = () => stringify(editable) !== raw; const isModified = () => stringify(editable) !== raw;
const hasMatter = () => Boolean(editable.matter); const hasMatter = () => editable.hasMatter;
const isMatterValid = () => editable.isMatterValid;
syncContent(raw);
return { return {
matter, matter,
isMatterValid,
syncMatter, syncMatter,
content, content,
syncContent, syncContent,
......
...@@ -11,6 +11,7 @@ describe('static_site_editor/services/front_matterify', () => { ...@@ -11,6 +11,7 @@ describe('static_site_editor/services/front_matterify', () => {
const frontMatterifiedContent = { const frontMatterifiedContent = {
source: content, source: content,
matter: yamlFrontMatterObj, matter: yamlFrontMatterObj,
hasMatter: true,
spacing, spacing,
content: body, content: body,
delimiter: '---', delimiter: '---',
...@@ -19,6 +20,7 @@ describe('static_site_editor/services/front_matterify', () => { ...@@ -19,6 +20,7 @@ describe('static_site_editor/services/front_matterify', () => {
const frontMatterifiedBody = { const frontMatterifiedBody = {
source: body, source: body,
matter: null, matter: null,
hasMatter: false,
spacing: null, spacing: null,
content: body, content: body,
delimiter: null, delimiter: null,
...@@ -33,6 +35,12 @@ describe('static_site_editor/services/front_matterify', () => { ...@@ -33,6 +35,12 @@ describe('static_site_editor/services/front_matterify', () => {
`('returns $target from $frontMatterified', ({ frontMatterified, target }) => { `('returns $target from $frontMatterified', ({ frontMatterified, target }) => {
expect(frontMatterified).toEqual(target); expect(frontMatterified).toEqual(target);
}); });
it('should throw when matter is invalid', () => {
const invalidContent = `---\nkey: val\nkeyNoVal\n---\n${body}`;
expect(() => frontMatterify(invalidContent)).toThrow();
});
}); });
describe('stringify', () => { describe('stringify', () => {
......
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