Skip to content

Commit

Permalink
feat(core): add getContent to nodePasteRules (#4212)
Browse files Browse the repository at this point in the history
  • Loading branch information
daibhin authored Aug 9, 2024
1 parent c7fd0f8 commit 86a8553
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-rice-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/core": minor
---

Add `getContent` to nodePasteRules to allow specifying inner content to a created node
19 changes: 14 additions & 5 deletions packages/core/src/pasteRules/nodePasteRule.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NodeType } from '@tiptap/pm/model'

import { PasteRule, PasteRuleFinder } from '../PasteRule.js'
import { ExtendedRegExpMatchArray } from '../types.js'
import { ExtendedRegExpMatchArray, JSONContent } from '../types.js'
import { callOrReturn } from '../utilities/index.js'

/**
Expand All @@ -17,23 +17,32 @@ export function nodePasteRule(config: {
| ((match: ExtendedRegExpMatchArray, event: ClipboardEvent) => Record<string, any>)
| false
| null
getContent?:
| JSONContent[]
| ((attrs: Record<string, any>) => JSONContent[])
| false
| null
}) {
return new PasteRule({
find: config.find,
handler({
match, chain, range, pasteEvent,
}) {
const attributes = callOrReturn(config.getAttributes, undefined, match, pasteEvent)
const content = callOrReturn(config.getContent, undefined, attributes)

if (attributes === false || attributes === null) {
return null
}

const node = { type: config.type.name, attrs: attributes } as JSONContent

if (content) {
node.content = content
}

if (match.input) {
chain().deleteRange(range).insertContentAt(range.from, {
type: config.type.name,
attrs: attributes,
})
chain().deleteRange(range).insertContentAt(range.from, node)
}
},
})
Expand Down

0 comments on commit 86a8553

Please sign in to comment.