Skip to content

Commit

Permalink
fix(editor): clipboard content is sometimes pasted as `ClipData.Item …
Browse files Browse the repository at this point in the history
…{ ... }`
  • Loading branch information
Rosemoe committed Dec 5, 2024
1 parent 72d8b57 commit 6e04493
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@
package io.github.rosemoe.sora.util;

import android.content.ClipData;
import android.content.Context;

import androidx.annotation.Nullable;

public class ClipDataUtils {

public static String clipDataToString(@Nullable ClipData clipData) {
return clipDataToString(null, clipData);
}

public static String clipDataToString(@Nullable Context context, @Nullable ClipData clipData) {
if (clipData == null) {
return "";
}
Expand All @@ -39,11 +44,9 @@ public static String clipDataToString(@Nullable ClipData clipData) {
sb.append('\n');
}
var item = clipData.getItemAt(i);
var text = item.getText();
if (text == null) {
text = item.toString();
}
sb.append(text);
var text = context != null ? item.coerceToText(context) : item.getText();
if (text != null)
sb.append(text);
}
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3512,7 +3512,7 @@ public void pasteText() {
if (!clipboardManager.hasPrimaryClip() || (clip = clipboardManager.getPrimaryClip()) == null) {
return;
}
pasteText(ClipDataUtils.clipDataToString(clip));
pasteText(ClipDataUtils.clipDataToString(getContext(), clip));
} catch (Exception e) {
Log.w(LOG_TAG, "Error pasting text to editor", e);
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -4736,7 +4736,7 @@ public boolean onDragEvent(DragEvent event) {
}
touchHandler.draggingSelection = null;
setSelection(targetPos.line, targetPos.column);
pasteText(ClipDataUtils.clipDataToString(event.getClipData()));
pasteText(ClipDataUtils.clipDataToString(getContext(), event.getClipData()));
requestFocus();
postInvalidate();
// Call super for notifying listeners
Expand Down

0 comments on commit 6e04493

Please sign in to comment.