mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: copy paste check box
This commit is contained in:
parent
7f249ebae2
commit
d40a3c33fd
@ -3,6 +3,7 @@ import 'dart:collection';
|
|||||||
import 'package:flowy_editor/document/attributes.dart';
|
import 'package:flowy_editor/document/attributes.dart';
|
||||||
import 'package:flowy_editor/document/node.dart';
|
import 'package:flowy_editor/document/node.dart';
|
||||||
import 'package:flowy_editor/document/text_delta.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/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:html/parser.dart' show parse;
|
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>[];
|
final childNodes = <html.Node>[];
|
||||||
String tagName = tagParagraph;
|
String tagName = tagParagraph;
|
||||||
|
|
||||||
if (subType == "bulleted-list") {
|
if (subType == StyleKey.bulletedList) {
|
||||||
tagName = tagList;
|
tagName = tagList;
|
||||||
|
} else if (subType == StyleKey.checkbox) {
|
||||||
|
childNodes.add(html.Element.html('<input type="checkbox" />'));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (final op in delta.operations) {
|
for (final op in delta.operations) {
|
||||||
if (op is TextInsert) {
|
if (op is TextInsert) {
|
||||||
final attributes = op.attributes;
|
final attributes = op.attributes;
|
||||||
if (attributes != null && attributes["bold"] == true) {
|
if (attributes != null && attributes[StyleKey.bold] == true) {
|
||||||
final strong = html.Element.tag("strong");
|
final strong = html.Element.tag("strong");
|
||||||
strong.append(html.Text(op.content));
|
strong.append(html.Text(op.content));
|
||||||
childNodes.add(strong);
|
childNodes.add(strong);
|
||||||
@ -246,13 +258,7 @@ html.Element deltaToHtml(Delta delta, [String? subType]) {
|
|||||||
|
|
||||||
String stringify(html.Node node) {
|
String stringify(html.Node node) {
|
||||||
if (node is html.Element) {
|
if (node is html.Element) {
|
||||||
String result = '<${node.localName}>';
|
return node.outerHtml;
|
||||||
|
|
||||||
for (final node in node.nodes) {
|
|
||||||
result += stringify(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result += '</${node.localName}>';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node is html.Text) {
|
if (node is html.Text) {
|
||||||
|
@ -60,10 +60,7 @@ _handleCopy(EditorState editorState) async {
|
|||||||
final nodeAtPath = editorState.document.nodeAtPath(selection.end.path)!;
|
final nodeAtPath = editorState.document.nodeAtPath(selection.end.path)!;
|
||||||
if (nodeAtPath.type == "text") {
|
if (nodeAtPath.type == "text") {
|
||||||
final textNode = nodeAtPath as TextNode;
|
final textNode = nodeAtPath as TextNode;
|
||||||
final delta =
|
final htmlString = stringify(textNodeToHtml(textNode));
|
||||||
textNode.delta.slice(selection.start.offset, selection.end.offset);
|
|
||||||
|
|
||||||
final htmlString = stringify(deltaToHtml(delta));
|
|
||||||
debugPrint('copy html: $htmlString');
|
debugPrint('copy html: $htmlString');
|
||||||
RichClipboard.setData(RichClipboardData(html: htmlString));
|
RichClipboard.setData(RichClipboardData(html: htmlString));
|
||||||
} else {
|
} else {
|
||||||
@ -81,18 +78,12 @@ _handleCopy(EditorState editorState) async {
|
|||||||
final node = traverser.current;
|
final node = traverser.current;
|
||||||
if (node.type == "text") {
|
if (node.type == "text") {
|
||||||
final textNode = node as TextNode;
|
final textNode = node as TextNode;
|
||||||
String? subType = textNode.attributes["subtype"];
|
|
||||||
if (node == beginNode) {
|
if (node == beginNode) {
|
||||||
final htmlElement =
|
nodes.add(textNodeToHtml(textNode));
|
||||||
deltaToHtml(textNode.delta.slice(selection.start.offset), subType);
|
|
||||||
nodes.add(htmlElement);
|
|
||||||
} else if (node == endNode) {
|
} else if (node == endNode) {
|
||||||
final htmlElement =
|
nodes.add(textNodeToHtml(textNode, end: selection.end.offset));
|
||||||
deltaToHtml(textNode.delta.slice(0, selection.end.offset), subType);
|
|
||||||
nodes.add(htmlElement);
|
|
||||||
} else {
|
} else {
|
||||||
final htmlElement = deltaToHtml(textNode.delta, subType);
|
nodes.add(textNodeToHtml(textNode));
|
||||||
nodes.add(htmlElement);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO: handle image and other blocks
|
// TODO: handle image and other blocks
|
||||||
|
Loading…
x
Reference in New Issue
Block a user