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

Alter attribute input for mentions #720

Merged
merged 23 commits into from
Jun 16, 2023
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
52 changes: 8 additions & 44 deletions bindings/wysiwyg-ffi/src/ffi_composer_model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::vec;

use widestring::Utf16String;

Expand Down Expand Up @@ -259,21 +260,9 @@ impl ComposerModel {
}

/// Creates an at-room mention node and inserts it into the composer at the current selection
pub fn insert_at_room_mention(
self: &Arc<Self>,
attributes: Vec<Attribute>,
) -> Arc<ComposerUpdate> {
let attrs = attributes
.iter()
.map(|attr| {
(
Utf16String::from_str(&attr.key),
Utf16String::from_str(&attr.value),
)
})
.collect();
pub fn insert_at_room_mention(self: &Arc<Self>) -> Arc<ComposerUpdate> {
Arc::new(ComposerUpdate::from(
self.inner.lock().unwrap().insert_at_room_mention(attrs),
self.inner.lock().unwrap().insert_at_room_mention(vec![]),
))
}

Expand All @@ -282,19 +271,11 @@ impl ComposerModel {
self: &Arc<Self>,
url: String,
text: String,
attributes: Vec<Attribute>,
_attributes: Vec<Attribute>, // TODO remove attributes
) -> Arc<ComposerUpdate> {
let url = Utf16String::from_str(&url);
let text = Utf16String::from_str(&text);
let attrs = attributes
.iter()
.map(|attr| {
(
Utf16String::from_str(&attr.key),
Utf16String::from_str(&attr.value),
)
})
.collect();
let attrs = vec![];
jonnyandrew marked this conversation as resolved.
Show resolved Hide resolved
Arc::new(ComposerUpdate::from(
self.inner.lock().unwrap().insert_mention(url, text, attrs),
))
Expand All @@ -305,18 +286,9 @@ impl ComposerModel {
pub fn insert_at_room_mention_at_suggestion(
self: &Arc<Self>,
suggestion: SuggestionPattern,
attributes: Vec<Attribute>,
) -> Arc<ComposerUpdate> {
let suggestion = wysiwyg::SuggestionPattern::from(suggestion);
let attrs = attributes
.iter()
.map(|attr| {
(
Utf16String::from_str(&attr.key),
Utf16String::from_str(&attr.value),
)
})
.collect();
let attrs = vec![];
Arc::new(ComposerUpdate::from(
self.inner
.lock()
Expand All @@ -332,20 +304,12 @@ impl ComposerModel {
url: String,
text: String,
suggestion: SuggestionPattern,
attributes: Vec<Attribute>,
_attributes: Vec<Attribute>, // TODO remove attributes
jonnyandrew marked this conversation as resolved.
Show resolved Hide resolved
) -> Arc<ComposerUpdate> {
let url = Utf16String::from_str(&url);
let text = Utf16String::from_str(&text);
let suggestion = wysiwyg::SuggestionPattern::from(suggestion);
let attrs = attributes
.iter()
.map(|attr| {
(
Utf16String::from_str(&attr.key),
Utf16String::from_str(&attr.value),
)
})
.collect();
let attrs = vec![];
Arc::new(ComposerUpdate::from(
self.inner
.lock()
Expand Down
9 changes: 3 additions & 6 deletions bindings/wysiwyg-ffi/src/ffi_composer_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ mod test {
use std::{collections::HashMap, sync::Arc};

use crate::{
ActionState, Attribute, ComposerAction, ComposerModel, MenuAction,
MenuState, SuggestionPattern,
ActionState, ComposerAction, ComposerModel, MenuAction, MenuState,
SuggestionPattern,
};

#[test]
Expand Down Expand Up @@ -229,10 +229,7 @@ mod test {
"https://matrix.to/#/@alice:matrix.org".into(),
"Alice".into(),
suggestion_pattern,
vec![Attribute {
key: "data-mention-type".into(),
value: "user".into(),
}],
vec![], // TODO remove argument when function signature changes
);
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/wysiwyg-ffi/src/wysiwyg_composer.udl
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ interface ComposerModel {
ComposerUpdate set_link(string url, sequence<Attribute> attributes);
ComposerUpdate set_link_with_text(string url, string text, sequence<Attribute> attributes);
ComposerUpdate remove_links();
ComposerUpdate insert_at_room_mention(sequence<Attribute> attributes);
ComposerUpdate insert_at_room_mention();
ComposerUpdate insert_mention(string url, string text, sequence<Attribute> attributes);
ComposerUpdate insert_at_room_mention_at_suggestion(SuggestionPattern suggestion, sequence<Attribute> attributes);
ComposerUpdate insert_at_room_mention_at_suggestion(SuggestionPattern suggestion);
ComposerUpdate insert_mention_at_suggestion(string url, string text, SuggestionPattern suggestion, sequence<Attribute> attributes);
ComposerUpdate code_block();
ComposerUpdate quote();
Expand Down
12 changes: 12 additions & 0 deletions crates/wysiwyg/src/dom/nodes/mention_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,16 @@ impl<S: UnicodeString> MentionNode<S> {
let attributes = if as_message {
vec![("href".into(), S::from(mention.uri()))]
} else {
// this is now only required for us to attach a custom style attribute for web
let mut attrs = self.attributes.clone();
let data_mention_type = match mention.kind() {
MentionKind::Room => "room",
MentionKind::User => "user",
};
attrs.push((
"data-mention-type".into(),
data_mention_type.into(),
));
attrs.push(("href".into(), S::from(mention.uri())));
attrs.push(("contenteditable".into(), "false".into()));
attrs
Expand All @@ -184,7 +193,10 @@ impl<S: UnicodeString> MentionNode<S> {
if as_message {
formatter.push(self.display_text())
} else {
// this is now only required for us to attach a custom style attribute for web
let mut attributes = self.attributes.clone();
attributes
.push(("data-mention-type".into(), "at-room".into()));
attributes.push(("href".into(), "#".into())); // designates a placeholder link in html
attributes.push(("contenteditable".into(), "false".into()));

Expand Down
25 changes: 4 additions & 21 deletions crates/wysiwyg/src/dom/parser/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,8 +831,8 @@ mod js {
self.current_path.push(DomNodeKind::Link);

let mut attributes = vec![];
let valid_attributes =
["contenteditable", "data-mention-type", "style"];
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

// we only need to pass in a style attribute from web to allow CSS variable insertion
let valid_attributes = ["style"];

for attr in valid_attributes.into_iter() {
if node
Expand Down Expand Up @@ -1081,27 +1081,10 @@ mod js {
roundtrip(r#"foo <a href="">bar</a> baz"#);
}

#[wasm_bindgen_test]
fn a_with_attributes() {
roundtrip(
r#"<a contenteditable="false" data-mention-type="user" style="something" href="http://example.com">a user mention</a>"#,
);
}

#[wasm_bindgen_test]
fn a_with_bad_attribute() {
let html = r#"<a invalidattribute="true" href="http://example.com">a user mention</a>"#;
let dom = HtmlParser::default().parse::<Utf16String>(html).unwrap();
assert_eq!(
dom.to_string(),
r#"<a href="http://example.com">a user mention</a>"#
);
}

Comment on lines -1084 to -1100
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No longer good tests, as we don't have links with attributes

#[wasm_bindgen_test]
fn mention_with_attributes() {
roundtrip(
r#"<a contenteditable="false" data-mention-type="user" style="something" href="https://matrix.to/@test:example.org">test</a>"#,
r#"<a style="something" href="https://matrix.to/@test:example.org">test</a>"#,
);
}

Expand All @@ -1111,7 +1094,7 @@ mod js {
let dom = HtmlParser::default().parse::<Utf16String>(html).unwrap();
assert_eq!(
dom.to_string(),
r#"<a href="https://matrix.to/#/@test:example.org" contenteditable="false">test</a>"#
r#"<a data-mention-type="user" href="https://matrix.to/#/@test:example.org" contenteditable="false">test</a>"#
);
}

Expand Down
6 changes: 3 additions & 3 deletions crates/wysiwyg/src/tests/test_deleting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ fn backspace_mention_multiple() {
model.backspace();
assert_eq!(
restore_whitespace(&tx(&model)),
"<a href=\"https://matrix.to/#/@test:example.org\" contenteditable=\"false\">first</a>|"
"<a data-mention-type=\"user\" href=\"https://matrix.to/#/@test:example.org\" contenteditable=\"false\">first</a>|"
);
model.backspace();
assert_eq!(restore_whitespace(&tx(&model)), "|");
Expand Down Expand Up @@ -972,7 +972,7 @@ fn delete_first_mention_of_multiple() {
model.delete();
assert_eq!(
restore_whitespace(&tx(&model)),
"|<a href=\"https://matrix.to/#/@test:example.org\" contenteditable=\"false\">second</a>"
"|<a data-mention-type=\"user\" href=\"https://matrix.to/#/@test:example.org\" contenteditable=\"false\">second</a>"
);
model.delete();
assert_eq!(restore_whitespace(&tx(&model)), "|");
Expand Down Expand Up @@ -1000,7 +1000,7 @@ fn delete_second_mention_of_multiple() {
model.delete();
assert_eq!(
restore_whitespace(&tx(&model)),
"<a href=\"https://matrix.to/#/@test:example.org\" contenteditable=\"false\">first</a> |"
"<a data-mention-type=\"user\" href=\"https://matrix.to/#/@test:example.org\" contenteditable=\"false\">first</a> |"
);
}

Expand Down
Loading