From 1b1de59c0bd623ebfa2df80165801e27ae86cea4 Mon Sep 17 00:00:00 2001 From: Matheus Richard Date: Thu, 2 Jul 2026 15:57:43 -0300 Subject: [PATCH] Fix plain text paste being ignored without markdown Pasting plain, non-URL text did nothing whenever `supportsMarkdown` was false, which happens with `markdown: false` or in a plain-text-only editor (`richText: false`). `#pastePlainTextOrURL` resolves the clipboard text through the asynchronous `item.getAsString` callback. The non-markdown branch ignored that resolved text and handed the whole `clipboardData` to `$insertDataTransferForRichText` instead. By the time the callback ran, the paste event was over, so that DataTransfer read back empty and nothing got inserted. The URL and Markdown branches worked because they already used the resolved text string. This commit inserts the resolved text in the non-markdown branch too, rather than re-reading the stale clipboard, and drops the now-unused rich-text path. --- src/editor/clipboard.js | 10 +------- test/system/plain_text_paste_test.rb | 38 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 test/system/plain_text_paste_test.rb diff --git a/src/editor/clipboard.js b/src/editor/clipboard.js index 0c2268f10..825c1aa6f 100644 --- a/src/editor/clipboard.js +++ b/src/editor/clipboard.js @@ -4,7 +4,6 @@ import { nextFrame } from "../helpers/timing_helper" import { addBlockSpacing, dispatch, parseHtml } from "../helpers/html_helper" import { $isCodeNode } from "@lexical/code" import { $createTextNode, $getSelection, $isParagraphNode, $isRangeSelection, $onUpdate, COMMAND_PRIORITY_NORMAL, PASTE_COMMAND, PASTE_TAG, SELECTION_INSERT_CLIPBOARD_NODES_COMMAND } from "lexical" -import { $insertDataTransferForRichText } from "@lexical/clipboard" import { $createLinkNode, $isLinkNode, $toggleLink } from "@lexical/link" import { ListenerBin } from "../helpers/listener_helper" import NodeInserter from "./contents/node_inserter" @@ -148,7 +147,7 @@ export default class Clipboard { } else if (this.editorElement.supportsMarkdown) { this.#pasteMarkdown(text) } else { - this.#pasteRichText(clipboardData) + this.contents.insertText(text, { tag: PASTE_TAG }) } }) } @@ -226,13 +225,6 @@ export default class Clipboard { && Array.from(paragraph.childNodes).every((node) => node.nodeType === Node.TEXT_NODE || node.nodeName === "BR") } - #pasteRichText(clipboardData) { - this.editor.update(() => { - const selection = $getSelection() - $insertDataTransferForRichText(clipboardData, selection, this.editor) - }, { tag: PASTE_TAG }) - } - #handlePastedFiles(clipboardData) { if (!this.editorElement.supportsAttachments) return false diff --git a/test/system/plain_text_paste_test.rb b/test/system/plain_text_paste_test.rb new file mode 100644 index 000000000..bc4c6e417 --- /dev/null +++ b/test/system/plain_text_paste_test.rb @@ -0,0 +1,38 @@ +require "application_system_test_case" + +class PlainTextPasteTest < ApplicationSystemTestCase + PASTED_TEXT = "Plain text pasted from the clipboard" + MODIFIER = RUBY_PLATFORM.include?("darwin") ? :command : :control + + test "pastes plain text when markdown is disabled" do + visit new_post_path(markdown_disabled: 1) + paste_from_clipboard PASTED_TEXT + + find_editor.within_contents do + assert_text PASTED_TEXT + end + end + + test "pastes plain text in a plain-text-only editor" do + visit new_post_path(rich_text_disabled: 1) + paste_from_clipboard PASTED_TEXT + + find_editor.within_contents do + assert_text PASTED_TEXT + end + end + + private + def paste_from_clipboard(text) + copy_to_clipboard text + + find_editor.click + find_editor.content_element.send_keys [ MODIFIER, "v" ] + end + + def copy_to_clipboard(text) + title = find_field("Post title") + title.set text + title.send_keys [ MODIFIER, "a" ], [ MODIFIER, "c" ] + end +end