Skip to content

Commit

Permalink
fix: transfer the whole line into link when text contains url (#693) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
penkzhou authored Mar 6, 2024
1 parent d4d35c0 commit 0ad1986
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
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

0 comments on commit 0ad1986

Please sign in to comment.