add paste rules
This commit is contained in:
52
packages/tiptap-commands/src/commands/wrappingPasteRule.js
Normal file
52
packages/tiptap-commands/src/commands/wrappingPasteRule.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import { Plugin } from 'prosemirror-state'
|
||||
import { Slice, Fragment } from 'prosemirror-model'
|
||||
|
||||
export default function (regexp, type, getAttrs) {
|
||||
|
||||
const handler = fragment => {
|
||||
const nodes = []
|
||||
|
||||
fragment.forEach(child => {
|
||||
if (child.isText) {
|
||||
const { text } = child
|
||||
let pos = 0
|
||||
let match
|
||||
|
||||
do {
|
||||
match = regexp.exec(text)
|
||||
if (match) {
|
||||
const start = match.index
|
||||
const end = start + match[0].length
|
||||
const attrs = getAttrs instanceof Function ? getAttrs(match[0]) : getAttrs
|
||||
|
||||
if (start > 0) {
|
||||
nodes.push(child.cut(pos, start))
|
||||
}
|
||||
|
||||
nodes.push(child
|
||||
.cut(start, end)
|
||||
.mark(type.create(attrs)
|
||||
.addToSet(child.marks)))
|
||||
|
||||
pos = end
|
||||
}
|
||||
} while (match)
|
||||
|
||||
if (pos < text.length) {
|
||||
nodes.push(child.cut(pos))
|
||||
}
|
||||
} else {
|
||||
nodes.push(child.copy(handler(child.content)))
|
||||
}
|
||||
})
|
||||
|
||||
return Fragment.fromArray(nodes)
|
||||
}
|
||||
|
||||
return new Plugin({
|
||||
props: {
|
||||
transformPasted: slice => new Slice(handler(slice.content), slice.openStart, slice.openEnd),
|
||||
},
|
||||
})
|
||||
|
||||
}
|
||||
@@ -48,6 +48,7 @@ import toggleBlockType from './commands/toggleBlockType'
|
||||
import toggleList from './commands/toggleList'
|
||||
import toggleWrap from './commands/toggleWrap'
|
||||
import updateMark from './commands/updateMark'
|
||||
import wrappingPasteRule from './commands/wrappingPasteRule'
|
||||
|
||||
export {
|
||||
// prosemirror-commands
|
||||
@@ -98,4 +99,5 @@ export {
|
||||
toggleList,
|
||||
toggleWrap,
|
||||
updateMark,
|
||||
wrappingPasteRule,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user