mock-doc.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { ShareLatexOTShareDoc } from '../../../../../types/share-doc'
  2. import { EventEmitter } from 'events'
  3. export const docId = 'test-doc'
  4. export function mockDocContent(content: string) {
  5. return `
  6. \\documentclass{article}
  7. \\title{Your Paper}
  8. \\author{You}
  9. \\begin{document}
  10. \\maketitle
  11. \\begin{abstract}
  12. Your abstract.
  13. \\end{abstracts}
  14. \\section{Introduction}
  15. Your introduction goes here!
  16. \\section{Results}
  17. Your results go here! \\cite{foo}
  18. ${content}
  19. \\end{document}`
  20. }
  21. const contentLines = Array.from(Array(100), (e, i) => `contentLine ${i}`)
  22. const defaultContent = mockDocContent(contentLines.join('\n'))
  23. const MAX_DOC_LENGTH = 2 * 1024 * 1024 // ol-maxDocLength
  24. class MockShareDoc extends EventEmitter {
  25. otType = 'sharejs-text-ot' as const
  26. snapshot = ''
  27. constructor(public text: string) {
  28. super()
  29. }
  30. getText() {
  31. return this.text
  32. }
  33. insert() {
  34. // do nothing
  35. }
  36. del() {
  37. // do nothing
  38. }
  39. submitOp() {
  40. // do nothing
  41. }
  42. }
  43. export const mockDoc = (
  44. content = defaultContent,
  45. { rangesOptions = {} } = {}
  46. ) => {
  47. const mockShareJSDoc: ShareLatexOTShareDoc = new MockShareDoc(content)
  48. return {
  49. doc_id: docId,
  50. getType: () => 'sharejs-text-ot',
  51. getSnapshot: () => {
  52. return content
  53. },
  54. attachToCM6: (cm6: any) => {
  55. cm6.attachShareJs(mockShareJSDoc, MAX_DOC_LENGTH)
  56. },
  57. detachFromCM6: () => {
  58. // Do nothing
  59. },
  60. on: () => {
  61. // Do nothing
  62. },
  63. off: () => {
  64. // Do nothing
  65. },
  66. ranges: {
  67. changes: [],
  68. comments: [],
  69. getIdSeed: () => '123',
  70. setIdSeed: () => {},
  71. getTrackedDeletesLength: () => 0,
  72. getDirtyState: () => ({
  73. comment: {
  74. moved: {},
  75. removed: {},
  76. added: {},
  77. },
  78. change: {
  79. moved: {},
  80. removed: {},
  81. added: {},
  82. },
  83. }),
  84. resetDirtyState: () => {},
  85. removeCommentId: () => {},
  86. ...rangesOptions,
  87. },
  88. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  89. submitOp: (op: any) => {},
  90. setTrackChangesIdSeeds: () => {},
  91. getTrackingChanges: () => true,
  92. setTrackChangesUserId: () => {},
  93. getInflightOp: () => null,
  94. getPendingOp: () => null,
  95. hasBufferedOps: () => false,
  96. leaveAndCleanUpPromise: () => false,
  97. isHistoryOT: () => false,
  98. }
  99. }