From 4f83f54683c94ba6559376f8435f2f36f528131e Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Sat, 13 Aug 2016 17:15:27 -0700 Subject: [PATCH] refactor mark component normalization --- lib/models/schema.js | 153 ++++++++++++++++++++++++++----------------- 1 file changed, 93 insertions(+), 60 deletions(-) diff --git a/lib/models/schema.js b/lib/models/schema.js index 0e7d31fad..6b7cf1923 100644 --- a/lib/models/schema.js +++ b/lib/models/schema.js @@ -152,24 +152,7 @@ class Schema extends new Record(DEFAULTS) { __getComponent(object) { const match = this.rules.find(rule => rule.match(object) && rule.component) if (!match) return - - const { component } = match - - // Marks need special handling to normalize components. - if (object.kind != 'mark') return match.component - - // If it's a React component, we're good. - if (isReactComponent(component)) return component - - // Handle all other types... - switch (typeOf(component)) { - case 'function': - return component - case 'object': - return props => {props.children} - case 'string': - return props => {props.children} - } + return match.component } /** @@ -212,53 +195,16 @@ class Schema extends new Record(DEFAULTS) { */ function normalizeProperties(properties) { - let { rules, nodes, marks } = properties - if (!rules) rules = [] + let { rules = [], nodes, marks } = properties - // If there's a `nodes` property, it's the node rule shorthand dictionary. if (nodes) { - for (const key in nodes) { - const value = nodes[key] - const match = { - kinds: ['block', 'inline'], - type: key, - } - - if (value.component) { - rules.push({ - match, - ...value, - }) - } else { - rules.push({ - match, - component: value - }) - } - } + const array = normalizeNodes(nodes) + rules = rules.concat(array) } - // If there's a `marks` property, it's the mark rule shorthand dictionary. if (marks) { - for (const key in marks) { - const value = marks[key] - const match = { - kind: 'mark', - type: key, - } - - if (value.component) { - rules.push({ - match, - ...value, - }) - } else { - rules.push({ - match, - component: value - }) - } - } + const array = normalizeMarks(marks) + rules = rules.concat(array) } return { @@ -266,6 +212,93 @@ function normalizeProperties(properties) { } } +/** + * Normalize a `nodes` shorthand argument. + * + * @param {Object} nodes + * @return {Array} + */ + +function normalizeNodes(nodes) { + const rules = [] + + for (const key in nodes) { + const value = nodes[key] + const match = { + kinds: ['block', 'inline'], + type: key, + } + + if (value.component) { + rules.push({ + match, + ...value, + }) + } else { + rules.push({ + match, + component: value + }) + } + } + + return rules +} + +/** + * Normalize a `marks` shorthand argument. + * + * @param {Object} marks + * @return {Array} + */ + +function normalizeMarks(marks) { + const rules = [] + + for (const key in marks) { + const value = marks[key] + const match = { + kind: 'mark', + type: key, + } + + if (value.component) { + rules.push({ + match, + ...value, + component: normalizeMarkComponent(value.component), + }) + } else { + rules.push({ + match, + component: normalizeMarkComponent(value) + }) + } + } + + return rules +} + +/** + * Normalize a mark `component`. + * + * @param {Component || Function || Object || String} component + * @return {Component} + */ + +function normalizeMarkComponent(component) { + if (isReactComponent(component)) return component + + switch (typeOf(component)) { + case 'function': + return component + case 'object': + return props => {props.children} + case 'string': + return props => {props.children} + } +} + /** * Normalize a `rule` object. *