Make headers buttons toggle headers

This commit is contained in:
Mathias Biilmann Christensen 2016-11-01 17:51:49 -07:00
parent 97928a7b34
commit 27d33d2a84

View File

@ -82,8 +82,21 @@ export default class Editor extends Component {
handleHeader = level => (
() => {
const command = setBlockType(schema.nodes.heading, { level });
command(this.view.state, this.handleAction);
const state = this.view.state;
const { $from, to, node } = state.selection;
let nodeType = schema.nodes.heading;
let attrs = { level };
let inHeader = node && node.hasMarkup(nodeType, attrs);
if (!inHeader) {
inHeader = to <= $from.end() && $from.parent.hasMarkup(nodeType, attrs);
}
if (inHeader) {
nodeType = schema.nodes.paragraph;
attrs = {};
}
const command = setBlockType(nodeType, { level });
command(state, this.handleAction);
}
);