Skip to content

Add Rich Paste Support#642

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

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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
…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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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.

1 participant