Browse Source

fix selection link

lucky1213 4 years ago
parent
commit
8221c32e53

+ 26
- 24
packages/notus/lib/src/heuristics/insert_rules.dart View File

156
     if (previous == null || previous.data.contains('\n')) return null;
156
     if (previous == null || previous.data.contains('\n')) return null;
157
 
157
 
158
     final attributes = previous.attributes;
158
     final attributes = previous.attributes;
159
+    final hasHighLight = (attributes != null && attributes.containsKey(NotusAttribute.highlight.key));
160
+    
159
     final hasLink =
161
     final hasLink =
160
         (attributes != null && attributes.containsKey(NotusAttribute.link.key));
162
         (attributes != null && attributes.containsKey(NotusAttribute.link.key));
161
-    final hasHighLight = (attributes != null && attributes.containsKey(NotusAttribute.highlight.key));
162
-    if (hasHighLight) {
163
-      attributes.remove(NotusAttribute.highlight.key);
164
-    }
165
-    if (!hasLink) {
163
+    if (!hasLink && !hasHighLight) {
166
       return Delta()
164
       return Delta()
167
         ..retain(index)
165
         ..retain(index)
168
         ..insert(text, attributes);
166
         ..insert(text, attributes);
172
     // Link style should NOT be preserved on the boundaries.
170
     // Link style should NOT be preserved on the boundaries.
173
     var noLinkAttributes = previous.attributes;
171
     var noLinkAttributes = previous.attributes;
174
     noLinkAttributes.remove(NotusAttribute.link.key);
172
     noLinkAttributes.remove(NotusAttribute.link.key);
173
+    noLinkAttributes.remove(NotusAttribute.highlight.key);
174
+    
175
     final noLinkResult = Delta()
175
     final noLinkResult = Delta()
176
       ..retain(index)
176
       ..retain(index)
177
       ..insert(text, noLinkAttributes.isEmpty ? null : noLinkAttributes);
177
       ..insert(text, noLinkAttributes.isEmpty ? null : noLinkAttributes);
178
-    final next = iter.next();
179
-    if (next == null) {
180
-      // Nothing after us, we are not inside link-styled fragment.
181
-      return noLinkResult;
182
-    }
183
-    final nextAttributes = next.attributes ?? <String, dynamic>{};
184
-    if (!nextAttributes.containsKey(NotusAttribute.link.key)) {
185
-      // Next fragment is not styled as link.
186
-      return noLinkResult;
187
-    }
188
-    // We must make sure links are identical in previous and next operations.
189
-    if (attributes[NotusAttribute.link.key] ==
190
-        nextAttributes[NotusAttribute.link.key]) {
191
-      return Delta()
192
-        ..retain(index)
193
-        ..insert(text, attributes);
194
-    } else {
195
-      return noLinkResult;
196
-    }
178
+    return noLinkResult;
179
+
180
+    // final next = iter.next();
181
+    // if (next == null) {
182
+    //   // Nothing after us, we are not inside link-styled fragment.
183
+    //   return noLinkResult;
184
+    // }
185
+    // final nextAttributes = next.attributes ?? <String, dynamic>{};
186
+    // if (!nextAttributes.containsKey(NotusAttribute.link.key)) {
187
+    //   // Next fragment is not styled as link.
188
+    //   return noLinkResult;
189
+    // }
190
+    // // We must make sure links are identical in previous and next operations.
191
+    // if (attributes[NotusAttribute.link.key] ==
192
+    //     nextAttributes[NotusAttribute.link.key]) {
193
+    //   return Delta()
194
+    //     ..retain(index)
195
+    //     ..insert(text, attributes);
196
+    // } else {
197
+    //   return noLinkResult;
198
+    // }
197
   }
199
   }
198
 }
200
 }
199
 
201
 

+ 2
- 3
packages/zefyr/example/lib/src/images.dart View File

111
                         Navigator.pop(
111
                         Navigator.pop(
112
                             buildContext,
112
                             buildContext,
113
                             ZefyrLinkEntity(
113
                             ZefyrLinkEntity(
114
-                              text:
115
-                                  '测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用测试用',
116
-                              url: _controller.text,
114
+                              text: _controller.text,
115
+                              url: _controller2.text,
117
                             ));
116
                             ));
118
                       },
117
                       },
119
                       child: Text('确定'),
118
                       child: Text('确定'),

+ 26
- 3
packages/zefyr/lib/src/widgets/rich_text.dart View File

115
     return super.getPositionForOffset(offset);
115
     return super.getPositionForOffset(offset);
116
   }
116
   }
117
 
117
 
118
-  TextRange getRenderBoxWordBoundary(int length) {
118
+  bool getPositionHasRenderBox(TextPosition position, int length) {
119
+    if (node.length == position.offset + 1) {
120
+      return false;
121
+    }
122
+    return true;
123
+  }
124
+
125
+  int getLinkWordBoundaryStart(TextPosition position, int length) {
126
+    var wordStart = position.offset;
127
+    var hasLink = node.length < (position.offset + length) ? true : false;
128
+    if (position.offset == 0 || !hasLink) {
129
+      return wordStart;
130
+    } else {
131
+      var newPosition = TextPosition(
132
+        offset: position.offset - 1,
133
+        affinity: position.affinity,
134
+      );
135
+      return getLinkWordBoundaryStart(newPosition, length);
136
+    }
137
+  }
138
+
139
+  TextRange getLinkWordBoundary(TextPosition position, int length) {
140
+    final localRange = super.getWordBoundary(position);
141
+    int start = getLinkWordBoundaryStart(position, length);
119
     return TextRange(
142
     return TextRange(
120
-      start: node.documentOffset + node.length - length - 1,
121
-      end: node.documentOffset + node.length - 1,
143
+      start: node.documentOffset + localRange.start,
144
+      end: node.documentOffset + localRange.end,
122
     );
145
     );
123
   }
146
   }
124
 
147
 

+ 15
- 11
packages/zefyr/lib/src/widgets/selection.dart View File

269
         var spanPosition = paragraph.getRenderBoxPositionForOffset(localPoint);
269
         var spanPosition = paragraph.getRenderBoxPositionForOffset(localPoint);
270
         // // 第一个字符和最后一个字符将不触发onTap
270
         // // 第一个字符和最后一个字符将不触发onTap
271
         final TextSpan span = paragraph.text.getSpanForPosition(TextPosition(
271
         final TextSpan span = paragraph.text.getSpanForPosition(TextPosition(
272
-          offset: spanPosition.offset == 0 ? spanPosition.offset - 1 : spanPosition.offset + 1,
272
+          offset: spanPosition.offset,
273
           affinity: spanPosition.affinity,
273
           affinity: spanPosition.affinity,
274
         ));
274
         ));
275
-        final recognizer = (span?.recognizer as TapGestureRecognizer);
276
-        if (recognizer != null) {
277
-          print('当前点击的是link');
278
-          // 关闭键盘 触发tap,改变光标,但不弹出键盘
279
-          _scope.closeKeyboard();
280
-          recognizer.onTap();
281
-          _scope.controller.updateSelection(selection, source: ChangeSource.remote);
282
-          return;
275
+        if (span != null) {
276
+          if (paragraph.getPositionHasRenderBox(spanPosition, span.text.length)) {
277
+            final recognizer = (span?.recognizer as TapGestureRecognizer);
278
+            if (recognizer != null) {
279
+              print('当前点击的是link');
280
+              // 关闭键盘 触发tap,改变光标,但不弹出键盘
281
+              _scope.closeKeyboard();
282
+              recognizer.onTap();
283
+              _scope.controller.updateSelection(selection, source: ChangeSource.remote);
284
+              return;
285
+            }
286
+          }
283
         }
287
         }
284
       }
288
       }
285
     } else if (box?.child is RenderEditableImage) {
289
     } else if (box?.child is RenderEditableImage) {
328
     //     var spanPosition = paragraph.getRenderBoxPositionForOffset(localPoint);
332
     //     var spanPosition = paragraph.getRenderBoxPositionForOffset(localPoint);
329
     //     // // 第一个字符和最后一个字符将不触发onTap
333
     //     // // 第一个字符和最后一个字符将不触发onTap
330
     //     final TextSpan span = paragraph.text.getSpanForPosition(TextPosition(
334
     //     final TextSpan span = paragraph.text.getSpanForPosition(TextPosition(
331
-    //       offset: spanPosition.offset == 0 ? spanPosition.offset - 1 : spanPosition.offset + 1,
335
+    //       offset: spanPosition.offset,
332
     //       affinity: spanPosition.affinity,
336
     //       affinity: spanPosition.affinity,
333
     //     ));
337
     //     ));
334
     //     if (span != null) {
338
     //     if (span != null) {
335
-    //       word = paragraph.getRenderBoxWordBoundary(span.text.length);
339
+    //       word = paragraph.getLinkWordBoundary(spanPosition, span.text.length);
336
     //     }
340
     //     }
337
     //   }
341
     //   }
338
     // }
342
     // }