import { UxCommand } from "../constants"; import { Command } from "../types"; import { closest } from "../pquery"; import { schema } from "../schema"; import { compose } from "../compose"; import { WedgeSelection } from "../wedge/WedgeSelection"; const { hr } = compose; export const insertHorizontalRule: Command = (state, dispatch) => { const { $anchor, empty } = state.selection; if (empty && closest($anchor, node => node.type === schema.nodes.p) !== null) { if (dispatch !== undefined) { dispatch(state.tr.replaceSelectionWith(hr().node)); } return true; } if (state.selection instanceof WedgeSelection) { if (dispatch !== undefined) { const tr = state.tr; tr.replaceSelectionWith(hr().node); tr.setSelection(new WedgeSelection(tr.doc.resolve(state.selection.anchor + 1))); dispatch(tr); } return true; } return false; }; export const ux = { [UxCommand.InsertHorizontalRule]: insertHorizontalRule };