parent
8306e02c00
commit
162711deaf
6 changed files with 165 additions and 16 deletions
@ -1,6 +1,16 @@ |
|||||||
|
import 'package:quill_delta/quill_delta.dart'; |
||||||
|
|
||||||
|
import 'container.dart'; |
||||||
import 'line.dart'; |
import 'line.dart'; |
||||||
import 'node.dart'; |
|
||||||
|
|
||||||
class Block extends Container<Line> { |
class Block extends Container<Line> { |
||||||
|
@override |
||||||
|
Line get defaultChild => Line(); |
||||||
|
|
||||||
} |
@override |
||||||
|
Delta toDelta() { |
||||||
|
return children |
||||||
|
.map((child) => child.toDelta()) |
||||||
|
.fold(Delta(), (a, b) => a.concat(b)); |
||||||
|
} |
||||||
|
} |
||||||
|
@ -0,0 +1,53 @@ |
|||||||
|
import 'dart:collection'; |
||||||
|
|
||||||
|
import 'node.dart'; |
||||||
|
|
||||||
|
/* Container of multiple nodes */ |
||||||
|
abstract class Container<T extends Node> extends Node { |
||||||
|
final LinkedList<Node> _children = LinkedList<Node>(); |
||||||
|
|
||||||
|
LinkedList<Node> get children => _children; |
||||||
|
|
||||||
|
int get childCount => _children.length; |
||||||
|
|
||||||
|
Node get first => _children.first; |
||||||
|
|
||||||
|
Node get last => _children.last; |
||||||
|
|
||||||
|
bool get isEmpty => _children.isEmpty; |
||||||
|
|
||||||
|
bool get isNotEmpty => _children.isNotEmpty; |
||||||
|
|
||||||
|
/// abstract methods begin |
||||||
|
|
||||||
|
T get defaultChild; |
||||||
|
|
||||||
|
/// abstract methods end |
||||||
|
|
||||||
|
add(T node) { |
||||||
|
assert(node.parent == null); |
||||||
|
node.parent = this; |
||||||
|
_children.add(node); |
||||||
|
} |
||||||
|
|
||||||
|
addFirst(T node) { |
||||||
|
assert(node.parent == null); |
||||||
|
node.parent = this; |
||||||
|
_children.addFirst(node); |
||||||
|
} |
||||||
|
|
||||||
|
void remove(T node) { |
||||||
|
assert(node.parent == this); |
||||||
|
node.parent = null; |
||||||
|
_children.remove(node); |
||||||
|
} |
||||||
|
|
||||||
|
@override |
||||||
|
String toPlainText() => children.map((child) => child.toPlainText()).join(); |
||||||
|
|
||||||
|
@override |
||||||
|
int get length => _children.fold(0, (cur, node) => cur + node.length); |
||||||
|
|
||||||
|
@override |
||||||
|
String toString() => _children.join('\n'); |
||||||
|
} |
@ -1,5 +1,42 @@ |
|||||||
|
import 'package:quill_delta/quill_delta.dart'; |
||||||
|
|
||||||
import 'node.dart'; |
import 'node.dart'; |
||||||
|
|
||||||
/* A leaf node in document tree */ |
/* A leaf node in document tree */ |
||||||
abstract class Leaf extends Node { |
abstract class Leaf extends Node { |
||||||
|
Object _value; |
||||||
|
|
||||||
|
Object get value => _value; |
||||||
|
|
||||||
|
Leaf.val(Object val) |
||||||
|
: assert(val != null), |
||||||
|
_value = val; |
||||||
|
|
||||||
|
@override |
||||||
|
int get length { |
||||||
|
if (_value is String) { |
||||||
|
return (_value as String).length; |
||||||
|
} |
||||||
|
// return 1 for embedded object |
||||||
|
return 1; |
||||||
|
} |
||||||
|
|
||||||
|
@override |
||||||
|
Delta toDelta() { |
||||||
|
return null; // TODO |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class Text extends Leaf { |
||||||
|
Text([String text = '']) |
||||||
|
: assert(!text.contains('\n')), |
||||||
|
super.val(text); |
||||||
|
|
||||||
|
@override |
||||||
|
String get value => _value as String; |
||||||
|
|
||||||
|
@override |
||||||
|
String toPlainText() { |
||||||
|
return value; |
||||||
|
} |
||||||
} |
} |
||||||
|
@ -1,5 +1,31 @@ |
|||||||
|
import 'package:quill_delta/quill_delta.dart'; |
||||||
|
|
||||||
|
import 'block.dart'; |
||||||
|
import 'container.dart'; |
||||||
import 'leaf.dart'; |
import 'leaf.dart'; |
||||||
import 'node.dart'; |
|
||||||
|
|
||||||
class Line extends Container<Leaf> { |
class Line extends Container<Leaf> { |
||||||
|
|
||||||
|
@override |
||||||
|
Leaf get defaultChild => Text(); |
||||||
|
|
||||||
|
@override |
||||||
|
int get length => super.length + 1; |
||||||
|
|
||||||
|
@override |
||||||
|
Delta toDelta() { |
||||||
|
final delta = children |
||||||
|
.map((child) => child.toDelta()) |
||||||
|
.fold(Delta(), (a, b) => a.concat(b)); |
||||||
|
var attributes = style; |
||||||
|
if (parent is Block) { |
||||||
|
Block block = parent; |
||||||
|
attributes = attributes.mergeAll(block.style); |
||||||
|
} |
||||||
|
delta.insert('\n', attributes.toJson()); |
||||||
|
return delta; |
||||||
|
} |
||||||
|
|
||||||
|
@override |
||||||
|
String toPlainText() => super.toPlainText() + '\n'; |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue