ソースを参照

Updated editable to use new ZefyrScope

Anatoly Pulyaevskiy 6 年 前
コミット
43e68d44a9

+ 16
- 0
packages/zefyr/example/lib/src/full_page.dart ファイルの表示

@@ -1,3 +1,4 @@
1
+import 'dart:async';
1 2
 import 'dart:convert';
2 3
 
3 4
 import 'package:flutter/material.dart';
@@ -39,6 +40,21 @@ class _FullPageEditorScreenState extends State<FullPageEditorScreen> {
39 40
       ZefyrController(NotusDocument.fromDelta(getDelta()));
40 41
   final FocusNode _focusNode = new FocusNode();
41 42
   bool _editing = false;
43
+  StreamSubscription<NotusChange> _sub;
44
+
45
+  @override
46
+  void initState() {
47
+    super.initState();
48
+    _sub = _controller.document.changes.listen((change) {
49
+      print('${change.source}: ${change.change}');
50
+    });
51
+  }
52
+
53
+  @override
54
+  void dispose() {
55
+    _sub.cancel();
56
+    super.dispose();
57
+  }
42 58
 
43 59
   @override
44 60
   Widget build(BuildContext context) {

+ 22
- 7
packages/zefyr/lib/src/widgets/editable_text.dart ファイルの表示

@@ -18,11 +18,14 @@ import 'list.dart';
18 18
 import 'paragraph.dart';
19 19
 import 'quote.dart';
20 20
 import 'render_context.dart';
21
+import 'scope.dart';
21 22
 import 'selection.dart';
23
+import 'theme.dart';
22 24
 
23 25
 /// Core widget responsible for editing Zefyr documents.
24 26
 ///
25
-/// Depends on presence of [ZefyrTheme] somewhere up the widget tree.
27
+/// Depends on presence of [ZefyrTheme] and [ZefyrScope] somewhere up the
28
+/// widget tree.
26 29
 ///
27 30
 /// Consider using [ZefyrEditor] which wraps this widget and adds a toolbar to
28 31
 /// edit style attributes.
@@ -95,8 +98,6 @@ class _ZefyrEditableTextState extends State<ZefyrEditableText>
95 98
 
96 99
   /// Current text selection.
97 100
   TextSelection get selection => widget.controller.selection;
98
-  ZefyrRenderContext get renderContext => _renderContext;
99
-  ValueNotifier<bool> get showCursor => _cursorTimer.value;
100 101
 
101 102
   /// Express interest in interacting with the keyboard.
102 103
   ///
@@ -165,6 +166,22 @@ class _ZefyrEditableTextState extends State<ZefyrEditableText>
165 166
     }
166 167
   }
167 168
 
169
+  @override
170
+  void didChangeDependencies() {
171
+    super.didChangeDependencies();
172
+    final scope = ZefyrScope.of(context);
173
+    if (_renderContext != scope.renderContext) {
174
+      _renderContext?.removeListener(_handleRenderContextChange);
175
+      _renderContext = scope.renderContext;
176
+      _renderContext.addListener(_handleRenderContextChange);
177
+    }
178
+    if (_cursorTimer != scope.cursorTimer) {
179
+      _cursorTimer?.stop();
180
+      _cursorTimer = scope.cursorTimer;
181
+      _cursorTimer.startOrStop(focusNode, selection);
182
+    }
183
+  }
184
+
168 185
   @override
169 186
   void dispose() {
170 187
     _cancelSubscriptions();
@@ -183,8 +200,8 @@ class _ZefyrEditableTextState extends State<ZefyrEditableText>
183 200
   //
184 201
 
185 202
   final ScrollController _scrollController = ScrollController();
186
-  final ZefyrRenderContext _renderContext = ZefyrRenderContext();
187
-  final CursorTimer _cursorTimer = CursorTimer();
203
+  ZefyrRenderContext _renderContext;
204
+  CursorTimer _cursorTimer;
188 205
   InputConnectionController _input;
189 206
   bool _didAutoFocus = false;
190 207
 
@@ -223,7 +240,6 @@ class _ZefyrEditableTextState extends State<ZefyrEditableText>
223 240
 
224 241
   void _updateSubscriptions([ZefyrEditableText oldWidget]) {
225 242
     if (oldWidget == null) {
226
-      _renderContext.addListener(_handleRenderContextChange);
227 243
       widget.controller.addListener(_handleLocalValueChange);
228 244
       focusNode.addListener(_handleFocusChange);
229 245
       return;
@@ -243,7 +259,6 @@ class _ZefyrEditableTextState extends State<ZefyrEditableText>
243 259
 
244 260
   void _cancelSubscriptions() {
245 261
     _renderContext.removeListener(_handleRenderContextChange);
246
-    _renderContext.dispose();
247 262
     widget.controller.removeListener(_handleLocalValueChange);
248 263
     focusNode.removeListener(_handleFocusChange);
249 264
     _input.closeConnection();

+ 3
- 3
packages/zefyr/lib/src/widgets/editor.dart ファイルの表示

@@ -144,9 +144,9 @@ class _ZefyrEditorState extends State<ZefyrEditor> {
144 144
   @override
145 145
   Widget build(BuildContext context) {
146 146
     Widget editable = new ZefyrEditableText(
147
-      controller: widget.controller,
148
-      focusNode: widget.focusNode,
149
-      imageDelegate: _imageDelegate,
147
+      controller: _scope.controller,
148
+      focusNode: _scope.focusNode,
149
+      imageDelegate: _scope.imageDelegate,
150 150
       autofocus: widget.autofocus,
151 151
       enabled: widget.enabled,
152 152
       padding: widget.padding,

+ 1
- 1
packages/zefyr/lib/src/widgets/scope.dart ファイルの表示

@@ -176,7 +176,7 @@ class ZefyrScope extends ChangeNotifier {
176 176
     final selection = _controller.selection;
177 177
     if (_selectionStyle != attrs || _selection != selection) {
178 178
       _selectionStyle = attrs;
179
-      _selection = _controller.selection;
179
+      _selection = selection;
180 180
       notifyListeners();
181 181
     }
182 182
   }

+ 20
- 1
packages/zefyr/lib/src/widgets/selection.dart ファイルの表示

@@ -294,6 +294,8 @@ class SelectionHandleDriver extends StatefulWidget {
294 294
 }
295 295
 
296 296
 class _SelectionHandleDriverState extends State<SelectionHandleDriver> {
297
+  ZefyrScope _scope;
298
+
297 299
   /// Current document selection.
298 300
   TextSelection get selection => _selection;
299 301
   TextSelection _selection;
@@ -327,7 +329,18 @@ class _SelectionHandleDriverState extends State<SelectionHandleDriver> {
327 329
   void didChangeDependencies() {
328 330
     super.didChangeDependencies();
329 331
     final scope = ZefyrScope.of(context);
330
-    _selection = scope.selection;
332
+    if (_scope != scope) {
333
+      _scope?.removeListener(_handleScopeChange);
334
+      _scope = scope;
335
+      _scope.addListener(_handleScopeChange);
336
+    }
337
+    _selection = _scope.selection;
338
+  }
339
+
340
+  @override
341
+  void dispose() {
342
+    _scope?.removeListener(_handleScopeChange);
343
+    super.dispose();
331 344
   }
332 345
 
333 346
   //
@@ -386,6 +399,12 @@ class _SelectionHandleDriverState extends State<SelectionHandleDriver> {
386 399
 
387 400
   Offset _dragPosition;
388 401
 
402
+  void _handleScopeChange() {
403
+    setState(() {
404
+      _selection = _scope.selection;
405
+    });
406
+  }
407
+
389 408
   void _handleDragStart(DragStartDetails details) {
390 409
     _dragPosition = details.globalPosition;
391 410
   }