From ee80fd5d972b4ed4e5c57824b4747c7250f1d85e Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Fri, 12 Aug 2022 15:26:34 +0800 Subject: [PATCH] feat: copy underline --- .../flowy_editor/lib/src/document/selection.dart | 7 +++++++ .../flowy_editor/lib/src/infra/html_converter.dart | 11 ++++++++++- .../copy_paste_handler.dart | 3 ++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/frontend/app_flowy/packages/flowy_editor/lib/src/document/selection.dart b/frontend/app_flowy/packages/flowy_editor/lib/src/document/selection.dart index 641a985577..3ac7293a2c 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/src/document/selection.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/src/document/selection.dart @@ -46,6 +46,13 @@ class Selection { (start.path <= end.path && !pathEquals(start.path, end.path)) || (isSingle && start.offset < end.offset); + Selection normalize() { + if (isForward) { + return Selection(start: end, end: start); + } + return this; + } + Selection get reversed => copyWith(start: end, end: start); Selection collapse({bool atStart = false}) { diff --git a/frontend/app_flowy/packages/flowy_editor/lib/src/infra/html_converter.dart b/frontend/app_flowy/packages/flowy_editor/lib/src/infra/html_converter.dart index e16237c0fa..2ec4bd382e 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/src/infra/html_converter.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/src/infra/html_converter.dart @@ -18,6 +18,7 @@ const String tagParagraph = "p"; const String tagImage = "img"; const String tagAnchor = "a"; const String tagBold = "b"; +const String tagUnderline = "u"; const String tagStrong = "strong"; const String tagSpan = "span"; const String tagCode = "code"; @@ -54,7 +55,8 @@ class HTMLToNodesConverter { if (child.localName == tagAnchor || child.localName == tagSpan || child.localName == tagCode || - child.localName == tagStrong) { + child.localName == tagStrong || + child.localName == tagUnderline) { _handleRichTextElement(delta, child); } else if (child.localName == tagBold) { // Google docs wraps the the content inside the `` tag. @@ -203,6 +205,8 @@ class HTMLToNodesConverter { delta.insert(element.text, attributes); } else if (element.localName == tagStrong || element.localName == tagBold) { delta.insert(element.text, {"bold": true}); + } else if (element.localName == tagUnderline) { + delta.insert(element.text, {"underline": true}); } else { delta.insert(element.text); } @@ -454,6 +458,11 @@ class NodesToHTMLConverter { final strong = html.Element.tag(tagStrong); strong.append(html.Text(op.content)); childNodes.add(strong); + } else if (attributes.length == 1 && + attributes[StyleKey.underline] == true) { + final strong = html.Element.tag(tagUnderline); + strong.append(html.Text(op.content)); + childNodes.add(strong); } else { final span = html.Element.tag(tagSpan); final cssString = _attributesToCssStyle(attributes); diff --git a/frontend/app_flowy/packages/flowy_editor/lib/src/service/internal_key_event_handlers/copy_paste_handler.dart b/frontend/app_flowy/packages/flowy_editor/lib/src/service/internal_key_event_handlers/copy_paste_handler.dart index a5f392a4eb..363b84967a 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/src/service/internal_key_event_handlers/copy_paste_handler.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/src/service/internal_key_event_handlers/copy_paste_handler.dart @@ -6,10 +6,11 @@ import 'package:flutter/services.dart'; import 'package:rich_clipboard/rich_clipboard.dart'; _handleCopy(EditorState editorState) async { - final selection = editorState.cursorSelection; + var selection = editorState.cursorSelection; if (selection == null || selection.isCollapsed) { return; } + selection = selection.normalize(); if (pathEquals(selection.start.path, selection.end.path)) { final nodeAtPath = editorState.document.nodeAtPath(selection.end.path)!; if (nodeAtPath.type == "text") {