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

Commit

Permalink
[Android] Fix missing link action updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnyandrew committed Sep 13, 2023
1 parent c65b914 commit d88b92c
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 27 deletions.
5 changes: 5 additions & 0 deletions bindings/wysiwyg-ffi/src/ffi_composer_update.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use widestring::Utf16String;

use crate::ffi_link_actions::LinkAction;
use crate::ffi_menu_state::MenuState;
use crate::ffi_text_update::TextUpdate;
use crate::MenuAction;
Expand Down Expand Up @@ -28,6 +29,10 @@ impl ComposerUpdate {
pub fn menu_action(&self) -> MenuAction {
MenuAction::from(self.inner.menu_action.clone())
}

pub fn link_action(&self) -> LinkAction {
LinkAction::from(self.inner.link_action.clone())
}
}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions bindings/wysiwyg-ffi/src/ffi_link_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use widestring::Utf16String;

#[derive(uniffi::Enum)]
pub enum LinkAction {
Keep,
CreateWithText,
Create,
Edit { url: String },
Expand All @@ -11,6 +12,7 @@ pub enum LinkAction {
impl From<wysiwyg::LinkAction<Utf16String>> for LinkAction {
fn from(inner: wysiwyg::LinkAction<Utf16String>) -> Self {
match inner {
wysiwyg::LinkAction::Keep => Self::Keep,
wysiwyg::LinkAction::CreateWithText => Self::CreateWithText,
wysiwyg::LinkAction::Create => Self::Create,
wysiwyg::LinkAction::Edit(url) => Self::Edit {
Expand Down
12 changes: 12 additions & 0 deletions bindings/wysiwyg-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ pub struct LinkAction {
pub create: Option<Create>,
pub edit_link: Option<Edit>,
pub disabled: Option<Disabled>,
pub keep: Option<Keep>,
}

impl From<wysiwyg::LinkAction<Utf16String>> for LinkAction {
Expand All @@ -853,12 +854,14 @@ impl From<wysiwyg::LinkAction<Utf16String>> for LinkAction {
create: None,
edit_link: None,
disabled: None,
keep: None,
},
wysiwyg::LinkAction::Create => Self {
create_with_text: None,
create: Some(Create),
edit_link: None,
disabled: None,
keep: None,
},
wysiwyg::LinkAction::Edit(url) => {
let url = url.to_string();
Expand All @@ -867,13 +870,22 @@ impl From<wysiwyg::LinkAction<Utf16String>> for LinkAction {
create: None,
edit_link: Some(Edit { url }),
disabled: None,
keep: None,
}
}
wysiwyg::LinkAction::Disabled => Self {
create_with_text: None,
create: None,
edit_link: None,
disabled: Some(Disabled),
keep: None,
},
wysiwyg::LinkAction::Keep => Self {
create_with_text: None,
create: None,
edit_link: None,
disabled: None,
keep: Some(Keep),
},
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/wysiwyg/src/composer_model/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ where
self.state.end,
menu_state,
self.compute_menu_action(),
self.get_link_action(),
)
}

Expand All @@ -169,6 +170,7 @@ where
self.state.end,
self.compute_menu_state(MenuStateComputeType::KeepIfUnchanged),
self.compute_menu_action(),
self.get_link_action(),
)
}

Expand All @@ -184,6 +186,7 @@ where
self.state.end,
self.compute_menu_state(MenuStateComputeType::AlwaysUpdate),
self.compute_menu_action(),
self.get_link_action(),
)
}

Expand Down
4 changes: 3 additions & 1 deletion crates/wysiwyg/src/composer_model/replace_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ mod test {
use crate::tests::testutils_composer_model::cm;
use crate::tests::testutils_conversion::utf16;
use crate::{
ComposerAction, ComposerUpdate, Location, MenuAction, MenuState,
ComposerAction, ComposerUpdate, LinkAction, Location, MenuAction,
MenuState,
};
use strum::IntoEnumIterator;

Expand All @@ -263,6 +264,7 @@ mod test {
action_states: indent_unindent_redo_disabled()
}),
MenuAction::None,
LinkAction::CreateWithText,
),
);
}
Expand Down
10 changes: 9 additions & 1 deletion crates/wysiwyg/src/composer_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

use crate::dom::UnicodeString;
use crate::{
Location, MenuAction, MenuState, ReplaceAll, Selection, TextUpdate,
LinkAction, Location, MenuAction, MenuState, ReplaceAll, Selection,
TextUpdate,
};

#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -25,6 +26,7 @@ where
pub text_update: TextUpdate<S>,
pub menu_state: MenuState,
pub menu_action: MenuAction,
pub link_action: LinkAction<S>,
}

impl<S> ComposerUpdate<S>
Expand All @@ -36,6 +38,7 @@ where
text_update: TextUpdate::<S>::Keep,
menu_state: MenuState::Keep,
menu_action: MenuAction::Keep,
link_action: LinkAction::Keep,
}
}

Expand All @@ -47,6 +50,7 @@ where
text_update: TextUpdate::<S>::Keep,
menu_state,
menu_action,
link_action: LinkAction::Keep,
}
}

Expand All @@ -55,11 +59,13 @@ where
end: Location,
menu_state: MenuState,
menu_action: MenuAction,
link_action: LinkAction<S>,
) -> Self {
Self {
text_update: TextUpdate::<S>::Select(Selection { start, end }),
menu_state,
menu_action,
link_action,
}
}

Expand All @@ -69,6 +75,7 @@ where
end: Location,
menu_state: MenuState,
menu_action: MenuAction,
link_action: LinkAction<S>,
) -> Self {
Self {
text_update: TextUpdate::ReplaceAll(ReplaceAll {
Expand All @@ -78,6 +85,7 @@ where
}),
menu_state,
menu_action,
link_action,
}
}
}
3 changes: 2 additions & 1 deletion crates/wysiwyg/src/link_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

use crate::UnicodeString;

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LinkAction<S: UnicodeString> {
Keep,
CreateWithText,
Create,
Edit(S),
Expand Down
2 changes: 1 addition & 1 deletion platforms/android/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withText
import io.element.android.wysiwyg.compose.testutils.ComposerActions.DEFAULT_ACTIONS
import io.element.android.wysiwyg.compose.testutils.EditorActions
import io.element.android.wysiwyg.compose.testutils.StateFactory.createState
import io.element.android.wysiwyg.compose.testutils.ViewMatchers.isRichTextEditor
import io.element.android.wysiwyg.compose.testutils.copy
import io.element.android.wysiwyg.compose.testutils.showContent
import io.element.android.wysiwyg.view.models.LinkAction
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Rule
Expand Down Expand Up @@ -128,4 +130,26 @@ class RichTextEditorTest {
assertEquals("Hello, <a href=\"https://matrix.org\">element</a>", state.messageHtml)
assertEquals("Hello, [element](<https://matrix.org>)", state.messageMarkdown)
}

@Test
fun testLinkActionUpdates() = runTest {
val state = createState()
composeTestRule.showContent(state)

composeTestRule.runOnUiThread {
state.setHtml("<a href=\"https://matrix.org\">matrix</a> <a href=\"https://element.io\">element</a> plain")
}

onView(withText("matrix element plain")).perform(EditorActions.setSelection(0, 0))
assertEquals(LinkAction.SetLink("https://matrix.org"), state.linkAction)

onView(withText("matrix element plain")).perform(EditorActions.setSelection(8, 8))
assertEquals(LinkAction.SetLink("https://element.io"), state.linkAction)

onView(withText("matrix element plain")).perform(EditorActions.setSelection(16, 16))
assertEquals(LinkAction.InsertLink, state.linkAction)

onView(withText("matrix element plain")).perform(EditorActions.setSelection(16, 20))
assertEquals(LinkAction.SetLink(null), state.linkAction)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.element.android.wysiwyg.compose.testutils

import android.view.View
import androidx.appcompat.widget.AppCompatEditText
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import org.hamcrest.Matcher

object Editor {
class SetSelection(
private val start: Int,
private val end: Int,
) : ViewAction {
override fun getConstraints(): Matcher<View> = isDisplayed()

override fun getDescription(): String = "Set selection to $start, $end"

override fun perform(uiController: UiController?, view: View?) {
val editor = view as? AppCompatEditText ?: return
editor.setSelection(start, end)
}
}

}

object EditorActions {
fun setSelection(start: Int, end: Int) = Editor.SetSelection(start, end)
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ private fun RealEditor(
menuActionListener = EditorEditText.OnMenuActionChangedListener { menuAction ->
state.menuAction = menuAction
}
linkActionChangedListener = EditorEditText.OnLinkActionChangedListener { linkAction ->
state.linkAction = linkAction
}
onFocusChangeListener =
View.OnFocusChangeListener { _, hasFocus -> state.hasFocus = hasFocus }

Expand All @@ -89,7 +92,6 @@ private fun RealEditor(
state.messageHtml = getContentAsMessageHtml()
state.messageMarkdown = getMarkdown()
state.lineCount = lineCount
state.linkAction = getLinkAction()
}

applyDefaultStyle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class EditorEditText : AppCompatEditText {
fun onMenuActionChanged(menuAction: MenuAction)
}

fun interface OnLinkActionChangedListener {
fun onLinkActionChanged(linkAction: LinkAction?)
}

/**
* Set the mention display handler to display mentions in a custom way.
*/
Expand All @@ -131,6 +135,18 @@ class EditorEditText : AppCompatEditText {
}
}

/**
* Set the link action listener to be notified when the available link action changes.
*/
var linkActionChangedListener: OnLinkActionChangedListener? = null
set(value) {
field = value

viewModel.linkActionCallback = { linkAction ->
value?.onLinkActionChanged(linkAction)
}
}

/**
* When not null, it will serve as an error callback for the client integrating this lib.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.element.android.wysiwyg.internal.view.models


import io.element.android.wysiwyg.view.models.LinkAction
import uniffi.wysiwyg_composer.LinkAction as InternalLinkAction

internal fun InternalLinkAction.toApiModel(
prevAction: LinkAction?
): LinkAction? =
when (this) {
is InternalLinkAction.Keep -> prevAction
is InternalLinkAction.Edit -> LinkAction.SetLink(currentUrl = url)
is InternalLinkAction.Create -> LinkAction.SetLink(currentUrl = null)
is InternalLinkAction.CreateWithText -> LinkAction.InsertLink
is InternalLinkAction.Disabled -> null
}
Loading

0 comments on commit d88b92c

Please sign in to comment.