Browse Source

Updated editable to use new ZefyrScope

Anatoly Pulyaevskiy 6 years ago
parent
commit
43e68d44a9

+ 16
- 0
packages/zefyr/example/lib/src/full_page.dart View File

1
+import 'dart:async';
1
 import 'dart:convert';
2
 import 'dart:convert';
2
 
3
 
3
 import 'package:flutter/material.dart';
4
 import 'package:flutter/material.dart';
39
       ZefyrController(NotusDocument.fromDelta(getDelta()));
40
       ZefyrController(NotusDocument.fromDelta(getDelta()));
40
   final FocusNode _focusNode = new FocusNode();
41
   final FocusNode _focusNode = new FocusNode();
41
   bool _editing = false;
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
   @override
59
   @override
44
   Widget build(BuildContext context) {
60
   Widget build(BuildContext context) {

+ 22
- 7
packages/zefyr/lib/src/widgets/editable_text.dart View File

18
 import 'paragraph.dart';
18
 import 'paragraph.dart';
19
 import 'quote.dart';
19
 import 'quote.dart';
20
 import 'render_context.dart';
20
 import 'render_context.dart';
21
+import 'scope.dart';
21
 import 'selection.dart';
22
 import 'selection.dart';
23
+import 'theme.dart';
22
 
24
 
23
 /// Core widget responsible for editing Zefyr documents.
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
 /// Consider using [ZefyrEditor] which wraps this widget and adds a toolbar to
30
 /// Consider using [ZefyrEditor] which wraps this widget and adds a toolbar to
28
 /// edit style attributes.
31
 /// edit style attributes.
95
 
98
 
96
   /// Current text selection.
99
   /// Current text selection.
97
   TextSelection get selection => widget.controller.selection;
100
   TextSelection get selection => widget.controller.selection;
98
-  ZefyrRenderContext get renderContext => _renderContext;
99
-  ValueNotifier<bool> get showCursor => _cursorTimer.value;
100
 
101
 
101
   /// Express interest in interacting with the keyboard.
102
   /// Express interest in interacting with the keyboard.
102
   ///
103
   ///
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
   @override
185
   @override
169
   void dispose() {
186
   void dispose() {
170
     _cancelSubscriptions();
187
     _cancelSubscriptions();
183
   //
200
   //
184
 
201
 
185
   final ScrollController _scrollController = ScrollController();
202
   final ScrollController _scrollController = ScrollController();
186
-  final ZefyrRenderContext _renderContext = ZefyrRenderContext();
187
-  final CursorTimer _cursorTimer = CursorTimer();
203
+  ZefyrRenderContext _renderContext;
204
+  CursorTimer _cursorTimer;
188
   InputConnectionController _input;
205
   InputConnectionController _input;
189
   bool _didAutoFocus = false;
206
   bool _didAutoFocus = false;
190
 
207
 
223
 
240
 
224
   void _updateSubscriptions([ZefyrEditableText oldWidget]) {
241
   void _updateSubscriptions([ZefyrEditableText oldWidget]) {
225
     if (oldWidget == null) {
242
     if (oldWidget == null) {
226
-      _renderContext.addListener(_handleRenderContextChange);
227
       widget.controller.addListener(_handleLocalValueChange);
243
       widget.controller.addListener(_handleLocalValueChange);
228
       focusNode.addListener(_handleFocusChange);
244
       focusNode.addListener(_handleFocusChange);
229
       return;
245
       return;
243
 
259
 
244
   void _cancelSubscriptions() {
260
   void _cancelSubscriptions() {
245
     _renderContext.removeListener(_handleRenderContextChange);
261
     _renderContext.removeListener(_handleRenderContextChange);
246
-    _renderContext.dispose();
247
     widget.controller.removeListener(_handleLocalValueChange);
262
     widget.controller.removeListener(_handleLocalValueChange);
248
     focusNode.removeListener(_handleFocusChange);
263
     focusNode.removeListener(_handleFocusChange);
249
     _input.closeConnection();
264
     _input.closeConnection();

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

144
   @override
144
   @override
145
   Widget build(BuildContext context) {
145
   Widget build(BuildContext context) {
146
     Widget editable = new ZefyrEditableText(
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
       autofocus: widget.autofocus,
150
       autofocus: widget.autofocus,
151
       enabled: widget.enabled,
151
       enabled: widget.enabled,
152
       padding: widget.padding,
152
       padding: widget.padding,

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

176
     final selection = _controller.selection;
176
     final selection = _controller.selection;
177
     if (_selectionStyle != attrs || _selection != selection) {
177
     if (_selectionStyle != attrs || _selection != selection) {
178
       _selectionStyle = attrs;
178
       _selectionStyle = attrs;
179
-      _selection = _controller.selection;
179
+      _selection = selection;
180
       notifyListeners();
180
       notifyListeners();
181
     }
181
     }
182
   }
182
   }

+ 20
- 1
packages/zefyr/lib/src/widgets/selection.dart View File

294
 }
294
 }
295
 
295
 
296
 class _SelectionHandleDriverState extends State<SelectionHandleDriver> {
296
 class _SelectionHandleDriverState extends State<SelectionHandleDriver> {
297
+  ZefyrScope _scope;
298
+
297
   /// Current document selection.
299
   /// Current document selection.
298
   TextSelection get selection => _selection;
300
   TextSelection get selection => _selection;
299
   TextSelection _selection;
301
   TextSelection _selection;
327
   void didChangeDependencies() {
329
   void didChangeDependencies() {
328
     super.didChangeDependencies();
330
     super.didChangeDependencies();
329
     final scope = ZefyrScope.of(context);
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
 
399
 
387
   Offset _dragPosition;
400
   Offset _dragPosition;
388
 
401
 
402
+  void _handleScopeChange() {
403
+    setState(() {
404
+      _selection = _scope.selection;
405
+    });
406
+  }
407
+
389
   void _handleDragStart(DragStartDetails details) {
408
   void _handleDragStart(DragStartDetails details) {
390
     _dragPosition = details.globalPosition;
409
     _dragPosition = details.globalPosition;
391
   }
410
   }