Skip to content

Commit

Permalink
chore: sync the latest code from AppFlowy (#41)
Browse files Browse the repository at this point in the history
* chore: sync latest code from appflowy

* chore: sync latest tests from appflowy
  • Loading branch information
LucasXu0 authored Apr 6, 2023
1 parent 06240a0 commit 7885d5f
Show file tree
Hide file tree
Showing 32 changed files with 723 additions and 304 deletions.
66 changes: 33 additions & 33 deletions lib/appflowy_editor.dart
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
/// AppFlowyEditor library
library appflowy_editor;

export 'src/commands/command_extension.dart';
export 'src/commands/text/text_commands.dart';
export 'src/core/document/attributes.dart';
export 'src/core/document/document.dart';
export 'src/infra/log.dart';
export 'src/render/style/editor_style.dart';
export 'src/core/document/node.dart';
export 'src/core/document/node_iterator.dart';
export 'src/core/document/path.dart';
export 'src/core/document/text_delta.dart';
export 'src/core/legacy/built_in_attribute_keys.dart';
export 'src/core/location/position.dart';
export 'src/core/location/selection.dart';
export 'src/core/document/document.dart';
export 'src/core/document/text_delta.dart';
export 'src/core/document/attributes.dart';
export 'src/core/legacy/built_in_attribute_keys.dart';
export 'src/editor_state.dart';
export 'src/core/transform/operation.dart';
export 'src/core/transform/transaction.dart';
export 'src/editor_state.dart';
export 'src/render/selection/selectable.dart';
export 'src/render/selection_menu/selection_menu_service.dart';
export 'src/service/editor_service.dart';
export 'src/service/render_plugin_service.dart';
export 'src/service/service.dart';
export 'src/service/selection_service.dart';
export 'src/service/scroll_service.dart';
export 'src/service/toolbar_service.dart';
export 'src/service/keyboard_service.dart';
export 'src/service/input_service.dart';
export 'src/service/shortcut_event/keybinding.dart';
export 'src/service/shortcut_event/shortcut_event.dart';
export 'src/service/shortcut_event/shortcut_event_handler.dart';
export 'src/extensions/attributes_extension.dart';
export 'src/extensions/node_extensions.dart';
export 'src/infra/log.dart';
export 'src/render/rich_text/default_selectable.dart';
export 'src/render/rich_text/flowy_rich_text.dart';
export 'src/render/selection_menu/selection_menu_widget.dart';
export 'src/render/selection_menu/selection_menu_item_widget.dart';
export 'src/l10n/l10n.dart';
export 'src/plugins/markdown/decoder/delta_markdown_decoder.dart';
export 'src/plugins/markdown/document_markdown.dart';
export 'src/render/style/plugin_styles.dart';
export 'src/plugins/markdown/encoder/delta_markdown_encoder.dart';
export 'src/plugins/markdown/encoder/document_markdown_encoder.dart';
export 'src/plugins/markdown/encoder/parser/image_node_parser.dart';
export 'src/plugins/markdown/encoder/parser/node_parser.dart';
export 'src/plugins/markdown/encoder/parser/text_node_parser.dart';
export 'src/plugins/markdown/encoder/parser/image_node_parser.dart';
export 'src/plugins/markdown/decoder/delta_markdown_decoder.dart';
export 'src/plugins/markdown/document_markdown.dart';
export 'src/plugins/quill_delta/delta_document_encoder.dart';
export 'src/commands/text/text_commands.dart';
export 'src/commands/command_extension.dart';
export 'src/render/toolbar/toolbar_item.dart';
export 'src/extensions/node_extensions.dart';
export 'src/render/action_menu/action_menu.dart';
export 'src/render/action_menu/action_menu_item.dart';
export 'src/render/rich_text/default_selectable.dart';
export 'src/render/rich_text/flowy_rich_text.dart';
export 'src/render/selection/selectable.dart';
export 'src/render/selection_menu/selection_menu_item_widget.dart';
export 'src/render/selection_menu/selection_menu_service.dart';
export 'src/render/selection_menu/selection_menu_widget.dart';
export 'src/render/style/editor_style.dart';
export 'src/render/style/plugin_styles.dart';
export 'src/render/toolbar/toolbar_item.dart';
export 'src/service/editor_service.dart';
export 'src/service/input_service.dart';
export 'src/service/keyboard_service.dart';
export 'src/service/render_plugin_service.dart';
export 'src/service/scroll_service.dart';
export 'src/service/selection_service.dart';
export 'src/service/service.dart';
export 'src/service/shortcut_event/keybinding.dart';
export 'src/service/shortcut_event/shortcut_event.dart';
export 'src/service/shortcut_event/shortcut_event_handler.dart';
export 'src/service/toolbar_service.dart';
export 'src/core/document/node_iterator.dart';
45 changes: 38 additions & 7 deletions lib/src/core/transform/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,19 @@ extension TextTransaction on Transaction {
);
} else {
deleteNode(textNode);
if (i == textNodes.length - 1) {
final delta = Delta()
..insert(texts[0])
..addAll(
textNodes.last.delta.slice(selection.end.offset),
);
replaceText(
textNode,
selection.start.offset,
texts[0].length,
delta.toPlainText(),
);
}
}
}
afterSelection = null;
Expand All @@ -371,6 +384,8 @@ extension TextTransaction on Transaction {

if (textNodes.length < texts.length) {
final length = texts.length;
var path = textNodes.first.path;

for (var i = 0; i < texts.length; i++) {
final text = texts[i];
if (i == 0) {
Expand All @@ -380,13 +395,15 @@ extension TextTransaction on Transaction {
textNodes.first.toPlainText().length,
text,
);
} else if (i == length - 1) {
path = path.next;
} else if (i == length - 1 && textNodes.length >= 2) {
replaceText(
textNodes.last,
0,
selection.endIndex,
text,
);
path = path.next;
} else {
if (i < textNodes.length - 1) {
replaceText(
Expand All @@ -395,14 +412,28 @@ extension TextTransaction on Transaction {
textNodes[i].toPlainText().length,
text,
);
path = path.next;
} else {
var path = textNodes.first.path;
var j = i - textNodes.length + length - 1;
while (j > 0) {
path = path.next;
j--;
if (i == texts.length - 1) {
final delta = Delta()
..insert(text)
..addAll(
textNodes.last.delta.slice(selection.end.offset),
);
insertNode(
path,
TextNode(
delta: delta,
),
);
} else {
insertNode(
path,
TextNode(
delta: Delta()..insert(text),
),
);
}
insertNode(path, TextNode(delta: Delta()..insert(text)));
}
}
}
Expand Down
23 changes: 15 additions & 8 deletions lib/src/render/toolbar/toolbar_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:appflowy_editor/src/render/link_menu/link_menu.dart';
import 'package:appflowy_editor/src/extensions/text_node_extensions.dart';
import 'package:appflowy_editor/src/extensions/editor_state_extensions.dart';
import 'package:appflowy_editor/src/service/default_text_operations/format_rich_text_style.dart';
import 'dart:io' show Platform;

import 'package:flutter/material.dart' hide Overlay, OverlayEntry;

Expand Down Expand Up @@ -127,7 +128,8 @@ List<ToolbarItem> defaultToolbarItems = [
ToolbarItem(
id: 'appflowy.toolbar.bold',
type: 2,
tooltipsMessage: AppFlowyEditorLocalizations.current.bold,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.bold}\n${Platform.isMacOS ? "⌘ + B" : "CTRL + B"}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/bold',
color: isHighlight ? Colors.lightBlue : null,
Expand All @@ -143,7 +145,8 @@ List<ToolbarItem> defaultToolbarItems = [
ToolbarItem(
id: 'appflowy.toolbar.italic',
type: 2,
tooltipsMessage: AppFlowyEditorLocalizations.current.italic,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.italic}\n${Platform.isMacOS ? "⌘ + I" : "CTRL + I"}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/italic',
color: isHighlight ? Colors.lightBlue : null,
Expand All @@ -159,7 +162,8 @@ List<ToolbarItem> defaultToolbarItems = [
ToolbarItem(
id: 'appflowy.toolbar.underline',
type: 2,
tooltipsMessage: AppFlowyEditorLocalizations.current.underline,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.underline}\n${Platform.isMacOS ? "⌘ + U" : "CTRL + U"}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/underline',
color: isHighlight ? Colors.lightBlue : null,
Expand All @@ -175,7 +179,8 @@ List<ToolbarItem> defaultToolbarItems = [
ToolbarItem(
id: 'appflowy.toolbar.strikethrough',
type: 2,
tooltipsMessage: AppFlowyEditorLocalizations.current.strikethrough,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.strikethrough}\n${Platform.isMacOS ? "⌘ + SHIFT + S" : "CTRL + SHIFT + S"}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/strikethrough',
color: isHighlight ? Colors.lightBlue : null,
Expand All @@ -191,7 +196,8 @@ List<ToolbarItem> defaultToolbarItems = [
ToolbarItem(
id: 'appflowy.toolbar.code',
type: 2,
tooltipsMessage: AppFlowyEditorLocalizations.current.embedCode,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.embedCode}\n${Platform.isMacOS ? "⌘ + E" : "CTRL + E"}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/code',
color: isHighlight ? Colors.lightBlue : null,
Expand Down Expand Up @@ -241,7 +247,8 @@ List<ToolbarItem> defaultToolbarItems = [
ToolbarItem(
id: 'appflowy.toolbar.link',
type: 4,
tooltipsMessage: AppFlowyEditorLocalizations.current.link,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.link}\n${Platform.isMacOS ? "⌘ + K" : "CTRL + K"}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/link',
color: isHighlight ? Colors.lightBlue : null,
Expand All @@ -257,7 +264,8 @@ List<ToolbarItem> defaultToolbarItems = [
ToolbarItem(
id: 'appflowy.toolbar.highlight',
type: 4,
tooltipsMessage: AppFlowyEditorLocalizations.current.highlight,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.highlight}\n${Platform.isMacOS ? "⌘ + SHIFT + H" : "CTRL + SHIFT + H"}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/highlight',
color: isHighlight ? Colors.lightBlue : null,
Expand Down Expand Up @@ -393,7 +401,6 @@ void showLinkMenu(
top: matchRect.bottom + 5.0,
left: matchRect.left,
child: Material(
borderRadius: BorderRadius.circular(6.0),
child: LinkMenu(
linkText: linkText,
editorState: editorState,
Expand Down
1 change: 1 addition & 0 deletions lib/src/render/toolbar/toolbar_item_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ToolbarItemWidget extends StatelessWidget {
width: 28,
height: 28,
child: Tooltip(
textAlign: TextAlign.center,
preferBelow: false,
message: item.tooltipsMessage,
child: MouseRegion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void _pasteSingleLine(
/// parse url from the line text
/// reference: https://stackoverflow.com/questions/59444837/flutter-dart-regex-to-extract-urls-from-a-string
Delta _lineContentToDelta(String lineContent) {
final exp = RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+');
final exp = RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\#\w/\-?=%.]+');
final Iterable<RegExpMatch> matches = exp.allMatches(lineContent);

final delta = Delta();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ ShortcutEventHandler underscoreToItalicHandler = (editorState, event) {
return KeyEventResult.handled;
};

ShortcutEventHandler doubleAsteriskToBoldHanlder = (editorState, event) {
ShortcutEventHandler doubleAsteriskToBoldHandler = (editorState, event) {
final selectionService = editorState.service.selectionService;
final selection = selectionService.currentSelection.value;
final textNodes = selectionService.currentSelectedNodes.whereType<TextNode>();
Expand Down Expand Up @@ -367,7 +367,7 @@ ShortcutEventHandler doubleAsteriskToBoldHanlder = (editorState, event) {
};

//Implement in the same way as doubleAsteriskToBoldHanlder
ShortcutEventHandler doubleUnderscoreToBoldHanlder = (editorState, event) {
ShortcutEventHandler doubleUnderscoreToBoldHandler = (editorState, event) {
final selectionService = editorState.service.selectionService;
final selection = selectionService.currentSelection.value;
final textNodes = selectionService.currentSelectedNodes.whereType<TextNode>();
Expand Down
11 changes: 8 additions & 3 deletions lib/src/service/keyboard_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,13 @@ class _AppFlowyKeyboardState extends State<AppFlowyKeyboard>
return KeyEventResult.ignored;
}

Log.keyboard.debug('on keyboard event $event');

if (event is! RawKeyDownEvent) {
return KeyEventResult.ignored;
}

// TODO: use cache to optimize the searching time.
for (final shortcutEvent in widget.shortcutEvents) {
if (shortcutEvent.keybindings.containsKeyEvent(event)) {
if (shortcutEvent.canRespondToRawKeyEvent(event)) {
final result = shortcutEvent.handler(widget.editorState, event);
if (result == KeyEventResult.handled) {
return KeyEventResult.handled;
Expand Down Expand Up @@ -157,3 +155,10 @@ class _AppFlowyKeyboardState extends State<AppFlowyKeyboard>
return onKey(event);
}
}

extension on ShortcutEvent {
bool canRespondToRawKeyEvent(RawKeyEvent event) {
return ((character?.isNotEmpty ?? false) && character == event.character) ||
keybindings.containsKeyEvent(event);
}
}
43 changes: 22 additions & 21 deletions lib/src/service/shortcut_event/built_in_shortcut_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,6 @@ List<ShortcutEvent> builtInShortcutEvents = [
linuxCommand: 'shift+alt+arrow left',
handler: cursorLeftWordSelect,
),
ShortcutEvent(
key: 'Move cursor left one word',
command: 'alt+arrow left',
windowsCommand: 'alt+arrow left',
linuxCommand: 'alt+arrow left',
handler: cursorLeftWordMove,
),
ShortcutEvent(
key: 'Move cursor right one word',
command: 'alt+arrow right',
windowsCommand: 'alt+arrow right',
linuxCommand: 'alt+arrow right',
handler: cursorRightWordMove,
),
ShortcutEvent(
key: 'Cursor right word select',
command: 'shift+alt+arrow right',
Expand Down Expand Up @@ -261,7 +247,7 @@ List<ShortcutEvent> builtInShortcutEvents = [
),
ShortcutEvent(
key: 'selection menu',
command: 'slash,shift+slash',
character: '/',
handler: slashShortcutHandler,
),
ShortcutEvent(
Expand Down Expand Up @@ -303,7 +289,7 @@ List<ShortcutEvent> builtInShortcutEvents = [
),
ShortcutEvent(
key: 'Double tilde to strikethrough',
command: 'tilde,shift+tilde',
character: '~',
handler: doubleTildeToStrikethrough,
),
ShortcutEvent(
Expand All @@ -318,19 +304,34 @@ List<ShortcutEvent> builtInShortcutEvents = [
),
ShortcutEvent(
key: 'Underscore to italic',
command: 'shift+underscore',
character: '_',
handler: underscoreToItalicHandler,
),
ShortcutEvent(
key: 'Double asterisk to bold',
command: 'shift+digit 8',
handler: doubleAsteriskToBoldHanlder,
character: '*',
handler: doubleAsteriskToBoldHandler,
),
ShortcutEvent(
key: 'Double underscore to bold',
command: 'shift+underscore',
handler: doubleUnderscoreToBoldHanlder,
character: '_',
handler: doubleUnderscoreToBoldHandler,
),
ShortcutEvent(
key: 'Move cursor left one word',
command: 'alt+arrow left',
windowsCommand: 'alt+arrow left',
linuxCommand: 'alt+arrow left',
handler: cursorLeftWordMove,
),
ShortcutEvent(
key: 'Move cursor right one word',
command: 'alt+arrow right',
windowsCommand: 'alt+arrow right',
linuxCommand: 'alt+arrow right',
handler: cursorRightWordMove,
),

// https://github.com/flutter/flutter/issues/104944
// Workaround: Using space editing on the web platform often results in errors,
// so adding a shortcut event to handle the space input instead of using the
Expand Down
Loading

0 comments on commit 7885d5f

Please sign in to comment.