feat: add forEach command

This commit is contained in:
Philipp Kühn 2021-06-04 22:25:53 +02:00
parent 78e2a6e775
commit 783ce4e3ac
2 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,24 @@
import { Command, RawCommands } from '../types'
declare module '@tiptap/core' {
interface Commands<ReturnType> {
forEach: {
/**
* Loop through an array of items.
*/
forEach: <T>(
items: T[],
fn: (
item: T,
props: Parameters<Command>[0] & {
index: number,
},
) => boolean,
) => ReturnType,
}
}
}
export const forEach: RawCommands['forEach'] = (items, fn) => props => {
return items.every((item, index) => fn(item, { ...props, index }))
}

View File

@ -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,