mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-08 01:53:04 +08:00
feat(core): add getContent
to nodePasteRules (#4212)
This commit is contained in:
parent
c7fd0f80af
commit
86a855355f
5
.changeset/gorgeous-rice-yell.md
Normal file
5
.changeset/gorgeous-rice-yell.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@tiptap/core": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Add `getContent` to nodePasteRules to allow specifying inner content to a created node
|
@ -1,7 +1,7 @@
|
|||||||
import { NodeType } from '@tiptap/pm/model'
|
import { NodeType } from '@tiptap/pm/model'
|
||||||
|
|
||||||
import { PasteRule, PasteRuleFinder } from '../PasteRule.js'
|
import { PasteRule, PasteRuleFinder } from '../PasteRule.js'
|
||||||
import { ExtendedRegExpMatchArray } from '../types.js'
|
import { ExtendedRegExpMatchArray, JSONContent } from '../types.js'
|
||||||
import { callOrReturn } from '../utilities/index.js'
|
import { callOrReturn } from '../utilities/index.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -17,6 +17,11 @@ export function nodePasteRule(config: {
|
|||||||
| ((match: ExtendedRegExpMatchArray, event: ClipboardEvent) => Record<string, any>)
|
| ((match: ExtendedRegExpMatchArray, event: ClipboardEvent) => Record<string, any>)
|
||||||
| false
|
| false
|
||||||
| null
|
| null
|
||||||
|
getContent?:
|
||||||
|
| JSONContent[]
|
||||||
|
| ((attrs: Record<string, any>) => JSONContent[])
|
||||||
|
| false
|
||||||
|
| null
|
||||||
}) {
|
}) {
|
||||||
return new PasteRule({
|
return new PasteRule({
|
||||||
find: config.find,
|
find: config.find,
|
||||||
@ -24,16 +29,20 @@ export function nodePasteRule(config: {
|
|||||||
match, chain, range, pasteEvent,
|
match, chain, range, pasteEvent,
|
||||||
}) {
|
}) {
|
||||||
const attributes = callOrReturn(config.getAttributes, undefined, match, pasteEvent)
|
const attributes = callOrReturn(config.getAttributes, undefined, match, pasteEvent)
|
||||||
|
const content = callOrReturn(config.getContent, undefined, attributes)
|
||||||
|
|
||||||
if (attributes === false || attributes === null) {
|
if (attributes === false || attributes === null) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const node = { type: config.type.name, attrs: attributes } as JSONContent
|
||||||
|
|
||||||
|
if (content) {
|
||||||
|
node.content = content
|
||||||
|
}
|
||||||
|
|
||||||
if (match.input) {
|
if (match.input) {
|
||||||
chain().deleteRange(range).insertContentAt(range.from, {
|
chain().deleteRange(range).insertContentAt(range.from, node)
|
||||||
type: config.type.name,
|
|
||||||
attrs: attributes,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user