feat: integrate into appflowy
This commit is contained in:
parent
14ca76611a
commit
77f549c4e9
5 changed files with 35 additions and 9 deletions
|
|
@ -2,6 +2,7 @@ import 'dart:collection';
|
||||||
|
|
||||||
import 'package:appflowy_editor/appflowy_editor.dart';
|
import 'package:appflowy_editor/appflowy_editor.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:nanoid/nanoid.dart';
|
||||||
|
|
||||||
/// [Node] represents a node in the document tree.
|
/// [Node] represents a node in the document tree.
|
||||||
///
|
///
|
||||||
|
|
@ -20,6 +21,7 @@ import 'package:flutter/material.dart';
|
||||||
class Node extends ChangeNotifier with LinkedListEntry<Node> {
|
class Node extends ChangeNotifier with LinkedListEntry<Node> {
|
||||||
Node({
|
Node({
|
||||||
required this.type,
|
required this.type,
|
||||||
|
String? id,
|
||||||
this.parent,
|
this.parent,
|
||||||
Attributes attributes = const {},
|
Attributes attributes = const {},
|
||||||
Iterable<Node> children = const [],
|
Iterable<Node> children = const [],
|
||||||
|
|
@ -29,7 +31,8 @@ class Node extends ChangeNotifier with LinkedListEntry<Node> {
|
||||||
(e) => e..unlink(),
|
(e) => e..unlink(),
|
||||||
),
|
),
|
||||||
), // unlink the given children to avoid the error of "node has already a parent"
|
), // unlink the given children to avoid the error of "node has already a parent"
|
||||||
_attributes = attributes {
|
_attributes = attributes,
|
||||||
|
id = id ?? nanoid(10) {
|
||||||
for (final child in this.children) {
|
for (final child in this.children) {
|
||||||
child.parent = this;
|
child.parent = this;
|
||||||
}
|
}
|
||||||
|
|
@ -54,11 +57,14 @@ class Node extends ChangeNotifier with LinkedListEntry<Node> {
|
||||||
/// The type of the node.
|
/// The type of the node.
|
||||||
final String type;
|
final String type;
|
||||||
|
|
||||||
|
/// The id of the node.
|
||||||
|
final String id;
|
||||||
|
|
||||||
@Deprecated('Use type instead')
|
@Deprecated('Use type instead')
|
||||||
String get subtype => type;
|
String get subtype => type;
|
||||||
|
|
||||||
@Deprecated('Use type instead')
|
// @Deprecated('Use type instead')
|
||||||
String get id => type;
|
// String get id => type;
|
||||||
|
|
||||||
/// The parent of the node.
|
/// The parent of the node.
|
||||||
Node? parent;
|
Node? parent;
|
||||||
|
|
@ -111,8 +117,9 @@ class Node extends ChangeNotifier with LinkedListEntry<Node> {
|
||||||
|
|
||||||
Log.editor.debug('insert Node $entry at path ${path + [index]}}');
|
Log.editor.debug('insert Node $entry at path ${path + [index]}}');
|
||||||
|
|
||||||
|
entry.parent = this;
|
||||||
|
|
||||||
if (children.isEmpty) {
|
if (children.isEmpty) {
|
||||||
entry.parent = this;
|
|
||||||
_children.add(entry);
|
_children.add(entry);
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
return;
|
return;
|
||||||
|
|
@ -160,6 +167,15 @@ class Node extends ChangeNotifier with LinkedListEntry<Node> {
|
||||||
parent = null;
|
parent = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
return '''Node(id: $id,
|
||||||
|
type: $type,
|
||||||
|
attributes: $attributes,
|
||||||
|
children: $children,
|
||||||
|
)''';
|
||||||
|
}
|
||||||
|
|
||||||
Delta? get delta {
|
Delta? get delta {
|
||||||
if (attributes['delta'] is List) {
|
if (attributes['delta'] is List) {
|
||||||
return Delta.fromJson(attributes['delta']);
|
return Delta.fromJson(attributes['delta']);
|
||||||
|
|
@ -186,11 +202,13 @@ class Node extends ChangeNotifier with LinkedListEntry<Node> {
|
||||||
|
|
||||||
Node copyWith({
|
Node copyWith({
|
||||||
String? type,
|
String? type,
|
||||||
|
String? id,
|
||||||
Iterable<Node>? children,
|
Iterable<Node>? children,
|
||||||
Attributes? attributes,
|
Attributes? attributes,
|
||||||
}) {
|
}) {
|
||||||
final node = Node(
|
final node = Node(
|
||||||
type: type ?? this.type,
|
type: type ?? this.type,
|
||||||
|
id: id ?? this.id,
|
||||||
attributes: attributes ?? {...this.attributes},
|
attributes: attributes ?? {...this.attributes},
|
||||||
children: children ?? [],
|
children: children ?? [],
|
||||||
);
|
);
|
||||||
|
|
@ -224,6 +242,7 @@ class TextNode extends Node {
|
||||||
type: 'text',
|
type: 'text',
|
||||||
children: children?.toList() ?? [],
|
children: children?.toList() ?? [],
|
||||||
attributes: attributes ?? {},
|
attributes: attributes ?? {},
|
||||||
|
id: '',
|
||||||
);
|
);
|
||||||
|
|
||||||
TextNode.empty({Attributes? attributes})
|
TextNode.empty({Attributes? attributes})
|
||||||
|
|
@ -258,6 +277,7 @@ class TextNode extends Node {
|
||||||
Iterable<Node>? children,
|
Iterable<Node>? children,
|
||||||
Attributes? attributes,
|
Attributes? attributes,
|
||||||
Delta? delta,
|
Delta? delta,
|
||||||
|
String? id,
|
||||||
}) {
|
}) {
|
||||||
final textNode = TextNode(
|
final textNode = TextNode(
|
||||||
children: children ?? [],
|
children: children ?? [],
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
import 'package:appflowy_editor/appflowy_editor.dart';
|
import 'package:appflowy_editor/appflowy_editor.dart';
|
||||||
import 'package:appflowy_editor/src/editor/block_component/base_component/block_component_configuration.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.dart';
|
||||||
|
|
||||||
class HeadingBlockKeys {
|
class HeadingBlockKeys {
|
||||||
HeadingBlockKeys._();
|
const HeadingBlockKeys._();
|
||||||
|
|
||||||
/// The level data of a heading block.
|
/// The level data of a heading block.
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -13,11 +13,14 @@ Iterable<ThemeExtension<dynamic>> get darkEditorStyleExtension => [
|
||||||
class EditorStyle extends ThemeExtension<EditorStyle> {
|
class EditorStyle extends ThemeExtension<EditorStyle> {
|
||||||
// Editor styles
|
// Editor styles
|
||||||
final EdgeInsets? padding;
|
final EdgeInsets? padding;
|
||||||
final Color? backgroundColor;
|
|
||||||
final Color cursorColor;
|
final Color cursorColor;
|
||||||
final Color selectionColor;
|
final Color selectionColor;
|
||||||
final TextStyleConfiguration textStyleConfiguration;
|
final TextStyleConfiguration textStyleConfiguration;
|
||||||
|
|
||||||
|
@Deprecated('customize the editor\'s background color directly')
|
||||||
|
final Color? backgroundColor;
|
||||||
|
|
||||||
// Text styles
|
// Text styles
|
||||||
@Deprecated('customize the block component directly')
|
@Deprecated('customize the block component directly')
|
||||||
final EdgeInsets? textPadding;
|
final EdgeInsets? textPadding;
|
||||||
|
|
@ -68,10 +71,10 @@ class EditorStyle extends ThemeExtension<EditorStyle> {
|
||||||
|
|
||||||
const EditorStyle({
|
const EditorStyle({
|
||||||
required this.padding,
|
required this.padding,
|
||||||
required this.backgroundColor,
|
|
||||||
required this.cursorColor,
|
required this.cursorColor,
|
||||||
required this.selectionColor,
|
required this.selectionColor,
|
||||||
required this.textStyleConfiguration,
|
required this.textStyleConfiguration,
|
||||||
|
required this.backgroundColor,
|
||||||
required this.selectionMenuBackgroundColor,
|
required this.selectionMenuBackgroundColor,
|
||||||
required this.selectionMenuItemTextColor,
|
required this.selectionMenuItemTextColor,
|
||||||
required this.selectionMenuItemIconColor,
|
required this.selectionMenuItemIconColor,
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ class AppFlowyEditor extends StatefulWidget {
|
||||||
ScrollController? scrollController,
|
ScrollController? scrollController,
|
||||||
bool editable = true,
|
bool editable = true,
|
||||||
bool autoFocus = false,
|
bool autoFocus = false,
|
||||||
|
Selection? focusedSelection,
|
||||||
EditorStyle? editorStyle,
|
EditorStyle? editorStyle,
|
||||||
Map<String, BlockComponentBuilder> blockComponentBuilders = const {},
|
Map<String, BlockComponentBuilder> blockComponentBuilders = const {},
|
||||||
List<CharacterShortcutEvent> characterShortcutEvents = const [],
|
List<CharacterShortcutEvent> characterShortcutEvents = const [],
|
||||||
|
|
@ -43,6 +44,7 @@ class AppFlowyEditor extends StatefulWidget {
|
||||||
scrollController: scrollController,
|
scrollController: scrollController,
|
||||||
editable: editable,
|
editable: editable,
|
||||||
autoFocus: autoFocus,
|
autoFocus: autoFocus,
|
||||||
|
focusedSelection: focusedSelection,
|
||||||
blockComponentBuilders: blockComponentBuilders,
|
blockComponentBuilders: blockComponentBuilders,
|
||||||
characterShortcutEvents: characterShortcutEvents,
|
characterShortcutEvents: characterShortcutEvents,
|
||||||
commandShortcutEvents: commandShortcutEvents,
|
commandShortcutEvents: commandShortcutEvents,
|
||||||
|
|
@ -56,6 +58,7 @@ class AppFlowyEditor extends StatefulWidget {
|
||||||
ScrollController? scrollController,
|
ScrollController? scrollController,
|
||||||
bool editable = true,
|
bool editable = true,
|
||||||
bool autoFocus = false,
|
bool autoFocus = false,
|
||||||
|
Selection? focusedSelection,
|
||||||
EditorStyle? editorStyle,
|
EditorStyle? editorStyle,
|
||||||
}) : this(
|
}) : this(
|
||||||
key: key,
|
key: key,
|
||||||
|
|
@ -63,6 +66,7 @@ class AppFlowyEditor extends StatefulWidget {
|
||||||
scrollController: scrollController,
|
scrollController: scrollController,
|
||||||
editable: editable,
|
editable: editable,
|
||||||
autoFocus: autoFocus,
|
autoFocus: autoFocus,
|
||||||
|
focusedSelection: focusedSelection,
|
||||||
blockComponentBuilders: standardBlockComponentBuilderMap,
|
blockComponentBuilders: standardBlockComponentBuilderMap,
|
||||||
characterShortcutEvents: standardCharacterShortcutEvents,
|
characterShortcutEvents: standardCharacterShortcutEvents,
|
||||||
commandShortcutEvents: standardCommandShortcutEvents,
|
commandShortcutEvents: standardCommandShortcutEvents,
|
||||||
|
|
@ -176,7 +180,6 @@ class _AppFlowyEditorState extends State<AppFlowyEditor> {
|
||||||
key: editorState.service.scrollServiceKey,
|
key: editorState.service.scrollServiceKey,
|
||||||
scrollController: widget.scrollController,
|
scrollController: widget.scrollController,
|
||||||
child: Container(
|
child: Container(
|
||||||
color: widget.editorStyle.backgroundColor,
|
|
||||||
padding: widget.editorStyle.padding,
|
padding: widget.editorStyle.padding,
|
||||||
child: SelectionServiceWidget(
|
child: SelectionServiceWidget(
|
||||||
key: editorState.service.selectionServiceKey,
|
key: editorState.service.selectionServiceKey,
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ dependencies:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
tuple: ^2.0.1
|
tuple: ^2.0.1
|
||||||
collection: ^1.17.0
|
collection: ^1.17.0
|
||||||
|
nanoid: ^1.0.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue