Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
[Android] Edit link with text + url
Browse files Browse the repository at this point in the history
  • Loading branch information
aringenbach committed Oct 12, 2023
1 parent 7c14c8a commit fd62f2d
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ fun LinkDialog(
onRemoveLink: () -> Unit,
onSetLink: (url: String) -> Unit,
onInsertLink: (url: String, text: String) -> Unit,
onEditLink: (url: String, text: String) -> Unit,
onDismissRequest: () -> Unit,
) {
val currentUrl = (linkAction as? LinkAction.SetLink)?.currentUrl
val currentUrl = (linkAction as? LinkAction.SetLink)?.currentUrl ?: (linkAction as? LinkAction.EditLink)?.currentUrl
val currentText = (linkAction as? LinkAction.EditLink)?.currentText

var newText by remember { mutableStateOf("") }
var newText by remember { mutableStateOf(currentText ?: "") }
var newLink by remember { mutableStateOf(currentUrl ?: "") }

Dialog(onDismissRequest = onDismissRequest) {
Expand All @@ -45,7 +47,7 @@ fun LinkDialog(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
if (linkAction is LinkAction.InsertLink) {
if (linkAction is LinkAction.InsertLink || linkAction is LinkAction.EditLink) {
OutlinedTextField(
value = newText, onValueChange = { newText = it }, placeholder = {
Text(text = stringResource(R.string.link_text))
Expand All @@ -71,6 +73,7 @@ fun LinkDialog(
when (linkAction) {
LinkAction.InsertLink -> onInsertLink(newLink, newText)
is LinkAction.SetLink -> onSetLink(newLink)
is LinkAction.EditLink -> onEditLink(newLink, newText)
}
onDismissRequest()
}) {
Expand All @@ -80,6 +83,7 @@ fun LinkDialog(
when (linkAction) {
LinkAction.InsertLink -> R.string.link_insert
is LinkAction.SetLink -> R.string.link_set
is LinkAction.EditLink -> R.string.link_edit
}
)
)
Expand All @@ -98,6 +102,7 @@ fun PreviewSetLinkDialog() {
onRemoveLink = {},
onSetLink = {},
onInsertLink = { _, _ -> },
onEditLink = { _, _ -> },
onDismissRequest = {}
)
}
Expand All @@ -110,6 +115,7 @@ fun PreviewInsertLinkDialog() {
onRemoveLink = {},
onSetLink = {},
onInsertLink = { _, _ -> },
onEditLink = { _, _ -> },
onDismissRequest = {}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class MainActivity : ComponentActivity() {
)
}
},
onEditLink = { url, text ->
coroutineScope.launch {
state.editLink(
url, text
)
}
},
onDismissRequest = { linkDialogAction = null })
}
Surface(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
<string name="link_insert">Insert link</string>
<string name="link_text">Text</string>
<string name="link_url">Link</string>
<string name="link_edit">Edit link</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ class MainActivity : AppCompatActivity() {

dialogBinding.link.performClick()
}
override fun openEditLinkDialog(
currentText: String?,
currentLink: String?,
callback: (text: String, url: String) -> Unit
) {
val dialogBinding = DialogSetLinkBinding.inflate(LayoutInflater.from(context))
val title = R.string.edit_link
dialogBinding.link.setText(currentLink)
dialogBinding.text.setText(currentText)
AlertDialog.Builder(context)
.setTitle(title)
.setView(dialogBinding.root)
.setPositiveButton(android.R.string.ok) { _, _ ->
callback(dialogBinding.text.text.toString(), dialogBinding.link.text.toString())
}
.setNegativeButton(android.R.string.cancel, null)
.show()

dialogBinding.link.performClick()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class RichTextEditor : LinearLayout {
onSetLinkListener?.openSetLinkDialog(linkAction.currentUrl) { url ->
richTextEditText.setLink(url)
}
is LinkAction.EditLink ->
onSetLinkListener?.openEditLinkDialog(linkAction.currentText, linkAction.currentUrl) { text, url ->
richTextEditText.editLink(url = url, text = text)
}
}
}
undoButton.setOnClickListener {
Expand Down Expand Up @@ -199,4 +203,5 @@ class RichTextEditor : LinearLayout {
interface OnSetLinkListener {
fun openSetLinkDialog(currentLink: String?, callback: (url: String?) -> Unit)
fun openInsertLinkDialog(callback: (text: String, url: String) -> Unit)
fun openEditLinkDialog(currentText: String?, currentLink: String?, callback: (text: String, url: String) -> Unit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ private fun RealEditor(
is ViewAction.SetLink -> setLink(it.url)
is ViewAction.RemoveLink -> removeLink()
is ViewAction.InsertLink -> insertLink(it.url, it.text)
is ViewAction.EditLink -> editLink(it.url, it.text)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ class RichTextEditorState(
_viewActions.emit(ViewAction.InsertLink(url, text))
}

/**
* Edit a link with new URL and text.
*
* @param url The link URL to set
* @param text The new text to insert
*/
suspend fun editLink(url: String, text: String) {
_viewActions.emit(ViewAction.EditLink(url, text))
}

/**
* The content of the editor as HTML formatted for sending as a message.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal class FakeViewActionCollector(
ViewAction.RequestFocus -> requestFocus()
is ViewAction.SetHtml -> setHtml(value.html)
is ViewAction.SetLink -> setLink(value.url)
is ViewAction.EditLink -> editLink(value.url, value.text)
ViewAction.ToggleCodeBlock -> toggleCodeBlock()
is ViewAction.ToggleInlineFormat -> toggleInlineFormat(value.inlineFormat)
is ViewAction.ToggleList -> toggleList(value.ordered)
Expand Down Expand Up @@ -107,6 +108,10 @@ internal class FakeViewActionCollector(
state.linkAction = url?.let { LinkAction.SetLink(it) } ?: LinkAction.InsertLink
}

private fun editLink(url: String?, text: String?) {
state.linkAction = LinkAction.EditLink(currentText = text, currentUrl = url)
}

private fun removeLink() {
state.linkAction = LinkAction.InsertLink
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ internal sealed class ViewAction {
data class SetLink(val url: String?): ViewAction()
data object RemoveLink: ViewAction()
data class InsertLink(val url: String, val text: String): ViewAction()
data class EditLink(val url: String, val text: String): ViewAction()
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,21 @@ class EditorEditText : AppCompatEditText {
setSelectionFromComposerUpdate(result.selection.last)
}

/**
* Edit text and url for selected link. This method does nothing if there is no link in selection.
*
* @param url The updated link URL
* @param text The updated link text
*/
fun editLink(url: String, text: String) {
val result = viewModel.processInput(
EditorInputAction.EditLink(url, text)
) ?: return

setTextFromComposerUpdate(result.text)
setSelectionFromComposerUpdate(result.selection.last)
}

/**
* Remove a link for the current selection. Convenience for setLink(null).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import uniffi.wysiwyg_composer.LinkAction as InternalLinkAction

internal fun InternalLinkAction.toApiModel(): LinkAction? =
when (this) {
is InternalLinkAction.Edit -> LinkAction.SetLink(currentUrl = url)
is InternalLinkAction.Edit -> LinkAction.EditLink(currentUrl = url, currentText = text)
is InternalLinkAction.Create -> LinkAction.SetLink(currentUrl = null)
is InternalLinkAction.CreateWithText -> LinkAction.InsertLink
is InternalLinkAction.Disabled -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ internal sealed interface EditorInputAction {
*/
data class SetLink(val url: String): EditorInputAction

/**
* Edit a link to the [url] and [text] in the current selection.
*/
data class EditLink(val url: String, val text: String): EditorInputAction

/**
* Remove link on the current selection.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ internal class EditorViewModel(
attributes = emptyList()
)

is EditorInputAction.EditLink -> composer?.editLinkWithText(
url = action.url,
text = action.text,
attributes = emptyList()
)

is EditorInputAction.RemoveLink -> composer?.removeLinks()
is EditorInputAction.SetLinkWithText -> composer?.setLinkWithText(
action.link,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ sealed class LinkAction {
* Add or change the link url for the current selection, without supplying text.
*/
data class SetLink(val currentUrl: String?) : LinkAction()

/**
* Change the link url and text for the current selection.
*/
data class EditLink(val currentUrl: String?, val currentText: String?) : LinkAction()
}

0 comments on commit fd62f2d

Please sign in to comment.