Skip to content
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

fix: transfer the whole line into link when text contains url (#693) #737

Merged
merged 2 commits into from
Mar 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,44 @@ extension on EditorState {
final nodes = plainText
.split('\n')
.map(
(e) => e
(paragraph) => paragraph
..replaceAll(r'\r', '')
..trimRight(),
)
.map((e) {
// parse the url content
final Attributes attributes = {};
if (_hrefRegex.hasMatch(e)) {
attributes[AppFlowyRichTextKeys.href] = e;
.map((paragraph) {
Delta delta = Delta();
if (_hrefRegex.hasMatch(paragraph)) {
final firstMatch = _hrefRegex.firstMatch(paragraph);
if (firstMatch != null) {
int startPos = firstMatch.start;
int endPos = firstMatch.end;
final String? url = firstMatch.group(0);
if (url != null) {
/// insert the text before the link
if (startPos > 0) {
delta.insert(paragraph.substring(0, startPos));
}

/// insert the link
delta.insert(
paragraph.substring(startPos, endPos),
attributes: {AppFlowyRichTextKeys.href: url},
);

/// insert the text after the link
if (endPos < paragraph.length) {
delta.insert(paragraph.substring(endPos));
}
}
}
} else {
delta.insert(paragraph);
}
return Delta()..insert(e, attributes: attributes);
return delta;
})
.map((e) => paragraphNode(delta: e))
.map((paragraph) => paragraphNode(delta: paragraph))
.toList();

if (nodes.isEmpty) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ void main() async {
},
);

testWidgets(
'Copy text contains link',
(tester) async {
final editor = tester.editor..addParagraph(initialText: '');
await editor.startTesting();
await editor.updateSelection(
Selection.collapsed(Position(path: [0], offset: 0)),
);

const textWithLink = 'click https://appflowy.io/ jump to appflowy';
AppFlowyClipboard.mockSetData(
const AppFlowyClipboardData(text: textWithLink),
);

pasteCommand.execute(editor.editorState);
await tester.pumpAndSettle();

final delta = editor.nodeAtPath([0])!.delta!;
expect(delta.toPlainText(), textWithLink);
expect(
delta.everyAttributes(
(element) =>
element[AppFlowyRichTextKeys.href] == 'https://appflowy.io/',
),
false,
);

AppFlowyClipboard.mockSetData(null);
await editor.dispose();
},
);

testWidgets(
'Presses Command + A in small document and copy text and paste text',
(tester) async {
Expand Down
Loading