Create struct for segment leaf nodes

pull/1128/head
Adil Hanney 2 years ago
parent 3c676a7578
commit e850f52d45
  1. 11
      lib/src/models/documents/document.dart
  2. 8
      lib/src/models/structs/segment_leaf_node.dart
  3. 2
      lib/src/widgets/controller.dart
  4. 4
      lib/src/widgets/editor.dart
  5. 4
      lib/src/widgets/toolbar/link_style_button.dart

@ -4,6 +4,7 @@ import '../quill_delta.dart';
import '../rules/rule.dart'; import '../rules/rule.dart';
import '../structs/doc_change.dart'; import '../structs/doc_change.dart';
import '../structs/offset_value.dart'; import '../structs/offset_value.dart';
import '../structs/segment_leaf_node.dart';
import 'attribute.dart'; import 'attribute.dart';
import 'history.dart'; import 'history.dart';
import 'nodes/block.dart'; import 'nodes/block.dart';
@ -215,19 +216,15 @@ class Document {
} }
/// Given offset, find its leaf node in document /// Given offset, find its leaf node in document
Tuple2<Line?, Leaf?> querySegmentLeafNode(int offset) { SegmentLeafNode querySegmentLeafNode(int offset) {
final result = queryChild(offset); final result = queryChild(offset);
if (result.node == null) { if (result.node == null) {
return const Tuple2(null, null); return const SegmentLeafNode(null, null);
} }
final line = result.node as Line; final line = result.node as Line;
final segmentResult = line.queryChild(result.offset, false); final segmentResult = line.queryChild(result.offset, false);
if (segmentResult.node == null) { return SegmentLeafNode(line, segmentResult.node as Leaf?);
return Tuple2(line, null);
}
final segment = segmentResult.node as Leaf;
return Tuple2(line, segment);
} }
/// Composes [change] Delta into this document. /// Composes [change] Delta into this document.

@ -0,0 +1,8 @@
import '../../../flutter_quill.dart';
class SegmentLeafNode {
const SegmentLeafNode(this.line, this.leaf);
final Line? line;
final Leaf? leaf;
}

@ -366,7 +366,7 @@ class QuillController extends ChangeNotifier {
/// Given offset, find its leaf node in document /// Given offset, find its leaf node in document
Leaf? queryNode(int offset) { Leaf? queryNode(int offset) {
return document.querySegmentLeafNode(offset).item2; return document.querySegmentLeafNode(offset).leaf;
} }
/// Clipboard for image url and its corresponding style /// Clipboard for image url and its corresponding style

@ -645,11 +645,11 @@ class _QuillEditorSelectionGestureDetectorBuilder
final pos = renderEditor!.getPositionForOffset(details.globalPosition); final pos = renderEditor!.getPositionForOffset(details.globalPosition);
final result = final result =
editor!.widget.controller.document.querySegmentLeafNode(pos.offset); editor!.widget.controller.document.querySegmentLeafNode(pos.offset);
final line = result.item1; final line = result.line;
if (line == null) { if (line == null) {
return false; return false;
} }
final segmentLeaf = result.item2; final segmentLeaf = result.leaf;
if (segmentLeaf == null && line.length == 1) { if (segmentLeaf == null && line.length == 1) {
editor!.widget.controller.updateSelection( editor!.widget.controller.updateSelection(
TextSelection.collapsed(offset: pos.offset), ChangeSource.LOCAL); TextSelection.collapsed(offset: pos.offset), ChangeSource.LOCAL);

@ -95,7 +95,7 @@ class _LinkStyleButtonState extends State<LinkStyleButton> {
if (link != null) { if (link != null) {
// text should be the link's corresponding text, not selection // text should be the link's corresponding text, not selection
final leaf = final leaf =
widget.controller.document.querySegmentLeafNode(index).item2; widget.controller.document.querySegmentLeafNode(index).leaf;
if (leaf != null) { if (leaf != null) {
text = leaf.toPlainText(); text = leaf.toPlainText();
} }
@ -126,7 +126,7 @@ class _LinkStyleButtonState extends State<LinkStyleButton> {
var length = widget.controller.selection.end - index; var length = widget.controller.selection.end - index;
if (_getLinkAttributeValue() != null) { if (_getLinkAttributeValue() != null) {
// text should be the link's corresponding text, not selection // text should be the link's corresponding text, not selection
final leaf = widget.controller.document.querySegmentLeafNode(index).item2; final leaf = widget.controller.document.querySegmentLeafNode(index).leaf;
if (leaf != null) { if (leaf != null) {
final range = getLinkRange(leaf); final range = getLinkRange(leaf);
index = range.start; index = range.start;

Loading…
Cancel
Save