import { Schema, MarkType, NodeType } from "prosemirror-model"; import { EditorState } from "prosemirror-state"; import { closest } from "../pquery"; import { HeadingAttrs, schema } from "../schema"; import { isTextSelection } from "../util"; export function isHActive(editorState: EditorState, level: number) { const { selection } = editorState; return ( isTextSelection(selection) && selection.$anchor.sameParent(selection.$head) && closest(selection.$anchor, node => node.type === schema.nodes.h && (node.attrs as HeadingAttrs).l === level) !== null ); } export function isNodeActive(editorState: EditorState, nodeType: NodeType) { const { selection } = editorState; return ( isTextSelection(selection) && selection.$anchor.sameParent(selection.$head) && closest(selection.$anchor, node => node.type === nodeType) !== null ); } export function isBqActive(editorState: EditorState) { return isNodeActive(editorState, schema.nodes.bq); } export function isUlActive(editorState: EditorState) { return isNodeActive(editorState, schema.nodes.ul); } export function isOlActive(editorState: EditorState) { return isNodeActive(editorState, schema.nodes.ol); } export function isMarkActive(editorState: EditorState, markType: MarkType) { const { doc, storedMarks, selection: { $from, from, to, empty } } = editorState; if (empty) { return markType.isInSet(storedMarks != null ? storedMarks : $from.marks()) != null; } return doc.rangeHasMark(from, to, markType); }