Refactor code in raw_editor

pull/889/head
X Code 3 years ago
parent 1a26a3249a
commit f11bc5bc3c
  1. 113
      lib/src/widgets/raw_editor.dart

@ -284,7 +284,7 @@ class RawEditorState extends EditorState
assert(debugCheckHasMediaQuery(context)); assert(debugCheckHasMediaQuery(context));
super.build(context); super.build(context);
var _doc = widget.controller.document; var _doc = controller.document;
if (_doc.isEmpty() && widget.placeholder != null) { if (_doc.isEmpty() && widget.placeholder != null) {
_doc = Document.fromJson(jsonDecode( _doc = Document.fromJson(jsonDecode(
'[{"attributes":{"placeholder":true},"insert":"${widget.placeholder}\\n"}]')); '[{"attributes":{"placeholder":true},"insert":"${widget.placeholder}\\n"}]'));
@ -296,7 +296,7 @@ class RawEditorState extends EditorState
child: _Editor( child: _Editor(
key: _editorKey, key: _editorKey,
document: _doc, document: _doc,
selection: widget.controller.selection, selection: controller.selection,
hasFocus: _hasFocus, hasFocus: _hasFocus,
scrollable: widget.scrollable, scrollable: widget.scrollable,
cursorController: _cursorCont, cursorController: _cursorCont,
@ -335,7 +335,7 @@ class RawEditorState extends EditorState
key: _editorKey, key: _editorKey,
offset: offset, offset: offset,
document: _doc, document: _doc,
selection: widget.controller.selection, selection: controller.selection,
hasFocus: _hasFocus, hasFocus: _hasFocus,
scrollable: widget.scrollable, scrollable: widget.scrollable,
textDirection: _textDirection, textDirection: _textDirection,
@ -380,8 +380,8 @@ class RawEditorState extends EditorState
void _handleSelectionChanged( void _handleSelectionChanged(
TextSelection selection, SelectionChangedCause cause) { TextSelection selection, SelectionChangedCause cause) {
final oldSelection = widget.controller.selection; final oldSelection = controller.selection;
widget.controller.updateSelection(selection, ChangeSource.LOCAL); controller.updateSelection(selection, ChangeSource.LOCAL);
_selectionOverlay?.handlesVisible = _shouldShowSelectionHandles(); _selectionOverlay?.handlesVisible = _shouldShowSelectionHandles();
@ -403,7 +403,7 @@ class RawEditorState extends EditorState
} }
void _handleSelectionCompleted() { void _handleSelectionCompleted() {
widget.controller.onSelectionCompleted?.call(); controller.onSelectionCompleted?.call();
} }
/// Updates the checkbox positioned at [offset] in document /// Updates the checkbox positioned at [offset] in document
@ -411,23 +411,25 @@ class RawEditorState extends EditorState
void _handleCheckboxTap(int offset, bool value) { void _handleCheckboxTap(int offset, bool value) {
if (!widget.readOnly) { if (!widget.readOnly) {
_disableScrollControllerAnimateOnce = true; _disableScrollControllerAnimateOnce = true;
widget.controller.ignoreFocusOnTextChange = true; controller.ignoreFocusOnTextChange = true;
final currentSelection = widget.controller.selection.copyWith(); final currentSelection = controller.selection.copyWith();
final attribute = value ? Attribute.checked : Attribute.unchecked; final attribute = value ? Attribute.checked : Attribute.unchecked;
widget.controller.formatText(offset, 0, attribute); controller
..formatText(offset, 0, attribute)
// Checkbox tapping causes controller.selection to go to offset 0 // Checkbox tapping causes controller.selection to go to offset 0
// Stop toggling those two toolbar buttons // Stop toggling those two toolbar buttons
widget.controller.toolbarButtonToggler = { ..toolbarButtonToggler = {
Attribute.list.key: attribute, Attribute.list.key: attribute,
Attribute.header.key: Attribute.header Attribute.header.key: Attribute.header
}; };
// Go back from offset 0 to current selection // Go back from offset 0 to current selection
SchedulerBinding.instance.addPostFrameCallback((_) { SchedulerBinding.instance.addPostFrameCallback((_) {
widget.controller.ignoreFocusOnTextChange = false; controller
widget.controller.updateSelection(currentSelection, ChangeSource.LOCAL); ..ignoreFocusOnTextChange = false
..updateSelection(currentSelection, ChangeSource.LOCAL);
}); });
} }
} }
@ -444,11 +446,11 @@ class RawEditorState extends EditorState
final attrs = node.style.attributes; final attrs = node.style.attributes;
final editableTextBlock = EditableTextBlock( final editableTextBlock = EditableTextBlock(
block: node, block: node,
controller: widget.controller, controller: controller,
textDirection: _textDirection, textDirection: _textDirection,
scrollBottomInset: widget.scrollBottomInset, scrollBottomInset: widget.scrollBottomInset,
verticalSpacing: _getVerticalSpacingForBlock(node, _styles), verticalSpacing: _getVerticalSpacingForBlock(node, _styles),
textSelection: widget.controller.selection, textSelection: controller.selection,
color: widget.selectionColor, color: widget.selectionColor,
styles: _styles, styles: _styles,
enableInteractiveSelection: widget.enableInteractiveSelection, enableInteractiveSelection: widget.enableInteractiveSelection,
@ -482,7 +484,7 @@ class RawEditorState extends EditorState
customStyleBuilder: widget.customStyleBuilder, customStyleBuilder: widget.customStyleBuilder,
styles: _styles!, styles: _styles!,
readOnly: widget.readOnly, readOnly: widget.readOnly,
controller: widget.controller, controller: controller,
linkActionPicker: _linkActionPicker, linkActionPicker: _linkActionPicker,
onLaunchUrl: widget.onLaunchUrl, onLaunchUrl: widget.onLaunchUrl,
); );
@ -493,7 +495,7 @@ class RawEditorState extends EditorState
0, 0,
_getVerticalSpacingForLine(node, _styles), _getVerticalSpacingForLine(node, _styles),
_textDirection, _textDirection,
widget.controller.selection, controller.selection,
widget.selectionColor, widget.selectionColor,
widget.enableInteractiveSelection, widget.enableInteractiveSelection,
_hasFocus, _hasFocus,
@ -550,8 +552,8 @@ class RawEditorState extends EditorState
_clipboardStatus.addListener(_onChangedClipboardStatus); _clipboardStatus.addListener(_onChangedClipboardStatus);
widget.controller.addListener(() { controller.addListener(() {
_didChangeTextEditingValue(widget.controller.ignoreFocusOnTextChange); _didChangeTextEditingValue(controller.ignoreFocusOnTextChange);
}); });
_scrollController = widget.scrollController; _scrollController = widget.scrollController;
@ -641,9 +643,9 @@ class RawEditorState extends EditorState
_cursorCont.show.value = widget.showCursor; _cursorCont.show.value = widget.showCursor;
_cursorCont.style = widget.cursorStyle; _cursorCont.style = widget.cursorStyle;
if (widget.controller != oldWidget.controller) { if (controller != oldWidget.controller) {
oldWidget.controller.removeListener(_didChangeTextEditingValue); oldWidget.controller.removeListener(_didChangeTextEditingValue);
widget.controller.addListener(_didChangeTextEditingValue); controller.addListener(_didChangeTextEditingValue);
updateRemoteValueIfNeeded(); updateRemoteValueIfNeeded();
} }
@ -659,7 +661,7 @@ class RawEditorState extends EditorState
updateKeepAlive(); updateKeepAlive();
} }
if (widget.controller.selection != oldWidget.controller.selection) { if (controller.selection != oldWidget.controller.selection) {
_selectionOverlay?.update(textEditingValue); _selectionOverlay?.update(textEditingValue);
} }
@ -679,8 +681,7 @@ class RawEditorState extends EditorState
} }
bool _shouldShowSelectionHandles() { bool _shouldShowSelectionHandles() {
return widget.showSelectionHandles && return widget.showSelectionHandles && !controller.selection.isCollapsed;
!widget.controller.selection.isCollapsed;
} }
@override @override
@ -691,7 +692,7 @@ class RawEditorState extends EditorState
assert(!hasConnection); assert(!hasConnection);
_selectionOverlay?.dispose(); _selectionOverlay?.dispose();
_selectionOverlay = null; _selectionOverlay = null;
widget.controller.removeListener(_didChangeTextEditingValue); controller.removeListener(_didChangeTextEditingValue);
widget.focusNode.removeListener(_handleFocusChanged); widget.focusNode.removeListener(_handleFocusChanged);
_cursorCont.dispose(); _cursorCont.dispose();
_clipboardStatus _clipboardStatus
@ -708,11 +709,7 @@ class RawEditorState extends EditorState
if (kIsWeb) { if (kIsWeb) {
_onChangeTextEditingValue(ignoreFocus); _onChangeTextEditingValue(ignoreFocus);
if (!ignoreFocus) { if (!ignoreFocus) {
if (controller.skipRequestKeyboard) { requestKeyboard();
controller.skipRequestKeyboard = false;
} else {
requestKeyboard();
}
} }
return; return;
} }
@ -720,14 +717,10 @@ class RawEditorState extends EditorState
if (ignoreFocus || _keyboardVisible) { if (ignoreFocus || _keyboardVisible) {
_onChangeTextEditingValue(ignoreFocus); _onChangeTextEditingValue(ignoreFocus);
} else { } else {
if (controller.skipRequestKeyboard) { requestKeyboard();
controller.skipRequestKeyboard = false;
} else {
requestKeyboard();
}
if (mounted) { if (mounted) {
setState(() { setState(() {
// Use widget.controller.value in build() // Use controller.value in build()
// Trigger build and updateChildren // Trigger build and updateChildren
}); });
} }
@ -742,8 +735,7 @@ class RawEditorState extends EditorState
return; return;
} }
_showCaretOnScreen(); _showCaretOnScreen();
_cursorCont.startOrStopCursorTimerIfNeeded( _cursorCont.startOrStopCursorTimerIfNeeded(_hasFocus, controller.selection);
_hasFocus, widget.controller.selection);
if (hasConnection) { if (hasConnection) {
// To keep the cursor from blinking while typing, we want to restart the // To keep the cursor from blinking while typing, we want to restart the
// cursor timer every time a new character is typed. // cursor timer every time a new character is typed.
@ -766,7 +758,7 @@ class RawEditorState extends EditorState
}); });
if (mounted) { if (mounted) {
setState(() { setState(() {
// Use widget.controller.value in build() // Use controller.value in build()
// Trigger build and updateChildren // Trigger build and updateChildren
}); });
} }
@ -800,8 +792,7 @@ class RawEditorState extends EditorState
void _handleFocusChanged() { void _handleFocusChanged() {
openOrCloseConnection(); openOrCloseConnection();
_cursorCont.startOrStopCursorTimerIfNeeded( _cursorCont.startOrStopCursorTimerIfNeeded(_hasFocus, controller.selection);
_hasFocus, widget.controller.selection);
_updateOrDisposeSelectionOverlayIfNeeded(); _updateOrDisposeSelectionOverlayIfNeeded();
if (_hasFocus) { if (_hasFocus) {
WidgetsBinding.instance.addObserver(this); WidgetsBinding.instance.addObserver(this);
@ -890,6 +881,10 @@ class RawEditorState extends EditorState
/// keyboard become visible. /// keyboard become visible.
@override @override
void requestKeyboard() { void requestKeyboard() {
if (controller.skipRequestKeyboard) {
controller.skipRequestKeyboard = false;
return;
}
if (_hasFocus) { if (_hasFocus) {
openConnectionIfNeeded(); openConnectionIfNeeded();
_showCaretOnScreen(); _showCaretOnScreen();
@ -939,9 +934,9 @@ class RawEditorState extends EditorState
/// Copy current selection to [Clipboard]. /// Copy current selection to [Clipboard].
@override @override
void copySelection(SelectionChangedCause cause) { void copySelection(SelectionChangedCause cause) {
widget.controller.copiedImageUrl = null; controller.copiedImageUrl = null;
_pastePlainText = widget.controller.getPlainText(); _pastePlainText = controller.getPlainText();
_pasteStyle = widget.controller.getAllIndividualSelectionStyles(); _pasteStyle = controller.getAllIndividualSelectionStyles();
final selection = textEditingValue.selection; final selection = textEditingValue.selection;
final text = textEditingValue.text; final text = textEditingValue.text;
@ -968,9 +963,9 @@ class RawEditorState extends EditorState
/// Cut current selection to [Clipboard]. /// Cut current selection to [Clipboard].
@override @override
void cutSelection(SelectionChangedCause cause) { void cutSelection(SelectionChangedCause cause) {
widget.controller.copiedImageUrl = null; controller.copiedImageUrl = null;
_pastePlainText = widget.controller.getPlainText(); _pastePlainText = controller.getPlainText();
_pasteStyle = widget.controller.getAllIndividualSelectionStyles(); _pasteStyle = controller.getAllIndividualSelectionStyles();
if (widget.readOnly) { if (widget.readOnly) {
return; return;
@ -996,19 +991,17 @@ class RawEditorState extends EditorState
return; return;
} }
if (widget.controller.copiedImageUrl != null) { if (controller.copiedImageUrl != null) {
final index = textEditingValue.selection.baseOffset; final index = textEditingValue.selection.baseOffset;
final length = textEditingValue.selection.extentOffset - index; final length = textEditingValue.selection.extentOffset - index;
final copied = widget.controller.copiedImageUrl!; final copied = controller.copiedImageUrl!;
widget.controller controller.replaceText(
.replaceText(index, length, BlockEmbed.image(copied.item1), null); index, length, BlockEmbed.image(copied.item1), null);
if (copied.item2.isNotEmpty) { if (copied.item2.isNotEmpty) {
widget.controller.formatText( controller.formatText(getEmbedNode(controller, index + 1).item1, 1,
getEmbedNode(widget.controller, index + 1).item1,
1,
StyleAttribute(copied.item2)); StyleAttribute(copied.item2));
} }
widget.controller.copiedImageUrl = null; controller.copiedImageUrl = null;
await Clipboard.setData(const ClipboardData(text: '')); await Clipboard.setData(const ClipboardData(text: ''));
return; return;
} }
@ -1783,7 +1776,7 @@ class _UpdateTextSelectionToAdjacentLineAction<
return; return;
} }
_runSelection = state.textEditingValue.selection; _runSelection = state.textEditingValue.selection;
final currentSelection = state.widget.controller.selection; final currentSelection = state.controller.selection;
final continueCurrentRun = currentSelection.isValid && final continueCurrentRun = currentSelection.isValid &&
currentSelection.isCollapsed && currentSelection.isCollapsed &&
currentSelection.baseOffset == runSelection.baseOffset && currentSelection.baseOffset == runSelection.baseOffset &&

Loading…
Cancel
Save