Skip to content

Commit

Permalink
feat(portable-text-editor): remove any arbitrary properties from a sp…
Browse files Browse the repository at this point in the history
…lit block

There could be some arbritary properties on the value, make sure that these are not carried over to the new split block.
  • Loading branch information
skogsmaskin authored and robinpyon committed Feb 15, 2024
1 parent a7af2a8 commit 4a5ff45
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ import {
type PortableTextSpan,
type PortableTextTextBlock,
} from '@sanity/types'
import {pick} from 'lodash'
import {type Element, Transforms} from 'slate'

import {type PortableTextMemberSchemaTypes, type PortableTextSlateEditor} from '../../types/editor'
import {debugWithName} from '../../utils/debug'

const debug = debugWithName('plugin:withSchemaTypes')

const allowedTextBlockPropertyNames = [
'_key',
'_type',
'children',
'level',
'listItem',
'markDefs',
'style',
]

/**
* This plugin makes sure that schema types are recognized properly by Slate as blocks, voids, inlines
*
Expand All @@ -24,6 +36,8 @@ export function createWithSchemaTypes({
keyGenerator: () => string
}) {
return function withSchemaTypes(editor: PortableTextSlateEditor): PortableTextSlateEditor {
const {apply, normalizeNode} = editor

editor.isTextBlock = (value: unknown): value is PortableTextTextBlock => {
return isPortableTextTextBlock(value) && value._type === schemaTypes.block.name
}
Expand All @@ -49,8 +63,17 @@ export function createWithSchemaTypes({
)
}

// Make sure split blocks aren't carrying over any arbitrary props from the original block.
editor.apply = (operation) => {
if (operation.type === 'split_node') {
if (operation.properties._type === schemaTypes.block.name) {
operation.properties = pick(operation.properties, allowedTextBlockPropertyNames)
}
}
apply(operation)
}

// Extend Slate's default normalization to add `_type: 'span'` to texts if they are inserted without
const {normalizeNode} = editor
editor.normalizeNode = (entry) => {
const [node, path] = entry
if (node._type === undefined && path.length === 2) {
Expand Down

0 comments on commit 4a5ff45

Please sign in to comment.