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

View File

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