feat: add forEach command
This commit is contained in:
24
packages/core/src/commands/forEach.ts
Normal file
24
packages/core/src/commands/forEach.ts
Normal 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 }))
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ import * as exitCode from '../commands/exitCode'
|
|||||||
import * as extendMarkRange from '../commands/extendMarkRange'
|
import * as extendMarkRange from '../commands/extendMarkRange'
|
||||||
import * as first from '../commands/first'
|
import * as first from '../commands/first'
|
||||||
import * as focus from '../commands/focus'
|
import * as focus from '../commands/focus'
|
||||||
|
import * as forEach from '../commands/forEach'
|
||||||
import * as insertContent from '../commands/insertContent'
|
import * as insertContent from '../commands/insertContent'
|
||||||
import * as insertContentAt from '../commands/insertContentAt'
|
import * as insertContentAt from '../commands/insertContentAt'
|
||||||
import * as joinBackward from '../commands/joinBackward'
|
import * as joinBackward from '../commands/joinBackward'
|
||||||
@@ -60,6 +61,7 @@ export { exitCode }
|
|||||||
export { extendMarkRange }
|
export { extendMarkRange }
|
||||||
export { first }
|
export { first }
|
||||||
export { focus }
|
export { focus }
|
||||||
|
export { forEach }
|
||||||
export { insertContent }
|
export { insertContent }
|
||||||
export { insertContentAt }
|
export { insertContentAt }
|
||||||
export { joinBackward }
|
export { joinBackward }
|
||||||
@@ -114,6 +116,7 @@ export const Commands = Extension.create({
|
|||||||
...extendMarkRange,
|
...extendMarkRange,
|
||||||
...first,
|
...first,
|
||||||
...focus,
|
...focus,
|
||||||
|
...forEach,
|
||||||
...insertContent,
|
...insertContent,
|
||||||
...insertContentAt,
|
...insertContentAt,
|
||||||
...joinBackward,
|
...joinBackward,
|
||||||
|
|||||||
Reference in New Issue
Block a user