mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-02 11:42:53 +02:00
refactor mark component normalization
This commit is contained in:
@@ -152,24 +152,7 @@ class Schema extends new Record(DEFAULTS) {
|
|||||||
__getComponent(object) {
|
__getComponent(object) {
|
||||||
const match = this.rules.find(rule => rule.match(object) && rule.component)
|
const match = this.rules.find(rule => rule.match(object) && rule.component)
|
||||||
if (!match) return
|
if (!match) return
|
||||||
|
return match.component
|
||||||
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 => <span style={component}>{props.children}</span>
|
|
||||||
case 'string':
|
|
||||||
return props => <span className={component}>{props.children}</span>
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -212,53 +195,16 @@ class Schema extends new Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function normalizeProperties(properties) {
|
function normalizeProperties(properties) {
|
||||||
let { rules, nodes, marks } = properties
|
let { rules = [], nodes, marks } = properties
|
||||||
if (!rules) rules = []
|
|
||||||
|
|
||||||
// If there's a `nodes` property, it's the node rule shorthand dictionary.
|
|
||||||
if (nodes) {
|
if (nodes) {
|
||||||
for (const key in nodes) {
|
const array = normalizeNodes(nodes)
|
||||||
const value = nodes[key]
|
rules = rules.concat(array)
|
||||||
const match = {
|
|
||||||
kinds: ['block', 'inline'],
|
|
||||||
type: key,
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value.component) {
|
|
||||||
rules.push({
|
|
||||||
match,
|
|
||||||
...value,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
rules.push({
|
|
||||||
match,
|
|
||||||
component: value
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there's a `marks` property, it's the mark rule shorthand dictionary.
|
|
||||||
if (marks) {
|
if (marks) {
|
||||||
for (const key in marks) {
|
const array = normalizeMarks(marks)
|
||||||
const value = marks[key]
|
rules = rules.concat(array)
|
||||||
const match = {
|
|
||||||
kind: 'mark',
|
|
||||||
type: key,
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value.component) {
|
|
||||||
rules.push({
|
|
||||||
match,
|
|
||||||
...value,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
rules.push({
|
|
||||||
match,
|
|
||||||
component: value
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
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 => <span style={component}>{props.children}</span>
|
||||||
|
case 'string':
|
||||||
|
return props => <span className={component}>{props.children}</span>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalize a `rule` object.
|
* Normalize a `rule` object.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user