Commit c06b9ecc authored by derek-knox's avatar derek-knox

Apply review feedback

Refactor to hasValidMatter and refactor prior cache in syncContent
parent ff81c193
...@@ -84,8 +84,8 @@ export default { ...@@ -84,8 +84,8 @@ export default {
const { isModified, hasMatter, isMatterValid } = this.parsedSource; const { isModified, hasMatter, isMatterValid } = this.parsedSource;
this.isModified = isModified(); this.isModified = isModified();
this.hasMatter = hasMatter(); this.hasMatter = hasMatter();
const hasValidatedMatterCondition = this.hasMatter ? isMatterValid() : true; const hasValidMatter = this.hasMatter ? isMatterValid() : true;
this.isSaveable = this.isModified && hasValidatedMatterCondition; this.isSaveable = this.isModified && hasValidMatter;
}, },
onDrawerOpen() { onDrawerOpen() {
this.isDrawerOpen = true; this.isDrawerOpen = true;
......
...@@ -8,19 +8,6 @@ const hasMatter = (firstThreeChars, fourthChar) => { ...@@ -8,19 +8,6 @@ const hasMatter = (firstThreeChars, fourthChar) => {
return isYamlDelimiter && isFourthCharNewline; return isYamlDelimiter && isFourthCharNewline;
}; };
const validateMatter = matterStr => {
let isMatterValid = true;
let matter;
try {
matter = jsYaml.safeLoad(matterStr);
} catch (error) {
isMatterValid = false;
}
return { matter, isMatterValid };
};
export const frontMatterify = source => { export const frontMatterify = source => {
let index = 3; let index = 3;
let offset; let offset;
...@@ -30,7 +17,6 @@ export const frontMatterify = source => { ...@@ -30,7 +17,6 @@ export const frontMatterify = source => {
source, source,
matter: null, matter: null,
hasMatter: false, hasMatter: false,
isMatterValid: false,
spacing: null, spacing: null,
content: source, content: source,
delimiter: null, delimiter: null,
...@@ -54,7 +40,7 @@ export const frontMatterify = source => { ...@@ -54,7 +40,7 @@ export const frontMatterify = source => {
} }
const matterStr = source.slice(index, offset); const matterStr = source.slice(index, offset);
const { matter, isMatterValid } = validateMatter(matterStr); const matter = jsYaml.safeLoad(matterStr);
let content = source.slice(offset + delimiter.length); let content = source.slice(offset + delimiter.length);
let spacing = ''; let spacing = '';
...@@ -69,7 +55,6 @@ export const frontMatterify = source => { ...@@ -69,7 +55,6 @@ export const frontMatterify = source => {
source, source,
matter, matter,
hasMatter: true, hasMatter: true,
isMatterValid,
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);
let lastValidMatter = null;
const syncContent = (newVal, isBody) => { const syncContent = (newVal, isBody) => {
if (isBody) { if (isBody) {
editable.content = newVal; editable.content = newVal;
} else { } else {
// 1. Cache last valid matter to account for mid-edit resulting in matter invalidation try {
if (editable.hasMatter) { editable = frontMatterify(newVal);
lastValidMatter = editable.matter; editable.isMatterValid = true;
} } catch (e) {
editable.isMatterValid = false;
// 2. Update editable
editable = remake(newVal);
// 3. Use last valid matter cache if mid-edit results in matter invalidation
if (!editable.isMatterValid) {
editable.matter = lastValidMatter;
} }
} }
}; };
...@@ -39,6 +30,8 @@ const parseSourceFile = raw => { ...@@ -39,6 +30,8 @@ const parseSourceFile = raw => {
const isMatterValid = () => editable.isMatterValid; const isMatterValid = () => editable.isMatterValid;
syncContent(raw);
return { return {
matter, matter,
isMatterValid, isMatterValid,
......
...@@ -12,7 +12,6 @@ describe('static_site_editor/services/front_matterify', () => { ...@@ -12,7 +12,6 @@ describe('static_site_editor/services/front_matterify', () => {
source: content, source: content,
matter: yamlFrontMatterObj, matter: yamlFrontMatterObj,
hasMatter: true, hasMatter: true,
isMatterValid: true,
spacing, spacing,
content: body, content: body,
delimiter: '---', delimiter: '---',
...@@ -22,7 +21,6 @@ describe('static_site_editor/services/front_matterify', () => { ...@@ -22,7 +21,6 @@ describe('static_site_editor/services/front_matterify', () => {
source: body, source: body,
matter: null, matter: null,
hasMatter: false, hasMatter: false,
isMatterValid: false,
spacing: null, spacing: null,
content: body, content: body,
delimiter: null, delimiter: null,
...@@ -37,6 +35,12 @@ describe('static_site_editor/services/front_matterify', () => { ...@@ -37,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