From 93dc5c6881e84046b34e509ac36f45327a62383b Mon Sep 17 00:00:00 2001 From: Amir Panahandeh Date: Sun, 10 Dec 2023 12:35:42 +0330 Subject: [PATCH] Preserve line style when entering new line Fixes #158 --- packages/parchment/lib/src/heuristics.dart | 2 +- .../lib/src/heuristics/insert_rules.dart | 24 ++++++++++--------- .../test/heuristics/insert_rules_test.dart | 12 ++++++---- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/parchment/lib/src/heuristics.dart b/packages/parchment/lib/src/heuristics.dart index 1a9f8a20..6eb71c11 100644 --- a/packages/parchment/lib/src/heuristics.dart +++ b/packages/parchment/lib/src/heuristics.dart @@ -33,7 +33,7 @@ class ParchmentHeuristics { PreserveBlockStyleOnInsertRule(), // Lines PreserveLineStyleOnSplitRule(), - ResetLineFormatOnNewLineRule(), + PreserveLineFormatOnNewLineRule(), // Inlines PreserveInlineStylesRule(), // Catch-all diff --git a/packages/parchment/lib/src/heuristics/insert_rules.dart b/packages/parchment/lib/src/heuristics/insert_rules.dart index ac42396b..7c0b5f82 100644 --- a/packages/parchment/lib/src/heuristics/insert_rules.dart +++ b/packages/parchment/lib/src/heuristics/insert_rules.dart @@ -63,6 +63,7 @@ class CatchAllInsertRule extends InsertRule { } } +//TODO: Investigate if PreserveLineStyleOnSplitRule and PreserveLineFormatOnNewLineRule can be combined into a single heuristic /// Preserves line format when user splits the line into two. /// /// This rule ignores scenarios when the line is split on its edge, meaning @@ -112,13 +113,13 @@ class PreserveLineStyleOnSplitRule extends InsertRule { } } -/// Resets format for a newly inserted line when insert occurred at the end -/// of a line (right before a newline). +/// Preserves line format when insert occurred +/// at the end of a line (right before a newline). /// -/// This handles scenarios when a new line is added when at the end of a +/// This also handles scenarios when a new line is added when at the end of a /// heading line. The newly added line should be a regular paragraph. -class ResetLineFormatOnNewLineRule extends InsertRule { - const ResetLineFormatOnNewLineRule(); +class PreserveLineFormatOnNewLineRule extends InsertRule { + const PreserveLineFormatOnNewLineRule(); @override Delta? apply(Delta document, int index, Object data) { @@ -135,17 +136,18 @@ class ResetLineFormatOnNewLineRule extends InsertRule { final targetText = target.data as String; + Map? resetStyle; if (targetText.startsWith('\n')) { if (target.attributes != null && target.attributes!.containsKey(ParchmentAttribute.heading.key)) { // Reset heading style - final resetStyle = ParchmentAttribute.heading.unset.toJson(); - return Delta() - ..retain(index) - ..insert('\n', target.attributes) - ..retain(1, resetStyle) - ..trim(); + resetStyle = ParchmentAttribute.heading.unset.toJson(); } + return Delta() + ..retain(index) + ..insert('\n', target.attributes) + ..retain(1, resetStyle) + ..trim(); } return null; } diff --git a/packages/parchment/test/heuristics/insert_rules_test.dart b/packages/parchment/test/heuristics/insert_rules_test.dart index c7bcbc85..e976896c 100644 --- a/packages/parchment/test/heuristics/insert_rules_test.dart +++ b/packages/parchment/test/heuristics/insert_rules_test.dart @@ -59,8 +59,8 @@ void main() { }); }); - group('$ResetLineFormatOnNewLineRule', () { - final rule = const ResetLineFormatOnNewLineRule(); + group('$PreserveLineFormatOnNewLineRule', () { + final rule = const PreserveLineFormatOnNewLineRule(); test('applies when line-break is inserted at the end of line', () { final doc = Delta() @@ -75,10 +75,14 @@ void main() { expect(actual, expected); }); - test("doesn't apply without style reset if not needed", () { + test('applies without style reset if not needed', () { final doc = Delta()..insert('Hello world\n'); final actual = rule.apply(doc, 11, '\n'); - expect(actual, isNull); + expect(actual, isNotNull); + final expected = Delta() + ..retain(11) + ..insert('\n'); + expect(actual, expected); }); test('applies at the beginning of a document', () {