refactor: move attributes to core/document

This commit is contained in:
Lucas.Xu 2022-10-09 15:54:39 +08:00
parent 11eca2b3d9
commit cbb6b2d9b0
15 changed files with 153 additions and 25 deletions

View File

@ -9,7 +9,7 @@ export 'src/document/position.dart';
export 'src/document/selection.dart'; export 'src/document/selection.dart';
export 'src/document/state_tree.dart'; export 'src/document/state_tree.dart';
export 'src/document/text_delta.dart'; export 'src/document/text_delta.dart';
export 'src/document/attributes.dart'; export 'src/core/document/attributes.dart';
export 'src/document/built_in_attribute_keys.dart'; export 'src/document/built_in_attribute_keys.dart';
export 'src/editor_state.dart'; export 'src/editor_state.dart';
export 'src/operation/operation.dart'; export 'src/operation/operation.dart';

View File

@ -1,6 +1,6 @@
import 'package:appflowy_editor/src/commands/format_text.dart'; import 'package:appflowy_editor/src/commands/format_text.dart';
import 'package:appflowy_editor/src/commands/text_command_infra.dart'; import 'package:appflowy_editor/src/commands/text_command_infra.dart';
import 'package:appflowy_editor/src/document/attributes.dart'; import 'package:appflowy_editor/src/core/document/attributes.dart';
import 'package:appflowy_editor/src/document/built_in_attribute_keys.dart'; import 'package:appflowy_editor/src/document/built_in_attribute_keys.dart';
import 'package:appflowy_editor/src/core/document/node.dart'; import 'package:appflowy_editor/src/core/document/node.dart';
import 'package:appflowy_editor/src/document/path.dart'; import 'package:appflowy_editor/src/document/path.dart';

View File

@ -1,7 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'package:appflowy_editor/src/commands/text_command_infra.dart'; import 'package:appflowy_editor/src/commands/text_command_infra.dart';
import 'package:appflowy_editor/src/document/attributes.dart'; import 'package:appflowy_editor/src/core/document/attributes.dart';
import 'package:appflowy_editor/src/core/document/node.dart'; import 'package:appflowy_editor/src/core/document/node.dart';
import 'package:appflowy_editor/src/document/path.dart'; import 'package:appflowy_editor/src/document/path.dart';
import 'package:appflowy_editor/src/document/selection.dart'; import 'package:appflowy_editor/src/document/selection.dart';

View File

@ -23,21 +23,27 @@ Attributes? composeAttributes(
return attributes.isNotEmpty ? attributes : null; return attributes.isNotEmpty ? attributes : null;
} }
Attributes invertAttributes(Attributes? base, Attributes? other) { Attributes invertAttributes(Attributes? from, Attributes? to) {
base ??= {}; from ??= {};
other ??= {}; to ??= {};
final Attributes attributes = base.keys.fold({}, (previousValue, key) { final attributes = Attributes.from({});
if (other!.containsKey(key) && other[key] != base![key]) {
previousValue[key] = base[key]; // key in from but not in to, or value is different
for (final entry in from.entries) {
if ((!to.containsKey(entry.key) && entry.value != null) ||
to[entry.key] != entry.value) {
attributes[entry.key] = entry.value;
} }
return previousValue; }
});
return other.keys.fold(attributes, (previousValue, key) { // key in to but not in from, or value is different
if (!base!.containsKey(key) && other![key] != base[key]) { for (final entry in to.entries) {
previousValue[key] = null; if (!from.containsKey(entry.key) && entry.value != null) {
attributes[entry.key] = null;
} }
return previousValue; }
});
return attributes;
} }
int hashAttributes(Attributes base) => Object.hashAllUnordered( int hashAttributes(Attributes base) => Object.hashAllUnordered(

View File

@ -2,7 +2,7 @@ import 'dart:collection';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:appflowy_editor/src/document/attributes.dart'; import 'package:appflowy_editor/src/core/document/attributes.dart';
import 'package:appflowy_editor/src/document/built_in_attribute_keys.dart'; import 'package:appflowy_editor/src/document/built_in_attribute_keys.dart';
import 'package:appflowy_editor/src/document/path.dart'; import 'package:appflowy_editor/src/document/path.dart';
import 'package:appflowy_editor/src/document/text_delta.dart'; import 'package:appflowy_editor/src/document/text_delta.dart';

View File

@ -3,7 +3,7 @@ import 'dart:math';
import 'package:appflowy_editor/src/core/document/node.dart'; import 'package:appflowy_editor/src/core/document/node.dart';
import 'package:appflowy_editor/src/document/path.dart'; import 'package:appflowy_editor/src/document/path.dart';
import 'package:appflowy_editor/src/document/text_delta.dart'; import 'package:appflowy_editor/src/document/text_delta.dart';
import './attributes.dart'; import '../core/document/attributes.dart';
class StateTree { class StateTree {
final Node root; final Node root;

View File

@ -1,7 +1,7 @@
import 'dart:collection'; import 'dart:collection';
import 'dart:math'; import 'dart:math';
import 'package:appflowy_editor/src/document/attributes.dart'; import 'package:appflowy_editor/src/core/document/attributes.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
// constant number: 2^53 - 1 // constant number: 2^53 - 1

View File

@ -1,4 +1,4 @@
import 'package:appflowy_editor/src/document/attributes.dart'; import 'package:appflowy_editor/src/core/document/attributes.dart';
import 'package:appflowy_editor/src/document/built_in_attribute_keys.dart'; import 'package:appflowy_editor/src/document/built_in_attribute_keys.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';

View File

@ -1,7 +1,7 @@
import 'dart:collection'; import 'dart:collection';
import 'dart:ui'; import 'dart:ui';
import 'package:appflowy_editor/src/document/attributes.dart'; import 'package:appflowy_editor/src/core/document/attributes.dart';
import 'package:appflowy_editor/src/core/document/node.dart'; import 'package:appflowy_editor/src/core/document/node.dart';
import 'package:appflowy_editor/src/document/text_delta.dart'; import 'package:appflowy_editor/src/document/text_delta.dart';
import 'package:appflowy_editor/src/extensions/color_extension.dart'; import 'package:appflowy_editor/src/extensions/color_extension.dart';

View File

@ -1,7 +1,7 @@
import 'dart:collection'; import 'dart:collection';
import 'dart:math'; import 'dart:math';
import 'package:appflowy_editor/src/document/attributes.dart'; import 'package:appflowy_editor/src/core/document/attributes.dart';
import 'package:appflowy_editor/src/core/document/node.dart'; import 'package:appflowy_editor/src/core/document/node.dart';
import 'package:appflowy_editor/src/document/path.dart'; import 'package:appflowy_editor/src/document/path.dart';
import 'package:appflowy_editor/src/document/position.dart'; import 'package:appflowy_editor/src/document/position.dart';

View File

@ -1,4 +1,4 @@
import 'package:appflowy_editor/src/document/attributes.dart'; import 'package:appflowy_editor/src/core/document/attributes.dart';
import 'package:appflowy_editor/src/core/document/node.dart'; import 'package:appflowy_editor/src/core/document/node.dart';
import 'package:appflowy_editor/src/document/position.dart'; import 'package:appflowy_editor/src/document/position.dart';
import 'package:appflowy_editor/src/document/selection.dart'; import 'package:appflowy_editor/src/document/selection.dart';

View File

@ -2,7 +2,7 @@ import 'package:appflowy_editor/src/document/selection.dart';
import 'package:appflowy_editor/src/editor_state.dart'; import 'package:appflowy_editor/src/editor_state.dart';
import 'package:appflowy_editor/src/document/built_in_attribute_keys.dart'; import 'package:appflowy_editor/src/document/built_in_attribute_keys.dart';
import 'package:appflowy_editor/src/operation/transaction_builder.dart'; import 'package:appflowy_editor/src/operation/transaction_builder.dart';
import 'package:appflowy_editor/src/document/attributes.dart'; import 'package:appflowy_editor/src/core/document/attributes.dart';
void makeFollowingNodesIncremental( void makeFollowingNodesIncremental(
EditorState editorState, List<int> insertPath, Selection afterSelection, EditorState editorState, List<int> insertPath, Selection afterSelection,

View File

@ -0,0 +1,59 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter_test/flutter_test.dart';
void main() async {
group('attributes.dart', () {
test('composeAttributes', () {
final base = {
'a': 1,
'b': 2,
};
final other = {
'b': 3,
'c': 4,
'd': null,
};
expect(composeAttributes(base, other, keepNull: false), {
'a': 1,
'b': 3,
'c': 4,
});
expect(composeAttributes(base, other, keepNull: true), {
'a': 1,
'b': 3,
'c': 4,
'd': null,
});
expect(composeAttributes(null, other, keepNull: false), {
'b': 3,
'c': 4,
});
expect(composeAttributes(base, null, keepNull: false), {
'a': 1,
'b': 2,
});
});
test('invertAttributes', () {
final base = {
'a': 1,
'b': 2,
};
final other = {
'b': 3,
'c': 4,
'd': null,
};
expect(invertAttributes(base, other), {
'a': 1,
'b': 2,
'c': null,
});
expect(invertAttributes(other, base), {
'a': null,
'b': 3,
'c': 4,
});
});
});
}

View File

@ -147,5 +147,68 @@ void main() async {
expect(identical(node.children, base.children), false); expect(identical(node.children, base.children), false);
expect(identical(node.children.first, base.children.first), false); expect(identical(node.children.first, base.children.first), false);
}); });
test('test insert', () {
final base = Node(
type: 'base',
);
// insert at the front
final childA = Node(
type: 'child',
);
base.insert(childA, index: -1);
expect(
identical(base.childAtIndex(0), childA),
true,
);
// insert at the last
final childB = Node(
type: 'child',
);
base.insert(childB, index: 1000);
expect(
identical(base.childAtIndex(base.children.length - 1), childB),
true,
);
// insert at the last
final childC = Node(
type: 'child',
);
base.insert(childC);
expect(
identical(base.childAtIndex(base.children.length - 1), childC),
true,
);
});
test('test fromJson', () {
final node = Node.fromJson({
'type': 'example',
'attributes': {
'example': 'example',
},
'children': [
{
'type': 'example',
'attributes': {
'example': 'example',
},
},
],
});
expect(node.type, 'example');
expect(node.attributes, {'example': 'example'});
expect(node.children.length, 1);
expect(node.children.first.type, 'example');
expect(node.children.first.attributes, {'example': 'example'});
});
test('test toPlainText', () {
final textNode = TextNode.empty()..delta = (Delta()..insert('AppFlowy'));
expect(textNode.toPlainText(), 'AppFlowy');
});
}); });
} }

View File

@ -1,4 +1,4 @@
import 'package:appflowy_editor/src/document/attributes.dart'; import 'package:appflowy_editor/src/core/document/attributes.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:appflowy_editor/src/document/text_delta.dart'; import 'package:appflowy_editor/src/document/text_delta.dart';