tiptap/packages/core/src/commands/undoInputRule.ts
Arnau Gómez Farell 89bd9c7d29
Fix/enforce-type-imports-so-that-bundler-ignores-types (#6132)
* fix: enforce type imports so that the bundler ignores types

* chore: add changeset

* fix: export types with export type keyword
2025-03-03 15:15:30 +01:00

50 lines
1.2 KiB
TypeScript

import type { RawCommands } from '../types.js'
declare module '@tiptap/core' {
interface Commands<ReturnType> {
undoInputRule: {
/**
* Undo an input rule.
* @example editor.commands.undoInputRule()
*/
undoInputRule: () => ReturnType
}
}
}
export const undoInputRule: RawCommands['undoInputRule'] =
() =>
({ state, dispatch }) => {
const plugins = state.plugins
for (let i = 0; i < plugins.length; i += 1) {
const plugin = plugins[i]
let undoable
// @ts-ignore
// eslint-disable-next-line
if (plugin.spec.isInputRules && (undoable = plugin.getState(state))) {
if (dispatch) {
const tr = state.tr
const toUndo = undoable.transform
for (let j = toUndo.steps.length - 1; j >= 0; j -= 1) {
tr.step(toUndo.steps[j].invert(toUndo.docs[j]))
}
if (undoable.text) {
const marks = tr.doc.resolve(undoable.from).marks()
tr.replaceWith(undoable.from, undoable.to, state.schema.text(undoable.text, marks))
} else {
tr.delete(undoable.from, undoable.to)
}
}
return true
}
}
return false
}