mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-11 20:23:36 +08:00
refactoring
This commit is contained in:
parent
81c0d02f5d
commit
f556f00518
@ -1,7 +1,7 @@
|
||||
import { lift as originalLift } from 'prosemirror-commands'
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
import { Command } from '../types'
|
||||
import nodeIsActive from '../helpers/nodeIsActive'
|
||||
import isNodeActive from '../helpers/isNodeActive'
|
||||
import getNodeType from '../helpers/getNodeType'
|
||||
|
||||
/**
|
||||
@ -9,7 +9,7 @@ import getNodeType from '../helpers/getNodeType'
|
||||
*/
|
||||
export const lift = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
const isActive = nodeIsActive(state, type, attributes)
|
||||
const isActive = isNodeActive(state, type, attributes)
|
||||
|
||||
if (!isActive) {
|
||||
return false
|
||||
|
@ -1,14 +1,14 @@
|
||||
import { MarkType } from 'prosemirror-model'
|
||||
import { Command } from '../types'
|
||||
import getMarkType from '../helpers/getMarkType'
|
||||
import markIsActive from '../helpers/markIsActive'
|
||||
import isMarkActive from '../helpers/isMarkActive'
|
||||
|
||||
/**
|
||||
* Toggle a mark on and off.
|
||||
*/
|
||||
export const toggleMark = (typeOrName: string | MarkType, attributes?: {}): Command => ({ state, commands }) => {
|
||||
const type = getMarkType(typeOrName, state.schema)
|
||||
const isActive = markIsActive(state, type, attributes)
|
||||
const isActive = isMarkActive(state, type, attributes)
|
||||
|
||||
if (isActive) {
|
||||
return commands.unsetMark(type)
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
import { Command } from '../types'
|
||||
import nodeIsActive from '../helpers/nodeIsActive'
|
||||
import isNodeActive from '../helpers/isNodeActive'
|
||||
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 }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
const toggleType = getNodeType(toggleTypeOrName, state.schema)
|
||||
const isActive = nodeIsActive(state, type, attrs)
|
||||
const isActive = isNodeActive(state, type, attrs)
|
||||
|
||||
if (isActive) {
|
||||
return commands.setNode(toggleType)
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { wrapIn, lift } from 'prosemirror-commands'
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
import { Command } from '../types'
|
||||
import nodeIsActive from '../helpers/nodeIsActive'
|
||||
import isNodeActive from '../helpers/isNodeActive'
|
||||
import getNodeType from '../helpers/getNodeType'
|
||||
|
||||
/**
|
||||
@ -9,7 +9,7 @@ import getNodeType from '../helpers/getNodeType'
|
||||
*/
|
||||
export const toggleWrap = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
const isActive = nodeIsActive(state, type, attributes)
|
||||
const isActive = isNodeActive(state, type, attributes)
|
||||
|
||||
if (isActive) {
|
||||
return lift(state, dispatch)
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { wrapIn as originalWrapIn } from 'prosemirror-commands'
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
import { Command } from '../types'
|
||||
import nodeIsActive from '../helpers/nodeIsActive'
|
||||
import isNodeActive from '../helpers/isNodeActive'
|
||||
import getNodeType from '../helpers/getNodeType'
|
||||
|
||||
/**
|
||||
@ -9,7 +9,7 @@ import getNodeType from '../helpers/getNodeType'
|
||||
*/
|
||||
export const wrapIn = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
const isActive = nodeIsActive(state, type, attributes)
|
||||
const isActive = isNodeActive(state, type, attributes)
|
||||
|
||||
if (isActive) {
|
||||
return false
|
||||
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user