Skip to content

Commit

Permalink
fix(text editor): preserve whitespace and selection boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
john-traas committed Dec 9, 2024
1 parent dba8484 commit 95a5ee5
Showing 1 changed file with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ export const isExternalLink = (url: string): boolean => {
return !url.startsWith(window.location.origin);
};

const preserveSelection = (
state: EditorState,
tr: Transaction,
): Transaction => {
const { $from, $to } = state.selection;
const newSelection = TextSelection.create(tr.doc, $from.pos, $to.pos);

return tr.setSelection(newSelection);
};

/**
* Toggles or wraps a node type based on the selection and parameters.
* - Toggles to paragraph if the selection is of the specified type.
Expand Down Expand Up @@ -184,14 +194,15 @@ const toggleNodeType = (
$from.sameParent($from.doc.resolve($to.pos))
) {
if ($from.parent.type === nodeType) {
let tr = state.tr
.setBlockType($from.pos, $to.pos, paragraphType)
.setMeta('preserveWhitespace', true);
tr.replaceSelectionWith(schema.text($from.parent.textContent));

tr = preserveSelection(state, tr);

if (dispatch) {
dispatch(
state.tr.setBlockType(
$from.pos,
$to.pos,
paragraphType,
),
);
dispatch(tr);
}

return true;
Expand All @@ -203,7 +214,22 @@ const toggleNodeType = (
if (shouldWrap) {
return wrapIn(nodeType, attrs)(state, dispatch);
} else {
return setBlockType(nodeType, attrs)(state, dispatch);
let tr = state.tr
.setBlockType($from.pos, $to.pos, nodeType, attrs)
.setMeta('preserveWhitespace', true);
if (nodeType === paragraphType) {
tr.replaceSelectionWith(
schema.text($from.parent.textContent),
);
}

tr = preserveSelection(state, tr);

if (dispatch) {
dispatch(tr);
}

return true;
}
}
}
Expand Down

0 comments on commit 95a5ee5

Please sign in to comment.