Quellcode durchsuchen

Fixed analysis warnings in notus

Anatoly Pulyaevskiy vor 6 Jahren
Ursprung
Commit
5e876ef62b

+ 1
- 1
packages/notus/analysis_options.yaml Datei anzeigen

@@ -2,7 +2,7 @@ analyzer:
2 2
   strong-mode:
3 3
     implicit-casts: false
4 4
     implicit-dynamic: false
5
-    
5
+
6 6
 # Lint rules and documentation, see http://dart-lang.github.io/linter/lints
7 7
 linter:
8 8
   rules:

+ 4
- 4
packages/notus/lib/src/convert/markdown.dart Datei anzeigen

@@ -114,7 +114,7 @@ class _NotusMarkdownEncoder extends Converter<Delta, String> {
114 114
   String _writeLine(String text, NotusStyle style) {
115 115
     StringBuffer buffer = new StringBuffer();
116 116
     if (style.contains(NotusAttribute.heading)) {
117
-      _writeAttribute(buffer, style.get(NotusAttribute.heading));
117
+      _writeAttribute(buffer, style.get<int>(NotusAttribute.heading));
118 118
     }
119 119
 
120 120
     // Write the text itself
@@ -163,11 +163,11 @@ class _NotusMarkdownEncoder extends Converter<Delta, String> {
163 163
     } else if (attribute == NotusAttribute.italic) {
164 164
       _writeItalicTag(buffer);
165 165
     } else if (attribute.key == NotusAttribute.link.key) {
166
-      _writeLinkTag(buffer, attribute, close: close);
166
+      _writeLinkTag(buffer, attribute as NotusAttribute<String>, close: close);
167 167
     } else if (attribute.key == NotusAttribute.heading.key) {
168
-      _writeHeadingTag(buffer, attribute);
168
+      _writeHeadingTag(buffer, attribute as NotusAttribute<int>);
169 169
     } else if (attribute.key == NotusAttribute.block.key) {
170
-      _writeBlockTag(buffer, attribute, close: close);
170
+      _writeBlockTag(buffer, attribute as NotusAttribute<String>, close: close);
171 171
     } else {
172 172
       throw new ArgumentError('Cannot handle $attribute');
173 173
     }

+ 1
- 1
packages/notus/lib/src/document.dart Datei anzeigen

@@ -44,7 +44,7 @@ class NotusDocument {
44 44
     _loadDocument(_delta);
45 45
   }
46 46
 
47
-  NotusDocument.fromJson(dynamic data)
47
+  NotusDocument.fromJson(List data)
48 48
       : _heuristics = NotusHeuristics.fallback,
49 49
         _delta = Delta.fromJson(data) {
50 50
     _loadDocument(_delta);

+ 9
- 9
packages/notus/lib/src/document/attributes.dart Datei anzeigen

@@ -122,7 +122,7 @@ class NotusAttribute<T> implements NotusAttributeBuilder<T> {
122 122
   /// Embed style attribute.
123 123
   static const embed = const EmbedAttributeBuilder._();
124 124
 
125
-  factory NotusAttribute._fromKeyValue(String key, T value) {
125
+  static NotusAttribute _fromKeyValue(String key, dynamic value) {
126 126
     if (!_registry.containsKey(key))
127 127
       throw new ArgumentError.value(
128 128
           key, 'No attribute with key "$key" registered.');
@@ -166,7 +166,7 @@ class NotusAttribute<T> implements NotusAttributeBuilder<T> {
166 166
       new NotusAttribute<T>._(key, scope, value);
167 167
 
168 168
   @override
169
-  bool operator ==(other) {
169
+  bool operator ==(Object other) {
170 170
     if (identical(this, other)) return true;
171 171
     if (other is! NotusAttribute<T>) return false;
172 172
     NotusAttribute<T> typedOther = other;
@@ -190,11 +190,11 @@ class NotusStyle {
190 190
 
191 191
   final Map<String, NotusAttribute> _data;
192 192
 
193
-  static NotusStyle fromJson(Map data) {
193
+  static NotusStyle fromJson(Map<String, dynamic> data) {
194 194
     if (data == null) return new NotusStyle();
195 195
 
196
-    final result = data.map((key, value) {
197
-      var attr = new NotusAttribute._fromKeyValue(key, value);
196
+    final result = data.map((String key, dynamic value) {
197
+      var attr = NotusAttribute._fromKeyValue(key, value);
198 198
       return new MapEntry<String, NotusAttribute>(key, attr);
199 199
     });
200 200
     return new NotusStyle._(result);
@@ -228,7 +228,7 @@ class NotusStyle {
228 228
   /// [attribute].
229 229
   bool containsSame(NotusAttribute attribute) {
230 230
     assert(attribute != null);
231
-    return get(attribute) == attribute;
231
+    return get<dynamic>(attribute) == attribute;
232 232
   }
233 233
 
234 234
   /// Returns value of specified attribute [key] in this set.
@@ -290,11 +290,11 @@ class NotusStyle {
290 290
   /// Returns JSON-serializable representation of this style.
291 291
   Map<String, dynamic> toJson() => _data.isEmpty
292 292
       ? null
293
-      : _data.map(
294
-          (_, value) => new MapEntry<String, dynamic>(value.key, value.value));
293
+      : _data.map<String, dynamic>((String _, NotusAttribute value) =>
294
+          new MapEntry<String, dynamic>(value.key, value.value));
295 295
 
296 296
   @override
297
-  bool operator ==(other) {
297
+  bool operator ==(Object other) {
298 298
     if (identical(this, other)) return true;
299 299
     if (other is! NotusStyle) return false;
300 300
     NotusStyle typedOther = other;

+ 1
- 1
packages/notus/lib/src/document/block.dart Datei anzeigen

@@ -40,7 +40,7 @@ class BlockNode extends ContainerNode<LineNode>
40 40
       while (child != line) {
41 41
         child.unlink();
42 42
         before.add(child);
43
-        child = this.first;
43
+        child = this.first as LineNode;
44 44
       }
45 45
       line.unlink();
46 46
       insertBefore(line);

+ 2
- 2
packages/notus/lib/src/document/leaf.dart Datei anzeigen

@@ -52,7 +52,7 @@ abstract class LeafNode extends Node
52 52
     assert(index >= 0 && index <= length);
53 53
     if (index == 0) return this;
54 54
     if (index == length && isLast) return null;
55
-    if (index == length && !isLast) return next;
55
+    if (index == length && !isLast) return next as LeafNode;
56 56
 
57 57
     String text = _value;
58 58
     _value = text.substring(0, index);
@@ -124,7 +124,7 @@ abstract class LeafNode extends Node
124 124
   }
125 125
 
126 126
   @override
127
-  LineNode get parent => super.parent;
127
+  LineNode get parent => super.parent as LineNode;
128 128
 
129 129
   @override
130 130
   int get length => _value.length;

+ 5
- 3
packages/notus/lib/src/document/line.dart Datei anzeigen

@@ -34,13 +34,15 @@ class LineNode extends ContainerNode<LeafNode>
34 34
     if (isLast) {
35 35
       if (parent is BlockNode) {
36 36
         if (parent.isLast) return null;
37
-        return (parent.next is BlockNode)
37
+        LineNode line = (parent.next is BlockNode)
38 38
             ? (parent.next as BlockNode).first
39 39
             : parent.next;
40
+        return line;
40 41
       } else
41 42
         return null;
42 43
     } else {
43
-      return (next is BlockNode) ? (next as BlockNode).first : next;
44
+      LineNode line = (next is BlockNode) ? (next as BlockNode).first : next;
45
+      return line;
44 46
     }
45 47
   }
46 48
 
@@ -129,7 +131,7 @@ class LineNode extends ContainerNode<LeafNode>
129 131
       result = result.mergeAll(node.style);
130 132
       int pos = node.length - data.offset;
131 133
       while (!node.isLast && pos < local) {
132
-        node = node.next;
134
+        node = node.next as LeafNode;
133 135
         _handle(node.style);
134 136
         pos += node.length;
135 137
       }

+ 1
- 1
packages/notus/lib/src/document/node.dart Datei anzeigen

@@ -238,7 +238,7 @@ abstract class ContainerNode<T extends Node> extends Node {
238 238
 
239 239
     if (isEmpty) {
240 240
       assert(index == 0);
241
-      Node node = defaultChild;
241
+      T node = defaultChild;
242 242
       add(node);
243 243
       node.insert(index, value, style);
244 244
     } else {

+ 2
- 1
packages/notus/lib/src/heuristics/delete_rules.dart Datei anzeigen

@@ -69,7 +69,8 @@ class PreserveLineStyleOnMergeRule extends DeleteRule {
69 69
 
70 70
   Map<String, dynamic> _unsetAttributes(Map<String, dynamic> attributes) {
71 71
     if (attributes == null) return null;
72
-    return attributes.map((key, value) => new MapEntry(key, null));
72
+    return attributes.map<String, dynamic>((String key, dynamic value) =>
73
+        new MapEntry<String, dynamic>(key, null));
73 74
   }
74 75
 }
75 76
 

+ 3
- 3
packages/notus/lib/src/heuristics/insert_rules.dart Datei anzeigen

@@ -87,7 +87,7 @@ class ResetLineFormatOnNewLineRule extends InsertRule {
87 87
     final target = iter.next();
88 88
 
89 89
     if (target.data.startsWith('\n')) {
90
-      Map<String, dynamic> resetStyle = null;
90
+      Map<String, dynamic> resetStyle;
91 91
       if (target.attributes != null &&
92 92
           target.attributes.containsKey(NotusAttribute.heading.key)) {
93 93
         resetStyle = NotusAttribute.heading.unset.toJson();
@@ -290,8 +290,8 @@ class PreserveBlockStyleOnPasteRule extends InsertRule {
290 290
       }
291 291
     }
292 292
 
293
-    Map<String, dynamic> resetStyle = null;
294
-    Map<String, dynamic> blockStyle = null;
293
+    Map<String, dynamic> resetStyle;
294
+    Map<String, dynamic> blockStyle;
295 295
     if (lineStyle != null) {
296 296
       if (lineStyle.containsKey(NotusAttribute.heading.key)) {
297 297
         resetStyle = NotusAttribute.heading.unset.toJson();

+ 1
- 1
packages/notus/test/convert/markdown_test.dart Datei anzeigen

@@ -141,7 +141,7 @@ void main() {
141 141
 
142 142
 final doc =
143 143
     r'[{"insert":"Zefyr"},{"insert":"\n","attributes":{"heading":1}},{"insert":"Soft and gentle rich text editing for Flutter applications.","attributes":{"i":true}},{"insert":"\nZefyr is an "},{"insert":"early preview","attributes":{"b":true}},{"insert":" open source library.\nDocumentation"},{"insert":"\n","attributes":{"heading":3}},{"insert":"Quick Start"},{"insert":"\n","attributes":{"block":"ul"}},{"insert":"Data format and Document Model"},{"insert":"\n","attributes":{"block":"ul"}},{"insert":"Style attributes"},{"insert":"\n","attributes":{"block":"ul"}},{"insert":"Heuristic rules"},{"insert":"\n","attributes":{"block":"ul"}},{"insert":"Clean and modern look"},{"insert":"\n","attributes":{"heading":2}},{"insert":"Zefyr’s rich text editor is built with simplicity and flexibility in mind. It provides clean interface for distraction-free editing. Think Medium.com-like experience.\nimport ‘package:flutter/material.dart’;"},{"insert":"\n","attributes":{"block":"code"}},{"insert":"import ‘package:notus/notus.dart’;"},{"insert":"\n\n","attributes":{"block":"code"}},{"insert":"void main() {"},{"insert":"\n","attributes":{"block":"code"}},{"insert":" print(“Hello world!”);"},{"insert":"\n","attributes":{"block":"code"}},{"insert":"}"},{"insert":"\n","attributes":{"block":"code"}}]';
144
-final delta = Delta.fromJson(json.decode(doc));
144
+final delta = Delta.fromJson(json.decode(doc) as List);
145 145
 
146 146
 final expectedMarkdown = '''
147 147
 # Zefyr

+ 1
- 1
packages/notus/test/document/attributes_test.dart Datei anzeigen

@@ -7,7 +7,7 @@ import 'package:notus/notus.dart';
7 7
 void main() {
8 8
   group('$NotusStyle', () {
9 9
     test('get', () {
10
-      var attrs = NotusStyle.fromJson({'block': 'ul'});
10
+      var attrs = NotusStyle.fromJson(<String, dynamic>{'block': 'ul'});
11 11
       var attr = attrs.get(NotusAttribute.block);
12 12
       expect(attr, NotusAttribute.ul);
13 13
     });

+ 1
- 1
packages/notus/test/document_test.dart Datei anzeigen

@@ -46,7 +46,7 @@ void main() {
46 46
     test('json serialization', () {
47 47
       final original = dartconfDoc();
48 48
       final jsonData = json.encode(original);
49
-      final doc = NotusDocument.fromJson(json.decode(jsonData));
49
+      final doc = NotusDocument.fromJson(json.decode(jsonData) as List);
50 50
       expect(doc.toDelta(), original.toDelta());
51 51
       expect(json.encode(doc), jsonData);
52 52
     });

+ 26
- 7
packages/zefyr/lib/src/widgets/scope.dart Datei anzeigen

@@ -4,15 +4,34 @@ import 'package:notus/notus.dart';
4 4
 
5 5
 import 'controller.dart';
6 6
 import 'cursor_timer.dart';
7
+import 'editor.dart';
7 8
 import 'image.dart';
8 9
 import 'render_context.dart';
9
-
10
+import 'view.dart';
11
+
12
+/// Provides access to shared state of [ZefyrEditor] or [ZefyrView].
13
+///
14
+/// A scope object can be created by an editable widget like [ZefyrEditor] in
15
+/// which case it provides access to editing state, including focus nodes,
16
+/// selection and such. Editable scope can be created using
17
+/// [ZefyrScope.editable] constructor.
18
+///
19
+/// If a scope object is created by a view-only widget like [ZefyrView] then
20
+/// it only provides access to [imageDelegate].
21
+///
22
+/// Can be retrieved using [ZefyrScope.of].
10 23
 class ZefyrScope extends ChangeNotifier {
24
+  /// Creates a view-only scope.
25
+  ///
26
+  /// Normally used in [ZefyrView].
11 27
   ZefyrScope.view({@required ZefyrImageDelegate imageDelegate})
12 28
       : assert(imageDelegate != null),
13 29
         isEditable = false,
14 30
         _imageDelegate = imageDelegate;
15 31
 
32
+  /// Creates editable scope.
33
+  ///
34
+  /// Normally used in [ZefyrEditor].
16 35
   ZefyrScope.editable({
17 36
     @required ZefyrController controller,
18 37
     @required ZefyrImageDelegate imageDelegate,
@@ -35,6 +54,12 @@ class ZefyrScope extends ChangeNotifier {
35 54
     _focusNode.addListener(_handleFocusChange);
36 55
   }
37 56
 
57
+  static ZefyrScope of(BuildContext context) {
58
+    final ZefyrScopeAccess widget =
59
+        context.inheritFromWidgetOfExactType(ZefyrScopeAccess);
60
+    return widget.scope;
61
+  }
62
+
38 63
   ZefyrImageDelegate _imageDelegate;
39 64
   ZefyrImageDelegate get imageDelegate => _imageDelegate;
40 65
   set imageDelegate(ZefyrImageDelegate value) {
@@ -107,12 +132,6 @@ class ZefyrScope extends ChangeNotifier {
107 132
   /// objects which are not dependent on editing flow, e.g. [imageDelegate].
108 133
   final bool isEditable;
109 134
 
110
-  static ZefyrScope of(BuildContext context) {
111
-    final ZefyrScopeAccess widget =
112
-        context.inheritFromWidgetOfExactType(ZefyrScopeAccess);
113
-    return widget.scope;
114
-  }
115
-
116 135
   set toolbarFocusNode(FocusNode node) {
117 136
     assert(isEditable);
118 137
     assert(!_disposed || node == null);