Skip to content

Commit

Permalink
Use .format instead of .insert and .delete.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Farese committed Jul 6, 2023
1 parent 0c9ae6f commit 0425c0b
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/core/src/applyToYjs/node/mergeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getStoredPositionsInDeltaAbsolute,
restoreStoredPositionsWithDeltaAbsolute,
} from '../../utils/position';
import { getProperties } from '../../utils/slate';

export function mergeNode(
sharedRoot: Y.XmlText,
Expand All @@ -27,18 +28,26 @@ export function mergeNode(

if (!prev.yTarget || !target.yTarget) {
const { yParent: parent, textRange, slateTarget } = target;
if (!Text.isText(slateTarget)) {
throw new Error('Expected Slate target text node for merge op.');
if (!slateTarget) {
throw new Error('Expected Slate target node for merge op.');
}

const prevSibling = Node.get(slateRoot, Path.previous(op.path));
if (!Text.isText(prevSibling)) {
throw new Error('Path points to Y.Text but not a Slate text node.');
}

parent.delete(textRange.start, textRange.end - textRange.start);
parent.insert(textRange.start, slateTarget.text);
return;
const targetProps = getProperties(slateTarget);
const prevSiblingProps = getProperties(prevSibling);
const unsetProps = Object.keys(targetProps).reduce((acc, key) => {
const prevSiblingHasProp = key in prevSiblingProps;
return prevSiblingHasProp ? acc : { ...acc, [key]: null };
}, {});

return parent.format(textRange.start, textRange.end - textRange.start, {
...unsetProps,
...prevSiblingProps,
});
}

const deltaApplyYOffset = prev.yTarget.length;
Expand Down

0 comments on commit 0425c0b

Please sign in to comment.