add getSplittedAttributes helper

This commit is contained in:
Philipp Kühn 2021-01-29 09:33:42 +01:00
parent 72ff2d212e
commit 97eb9c411c
3 changed files with 46 additions and 45 deletions

View File

@ -2,6 +2,7 @@ import { canSplit } from 'prosemirror-transform'
import { ContentMatch, Fragment } from 'prosemirror-model'
import { EditorState, NodeSelection, TextSelection } from 'prosemirror-state'
import { Command } from '../types'
import getSplittedAttributes from '../helpers/getSplittedAttributes'
function defaultBlockAt(match: ContentMatch) {
for (let i = 0; i < match.edgeCount; i + 1) {
@ -42,22 +43,12 @@ export const splitBlock = (options: Partial<SplitBlockOptions> = {}): Command =>
const config = { ...defaultOptions, ...options }
const { selection, doc } = tr
const { $from, $to } = selection
const extensionAttributes = editor.extensionManager.attributes
.filter(item => item.type === $from.node().type.name)
const currentAttributes = $from.node().attrs
const newAttributes = Object.fromEntries(Object
.entries(currentAttributes)
.filter(([name]) => {
const extensionAttribute = extensionAttributes.find(item => item.name === name)
if (!extensionAttribute) {
return false
}
return extensionAttribute.attribute.keepOnSplit
}))
const newAttributes = getSplittedAttributes(
extensionAttributes,
$from.node().type.name,
$from.node().attrs,
)
if (selection instanceof NodeSelection && selection.node.isBlock) {
if (!$from.parentOffset || !canSplit(doc, $from.pos)) {

View File

@ -8,6 +8,7 @@ import { canSplit } from 'prosemirror-transform'
import { TextSelection } from 'prosemirror-state'
import { Command } from '../types'
import getNodeType from '../helpers/getNodeType'
import getSplittedAttributes from '../helpers/getSplittedAttributes'
/**
* Splits one list item into two list items.
@ -32,6 +33,8 @@ export const splitListItem = (typeOrName: string | NodeType): Command => ({
return false
}
const extensionAttributes = editor.extensionManager.attributes
if ($from.parent.content.size === 0 && $from.node(-1).childCount === $from.indexAfter(-1)) {
// In an empty block. If this is a nested list, the wrapping
// list item should be split. Otherwise, bail out and let next
@ -55,7 +58,12 @@ export const splitListItem = (typeOrName: string | NodeType): Command => ({
}
// Add a second list item with an empty default start node
const nextType = type.contentMatch.defaultType?.createAndFill($from.node().attrs) || undefined
const newNextTypeAttributes = getSplittedAttributes(
extensionAttributes,
$from.node().type.name,
$from.node().attrs,
)
const nextType = type.contentMatch.defaultType?.createAndFill(newNextTypeAttributes) || undefined
wrap = wrap.append(Fragment.from(type.createAndFill(null, nextType) || undefined))
tr
@ -75,35 +83,16 @@ export const splitListItem = (typeOrName: string | NodeType): Command => ({
? grandParent.contentMatchAt(0).defaultType
: null
const extensionAttributes = editor.extensionManager.attributes
const currentTypeAttributes = grandParent.attrs
const currentNextTypeAttributes = $from.node().attrs
const newTypeAttributes = Object.fromEntries(Object
.entries(currentTypeAttributes)
.filter(([name]) => {
const extensionAttribute = extensionAttributes.find(item => {
return item.type === grandParent.type.name && item.name === name
})
if (!extensionAttribute) {
return false
}
return extensionAttribute.attribute.keepOnSplit
}))
const newNextTypeAttributes = Object.fromEntries(Object
.entries(currentNextTypeAttributes)
.filter(([name]) => {
const extensionAttribute = extensionAttributes.find(item => {
return item.type === $from.node().type.name && item.name === name
})
if (!extensionAttribute) {
return false
}
return extensionAttribute.attribute.keepOnSplit
}))
const newTypeAttributes = getSplittedAttributes(
extensionAttributes,
grandParent.type.name,
grandParent.attrs,
)
const newNextTypeAttributes = getSplittedAttributes(
extensionAttributes,
$from.node().type.name,
$from.node().attrs,
)
tr.delete($from.pos, $to.pos)

View File

@ -0,0 +1,21 @@
import { AnyObject, ExtensionAttribute } from '../types'
export default function getSplittedAttributes(
extensionAttributes: ExtensionAttribute[],
typeName: string,
attributes: AnyObject,
): AnyObject {
return Object.fromEntries(Object
.entries(attributes)
.filter(([name]) => {
const extensionAttribute = extensionAttributes.find(item => {
return item.type === typeName && item.name === name
})
if (!extensionAttribute) {
return false
}
return extensionAttribute.attribute.keepOnSplit
}))
}