diff --git a/packages/core/src/commands/forEach.ts b/packages/core/src/commands/forEach.ts new file mode 100644 index 00000000..5b383ac0 --- /dev/null +++ b/packages/core/src/commands/forEach.ts @@ -0,0 +1,24 @@ +import { Command, RawCommands } from '../types' + +declare module '@tiptap/core' { + interface Commands { + forEach: { + /** + * Loop through an array of items. + */ + forEach: ( + items: T[], + fn: ( + item: T, + props: Parameters[0] & { + index: number, + }, + ) => boolean, + ) => ReturnType, + } + } +} + +export const forEach: RawCommands['forEach'] = (items, fn) => props => { + return items.every((item, index) => fn(item, { ...props, index })) +} diff --git a/packages/core/src/extensions/commands.ts b/packages/core/src/extensions/commands.ts index 266644b2..4c06b154 100644 --- a/packages/core/src/extensions/commands.ts +++ b/packages/core/src/extensions/commands.ts @@ -11,6 +11,7 @@ import * as exitCode from '../commands/exitCode' import * as extendMarkRange from '../commands/extendMarkRange' import * as first from '../commands/first' import * as focus from '../commands/focus' +import * as forEach from '../commands/forEach' import * as insertContent from '../commands/insertContent' import * as insertContentAt from '../commands/insertContentAt' import * as joinBackward from '../commands/joinBackward' @@ -60,6 +61,7 @@ export { exitCode } export { extendMarkRange } export { first } export { focus } +export { forEach } export { insertContent } export { insertContentAt } export { joinBackward } @@ -114,6 +116,7 @@ export const Commands = Extension.create({ ...extendMarkRange, ...first, ...focus, + ...forEach, ...insertContent, ...insertContentAt, ...joinBackward,