Skip to content

Commit 855f2f5

Browse files
committed
fix(text editor): preserve whitespace when toggling block types
1 parent dba8484 commit 855f2f5

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/components/text-editor/prosemirror-adapter/menu/menu-commands.ts

+20-8
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,13 @@ const toggleNodeType = (
184184
$from.sameParent($from.doc.resolve($to.pos))
185185
) {
186186
if ($from.parent.type === nodeType) {
187+
const tr = state.tr
188+
.setBlockType($from.pos, $to.pos, paragraphType)
189+
.setMeta('preserveWhitespace', true);
190+
tr.replaceSelectionWith(schema.text($from.parent.textContent));
191+
187192
if (dispatch) {
188-
dispatch(
189-
state.tr.setBlockType(
190-
$from.pos,
191-
$to.pos,
192-
paragraphType,
193-
),
194-
);
193+
dispatch(tr);
195194
}
196195

197196
return true;
@@ -203,7 +202,20 @@ const toggleNodeType = (
203202
if (shouldWrap) {
204203
return wrapIn(nodeType, attrs)(state, dispatch);
205204
} else {
206-
return setBlockType(nodeType, attrs)(state, dispatch);
205+
const tr = state.tr
206+
.setBlockType($from.pos, $to.pos, nodeType, attrs)
207+
.setMeta('preserveWhitespace', true);
208+
if (nodeType === paragraphType) {
209+
tr.replaceSelectionWith(
210+
schema.text($from.parent.textContent),
211+
);
212+
}
213+
214+
if (dispatch) {
215+
dispatch(tr);
216+
}
217+
218+
return true;
207219
}
208220
}
209221
}

0 commit comments

Comments
 (0)