Skip to content

Commit a507fb8

Browse files
authored
Merge pull request #1550 from LucasXu0/fix_windows_copy_paste
fix: Clipboard does not work in Windows #1406
2 parents 9711d67 + 4db5b7a commit a507fb8

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import 'dart:io' show Platform;
2+
3+
import 'package:rich_clipboard/rich_clipboard.dart';
4+
5+
class AppFlowyClipboardData {
6+
const AppFlowyClipboardData({
7+
required this.text,
8+
required this.html,
9+
});
10+
final String? text;
11+
final String? html;
12+
}
13+
14+
class AppFlowyClipboard {
15+
static Future<void> setData({
16+
String? text,
17+
String? html,
18+
}) async {
19+
// https://github.com/BringingFire/rich_clipboard/issues/13
20+
// Wrapping a `<html><body>` tag for html in Windows,
21+
// otherwise it will raise an exception
22+
if (Platform.isWindows && html != null) {
23+
if (!html.startsWith('<html><body>')) {
24+
html = '<html><body>$html</body></html>';
25+
}
26+
}
27+
28+
return RichClipboard.setData(
29+
RichClipboardData(
30+
text: text,
31+
html: html,
32+
),
33+
);
34+
}
35+
36+
static Future<AppFlowyClipboardData> getData() async {
37+
final data = await RichClipboard.getData();
38+
final text = data.text;
39+
var html = data.html;
40+
41+
// https://github.com/BringingFire/rich_clipboard/issues/13
42+
// Remove all the fragment symbol in Windows.
43+
if (Platform.isWindows && html != null) {
44+
html = html
45+
.replaceAll('<!--StartFragment-->', '')
46+
.replaceAll('<!--EndFragment-->', '');
47+
}
48+
49+
return AppFlowyClipboardData(
50+
text: text,
51+
html: html,
52+
);
53+
}
54+
}

frontend/app_flowy/packages/appflowy_editor/lib/src/render/image/image_node_builder.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:appflowy_editor/src/core/document/node.dart';
2+
import 'package:appflowy_editor/src/infra/clipboard.dart';
23
import 'package:appflowy_editor/src/service/render_plugin_service.dart';
34
import 'package:flutter/material.dart';
4-
import 'package:rich_clipboard/rich_clipboard.dart';
55

66
import 'image_node_widget.dart';
77

@@ -21,7 +21,7 @@ class ImageNodeBuilder extends NodeWidgetBuilder<Node> {
2121
width: width,
2222
alignment: _textToAlignment(align),
2323
onCopy: () {
24-
RichClipboard.setData(RichClipboardData(text: src));
24+
AppFlowyClipboard.setData(text: src);
2525
},
2626
onDelete: () {
2727
final transaction = context.editorState.transaction

frontend/app_flowy/packages/appflowy_editor/lib/src/render/toolbar/toolbar_item.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import 'package:appflowy_editor/appflowy_editor.dart';
22
import 'package:appflowy_editor/src/commands/text/text_commands.dart';
33
import 'package:appflowy_editor/src/extensions/url_launcher_extension.dart';
44
import 'package:appflowy_editor/src/flutter/overlay.dart';
5+
import 'package:appflowy_editor/src/infra/clipboard.dart';
56
import 'package:appflowy_editor/src/infra/flowy_svg.dart';
67
import 'package:appflowy_editor/src/render/link_menu/link_menu.dart';
78
import 'package:appflowy_editor/src/extensions/text_node_extensions.dart';
89
import 'package:appflowy_editor/src/extensions/editor_state_extensions.dart';
910
import 'package:appflowy_editor/src/service/default_text_operations/format_rich_text_style.dart';
1011

1112
import 'package:flutter/material.dart' hide Overlay, OverlayEntry;
12-
import 'package:rich_clipboard/rich_clipboard.dart';
1313

1414
typedef ToolbarItemEventHandler = void Function(
1515
EditorState editorState, BuildContext context);
@@ -363,7 +363,7 @@ void showLinkMenu(
363363
_dismissLinkMenu();
364364
},
365365
onCopyLink: () {
366-
RichClipboard.setData(RichClipboardData(text: linkText));
366+
AppFlowyClipboard.setData(text: linkText);
367367
_dismissLinkMenu();
368368
},
369369
onRemoveLink: () {

frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/copy_paste_handler.dart

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'package:appflowy_editor/appflowy_editor.dart';
2+
import 'package:appflowy_editor/src/infra/clipboard.dart';
23
import 'package:appflowy_editor/src/infra/html_converter.dart';
34
import 'package:appflowy_editor/src/core/document/node_iterator.dart';
45
import 'package:appflowy_editor/src/service/internal_key_event_handlers/number_list_helper.dart';
56
import 'package:flutter/material.dart';
6-
import 'package:rich_clipboard/rich_clipboard.dart';
77

88
int _textLengthOfNode(Node node) {
99
if (node is TextNode) {
@@ -38,14 +38,15 @@ void _handleCopy(EditorState editorState) async {
3838
startOffset: selection.start.offset,
3939
endOffset: selection.end.offset)
4040
.toHTMLString();
41+
final textString = textNode.toPlainText().substring(
42+
selection.startIndex,
43+
selection.endIndex,
44+
);
4145
Log.keyboard.debug('copy html: $htmlString');
42-
RichClipboard.setData(RichClipboardData(
46+
AppFlowyClipboard.setData(
47+
text: textString,
4348
html: htmlString,
44-
text: textNode.toPlainText().substring(
45-
selection.startIndex,
46-
selection.endIndex,
47-
),
48-
));
49+
);
4950
} else {
5051
Log.keyboard.debug('unimplemented: copy non-text');
5152
}
@@ -79,7 +80,10 @@ void _handleCopy(EditorState editorState) async {
7980
}
8081
text += '\n';
8182
}
82-
RichClipboard.setData(RichClipboardData(html: html, text: text));
83+
AppFlowyClipboard.setData(
84+
text: text,
85+
html: html,
86+
);
8387
}
8488

8589
void _pasteHTML(EditorState editorState, String html) {
@@ -186,7 +190,7 @@ void _pasteMultipleLinesInText(
186190
}
187191

188192
void _handlePaste(EditorState editorState) async {
189-
final data = await RichClipboard.getData();
193+
final data = await AppFlowyClipboard.getData();
190194

191195
if (editorState.cursorSelection?.isCollapsed ?? false) {
192196
_pastRichClipboard(editorState, data);
@@ -200,7 +204,7 @@ void _handlePaste(EditorState editorState) async {
200204
});
201205
}
202206

203-
void _pastRichClipboard(EditorState editorState, RichClipboardData data) {
207+
void _pastRichClipboard(EditorState editorState, AppFlowyClipboardData data) {
204208
if (data.html != null) {
205209
_pasteHTML(editorState, data.html!);
206210
return;

0 commit comments

Comments
 (0)