This commit is contained in:
Lucas.Xu 2022-08-11 20:14:36 +08:00
parent c53df59b48
commit f708afe673
4 changed files with 32 additions and 27 deletions

View File

@ -46,6 +46,8 @@ class Selection {
(start.path <= end.path && !pathEquals(start.path, end.path)) ||
(isSingle && start.offset < end.offset);
Selection get reversed => copyWith(start: end, end: start);
Selection collapse({bool atStart = false}) {
if (atStart) {
return Selection(start: start, end: start);

View File

@ -20,14 +20,17 @@ extension TextNodeExtension on TextNode {
bool allSatisfyInSelection(String styleKey, Selection selection) {
final ops = delta.whereType<TextInsert>();
final startOffset =
selection.isBackward ? selection.start.offset : selection.end.offset;
final endOffset =
selection.isBackward ? selection.end.offset : selection.start.offset;
var start = 0;
for (final op in ops) {
if (start >= selection.end.offset) {
if (start >= endOffset) {
break;
}
final length = op.length;
if (start < selection.end.offset &&
start + length > selection.start.offset) {
if (start < endOffset && start + length > startOffset) {
if (op.attributes == null ||
!op.attributes!.containsKey(styleKey) ||
op.attributes![styleKey] == false) {

View File

@ -43,7 +43,7 @@ bool insertTextNodeAfterSelection(
}
final node = nodes.first;
if (node is TextNode && node.delta.length == 0) {
if (node is TextNode && node.delta.isEmpty) {
formatTextNodes(editorState, attributes);
} else {
final next = selection.end.path.next;
@ -157,11 +157,18 @@ bool formatRichTextPartialStyle(EditorState editorState, String styleKey) {
}
bool formatRichTextStyle(EditorState editorState, Attributes attributes) {
final selection = editorState.service.selectionService.currentSelection.value;
final nodes = editorState.service.selectionService.currentSelectedNodes;
final textNodes = nodes.whereType<TextNode>().toList();
var selection = editorState.service.selectionService.currentSelection.value;
var nodes = editorState.service.selectionService.currentSelectedNodes;
if (selection == null || textNodes.isEmpty) {
if (selection == null) {
return false;
}
nodes = selection.isBackward ? nodes : nodes.reversed.toList(growable: false);
selection = selection.isBackward ? selection : selection.reversed;
var textNodes = nodes.whereType<TextNode>().toList();
if (textNodes.isEmpty) {
return false;
}
@ -180,28 +187,20 @@ bool formatRichTextStyle(EditorState editorState, Attributes attributes) {
} else {
for (var i = 0; i < textNodes.length; i++) {
final textNode = textNodes[i];
var index = 0;
var length = textNode.toRawString().length;
if (i == 0 && textNode == nodes.first) {
builder.formatText(
textNode,
selection.start.offset,
textNode.toRawString().length - selection.start.offset,
attributes,
);
index = selection.start.offset;
length = textNode.toRawString().length - selection.start.offset;
} else if (i == textNodes.length - 1 && textNode == nodes.last) {
builder.formatText(
textNode,
0,
selection.end.offset,
attributes,
);
} else {
builder.formatText(
textNode,
0,
textNode.toRawString().length,
attributes,
);
length = selection.end.offset;
}
builder.formatText(
textNode,
index,
length,
attributes,
);
}
}

View File

@ -487,6 +487,7 @@ class _FlowySelectionState extends State<FlowySelection>
max = mid - 1;
}
}
min = min.clamp(start, end);
final node = sortedNodes[min];
if (node.children.isNotEmpty && node.children.first.rect.top <= offset.dy) {
final children = node.children.toList(growable: false);