index.d.cts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { Node } from 'prosemirror-model';
  2. import { StepMap } from 'prosemirror-transform';
  3. /**
  4. Stores metadata for a part of a change.
  5. */
  6. declare class Span<Data = any> {
  7. /**
  8. The length of this span.
  9. */
  10. readonly length: number;
  11. /**
  12. The data associated with this span.
  13. */
  14. readonly data: Data;
  15. }
  16. /**
  17. A replaced range with metadata associated with it.
  18. */
  19. declare class Change<Data = any> {
  20. /**
  21. The start of the range deleted/replaced in the old document.
  22. */
  23. readonly fromA: number;
  24. /**
  25. The end of the range in the old document.
  26. */
  27. readonly toA: number;
  28. /**
  29. The start of the range inserted in the new document.
  30. */
  31. readonly fromB: number;
  32. /**
  33. The end of the range in the new document.
  34. */
  35. readonly toB: number;
  36. /**
  37. Data associated with the deleted content. The length of these
  38. spans adds up to `this.toA - this.fromA`.
  39. */
  40. readonly deleted: readonly Span<Data>[];
  41. /**
  42. Data associated with the inserted content. Length adds up to
  43. `this.toB - this.toA`.
  44. */
  45. readonly inserted: readonly Span<Data>[];
  46. /**
  47. This merges two changesets (the end document of x should be the
  48. start document of y) into a single one spanning the start of x to
  49. the end of y.
  50. */
  51. static merge<Data>(x: readonly Change<Data>[], y: readonly Change<Data>[], combine: (dataA: Data, dataB: Data) => Data): readonly Change<Data>[];
  52. }
  53. /**
  54. Simplifies a set of changes for presentation. This makes the
  55. assumption that having both insertions and deletions within a word
  56. is confusing, and, when such changes occur without a word boundary
  57. between them, they should be expanded to cover the entire set of
  58. words (in the new document) they touch. An exception is made for
  59. single-character replacements.
  60. */
  61. declare function simplifyChanges(changes: readonly Change[], doc: Node): Change<any>[];
  62. /**
  63. A change set tracks the changes to a document from a given point
  64. in the past. It condenses a number of step maps down to a flat
  65. sequence of replacements, and simplifies replacments that
  66. partially undo themselves by comparing their content.
  67. */
  68. declare class ChangeSet<Data = any> {
  69. /**
  70. Replaced regions.
  71. */
  72. readonly changes: readonly Change<Data>[];
  73. /**
  74. Computes a new changeset by adding the given step maps and
  75. metadata (either as an array, per-map, or as a single value to be
  76. associated with all maps) to the current set. Will not mutate the
  77. old set.
  78. Note that due to simplification that happens after each add,
  79. incrementally adding steps might create a different final set
  80. than adding all those changes at once, since different document
  81. tokens might be matched during simplification depending on the
  82. boundaries of the current changed ranges.
  83. */
  84. addSteps(newDoc: Node, maps: readonly StepMap[], data: Data | readonly Data[]): ChangeSet<Data>;
  85. /**
  86. The starting document of the change set.
  87. */
  88. get startDoc(): Node;
  89. /**
  90. Map the span's data values in the given set through a function
  91. and construct a new set with the resulting data.
  92. */
  93. map(f: (range: Span<Data>) => Data): ChangeSet<Data>;
  94. /**
  95. Compare two changesets and return the range in which they are
  96. changed, if any. If the document changed between the maps, pass
  97. the maps for the steps that changed it as second argument, and
  98. make sure the method is called on the old set and passed the new
  99. set. The returned positions will be in new document coordinates.
  100. */
  101. changedRange(b: ChangeSet, maps?: readonly StepMap[]): {
  102. from: number;
  103. to: number;
  104. } | null;
  105. /**
  106. Create a changeset with the given base object and configuration.
  107. The `combine` function is used to compare and combine metadata—it
  108. should return null when metadata isn't compatible, and a combined
  109. version for a merged range when it is.
  110. */
  111. static create<Data = any>(doc: Node, combine?: (dataA: Data, dataB: Data) => Data): ChangeSet<Data>;
  112. }
  113. export { Change, ChangeSet, Span, simplifyChanges };