This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/app/scenes/Document/schema.js
2018-11-18 13:12:46 -08:00

36 lines
904 B
JavaScript

// @flow
import { cloneDeep } from 'lodash';
import { Block, Change, Node, Mark } from 'slate';
import { schema as originalSchema } from 'rich-markdown-editor';
const schema = cloneDeep(originalSchema);
// add rules to the schema to ensure the first node is a heading
schema.document.nodes.unshift({ types: ['heading1'], min: 1, max: 1 });
schema.document.normalize = (
change: Change,
reason: string,
{
node,
child,
mark,
index,
}: { node: Node, mark?: Mark, child: Node, index: number }
) => {
switch (reason) {
case 'child_type_invalid': {
return change.setNodeByKey(
child.key,
index === 0 ? 'heading1' : 'paragraph'
);
}
case 'child_required': {
const block = Block.create(index === 0 ? 'heading1' : 'paragraph');
return change.insertNodeByKey(node.key, index, block);
}
default:
}
};
export default schema;