tiptap/packages/core/src/utils/markIsActive.ts

24 lines
634 B
TypeScript
Raw Normal View History

2020-03-30 06:20:38 +08:00
import { EditorState } from 'prosemirror-state'
import { MarkType } from 'prosemirror-model'
2020-10-05 23:12:57 +08:00
import markHasAttributes from './markHasAttributes'
2020-11-06 07:13:18 +08:00
import isEmptyObject from './isEmptyObject'
2020-03-30 06:20:38 +08:00
2020-11-06 07:13:18 +08:00
export default function markIsActive(state: EditorState, type: MarkType, attributes = {}) {
2020-03-30 06:20:38 +08:00
const {
from,
$from,
to,
empty,
} = state.selection
2020-11-06 07:13:18 +08:00
const hasMark = empty
? !!(type.isInSet(state.storedMarks || $from.marks()))
: state.doc.rangeHasMark(from, to, type)
2020-10-05 23:12:57 +08:00
2020-11-06 07:13:18 +08:00
const hasAttributes = markHasAttributes(state, type, attributes)
2020-03-30 06:20:38 +08:00
2020-11-06 07:13:18 +08:00
return isEmptyObject(attributes)
? hasMark
: hasMark && hasAttributes
2020-03-30 06:20:38 +08:00
}