tiptap/packages/core/src/commands/lift.ts

28 lines
779 B
TypeScript
Raw Normal View History

2020-11-18 23:43:27 +08:00
import { lift as originalLift } from 'prosemirror-commands'
2020-11-18 18:54:18 +08:00
import { NodeType } from 'prosemirror-model'
2021-06-05 03:56:29 +08:00
import { RawCommands } from '../types'
2020-12-01 16:11:58 +08:00
import isNodeActive from '../helpers/isNodeActive'
2020-11-30 16:42:53 +08:00
import getNodeType from '../helpers/getNodeType'
2020-11-18 18:54:18 +08:00
2021-02-11 01:05:02 +08:00
declare module '@tiptap/core' {
2021-06-05 03:56:29 +08:00
interface Commands<ReturnType> {
2021-02-16 18:27:58 +08:00
lift: {
/**
* Removes an existing wrap.
*/
2021-06-05 03:56:29 +08:00
lift: (typeOrName: string | NodeType, attributes?: Record<string, any>) => ReturnType,
2021-02-16 18:27:58 +08:00
}
2021-02-11 01:05:02 +08:00
}
}
2021-02-17 01:36:37 +08:00
export const lift: RawCommands['lift'] = (typeOrName, attributes = {}) => ({ state, dispatch }) => {
2020-11-18 18:54:18 +08:00
const type = getNodeType(typeOrName, state.schema)
2020-12-01 16:11:58 +08:00
const isActive = isNodeActive(state, type, attributes)
2020-11-18 18:54:18 +08:00
if (!isActive) {
return false
}
2020-11-18 23:43:27 +08:00
return originalLift(state, dispatch)
2020-11-18 18:54:18 +08:00
}