From d40a3c33fd11644199479883c2c84432056f07ff Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Wed, 10 Aug 2022 11:28:56 +0800 Subject: [PATCH] feat: copy paste check box --- .../lib/infra/html_converter.dart | 26 ++++++++++++------- .../copy_paste_handler.dart | 17 +++--------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/frontend/app_flowy/packages/flowy_editor/lib/infra/html_converter.dart b/frontend/app_flowy/packages/flowy_editor/lib/infra/html_converter.dart index 708e47cb85..13f30ce4ef 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/infra/html_converter.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/infra/html_converter.dart @@ -3,6 +3,7 @@ import 'dart:collection'; import 'package:flowy_editor/document/attributes.dart'; import 'package:flowy_editor/document/node.dart'; import 'package:flowy_editor/document/text_delta.dart'; +import 'package:flowy_editor/render/rich_text/rich_text_style.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:html/parser.dart' show parse; @@ -206,18 +207,29 @@ class HTMLConverter { } } -html.Element deltaToHtml(Delta delta, [String? subType]) { +html.Element textNodeToHtml(TextNode textNode, {int? end}) { + String? subType = textNode.attributes["subtype"]; + return deltaToHtml(textNode.delta, subType: subType, end: end); +} + +html.Element deltaToHtml(Delta delta, {String? subType, int? end}) { + if (end != null) { + delta = delta.slice(0, end); + } + final childNodes = []; String tagName = tagParagraph; - if (subType == "bulleted-list") { + if (subType == StyleKey.bulletedList) { tagName = tagList; + } else if (subType == StyleKey.checkbox) { + childNodes.add(html.Element.html('')); } for (final op in delta.operations) { if (op is TextInsert) { final attributes = op.attributes; - if (attributes != null && attributes["bold"] == true) { + if (attributes != null && attributes[StyleKey.bold] == true) { final strong = html.Element.tag("strong"); strong.append(html.Text(op.content)); childNodes.add(strong); @@ -246,13 +258,7 @@ html.Element deltaToHtml(Delta delta, [String? subType]) { String stringify(html.Node node) { if (node is html.Element) { - String result = '<${node.localName}>'; - - for (final node in node.nodes) { - result += stringify(node); - } - - return result += ''; + return node.outerHtml; } if (node is html.Text) { diff --git a/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/copy_paste_handler.dart b/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/copy_paste_handler.dart index d3eeb073b9..3d1979dad0 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/copy_paste_handler.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/copy_paste_handler.dart @@ -60,10 +60,7 @@ _handleCopy(EditorState editorState) async { final nodeAtPath = editorState.document.nodeAtPath(selection.end.path)!; if (nodeAtPath.type == "text") { final textNode = nodeAtPath as TextNode; - final delta = - textNode.delta.slice(selection.start.offset, selection.end.offset); - - final htmlString = stringify(deltaToHtml(delta)); + final htmlString = stringify(textNodeToHtml(textNode)); debugPrint('copy html: $htmlString'); RichClipboard.setData(RichClipboardData(html: htmlString)); } else { @@ -81,18 +78,12 @@ _handleCopy(EditorState editorState) async { final node = traverser.current; if (node.type == "text") { final textNode = node as TextNode; - String? subType = textNode.attributes["subtype"]; if (node == beginNode) { - final htmlElement = - deltaToHtml(textNode.delta.slice(selection.start.offset), subType); - nodes.add(htmlElement); + nodes.add(textNodeToHtml(textNode)); } else if (node == endNode) { - final htmlElement = - deltaToHtml(textNode.delta.slice(0, selection.end.offset), subType); - nodes.add(htmlElement); + nodes.add(textNodeToHtml(textNode, end: selection.end.offset)); } else { - final htmlElement = deltaToHtml(textNode.delta, subType); - nodes.add(htmlElement); + nodes.add(textNodeToHtml(textNode)); } } // TODO: handle image and other blocks