瀏覽代碼

Fixes broken layer link in embeds

Anatoly Pulyaevskiy 6 年之前
父節點
當前提交
a834f34385

+ 16
- 36
packages/zefyr/lib/src/widgets/common.dart 查看文件

44
   @override
44
   @override
45
   Widget build(BuildContext context) {
45
   Widget build(BuildContext context) {
46
     final scope = ZefyrScope.of(context);
46
     final scope = ZefyrScope.of(context);
47
-
48
     if (scope.isEditable) {
47
     if (scope.isEditable) {
49
       ensureVisible(context, scope);
48
       ensureVisible(context, scope);
50
     }
49
     }
55
       content = buildEmbed(context, scope);
54
       content = buildEmbed(context, scope);
56
     } else {
55
     } else {
57
       assert(widget.style != null);
56
       assert(widget.style != null);
58
-
59
-      final text = ZefyrRichText(
57
+      content = ZefyrRichText(
60
         node: widget.node,
58
         node: widget.node,
61
         text: buildText(context),
59
         text: buildText(context),
62
       );
60
       );
63
-      if (scope.isEditable) {
64
-        content = EditableBox(
65
-          child: text,
66
-          node: widget.node,
67
-          layerLink: _link,
68
-          renderContext: scope.renderContext,
69
-          showCursor: scope.showCursor,
70
-          selection: scope.selection,
71
-          selectionColor: theme.selectionColor,
72
-        );
73
-        content = CompositedTransformTarget(link: _link, child: content);
74
-      } else {
75
-        content = text;
76
-      }
61
+    }
62
+
63
+    if (scope.isEditable) {
64
+      content = EditableBox(
65
+        child: content,
66
+        node: widget.node,
67
+        layerLink: _link,
68
+        renderContext: scope.renderContext,
69
+        showCursor: scope.showCursor,
70
+        selection: scope.selection,
71
+        selectionColor: theme.selectionColor,
72
+      );
73
+      content = CompositedTransformTarget(link: _link, child: content);
77
     }
74
     }
78
 
75
 
79
     if (widget.padding != null) {
76
     if (widget.padding != null) {
143
   }
140
   }
144
 
141
 
145
   Widget buildEmbed(BuildContext context, ZefyrScope scope) {
142
   Widget buildEmbed(BuildContext context, ZefyrScope scope) {
146
-    final theme = ZefyrTheme.of(context);
147
-
148
     EmbedNode node = widget.node.children.single;
143
     EmbedNode node = widget.node.children.single;
149
     EmbedAttribute embed = node.style.get(NotusAttribute.embed);
144
     EmbedAttribute embed = node.style.get(NotusAttribute.embed);
150
 
145
 
151
-    Widget result;
152
     if (embed.type == EmbedType.horizontalRule) {
146
     if (embed.type == EmbedType.horizontalRule) {
153
-      result = ZefyrHorizontalRule(node: node);
147
+      return ZefyrHorizontalRule(node: node);
154
     } else if (embed.type == EmbedType.image) {
148
     } else if (embed.type == EmbedType.image) {
155
-      result = ZefyrImage(node: node, delegate: scope.imageDelegate);
149
+      return ZefyrImage(node: node, delegate: scope.imageDelegate);
156
     } else {
150
     } else {
157
       throw new UnimplementedError('Unimplemented embed type ${embed.type}');
151
       throw new UnimplementedError('Unimplemented embed type ${embed.type}');
158
     }
152
     }
159
-
160
-    if (!scope.isEditable) {
161
-      return result;
162
-    }
163
-
164
-    return new EditableBox(
165
-      child: result,
166
-      node: widget.node,
167
-      layerLink: _link,
168
-      renderContext: scope.renderContext,
169
-      showCursor: scope.showCursor,
170
-      selection: scope.selection,
171
-      selectionColor: theme.selectionColor,
172
-    );
173
   }
153
   }
174
 }
154
 }

+ 1
- 0
packages/zefyr/lib/src/widgets/editable_box.dart 查看文件

102
   set showCursor(ValueNotifier<bool> value) {
102
   set showCursor(ValueNotifier<bool> value) {
103
     assert(value != null);
103
     assert(value != null);
104
     if (_showCursor == value) return;
104
     if (_showCursor == value) return;
105
+    // TODO: only mark to repaint if cursor is collapsed and inside this box.
105
     if (attached) _showCursor.removeListener(markNeedsPaint);
106
     if (attached) _showCursor.removeListener(markNeedsPaint);
106
     _showCursor = value;
107
     _showCursor = value;
107
     if (attached) _showCursor.addListener(markNeedsPaint);
108
     if (attached) _showCursor.addListener(markNeedsPaint);

+ 6
- 0
packages/zefyr/lib/src/widgets/scope.dart 查看文件

1
+import 'package:flutter/foundation.dart';
1
 import 'package:flutter/material.dart';
2
 import 'package:flutter/material.dart';
2
 import 'package:notus/notus.dart';
3
 import 'package:notus/notus.dart';
3
 
4
 
192
     }
193
     }
193
     notifyListeners();
194
     notifyListeners();
194
   }
195
   }
196
+
197
+  @override
198
+  String toString() {
199
+    return '$ZefyrScope#${shortHash(this)}';
200
+  }
195
 }
201
 }
196
 
202
 
197
 class ZefyrScopeAccess extends InheritedWidget {
203
 class ZefyrScopeAccess extends InheritedWidget {

+ 11
- 13
packages/zefyr/lib/src/widgets/selection.dart 查看文件

10
 
10
 
11
 import 'controller.dart';
11
 import 'controller.dart';
12
 import 'editable_box.dart';
12
 import 'editable_box.dart';
13
+import 'render_context.dart';
13
 import 'scope.dart';
14
 import 'scope.dart';
14
 
15
 
15
 RenderEditableBox _getEditableBox(HitTestResult result) {
16
 RenderEditableBox _getEditableBox(HitTestResult result) {
232
 
233
 
233
     RenderEditableProxyBox box = _getEditableBox(result);
234
     RenderEditableProxyBox box = _getEditableBox(result);
234
     if (box == null) {
235
     if (box == null) {
235
-      final scope = ZefyrScope.of(context);
236
-      box = scope.renderContext.closestBoxForGlobalPoint(globalPoint);
236
+      box = _editor.renderContext.closestBoxForGlobalPoint(globalPoint);
237
     }
237
     }
238
     if (box == null) return null;
238
     if (box == null) return null;
239
 
239
 
349
 
349
 
350
   @override
350
   @override
351
   Widget build(BuildContext context) {
351
   Widget build(BuildContext context) {
352
-    final scope = ZefyrScope.of(context);
353
     if (selection == null ||
352
     if (selection == null ||
354
         selection.isCollapsed ||
353
         selection.isCollapsed ||
355
         widget.controls == null ||
354
         widget.controls == null ||
356
-        scope.focusOwner != FocusOwner.editor) {
355
+        _scope.focusOwner != FocusOwner.editor) {
357
       return new Container();
356
       return new Container();
358
     }
357
     }
359
-    final block = scope.renderContext.boxForTextOffset(documentOffset);
358
+    final block = _scope.renderContext.boxForTextOffset(documentOffset);
360
     final position = getPosition(block);
359
     final position = getPosition(block);
361
     Widget handle;
360
     Widget handle;
362
     if (position == null) {
361
     if (position == null) {
400
   Offset _dragPosition;
399
   Offset _dragPosition;
401
 
400
 
402
   void _handleScopeChange() {
401
   void _handleScopeChange() {
403
-    setState(() {
404
-      _selection = _scope.selection;
405
-    });
402
+    if (_selection != _scope.selection) {
403
+      setState(() {
404
+        _selection = _scope.selection;
405
+      });
406
+    }
406
   }
407
   }
407
 
408
 
408
   void _handleDragStart(DragStartDetails details) {
409
   void _handleDragStart(DragStartDetails details) {
412
   void _handleDragUpdate(DragUpdateDetails details) {
413
   void _handleDragUpdate(DragUpdateDetails details) {
413
     _dragPosition += details.delta;
414
     _dragPosition += details.delta;
414
     final globalPoint = _dragPosition;
415
     final globalPoint = _dragPosition;
415
-    final scope = ZefyrScope.of(context);
416
-    final paragraph = scope.renderContext.boxForGlobalPoint(globalPoint);
416
+    final paragraph = _scope.renderContext.boxForGlobalPoint(globalPoint);
417
     if (paragraph == null) {
417
     if (paragraph == null) {
418
       return;
418
       return;
419
     }
419
     }
430
     }
430
     }
431
 
431
 
432
     if (newSelection != _selection) {
432
     if (newSelection != _selection) {
433
-      scope.updateSelection(newSelection, source: ChangeSource.local);
433
+      _scope.updateSelection(newSelection, source: ChangeSource.local);
434
     }
434
     }
435
   }
435
   }
436
 }
436
 }
468
       return Container();
468
       return Container();
469
     }
469
     }
470
     final boxes = block.getEndpointsForSelection(selection);
470
     final boxes = block.getEndpointsForSelection(selection);
471
-
472
     // Find the horizontal midpoint, just above the selected text.
471
     // Find the horizontal midpoint, just above the selected text.
473
     final Offset midpoint = new Offset(
472
     final Offset midpoint = new Offset(
474
       (boxes.length == 1)
473
       (boxes.length == 1)
481
       block.localToGlobal(Offset.zero),
480
       block.localToGlobal(Offset.zero),
482
       block.localToGlobal(block.size.bottomRight(Offset.zero)),
481
       block.localToGlobal(block.size.bottomRight(Offset.zero)),
483
     );
482
     );
484
-
485
     final toolbar = widget.controls
483
     final toolbar = widget.controls
486
         .buildToolbar(context, editingRegion, midpoint, widget.delegate);
484
         .buildToolbar(context, editingRegion, midpoint, widget.delegate);
487
     return new CompositedTransformFollower(
485
     return new CompositedTransformFollower(