Improve link button

pull/602/head
X Code 3 years ago
parent 81f54551c5
commit 1c4fd1cd0e
  1. 17
      lib/src/models/documents/document.dart
  2. 12
      lib/src/widgets/editor.dart
  3. 11
      lib/src/widgets/toolbar/link_style_button.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<Line?, Leaf?> 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();

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

@ -103,10 +103,17 @@ class _LinkStyleButtonState extends State<LinkStyleButton> {
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(

Loading…
Cancel
Save