Merge branch 'master' into markdown-paste

This commit is contained in:
Philipp Kühn 2019-02-01 17:56:22 +01:00 committed by GitHub
commit d9b2e2054d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 5 deletions

View File

@ -29,7 +29,11 @@ body, .usertext {
export const ExplicitImportExample = `import javascript from 'highlight.js/lib/languages/javascript' export const ExplicitImportExample = `import javascript from 'highlight.js/lib/languages/javascript'
import css from 'highlight.js/lib/languages/css'
import { Editor } from 'tiptap' import { Editor } from 'tiptap'
import {
CodeBlockHighlight,
} from 'tiptap-extensions'
export default { export default {
components: { components: {
@ -38,7 +42,7 @@ export default {
data() { data() {
return { return {
extensions: [ extensions: [
new CodeBlockHighlightNode({ new CodeBlockHighlight({
languages: { languages: {
javascript, javascript,
css, css,

View File

@ -0,0 +1,14 @@
import { InputRule } from 'prosemirror-inputrules'
export default function (regexp, type, getAttrs) {
return new InputRule(regexp, (state, match, start, end) => {
const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
const { tr } = state
if (match[0]) {
tr.replaceWith(start - 1, end, type.create(attrs))
}
return tr
})
}

View File

@ -40,6 +40,7 @@ import {
import insertText from './commands/insertText' import insertText from './commands/insertText'
import markInputRule from './commands/markInputRule' import markInputRule from './commands/markInputRule'
import nodeInputRule from './commands/nodeInputRule'
import pasteRule from './commands/pasteRule' import pasteRule from './commands/pasteRule'
import markPasteRule from './commands/markPasteRule' import markPasteRule from './commands/markPasteRule'
import removeMark from './commands/removeMark' import removeMark from './commands/removeMark'
@ -93,6 +94,7 @@ export {
insertText, insertText,
markInputRule, markInputRule,
markPasteRule, markPasteRule,
nodeInputRule,
pasteRule, pasteRule,
removeMark, removeMark,
replaceText, replaceText,

View File

@ -122,13 +122,16 @@ export default class CodeBlockHighlight extends Node {
init(_, { doc }) { init(_, { doc }) {
return getDecorations(doc) return getDecorations(doc)
}, },
apply(tr, set) { apply(transaction, decorationSet, oldState) {
// TODO: find way to cache decorations // TODO: find way to cache decorations
// see: https://discuss.prosemirror.net/t/how-to-update-multiple-inline-decorations-on-node-change/1493 // see: https://discuss.prosemirror.net/t/how-to-update-multiple-inline-decorations-on-node-change/1493
if (tr.docChanged) {
return getDecorations(tr.doc) const previousNodeName = oldState.selection.$head.parent.type.name
if (transaction.docChanged && previousNodeName === 'code_block') {
return getDecorations(transaction.doc)
} }
return set.map(tr.mapping, tr.doc)
return decorationSet.map(transaction.mapping, transaction.doc)
}, },
}, },
props: { props: {

View File

@ -1,4 +1,5 @@
import { Node } from 'tiptap' import { Node } from 'tiptap'
import { nodeInputRule } from 'tiptap-commands'
export default class HorizontalRule extends Node { export default class HorizontalRule extends Node {
get name() { get name() {
@ -16,4 +17,10 @@ export default class HorizontalRule extends Node {
commands({ type }) { commands({ type }) {
return () => (state, dispatch) => dispatch(state.tr.replaceSelectionWith(type.create())) return () => (state, dispatch) => dispatch(state.tr.replaceSelectionWith(type.create()))
} }
inputRules({ type }) {
return [
nodeInputRule(/^(?:---|___\s|\*\*\*\s)$/, type),
]
}
} }