mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-24 03:39:01 +08:00
feat(core): add nodePasteRule to core
This commit is contained in:
parent
ec595ff803
commit
15123ee092
@ -1,2 +1,3 @@
|
||||
export * from './markPasteRule'
|
||||
export * from './nodePasteRule'
|
||||
export * from './textPasteRule'
|
||||
|
39
packages/core/src/pasteRules/nodePasteRule.ts
Normal file
39
packages/core/src/pasteRules/nodePasteRule.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
|
||||
import { PasteRule } from '../PasteRule'
|
||||
import { ExtendedRegExpMatchArray } from '../types'
|
||||
import { callOrReturn } from '../utilities'
|
||||
|
||||
/**
|
||||
* Build an paste rule that adds a node when the
|
||||
* matched text is pasted into it.
|
||||
*/
|
||||
export function nodePasteRule(config: {
|
||||
find: RegExp,
|
||||
type: NodeType,
|
||||
getAttributes?:
|
||||
| Record<string, any>
|
||||
| ((match: ExtendedRegExpMatchArray) => Record<string, any>)
|
||||
| false
|
||||
| null,
|
||||
}) {
|
||||
return new PasteRule({
|
||||
find: config.find,
|
||||
handler({ match, chain, range }) {
|
||||
const attributes = callOrReturn(config.getAttributes, undefined, match)
|
||||
|
||||
if (attributes === false || attributes === null) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (match.input) {
|
||||
chain()
|
||||
.deleteRange(range)
|
||||
.insertContent({
|
||||
type: config.type.name,
|
||||
attrs: attributes,
|
||||
})
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import { mergeAttributes, Node, PasteRule } from '@tiptap/core'
|
||||
import { mergeAttributes, Node, nodePasteRule } from '@tiptap/core'
|
||||
|
||||
import { getEmbedURLFromYoutubeURL, isValidYoutubeUrl, YOUTUBE_REGEX_GLOBAL } from './utils'
|
||||
|
||||
@ -96,17 +96,11 @@ export const Youtube = Node.create<YoutubeOptions>({
|
||||
}
|
||||
|
||||
return [
|
||||
new PasteRule({
|
||||
nodePasteRule({
|
||||
find: YOUTUBE_REGEX_GLOBAL,
|
||||
|
||||
handler({ match, chain, range }) {
|
||||
if (match.input) {
|
||||
chain()
|
||||
.deleteRange(range)
|
||||
.setYoutubeVideo({
|
||||
src: match.input,
|
||||
})
|
||||
}
|
||||
type: this.type,
|
||||
getAttributes: match => {
|
||||
return { src: match.input }
|
||||
},
|
||||
}),
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user