Merge branch 'main' of github.com:ueberdosis/tiptap-next into main

This commit is contained in:
Hans Pagel
2020-10-28 16:54:19 +01:00
2 changed files with 50 additions and 28 deletions

View File

@@ -44,8 +44,8 @@ export default {
], ],
content: ` content: `
<p>This is a basic example of implementing images. Drag to re-order.</p> <p>This is a basic example of implementing images. Drag to re-order.</p>
<img src="https://source.unsplash.com/8xznAGy4HcY/800x600" /> <img src="https://source.unsplash.com/8xznAGy4HcY/800x400" />
<img src="https://source.unsplash.com/K9QHL52rE2k/800x600" /> <img src="https://source.unsplash.com/K9QHL52rE2k/800x400" />
`, `,
}) })
}, },

View File

@@ -1,12 +1,8 @@
// import {
// baseKeymap, chainCommands, newlineInCode, createParagraphNear, liftEmptyBlock, splitBlock,
// } from 'prosemirror-commands'
import { canSplit } from 'prosemirror-transform' import { canSplit } from 'prosemirror-transform'
import { ContentMatch, Fragment } from 'prosemirror-model' import { ContentMatch, Fragment } from 'prosemirror-model'
import { NodeSelection, TextSelection } from 'prosemirror-state' import { NodeSelection, TextSelection } from 'prosemirror-state'
import { Command } from '../Editor' import { Command } from '../Editor'
import { createExtension } from '../Extension' import { createExtension } from '../Extension'
// import getNodeType from '../utils/getNodeType'
function defaultBlockAt(match: ContentMatch) { function defaultBlockAt(match: ContentMatch) {
for (let i = 0; i < match.edgeCount; i + 1) { for (let i = 0; i < match.edgeCount; i + 1) {
@@ -20,41 +16,67 @@ function defaultBlockAt(match: ContentMatch) {
export const SplitBlock = createExtension({ export const SplitBlock = createExtension({
addCommands() { addCommands() {
return { return {
splitBlock: (copyAttributes = false): Command => ({ state, dispatch }) => { splitBlock: (copyAttributes = false): Command => ({ tr, dispatch }) => {
// const type = getNodeType(typeOrName, state.schema) const { selection, doc } = tr
const { $from, $to } = selection
if (selection instanceof NodeSelection && selection.node.isBlock) {
if (!$from.parentOffset || !canSplit(doc, $from.pos)) {
return false
}
if (dispatch) {
dispatch(tr.split($from.pos).scrollIntoView())
}
const { $from, $to } = state.selection
if (state.selection instanceof NodeSelection && state.selection.node.isBlock) {
if (!$from.parentOffset || !canSplit(state.doc, $from.pos)) return false
if (dispatch) dispatch(state.tr.split($from.pos).scrollIntoView())
return true return true
} }
if (!$from.parent.isBlock) return false if (!$from.parent.isBlock) {
return false
}
if (dispatch) { if (dispatch) {
const atEnd = $to.parentOffset === $to.parent.content.size const atEnd = $to.parentOffset === $to.parent.content.size
const { tr } = state
if (state.selection instanceof TextSelection) tr.deleteSelection() if (selection instanceof TextSelection) {
const deflt = $from.depth === 0 ? null : defaultBlockAt($from.node(-1).contentMatchAt($from.indexAfter(-1))) tr.deleteSelection()
let types = atEnd && deflt ? [{ type: deflt, attrs: copyAttributes ? $from.node().attrs : {} }] : null }
// let types = atEnd && deflt ? [{ type: deflt }] : null
// @ts-ignore const deflt = $from.depth === 0
? undefined
: defaultBlockAt($from.node(-1).contentMatchAt($from.indexAfter(-1)))
let types = atEnd && deflt
? [{ type: deflt, attrs: copyAttributes ? $from.node().attrs : {} }]
: undefined
let can = canSplit(tr.doc, tr.mapping.map($from.pos), 1, types) let can = canSplit(tr.doc, tr.mapping.map($from.pos), 1, types)
// @ts-ignore
if (!types && !can && canSplit(tr.doc, tr.mapping.map($from.pos), 1, deflt && [{ type: deflt }])) { if (
// @ts-ignore !types
types = [{ type: deflt, attrs: copyAttributes ? $from.node().attrs : {} }] && !can
// types = [{ type: deflt }] && canSplit(tr.doc, tr.mapping.map($from.pos), 1, deflt ? [{ type: deflt }] : undefined)
) {
can = true can = true
types = deflt
? [{ type: deflt, attrs: copyAttributes ? $from.node().attrs : {} }]
: undefined
} }
if (can) { if (can) {
// @ts-ignore
tr.split(tr.mapping.map($from.pos), 1, types) tr.split(tr.mapping.map($from.pos), 1, types)
if (!atEnd && !$from.parentOffset && $from.parent.type !== deflt
// @ts-ignore if (
&& $from.node(-1).canReplace($from.index(-1), $from.indexAfter(-1), Fragment.from(deflt.create(), $from.parent))) { tr.setNodeMarkup(tr.mapping.map($from.before()), deflt) } !atEnd
&& !$from.parentOffset
&& $from.parent.type !== deflt
&& $from.node(-1).canReplace($from.index(-1), $from.indexAfter(-1), Fragment.from(deflt?.create()))
) {
tr.setNodeMarkup(tr.mapping.map($from.before()), deflt || undefined)
} }
}
dispatch(tr.scrollIntoView()) dispatch(tr.scrollIntoView())
} }