zefyr

heuristics.dart 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. import 'heuristics/delete_rules.dart';
  7. import 'heuristics/format_rules.dart';
  8. import 'heuristics/insert_rules.dart';
  9. /// Registry for insert, format and delete heuristic rules used by
  10. /// [NotusDocument] documents.
  11. class NotusHeuristics {
  12. /// Default set of heuristic rules.
  13. static const NotusHeuristics fallback = NotusHeuristics(
  14. formatRules: [
  15. FormatEmbedsRule(),
  16. FormatLinkAtCaretPositionRule(),
  17. ResolveLineFormatRule(),
  18. ResolveInlineFormatRule(),
  19. // No need in catch-all rule here since the above rules cover all
  20. // attributes.
  21. ],
  22. insertRules: [
  23. PreserveBlockStyleOnPasteRule(),
  24. ForceNewlineForInsertsAroundEmbedRule(),
  25. PreserveLineStyleOnSplitRule(),
  26. AutoExitBlockRule(),
  27. ResetLineFormatOnNewLineRule(),
  28. AutoFormatLinksRule(),
  29. PreserveInlineStylesRule(),
  30. CatchAllInsertRule(),
  31. ],
  32. deleteRules: [
  33. EnsureEmbedLineRule(),
  34. PreserveLineStyleOnMergeRule(),
  35. CatchAllDeleteRule(),
  36. ],
  37. );
  38. const NotusHeuristics({
  39. this.formatRules,
  40. this.insertRules,
  41. this.deleteRules,
  42. });
  43. /// List of format rules in this registry.
  44. final List<FormatRule> formatRules;
  45. /// List of insert rules in this registry.
  46. final List<InsertRule> insertRules;
  47. /// List of delete rules in this registry.
  48. final List<DeleteRule> deleteRules;
  49. /// Applies heuristic rules to specified insert operation based on current
  50. /// state of Notus [document].
  51. Delta applyInsertRules(NotusDocument document, int index, String insert) {
  52. final delta = document.toDelta();
  53. for (var rule in insertRules) {
  54. final result = rule.apply(delta, index, insert);
  55. if (result != null) return result..trim();
  56. }
  57. throw new StateError(
  58. 'Failed to apply insert heuristic rules: none applied.');
  59. }
  60. /// Applies heuristic rules to specified format operation based on current
  61. /// state of Notus [document].
  62. Delta applyFormatRules(
  63. NotusDocument document, int index, int length, NotusAttribute value) {
  64. final delta = document.toDelta();
  65. for (var rule in formatRules) {
  66. final result = rule.apply(delta, index, length, value);
  67. if (result != null) return result..trim();
  68. }
  69. throw new StateError(
  70. 'Failed to apply format heuristic rules: none applied.');
  71. }
  72. /// Applies heuristic rules to specified delete operation based on current
  73. /// state of Notus [document].
  74. Delta applyDeleteRules(NotusDocument document, int index, int length) {
  75. final delta = document.toDelta();
  76. for (var rule in deleteRules) {
  77. final result = rule.apply(delta, index, length);
  78. if (result != null) return result..trim();
  79. }
  80. throw new StateError(
  81. 'Failed to apply delete heuristic rules: none applied.');
  82. }
  83. }