refactoring
This commit is contained in:
@@ -1,20 +1,22 @@
|
||||
import { EditorState } from 'prosemirror-state'
|
||||
import nodeIsActive from './nodeIsActive'
|
||||
import markIsActive from './markIsActive'
|
||||
import isNodeActive from './isNodeActive'
|
||||
import isMarkActive from './isMarkActive'
|
||||
import getSchemaTypeNameByName from './getSchemaTypeNameByName'
|
||||
|
||||
export default function isActive(state: EditorState, name: string | null, attributes: { [key: string ]: any } = {}): boolean {
|
||||
if (name) {
|
||||
const schemaType = getSchemaTypeNameByName(name, state.schema)
|
||||
|
||||
if (schemaType === 'node') {
|
||||
return nodeIsActive(state, state.schema.nodes[name], attributes)
|
||||
} if (schemaType === 'mark') {
|
||||
return markIsActive(state, state.schema.marks[name], attributes)
|
||||
}
|
||||
|
||||
return false
|
||||
if (!name) {
|
||||
return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes)
|
||||
}
|
||||
|
||||
return nodeIsActive(state, null, attributes) || markIsActive(state, null, attributes)
|
||||
const schemaType = getSchemaTypeNameByName(name, state.schema)
|
||||
|
||||
if (schemaType === 'node') {
|
||||
return isNodeActive(state, name, attributes)
|
||||
}
|
||||
|
||||
if (schemaType === 'mark') {
|
||||
return isMarkActive(state, name, attributes)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
import { EditorState } from 'prosemirror-state'
|
||||
import { Mark, MarkType } from 'prosemirror-model'
|
||||
import objectIncludes from '../utilities/objectIncludes'
|
||||
import getMarkType from '../helpers/getMarkType'
|
||||
import getMarkType from './getMarkType'
|
||||
|
||||
type MarkRange = {
|
||||
export type MarkRange = {
|
||||
mark: Mark,
|
||||
from: number,
|
||||
to: number,
|
||||
}
|
||||
|
||||
export default function markIsActive(state: EditorState, typeOrName: MarkType | string | null, attributes = {}) {
|
||||
export default function isMarkActive(
|
||||
state: EditorState,
|
||||
typeOrName: MarkType | string | null,
|
||||
attributes = {},
|
||||
): boolean {
|
||||
const { from, to, empty } = state.selection
|
||||
const type = typeOrName
|
||||
? getMarkType(typeOrName, state.schema)
|
||||
@@ -60,5 +64,5 @@ export default function markIsActive(state: EditorState, typeOrName: MarkType |
|
||||
return sum + size
|
||||
}, 0)
|
||||
|
||||
return selectionRange <= range
|
||||
return selectionRange === range
|
||||
}
|
||||
@@ -1,15 +1,19 @@
|
||||
import { EditorState } from 'prosemirror-state'
|
||||
import { Node, NodeType } from 'prosemirror-model'
|
||||
import objectIncludes from '../utilities/objectIncludes'
|
||||
import getNodeType from '../helpers/getNodeType'
|
||||
import getNodeType from './getNodeType'
|
||||
|
||||
type NodeRange = {
|
||||
export type NodeRange = {
|
||||
node: Node,
|
||||
from: number,
|
||||
to: number,
|
||||
}
|
||||
|
||||
export default function nodeIsActive(state: EditorState, typeOrName: NodeType | string | null, attributes = {}) {
|
||||
export default function isNodeActive(
|
||||
state: EditorState,
|
||||
typeOrName: NodeType | string | null,
|
||||
attributes = {},
|
||||
): boolean {
|
||||
const { from, to, empty } = state.selection
|
||||
const type = typeOrName
|
||||
? getNodeType(typeOrName, state.schema)
|
||||
@@ -42,6 +46,8 @@ export default function nodeIsActive(state: EditorState, typeOrName: NodeType |
|
||||
.find(nodeRange => objectIncludes(nodeRange.node.attrs, attributes))
|
||||
}
|
||||
|
||||
const selectionRange = to - from
|
||||
|
||||
const range = nodeRanges
|
||||
.filter(nodeRange => {
|
||||
if (!type) {
|
||||
@@ -56,7 +62,5 @@ export default function nodeIsActive(state: EditorState, typeOrName: NodeType |
|
||||
return sum + size
|
||||
}, 0)
|
||||
|
||||
const selectionRange = to - from
|
||||
|
||||
return selectionRange <= range
|
||||
return selectionRange === range
|
||||
}
|
||||
Reference in New Issue
Block a user