add paste rules

This commit is contained in:
Philipp Kühn
2018-11-26 11:11:39 +01:00
parent df402a66ad
commit d27b0deb6a
6 changed files with 97 additions and 1 deletions

View 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),
},
})
}

View File

@@ -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,
}