feat: typo and document

This commit is contained in:
Lucas.Xu 2022-07-25 20:18:14 +08:00
parent 0bf1e61d55
commit fcb09e9636
13 changed files with 80 additions and 51 deletions

View File

@ -97,11 +97,22 @@ class _MyHomePageState extends State<MyHomePage> {
return FlowyEditor(
editorState: _editorState,
keyEventHandler: const [],
shortCuts: [
shortcuts: [
// TODO: this won't work, just a example for now.
{
'heading': (editorState, eventName) =>
debugPrint('shortcut => $eventName')
'h1': (editorState, eventName) {
debugPrint('shortcut => $eventName');
final selectedNodes = editorState.selectedNodes;
if (selectedNodes.isEmpty) {
return;
}
final textNode = selectedNodes.first as TextNode;
TransactionBuilder(editorState)
..formatText(textNode, 0, textNode.toRawString().length, {
'heading': 'h1',
})
..commit();
}
},
{
'bold': (editorState, eventName) =>

View File

@ -52,7 +52,7 @@ class __ImageNodeWidgetState extends State<_ImageNodeWidget> with Selectable {
}
@override
TextSelection? getTextSelection() {
TextSelection? getCurrentTextSelection() {
return null;
}
@ -62,12 +62,12 @@ class __ImageNodeWidgetState extends State<_ImageNodeWidget> with Selectable {
}
@override
Offset getLeftOfOffset() {
Offset getBackwardOffset() {
return Offset.zero;
}
@override
Offset getRightOfOffset() {
Offset getForwardOffset() {
return Offset.zero;
}

View File

@ -100,7 +100,7 @@ class _SelectedTextNodeWidgetState extends State<_SelectedTextNodeWidget>
}
@override
TextSelection? getTextSelection() {
TextSelection? getCurrentTextSelection() {
return _textSelection;
}
@ -111,7 +111,7 @@ class _SelectedTextNodeWidgetState extends State<_SelectedTextNodeWidget>
}
@override
Offset getLeftOfOffset() {
Offset getBackwardOffset() {
final textSelection = _textSelection;
if (textSelection != null) {
final leftTextSelection = TextSelection.collapsed(
@ -123,7 +123,7 @@ class _SelectedTextNodeWidgetState extends State<_SelectedTextNodeWidget>
}
@override
Offset getRightOfOffset() {
Offset getForwardOffset() {
final textSelection = _textSelection;
if (textSelection != null) {
final leftTextSelection = TextSelection.collapsed(

View File

@ -1,9 +1,9 @@
import 'package:flowy_editor/flowy_editor.dart';
import 'package:flutter/material.dart';
typedef FloatingShortCutHandler = void Function(
typedef FloatingShortcutHandler = void Function(
EditorState editorState, String eventName);
typedef FloatingShortCuts = List<Map<String, FloatingShortCutHandler>>;
typedef FloatingShortcuts = List<Map<String, FloatingShortcutHandler>>;
class FloatingShortcutWidget extends StatelessWidget {
const FloatingShortcutWidget({
@ -17,11 +17,11 @@ class FloatingShortcutWidget extends StatelessWidget {
final EditorState editorState;
final LayerLink layerLink;
final Rect rect;
final FloatingShortCuts floatingShortcuts;
final FloatingShortcuts floatingShortcuts;
List<String> get _shortcutNames =>
floatingShortcuts.map((shortcut) => shortcut.keys.first).toList();
List<FloatingShortCutHandler> get _shortcutHandlers =>
List<FloatingShortcutHandler> get _shortcutHandlers =>
floatingShortcuts.map((shortcut) => shortcut.values.first).toList();
@override
@ -38,7 +38,7 @@ class FloatingShortcutWidget extends StatelessWidget {
itemCount: floatingShortcuts.length,
itemBuilder: ((context, index) {
final name = _shortcutNameInIndex(index);
final handler = _shortCutHandlerInIndex(index);
final handler = _shortcutHandlerInIndex(index);
return Card(
child: GestureDetector(
onTap: () => handler(editorState, name),
@ -53,6 +53,6 @@ class FloatingShortcutWidget extends StatelessWidget {
}
String _shortcutNameInIndex(int index) => _shortcutNames[index];
FloatingShortCutHandler _shortCutHandlerInIndex(int index) =>
FloatingShortcutHandler _shortcutHandlerInIndex(int index) =>
_shortcutHandlers[index];
}

View File

@ -2,24 +2,40 @@ import 'package:flutter/material.dart';
///
mixin Selectable<T extends StatefulWidget> on State<T> {
/// Returns a [Rect] list for overlay.
/// [start] and [end] are global offsets.
/// The return result must be an local offset.
/// Returns a [List] of the [Rect] selection sorrounded by start and end
/// in current widget.
///
/// [start] and [end] are the offsets under the global coordinate system.
///
/// The return result must be a [List] of the [Rect]
/// under the local coordinate system.
List<Rect> getSelectionRectsInRange(Offset start, Offset end);
/// Returns a [Rect] for cursor.
/// The return result must be an local offset.
/// Returns a [Rect] for the offset in current widget.
///
/// [start] is the offset of the global coordination system.
///
/// The return result must be an offset of the local coordinate system.
Rect getCursorRect(Offset start);
/// Returns one unit offset to the left of the offset
Offset getLeftOfOffset(/* Cause */);
/// Returns a backward offset of the current offset based on the cause.
Offset getBackwardOffset(/* Cause */);
/// Returns one unit offset to the right of the offset
Offset getRightOfOffset(/* Cause */);
/// Returns a forward offset of the current offset based on the cause.
Offset getForwardOffset(/* Cause */);
/// For [TextNode] only.
TextSelection? getTextSelection();
///
/// Returns a [TextSelection] or [Null].
///
/// Only the widget rendered by [TextNode] need to implement the detail,
/// and the rest can return null.
TextSelection? getCurrentTextSelection();
/// For [TextNode] only.
///
/// Retruns a [Offset].
/// Only the widget rendered by [TextNode] need to implement the detail,
/// and the rest can return [Offset.zero].
Offset getOffsetByTextSelection(TextSelection textSelection);
}

View File

@ -15,12 +15,12 @@ class FlowyEditor extends StatefulWidget {
Key? key,
required this.editorState,
required this.keyEventHandler,
required this.shortCuts,
required this.shortcuts,
}) : super(key: key);
final EditorState editorState;
final List<FlowyKeyEventHandler> keyEventHandler;
final FloatingShortCuts shortCuts;
final FloatingShortcuts shortcuts;
@override
State<FlowyEditor> createState() => _FlowyEditorState();
@ -44,11 +44,11 @@ class _FlowyEditorState extends State<FlowyEditor> {
...widget.keyEventHandler,
],
editorState: editorState,
child: FloatingShortCut(
child: FloatingShortcut(
key: editorState.service.floatingShortcutServiceKey,
size: const Size(200, 150), // TODO: support customize size.
editorState: editorState,
floatingShortCuts: widget.shortCuts,
floatingShortcuts: widget.shortcuts,
child: editorState.build(context),
),
),

View File

@ -1,33 +1,35 @@
import 'package:flowy_editor/document/node.dart';
import 'package:flowy_editor/flowy_editor.dart';
import 'package:flowy_editor/render/selection/floating_shortcut_widget.dart';
import 'package:flutter/material.dart';
mixin FlowyFloatingShortCutService {
mixin FlowyFloatingShortcutService {
/// Show the floating shortcut widget beside the offset.
void showInOffset(Offset offset, LayerLink layerLink);
/// Hide the floating shortcut widget.
void hide();
}
class FloatingShortCut extends StatefulWidget {
const FloatingShortCut({
class FloatingShortcut extends StatefulWidget {
const FloatingShortcut({
Key? key,
required this.size,
required this.editorState,
required this.floatingShortCuts,
required this.floatingShortcuts,
required this.child,
}) : super(key: key);
final Size size;
final EditorState editorState;
final Widget child;
final FloatingShortCuts floatingShortCuts;
final FloatingShortcuts floatingShortcuts;
@override
State<FloatingShortCut> createState() => _FloatingShortCutState();
State<FloatingShortcut> createState() => _FloatingShortcutState();
}
class _FloatingShortCutState extends State<FloatingShortCut>
with FlowyFloatingShortCutService {
class _FloatingShortcutState extends State<FloatingShortcut>
with FlowyFloatingShortcutService {
OverlayEntry? _floatintShortcutOverlay;
@override
@ -38,7 +40,7 @@ class _FloatingShortCutState extends State<FloatingShortCut>
editorState: widget.editorState,
layerLink: layerLink,
rect: offset.translate(10, 0) & widget.size,
floatingShortcuts: widget.floatingShortCuts),
floatingShortcuts: widget.floatingShortcuts),
);
Overlay.of(context)?.insert(_floatintShortcutOverlay!);
}

View File

@ -24,9 +24,9 @@ FlowyKeyEventHandler arrowKeysHandler = (editorState, event) {
final selectable = node?.key?.currentState?.unwrapOrNull<Selectable>();
Offset? offset;
if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
offset = selectable?.getLeftOfOffset();
offset = selectable?.getBackwardOffset();
} else if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
offset = selectable?.getRightOfOffset();
offset = selectable?.getForwardOffset();
}
final selectionService = editorState.service.selectionService;
if (offset != null) {

View File

@ -17,7 +17,7 @@ FlowyKeyEventHandler deleteSingleTextNodeHandler = (editorState, event) {
final node = selectionNodes.first.unwrapOrNull<TextNode>();
final selectable = node?.key?.currentState?.unwrapOrNull<Selectable>();
if (selectable != null) {
final textSelection = selectable.getTextSelection();
final textSelection = selectable.getCurrentTextSelection();
if (textSelection != null) {
if (textSelection.isCollapsed) {
/// Three cases:

View File

@ -17,7 +17,7 @@ FlowyKeyEventHandler slashShortcutHandler = (editorState, event) {
final textNode = selectedNodes.first.unwrapOrNull<TextNode>();
final selectable = textNode?.key?.currentState?.unwrapOrNull<Selectable>();
final textSelection = selectable?.getTextSelection();
final textSelection = selectable?.getCurrentTextSelection();
if (textNode != null && selectable != null && textSelection != null) {
final offset = selectable.getOffsetByTextSelection(textSelection);
final rect = selectable.getCursorRect(offset);

View File

@ -1,5 +1,5 @@
import 'package:flowy_editor/render/selection/cursor_widget.dart';
import 'package:flowy_editor/render/selection/selection_widget.dart';
import 'package:flowy_editor/render/selection/flowy_selection_widget.dart';
import 'package:flowy_editor/extensions/object_extensions.dart';
import 'package:flowy_editor/service/floating_shortcut_service.dart';
import 'package:flutter/gestures.dart';
@ -293,9 +293,9 @@ class _FlowySelectionState extends State<FlowySelection>
}
void _clearFloatingShorts() {
final shortCutService = editorState
final shortcutService = editorState
.service.floatingShortcutServiceKey.currentState
?.unwrapOrNull<FlowyFloatingShortCutService>();
shortCutService?.hide();
?.unwrapOrNull<FlowyFloatingShortcutService>();
shortcutService?.hide();
}
}

View File

@ -14,14 +14,14 @@ class FlowyService {
// keyboard service
final keyboardServiceKey = GlobalKey(debugLabel: 'flowy_keyboard_service');
// floating toolbar service
// floating shortcut service
final floatingShortcutServiceKey =
GlobalKey(debugLabel: 'flowy_floating_shortcut_service');
FlowyFloatingShortCutService get floatingToolbarService {
FlowyFloatingShortcutService get floatingToolbarService {
assert(floatingShortcutServiceKey.currentState != null &&
floatingShortcutServiceKey.currentState
is FlowyFloatingShortCutService);
is FlowyFloatingShortcutService);
return floatingShortcutServiceKey.currentState!
as FlowyFloatingShortCutService;
as FlowyFloatingShortcutService;
}
}