import { lift, wrapIn } from "prosemirror-commands"; import { Slice } from "prosemirror-model"; import { ReplaceAroundStep } from "prosemirror-transform"; import { compose } from "../compose"; import { UxCommand } from "../constants"; import { closest } from "../pquery"; import { schema } from "../schema"; import { Command } from "../types"; const { fragment, bq, p } = compose; export const toggle: Command = (state, dispatch) => { const { $anchor } = state.selection; let match; if (closest($anchor, node => node.type === schema.nodes.bq) !== null) { return lift(state, dispatch); } else if (closest($anchor, node => node.type === schema.nodes.p) !== null) { return wrapIn(schema.nodes.bq)(state, dispatch); } else if ((match = closest($anchor, node => node.type === schema.nodes.h)) !== null) { if (dispatch !== undefined) { dispatch( state.tr.step( new ReplaceAroundStep( match.pos, match.pos + match.node.nodeSize, // We want to keep the content immediately inside the heading, so // the gap is set to that range. match.pos + 1, // h start match.pos + match.node.nodeSize - 1, // h end new Slice(fragment(bq(p())).fragment, 0, 0), 2, /* structure */ true ) ) ); } return true; } return false; }; export const ux = { [UxCommand.ToggleBlockquote]: toggle };