feat: copy paste check box

This commit is contained in:
Vincent Chan 2022-08-10 11:28:56 +08:00
parent 7f249ebae2
commit d40a3c33fd
2 changed files with 20 additions and 23 deletions

View File

@ -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 = <html.Node>[];
String tagName = tagParagraph;
if (subType == "bulleted-list") {
if (subType == StyleKey.bulletedList) {
tagName = tagList;
} else if (subType == StyleKey.checkbox) {
childNodes.add(html.Element.html('<input type="checkbox" />'));
}
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 += '</${node.localName}>';
return node.outerHtml;
}
if (node is html.Text) {

View File

@ -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