mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
Merge pull request #851 from AppFlowy-IO/feat/paste-url
Feat: paste url from plain text
This commit is contained in:
commit
03d76641ea
@ -149,6 +149,46 @@ _pastRichClipboard(EditorState editorState, RichClipboardData data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_pasteSingleLine(EditorState editorState, Selection selection, String line) {
|
||||||
|
final node = editorState.document.nodeAtPath(selection.end.path)! as TextNode;
|
||||||
|
final beginOffset = selection.end.offset;
|
||||||
|
TransactionBuilder(editorState)
|
||||||
|
..textEdit(
|
||||||
|
node,
|
||||||
|
() => Delta()
|
||||||
|
..retain(beginOffset)
|
||||||
|
..addAll(_lineContentToDelta(line)))
|
||||||
|
..setAfterSelection(Selection.collapsed(
|
||||||
|
Position(path: selection.end.path, offset: beginOffset + line.length)))
|
||||||
|
..commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// parse url from the line text
|
||||||
|
/// reference: https://stackoverflow.com/questions/59444837/flutter-dart-regex-to-extract-urls-from-a-string
|
||||||
|
Delta _lineContentToDelta(String lineContent) {
|
||||||
|
final exp = RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+');
|
||||||
|
final Iterable<RegExpMatch> matches = exp.allMatches(lineContent);
|
||||||
|
|
||||||
|
final delta = Delta();
|
||||||
|
|
||||||
|
var lastUrlEndOffset = 0;
|
||||||
|
|
||||||
|
for (final match in matches) {
|
||||||
|
if (lastUrlEndOffset < match.start) {
|
||||||
|
delta.insert(lineContent.substring(lastUrlEndOffset, match.start));
|
||||||
|
}
|
||||||
|
final linkContent = lineContent.substring(match.start, match.end);
|
||||||
|
delta.insert(linkContent, {"href": linkContent});
|
||||||
|
lastUrlEndOffset = match.end;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lastUrlEndOffset < lineContent.length) {
|
||||||
|
delta.insert(lineContent.substring(lastUrlEndOffset, lineContent.length));
|
||||||
|
}
|
||||||
|
|
||||||
|
return delta;
|
||||||
|
}
|
||||||
|
|
||||||
_handlePastePlainText(EditorState editorState, String plainText) {
|
_handlePastePlainText(EditorState editorState, String plainText) {
|
||||||
final selection = editorState.cursorSelection?.normalize();
|
final selection = editorState.cursorSelection?.normalize();
|
||||||
if (selection == null) {
|
if (selection == null) {
|
||||||
@ -163,18 +203,8 @@ _handlePastePlainText(EditorState editorState, String plainText) {
|
|||||||
if (lines.isEmpty) {
|
if (lines.isEmpty) {
|
||||||
return;
|
return;
|
||||||
} else if (lines.length == 1) {
|
} else if (lines.length == 1) {
|
||||||
final node =
|
// single line
|
||||||
editorState.document.nodeAtPath(selection.end.path)! as TextNode;
|
_pasteSingleLine(editorState, selection, lines.first);
|
||||||
final beginOffset = selection.end.offset;
|
|
||||||
TransactionBuilder(editorState)
|
|
||||||
..textEdit(
|
|
||||||
node,
|
|
||||||
() => Delta()
|
|
||||||
..retain(beginOffset)
|
|
||||||
..insert(lines[0]))
|
|
||||||
..setAfterSelection(Selection.collapsed(Position(
|
|
||||||
path: selection.end.path, offset: beginOffset + lines[0].length)))
|
|
||||||
..commit();
|
|
||||||
} else {
|
} else {
|
||||||
final firstLine = lines[0];
|
final firstLine = lines[0];
|
||||||
final beginOffset = selection.end.offset;
|
final beginOffset = selection.end.offset;
|
||||||
@ -196,11 +226,9 @@ _handlePastePlainText(EditorState editorState, String plainText) {
|
|||||||
if (index++ == remains.length - 1) {
|
if (index++ == remains.length - 1) {
|
||||||
return TextNode(
|
return TextNode(
|
||||||
type: "text",
|
type: "text",
|
||||||
delta: Delta()
|
delta: _lineContentToDelta(e)..addAll(insertedLineSuffix));
|
||||||
..insert(e)
|
|
||||||
..addAll(insertedLineSuffix));
|
|
||||||
}
|
}
|
||||||
return TextNode(type: "text", delta: Delta()..insert(e));
|
return TextNode(type: "text", delta: _lineContentToDelta(e));
|
||||||
}).toList();
|
}).toList();
|
||||||
// insert first line
|
// insert first line
|
||||||
tb.textEdit(
|
tb.textEdit(
|
||||||
|
Loading…
Reference in New Issue
Block a user