zefyr

delete_rules.dart 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2018, the Zefyr project authors. Please see the AUTHORS file
  2. // for details. All rights reserved. Use of this source code is governed by a
  3. // BSD-style license that can be found in the LICENSE file.
  4. import 'package:notus/notus.dart';
  5. import 'package:quill_delta/quill_delta.dart';
  6. /// A heuristic rule for delete operations.
  7. abstract class DeleteRule {
  8. /// Constant constructor allows subclasses to declare constant constructors.
  9. const DeleteRule();
  10. /// Applies heuristic rule to a delete operation on a [document] and returns
  11. /// resulting [Delta].
  12. Delta apply(Delta document, int index, int length);
  13. }
  14. /// Fallback rule for delete operations which simply deletes specified text
  15. /// range without any special handling.
  16. class CatchAllDeleteRule extends DeleteRule {
  17. const CatchAllDeleteRule();
  18. @override
  19. Delta apply(Delta document, int index, int length) {
  20. return Delta()
  21. ..retain(index)
  22. ..delete(length);
  23. }
  24. }
  25. /// Preserves line format when user deletes the line's line-break character
  26. /// effectively merging it with the next line.
  27. ///
  28. /// This rule makes sure to apply all style attributes of deleted line-break
  29. /// to the next available line-break, which may reset any style attributes
  30. /// already present there.
  31. class PreserveLineStyleOnMergeRule extends DeleteRule {
  32. const PreserveLineStyleOnMergeRule();
  33. @override
  34. Delta apply(Delta document, int index, int length) {
  35. final iter = DeltaIterator(document);
  36. iter.skip(index);
  37. final target = iter.next(1);
  38. if (target.data != '\n') return null;
  39. iter.skip(length - 1);
  40. final result = Delta()
  41. ..retain(index)
  42. ..delete(length);
  43. // Look for next line-break to apply the attributes
  44. while (iter.hasNext) {
  45. final op = iter.next();
  46. final lf = op.data.indexOf('\n');
  47. if (lf == -1) {
  48. result..retain(op.length);
  49. continue;
  50. }
  51. var attributes = _unsetAttributes(op.attributes);
  52. if (target.isNotPlain) {
  53. attributes ??= <String, dynamic>{};
  54. attributes.addAll(target.attributes);
  55. }
  56. result..retain(lf)..retain(1, attributes);
  57. break;
  58. }
  59. return result;
  60. }
  61. Map<String, dynamic> _unsetAttributes(Map<String, dynamic> attributes) {
  62. if (attributes == null) return null;
  63. return attributes.map<String, dynamic>(
  64. (String key, dynamic value) => MapEntry<String, dynamic>(key, null));
  65. }
  66. }
  67. /// Prevents user from merging line containing an embed with other lines.
  68. class EnsureEmbedLineRule extends DeleteRule {
  69. const EnsureEmbedLineRule();
  70. @override
  71. Delta apply(Delta document, int index, int length) {
  72. final iter = DeltaIterator(document);
  73. // First, check if line-break deleted after an embed.
  74. var op = iter.skip(index);
  75. var indexDelta = 0;
  76. var lengthDelta = 0;
  77. var remaining = length;
  78. var foundEmbed = false;
  79. var hasLineBreakBefore = false;
  80. if (op != null && op.data.endsWith(kZeroWidthSpace)) {
  81. foundEmbed = true;
  82. var candidate = iter.next(1);
  83. remaining--;
  84. if (candidate.data == '\n') {
  85. indexDelta += 1;
  86. lengthDelta -= 1;
  87. /// Check if it's an empty line
  88. candidate = iter.next(1);
  89. remaining--;
  90. if (candidate.data == '\n') {
  91. // Allow deleting empty line after an embed.
  92. lengthDelta += 1;
  93. }
  94. }
  95. } else {
  96. // If op is `null` it's a beginning of the doc, e.g. implicit line break.
  97. hasLineBreakBefore = op == null || op.data.endsWith('\n');
  98. }
  99. // Second, check if line-break deleted before an embed.
  100. op = iter.skip(remaining);
  101. if (op != null && op.data.endsWith('\n')) {
  102. final candidate = iter.next(1);
  103. // If there is a line-break before deleted range we allow the operation
  104. // since it results in a correctly formatted line with single embed in it.
  105. if (candidate.data == kZeroWidthSpace && !hasLineBreakBefore) {
  106. foundEmbed = true;
  107. lengthDelta -= 1;
  108. }
  109. }
  110. if (foundEmbed) {
  111. return Delta()
  112. ..retain(index + indexDelta)
  113. ..delete(length + lengthDelta);
  114. }
  115. return null; // fallback
  116. }
  117. }