add getSplittedAttributes helper
This commit is contained in:
@@ -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)) {
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
|
|||||||
21
packages/core/src/helpers/getSplittedAttributes.ts
Normal file
21
packages/core/src/helpers/getSplittedAttributes.ts
Normal 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
|
||||||
|
}))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user