dartlangeditorflutterflutter-appsflutter-examplesflutter-packageflutter-widgetquillquill-deltaquilljsreactquillrich-textrich-text-editorwysiwygwysiwyg-editor
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1777 lines
59 KiB
1777 lines
59 KiB
4 years ago
|
import 'dart:math' as math;
|
||
2 years ago
|
|
||
1 year ago
|
import 'package:flutter/cupertino.dart'
|
||
|
show CupertinoTheme, cupertinoTextSelectionControls;
|
||
9 months ago
|
import 'package:flutter/foundation.dart'
|
||
|
show ValueListenable, defaultTargetPlatform;
|
||
1 year ago
|
import 'package:flutter/gestures.dart' show PointerDeviceKind;
|
||
4 years ago
|
import 'package:flutter/material.dart';
|
||
|
import 'package:flutter/rendering.dart';
|
||
3 years ago
|
import 'package:flutter/services.dart';
|
||
4 years ago
|
|
||
9 months ago
|
import '../common/utils/platform.dart';
|
||
|
import '../document/document.dart';
|
||
|
import '../document/nodes/container.dart' as container_node;
|
||
|
import '../document/nodes/leaf.dart';
|
||
|
import '../l10n/widgets/localizations.dart';
|
||
|
import 'config/editor_configurations.dart';
|
||
1 year ago
|
import 'editor_builder.dart';
|
||
9 months ago
|
import 'embed/embed_editor_builder.dart';
|
||
|
import 'provider.dart';
|
||
|
import 'raw_editor/config/raw_editor_configurations.dart';
|
||
|
import 'raw_editor/raw_editor.dart';
|
||
|
import 'widgets/box.dart';
|
||
|
import 'widgets/cursor.dart';
|
||
|
import 'widgets/delegate.dart';
|
||
|
import 'widgets/float_cursor.dart';
|
||
|
import 'widgets/text/text_selection.dart';
|
||
4 years ago
|
|
||
4 years ago
|
/// Base interface for editable render objects.
|
||
3 years ago
|
abstract class RenderAbstractEditor implements TextLayoutMetrics {
|
||
4 years ago
|
TextSelection selectWordAtPosition(TextPosition position);
|
||
|
|
||
|
TextSelection selectLineAtPosition(TextPosition position);
|
||
|
|
||
4 years ago
|
/// Returns preferred line height at specified `position` in text.
|
||
4 years ago
|
double preferredLineHeight(TextPosition position);
|
||
|
|
||
4 years ago
|
/// Returns [Rect] for caret in local coordinates
|
||
|
///
|
||
|
/// Useful to enforce visibility of full caret at given position
|
||
|
Rect getLocalRectForCaret(TextPosition position);
|
||
|
|
||
|
/// Returns the local coordinates of the endpoints of the given selection.
|
||
|
///
|
||
|
/// If the selection is collapsed (and therefore occupies a single point), the
|
||
|
/// returned list is of length one. Otherwise, the selection is not collapsed
|
||
|
/// and the returned list is of length two. In this case, however, the two
|
||
|
/// points might actually be co-located (e.g., because of a bidirectional
|
||
|
/// selection that contains some text but whose ends meet in the middle).
|
||
4 years ago
|
TextPosition getPositionForOffset(Offset offset);
|
||
|
|
||
3 years ago
|
/// Returns the local coordinates of the endpoints of the given selection.
|
||
|
///
|
||
|
/// If the selection is collapsed (and therefore occupies a single point), the
|
||
|
/// returned list is of length one. Otherwise, the selection is not collapsed
|
||
|
/// and the returned list is of length two. In this case, however, the two
|
||
|
/// points might actually be co-located (e.g., because of a bidirectional
|
||
|
/// selection that contains some text but whose ends meet in the middle).
|
||
4 years ago
|
List<TextSelectionPoint> getEndpointsForSelection(
|
||
|
TextSelection textSelection);
|
||
|
|
||
3 years ago
|
/// Sets the screen position of the floating cursor and the text position
|
||
|
/// closest to the cursor.
|
||
|
/// `resetLerpValue` drives the size of the floating cursor.
|
||
|
/// See [EditorState.floatingCursorResetController].
|
||
|
void setFloatingCursor(FloatingCursorDragState dragState,
|
||
|
Offset lastBoundedOffset, TextPosition lastTextPosition,
|
||
|
{double? resetLerpValue});
|
||
|
|
||
4 years ago
|
/// If [ignorePointer] is false (the default) then this method is called by
|
||
|
/// the internal gesture recognizer's [TapGestureRecognizer.onTapDown]
|
||
|
/// callback.
|
||
|
///
|
||
|
/// When [ignorePointer] is true, an ancestor widget must respond to tap
|
||
|
/// down events by calling this method.
|
||
4 years ago
|
void handleTapDown(TapDownDetails details);
|
||
|
|
||
4 years ago
|
/// Selects the set words of a paragraph in a given range of global positions.
|
||
|
///
|
||
|
/// The first and last endpoints of the selection will always be at the
|
||
|
/// beginning and end of a word respectively.
|
||
|
///
|
||
|
/// {@macro flutter.rendering.editable.select}
|
||
4 years ago
|
void selectWordsInRange(
|
||
|
Offset from,
|
||
|
Offset to,
|
||
|
SelectionChangedCause cause,
|
||
|
);
|
||
|
|
||
4 years ago
|
/// Move the selection to the beginning or end of a word.
|
||
|
///
|
||
|
/// {@macro flutter.rendering.editable.select}
|
||
4 years ago
|
void selectWordEdge(SelectionChangedCause cause);
|
||
|
|
||
3 years ago
|
///
|
||
|
/// Returns the new selection. Note that the returned value may not be
|
||
|
/// yet reflected in the latest widget state.
|
||
|
///
|
||
|
/// Returns null if no change occurred.
|
||
|
TextSelection? selectPositionAt(
|
||
|
{required Offset from, required SelectionChangedCause cause, Offset? to});
|
||
4 years ago
|
|
||
4 years ago
|
/// Select a word around the location of the last tap down.
|
||
|
///
|
||
|
/// {@macro flutter.rendering.editable.select}
|
||
4 years ago
|
void selectWord(SelectionChangedCause cause);
|
||
|
|
||
4 years ago
|
/// Move selection to the location of the last tap down.
|
||
|
///
|
||
|
/// {@template flutter.rendering.editable.select}
|
||
|
/// This method is mainly used to translate user inputs in global positions
|
||
|
/// into a [TextSelection]. When used in conjunction with a [EditableText],
|
||
|
/// the selection change is fed back into [TextEditingController.selection].
|
||
|
///
|
||
|
/// If you have a [TextEditingController], it's generally easier to
|
||
|
/// programmatically manipulate its `value` or `selection` directly.
|
||
|
/// {@endtemplate}
|
||
3 years ago
|
void selectPosition({required SelectionChangedCause cause});
|
||
4 years ago
|
}
|
||
|
|
||
|
class QuillEditor extends StatefulWidget {
|
||
2 years ago
|
const QuillEditor({
|
||
1 year ago
|
required this.configurations,
|
||
2 years ago
|
required this.focusNode,
|
||
|
required this.scrollController,
|
||
1 year ago
|
super.key,
|
||
|
});
|
||
4 years ago
|
|
||
|
factory QuillEditor.basic({
|
||
1 year ago
|
/// The configurations for the quill editor widget of flutter quill
|
||
1 year ago
|
required QuillEditorConfigurations configurations,
|
||
2 years ago
|
FocusNode? focusNode,
|
||
1 year ago
|
ScrollController? scrollController,
|
||
4 years ago
|
}) {
|
||
|
return QuillEditor(
|
||
1 year ago
|
scrollController: scrollController ?? ScrollController(),
|
||
2 years ago
|
focusNode: focusNode ?? FocusNode(),
|
||
1 year ago
|
configurations: configurations.copyWith(
|
||
1 year ago
|
textSelectionThemeData: configurations.textSelectionThemeData,
|
||
|
autoFocus: configurations.autoFocus,
|
||
|
expands: configurations.expands,
|
||
|
padding: configurations.padding,
|
||
|
keyboardAppearance: configurations.keyboardAppearance,
|
||
|
embedBuilders: configurations.embedBuilders,
|
||
|
editorKey: configurations.editorKey,
|
||
1 year ago
|
),
|
||
4 years ago
|
);
|
||
4 years ago
|
}
|
||
|
|
||
1 year ago
|
/// The configurations for the quill editor widget of flutter quill
|
||
|
final QuillEditorConfigurations configurations;
|
||
3 years ago
|
|
||
|
/// Controls whether this editor has keyboard focus.
|
||
4 years ago
|
final FocusNode focusNode;
|
||
3 years ago
|
|
||
|
/// The [ScrollController] to use when vertically scrolling the contents.
|
||
4 years ago
|
final ScrollController scrollController;
|
||
3 years ago
|
|
||
4 years ago
|
@override
|
||
3 years ago
|
QuillEditorState createState() => QuillEditorState();
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
class QuillEditorState extends State<QuillEditor>
|
||
4 years ago
|
implements EditorTextSelectionGestureDetectorBuilderDelegate {
|
||
2 years ago
|
late GlobalKey<EditorState> _editorKey;
|
||
4 years ago
|
late EditorTextSelectionGestureDetectorBuilder
|
||
|
_selectionGestureDetectorBuilder;
|
||
|
|
||
1 year ago
|
QuillEditorConfigurations get configurations {
|
||
|
return widget.configurations;
|
||
|
}
|
||
|
|
||
4 years ago
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
11 months ago
|
widget.configurations.controller.editorFocusNode ??= widget.focusNode;
|
||
10 months ago
|
if (configurations.autoFocus) {
|
||
|
widget.configurations.controller.editorFocusNode?.requestFocus();
|
||
|
}
|
||
1 year ago
|
_editorKey = configurations.editorKey ?? GlobalKey<EditorState>();
|
||
4 years ago
|
_selectionGestureDetectorBuilder =
|
||
2 years ago
|
_QuillEditorSelectionGestureDetectorBuilder(
|
||
1 year ago
|
this,
|
||
|
configurations.detectWordBoundary,
|
||
|
);
|
||
4 years ago
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
final theme = Theme.of(context);
|
||
1 year ago
|
final selectionTheme =
|
||
1 year ago
|
configurations.textSelectionThemeData ?? TextSelectionTheme.of(context);
|
||
4 years ago
|
|
||
|
TextSelectionControls textSelectionControls;
|
||
|
bool paintCursorAboveText;
|
||
|
bool cursorOpacityAnimates;
|
||
|
Offset? cursorOffset;
|
||
|
Color? cursorColor;
|
||
|
Color selectionColor;
|
||
|
Radius? cursorRadius;
|
||
|
|
||
1 year ago
|
if (isAppleOS(
|
||
|
platform: theme.platform,
|
||
|
supportWeb: true,
|
||
|
)) {
|
||
3 years ago
|
final cupertinoTheme = CupertinoTheme.of(context);
|
||
|
textSelectionControls = cupertinoTextSelectionControls;
|
||
|
paintCursorAboveText = true;
|
||
|
cursorOpacityAnimates = true;
|
||
|
cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor;
|
||
|
selectionColor = selectionTheme.selectionColor ??
|
||
|
cupertinoTheme.primaryColor.withOpacity(0.40);
|
||
|
cursorRadius ??= const Radius.circular(2);
|
||
2 years ago
|
cursorOffset = Offset(
|
||
1 year ago
|
iOSHorizontalOffset / MediaQuery.devicePixelRatioOf(context), 0);
|
||
3 years ago
|
} else {
|
||
3 years ago
|
textSelectionControls = materialTextSelectionControls;
|
||
|
paintCursorAboveText = false;
|
||
|
cursorOpacityAnimates = false;
|
||
|
cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
|
||
|
selectionColor = selectionTheme.selectionColor ??
|
||
|
theme.colorScheme.primary.withOpacity(0.40);
|
||
4 years ago
|
}
|
||
|
|
||
1 year ago
|
final showSelectionToolbar = configurations.enableInteractiveSelection &&
|
||
|
configurations.enableSelectionToolbar;
|
||
|
|
||
1 year ago
|
final child = FlutterQuillLocalizationsWidget(
|
||
|
child: QuillEditorProvider(
|
||
|
editorConfigurations: configurations,
|
||
|
child: QuillEditorBuilderWidget(
|
||
|
builder: configurations.builder,
|
||
|
child: QuillRawEditor(
|
||
|
key: _editorKey,
|
||
|
configurations: QuillRawEditorConfigurations(
|
||
1 year ago
|
controller: configurations.controller,
|
||
1 year ago
|
focusNode: widget.focusNode,
|
||
|
scrollController: widget.scrollController,
|
||
|
scrollable: configurations.scrollable,
|
||
9 months ago
|
enableMarkdownStyleConversion:
|
||
|
configurations.enableMarkdownStyleConversion,
|
||
1 year ago
|
scrollBottomInset: configurations.scrollBottomInset,
|
||
|
padding: configurations.padding,
|
||
1 year ago
|
readOnly: configurations.readOnly,
|
||
12 months ago
|
checkBoxReadOnly: configurations.checkBoxReadOnly,
|
||
1 year ago
|
disableClipboard: configurations.disableClipboard,
|
||
1 year ago
|
placeholder: configurations.placeholder,
|
||
|
onLaunchUrl: configurations.onLaunchUrl,
|
||
|
contextMenuBuilder: showSelectionToolbar
|
||
|
? (configurations.contextMenuBuilder ??
|
||
|
QuillRawEditorConfigurations.defaultContextMenuBuilder)
|
||
|
: null,
|
||
|
showSelectionHandles: isMobile(
|
||
|
platform: theme.platform,
|
||
|
supportWeb: true,
|
||
|
),
|
||
|
showCursor: configurations.showCursor ?? true,
|
||
|
cursorStyle: CursorStyle(
|
||
|
color: cursorColor,
|
||
|
backgroundColor: Colors.grey,
|
||
|
width: 2,
|
||
|
radius: cursorRadius,
|
||
|
offset: cursorOffset,
|
||
|
paintAboveText:
|
||
|
configurations.paintCursorAboveText ?? paintCursorAboveText,
|
||
|
opacityAnimates: cursorOpacityAnimates,
|
||
|
),
|
||
|
textCapitalization: configurations.textCapitalization,
|
||
|
minHeight: configurations.minHeight,
|
||
|
maxHeight: configurations.maxHeight,
|
||
|
maxContentWidth: configurations.maxContentWidth,
|
||
|
customStyles: configurations.customStyles,
|
||
|
expands: configurations.expands,
|
||
|
autoFocus: configurations.autoFocus,
|
||
|
selectionColor: selectionColor,
|
||
|
selectionCtrls:
|
||
|
configurations.textSelectionControls ?? textSelectionControls,
|
||
|
keyboardAppearance: configurations.keyboardAppearance,
|
||
|
enableInteractiveSelection:
|
||
|
configurations.enableInteractiveSelection,
|
||
|
scrollPhysics: configurations.scrollPhysics,
|
||
|
embedBuilder: _getEmbedBuilder,
|
||
|
linkActionPickerDelegate: configurations.linkActionPickerDelegate,
|
||
|
customStyleBuilder: configurations.customStyleBuilder,
|
||
|
customRecognizerBuilder: configurations.customRecognizerBuilder,
|
||
|
floatingCursorDisabled: configurations.floatingCursorDisabled,
|
||
|
onImagePaste: configurations.onImagePaste,
|
||
1 year ago
|
onGifPaste: configurations.onGifPaste,
|
||
1 year ago
|
customShortcuts: configurations.customShortcuts,
|
||
|
customActions: configurations.customActions,
|
||
|
customLinkPrefixes: configurations.customLinkPrefixes,
|
||
|
isOnTapOutsideEnabled: configurations.isOnTapOutsideEnabled,
|
||
|
onTapOutside: configurations.onTapOutside,
|
||
|
dialogTheme: configurations.dialogTheme,
|
||
|
contentInsertionConfiguration:
|
||
|
configurations.contentInsertionConfiguration,
|
||
1 year ago
|
enableScribble: configurations.enableScribble,
|
||
|
onScribbleActivated: configurations.onScribbleActivated,
|
||
|
scribbleAreaInsets: configurations.scribbleAreaInsets,
|
||
11 months ago
|
readOnlyMouseCursor: configurations.readOnlyMouseCursor,
|
||
9 months ago
|
magnifierConfiguration: configurations.magnifierConfiguration,
|
||
1 year ago
|
),
|
||
1 year ago
|
),
|
||
1 year ago
|
),
|
||
3 years ago
|
),
|
||
|
);
|
||
|
|
||
1 year ago
|
final editor = selectionEnabled
|
||
|
? _selectionGestureDetectorBuilder.build(
|
||
|
behavior: HitTestBehavior.translucent,
|
||
|
detectWordBoundary: configurations.detectWordBoundary,
|
||
|
child: child,
|
||
|
)
|
||
|
: child;
|
||
3 years ago
|
|
||
1 year ago
|
if (isWeb()) {
|
||
3 years ago
|
// Intercept RawKeyEvent on Web to prevent it from propagating to parents
|
||
|
// that might interfere with the editor key behavior, such as
|
||
|
// SingleChildScrollView. Thanks to @wliumelb for the workaround.
|
||
|
// See issue https://github.com/singerdmx/flutter-quill/issues/304
|
||
1 year ago
|
return KeyboardListener(
|
||
|
onKeyEvent: (_) {},
|
||
3 years ago
|
focusNode: FocusNode(
|
||
1 year ago
|
onKeyEvent: (node, event) => KeyEventResult.skipRemainingHandlers,
|
||
3 years ago
|
),
|
||
|
child: editor,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
return editor;
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
EmbedBuilder _getEmbedBuilder(Embed node) {
|
||
1 year ago
|
final builders = configurations.embedBuilders;
|
||
2 years ago
|
|
||
3 years ago
|
if (builders != null) {
|
||
|
for (final builder in builders) {
|
||
2 years ago
|
if (builder.key == node.value.type) {
|
||
|
return builder;
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
1 year ago
|
final unknownEmbedBuilder = configurations.unknownEmbedBuilder;
|
||
|
if (unknownEmbedBuilder != null) {
|
||
|
return unknownEmbedBuilder;
|
||
2 years ago
|
}
|
||
3 years ago
|
|
||
|
throw UnimplementedError(
|
||
|
'Embeddable type "${node.value.type}" is not supported by supplied '
|
||
|
'embed builders. You must pass your own builder function to '
|
||
2 years ago
|
'embedBuilders property of QuillEditor or QuillField widgets or '
|
||
|
'specify an unknownEmbedBuilder.',
|
||
3 years ago
|
);
|
||
|
}
|
||
|
|
||
4 years ago
|
@override
|
||
3 years ago
|
GlobalKey<EditorState> get editableTextKey => _editorKey;
|
||
4 years ago
|
|
||
|
@override
|
||
3 years ago
|
bool get forcePressEnabled => false;
|
||
4 years ago
|
|
||
|
@override
|
||
1 year ago
|
bool get selectionEnabled => configurations.enableInteractiveSelection;
|
||
4 years ago
|
|
||
|
void _requestKeyboard() {
|
||
1 year ago
|
final editorCurrentState = _editorKey.currentState;
|
||
|
if (editorCurrentState == null) {
|
||
|
throw ArgumentError.notNull(
|
||
|
'To request keyboard the editor key must not be null',
|
||
|
);
|
||
|
}
|
||
|
editorCurrentState.requestKeyboard();
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
class _QuillEditorSelectionGestureDetectorBuilder
|
||
|
extends EditorTextSelectionGestureDetectorBuilder {
|
||
2 years ago
|
_QuillEditorSelectionGestureDetectorBuilder(
|
||
|
this._state, this._detectWordBoundary)
|
||
2 years ago
|
: super(delegate: _state, detectWordBoundary: _detectWordBoundary);
|
||
4 years ago
|
|
||
3 years ago
|
final QuillEditorState _state;
|
||
2 years ago
|
final bool _detectWordBoundary;
|
||
4 years ago
|
|
||
|
@override
|
||
|
void onForcePressStart(ForcePressDetails details) {
|
||
|
super.onForcePressStart(details);
|
||
3 years ago
|
if (delegate.selectionEnabled && shouldShowSelectionToolbar) {
|
||
3 years ago
|
editor!.showToolbar();
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void onForcePressEnd(ForcePressDetails details) {}
|
||
|
|
||
|
@override
|
||
|
void onSingleLongTapMoveUpdate(LongPressMoveUpdateDetails details) {
|
||
1 year ago
|
if (_state.configurations.onSingleLongTapMoveUpdate != null) {
|
||
3 years ago
|
if (renderEditor != null &&
|
||
1 year ago
|
_state.configurations.onSingleLongTapMoveUpdate!(
|
||
|
details,
|
||
|
renderEditor!.getPositionForOffset,
|
||
|
)) {
|
||
3 years ago
|
return;
|
||
4 years ago
|
}
|
||
|
}
|
||
3 years ago
|
if (!delegate.selectionEnabled) {
|
||
4 years ago
|
return;
|
||
|
}
|
||
3 years ago
|
|
||
1 year ago
|
final platform = Theme.of(_state.context).platform;
|
||
1 year ago
|
if (isAppleOS(
|
||
|
platform: platform,
|
||
|
supportWeb: true,
|
||
|
)) {
|
||
3 years ago
|
renderEditor!.selectPositionAt(
|
||
|
from: details.globalPosition,
|
||
|
cause: SelectionChangedCause.longPress,
|
||
|
);
|
||
3 years ago
|
} else {
|
||
3 years ago
|
renderEditor!.selectWordsInRange(
|
||
|
details.globalPosition - details.offsetFromOrigin,
|
||
|
details.globalPosition,
|
||
|
SelectionChangedCause.longPress,
|
||
|
);
|
||
4 years ago
|
}
|
||
9 months ago
|
editor?.updateMagnifier(details.globalPosition);
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
bool _isPositionSelected(TapUpDetails details) {
|
||
1 year ago
|
if (_state.configurations.controller.document.isEmpty()) {
|
||
4 years ago
|
return false;
|
||
|
}
|
||
3 years ago
|
final pos = renderEditor!.getPositionForOffset(details.globalPosition);
|
||
1 year ago
|
final result = editor!.widget.configurations.controller.document
|
||
|
.querySegmentLeafNode(pos.offset);
|
||
2 years ago
|
final line = result.line;
|
||
3 years ago
|
if (line == null) {
|
||
4 years ago
|
return false;
|
||
|
}
|
||
2 years ago
|
final segmentLeaf = result.leaf;
|
||
3 years ago
|
if (segmentLeaf == null && line.length == 1) {
|
||
1 year ago
|
editor!.widget.configurations.controller.updateSelection(
|
||
1 year ago
|
TextSelection.collapsed(offset: pos.offset),
|
||
1 year ago
|
ChangeSource.local,
|
||
1 year ago
|
);
|
||
3 years ago
|
return true;
|
||
4 years ago
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void onTapDown(TapDownDetails details) {
|
||
1 year ago
|
if (_state.configurations.onTapDown != null) {
|
||
3 years ago
|
if (renderEditor != null &&
|
||
1 year ago
|
_state.configurations.onTapDown!(
|
||
|
details,
|
||
|
renderEditor!.getPositionForOffset,
|
||
|
)) {
|
||
3 years ago
|
return;
|
||
4 years ago
|
}
|
||
|
}
|
||
|
super.onTapDown(details);
|
||
|
}
|
||
|
|
||
3 years ago
|
bool isShiftClick(PointerDeviceKind deviceKind) {
|
||
1 year ago
|
final pressed = HardwareKeyboard.instance.logicalKeysPressed;
|
||
3 years ago
|
return deviceKind == PointerDeviceKind.mouse &&
|
||
1 year ago
|
(pressed.contains(LogicalKeyboardKey.shiftLeft) ||
|
||
|
pressed.contains(LogicalKeyboardKey.shiftRight));
|
||
3 years ago
|
}
|
||
|
|
||
4 years ago
|
@override
|
||
|
void onSingleTapUp(TapUpDetails details) {
|
||
1 year ago
|
if (_state.configurations.onTapUp != null &&
|
||
3 years ago
|
renderEditor != null &&
|
||
1 year ago
|
_state.configurations.onTapUp!(
|
||
|
details,
|
||
|
renderEditor!.getPositionForOffset,
|
||
|
)) {
|
||
3 years ago
|
return;
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
editor!.hideToolbar();
|
||
4 years ago
|
|
||
3 years ago
|
try {
|
||
|
if (delegate.selectionEnabled && !_isPositionSelected(details)) {
|
||
1 year ago
|
final platform = Theme.of(_state.context).platform;
|
||
1 year ago
|
if (isAppleOS(platform: platform, supportWeb: true) ||
|
||
|
isDesktop(platform: platform, supportWeb: true)) {
|
||
3 years ago
|
// added isDesktop() to enable extend selection in Windows platform
|
||
3 years ago
|
switch (details.kind) {
|
||
|
case PointerDeviceKind.mouse:
|
||
|
case PointerDeviceKind.stylus:
|
||
|
case PointerDeviceKind.invertedStylus:
|
||
|
// Precise devices should place the cursor at a precise position.
|
||
|
// If `Shift` key is pressed then
|
||
|
// extend current selection instead.
|
||
|
if (isShiftClick(details.kind)) {
|
||
|
renderEditor!
|
||
|
..extendSelection(details.globalPosition,
|
||
|
cause: SelectionChangedCause.tap)
|
||
|
..onSelectionCompleted();
|
||
|
} else {
|
||
|
renderEditor!
|
||
|
..selectPosition(cause: SelectionChangedCause.tap)
|
||
|
..onSelectionCompleted();
|
||
|
}
|
||
|
|
||
|
break;
|
||
|
case PointerDeviceKind.touch:
|
||
|
case PointerDeviceKind.unknown:
|
||
|
// On macOS/iOS/iPadOS a touch tap places the cursor at the edge
|
||
|
// of the word.
|
||
2 years ago
|
if (_detectWordBoundary) {
|
||
|
renderEditor!
|
||
|
..selectWordEdge(SelectionChangedCause.tap)
|
||
|
..onSelectionCompleted();
|
||
|
} else {
|
||
|
renderEditor!
|
||
|
..selectPosition(cause: SelectionChangedCause.tap)
|
||
|
..onSelectionCompleted();
|
||
|
}
|
||
3 years ago
|
break;
|
||
3 years ago
|
case PointerDeviceKind.trackpad:
|
||
|
// TODO: Handle this case.
|
||
|
break;
|
||
3 years ago
|
}
|
||
|
} else {
|
||
|
renderEditor!
|
||
|
..selectPosition(cause: SelectionChangedCause.tap)
|
||
|
..onSelectionCompleted();
|
||
3 years ago
|
}
|
||
4 years ago
|
}
|
||
3 years ago
|
} finally {
|
||
|
_state._requestKeyboard();
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void onSingleLongTapStart(LongPressStartDetails details) {
|
||
1 year ago
|
if (_state.configurations.onSingleLongTapStart != null) {
|
||
3 years ago
|
if (renderEditor != null &&
|
||
1 year ago
|
_state.configurations.onSingleLongTapStart!(
|
||
|
details,
|
||
|
renderEditor!.getPositionForOffset,
|
||
|
)) {
|
||
3 years ago
|
return;
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
if (delegate.selectionEnabled) {
|
||
1 year ago
|
final platform = Theme.of(_state.context).platform;
|
||
1 year ago
|
if (isAppleOS(
|
||
|
platform: platform,
|
||
|
supportWeb: true,
|
||
|
)) {
|
||
3 years ago
|
renderEditor!.selectPositionAt(
|
||
|
from: details.globalPosition,
|
||
|
cause: SelectionChangedCause.longPress,
|
||
|
);
|
||
3 years ago
|
} else {
|
||
3 years ago
|
renderEditor!.selectWord(SelectionChangedCause.longPress);
|
||
|
Feedback.forLongPress(_state.context);
|
||
4 years ago
|
}
|
||
|
}
|
||
9 months ago
|
|
||
|
_showMagnifierIfSupportedByPlatform(details.globalPosition);
|
||
4 years ago
|
}
|
||
|
|
||
|
@override
|
||
|
void onSingleLongTapEnd(LongPressEndDetails details) {
|
||
1 year ago
|
if (_state.configurations.onSingleLongTapEnd != null) {
|
||
4 years ago
|
if (renderEditor != null) {
|
||
1 year ago
|
if (_state.configurations.onSingleLongTapEnd!(
|
||
|
details,
|
||
|
renderEditor!.getPositionForOffset,
|
||
|
)) {
|
||
4 years ago
|
return;
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
if (delegate.selectionEnabled) {
|
||
3 years ago
|
renderEditor!.onSelectionCompleted();
|
||
3 years ago
|
}
|
||
4 years ago
|
}
|
||
|
}
|
||
9 months ago
|
_hideMagnifierIfSupportedByPlatform();
|
||
4 years ago
|
super.onSingleLongTapEnd(details);
|
||
|
}
|
||
9 months ago
|
|
||
|
void _showMagnifierIfSupportedByPlatform(Offset positionToShow) {
|
||
|
switch (defaultTargetPlatform) {
|
||
|
case TargetPlatform.android:
|
||
|
case TargetPlatform.iOS:
|
||
|
editor?.showMagnifier(positionToShow);
|
||
|
default:
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void _hideMagnifierIfSupportedByPlatform() {
|
||
|
switch (defaultTargetPlatform) {
|
||
|
case TargetPlatform.android:
|
||
|
case TargetPlatform.iOS:
|
||
|
editor?.hideMagnifier();
|
||
|
default:
|
||
|
}
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
/// Signature for the callback that reports when the user changes the selection
|
||
|
/// (including the cursor location).
|
||
|
///
|
||
|
/// Used by [RenderEditor.onSelectionChanged].
|
||
4 years ago
|
typedef TextSelectionChangedHandler = void Function(
|
||
|
TextSelection selection, SelectionChangedCause cause);
|
||
|
|
||
3 years ago
|
/// Signature for the callback that reports when a selection action is actually
|
||
|
/// completed and ratified. Completion is defined as when the user input has
|
||
|
/// concluded for an entire selection action. For simple taps and keyboard input
|
||
|
/// events that change the selection, this callback is invoked immediately
|
||
|
/// following the TextSelectionChangedHandler. For long taps, the selection is
|
||
|
/// considered complete at the up event of a long tap. For drag selections, the
|
||
|
/// selection completes once the drag/pan event ends or is interrupted.
|
||
|
///
|
||
|
/// Used by [RenderEditor.onSelectionCompleted].
|
||
|
typedef TextSelectionCompletedHandler = void Function();
|
||
|
|
||
3 years ago
|
// The padding applied to text field. Used to determine the bounds when
|
||
|
// moving the floating cursor.
|
||
|
const EdgeInsets _kFloatingCursorAddedMargin = EdgeInsets.fromLTRB(4, 4, 4, 5);
|
||
|
|
||
|
// The additional size on the x and y axis with which to expand the prototype
|
||
|
// cursor to render the floating cursor in pixels.
|
||
|
const EdgeInsets _kFloatingCaretSizeIncrease =
|
||
|
EdgeInsets.symmetric(horizontal: 0.5, vertical: 1);
|
||
|
|
||
3 years ago
|
/// Displays a document as a vertical list of document segments (lines
|
||
|
/// and blocks).
|
||
|
///
|
||
|
/// Children of [RenderEditor] must be instances of [RenderEditableBox].
|
||
4 years ago
|
class RenderEditor extends RenderEditableContainerBox
|
||
3 years ago
|
with RelayoutWhenSystemFontsChangeMixin
|
||
4 years ago
|
implements RenderAbstractEditor {
|
||
3 years ago
|
RenderEditor({
|
||
|
required this.document,
|
||
1 year ago
|
required super.textDirection,
|
||
3 years ago
|
required bool hasFocus,
|
||
|
required this.selection,
|
||
3 years ago
|
required this.scrollable,
|
||
3 years ago
|
required LayerLink startHandleLayerLink,
|
||
|
required LayerLink endHandleLayerLink,
|
||
1 year ago
|
required super.padding,
|
||
3 years ago
|
required CursorCont cursorController,
|
||
|
required this.onSelectionChanged,
|
||
3 years ago
|
required this.onSelectionCompleted,
|
||
1 year ago
|
required super.scrollBottomInset,
|
||
3 years ago
|
required this.floatingCursorDisabled,
|
||
|
ViewportOffset? offset,
|
||
1 year ago
|
super.children,
|
||
3 years ago
|
EdgeInsets floatingCursorAddedMargin =
|
||
|
const EdgeInsets.fromLTRB(4, 4, 4, 5),
|
||
3 years ago
|
double? maxContentWidth,
|
||
3 years ago
|
}) : _hasFocus = hasFocus,
|
||
|
_extendSelectionOrigin = selection,
|
||
|
_startHandleLayerLink = startHandleLayerLink,
|
||
|
_endHandleLayerLink = endHandleLayerLink,
|
||
|
_cursorController = cursorController,
|
||
3 years ago
|
_maxContentWidth = maxContentWidth,
|
||
3 years ago
|
super(
|
||
3 years ago
|
container: document.root,
|
||
4 years ago
|
);
|
||
|
|
||
3 years ago
|
final CursorCont _cursorController;
|
||
3 years ago
|
final bool floatingCursorDisabled;
|
||
3 years ago
|
final bool scrollable;
|
||
3 years ago
|
|
||
4 years ago
|
Document document;
|
||
|
TextSelection selection;
|
||
|
bool _hasFocus = false;
|
||
|
LayerLink _startHandleLayerLink;
|
||
|
LayerLink _endHandleLayerLink;
|
||
3 years ago
|
|
||
|
/// Called when the selection changes.
|
||
4 years ago
|
TextSelectionChangedHandler onSelectionChanged;
|
||
3 years ago
|
TextSelectionCompletedHandler onSelectionCompleted;
|
||
4 years ago
|
final ValueNotifier<bool> _selectionStartInViewport =
|
||
|
ValueNotifier<bool>(true);
|
||
|
|
||
|
ValueListenable<bool> get selectionStartInViewport =>
|
||
|
_selectionStartInViewport;
|
||
|
|
||
|
ValueListenable<bool> get selectionEndInViewport => _selectionEndInViewport;
|
||
|
final ValueNotifier<bool> _selectionEndInViewport = ValueNotifier<bool>(true);
|
||
|
|
||
4 years ago
|
void _updateSelectionExtentsVisibility(Offset effectiveOffset) {
|
||
|
final visibleRegion = Offset.zero & size;
|
||
|
final startPosition =
|
||
|
TextPosition(offset: selection.start, affinity: selection.affinity);
|
||
|
final startOffset = _getOffsetForCaret(startPosition);
|
||
|
// TODO(justinmc): https://github.com/flutter/flutter/issues/31495
|
||
|
// Check if the selection is visible with an approximation because a
|
||
|
// difference between rounded and unrounded values causes the caret to be
|
||
|
// reported as having a slightly (< 0.5) negative y offset. This rounding
|
||
|
// happens in paragraph.cc's layout and TextPainer's
|
||
|
// _applyFloatingPointHack. Ideally, the rounding mismatch will be fixed and
|
||
|
// this can be changed to be a strict check instead of an approximation.
|
||
|
const visibleRegionSlop = 0.5;
|
||
|
_selectionStartInViewport.value = visibleRegion
|
||
|
.inflate(visibleRegionSlop)
|
||
|
.contains(startOffset + effectiveOffset);
|
||
|
|
||
|
final endPosition =
|
||
|
TextPosition(offset: selection.end, affinity: selection.affinity);
|
||
|
final endOffset = _getOffsetForCaret(endPosition);
|
||
|
_selectionEndInViewport.value = visibleRegion
|
||
|
.inflate(visibleRegionSlop)
|
||
|
.contains(endOffset + effectiveOffset);
|
||
|
}
|
||
|
|
||
|
// returns offset relative to this at which the caret will be painted
|
||
|
// given a global TextPosition
|
||
|
Offset _getOffsetForCaret(TextPosition position) {
|
||
|
final child = childAtPosition(position);
|
||
|
final childPosition = child.globalToLocalPosition(position);
|
||
|
final boxParentData = child.parentData as BoxParentData;
|
||
|
final localOffsetForCaret = child.getOffsetForCaret(childPosition);
|
||
|
return boxParentData.offset + localOffsetForCaret;
|
||
|
}
|
||
|
|
||
4 years ago
|
void setDocument(Document doc) {
|
||
|
if (document == doc) {
|
||
|
return;
|
||
|
}
|
||
|
document = doc;
|
||
|
markNeedsLayout();
|
||
|
}
|
||
|
|
||
|
void setHasFocus(bool h) {
|
||
|
if (_hasFocus == h) {
|
||
|
return;
|
||
|
}
|
||
|
_hasFocus = h;
|
||
|
markNeedsSemanticsUpdate();
|
||
|
}
|
||
|
|
||
4 years ago
|
Offset get _paintOffset => Offset(0, -(offset?.pixels ?? 0.0));
|
||
|
|
||
|
ViewportOffset? get offset => _offset;
|
||
|
ViewportOffset? _offset;
|
||
|
|
||
|
set offset(ViewportOffset? value) {
|
||
|
if (_offset == value) return;
|
||
|
if (attached) _offset?.removeListener(markNeedsPaint);
|
||
|
_offset = value;
|
||
|
if (attached) _offset?.addListener(markNeedsPaint);
|
||
|
markNeedsLayout();
|
||
|
}
|
||
|
|
||
4 years ago
|
void setSelection(TextSelection t) {
|
||
|
if (selection == t) {
|
||
|
return;
|
||
|
}
|
||
|
selection = t;
|
||
|
markNeedsPaint();
|
||
3 years ago
|
|
||
|
if (!_shiftPressed && !_isDragging) {
|
||
|
// Only update extend selection origin if Shift key is not pressed and
|
||
|
// user is not dragging selection.
|
||
|
_extendSelectionOrigin = selection;
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
bool get _shiftPressed =>
|
||
1 year ago
|
HardwareKeyboard.instance.logicalKeysPressed
|
||
|
.contains(LogicalKeyboardKey.shiftLeft) ||
|
||
|
HardwareKeyboard.instance.logicalKeysPressed
|
||
|
.contains(LogicalKeyboardKey.shiftRight);
|
||
3 years ago
|
|
||
4 years ago
|
void setStartHandleLayerLink(LayerLink value) {
|
||
|
if (_startHandleLayerLink == value) {
|
||
|
return;
|
||
|
}
|
||
|
_startHandleLayerLink = value;
|
||
|
markNeedsPaint();
|
||
|
}
|
||
|
|
||
|
void setEndHandleLayerLink(LayerLink value) {
|
||
|
if (_endHandleLayerLink == value) {
|
||
|
return;
|
||
|
}
|
||
|
_endHandleLayerLink = value;
|
||
|
markNeedsPaint();
|
||
|
}
|
||
|
|
||
|
void setScrollBottomInset(double value) {
|
||
|
if (scrollBottomInset == value) {
|
||
|
return;
|
||
|
}
|
||
|
scrollBottomInset = value;
|
||
|
markNeedsPaint();
|
||
|
}
|
||
|
|
||
3 years ago
|
double? _maxContentWidth;
|
||
2 years ago
|
|
||
3 years ago
|
set maxContentWidth(double? value) {
|
||
|
if (_maxContentWidth == value) return;
|
||
|
_maxContentWidth = value;
|
||
|
markNeedsLayout();
|
||
|
}
|
||
|
|
||
4 years ago
|
@override
|
||
|
List<TextSelectionPoint> getEndpointsForSelection(
|
||
|
TextSelection textSelection) {
|
||
|
if (textSelection.isCollapsed) {
|
||
|
final child = childAtPosition(textSelection.extent);
|
||
|
final localPosition = TextPosition(
|
||
2 years ago
|
offset: textSelection.extentOffset - child.container.offset,
|
||
|
affinity: textSelection.affinity,
|
||
|
);
|
||
4 years ago
|
final localOffset = child.getOffsetForCaret(localPosition);
|
||
|
final parentData = child.parentData as BoxParentData;
|
||
|
return <TextSelectionPoint>[
|
||
|
TextSelectionPoint(
|
||
|
Offset(0, child.preferredLineHeight(localPosition)) +
|
||
|
localOffset +
|
||
|
parentData.offset,
|
||
|
null)
|
||
|
];
|
||
|
}
|
||
|
|
||
|
final baseNode = _container.queryChild(textSelection.start, false).node;
|
||
|
|
||
|
var baseChild = firstChild;
|
||
|
while (baseChild != null) {
|
||
3 years ago
|
if (baseChild.container == baseNode) {
|
||
4 years ago
|
break;
|
||
|
}
|
||
|
baseChild = childAfter(baseChild);
|
||
|
}
|
||
|
assert(baseChild != null);
|
||
|
|
||
|
final baseParentData = baseChild!.parentData as BoxParentData;
|
||
|
final baseSelection =
|
||
3 years ago
|
localSelection(baseChild.container, textSelection, true);
|
||
4 years ago
|
var basePoint = baseChild.getBaseEndpointForSelection(baseSelection);
|
||
|
basePoint = TextSelectionPoint(
|
||
1 year ago
|
basePoint.point + baseParentData.offset,
|
||
|
basePoint.direction,
|
||
|
);
|
||
4 years ago
|
|
||
|
final extentNode = _container.queryChild(textSelection.end, false).node;
|
||
|
RenderEditableBox? extentChild = baseChild;
|
||
|
while (extentChild != null) {
|
||
3 years ago
|
if (extentChild.container == extentNode) {
|
||
4 years ago
|
break;
|
||
|
}
|
||
|
extentChild = childAfter(extentChild);
|
||
|
}
|
||
|
assert(extentChild != null);
|
||
|
|
||
|
final extentParentData = extentChild!.parentData as BoxParentData;
|
||
|
final extentSelection =
|
||
3 years ago
|
localSelection(extentChild.container, textSelection, true);
|
||
4 years ago
|
var extentPoint =
|
||
|
extentChild.getExtentEndpointForSelection(extentSelection);
|
||
|
extentPoint = TextSelectionPoint(
|
||
1 year ago
|
extentPoint.point + extentParentData.offset,
|
||
|
extentPoint.direction,
|
||
|
);
|
||
4 years ago
|
|
||
|
return <TextSelectionPoint>[basePoint, extentPoint];
|
||
|
}
|
||
|
|
||
|
Offset? _lastTapDownPosition;
|
||
|
|
||
3 years ago
|
// Used on Desktop (mouse and keyboard enabled platforms) as base offset
|
||
|
// for extending selection, either with combination of `Shift` + Click or
|
||
|
// by dragging
|
||
|
TextSelection? _extendSelectionOrigin;
|
||
|
|
||
4 years ago
|
@override
|
||
|
void handleTapDown(TapDownDetails details) {
|
||
|
_lastTapDownPosition = details.globalPosition;
|
||
|
}
|
||
|
|
||
3 years ago
|
bool _isDragging = false;
|
||
|
|
||
|
void handleDragStart(DragStartDetails details) {
|
||
|
_isDragging = true;
|
||
|
|
||
|
final newSelection = selectPositionAt(
|
||
|
from: details.globalPosition,
|
||
|
cause: SelectionChangedCause.drag,
|
||
|
);
|
||
|
|
||
|
if (newSelection == null) return;
|
||
|
// Make sure to remember the origin for extend selection.
|
||
|
_extendSelectionOrigin = newSelection;
|
||
|
}
|
||
|
|
||
|
void handleDragEnd(DragEndDetails details) {
|
||
|
_isDragging = false;
|
||
3 years ago
|
onSelectionCompleted();
|
||
3 years ago
|
}
|
||
|
|
||
4 years ago
|
@override
|
||
|
void selectWordsInRange(
|
||
|
Offset from,
|
||
|
Offset? to,
|
||
|
SelectionChangedCause cause,
|
||
|
) {
|
||
|
final firstPosition = getPositionForOffset(from);
|
||
|
final firstWord = selectWordAtPosition(firstPosition);
|
||
|
final lastWord =
|
||
|
to == null ? firstWord : selectWordAtPosition(getPositionForOffset(to));
|
||
|
|
||
|
_handleSelectionChange(
|
||
|
TextSelection(
|
||
|
baseOffset: firstWord.base.offset,
|
||
|
extentOffset: lastWord.extent.offset,
|
||
|
affinity: firstWord.affinity,
|
||
|
),
|
||
|
cause,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
void _handleSelectionChange(
|
||
|
TextSelection nextSelection,
|
||
|
SelectionChangedCause cause,
|
||
|
) {
|
||
|
final focusingEmpty = nextSelection.baseOffset == 0 &&
|
||
|
nextSelection.extentOffset == 0 &&
|
||
|
!_hasFocus;
|
||
|
if (nextSelection == selection &&
|
||
|
cause != SelectionChangedCause.keyboard &&
|
||
|
!focusingEmpty) {
|
||
|
return;
|
||
|
}
|
||
|
onSelectionChanged(nextSelection, cause);
|
||
|
}
|
||
|
|
||
3 years ago
|
/// Extends current selection to the position closest to specified offset.
|
||
|
void extendSelection(Offset to, {required SelectionChangedCause cause}) {
|
||
|
/// The below logic does not exactly match the native version because
|
||
|
/// we do not allow swapping of base and extent positions.
|
||
|
assert(_extendSelectionOrigin != null);
|
||
|
final position = getPositionForOffset(to);
|
||
|
|
||
|
if (position.offset < _extendSelectionOrigin!.baseOffset) {
|
||
|
_handleSelectionChange(
|
||
|
TextSelection(
|
||
|
baseOffset: position.offset,
|
||
|
extentOffset: _extendSelectionOrigin!.extentOffset,
|
||
|
affinity: selection.affinity,
|
||
|
),
|
||
|
cause,
|
||
|
);
|
||
|
} else if (position.offset > _extendSelectionOrigin!.extentOffset) {
|
||
|
_handleSelectionChange(
|
||
|
TextSelection(
|
||
|
baseOffset: _extendSelectionOrigin!.baseOffset,
|
||
|
extentOffset: position.offset,
|
||
|
affinity: selection.affinity,
|
||
|
),
|
||
|
cause,
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
@override
|
||
|
void selectWordEdge(SelectionChangedCause cause) {
|
||
|
assert(_lastTapDownPosition != null);
|
||
|
final position = getPositionForOffset(_lastTapDownPosition!);
|
||
|
final child = childAtPosition(position);
|
||
3 years ago
|
final nodeOffset = child.container.offset;
|
||
4 years ago
|
final localPosition = TextPosition(
|
||
|
offset: position.offset - nodeOffset,
|
||
|
affinity: position.affinity,
|
||
|
);
|
||
|
final localWord = child.getWordBoundary(localPosition);
|
||
|
final word = TextRange(
|
||
|
start: localWord.start + nodeOffset,
|
||
|
end: localWord.end + nodeOffset,
|
||
|
);
|
||
2 years ago
|
if (position.offset - word.start <= 1 && word.end != position.offset) {
|
||
4 years ago
|
_handleSelectionChange(
|
||
|
TextSelection.collapsed(offset: word.start),
|
||
|
cause,
|
||
|
);
|
||
|
} else {
|
||
|
_handleSelectionChange(
|
||
|
TextSelection.collapsed(
|
||
|
offset: word.end, affinity: TextAffinity.upstream),
|
||
|
cause,
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@override
|
||
3 years ago
|
TextSelection? selectPositionAt({
|
||
|
required Offset from,
|
||
|
required SelectionChangedCause cause,
|
||
4 years ago
|
Offset? to,
|
||
3 years ago
|
}) {
|
||
4 years ago
|
final fromPosition = getPositionForOffset(from);
|
||
|
final toPosition = to == null ? null : getPositionForOffset(to);
|
||
|
|
||
|
var baseOffset = fromPosition.offset;
|
||
|
var extentOffset = fromPosition.offset;
|
||
|
if (toPosition != null) {
|
||
|
baseOffset = math.min(fromPosition.offset, toPosition.offset);
|
||
|
extentOffset = math.max(fromPosition.offset, toPosition.offset);
|
||
|
}
|
||
|
|
||
|
final newSelection = TextSelection(
|
||
|
baseOffset: baseOffset,
|
||
|
extentOffset: extentOffset,
|
||
|
affinity: fromPosition.affinity,
|
||
|
);
|
||
3 years ago
|
|
||
|
// Call [onSelectionChanged] only when the selection actually changed.
|
||
4 years ago
|
_handleSelectionChange(newSelection, cause);
|
||
3 years ago
|
return newSelection;
|
||
4 years ago
|
}
|
||
|
|
||
|
@override
|
||
|
void selectWord(SelectionChangedCause cause) {
|
||
|
selectWordsInRange(_lastTapDownPosition!, null, cause);
|
||
|
}
|
||
|
|
||
|
@override
|
||
3 years ago
|
void selectPosition({required SelectionChangedCause cause}) {
|
||
|
selectPositionAt(from: _lastTapDownPosition!, cause: cause);
|
||
4 years ago
|
}
|
||
|
|
||
|
@override
|
||
|
TextSelection selectWordAtPosition(TextPosition position) {
|
||
3 years ago
|
final word = getWordBoundary(position);
|
||
|
// When long-pressing past the end of the text, we want a collapsed cursor.
|
||
4 years ago
|
if (position.offset >= word.end) {
|
||
|
return TextSelection.fromPosition(position);
|
||
|
}
|
||
|
return TextSelection(baseOffset: word.start, extentOffset: word.end);
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
TextSelection selectLineAtPosition(TextPosition position) {
|
||
3 years ago
|
final line = getLineAtOffset(position);
|
||
4 years ago
|
|
||
3 years ago
|
// When long-pressing past the end of the text, we want a collapsed cursor.
|
||
4 years ago
|
if (position.offset >= line.end) {
|
||
|
return TextSelection.fromPosition(position);
|
||
|
}
|
||
|
return TextSelection(baseOffset: line.start, extentOffset: line.end);
|
||
|
}
|
||
|
|
||
3 years ago
|
@override
|
||
|
void performLayout() {
|
||
|
assert(() {
|
||
3 years ago
|
if (!scrollable || !constraints.hasBoundedHeight) return true;
|
||
3 years ago
|
throw FlutterError.fromParts(<DiagnosticsNode>[
|
||
|
ErrorSummary('RenderEditableContainerBox must have '
|
||
3 years ago
|
'unlimited space along its main axis when it is scrollable.'),
|
||
3 years ago
|
ErrorDescription('RenderEditableContainerBox does not clip or'
|
||
|
' resize its children, so it must be '
|
||
|
'placed in a parent that does not constrain the main '
|
||
|
'axis.'),
|
||
|
ErrorHint(
|
||
|
'You probably want to put the RenderEditableContainerBox inside a '
|
||
3 years ago
|
'RenderViewport with a matching main axis or disable the '
|
||
|
'scrollable property.')
|
||
3 years ago
|
]);
|
||
|
}());
|
||
|
assert(() {
|
||
|
if (constraints.hasBoundedWidth) return true;
|
||
|
throw FlutterError.fromParts(<DiagnosticsNode>[
|
||
|
ErrorSummary('RenderEditableContainerBox must have a bounded'
|
||
|
' constraint for its cross axis.'),
|
||
|
ErrorDescription('RenderEditableContainerBox forces its children to '
|
||
|
"expand to fit the RenderEditableContainerBox's container, "
|
||
|
'so it must be placed in a parent that constrains the cross '
|
||
|
'axis to a finite dimension.'),
|
||
|
]);
|
||
|
}());
|
||
|
|
||
|
resolvePadding();
|
||
|
assert(resolvedPadding != null);
|
||
|
|
||
|
var mainAxisExtent = resolvedPadding!.top;
|
||
|
var child = firstChild;
|
||
|
final innerConstraints = BoxConstraints.tightFor(
|
||
|
width: math.min(
|
||
|
_maxContentWidth ?? double.infinity, constraints.maxWidth))
|
||
|
.deflate(resolvedPadding!);
|
||
|
final leftOffset = _maxContentWidth == null
|
||
|
? 0.0
|
||
|
: math.max((constraints.maxWidth - _maxContentWidth!) / 2, 0);
|
||
|
while (child != null) {
|
||
|
child.layout(innerConstraints, parentUsesSize: true);
|
||
|
final childParentData = child.parentData as EditableContainerParentData
|
||
|
..offset = Offset(resolvedPadding!.left + leftOffset, mainAxisExtent);
|
||
|
mainAxisExtent += child.size.height;
|
||
|
assert(child.parentData == childParentData);
|
||
|
child = childParentData.nextSibling;
|
||
|
}
|
||
|
mainAxisExtent += resolvedPadding!.bottom;
|
||
|
size = constraints.constrain(Size(constraints.maxWidth, mainAxisExtent));
|
||
|
|
||
|
assert(size.isFinite);
|
||
|
}
|
||
|
|
||
4 years ago
|
@override
|
||
|
void paint(PaintingContext context, Offset offset) {
|
||
3 years ago
|
if (_hasFocus &&
|
||
|
_cursorController.show.value &&
|
||
|
!_cursorController.style.paintAboveText) {
|
||
|
_paintFloatingCursor(context, offset);
|
||
|
}
|
||
4 years ago
|
defaultPaint(context, offset);
|
||
4 years ago
|
_updateSelectionExtentsVisibility(offset + _paintOffset);
|
||
4 years ago
|
_paintHandleLayers(context, getEndpointsForSelection(selection));
|
||
3 years ago
|
|
||
|
if (_hasFocus &&
|
||
|
_cursorController.show.value &&
|
||
|
_cursorController.style.paintAboveText) {
|
||
|
_paintFloatingCursor(context, offset);
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
|
@override
|
||
|
bool hitTestChildren(BoxHitTestResult result, {required Offset position}) {
|
||
|
return defaultHitTestChildren(result, position: position);
|
||
|
}
|
||
|
|
||
|
void _paintHandleLayers(
|
||
|
PaintingContext context, List<TextSelectionPoint> endpoints) {
|
||
|
var startPoint = endpoints[0].point;
|
||
|
startPoint = Offset(
|
||
|
startPoint.dx.clamp(0.0, size.width),
|
||
|
startPoint.dy.clamp(0.0, size.height),
|
||
|
);
|
||
|
context.pushLayer(
|
||
|
LeaderLayer(link: _startHandleLayerLink, offset: startPoint),
|
||
|
super.paint,
|
||
|
Offset.zero,
|
||
|
);
|
||
|
if (endpoints.length == 2) {
|
||
|
var endPoint = endpoints[1].point;
|
||
|
endPoint = Offset(
|
||
|
endPoint.dx.clamp(0.0, size.width),
|
||
|
endPoint.dy.clamp(0.0, size.height),
|
||
|
);
|
||
|
context.pushLayer(
|
||
|
LeaderLayer(link: _endHandleLayerLink, offset: endPoint),
|
||
|
super.paint,
|
||
|
Offset.zero,
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
double preferredLineHeight(TextPosition position) {
|
||
|
final child = childAtPosition(position);
|
||
|
return child.preferredLineHeight(
|
||
3 years ago
|
TextPosition(offset: position.offset - child.container.offset));
|
||
4 years ago
|
}
|
||
|
|
||
|
@override
|
||
|
TextPosition getPositionForOffset(Offset offset) {
|
||
|
final local = globalToLocal(offset);
|
||
3 years ago
|
final child = childAtOffset(local);
|
||
4 years ago
|
|
||
|
final parentData = child.parentData as BoxParentData;
|
||
|
final localOffset = local - parentData.offset;
|
||
|
final localPosition = child.getPositionForOffset(localOffset);
|
||
|
return TextPosition(
|
||
3 years ago
|
offset: localPosition.offset + child.container.offset,
|
||
4 years ago
|
affinity: localPosition.affinity,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
/// Returns the y-offset of the editor at which [selection] is visible.
|
||
|
///
|
||
|
/// The offset is the distance from the top of the editor and is the minimum
|
||
|
/// from the current scroll position until [selection] becomes visible.
|
||
|
/// Returns null if [selection] is already visible.
|
||
3 years ago
|
///
|
||
|
/// Finds the closest scroll offset that fully reveals the editing cursor.
|
||
|
///
|
||
|
/// The `scrollOffset` parameter represents current scroll offset in the
|
||
|
/// parent viewport.
|
||
|
///
|
||
|
/// The `offsetInViewport` parameter represents the editor's vertical offset
|
||
|
/// in the parent viewport. This value should normally be 0.0 if this editor
|
||
|
/// is the only child of the viewport or if it's the topmost child. Otherwise
|
||
|
/// it should be a positive value equal to total height of all siblings of
|
||
|
/// this editor from above it.
|
||
|
///
|
||
|
/// Returns `null` if the cursor is currently visible.
|
||
4 years ago
|
double? getOffsetToRevealCursor(
|
||
|
double viewportHeight, double scrollOffset, double offsetInViewport) {
|
||
3 years ago
|
// Endpoints coordinates represents lower left or lower right corner of
|
||
|
// the selection. If we want to scroll up to reveal the caret we need to
|
||
|
// adjust the dy value by the height of the line. We also add a small margin
|
||
|
// so that the caret is not too close to the edge of the viewport.
|
||
4 years ago
|
final endpoints = getEndpointsForSelection(selection);
|
||
4 years ago
|
|
||
4 years ago
|
// when we drag the right handle, we should get the last point
|
||
|
TextSelectionPoint endpoint;
|
||
|
if (selection.isCollapsed) {
|
||
|
endpoint = endpoints.first;
|
||
|
} else {
|
||
|
if (selection is DragTextSelection) {
|
||
4 years ago
|
endpoint = (selection as DragTextSelection).first
|
||
|
? endpoints.first
|
||
|
: endpoints.last;
|
||
4 years ago
|
} else {
|
||
|
endpoint = endpoints.first;
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
3 years ago
|
// Collapsed selection => caret
|
||
4 years ago
|
final child = childAtPosition(selection.extent);
|
||
|
const kMargin = 8.0;
|
||
|
|
||
|
final caretTop = endpoint.point.dy -
|
||
|
child.preferredLineHeight(TextPosition(
|
||
3 years ago
|
offset: selection.extentOffset - child.container.documentOffset)) -
|
||
4 years ago
|
kMargin +
|
||
|
offsetInViewport +
|
||
|
scrollBottomInset;
|
||
|
final caretBottom =
|
||
|
endpoint.point.dy + kMargin + offsetInViewport + scrollBottomInset;
|
||
|
double? dy;
|
||
|
if (caretTop < scrollOffset) {
|
||
|
dy = caretTop;
|
||
|
} else if (caretBottom > scrollOffset + viewportHeight) {
|
||
|
dy = caretBottom - viewportHeight;
|
||
|
}
|
||
|
if (dy == null) {
|
||
|
return null;
|
||
|
}
|
||
3 years ago
|
// Clamping to 0.0 so that the content does not jump unnecessarily.
|
||
4 years ago
|
return math.max(dy, 0);
|
||
|
}
|
||
4 years ago
|
|
||
|
@override
|
||
|
Rect getLocalRectForCaret(TextPosition position) {
|
||
|
final targetChild = childAtPosition(position);
|
||
|
final localPosition = targetChild.globalToLocalPosition(position);
|
||
|
|
||
|
final childLocalRect = targetChild.getLocalRectForCaret(localPosition);
|
||
|
|
||
|
final boxParentData = targetChild.parentData as BoxParentData;
|
||
|
return childLocalRect.shift(Offset(0, boxParentData.offset.dy));
|
||
|
}
|
||
3 years ago
|
|
||
|
// Start floating cursor
|
||
|
|
||
|
FloatingCursorPainter get _floatingCursorPainter => FloatingCursorPainter(
|
||
|
floatingCursorRect: _floatingCursorRect,
|
||
|
style: _cursorController.style,
|
||
|
);
|
||
|
|
||
|
bool _floatingCursorOn = false;
|
||
|
Rect? _floatingCursorRect;
|
||
|
|
||
|
TextPosition get floatingCursorTextPosition => _floatingCursorTextPosition;
|
||
|
late TextPosition _floatingCursorTextPosition;
|
||
|
|
||
|
// The relative origin in relation to the distance the user has theoretically
|
||
|
// dragged the floating cursor offscreen.
|
||
|
// This value is used to account for the difference
|
||
|
// in the rendering position and the raw offset value.
|
||
|
Offset _relativeOrigin = Offset.zero;
|
||
|
Offset? _previousOffset;
|
||
|
bool _resetOriginOnLeft = false;
|
||
|
bool _resetOriginOnRight = false;
|
||
|
bool _resetOriginOnTop = false;
|
||
|
bool _resetOriginOnBottom = false;
|
||
|
|
||
|
/// Returns the position within the editor closest to the raw cursor offset.
|
||
|
Offset calculateBoundedFloatingCursorOffset(
|
||
|
Offset rawCursorOffset, double preferredLineHeight) {
|
||
|
var deltaPosition = Offset.zero;
|
||
|
final topBound = _kFloatingCursorAddedMargin.top;
|
||
|
final bottomBound =
|
||
|
size.height - preferredLineHeight + _kFloatingCursorAddedMargin.bottom;
|
||
|
final leftBound = _kFloatingCursorAddedMargin.left;
|
||
|
final rightBound = size.width - _kFloatingCursorAddedMargin.right;
|
||
|
|
||
|
if (_previousOffset != null) {
|
||
|
deltaPosition = rawCursorOffset - _previousOffset!;
|
||
|
}
|
||
|
|
||
|
// If the raw cursor offset has gone off an edge,
|
||
|
// we want to reset the relative origin of
|
||
|
// the dragging when the user drags back into the field.
|
||
|
if (_resetOriginOnLeft && deltaPosition.dx > 0) {
|
||
|
_relativeOrigin =
|
||
|
Offset(rawCursorOffset.dx - leftBound, _relativeOrigin.dy);
|
||
|
_resetOriginOnLeft = false;
|
||
|
} else if (_resetOriginOnRight && deltaPosition.dx < 0) {
|
||
|
_relativeOrigin =
|
||
|
Offset(rawCursorOffset.dx - rightBound, _relativeOrigin.dy);
|
||
|
_resetOriginOnRight = false;
|
||
|
}
|
||
|
if (_resetOriginOnTop && deltaPosition.dy > 0) {
|
||
|
_relativeOrigin =
|
||
|
Offset(_relativeOrigin.dx, rawCursorOffset.dy - topBound);
|
||
|
_resetOriginOnTop = false;
|
||
|
} else if (_resetOriginOnBottom && deltaPosition.dy < 0) {
|
||
|
_relativeOrigin =
|
||
|
Offset(_relativeOrigin.dx, rawCursorOffset.dy - bottomBound);
|
||
|
_resetOriginOnBottom = false;
|
||
|
}
|
||
|
|
||
|
final currentX = rawCursorOffset.dx - _relativeOrigin.dx;
|
||
|
final currentY = rawCursorOffset.dy - _relativeOrigin.dy;
|
||
|
final double adjustedX =
|
||
|
math.min(math.max(currentX, leftBound), rightBound);
|
||
|
final double adjustedY =
|
||
|
math.min(math.max(currentY, topBound), bottomBound);
|
||
|
final adjustedOffset = Offset(adjustedX, adjustedY);
|
||
|
|
||
|
if (currentX < leftBound && deltaPosition.dx < 0) {
|
||
|
_resetOriginOnLeft = true;
|
||
|
} else if (currentX > rightBound && deltaPosition.dx > 0) {
|
||
|
_resetOriginOnRight = true;
|
||
|
}
|
||
|
if (currentY < topBound && deltaPosition.dy < 0) {
|
||
|
_resetOriginOnTop = true;
|
||
|
} else if (currentY > bottomBound && deltaPosition.dy > 0) {
|
||
|
_resetOriginOnBottom = true;
|
||
|
}
|
||
|
|
||
|
_previousOffset = rawCursorOffset;
|
||
|
|
||
|
return adjustedOffset;
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void setFloatingCursor(FloatingCursorDragState dragState,
|
||
|
Offset boundedOffset, TextPosition textPosition,
|
||
|
{double? resetLerpValue}) {
|
||
3 years ago
|
if (floatingCursorDisabled) return;
|
||
|
|
||
3 years ago
|
if (dragState == FloatingCursorDragState.Start) {
|
||
|
_relativeOrigin = Offset.zero;
|
||
|
_previousOffset = null;
|
||
|
_resetOriginOnBottom = false;
|
||
|
_resetOriginOnTop = false;
|
||
|
_resetOriginOnRight = false;
|
||
|
_resetOriginOnBottom = false;
|
||
|
}
|
||
|
_floatingCursorOn = dragState != FloatingCursorDragState.End;
|
||
|
if (_floatingCursorOn) {
|
||
|
_floatingCursorTextPosition = textPosition;
|
||
|
final sizeAdjustment = resetLerpValue != null
|
||
|
? EdgeInsets.lerp(
|
||
|
_kFloatingCaretSizeIncrease, EdgeInsets.zero, resetLerpValue)!
|
||
|
: _kFloatingCaretSizeIncrease;
|
||
|
final child = childAtPosition(textPosition);
|
||
|
final caretPrototype =
|
||
|
child.getCaretPrototype(child.globalToLocalPosition(textPosition));
|
||
|
_floatingCursorRect =
|
||
|
sizeAdjustment.inflateRect(caretPrototype).shift(boundedOffset);
|
||
|
_cursorController
|
||
|
.setFloatingCursorTextPosition(_floatingCursorTextPosition);
|
||
|
} else {
|
||
|
_floatingCursorRect = null;
|
||
|
_cursorController.setFloatingCursorTextPosition(null);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void _paintFloatingCursor(PaintingContext context, Offset offset) {
|
||
|
_floatingCursorPainter.paint(context.canvas);
|
||
|
}
|
||
|
|
||
3 years ago
|
// End floating cursor
|
||
|
|
||
|
// Start TextLayoutMetrics implementation
|
||
|
|
||
|
/// Return a [TextSelection] containing the line of the given [TextPosition].
|
||
|
@override
|
||
|
TextSelection getLineAtOffset(TextPosition position) {
|
||
|
final child = childAtPosition(position);
|
||
3 years ago
|
final nodeOffset = child.container.offset;
|
||
3 years ago
|
final localPosition = TextPosition(
|
||
|
offset: position.offset - nodeOffset, affinity: position.affinity);
|
||
|
final localLineRange = child.getLineBoundary(localPosition);
|
||
|
final line = TextRange(
|
||
|
start: localLineRange.start + nodeOffset,
|
||
|
end: localLineRange.end + nodeOffset,
|
||
|
);
|
||
|
return TextSelection(baseOffset: line.start, extentOffset: line.end);
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
TextRange getWordBoundary(TextPosition position) {
|
||
|
final child = childAtPosition(position);
|
||
3 years ago
|
final nodeOffset = child.container.offset;
|
||
3 years ago
|
final localPosition = TextPosition(
|
||
|
offset: position.offset - nodeOffset, affinity: position.affinity);
|
||
|
final localWord = child.getWordBoundary(localPosition);
|
||
|
return TextRange(
|
||
|
start: localWord.start + nodeOffset,
|
||
|
end: localWord.end + nodeOffset,
|
||
|
);
|
||
|
}
|
||
|
|
||
9 months ago
|
/// Returns the TextPosition after moving by the vertical offset.
|
||
|
TextPosition getTextPositionMoveVertical(
|
||
|
TextPosition position, double verticalOffset) {
|
||
|
final caretOfs = localToGlobal(_getOffsetForCaret(position));
|
||
|
return getPositionForOffset(caretOfs.translate(0, verticalOffset));
|
||
|
}
|
||
|
|
||
3 years ago
|
/// Returns the TextPosition above the given offset into the text.
|
||
|
///
|
||
|
/// If the offset is already on the first line, the offset of the first
|
||
|
/// character will be returned.
|
||
|
@override
|
||
|
TextPosition getTextPositionAbove(TextPosition position) {
|
||
|
final child = childAtPosition(position);
|
||
3 years ago
|
final localPosition =
|
||
|
TextPosition(offset: position.offset - child.container.documentOffset);
|
||
3 years ago
|
|
||
|
var newPosition = child.getPositionAbove(localPosition);
|
||
|
|
||
|
if (newPosition == null) {
|
||
|
// There was no text above in the current child, check the direct
|
||
|
// sibling.
|
||
|
final sibling = childBefore(child);
|
||
|
if (sibling == null) {
|
||
|
// reached beginning of the document, move to the
|
||
|
// first character
|
||
|
newPosition = const TextPosition(offset: 0);
|
||
|
} else {
|
||
|
final caretOffset = child.getOffsetForCaret(localPosition);
|
||
3 years ago
|
final testPosition = TextPosition(offset: sibling.container.length - 1);
|
||
3 years ago
|
final testOffset = sibling.getOffsetForCaret(testPosition);
|
||
|
final finalOffset = Offset(caretOffset.dx, testOffset.dy);
|
||
|
final siblingPosition = sibling.getPositionForOffset(finalOffset);
|
||
|
newPosition = TextPosition(
|
||
3 years ago
|
offset: sibling.container.documentOffset + siblingPosition.offset);
|
||
3 years ago
|
}
|
||
|
} else {
|
||
|
newPosition = TextPosition(
|
||
3 years ago
|
offset: child.container.documentOffset + newPosition.offset);
|
||
3 years ago
|
}
|
||
|
return newPosition;
|
||
|
}
|
||
|
|
||
|
/// Returns the TextPosition below the given offset into the text.
|
||
|
///
|
||
|
/// If the offset is already on the last line, the offset of the last
|
||
|
/// character will be returned.
|
||
|
@override
|
||
|
TextPosition getTextPositionBelow(TextPosition position) {
|
||
|
final child = childAtPosition(position);
|
||
1 year ago
|
final localPosition = TextPosition(
|
||
|
offset: position.offset - child.container.documentOffset,
|
||
|
);
|
||
3 years ago
|
|
||
|
var newPosition = child.getPositionBelow(localPosition);
|
||
|
|
||
|
if (newPosition == null) {
|
||
9 months ago
|
// There was no text below in the current child, check the direct sibling.
|
||
3 years ago
|
final sibling = childAfter(child);
|
||
|
if (sibling == null) {
|
||
9 months ago
|
// reached end of the document, move to the
|
||
3 years ago
|
// last character
|
||
|
newPosition = TextPosition(offset: document.length - 1);
|
||
|
} else {
|
||
|
final caretOffset = child.getOffsetForCaret(localPosition);
|
||
|
const testPosition = TextPosition(offset: 0);
|
||
|
final testOffset = sibling.getOffsetForCaret(testPosition);
|
||
|
final finalOffset = Offset(caretOffset.dx, testOffset.dy);
|
||
|
final siblingPosition = sibling.getPositionForOffset(finalOffset);
|
||
|
newPosition = TextPosition(
|
||
1 year ago
|
offset: sibling.container.documentOffset + siblingPosition.offset,
|
||
|
);
|
||
3 years ago
|
}
|
||
|
} else {
|
||
|
newPosition = TextPosition(
|
||
1 year ago
|
offset: child.container.documentOffset + newPosition.offset,
|
||
|
);
|
||
3 years ago
|
}
|
||
|
return newPosition;
|
||
|
}
|
||
|
|
||
|
// End TextLayoutMetrics implementation
|
||
|
|
||
3 years ago
|
QuillVerticalCaretMovementRun startVerticalCaretMovement(
|
||
|
TextPosition startPosition) {
|
||
|
return QuillVerticalCaretMovementRun._(
|
||
|
this,
|
||
|
startPosition,
|
||
|
);
|
||
|
}
|
||
|
|
||
3 years ago
|
@override
|
||
|
void systemFontsDidChange() {
|
||
|
super.systemFontsDidChange();
|
||
|
markNeedsLayout();
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
1 year ago
|
class QuillVerticalCaretMovementRun implements Iterator<TextPosition> {
|
||
3 years ago
|
QuillVerticalCaretMovementRun._(
|
||
|
this._editor,
|
||
|
this._currentTextPosition,
|
||
|
);
|
||
|
|
||
|
TextPosition _currentTextPosition;
|
||
|
|
||
|
final RenderEditor _editor;
|
||
|
|
||
|
@override
|
||
|
TextPosition get current {
|
||
|
return _currentTextPosition;
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
bool moveNext() {
|
||
|
_currentTextPosition = _editor.getTextPositionBelow(_currentTextPosition);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool movePrevious() {
|
||
|
_currentTextPosition = _editor.getTextPositionAbove(_currentTextPosition);
|
||
|
return true;
|
||
|
}
|
||
9 months ago
|
|
||
|
void moveVertical(double verticalOffset) {
|
||
|
_currentTextPosition = _editor.getTextPositionMoveVertical(
|
||
|
_currentTextPosition, verticalOffset);
|
||
|
}
|
||
3 years ago
|
}
|
||
|
|
||
4 years ago
|
class EditableContainerParentData
|
||
|
extends ContainerBoxParentData<RenderEditableBox> {}
|
||
|
|
||
3 years ago
|
/// Multi-child render box of editable content.
|
||
|
///
|
||
|
/// Common ancestor for [RenderEditor] and [RenderEditableTextBlock].
|
||
4 years ago
|
class RenderEditableContainerBox extends RenderBox
|
||
|
with
|
||
|
ContainerRenderObjectMixin<RenderEditableBox,
|
||
|
EditableContainerParentData>,
|
||
|
RenderBoxContainerDefaultsMixin<RenderEditableBox,
|
||
|
EditableContainerParentData> {
|
||
1 year ago
|
RenderEditableContainerBox({
|
||
1 year ago
|
required container_node.QuillContainer container,
|
||
1 year ago
|
required this.textDirection,
|
||
|
required this.scrollBottomInset,
|
||
|
required EdgeInsetsGeometry padding,
|
||
|
List<RenderEditableBox>? children,
|
||
|
}) : assert(padding.isNonNegative),
|
||
3 years ago
|
_container = container,
|
||
|
_padding = padding {
|
||
4 years ago
|
addAll(children);
|
||
|
}
|
||
|
|
||
1 year ago
|
container_node.QuillContainer _container;
|
||
4 years ago
|
TextDirection textDirection;
|
||
|
EdgeInsetsGeometry _padding;
|
||
|
double scrollBottomInset;
|
||
|
EdgeInsets? _resolvedPadding;
|
||
|
|
||
1 year ago
|
container_node.QuillContainer get container => _container;
|
||
4 years ago
|
|
||
1 year ago
|
void setContainer(container_node.QuillContainer c) {
|
||
4 years ago
|
if (_container == c) {
|
||
|
return;
|
||
|
}
|
||
|
_container = c;
|
||
|
markNeedsLayout();
|
||
|
}
|
||
|
|
||
|
EdgeInsetsGeometry getPadding() => _padding;
|
||
|
|
||
|
void setPadding(EdgeInsetsGeometry value) {
|
||
|
assert(value.isNonNegative);
|
||
|
if (_padding == value) {
|
||
|
return;
|
||
|
}
|
||
|
_padding = value;
|
||
|
_markNeedsPaddingResolution();
|
||
|
}
|
||
|
|
||
|
EdgeInsets? get resolvedPadding => _resolvedPadding;
|
||
|
|
||
3 years ago
|
void resolvePadding() {
|
||
4 years ago
|
if (_resolvedPadding != null) {
|
||
|
return;
|
||
|
}
|
||
|
_resolvedPadding = _padding.resolve(textDirection);
|
||
|
_resolvedPadding = _resolvedPadding!.copyWith(left: _resolvedPadding!.left);
|
||
|
|
||
|
assert(_resolvedPadding!.isNonNegative);
|
||
|
}
|
||
|
|
||
|
RenderEditableBox childAtPosition(TextPosition position) {
|
||
|
assert(firstChild != null);
|
||
3 years ago
|
final targetNode = container.queryChild(position.offset, false).node;
|
||
4 years ago
|
|
||
|
var targetChild = firstChild;
|
||
|
while (targetChild != null) {
|
||
3 years ago
|
if (targetChild.container == targetNode) {
|
||
4 years ago
|
break;
|
||
|
}
|
||
3 years ago
|
final newChild = childAfter(targetChild);
|
||
|
if (newChild == null) {
|
||
9 months ago
|
// At start of document fails to find the position
|
||
|
targetChild = childAtOffset(const Offset(0, 0));
|
||
3 years ago
|
break;
|
||
|
}
|
||
|
targetChild = newChild;
|
||
4 years ago
|
}
|
||
|
if (targetChild == null) {
|
||
|
throw 'targetChild should not be null';
|
||
|
}
|
||
|
return targetChild;
|
||
|
}
|
||
|
|
||
|
void _markNeedsPaddingResolution() {
|
||
|
_resolvedPadding = null;
|
||
|
markNeedsLayout();
|
||
|
}
|
||
|
|
||
3 years ago
|
/// Returns child of this container located at the specified local `offset`.
|
||
|
///
|
||
|
/// If `offset` is above this container (offset.dy is negative) returns
|
||
|
/// the first child. Likewise, if `offset` is below this container then
|
||
|
/// returns the last child.
|
||
|
RenderEditableBox childAtOffset(Offset offset) {
|
||
4 years ago
|
assert(firstChild != null);
|
||
3 years ago
|
resolvePadding();
|
||
4 years ago
|
|
||
|
if (offset.dy <= _resolvedPadding!.top) {
|
||
3 years ago
|
return firstChild!;
|
||
4 years ago
|
}
|
||
|
if (offset.dy >= size.height - _resolvedPadding!.bottom) {
|
||
3 years ago
|
return lastChild!;
|
||
4 years ago
|
}
|
||
|
|
||
|
var child = firstChild;
|
||
|
final dx = -offset.dx;
|
||
|
var dy = _resolvedPadding!.top;
|
||
|
while (child != null) {
|
||
|
if (child.size.contains(offset.translate(dx, -dy))) {
|
||
|
return child;
|
||
|
}
|
||
|
dy += child.size.height;
|
||
|
child = childAfter(child);
|
||
|
}
|
||
2 years ago
|
|
||
|
// this case possible, when editor not scrollable,
|
||
|
// but minHeight > content height and tap was under content
|
||
|
return lastChild!;
|
||
4 years ago
|
}
|
||
|
|
||
|
@override
|
||
|
void setupParentData(RenderBox child) {
|
||
|
if (child.parentData is EditableContainerParentData) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
child.parentData = EditableContainerParentData();
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void performLayout() {
|
||
|
assert(constraints.hasBoundedWidth);
|
||
3 years ago
|
resolvePadding();
|
||
4 years ago
|
assert(_resolvedPadding != null);
|
||
|
|
||
|
var mainAxisExtent = _resolvedPadding!.top;
|
||
|
var child = firstChild;
|
||
|
final innerConstraints =
|
||
|
BoxConstraints.tightFor(width: constraints.maxWidth)
|
||
|
.deflate(_resolvedPadding!);
|
||
|
while (child != null) {
|
||
|
child.layout(innerConstraints, parentUsesSize: true);
|
||
|
final childParentData = (child.parentData as EditableContainerParentData)
|
||
|
..offset = Offset(_resolvedPadding!.left, mainAxisExtent);
|
||
|
mainAxisExtent += child.size.height;
|
||
|
assert(child.parentData == childParentData);
|
||
|
child = childParentData.nextSibling;
|
||
|
}
|
||
|
mainAxisExtent += _resolvedPadding!.bottom;
|
||
|
size = constraints.constrain(Size(constraints.maxWidth, mainAxisExtent));
|
||
|
|
||
|
assert(size.isFinite);
|
||
|
}
|
||
|
|
||
|
double _getIntrinsicCrossAxis(double Function(RenderBox child) childSize) {
|
||
|
var extent = 0.0;
|
||
|
var child = firstChild;
|
||
|
while (child != null) {
|
||
|
extent = math.max(extent, childSize(child));
|
||
|
final childParentData = child.parentData as EditableContainerParentData;
|
||
|
child = childParentData.nextSibling;
|
||
|
}
|
||
|
return extent;
|
||
|
}
|
||
|
|
||
|
double _getIntrinsicMainAxis(double Function(RenderBox child) childSize) {
|
||
|
var extent = 0.0;
|
||
|
var child = firstChild;
|
||
|
while (child != null) {
|
||
|
extent += childSize(child);
|
||
|
final childParentData = child.parentData as EditableContainerParentData;
|
||
|
child = childParentData.nextSibling;
|
||
|
}
|
||
|
return extent;
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
double computeMinIntrinsicWidth(double height) {
|
||
3 years ago
|
resolvePadding();
|
||
4 years ago
|
return _getIntrinsicCrossAxis((child) {
|
||
|
final childHeight = math.max<double>(
|
||
|
0, height - _resolvedPadding!.top + _resolvedPadding!.bottom);
|
||
|
return child.getMinIntrinsicWidth(childHeight) +
|
||
|
_resolvedPadding!.left +
|
||
|
_resolvedPadding!.right;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
double computeMaxIntrinsicWidth(double height) {
|
||
3 years ago
|
resolvePadding();
|
||
4 years ago
|
return _getIntrinsicCrossAxis((child) {
|
||
|
final childHeight = math.max<double>(
|
||
|
0, height - _resolvedPadding!.top + _resolvedPadding!.bottom);
|
||
|
return child.getMaxIntrinsicWidth(childHeight) +
|
||
|
_resolvedPadding!.left +
|
||
|
_resolvedPadding!.right;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
double computeMinIntrinsicHeight(double width) {
|
||
3 years ago
|
resolvePadding();
|
||
4 years ago
|
return _getIntrinsicMainAxis((child) {
|
||
|
final childWidth = math.max<double>(
|
||
|
0, width - _resolvedPadding!.left + _resolvedPadding!.right);
|
||
|
return child.getMinIntrinsicHeight(childWidth) +
|
||
|
_resolvedPadding!.top +
|
||
|
_resolvedPadding!.bottom;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
double computeMaxIntrinsicHeight(double width) {
|
||
3 years ago
|
resolvePadding();
|
||
4 years ago
|
return _getIntrinsicMainAxis((child) {
|
||
|
final childWidth = math.max<double>(
|
||
|
0, width - _resolvedPadding!.left + _resolvedPadding!.right);
|
||
|
return child.getMaxIntrinsicHeight(childWidth) +
|
||
|
_resolvedPadding!.top +
|
||
|
_resolvedPadding!.bottom;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
double computeDistanceToActualBaseline(TextBaseline baseline) {
|
||
3 years ago
|
resolvePadding();
|
||
4 years ago
|
return defaultComputeDistanceToFirstActualBaseline(baseline)! +
|
||
|
_resolvedPadding!.top;
|
||
|
}
|
||
|
}
|