feat(core): add nodePasteRule to core
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
export * from './markPasteRule'
|
export * from './markPasteRule'
|
||||||
|
export * from './nodePasteRule'
|
||||||
export * from './textPasteRule'
|
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'
|
import { getEmbedURLFromYoutubeURL, isValidYoutubeUrl, YOUTUBE_REGEX_GLOBAL } from './utils'
|
||||||
|
|
||||||
@@ -96,17 +96,11 @@ export const Youtube = Node.create<YoutubeOptions>({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
new PasteRule({
|
nodePasteRule({
|
||||||
find: YOUTUBE_REGEX_GLOBAL,
|
find: YOUTUBE_REGEX_GLOBAL,
|
||||||
|
type: this.type,
|
||||||
handler({ match, chain, range }) {
|
getAttributes: match => {
|
||||||
if (match.input) {
|
return { src: match.input }
|
||||||
chain()
|
|
||||||
.deleteRange(range)
|
|
||||||
.setYoutubeVideo({
|
|
||||||
src: match.input,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user