GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(mobile): Keep a copied message's image when Android pastes plain … · patroza/t3code@21aebbf

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/mobile/modules/t3-composer-editor/android/src
    • main/java/expo/modules/t3composereditor
      • T3ComposerEditorModule.kt
      • T3ComposerEditorView.kt
    • test/java/expo/modules/t3composereditor
      • ComposerPasteTest.kt
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorModule.kt‎

Copy file name to clipboardExpand all lines: apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorModule.kt
+19-4Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ internal object T3ComposerClipboard {
1313
fun hasThreadLink(text: String): Boolean =
1414
text.contains("t3-thread://v2/") || text.contains("https://app.t3.codes/threads/")
1515

16+
// Clipboard bridges can echo our text back without its HTML context metadata.
17+
private var copiedContext: Pair<String, String>? = null
18+
19+
@Synchronized
1620
fun write(context: Context, text: String, fragment: String) {
1721
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
1822
val payload = try {
@@ -22,6 +26,7 @@ internal object T3ComposerClipboard {
2226
}
2327
val records = payload?.optJSONArray("records")
2428
if (payload == null || records == null) {
29+
copiedContext = null
2530
clipboard.setPrimaryClip(ClipData.newPlainText("T3 Code", text))
2631
return
2732
}
@@ -35,8 +40,10 @@ internal object T3ComposerClipboard {
3540
}
3641
)
3742
payload.put("records", JSONArray(selected))
38-
val encoded = java.net.URLEncoder.encode(payload.toString(), "UTF-8").replace("+", "%20")
43+
val serialized = payload.toString()
44+
val encoded = java.net.URLEncoder.encode(serialized, "UTF-8").replace("+", "%20")
3945
val escaped = text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
46+
copiedContext = if (selected.isEmpty()) null else text to serialized
4047
clipboard.setPrimaryClip(
4148
if (selected.isEmpty()) {
4249
ClipData.newPlainText(
@@ -53,14 +60,22 @@ internal object T3ComposerClipboard {
5360
)
5461
}
5562

63+
@Synchronized
5664
fun read(context: Context): Map<String, String> {
5765
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
5866
val clip = clipboard.primaryClip
5967
val item = if (clip != null && clip.itemCount > 0) clip.getItemAt(0) else null
68+
val text = item?.text?.toString() ?: ""
69+
val html = item?.htmlText ?: ""
70+
val copied = copiedContext?.takeIf {
71+
clip?.itemCount == 1 && item?.uri == null && it.first == text
72+
}
73+
if (copied == null) copiedContext = null
6074
return mapOf(
61-
"text" to (item?.text?.toString() ?: ""),
62-
"html" to (item?.htmlText ?: ""),
63-
"fragment" to ""
75+
"text" to text,
76+
"html" to html,
77+
"fragment" to
78+
if (!html.contains("data-t3-context-fragment=")) copied?.second.orEmpty() else ""
6479
)
6580
}
6681
}
Collapse file

‎apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt‎

Copy file name to clipboardExpand all lines: apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,8 @@ internal class SelectionAwareEditText(
906906
private fun pasteContextOrImages(): Boolean {
907907
val payload = T3ComposerClipboard.read(context)
908908
return when {
909-
payload["html"]?.contains("data-t3-context-fragment=") == true -> pasteContext(payload)
909+
!payload["fragment"].isNullOrEmpty() ||
910+
payload["html"]?.contains("data-t3-context-fragment=") == true -> pasteContext(payload)
910911
pasteImagesOrInterceptedText() -> true
911912
isSingleItemClip() && T3ComposerClipboard.hasThreadLink(payload["text"].orEmpty()) ->
912913
pasteContext(payload)
Collapse file

‎apps/mobile/modules/t3-composer-editor/android/src/test/java/expo/modules/t3composereditor/ComposerPasteTest.kt‎

Copy file name to clipboardExpand all lines: apps/mobile/modules/t3-composer-editor/android/src/test/java/expo/modules/t3composereditor/ComposerPasteTest.kt
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import android.view.View
1515
import android.view.KeyEvent
1616
import android.view.inputmethod.BaseInputConnection
1717
import android.view.inputmethod.EditorInfo
18+
import org.json.JSONObject
1819
import org.junit.Assert.assertEquals
1920
import org.junit.Assert.assertFalse
2021
import org.junit.Assert.assertNull
@@ -50,6 +51,46 @@ class ComposerPasteTest {
5051
)
5152
)
5253

54+
@Test
55+
@Config(sdk = [33, 36])
56+
fun sentImageCopySurvivesAClipboardBridgeDroppingHtml() {
57+
val id = "file_sent-image"
58+
val copiedText = "Review this image\n ![android-smoke.png](t3-context://v1/image/$id)"
59+
val fragment = """
60+
{
61+
"version": 1,
62+
"source": {"environmentId": "source", "messageId": "sent-message"},
63+
"records": [{
64+
"version": 1, "contextId": "$id", "label": "android-smoke.png", "kind": "image",
65+
"attachmentId": "stored-image", "name": "android-smoke.png",
66+
"mimeType": "image/png", "sizeBytes": 53249
67+
}]
68+
}
69+
""".trimIndent()
70+
T3ComposerClipboard.write(context, copiedText, fragment)
71+
clipboard.setPrimaryClip(ClipData.newPlainText("clipboard bridge", copiedText))
72+
editor.setText("")
73+
editor.setSelection(0)
74+
var pasted: Map<String, String>? = null
75+
editor.pasteContextListener = { pasted = it }
76+
77+
assertTrue(editor.onTextContextMenuItem(android.R.id.paste))
78+
assertEquals(copiedText, pasted?.get("text"))
79+
val recovered = JSONObject(requireNotNull(pasted?.get("fragment")))
80+
assertEquals("source", recovered.getJSONObject("source").getString("environmentId"))
81+
assertEquals(id, recovered.getJSONArray("records").getJSONObject(0).getString("contextId"))
82+
assertEquals("", editor.text.toString())
83+
84+
editor.pasteContextListener = { error("Paste as Text must stay plain") }
85+
assertTrue(pasteAsText())
86+
assertEquals(copiedText, editor.text.toString())
87+
88+
clipboard.setPrimaryClip(ClipData.newPlainText("other app", "Different text"))
89+
assertEquals("", T3ComposerClipboard.read(context)["fragment"])
90+
clipboard.setPrimaryClip(ClipData.newPlainText("other app", copiedText))
91+
assertEquals("", T3ComposerClipboard.read(context)["fragment"])
92+
}
93+
5394
@Test
5495
fun shortcutKeepsLargeTextInlineAndUndoable() {
5596
val pasted = "x".repeat(32 * 1024)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.