refactoring

This commit is contained in:
Philipp Kühn 2020-12-01 09:11:58 +01:00
parent 81c0d02f5d
commit f556f00518
8 changed files with 43 additions and 33 deletions

View File

@ -1,7 +1,7 @@
import { lift as originalLift } from 'prosemirror-commands' import { lift as originalLift } from 'prosemirror-commands'
import { NodeType } from 'prosemirror-model' import { NodeType } from 'prosemirror-model'
import { Command } from '../types' import { Command } from '../types'
import nodeIsActive from '../helpers/nodeIsActive' import isNodeActive from '../helpers/isNodeActive'
import getNodeType from '../helpers/getNodeType' import getNodeType from '../helpers/getNodeType'
/** /**
@ -9,7 +9,7 @@ import getNodeType from '../helpers/getNodeType'
*/ */
export const lift = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => { export const lift = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema) const type = getNodeType(typeOrName, state.schema)
const isActive = nodeIsActive(state, type, attributes) const isActive = isNodeActive(state, type, attributes)
if (!isActive) { if (!isActive) {
return false return false

View File

@ -1,14 +1,14 @@
import { MarkType } from 'prosemirror-model' import { MarkType } from 'prosemirror-model'
import { Command } from '../types' import { Command } from '../types'
import getMarkType from '../helpers/getMarkType' import getMarkType from '../helpers/getMarkType'
import markIsActive from '../helpers/markIsActive' import isMarkActive from '../helpers/isMarkActive'
/** /**
* Toggle a mark on and off. * Toggle a mark on and off.
*/ */
export const toggleMark = (typeOrName: string | MarkType, attributes?: {}): Command => ({ state, commands }) => { export const toggleMark = (typeOrName: string | MarkType, attributes?: {}): Command => ({ state, commands }) => {
const type = getMarkType(typeOrName, state.schema) const type = getMarkType(typeOrName, state.schema)
const isActive = markIsActive(state, type, attributes) const isActive = isMarkActive(state, type, attributes)
if (isActive) { if (isActive) {
return commands.unsetMark(type) return commands.unsetMark(type)

View File

@ -1,6 +1,6 @@
import { NodeType } from 'prosemirror-model' import { NodeType } from 'prosemirror-model'
import { Command } from '../types' import { Command } from '../types'
import nodeIsActive from '../helpers/nodeIsActive' import isNodeActive from '../helpers/isNodeActive'
import getNodeType from '../helpers/getNodeType' import getNodeType from '../helpers/getNodeType'
/** /**
@ -9,7 +9,7 @@ import getNodeType from '../helpers/getNodeType'
export const toggleNode = (typeOrName: string | NodeType, toggleTypeOrName: string | NodeType, attrs = {}): Command => ({ state, commands }) => { export const toggleNode = (typeOrName: string | NodeType, toggleTypeOrName: string | NodeType, attrs = {}): Command => ({ state, commands }) => {
const type = getNodeType(typeOrName, state.schema) const type = getNodeType(typeOrName, state.schema)
const toggleType = getNodeType(toggleTypeOrName, state.schema) const toggleType = getNodeType(toggleTypeOrName, state.schema)
const isActive = nodeIsActive(state, type, attrs) const isActive = isNodeActive(state, type, attrs)
if (isActive) { if (isActive) {
return commands.setNode(toggleType) return commands.setNode(toggleType)

View File

@ -1,7 +1,7 @@
import { wrapIn, lift } from 'prosemirror-commands' import { wrapIn, lift } from 'prosemirror-commands'
import { NodeType } from 'prosemirror-model' import { NodeType } from 'prosemirror-model'
import { Command } from '../types' import { Command } from '../types'
import nodeIsActive from '../helpers/nodeIsActive' import isNodeActive from '../helpers/isNodeActive'
import getNodeType from '../helpers/getNodeType' import getNodeType from '../helpers/getNodeType'
/** /**
@ -9,7 +9,7 @@ import getNodeType from '../helpers/getNodeType'
*/ */
export const toggleWrap = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => { export const toggleWrap = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema) const type = getNodeType(typeOrName, state.schema)
const isActive = nodeIsActive(state, type, attributes) const isActive = isNodeActive(state, type, attributes)
if (isActive) { if (isActive) {
return lift(state, dispatch) return lift(state, dispatch)

View File

@ -1,7 +1,7 @@
import { wrapIn as originalWrapIn } from 'prosemirror-commands' import { wrapIn as originalWrapIn } from 'prosemirror-commands'
import { NodeType } from 'prosemirror-model' import { NodeType } from 'prosemirror-model'
import { Command } from '../types' import { Command } from '../types'
import nodeIsActive from '../helpers/nodeIsActive' import isNodeActive from '../helpers/isNodeActive'
import getNodeType from '../helpers/getNodeType' import getNodeType from '../helpers/getNodeType'
/** /**
@ -9,7 +9,7 @@ import getNodeType from '../helpers/getNodeType'
*/ */
export const wrapIn = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => { export const wrapIn = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema) const type = getNodeType(typeOrName, state.schema)
const isActive = nodeIsActive(state, type, attributes) const isActive = isNodeActive(state, type, attributes)
if (isActive) { if (isActive) {
return false return false

View File

@ -1,20 +1,22 @@
import { EditorState } from 'prosemirror-state' import { EditorState } from 'prosemirror-state'
import nodeIsActive from './nodeIsActive' import isNodeActive from './isNodeActive'
import markIsActive from './markIsActive' import isMarkActive from './isMarkActive'
import getSchemaTypeNameByName from './getSchemaTypeNameByName' import getSchemaTypeNameByName from './getSchemaTypeNameByName'
export default function isActive(state: EditorState, name: string | null, attributes: { [key: string ]: any } = {}): boolean { export default function isActive(state: EditorState, name: string | null, attributes: { [key: string ]: any } = {}): boolean {
if (name) { if (!name) {
const schemaType = getSchemaTypeNameByName(name, state.schema) return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes)
if (schemaType === 'node') {
return nodeIsActive(state, state.schema.nodes[name], attributes)
} if (schemaType === 'mark') {
return markIsActive(state, state.schema.marks[name], attributes)
}
return false
} }
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
} }

View File

@ -1,15 +1,19 @@
import { EditorState } from 'prosemirror-state' import { EditorState } from 'prosemirror-state'
import { Mark, MarkType } from 'prosemirror-model' import { Mark, MarkType } from 'prosemirror-model'
import objectIncludes from '../utilities/objectIncludes' import objectIncludes from '../utilities/objectIncludes'
import getMarkType from '../helpers/getMarkType' import getMarkType from './getMarkType'
type MarkRange = { export type MarkRange = {
mark: Mark, mark: Mark,
from: number, from: number,
to: 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 { from, to, empty } = state.selection
const type = typeOrName const type = typeOrName
? getMarkType(typeOrName, state.schema) ? getMarkType(typeOrName, state.schema)
@ -60,5 +64,5 @@ export default function markIsActive(state: EditorState, typeOrName: MarkType |
return sum + size return sum + size
}, 0) }, 0)
return selectionRange <= range return selectionRange === range
} }

View File

@ -1,15 +1,19 @@
import { EditorState } from 'prosemirror-state' import { EditorState } from 'prosemirror-state'
import { Node, NodeType } from 'prosemirror-model' import { Node, NodeType } from 'prosemirror-model'
import objectIncludes from '../utilities/objectIncludes' import objectIncludes from '../utilities/objectIncludes'
import getNodeType from '../helpers/getNodeType' import getNodeType from './getNodeType'
type NodeRange = { export type NodeRange = {
node: Node, node: Node,
from: number, from: number,
to: 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 { from, to, empty } = state.selection
const type = typeOrName const type = typeOrName
? getNodeType(typeOrName, state.schema) ? getNodeType(typeOrName, state.schema)
@ -42,6 +46,8 @@ export default function nodeIsActive(state: EditorState, typeOrName: NodeType |
.find(nodeRange => objectIncludes(nodeRange.node.attrs, attributes)) .find(nodeRange => objectIncludes(nodeRange.node.attrs, attributes))
} }
const selectionRange = to - from
const range = nodeRanges const range = nodeRanges
.filter(nodeRange => { .filter(nodeRange => {
if (!type) { if (!type) {
@ -56,7 +62,5 @@ export default function nodeIsActive(state: EditorState, typeOrName: NodeType |
return sum + size return sum + size
}, 0) }, 0)
const selectionRange = to - from return selectionRange === range
return selectionRange <= range
} }