comments.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import { Range, RangeSet, RangeValue, Transaction } from '@codemirror/state'
  2. import {
  3. AnyOperation,
  4. Change,
  5. CommentOperation,
  6. } from '../../../../../../types/change'
  7. import { ThreadId } from '../../../../../../types/review-panel/review-panel'
  8. import { DocumentContainer } from '@/features/ide-react/editor/document-container'
  9. export type StoredComment = {
  10. text: string
  11. comments: {
  12. offset: number
  13. text: string
  14. comment: Change<CommentOperation>
  15. }[]
  16. }
  17. /**
  18. * Find tracked comments within the range of the current transaction's changes
  19. */
  20. export const findCommentsInCut = (
  21. currentDoc: DocumentContainer,
  22. transaction: Transaction
  23. ) => {
  24. const items: StoredComment[] = []
  25. transaction.changes.iterChanges((fromA, toA) => {
  26. const comments = currentDoc
  27. .ranges!.comments.filter(
  28. comment =>
  29. fromA <= comment.op.p && comment.op.p + comment.op.c.length <= toA
  30. )
  31. .map(comment => ({
  32. offset: comment.op.p - fromA,
  33. text: comment.op.c,
  34. comment,
  35. }))
  36. if (comments.length) {
  37. items.push({
  38. text: transaction.startState.sliceDoc(fromA, toA),
  39. comments,
  40. })
  41. }
  42. })
  43. return items
  44. }
  45. /**
  46. * Find stored comments matching the text of the current transaction's changes
  47. */
  48. export const findCommentsInPaste = (
  49. storedComments: StoredComment[],
  50. transaction: Transaction
  51. ) => {
  52. const ops: CommentOperation[] = []
  53. transaction.changes.iterChanges((fromA, toA, fromB, toB, inserted) => {
  54. const insertedText = inserted.toString()
  55. // note: only using the first match
  56. const matchedComment = storedComments.find(
  57. item => item.text === insertedText
  58. )
  59. if (matchedComment) {
  60. for (const { offset, text, comment } of matchedComment.comments) {
  61. // Resubmitting an existing comment op (by thread id) will move it
  62. ops.push({
  63. c: text,
  64. p: fromB + offset,
  65. t: comment.id as ThreadId,
  66. })
  67. }
  68. }
  69. })
  70. return ops
  71. }
  72. class CommentRangeValue extends RangeValue {
  73. constructor(
  74. public content: string,
  75. public comment: Change<CommentOperation>
  76. ) {
  77. super()
  78. }
  79. }
  80. /**
  81. * Find tracked comments with no content with the ranges of a transaction's changes
  82. */
  83. export const findDetachedCommentsInChanges = (
  84. currentDoc: DocumentContainer,
  85. transaction: Transaction
  86. ) => {
  87. const items: Range<CommentRangeValue>[] = []
  88. transaction.changes.iterChanges((fromA, toA) => {
  89. for (const comment of currentDoc.ranges!.comments) {
  90. const content = comment.op.c
  91. // TODO: handle comments that were never attached
  92. if (!content.length) {
  93. continue
  94. }
  95. const from = comment.op.p
  96. const to = from + content.length
  97. if (fromA <= from && to <= toA) {
  98. items.push(new CommentRangeValue(content, comment).range(from, to))
  99. }
  100. }
  101. })
  102. return RangeSet.of(items, true)
  103. }
  104. /**
  105. * Submit operations to the ShareJS doc
  106. * (used when restoring comments on paste)
  107. */
  108. const submitOps = (
  109. currentDoc: DocumentContainer,
  110. ops: AnyOperation[],
  111. transaction: Transaction
  112. ) => {
  113. for (const op of ops) {
  114. currentDoc.submitOp(op)
  115. }
  116. // Check that comments still match text. Will throw error if not.
  117. currentDoc.ranges!.validate(transaction.state.doc.toString())
  118. }
  119. /**
  120. * Wait for the ShareJS doc to fire an event, then submit the operations.
  121. */
  122. const submitOpsAfterEvent = (
  123. currentDoc: DocumentContainer,
  124. eventName: string,
  125. ops: AnyOperation[],
  126. transaction: Transaction
  127. ) => {
  128. // We have to wait until the change has been processed by the range
  129. // tracker, since if we move the ops into place beforehand, they will be
  130. // moved again when the changes are processed by the range tracker. This
  131. // ranges:dirty event is fired after the doc has applied the changes to
  132. // the range tracker.
  133. // TODO: could put this in an update listener instead, if the ShareJS doc has been updated by then?
  134. currentDoc.on(eventName, () => {
  135. currentDoc.off(eventName)
  136. window.setTimeout(() => submitOps(currentDoc, ops, transaction))
  137. })
  138. }
  139. /**
  140. * Look through the comments stored on cut, and restore those in text that matches the pasted text.
  141. */
  142. export const restoreCommentsOnPaste = (
  143. currentDoc: DocumentContainer,
  144. transaction: Transaction,
  145. storedComments: StoredComment[]
  146. ) => {
  147. if (storedComments.length) {
  148. const ops = findCommentsInPaste(storedComments, transaction)
  149. if (ops.length) {
  150. submitOpsAfterEvent(
  151. currentDoc,
  152. 'ranges:dirty.paste-cm6',
  153. ops,
  154. transaction
  155. )
  156. }
  157. }
  158. }
  159. /**
  160. * When undoing a change, find comments from the original content and restore them.
  161. */
  162. export const restoreDetachedComments = (
  163. currentDoc: DocumentContainer,
  164. transaction: Transaction,
  165. storedComments: RangeSet<any>
  166. ) => {
  167. const ops: CommentOperation[] = []
  168. const cursor = storedComments.iter()
  169. while (cursor.value) {
  170. const { id } = cursor.value.comment
  171. const comment = currentDoc.ranges!.comments.find(item => item.id === id)
  172. // check that the comment still exists and is detached
  173. if (comment && comment.op.c === '') {
  174. const content = transaction.state.doc.sliceString(
  175. cursor.from,
  176. cursor.from + cursor.value.content.length
  177. )
  178. if (cursor.value.content === content) {
  179. ops.push({
  180. c: cursor.value.content,
  181. p: cursor.from,
  182. t: id,
  183. })
  184. }
  185. }
  186. cursor.next()
  187. }
  188. // FIXME: timing issue with rapid undos
  189. if (ops.length) {
  190. window.setTimeout(() => {
  191. submitOps(currentDoc, ops, transaction)
  192. }, 0)
  193. }
  194. // submitOpsAfterEvent('ranges:dirty.undo-cm6', ops, transaction)
  195. }