mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-15 02:59:01 +08:00
Merge pull request #184 from Chrissi2812/markdown-paste
Markdown paste support.
This commit is contained in:
commit
89ec3800db
57
packages/tiptap-commands/src/commands/markPasteRule.js
Normal file
57
packages/tiptap-commands/src/commands/markPasteRule.js
Normal file
@ -0,0 +1,57 @@
|
||||
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
|
||||
|
||||
// eslint-disable-next-line
|
||||
while ((match = regexp.exec(text)) !== null) {
|
||||
if (match[1]) {
|
||||
const start = match.index
|
||||
const end = start + match[0].length
|
||||
const textStart = start + match[0].indexOf(match[1])
|
||||
const textEnd = textStart + match[1].length
|
||||
const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
|
||||
|
||||
// adding text before markdown to nodes
|
||||
if (start > 0) {
|
||||
nodes.push(child.cut(pos, start))
|
||||
}
|
||||
|
||||
// adding the markdown part to nodes
|
||||
nodes.push(child
|
||||
.cut(textStart, textEnd)
|
||||
.mark(type.create(attrs)
|
||||
.addToSet(child.marks)))
|
||||
|
||||
pos = end
|
||||
}
|
||||
}
|
||||
|
||||
// adding rest of text to nodes
|
||||
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),
|
||||
},
|
||||
})
|
||||
|
||||
}
|
@ -42,6 +42,7 @@ import insertText from './commands/insertText'
|
||||
import markInputRule from './commands/markInputRule'
|
||||
import nodeInputRule from './commands/nodeInputRule'
|
||||
import pasteRule from './commands/pasteRule'
|
||||
import markPasteRule from './commands/markPasteRule'
|
||||
import removeMark from './commands/removeMark'
|
||||
import replaceText from './commands/replaceText'
|
||||
import setInlineBlockType from './commands/setInlineBlockType'
|
||||
@ -92,6 +93,7 @@ export {
|
||||
// custom
|
||||
insertText,
|
||||
markInputRule,
|
||||
markPasteRule,
|
||||
nodeInputRule,
|
||||
pasteRule,
|
||||
removeMark,
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Mark } from 'tiptap'
|
||||
import { toggleMark, markInputRule } from 'tiptap-commands'
|
||||
import { toggleMark, markInputRule, markPasteRule } from 'tiptap-commands'
|
||||
|
||||
export default class Bold extends Mark {
|
||||
|
||||
@ -42,4 +42,10 @@ export default class Bold extends Mark {
|
||||
]
|
||||
}
|
||||
|
||||
pasteRules({ type }) {
|
||||
return [
|
||||
markPasteRule(/(?:\*\*|__)([^*_]+)(?:\*\*|__)/g, type),
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Mark } from 'tiptap'
|
||||
import { toggleMark, markInputRule } from 'tiptap-commands'
|
||||
import { toggleMark, markInputRule, markPasteRule } from 'tiptap-commands'
|
||||
|
||||
export default class Code extends Mark {
|
||||
|
||||
@ -32,4 +32,10 @@ export default class Code extends Mark {
|
||||
]
|
||||
}
|
||||
|
||||
pasteRules({ type }) {
|
||||
return [
|
||||
markPasteRule(/(?:`)([^`]+)(?:`)/g, type),
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Mark } from 'tiptap'
|
||||
import { toggleMark, markInputRule } from 'tiptap-commands'
|
||||
import { toggleMark, markInputRule, markPasteRule } from 'tiptap-commands'
|
||||
|
||||
export default class Italic extends Mark {
|
||||
|
||||
@ -34,4 +34,10 @@ export default class Italic extends Mark {
|
||||
]
|
||||
}
|
||||
|
||||
pasteRules({ type }) {
|
||||
return [
|
||||
markPasteRule(/(?:^|[^*_])(?:\*|_)([^*_]+)(?:\*|_)/g, type),
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Mark } from 'tiptap'
|
||||
import { toggleMark, markInputRule } from 'tiptap-commands'
|
||||
import { toggleMark, markInputRule, markPasteRule } from 'tiptap-commands'
|
||||
|
||||
export default class Strike extends Mark {
|
||||
|
||||
@ -44,4 +44,10 @@ export default class Strike extends Mark {
|
||||
]
|
||||
}
|
||||
|
||||
pasteRules({ type }) {
|
||||
return [
|
||||
markPasteRule(/~([^~]+)~/g, type),
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -31,6 +31,7 @@ export default class Editor {
|
||||
onUpdate: () => {},
|
||||
onFocus: () => {},
|
||||
onBlur: () => {},
|
||||
onPaste: () => {},
|
||||
}
|
||||
|
||||
this.init(options)
|
||||
@ -198,6 +199,8 @@ export default class Editor {
|
||||
createView() {
|
||||
const view = new EditorView(this.element, {
|
||||
state: this.state,
|
||||
handlePaste: this.options.onPaste,
|
||||
handleDrop: this.options.onPaste,
|
||||
dispatchTransaction: this.dispatchTransaction.bind(this),
|
||||
})
|
||||
|
||||
|
@ -27,6 +27,10 @@ export default class Extension {
|
||||
return []
|
||||
}
|
||||
|
||||
pasteRules() {
|
||||
return []
|
||||
}
|
||||
|
||||
keys() {
|
||||
return {}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user