Skip to content

Add rich content listener to markdown text input #2918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/2917.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed sending rich content from android keyboards on the markdown text input
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ fun TextComposer(
onTyping = onTyping,
onSuggestionReceived = onSuggestionReceived,
richTextEditorStyle = style,
onRichContentSelected = onRichContentSelected,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@

package io.element.android.libraries.textcomposer.components.markdown

import android.content.ClipData
import android.graphics.Color
import android.net.Uri
import android.text.Editable
import android.text.InputType
import android.text.Selection
import android.view.View
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.text.getSpans
import androidx.core.view.ContentInfoCompat
import androidx.core.view.OnReceiveContentListener
import androidx.core.view.ViewCompat
import androidx.core.view.setPadding
import androidx.core.widget.addTextChangedListener
import io.element.android.libraries.designsystem.preview.ElementPreview
Expand All @@ -48,8 +54,33 @@
onTyping: (Boolean) -> Unit,
onSuggestionReceived: (Suggestion?) -> Unit,
richTextEditorStyle: RichTextEditorStyle,
onRichContentSelected: ((Uri) -> Unit)?,
) {
val canUpdateState = !subcomposing

// Copied from io.element.android.wysiwyg.internal.utils.UriContentListener
class ReceiveUriContentListener(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should provide this to the RTE and not the other way around. I'll keep it in mind.

private val onContent: (uri: Uri) -> Unit,
) : OnReceiveContentListener {
override fun onReceiveContent(view: View, payload: ContentInfoCompat): ContentInfoCompat? {
val split = payload.partition { item -> item.uri != null }
val uriContent = split.first
val remaining = split.second

Check warning on line 68 in libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt

View check run for this annotation

Codecov / codecov/patch

libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt#L67-L68

Added lines #L67 - L68 were not covered by tests

if (uriContent != null) {
val clip: ClipData = uriContent.clip

Check warning on line 71 in libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt

View check run for this annotation

Codecov / codecov/patch

libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt#L71

Added line #L71 was not covered by tests
for (i in 0 until clip.itemCount) {
val uri = clip.getItemAt(i).uri

Check warning on line 73 in libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt

View check run for this annotation

Codecov / codecov/patch

libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt#L73

Added line #L73 was not covered by tests
// ... app-specific logic to handle the URI ...
onContent(uri)

Check warning on line 75 in libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt

View check run for this annotation

Codecov / codecov/patch

libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt#L75

Added line #L75 was not covered by tests
}
}
// Return anything that we didn't handle ourselves. This preserves the default platform
// behavior for text and anything else for which we are not implementing custom handling.
return remaining

Check warning on line 80 in libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt

View check run for this annotation

Codecov / codecov/patch

libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt#L80

Added line #L80 was not covered by tests
}
}

AndroidView(
modifier = Modifier
.padding(top = 6.dp, bottom = 6.dp)
Expand Down Expand Up @@ -82,6 +113,13 @@
state.currentMentionSuggestion = editableText.checkSuggestionNeeded()
onSuggestionReceived(state.currentMentionSuggestion)
}
if (onRichContentSelected != null) {
ViewCompat.setOnReceiveContentListener(
this,
arrayOf("image/*"),
ReceiveUriContentListener { onRichContentSelected(it) }
)
}
state.requestFocusAction = { this.requestFocus() }
}
}
Expand Down Expand Up @@ -152,6 +190,7 @@
onTyping = {},
onSuggestionReceived = {},
richTextEditorStyle = style,
onRichContentSelected = {},
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class MarkdownTextInputTest {
onTyping = onTyping,
onSuggestionReceived = onSuggestionReceived,
richTextEditorStyle = style,
onRichContentSelected = null,
)
}
}
Expand Down
Loading