Skip to content

A platform for community discussion. Free, open, simple.

UX: add hard break with double space+enter on rich editor (#34157)

Renato Atilio909d19ddb8bd
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/core/keymap.js b/app/assets/javascripts/discourse/app/static/prosemirror/core/keymap.js
index aa5b13958c1..2d43997327f 100644
--- a/app/assets/javascripts/discourse/app/static/prosemirror/core/keymap.js
+++ b/app/assets/javascripts/discourse/app/static/prosemirror/core/keymap.js
@@ -8,7 +8,7 @@ import {
import { redo, undo } from "prosemirror-history";
import { undoInputRule } from "prosemirror-inputrules";
import { splitListItem } from "prosemirror-schema-list";
-import { atBlockStart } from "../lib/plugin-utils";
+import { atBlockStart, inNode } from "../lib/plugin-utils";
const BACKSPACE_UNSET_NODES = ["heading", "code_block"];
@@ -69,7 +69,31 @@ export function buildKeymap(
return true;
});
- keys["Enter"] = splitListItem(schema.nodes.list_item);
+ const doubleSpaceHardBreak = (state, dispatch) => {
+ const { $from } = state.selection;
+ if ($from.parent.type.spec.code || inNode(state, schema.nodes.code_block)) {
+ return false;
+ }
+
+ if ($from.nodeBefore?.text.endsWith(" ")) {
+ if (dispatch) {
+ const tr = state.tr.replaceRangeWith(
+ $from.pos - 2,
+ $from.pos,
+ schema.nodes.hard_break.create()
+ );
+
+ dispatch(tr.scrollIntoView());
+ }
+ return true;
+ }
+ return false;
+ };
+
+ keys["Enter"] = chainCommands(
+ doubleSpaceHardBreak,
+ splitListItem(schema.nodes.list_item)
+ );
keys["Mod-Shift-_"] = (state, dispatch) => {
dispatch?.(
diff --git a/spec/system/composer/prosemirror_editor_spec.rb b/spec/system/composer/prosemirror_editor_spec.rb
index 772ea1d1d66..2a416fa7e41 100644
--- a/spec/system/composer/prosemirror_editor_spec.rb
+++ b/spec/system/composer/prosemirror_editor_spec.rb
@@ -600,6 +600,16 @@ describe "Composer - ProseMirror editor", type: :system do
expect(rich).to have_css("hr")
end
+ it "creates hard break when pressing Enter after double space at end of line" do
+ open_composer
+ composer.type_content("Line with double space ")
+ composer.send_keys(:enter)
+ composer.type_content("Next line")
+
+ composer.toggle_rich_editor
+ expect(composer).to have_value("Line with double space\nNext line")
+ end
+
it "supports Backspace to reset a heading" do
open_composer
composer.type_content("# With text")