Skip to content

Add Rich Paste Support#642

Closed
tfkci wants to merge 4 commits intoMohamedRejeb:mainfrom
tfkci:feature/rich-paste-support
Closed

Add Rich Paste Support#642
tfkci wants to merge 4 commits intoMohamedRejeb:mainfrom
tfkci:feature/rich-paste-support

Conversation

@tfkci
Copy link
Copy Markdown

@tfkci tfkci commented Mar 24, 2026

Preserve spans when user externally copy text and paste to editor

tfkci and others added 4 commits March 24, 2026 23:50
On Android, intercept paste actions before BasicTextField's plain-text
handler and route clipboard content through the existing HTML pipeline:

• ClipData.Item.htmlText  → used directly (Chrome, Docs, Gmail, …)
• ClipData.Item.text is Spanned → converted via android.text.Html.toHtml()
  (covers AppCompatEditText copies carrying StyleSpan, UnderlineSpan,
   URLSpan, ForegroundColorSpan, StrikethroughSpan, etc.)
• Plain text (no spans) → returns false, default paste flow unchanged

Two intercept points are wired up:
  - SpannedPasteTextToolbar wraps LocalTextToolbar to catch the
    context-menu "Paste" action.
  - onPreviewKeyEvent intercepts Ctrl+V / Cmd+V on hardware keyboards.

All non-Android targets receive a no-op actual that returns false.
The skipSigning Gradle property is added to module.publication.gradle.kts
to allow unsigned publishToMavenLocal builds during local development.

Also merges upstream PR MohamedRejeb#638 (fix StringIndexOutOfBoundsException in
MarkdownUtils for nested indented lists).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
The previous implementation only covered two paste paths:
  • Long-press → context menu "Paste"  (TextToolbar wrapper)
  • Hardware keyboard Ctrl/Cmd+V        (onPreviewKeyEvent)

Soft-keyboard IME paste (the "Paste" button in the IME toolbar or
suggestion strip) goes through InputConnection.performContextMenuAction,
which calls ClipboardManager.getText() directly — bypassing both of the
above intercept points, causing plain-text to be inserted.

Fix: RichTextClipboardManager.getText() now calls
SpannedPasteHandler.readHtml() (new method).  When HTML or Spanned
content is found in the platform ClipData, it stores the HTML in
RichTextState.pendingHtmlPaste and returns the plain-text fallback so
the IME gets a valid cursor position.

RichTextState.onTextFieldValueChange() consumes pendingHtmlPaste at its
very top — before handleAddingCharacters runs — and replaces the
would-be plain-text insertion with a proper insertHtmlAfterSelection()
call.  This path is also taken for TextToolbar / Ctrl+V pastes, but
those are intercepted earlier (tryPasteSpanned) and never reach
onTextFieldValueChange with pendingHtmlPaste set.

All paste paths now preserve formatting:
  • Soft keyboard IME paste  ← new
  • Context menu paste        ← existing, still works
  • Hardware Ctrl/Cmd+V       ← existing, still works
  • Plain text (no spans)     ← falls through unchanged

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…Pasteboard

- Add getHtmlIfMatch(addedText) to SpannedPasteHandler interface so
  onTextFieldValueChange can probe the clipboard when the IME paste
  button bypasses RichTextClipboardManager.getText() entirely
- Implement getHtmlIfMatch in AndroidSpannedPasteHandler: compares the
  added text against clipboard plain text; returns HTML if they match
- Replace iOS no-op with IosSpannedPasteHandler backed by
  UIPasteboard.generalPasteboard, covering all three paste paths:
  context menu (tryPasteSpanned), hardware keyboard (tryPasteSpanned),
  and soft keyboard / IME (getHtmlIfMatch)
- Wire spannedPasteHandler into RichTextState via SideEffect so
  onTextFieldValueChange has access to the platform handler
- Add PasteDebugLog expect/actual for all platforms (RichPaste tag)

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
iOS clipboard HTML carries the source page's background-color (usually
white or transparent) on every span.  Strip white/#fff/rgb(255,255,255)/
rgba(255,255,255,*) and transparent values so they don't render as white
boxes behind text in the editor.  Intentional highlight colors are left
intact.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
MohamedRejeb added a commit that referenced this pull request Apr 17, 2026
Issue #626 reported that copying styled text from the editor and
pasting into a plain-text target (e.g. the Android notes app)
collapsed everything onto a single line. The issue was filed on
2025-10-21, before rich clipboard support landed in 3770676
(2026-04-11, PR #642).

Before #642, the library relied on Compose's default copy, which
reads from the transformed annotated string — and since the
annotated string replaces '\n' with ' ' for rendering, the plain
text payload lost paragraph breaks. #642 overrode setClipEntry on
all platforms to build the payload from toText(copySelection) and
toHtml(copySelection), both of which preserve '\n' and block tags.

These tests lock in that behavior across seven scenarios:
- copy across <p> paragraphs (plain and styled)
- copy across <br> paragraphs (isFromLineBreak = true)
- copy with mixed inline styles across <br> boundaries
- mid-range copy that crosses a paragraph boundary
- copy across an ordered list
- toHtml(copySelection) preserves block-level tags

Marking #626 as already fixed by #642.
@MohamedRejeb
Copy link
Copy Markdown
Owner

Hello
Thanks for your contribution
This feature is already implemented in #650 so I'm going to close this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants