A platform for community discussion. Free, open, simple.
UX: backspace rich editor keymap improvements (#32234)
Renato Atilio5836a9a664e2
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/core/keymap.js b/app/assets/javascripts/discourse/app/static/prosemirror/core/keymap.jsindex 8c0cc81643e..7ef4a7cf1e5 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/core/keymap.js+++ b/app/assets/javascripts/discourse/app/static/prosemirror/core/keymap.js@@ -1,12 +1,16 @@import {chainCommands,exitCode,+ joinTextblockBackward,selectParentNode,setBlockType,} from "prosemirror-commands";import { redo, undo } from "prosemirror-history";import { undoInputRule } from "prosemirror-inputrules";import { splitListItem } from "prosemirror-schema-list";+import { atBlockStart } from "../lib/plugin-utils";++const BACKSPACE_UNSET_NODES = ["heading", "code_block"];const isMac =typeof navigator !== "undefined"@@ -26,10 +30,25 @@ export function buildKeymap(keys["Mod-z"] = undo;keys["Shift-Mod-z"] = redo;- keys["Backspace"] = undoInputRule;++ const backspaceUnset = (state, dispatch, view) => {+ const $pos = atBlockStart(state, view);+ if (BACKSPACE_UNSET_NODES.includes($pos?.parent.type.name)) {+ return setBlockType(schema.nodes.paragraph)(state, dispatch, view);+ }+ return false;+ };++ keys["Backspace"] = chainCommands(+ undoInputRule,+ backspaceUnset,+ joinTextblockBackward+ );+if (!isMac) {keys["Mod-y"] = redo;}+keys["Escape"] = selectParentNode;// The above keys are always includeddiff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/code-block.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/code-block.jsindex d120271d7a8..9ece156970f 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/code-block.js+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/code-block.js@@ -78,59 +78,21 @@ class CodeBlockWithLangSelectorNodeView {/** @type {RichEditorExtension} */const extension = {nodeViews: { code_block: CodeBlockWithLangSelectorNodeView },- plugins({ pmState: { Plugin }, getContext }) {- return [- async () =>- highlightPlugin(- (hljs = await ensureHighlightJs(- getContext().session.highlightJsPath- )),- ["code_block", "html_block"],-- // NOTE: If the language has not been set with the code block, we default to plain- // text rather than autodetecting. This is to work around an infinite loop issue- // in prosemirror-highlightjs when autodetecting which hangs the browser sometimes- // for > 10 seconds, for example:- //- // https://github.com/b-kelly/prosemirror-highlightjs/issues/21- //- // We can remove this if we find some other workaround.- (node) => node.attrs.params || "text"- ),- new Plugin({- props: {- // Handles removal of the code_block when it's at the start of the document- handleKeyDown(view, event) {- if (- event.key === "Backspace" &&- view.state.selection.$from.parent.type ===- view.state.schema.nodes.code_block &&- view.state.selection.$from.start() === 1 &&- view.state.selection.$from.parentOffset === 0- ) {- const { tr } = view.state;-- const codeBlock = view.state.selection.$from.parent;- const paragraph = view.state.schema.nodes.paragraph.create(- null,- codeBlock.content- );- tr.replaceWith(- view.state.selection.$from.before(),- view.state.selection.$from.after(),- paragraph- );- tr.setSelection(- new view.state.selection.constructor(tr.doc.resolve(1))- );-- view.dispatch(tr);- return true;- }- },- },- }),- ];+ async plugins({ getContext }) {+ return highlightPlugin(+ (hljs = await ensureHighlightJs(getContext().session.highlightJsPath)),+ ["code_block", "html_block"],++ // NOTE: If the language has not been set with the code block, we default to plain+ // text rather than autodetecting. This is to work around an infinite loop issue+ // in prosemirror-highlightjs when autodetecting which hangs the browser sometimes+ // for > 10 seconds, for example:+ //+ // https://github.com/b-kelly/prosemirror-highlightjs/issues/21+ //+ // We can remove this if we find some other workaround.+ (node) => node.attrs.params || "text"+ );},};diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/lib/plugin-utils.js b/app/assets/javascripts/discourse/app/static/prosemirror/lib/plugin-utils.jsindex e0e0093b76d..93ea8338511 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/lib/plugin-utils.js+++ b/app/assets/javascripts/discourse/app/static/prosemirror/lib/plugin-utils.js@@ -74,3 +74,15 @@ export function getChangedRanges(tr) {return changes;}++// from https://github.com/ProseMirror/prosemirror-commands/blob/master/src/commands.ts+export function atBlockStart(state, view) {+ let { $cursor } = state.selection;+ if (+ !$cursor ||+ (view ? !view.endOfTextblock("backward", state) : $cursor.parentOffset > 0)+ ) {+ return null;+ }+ return $cursor;+}diff --git a/spec/system/composer/prosemirror_editor_spec.rb b/spec/system/composer/prosemirror_editor_spec.rbindex 38b47882d27..a1c3969aa30 100644--- a/spec/system/composer/prosemirror_editor_spec.rb+++ b/spec/system/composer/prosemirror_editor_spec.rb@@ -441,6 +441,39 @@ describe "Composer - ProseMirror editor", type: :system doexpect(rich).to have_css("hr")end++ it "supports Backspace to reset a heading" do+ open_composer_and_toggle_rich_editor+ composer.type_content("# With text")++ expect(rich).to have_css("h1", text: "With text")++ composer.send_keys(:home)+ composer.send_keys(:backspace)++ expect(rich).to have_css("p", text: "With text")+ end++ it "supports Backspace to reset a code_block" do+ open_composer_and_toggle_rich_editor+ composer.type_content("```code block")+ composer.send_keys(:home)+ composer.send_keys(%i[backspace])++ expect(rich).to have_css("p", text: "code block")+ end++ it "doesn't add a new list item when backspacing from below a list" do+ open_composer_and_toggle_rich_editor+ composer.type_content("1. Item 1\nItem 2")+ composer.send_keys(:down)+ composer.type_content("Item 3")+ composer.send_keys(:home)+ composer.send_keys(:backspace)++ expect(rich).to have_css("ol li", text: "Item 1")+ expect(rich).to have_css("ol li", text: "Item 2Item 3")+ endenddescribe "pasting content" do