Browse Source

Fixed analysis warnings in notus

Anatoly Pulyaevskiy 6 years ago
parent
commit
5e876ef62b

+ 1
- 1
packages/notus/analysis_options.yaml View File

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

+ 4
- 4
packages/notus/lib/src/convert/markdown.dart View File

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

+ 1
- 1
packages/notus/lib/src/document.dart View File

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

+ 9
- 9
packages/notus/lib/src/document/attributes.dart View File

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

+ 1
- 1
packages/notus/lib/src/document/block.dart View File

40
       while (child != line) {
40
       while (child != line) {
41
         child.unlink();
41
         child.unlink();
42
         before.add(child);
42
         before.add(child);
43
-        child = this.first;
43
+        child = this.first as LineNode;
44
       }
44
       }
45
       line.unlink();
45
       line.unlink();
46
       insertBefore(line);
46
       insertBefore(line);

+ 2
- 2
packages/notus/lib/src/document/leaf.dart View File

52
     assert(index >= 0 && index <= length);
52
     assert(index >= 0 && index <= length);
53
     if (index == 0) return this;
53
     if (index == 0) return this;
54
     if (index == length && isLast) return null;
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
     String text = _value;
57
     String text = _value;
58
     _value = text.substring(0, index);
58
     _value = text.substring(0, index);
124
   }
124
   }
125
 
125
 
126
   @override
126
   @override
127
-  LineNode get parent => super.parent;
127
+  LineNode get parent => super.parent as LineNode;
128
 
128
 
129
   @override
129
   @override
130
   int get length => _value.length;
130
   int get length => _value.length;

+ 5
- 3
packages/notus/lib/src/document/line.dart View File

34
     if (isLast) {
34
     if (isLast) {
35
       if (parent is BlockNode) {
35
       if (parent is BlockNode) {
36
         if (parent.isLast) return null;
36
         if (parent.isLast) return null;
37
-        return (parent.next is BlockNode)
37
+        LineNode line = (parent.next is BlockNode)
38
             ? (parent.next as BlockNode).first
38
             ? (parent.next as BlockNode).first
39
             : parent.next;
39
             : parent.next;
40
+        return line;
40
       } else
41
       } else
41
         return null;
42
         return null;
42
     } else {
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
       result = result.mergeAll(node.style);
131
       result = result.mergeAll(node.style);
130
       int pos = node.length - data.offset;
132
       int pos = node.length - data.offset;
131
       while (!node.isLast && pos < local) {
133
       while (!node.isLast && pos < local) {
132
-        node = node.next;
134
+        node = node.next as LeafNode;
133
         _handle(node.style);
135
         _handle(node.style);
134
         pos += node.length;
136
         pos += node.length;
135
       }
137
       }

+ 1
- 1
packages/notus/lib/src/document/node.dart View File

238
 
238
 
239
     if (isEmpty) {
239
     if (isEmpty) {
240
       assert(index == 0);
240
       assert(index == 0);
241
-      Node node = defaultChild;
241
+      T node = defaultChild;
242
       add(node);
242
       add(node);
243
       node.insert(index, value, style);
243
       node.insert(index, value, style);
244
     } else {
244
     } else {

+ 2
- 1
packages/notus/lib/src/heuristics/delete_rules.dart View File

69
 
69
 
70
   Map<String, dynamic> _unsetAttributes(Map<String, dynamic> attributes) {
70
   Map<String, dynamic> _unsetAttributes(Map<String, dynamic> attributes) {
71
     if (attributes == null) return null;
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 View File

87
     final target = iter.next();
87
     final target = iter.next();
88
 
88
 
89
     if (target.data.startsWith('\n')) {
89
     if (target.data.startsWith('\n')) {
90
-      Map<String, dynamic> resetStyle = null;
90
+      Map<String, dynamic> resetStyle;
91
       if (target.attributes != null &&
91
       if (target.attributes != null &&
92
           target.attributes.containsKey(NotusAttribute.heading.key)) {
92
           target.attributes.containsKey(NotusAttribute.heading.key)) {
93
         resetStyle = NotusAttribute.heading.unset.toJson();
93
         resetStyle = NotusAttribute.heading.unset.toJson();
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
     if (lineStyle != null) {
295
     if (lineStyle != null) {
296
       if (lineStyle.containsKey(NotusAttribute.heading.key)) {
296
       if (lineStyle.containsKey(NotusAttribute.heading.key)) {
297
         resetStyle = NotusAttribute.heading.unset.toJson();
297
         resetStyle = NotusAttribute.heading.unset.toJson();

+ 1
- 1
packages/notus/test/convert/markdown_test.dart View File

141
 
141
 
142
 final doc =
142
 final doc =
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"}}]';
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
 final expectedMarkdown = '''
146
 final expectedMarkdown = '''
147
 # Zefyr
147
 # Zefyr

+ 1
- 1
packages/notus/test/document/attributes_test.dart View File

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

+ 1
- 1
packages/notus/test/document_test.dart View File

46
     test('json serialization', () {
46
     test('json serialization', () {
47
       final original = dartconfDoc();
47
       final original = dartconfDoc();
48
       final jsonData = json.encode(original);
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
       expect(doc.toDelta(), original.toDelta());
50
       expect(doc.toDelta(), original.toDelta());
51
       expect(json.encode(doc), jsonData);
51
       expect(json.encode(doc), jsonData);
52
     });
52
     });

+ 26
- 7
packages/zefyr/lib/src/widgets/scope.dart View File

4
 
4
 
5
 import 'controller.dart';
5
 import 'controller.dart';
6
 import 'cursor_timer.dart';
6
 import 'cursor_timer.dart';
7
+import 'editor.dart';
7
 import 'image.dart';
8
 import 'image.dart';
8
 import 'render_context.dart';
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
 class ZefyrScope extends ChangeNotifier {
23
 class ZefyrScope extends ChangeNotifier {
24
+  /// Creates a view-only scope.
25
+  ///
26
+  /// Normally used in [ZefyrView].
11
   ZefyrScope.view({@required ZefyrImageDelegate imageDelegate})
27
   ZefyrScope.view({@required ZefyrImageDelegate imageDelegate})
12
       : assert(imageDelegate != null),
28
       : assert(imageDelegate != null),
13
         isEditable = false,
29
         isEditable = false,
14
         _imageDelegate = imageDelegate;
30
         _imageDelegate = imageDelegate;
15
 
31
 
32
+  /// Creates editable scope.
33
+  ///
34
+  /// Normally used in [ZefyrEditor].
16
   ZefyrScope.editable({
35
   ZefyrScope.editable({
17
     @required ZefyrController controller,
36
     @required ZefyrController controller,
18
     @required ZefyrImageDelegate imageDelegate,
37
     @required ZefyrImageDelegate imageDelegate,
35
     _focusNode.addListener(_handleFocusChange);
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
   ZefyrImageDelegate _imageDelegate;
63
   ZefyrImageDelegate _imageDelegate;
39
   ZefyrImageDelegate get imageDelegate => _imageDelegate;
64
   ZefyrImageDelegate get imageDelegate => _imageDelegate;
40
   set imageDelegate(ZefyrImageDelegate value) {
65
   set imageDelegate(ZefyrImageDelegate value) {
107
   /// objects which are not dependent on editing flow, e.g. [imageDelegate].
132
   /// objects which are not dependent on editing flow, e.g. [imageDelegate].
108
   final bool isEditable;
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
   set toolbarFocusNode(FocusNode node) {
135
   set toolbarFocusNode(FocusNode node) {
117
     assert(isEditable);
136
     assert(isEditable);
118
     assert(!_disposed || node == null);
137
     assert(!_disposed || node == null);