-
Notifications
You must be signed in to change notification settings - Fork 861
/
Copy pathquill_controller.dart
714 lines (621 loc) · 21.3 KB
/
quill_controller.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
import 'dart:math' as math;
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/services.dart' show ClipboardData, Clipboard;
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart' show experimental;
import '../../quill_delta.dart';
import '../common/structs/image_url.dart';
import '../common/structs/offset_value.dart';
import '../common/utils/embeds.dart';
import '../delta/delta_diff.dart';
import '../document/attribute.dart';
import '../document/document.dart';
import '../document/nodes/embeddable.dart';
import '../document/nodes/leaf.dart';
import '../document/structs/doc_change.dart';
import '../document/style.dart';
import '../editor/config/editor_configurations.dart';
import '../toolbar/config/simple_toolbar_configurations.dart';
import 'quill_controller_configurations.dart';
import 'quill_controller_rich_paste.dart';
import 'web/quill_controller_web_stub.dart'
if (dart.library.html) 'web/quill_controller_web_real.dart';
typedef ReplaceTextCallback = bool Function(int index, int len, Object? data);
typedef DeleteCallback = void Function(int cursorPosition, bool forward);
class QuillController extends ChangeNotifier {
QuillController({
required Document document,
required TextSelection selection,
this.configurations = const QuillControllerConfigurations(),
this.keepStyleOnNewLine = true,
this.onReplaceText,
this.onDelete,
this.onSelectionCompleted,
this.onSelectionChanged,
this.readOnly = false,
this.editorFocusNode,
}) : _document = document,
_selection = selection {
if (kIsWeb) {
initializeWebPasteEvent();
}
}
factory QuillController.basic(
{QuillControllerConfigurations configurations =
const QuillControllerConfigurations(),
FocusNode? editorFocusNode}) =>
QuillController(
configurations: configurations,
editorFocusNode: editorFocusNode,
document: Document(),
selection: const TextSelection.collapsed(offset: 0),
);
final QuillControllerConfigurations configurations;
/// Editor configurations
///
/// Caches configuration set in QuillEditor ctor.
QuillEditorConfigurations? _editorConfigurations;
QuillEditorConfigurations get editorConfigurations =>
_editorConfigurations ?? const QuillEditorConfigurations();
set editorConfigurations(QuillEditorConfigurations? value) =>
_editorConfigurations = document.editorConfigurations = value;
/// Toolbar configurations
///
/// Caches configuration set in QuillSimpleToolbar ctor.
QuillSimpleToolbarConfigurations? _toolbarConfigurations;
QuillSimpleToolbarConfigurations get toolbarConfigurations =>
_toolbarConfigurations ?? const QuillSimpleToolbarConfigurations();
set toolbarConfigurations(QuillSimpleToolbarConfigurations? value) =>
_toolbarConfigurations = value;
/// Document managed by this controller.
Document _document;
Document get document => _document;
set document(Document doc) {
_document = doc;
_document.editorConfigurations = editorConfigurations;
// Prevent the selection from
_selection = const TextSelection(baseOffset: 0, extentOffset: 0);
notifyListeners();
}
@experimental
void setContents(
Delta delta, {
ChangeSource changeSource = ChangeSource.local,
}) {
final newDocument = Document.fromDelta(delta);
final change = DocChange(_document.toDelta(), delta, changeSource);
newDocument.documentChangeObserver.add(change);
newDocument.history.handleDocChange(change);
_document = newDocument;
notifyListeners();
}
/// Tells whether to keep or reset the [toggledStyle]
/// when user adds a new line.
final bool keepStyleOnNewLine;
/// Currently selected text within the [document].
TextSelection get selection => _selection;
TextSelection _selection;
/// Custom [replaceText] handler
/// Return false to ignore the event
ReplaceTextCallback? onReplaceText;
/// Custom delete handler
DeleteCallback? onDelete;
void Function()? onSelectionCompleted;
void Function(TextSelection textSelection)? onSelectionChanged;
/// Store any styles attribute that got toggled by the tap of a button
/// and that has not been applied yet.
/// It gets reset after each format action within the [document].
Style toggledStyle = const Style();
/// [raw_editor_actions] handling of backspace event may need to force the style displayed in the toolbar
void forceToggledStyle(Style style) {
toggledStyle = style;
notifyListeners();
}
bool ignoreFocusOnTextChange = false;
/// Skip requestKeyboard being called
/// in [QuillRawEditorState._didChangeTextEditingValue]
bool skipRequestKeyboard = false;
/// True when this [QuillController] instance has been disposed.
///
/// A safety mechanism to ensure that listeners don't crash when adding,
/// removing or listeners to this instance.
bool _isDisposed = false;
Stream<DocChange> get changes => document.changes;
TextEditingValue get plainTextEditingValue => TextEditingValue(
text: document.toPlainText(),
selection: selection,
);
/// Only attributes applied to all characters within this range are
/// included in the result.
Style getSelectionStyle() {
return document
.collectStyle(selection.start, selection.end - selection.start)
.mergeAll(toggledStyle);
}
// Increases or decreases the indent of the current selection by 1.
void indentSelection(bool isIncrease) {
if (selection.isCollapsed) {
_indentSelectionFormat(isIncrease);
} else {
_indentSelectionEachLine(isIncrease);
}
}
void _indentSelectionFormat(bool isIncrease) {
final indent = getSelectionStyle().attributes[Attribute.indent.key];
if (indent == null) {
if (isIncrease) {
formatSelection(Attribute.indentL1);
}
return;
}
if (indent.value == 1 && !isIncrease) {
formatSelection(Attribute.clone(Attribute.indentL1, null));
return;
}
if (isIncrease) {
if (indent.value < 5) {
formatSelection(Attribute.getIndentLevel(indent.value + 1));
}
return;
}
formatSelection(Attribute.getIndentLevel(indent.value - 1));
}
void _indentSelectionEachLine(bool isIncrease) {
final styles = document.collectAllStylesWithOffset(
selection.start,
selection.end - selection.start,
);
for (final style in styles) {
final indent = style.value.attributes[Attribute.indent.key];
final formatIndex = math.max(style.offset, selection.start);
final formatLength = math.min(
style.offset + (style.length ?? 0),
selection.end,
) -
style.offset;
Attribute? formatAttribute;
if (indent == null) {
if (isIncrease) {
formatAttribute = Attribute.indentL1;
}
} else if (indent.value == 1 && !isIncrease) {
formatAttribute = Attribute.clone(Attribute.indentL1, null);
} else if (isIncrease) {
if (indent.value < 5) {
formatAttribute = Attribute.getIndentLevel(indent.value + 1);
}
} else {
formatAttribute = Attribute.getIndentLevel(indent.value - 1);
}
if (formatAttribute != null) {
document.format(formatIndex, formatLength, formatAttribute);
}
}
notifyListeners();
}
/// Returns all styles and Embed for each node within selection
List<OffsetValue> getAllIndividualSelectionStylesAndEmbed() {
final stylesAndEmbed = document.collectAllIndividualStyleAndEmbed(
selection.start, selection.end - selection.start);
return stylesAndEmbed;
}
/// Returns plain text for each node within selection
String getPlainText() {
final text =
document.getPlainText(selection.start, selection.end - selection.start);
return text;
}
/// Returns all styles for any character within the specified text range.
List<Style> getAllSelectionStyles() {
final styles = document.collectAllStyles(
selection.start, selection.end - selection.start)
..add(toggledStyle);
return styles;
}
void undo() {
final result = document.undo();
if (result.changed) {
_handleHistoryChange(result.len);
}
}
void _handleHistoryChange(int len) {
updateSelection(
TextSelection.collapsed(
offset: len,
),
ChangeSource.local,
);
}
void redo() {
final result = document.redo();
if (result.changed) {
_handleHistoryChange(result.len);
}
}
bool get hasUndo => document.hasUndo;
bool get hasRedo => document.hasRedo;
/// clear editor
void clear() {
replaceText(0, plainTextEditingValue.text.length - 1, '',
const TextSelection.collapsed(offset: 0));
}
void replaceText(
int index,
int len,
Object? data,
TextSelection? textSelection, {
bool ignoreFocus = false,
bool shouldNotifyListeners = true,
}) {
assert(data is String || data is Embeddable || data is Delta);
if (onReplaceText != null && !onReplaceText!(index, len, data)) {
return;
}
Delta? delta;
Style? style;
if (len > 0 || data is! String || data.isNotEmpty) {
delta = document.replace(index, len, data);
/// Remove block styles as they can only be attached to line endings
style = Style.attr(Map<String, Attribute>.fromEntries(toggledStyle
.attributes.entries
.where((a) => a.value.scope != AttributeScope.block)));
var shouldRetainDelta = style.isNotEmpty &&
delta.isNotEmpty &&
delta.length <= 2 &&
delta.last.isInsert;
if (shouldRetainDelta &&
style.isNotEmpty &&
delta.length == 2 &&
delta.last.data == '\n') {
// if all attributes are inline, shouldRetainDelta should be false
final anyAttributeNotInline =
style.values.any((attr) => !attr.isInline);
if (!anyAttributeNotInline) {
shouldRetainDelta = false;
}
}
if (shouldRetainDelta) {
final retainDelta = Delta()
..retain(index)
..retain(data is String ? data.length : 1, style.toJson());
document.compose(retainDelta, ChangeSource.local);
}
}
if (textSelection != null) {
if (delta == null || delta.isEmpty) {
_updateSelection(textSelection);
} else {
final user = Delta()
..retain(index)
..insert(data)
..delete(len);
final positionDelta = getPositionDelta(user, delta);
_updateSelection(
textSelection.copyWith(
baseOffset: textSelection.baseOffset + positionDelta,
extentOffset: textSelection.extentOffset + positionDelta,
),
insertNewline: data == '\n');
}
}
if (ignoreFocus) {
ignoreFocusOnTextChange = true;
}
if (shouldNotifyListeners) {
notifyListeners();
}
ignoreFocusOnTextChange = false;
}
/// Called in two cases:
/// forward == false && textBefore.isEmpty
/// forward == true && textAfter.isEmpty
/// Android only
/// see https://github.com/singerdmx/flutter-quill/discussions/514
void handleDelete(int cursorPosition, bool forward) =>
onDelete?.call(cursorPosition, forward);
void formatTextStyle(int index, int len, Style style) {
style.attributes.forEach((key, attr) {
formatText(index, len, attr);
});
}
void formatText(
int index,
int len,
Attribute? attribute, {
bool shouldNotifyListeners = true,
}) {
if (len == 0 &&
attribute!.isInline &&
attribute.key != Attribute.link.key) {
// Add the attribute to our toggledStyle.
// It will be used later upon insertion.
toggledStyle = toggledStyle.put(attribute);
}
final change = document.format(index, len, attribute);
// Transform selection against the composed change and give priority to
// the change. This is needed in cases when format operation actually
// inserts data into the document (e.g. embeds).
final adjustedSelection = selection.copyWith(
baseOffset: change.transformPosition(selection.baseOffset),
extentOffset: change.transformPosition(selection.extentOffset));
if (selection != adjustedSelection) {
_updateSelection(adjustedSelection);
}
if (shouldNotifyListeners) {
notifyListeners();
}
}
void formatSelection(Attribute? attribute,
{bool shouldNotifyListeners = true}) {
formatText(
selection.start,
selection.end - selection.start,
attribute,
shouldNotifyListeners: shouldNotifyListeners,
);
}
void moveCursorToStart() {
updateSelection(
const TextSelection.collapsed(offset: 0),
ChangeSource.local,
);
}
void moveCursorToPosition(int position) {
updateSelection(
TextSelection.collapsed(offset: position),
ChangeSource.local,
);
}
void moveCursorToEnd() {
updateSelection(
TextSelection.collapsed(offset: plainTextEditingValue.text.length),
ChangeSource.local,
);
}
void updateSelection(TextSelection textSelection, ChangeSource source) {
_updateSelection(textSelection);
notifyListeners();
}
void compose(Delta delta, TextSelection textSelection, ChangeSource source) {
if (delta.isNotEmpty) {
document.compose(delta, source);
}
textSelection = selection.copyWith(
baseOffset: delta.transformPosition(selection.baseOffset, force: false),
extentOffset: delta.transformPosition(
selection.extentOffset,
force: false,
),
);
if (selection != textSelection) {
_updateSelection(textSelection);
}
notifyListeners();
}
@override
void addListener(VoidCallback listener) {
// By using `_isDisposed`, make sure that `addListener` won't be called on a
// disposed `ChangeListener`
if (!_isDisposed) {
super.addListener(listener);
}
}
@override
void removeListener(VoidCallback listener) {
// By using `_isDisposed`, make sure that `removeListener` won't be called
// on a disposed `ChangeListener`
if (!_isDisposed) {
super.removeListener(listener);
}
}
@override
void dispose() {
if (!_isDisposed) {
document.close();
}
_isDisposed = true;
if (kIsWeb) {
closeWebPasteEvent();
}
super.dispose();
}
void _updateSelection(TextSelection textSelection,
{bool insertNewline = false}) {
_selection = textSelection;
final end = document.length - 1;
_selection = selection.copyWith(
baseOffset: math.min(selection.baseOffset, end),
extentOffset: math.min(selection.extentOffset, end));
if (keepStyleOnNewLine) {
if (insertNewline && selection.start > 0) {
final style = document.collectStyle(selection.start - 1, 0);
final ignoredStyles = style.attributes.values.where(
(s) => !s.isInline || s.key == Attribute.link.key,
);
toggledStyle = style.removeAll(ignoredStyles.toSet());
} else {
toggledStyle = const Style();
}
} else {
toggledStyle = const Style();
}
onSelectionChanged?.call(textSelection);
}
/// Given offset, find its leaf node in document
Leaf? queryNode(int offset) {
return document.querySegmentLeafNode(offset).leaf;
}
// Notify toolbar buttons directly with attributes
Map<String, Attribute> toolbarButtonToggler = const {};
/// Clipboard caches last copy to allow paste with styles. Static to allow paste between multiple instances of editor.
static String _pastePlainText = '';
static Delta _pasteDelta = Delta();
static List<OffsetValue> _pasteStyleAndEmbed = <OffsetValue>[];
String get pastePlainText => _pastePlainText;
Delta get pasteDelta => _pasteDelta;
List<OffsetValue> get pasteStyleAndEmbed => _pasteStyleAndEmbed;
bool readOnly;
/// Used to give focus to the editor following a toolbar action
FocusNode? editorFocusNode;
ImageUrl? _copiedImageUrl;
ImageUrl? get copiedImageUrl => _copiedImageUrl;
set copiedImageUrl(ImageUrl? value) {
_copiedImageUrl = value;
Clipboard.setData(const ClipboardData(text: ''));
}
bool clipboardSelection(bool copy) {
copiedImageUrl = null;
/// Get the text for the selected region and expand the content of Embedded objects.
_pastePlainText = document.getPlainText(
selection.start, selection.end - selection.start, true);
/// Get the internal representation so it can be pasted into a QuillEditor with style retained.
_pasteStyleAndEmbed = getAllIndividualSelectionStylesAndEmbed();
/// Get the deltas for the selection so they can be pasted into a QuillEditor with styles and embeds retained.
_pasteDelta = document.toDelta().slice(selection.start, selection.end);
if (!selection.isCollapsed) {
Clipboard.setData(ClipboardData(text: _pastePlainText));
if (!copy) {
if (readOnly) return false;
final sel = selection;
replaceText(sel.start, sel.end - sel.start, '',
TextSelection.collapsed(offset: sel.start));
}
return true;
}
return false;
}
/// Returns whether paste operation was handled here.
/// updateEditor is called if paste operation was successful.
Future<bool> clipboardPaste({void Function()? updateEditor}) async {
if (readOnly || !selection.isValid) return true;
final pasteUsingInternalImageSuccess = await _pasteInternalImage();
if (pasteUsingInternalImageSuccess) {
updateEditor?.call();
return true;
}
final pasteUsingHtmlSuccess = await pasteHTML();
if (pasteUsingHtmlSuccess) {
updateEditor?.call();
return true;
}
final pasteUsingMarkdownSuccess = await pasteMarkdown();
if (pasteUsingMarkdownSuccess) {
updateEditor?.call();
return true;
}
// Snapshot the input before using `await`.
// See https://github.com/flutter/flutter/issues/11427
final plainTextClipboardData =
await Clipboard.getData(Clipboard.kTextPlain);
if (pasteUsingPlainOrDelta(plainTextClipboardData?.text)) {
updateEditor?.call();
return true;
}
if (await configurations.onClipboardPaste?.call() == true) {
updateEditor?.call();
return true;
}
return false;
}
/// Internal method to allow unit testing
bool pasteUsingPlainOrDelta(String? clipboardText) {
if (clipboardText != null) {
/// Internal copy-paste preserves styles and embeds
if (clipboardText == _pastePlainText &&
_pastePlainText.isNotEmpty &&
_pasteDelta.isNotEmpty) {
replaceText(selection.start, selection.end - selection.start,
_pasteDelta, TextSelection.collapsed(offset: selection.end));
} else {
replaceText(
selection.start,
selection.end - selection.start,
clipboardText,
TextSelection.collapsed(
offset: selection.end + clipboardText.length));
}
return true;
}
return false;
}
/// Return true if can paste internal image
Future<bool> _pasteInternalImage() async {
final copiedImageUrl = _copiedImageUrl;
if (copiedImageUrl != null) {
final index = selection.baseOffset;
final length = selection.extentOffset - index;
replaceText(
index,
length,
BlockEmbed.image(copiedImageUrl.url),
null,
);
if (copiedImageUrl.styleString.isNotEmpty) {
formatText(
getEmbedNode(this, index + 1).offset,
1,
StyleAttribute(copiedImageUrl.styleString),
);
}
_copiedImageUrl = null;
await Clipboard.setData(
const ClipboardData(text: ''),
);
return true;
}
return false;
}
void replaceTextWithEmbeds(
int index,
int len,
String insertedText,
TextSelection? textSelection, {
bool ignoreFocus = false,
bool shouldNotifyListeners = true,
}) {
final containsEmbed =
insertedText.codeUnits.contains(Embed.kObjectReplacementInt);
insertedText =
containsEmbed ? _adjustInsertedText(insertedText) : insertedText;
replaceText(index, len, insertedText, textSelection,
ignoreFocus: ignoreFocus, shouldNotifyListeners: shouldNotifyListeners);
_applyPasteStyleAndEmbed(insertedText, index, containsEmbed);
}
void _applyPasteStyleAndEmbed(
String insertedText, int start, bool containsEmbed) {
if (insertedText == pastePlainText && pastePlainText != '' ||
containsEmbed) {
final pos = start;
for (final p in pasteStyleAndEmbed) {
final offset = p.offset;
final styleAndEmbed = p.value;
final local = pos + offset;
if (styleAndEmbed is Embeddable) {
replaceText(local, 0, styleAndEmbed, null);
} else {
final style = styleAndEmbed as Style;
if (style.isInline) {
formatTextStyle(local, p.length!, style);
} else if (style.isBlock) {
final node = document.queryChild(local).node;
if (node != null && p.length == node.length - 1) {
for (final attribute in style.values) {
document.format(local, 0, attribute);
}
}
}
}
}
}
}
String _adjustInsertedText(String text) {
final sb = StringBuffer();
for (var i = 0; i < text.length; i++) {
if (text.codeUnitAt(i) == Embed.kObjectReplacementInt) {
continue;
}
sb.write(text[i]);
}
return sb.toString();
}
}