Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support frontmatter defaults in the front end #337

Merged
merged 9 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion spec/fixtures/site/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,22 @@ defaults:
scope:
path: ""
values:
some_front_matter: "default"
all: true
- scope:
path: ''
type: pages
values:
page_only: true
- scope:
path: ''
type: puppies
values:
breed: ''
- scope:
path: 'test'
type: posts
values:
post_test_only: true

# Dummy Collection.
collections:
Expand Down
4 changes: 2 additions & 2 deletions spec/jekyll-admin/apiable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
end

it "includes front matter defaults as top-level keys" do
expect(as_api).to have_key("some_front_matter")
expect(as_api["some_front_matter"]).to eql("default")
expect(as_api).to have_key("all")
expect(as_api["all"]).to eql(true)
end

it "includes front matter as top-level keys" do
Expand Down
4 changes: 2 additions & 2 deletions spec/jekyll-admin/server/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def app
it "includes front matter defaults" do
get "/collections/posts/entries/"
expect(last_response).to be_ok
expect(first_document.key?("some_front_matter")).to eq(true)
expect(first_document.key?("all")).to eq(true)
end

it "doesn't include the raw front matter" do
Expand Down Expand Up @@ -181,7 +181,7 @@ def app

it "contains front matter defaults" do
get "/collections/posts/2016-01-01-test-post.md"
expect(last_response_parsed.key?("some_front_matter")).to eql(true)
expect(last_response_parsed.key?("all")).to eql(true)
end

it "contains raw front matter" do
Expand Down
8 changes: 5 additions & 3 deletions spec/jekyll-admin/server/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def app
it "includes front matter defaults" do
get "/pages"
expect(last_response).to be_ok
expect(first_page).to have_key("some_front_matter")
expect(first_page).to have_key("all")
expect(first_page).to have_key("page_only")
end

it "doesn't include the raw front matter" do
Expand Down Expand Up @@ -101,7 +102,8 @@ def app

it "contains front matter defaults" do
get "/pages/page.md"
expect(last_response_parsed.key?("some_front_matter")).to eql(true)
expect(last_response_parsed.key?("all")).to eql(true)
expect(last_response_parsed.key?("page_only")).to eql(true)
end

it "contains raw front matter" do
Expand All @@ -112,7 +114,7 @@ def app

it "raw front matter doesn't include defaults" do
get "/pages/page.md"
expect(front_matter.key?("some_front_matter")).to eql(false)
expect(front_matter.key?("all")).to eql(false)
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/containers/MetaFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class MetaFields extends Component {
}

shouldComponentUpdate(nextProps) {
return nextProps.metadata !== this.props.metadata;
return !_.isEqual(nextProps.metadata, this.props.metadata);
}

render() {
Expand Down
13 changes: 9 additions & 4 deletions src/containers/views/DocumentEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Metadata from '../../containers/MetaFields';
import { fetchDocument, deleteDocument, putDocument } from '../../actions/collections';
import { updateTitle, updateBody, updatePath } from '../../actions/metadata';
import { clearErrors } from '../../actions/utils';
import { injectDefaultFields } from '../../utils/metadata';
import { preventDefault } from '../../utils/helpers';
import {
getLeaveMessage, getDeleteMessage, getNotFoundMessage
Expand Down Expand Up @@ -97,7 +98,7 @@ export class DocumentEdit extends Component {
render() {
const {
isFetching, currentDocument, errors, updateTitle, updateBody, updatePath, updated,
fieldChanged, params
fieldChanged, params, config
} = this.props;

if (isFetching) {
Expand All @@ -113,6 +114,8 @@ export class DocumentEdit extends Component {
} = currentDocument;
const [directory, ...rest] = params.splat;

const metafields = injectDefaultFields(config, directory, collection, front_matter);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config is since recently,

{"content": {"foo": "bar"}, "raw_content": "foo: bar\n"}

So either this line and all instances of config passed to injectDefaultFields should be changed to config.content or change injectDefaultFields function to extract defaults from config.content


const keyboardHandlers = {
'save': this.handleClickSave,
};
Expand All @@ -139,7 +142,7 @@ export class DocumentEdit extends Component {
initialValue={raw_content}
ref="editor" />
<Splitter />
<Metadata fields={{title, path: name, raw_content, ...front_matter}} />
<Metadata fields={{title, path: name, raw_content, ...metafields}} />
</div>

<div className="content-side">
Expand Down Expand Up @@ -188,15 +191,17 @@ DocumentEdit.propTypes = {
fieldChanged: PropTypes.bool.isRequired,
params: PropTypes.object.isRequired,
router: PropTypes.object.isRequired,
route: PropTypes.object.isRequired
route: PropTypes.object.isRequired,
config: PropTypes.object.isRequired
};

const mapStateToProps = (state) => ({
currentDocument: state.collections.currentDocument,
isFetching: state.collections.isFetching,
fieldChanged: state.metadata.fieldChanged,
updated: state.collections.updated,
errors: state.utils.errors
errors: state.utils.errors,
config: state.config.config
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
Expand Down
20 changes: 11 additions & 9 deletions src/containers/views/DocumentNew.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { updateTitle, updateBody, updatePath } from '../../actions/metadata';
import { createDocument } from '../../actions/collections';
import { clearErrors } from '../../actions/utils';
import { getFilenameFromPath } from '../../utils/helpers';
import { injectDefaultFields } from '../../utils/metadata';
import {
getLeaveMessage, getDeleteMessage, getNotFoundMessage
} from '../../constants/lang';
Expand Down Expand Up @@ -59,13 +60,14 @@ export class DocumentNew extends Component {
}

render() {
const {
errors, updated, updateTitle, updateBody, updatePath, fieldChanged, params
} = this.props;
const { errors, updated, updateTitle, updateBody, updatePath, fieldChanged,
params, config } = this.props;

const collection = params.collection_name;
const link = `${ADMIN_PREFIX}/collections/${collection}`;

const metafields = injectDefaultFields(config, params.splat, collection);

return (
<div className="single">
{errors.length > 0 && <Errors errors={errors} />}
Expand All @@ -86,7 +88,7 @@ export class DocumentNew extends Component {
initialValue=""
ref="editor" />
<Splitter />
<Metadata fields={{}} />
<Metadata fields={metafields} />
</div>

<div className="content-side">
Expand Down Expand Up @@ -115,14 +117,16 @@ DocumentNew.propTypes = {
updated: PropTypes.bool.isRequired,
params: PropTypes.object.isRequired,
router: PropTypes.object.isRequired,
route: PropTypes.object.isRequired
route: PropTypes.object.isRequired,
config: PropTypes.object.isRequired
};

const mapStateToProps = (state) => ({
currentDocument: state.collections.currentDocument,
fieldChanged: state.metadata.fieldChanged,
errors: state.utils.errors,
updated: state.collections.updated
updated: state.collections.updated,
config: state.config.config
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
Expand All @@ -133,6 +137,4 @@ const mapDispatchToProps = (dispatch) => bindActionCreators({
clearErrors
}, dispatch);

export default withRouter(
connect(mapStateToProps, mapDispatchToProps)(DocumentNew)
);
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(DocumentNew));
14 changes: 10 additions & 4 deletions src/containers/views/PageEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Metadata from '../MetaFields';
import { fetchPage, deletePage, putPage } from '../../actions/pages';
import { updateTitle, updateBody, updatePath } from '../../actions/metadata';
import { clearErrors } from '../../actions/utils';
import { injectDefaultFields } from '../../utils/metadata';
import { preventDefault } from '../../utils/helpers';
import {
getLeaveMessage, getDeleteMessage, getNotFoundMessage
Expand Down Expand Up @@ -89,7 +90,7 @@ export class PageEdit extends Component {

render() {
const { isFetching, page, errors, updateTitle, updateBody, updatePath,
updated, fieldChanged, params } = this.props;
updated, fieldChanged, params, config } = this.props;

if (isFetching) {
return null;
Expand All @@ -105,7 +106,10 @@ export class PageEdit extends Component {

const { name, raw_content, http_url, path, front_matter } = page;
const [directory, ...rest] = params.splat;

const title = front_matter && front_matter.title ? front_matter.title : '';
const metafields = injectDefaultFields(config, directory, 'pages', front_matter);

return (
<HotKeys
handlers={keyboardHandlers}
Expand All @@ -126,7 +130,7 @@ export class PageEdit extends Component {
initialValue={raw_content}
ref="editor" />
<Splitter />
<Metadata fields={{title, raw_content, path: name, ...front_matter}} />
<Metadata fields={{title, raw_content, path: name, ...metafields}} />
</div>

<div className="content-side">
Expand Down Expand Up @@ -173,15 +177,17 @@ PageEdit.propTypes = {
updated: PropTypes.bool.isRequired,
params: PropTypes.object.isRequired,
router: PropTypes.object.isRequired,
route: PropTypes.object.isRequired
route: PropTypes.object.isRequired,
config: PropTypes.object.isRequired
};

const mapStateToProps = (state) => ({
page: state.pages.page,
isFetching: state.pages.isFetching,
fieldChanged: state.metadata.fieldChanged,
updated: state.pages.updated,
errors: state.utils.errors
errors: state.utils.errors,
config: state.config.config
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
Expand Down
12 changes: 8 additions & 4 deletions src/containers/views/PageNew.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Metadata from '../../containers/MetaFields';
import { updateTitle, updateBody, updatePath, updateDraft } from '../../actions/metadata';
import { createPage } from '../../actions/pages';
import { clearErrors } from '../../actions/utils';
import { injectDefaultFields } from '../../utils/metadata';
import {
getLeaveMessage, getDeleteMessage, getNotFoundMessage
} from '../../constants/lang';
Expand Down Expand Up @@ -55,8 +56,9 @@ export class PageNew extends Component {

render() {
const { errors, updated, updateTitle, updateBody, updatePath,
updateDraft, fieldChanged, params } = this.props;
updateDraft, fieldChanged, params, config } = this.props;

const metafields = injectDefaultFields(config, params.splat, 'pages');
return (
<div className="single">
{errors.length > 0 && <Errors errors={errors} />}
Expand All @@ -77,7 +79,7 @@ export class PageNew extends Component {
initialValue=""
ref="editor" />
<Splitter />
<Metadata fields={{}} />
<Metadata fields={metafields} />
</div>

<div className="content-side">
Expand Down Expand Up @@ -107,14 +109,16 @@ PageNew.propTypes = {
updated: PropTypes.bool.isRequired,
router: PropTypes.object.isRequired,
route: PropTypes.object.isRequired,
params: PropTypes.object.isRequired
params: PropTypes.object.isRequired,
config: PropTypes.object.isRequired
};

const mapStateToProps = (state) => ({
page: state.pages.page,
fieldChanged: state.metadata.fieldChanged,
errors: state.utils.errors,
updated: state.pages.updated
updated: state.pages.updated,
config: state.config.config
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
Expand Down
3 changes: 2 additions & 1 deletion src/containers/views/tests/documentedit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DocumentEdit } from '../DocumentEdit';
import Errors from '../../../components/Errors';
import Button from '../../../components/Button';

import { doc } from './fixtures';
import { config, doc } from './fixtures';

const defaultProps = {
currentDocument: doc,
Expand All @@ -17,6 +17,7 @@ const defaultProps = {
isFetching: false,
router: {},
route: {},
config: config,
params: { collection_name: "movies", splat: [null, "inception", "md"] }
};

Expand Down
3 changes: 2 additions & 1 deletion src/containers/views/tests/documentnew.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { DocumentNew } from '../DocumentNew';
import Errors from '../../../components/Errors';
import Button from '../../../components/Button';

import { doc } from './fixtures';
import { config, doc } from './fixtures';

const defaultProps = {
errors: [],
fieldChanged: false,
updated: false,
router: {},
route: {},
config: config,
params: { collection_name: doc.collection }
};

Expand Down
3 changes: 2 additions & 1 deletion src/containers/views/tests/pageedit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PageEdit } from '../PageEdit';
import Errors from '../../../components/Errors';
import Button from '../../../components/Button';

import { page } from './fixtures';
import { config, page } from './fixtures';

const defaultProps = {
page: page,
Expand All @@ -17,6 +17,7 @@ const defaultProps = {
isFetching: false,
router: {},
route: {},
config: config,
params: { splat: [null, "page", "md"] }
};

Expand Down
3 changes: 2 additions & 1 deletion src/containers/views/tests/pagenew.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { PageNew } from '../PageNew';
import Errors from '../../../components/Errors';
import Button from '../../../components/Button';

import { page } from './fixtures';
import { config, page } from './fixtures';

const defaultProps = {
errors: [],
fieldChanged: false,
updated: false,
router: {},
route: {},
config: config,
params: { splat: 'page-dir' }
};

Expand Down
Loading