diff --git a/lib/src/models/rules/delete.dart b/lib/src/models/rules/delete.dart index 78b94f6b..2f36f76d 100644 --- a/lib/src/models/rules/delete.dart +++ b/lib/src/models/rules/delete.dart @@ -1,6 +1,7 @@ import 'package:meta/meta.dart' show immutable; import '../../../quill_delta.dart'; +import '../../models/documents/document.dart'; import '../documents/attribute.dart'; import '../documents/nodes/embeddable.dart'; import 'rule.dart'; @@ -26,9 +27,9 @@ class EnsureLastLineBreakDeleteRule extends DeleteRule { const EnsureLastLineBreakDeleteRule(); @override - Delta? applyRule(Delta document, int index, + Delta? applyRule(Document document, int index, {int? len, Object? data, Attribute? attribute}) { - final itr = DeltaIterator(document)..skip(index + len!); + final itr = DeltaIterator(document.toDelta())..skip(index + len!); return Delta() ..retain(index) @@ -43,9 +44,9 @@ class CatchAllDeleteRule extends DeleteRule { const CatchAllDeleteRule(); @override - Delta applyRule(Delta document, int index, + Delta applyRule(Document document, int index, {int? len, Object? data, Attribute? attribute}) { - final itr = DeltaIterator(document)..skip(index + len!); + final itr = DeltaIterator(document.toDelta())..skip(index + len!); return Delta() ..retain(index) @@ -64,9 +65,9 @@ class PreserveLineStyleOnMergeRule extends DeleteRule { const PreserveLineStyleOnMergeRule(); @override - Delta? applyRule(Delta document, int index, + Delta? applyRule(Document document, int index, {int? len, Object? data, Attribute? attribute}) { - final itr = DeltaIterator(document)..skip(index); + final itr = DeltaIterator(document.toDelta())..skip(index); var op = itr.next(1); if (op.data != '\n') { return null; @@ -121,9 +122,9 @@ class EnsureEmbedLineRule extends DeleteRule { const EnsureEmbedLineRule(); @override - Delta? applyRule(Delta document, int index, + Delta? applyRule(Document document, int index, {int? len, Object? data, Attribute? attribute}) { - final itr = DeltaIterator(document); + final itr = DeltaIterator(document.toDelta()); var op = itr.skip(index); final opAfter = itr.skip(index + 1); diff --git a/lib/src/models/rules/format.dart b/lib/src/models/rules/format.dart index 7f731e7a..f1e1b804 100644 --- a/lib/src/models/rules/format.dart +++ b/lib/src/models/rules/format.dart @@ -1,6 +1,7 @@ import 'package:meta/meta.dart' show immutable; import '../../../quill_delta.dart'; +import '../../models/documents/document.dart'; import '../documents/attribute.dart'; import 'rule.dart'; @@ -28,7 +29,7 @@ class ResolveLineFormatRule extends FormatRule { @override Delta? applyRule( - Delta document, + Document document, int index, { int? len, Object? data, @@ -41,7 +42,7 @@ class ResolveLineFormatRule extends FormatRule { // Apply line styles to all newline characters within range of this // retain operation. var result = Delta()..retain(index); - final itr = DeltaIterator(document)..skip(index); + final itr = DeltaIterator(document.toDelta())..skip(index); Operation op; for (var cur = 0; cur < len! && itr.hasNext; cur += op.length!) { op = itr.next(len - cur); @@ -119,7 +120,7 @@ class FormatLinkAtCaretPositionRule extends FormatRule { @override Delta? applyRule( - Delta document, + Document document, int index, { int? len, Object? data, @@ -130,7 +131,7 @@ class FormatLinkAtCaretPositionRule extends FormatRule { } final delta = Delta(); - final itr = DeltaIterator(document); + final itr = DeltaIterator(document.toDelta()); final before = itr.skip(index), after = itr.next(); int? beg = index, retain = 0; if (before != null && before.hasAttribute(attribute.key)) { @@ -159,7 +160,7 @@ class ResolveInlineFormatRule extends FormatRule { @override Delta? applyRule( - Delta document, + Document document, int index, { int? len, Object? data, @@ -170,7 +171,7 @@ class ResolveInlineFormatRule extends FormatRule { } final delta = Delta()..retain(index); - final itr = DeltaIterator(document)..skip(index); + final itr = DeltaIterator(document.toDelta())..skip(index); Operation op; for (var cur = 0; cur < len! && itr.hasNext; cur += op.length!) { @@ -205,7 +206,7 @@ class ResolveImageFormatRule extends FormatRule { @override Delta? applyRule( - Delta document, + Document document, int index, { int? len, Object? data, diff --git a/lib/src/models/rules/insert.dart b/lib/src/models/rules/insert.dart index b97058bc..9f6bf723 100644 --- a/lib/src/models/rules/insert.dart +++ b/lib/src/models/rules/insert.dart @@ -33,7 +33,7 @@ class PreserveLineStyleOnSplitRule extends InsertRule { @override Delta? applyRule( - Delta document, + Document document, int index, { int? len, Object? data, @@ -42,8 +42,7 @@ class PreserveLineStyleOnSplitRule extends InsertRule { if (data is! String || data != '\n') { return null; } - - final itr = DeltaIterator(document); + final itr = DeltaIterator(document.toDelta()); final before = itr.skip(index); if (before == null) { return null; @@ -84,7 +83,7 @@ class PreserveBlockStyleOnInsertRule extends InsertRule { @override Delta? applyRule( - Delta document, + Document document, int index, { int? len, Object? data, @@ -94,8 +93,7 @@ class PreserveBlockStyleOnInsertRule extends InsertRule { // Only interested in text containing at least one newline character. return null; } - - final itr = DeltaIterator(document)..skip(index); + final itr = DeltaIterator(document.toDelta())..skip(index); // Look for the next newline. final nextNewLine = _getNextNewLine(itr); @@ -171,7 +169,7 @@ class AutoExitBlockRule extends InsertRule { @override Delta? applyRule( - Delta document, + Document document, int index, { int? len, Object? data, @@ -181,7 +179,7 @@ class AutoExitBlockRule extends InsertRule { return null; } - final itr = DeltaIterator(document); + final itr = DeltaIterator(document.toDelta()); final prev = itr.skip(index), cur = itr.next(); final blockStyle = Style.fromJson(cur.attributes).getBlockExceptHeader(); // We are not in a block, ignore. @@ -241,7 +239,7 @@ class ResetLineFormatOnNewLineRule extends InsertRule { @override Delta? applyRule( - Delta document, + Document document, int index, { int? len, Object? data, @@ -251,7 +249,7 @@ class ResetLineFormatOnNewLineRule extends InsertRule { return null; } - final itr = DeltaIterator(document)..skip(index); + final itr = DeltaIterator(document.toDelta())..skip(index); final cur = itr.next(); if (cur.data is! String || !(cur.data as String).startsWith('\n')) { return null; @@ -278,7 +276,7 @@ class InsertEmbedsRule extends InsertRule { @override Delta? applyRule( - Delta document, + Document document, int index, { int? len, Object? data, @@ -295,7 +293,7 @@ class InsertEmbedsRule extends InsertRule { } final delta = Delta()..retain(index + (len ?? 0)); - final itr = DeltaIterator(document); + final itr = DeltaIterator(document.toDelta()); final prev = itr.skip(index), cur = itr.next(); final textBefore = prev?.data is String ? prev!.data as String? : ''; @@ -386,7 +384,7 @@ class AutoFormatMultipleLinksRule extends InsertRule { @override Delta? applyRule( - Delta document, + Document document, int index, { int? len, Object? data, @@ -397,7 +395,7 @@ class AutoFormatMultipleLinksRule extends InsertRule { if (data is! String) return null; // Get current text. - final entireText = Document.fromDelta(document).toPlainText(); + final entireText = document.toPlainText(); // Get word before insertion. final leftWordPart = entireText @@ -502,7 +500,7 @@ class AutoFormatLinksRule extends InsertRule { @override Delta? applyRule( - Delta document, + Document document, int index, { int? len, Object? data, @@ -512,7 +510,7 @@ class AutoFormatLinksRule extends InsertRule { return null; } - final itr = DeltaIterator(document); + final itr = DeltaIterator(document.toDelta()); final prev = itr.skip(index); if (prev == null || prev.data is! String) { return null; @@ -548,7 +546,7 @@ class PreserveInlineStylesRule extends InsertRule { @override Delta? applyRule( - Delta document, + Document document, int index, { int? len, Object? data, @@ -558,7 +556,7 @@ class PreserveInlineStylesRule extends InsertRule { return null; } - final itr = DeltaIterator(document); + final itr = DeltaIterator(document.toDelta()); var prev = itr.skip(len == 0 ? index : index + 1); if (prev == null || prev.data is! String) return null; @@ -609,7 +607,7 @@ class CatchAllInsertRule extends InsertRule { @override Delta applyRule( - Delta document, + Document document, int index, { int? len, Object? data, diff --git a/lib/src/models/rules/rule.dart b/lib/src/models/rules/rule.dart index d7132b3e..fd319bb5 100644 --- a/lib/src/models/rules/rule.dart +++ b/lib/src/models/rules/rule.dart @@ -14,7 +14,7 @@ abstract class Rule { const Rule(); Delta? apply( - Delta document, + Document document, int index, { int? len, Object? data, @@ -35,7 +35,7 @@ abstract class Rule { /// Applies heuristic rule to an operation on a [document] and returns /// resulting [Delta]. Delta? applyRule( - Delta document, + Document document, int index, { int? len, Object? data, @@ -85,13 +85,12 @@ class Rules { Object? data, Attribute? attribute, }) { - final delta = document.toDelta(); for (final rule in _customRules + _rules) { if (rule.type != ruleType) { continue; } try { - final result = rule.apply(delta, index, + final result = rule.apply(document, index, len: len, data: data, attribute: attribute); if (result != null) { return result..trim();