diff --git a/lib/src/models/documents/document.dart b/lib/src/models/documents/document.dart index eb0673f0..bc70fba7 100644 --- a/lib/src/models/documents/document.dart +++ b/lib/src/models/documents/document.dart @@ -9,6 +9,7 @@ import 'history.dart'; import 'nodes/block.dart'; import 'nodes/container.dart'; import 'nodes/embeddable.dart'; +import 'nodes/leaf.dart'; import 'nodes/line.dart'; import 'nodes/node.dart'; import 'style.dart'; @@ -136,6 +137,22 @@ class Document { return block.queryChild(res.offset, true); } + /// Given offset, find its leaf node in document + Tuple2 querySegmentLeafNode(int offset) { + final result = queryChild(offset); + if (result.node == null) { + return const Tuple2(null, null); + } + + final line = result.node as Line; + final segmentResult = line.queryChild(result.offset, false); + if (segmentResult.node == null) { + return Tuple2(line, null); + } + final segment = segmentResult.node as Leaf; + return Tuple2(line, segment); + } + void compose(Delta delta, ChangeSource changeSource) { assert(!_observer.isClosed); delta.trim(); diff --git a/lib/src/widgets/editor.dart b/lib/src/widgets/editor.dart index f1ec50cf..564193d7 100644 --- a/lib/src/widgets/editor.dart +++ b/lib/src/widgets/editor.dart @@ -9,7 +9,6 @@ import 'package:flutter/services.dart'; import '../models/documents/document.dart'; import '../models/documents/nodes/container.dart' as container_node; -import '../models/documents/nodes/line.dart'; import '../utils/platform.dart'; import 'box.dart'; import 'controller.dart'; @@ -535,13 +534,14 @@ class _QuillEditorSelectionGestureDetectorBuilder return false; } final pos = renderEditor!.getPositionForOffset(details.globalPosition); - final result = editor!.widget.controller.document.queryChild(pos.offset); - if (result.node == null) { + final result = + editor!.widget.controller.document.querySegmentLeafNode(pos.offset); + final line = result.item1; + if (line == null) { return false; } - final line = result.node as Line; - final segmentResult = line.queryChild(result.offset, false); - if (segmentResult.node == null && line.length == 1) { + final segmentLeaf = result.item2; + if (segmentLeaf == null && line.length == 1) { editor!.widget.controller.updateSelection( TextSelection.collapsed(offset: pos.offset), ChangeSource.LOCAL); return true; diff --git a/lib/src/widgets/toolbar/link_style_button.dart b/lib/src/widgets/toolbar/link_style_button.dart index 6f54a314..a7c1c0eb 100644 --- a/lib/src/widgets/toolbar/link_style_button.dart +++ b/lib/src/widgets/toolbar/link_style_button.dart @@ -103,10 +103,17 @@ class _LinkStyleButtonState extends State { final link = _getLinkAttributeValue(); final index = widget.controller.selection.baseOffset; + var text; if (link != null) { - // TODO: 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; + if (leaf != null) { + text = leaf.toPlainText(); + } } - final text = widget.controller.document + + text ??= widget.controller.document .toPlainText() .substring(index, widget.controller.selection.extentOffset); return _LinkDialog(