feat: copy underline

This commit is contained in:
Vincent Chan 2022-08-12 15:26:34 +08:00
parent 68a1acc9f2
commit ee80fd5d97
3 changed files with 19 additions and 2 deletions

View File

@ -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}) {

View File

@ -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 `<b></b>` 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);

View File

@ -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") {