Skip to content

Commit

Permalink
Preserve line style when entering new line (#190)
Browse files Browse the repository at this point in the history
Fixes #158
  • Loading branch information
Amir-P authored Dec 10, 2023
1 parent 4c3e95f commit 051a5a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/parchment/lib/src/heuristics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ParchmentHeuristics {
PreserveBlockStyleOnInsertRule(),
// Lines
PreserveLineStyleOnSplitRule(),
ResetLineFormatOnNewLineRule(),
PreserveLineFormatOnNewLineRule(),
// Inlines
PreserveInlineStylesRule(),
// Catch-all
Expand Down
24 changes: 13 additions & 11 deletions packages/parchment/lib/src/heuristics/insert_rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -135,17 +136,18 @@ class ResetLineFormatOnNewLineRule extends InsertRule {

final targetText = target.data as String;

Map<String, dynamic>? 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;
}
Expand Down
12 changes: 8 additions & 4 deletions packages/parchment/test/heuristics/insert_rules_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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', () {
Expand Down

0 comments on commit 051a5a4

Please sign in to comment.