Skip to content

Commit

Permalink
fix(BaseSchema): fixed definition of an empty string during serializa…
Browse files Browse the repository at this point in the history
…tion (#571)
  • Loading branch information
PMAWorks authored Jan 27, 2025
1 parent 0749cb3 commit 2926d71
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/extensions/base/BaseSchema/BaseSchemaSpecs/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {NodeSpec} from 'prosemirror-model';
import type {Node, NodeSpec} from 'prosemirror-model';

import type {ExtensionAuto} from '../../../../core';
import {nodeTypeFactory} from '../../../../utils/schema';
Expand Down Expand Up @@ -69,7 +69,7 @@ export const BaseSchemaSpecs: ExtensionAuto<BaseSchemaSpecsOptions> = (builder,
An empty line is added only if there is some content in the parent element.
This is necessary in order to prevent an empty document with empty lines
*/
if (opts.preserveEmptyRows && !node.content.size) {
if (opts.preserveEmptyRows && isEmptyString(node)) {
let isParentEmpty = true;

for (let index = 0; index < parent.content.childCount; index++) {
Expand All @@ -92,3 +92,19 @@ export const BaseSchemaSpecs: ExtensionAuto<BaseSchemaSpecsOptions> = (builder,
},
}));
};

const isEmptyString = (node: Node) => {
if (!node.content.size) {
return true;
}

if (
node.childCount === 1 &&
node.child(0).type.name === 'text' &&
node.child(0).text?.trim() === ''
) {
return true;
}

return false;
};

0 comments on commit 2926d71

Please sign in to comment.