Merge pull request #184 from Chrissi2812/markdown-paste

Markdown paste support.
This commit is contained in:
Philipp Kühn 2019-02-01 17:56:31 +01:00 committed by GitHub
commit 89ec3800db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 94 additions and 4 deletions

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

View File

@ -42,6 +42,7 @@ import insertText from './commands/insertText'
import markInputRule from './commands/markInputRule' import markInputRule from './commands/markInputRule'
import nodeInputRule from './commands/nodeInputRule' import nodeInputRule from './commands/nodeInputRule'
import pasteRule from './commands/pasteRule' import pasteRule from './commands/pasteRule'
import markPasteRule from './commands/markPasteRule'
import removeMark from './commands/removeMark' import removeMark from './commands/removeMark'
import replaceText from './commands/replaceText' import replaceText from './commands/replaceText'
import setInlineBlockType from './commands/setInlineBlockType' import setInlineBlockType from './commands/setInlineBlockType'
@ -92,6 +93,7 @@ export {
// custom // custom
insertText, insertText,
markInputRule, markInputRule,
markPasteRule,
nodeInputRule, nodeInputRule,
pasteRule, pasteRule,
removeMark, removeMark,

View File

@ -1,5 +1,5 @@
import { Mark } from 'tiptap' import { Mark } from 'tiptap'
import { toggleMark, markInputRule } from 'tiptap-commands' import { toggleMark, markInputRule, markPasteRule } from 'tiptap-commands'
export default class Bold extends Mark { export default class Bold extends Mark {
@ -42,4 +42,10 @@ export default class Bold extends Mark {
] ]
} }
pasteRules({ type }) {
return [
markPasteRule(/(?:\*\*|__)([^*_]+)(?:\*\*|__)/g, type),
]
}
} }

View File

@ -1,5 +1,5 @@
import { Mark } from 'tiptap' import { Mark } from 'tiptap'
import { toggleMark, markInputRule } from 'tiptap-commands' import { toggleMark, markInputRule, markPasteRule } from 'tiptap-commands'
export default class Code extends Mark { export default class Code extends Mark {
@ -32,4 +32,10 @@ export default class Code extends Mark {
] ]
} }
pasteRules({ type }) {
return [
markPasteRule(/(?:`)([^`]+)(?:`)/g, type),
]
}
} }

View File

@ -1,5 +1,5 @@
import { Mark } from 'tiptap' import { Mark } from 'tiptap'
import { toggleMark, markInputRule } from 'tiptap-commands' import { toggleMark, markInputRule, markPasteRule } from 'tiptap-commands'
export default class Italic extends Mark { export default class Italic extends Mark {
@ -34,4 +34,10 @@ export default class Italic extends Mark {
] ]
} }
pasteRules({ type }) {
return [
markPasteRule(/(?:^|[^*_])(?:\*|_)([^*_]+)(?:\*|_)/g, type),
]
}
} }

View File

@ -1,5 +1,5 @@
import { Mark } from 'tiptap' import { Mark } from 'tiptap'
import { toggleMark, markInputRule } from 'tiptap-commands' import { toggleMark, markInputRule, markPasteRule } from 'tiptap-commands'
export default class Strike extends Mark { export default class Strike extends Mark {
@ -44,4 +44,10 @@ export default class Strike extends Mark {
] ]
} }
pasteRules({ type }) {
return [
markPasteRule(/~([^~]+)~/g, type),
]
}
} }

View File

@ -31,6 +31,7 @@ export default class Editor {
onUpdate: () => {}, onUpdate: () => {},
onFocus: () => {}, onFocus: () => {},
onBlur: () => {}, onBlur: () => {},
onPaste: () => {},
} }
this.init(options) this.init(options)
@ -198,6 +199,8 @@ export default class Editor {
createView() { createView() {
const view = new EditorView(this.element, { const view = new EditorView(this.element, {
state: this.state, state: this.state,
handlePaste: this.options.onPaste,
handleDrop: this.options.onPaste,
dispatchTransaction: this.dispatchTransaction.bind(this), dispatchTransaction: this.dispatchTransaction.bind(this),
}) })

View File

@ -27,6 +27,10 @@ export default class Extension {
return [] return []
} }
pasteRules() {
return []
}
keys() { keys() {
return {} return {}
} }