HistoryManager.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS206: Consider reworking classes to avoid initClass
  14. * DS207: Consider shorter variations of null checks
  15. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  16. */
  17. import moment from 'moment'
  18. import ColorManager from '../colors/ColorManager'
  19. import displayNameForUser from './util/displayNameForUser'
  20. import './controllers/HistoryListController'
  21. import './controllers/HistoryDiffController'
  22. import './directives/infiniteScroll'
  23. let HistoryManager
  24. export default HistoryManager = (function () {
  25. HistoryManager = class HistoryManager {
  26. static initClass() {
  27. this.prototype.BATCH_SIZE = 10
  28. }
  29. constructor(ide, $scope) {
  30. this.ide = ide
  31. this.$scope = $scope
  32. this.reset()
  33. this.$scope.toggleHistory = () => {
  34. if (this.$scope.ui.view === 'history') {
  35. return this.hide()
  36. } else {
  37. return this.show()
  38. }
  39. }
  40. this.$scope.$watch('history.selection.updates', updates => {
  41. if (updates != null && updates.length > 0) {
  42. this._selectDocFromUpdates()
  43. return this.reloadDiff()
  44. }
  45. })
  46. this.$scope.$on('entity:selected', (event, entity) => {
  47. if (this.$scope.ui.view === 'history' && entity.type === 'doc') {
  48. this.$scope.history.selection.doc = entity
  49. return this.reloadDiff()
  50. }
  51. })
  52. }
  53. show() {
  54. this.$scope.ui.view = 'history'
  55. return this.reset()
  56. }
  57. hide() {
  58. this.$scope.ui.view = 'editor'
  59. // Make sure we run the 'open' logic for whatever is currently selected
  60. return this.$scope.$emit(
  61. 'entity:selected',
  62. this.ide.fileTreeManager.findSelectedEntity()
  63. )
  64. }
  65. reset() {
  66. return (this.$scope.history = {
  67. updates: [],
  68. nextBeforeTimestamp: null,
  69. atEnd: false,
  70. selection: {
  71. updates: [],
  72. doc: null,
  73. range: {
  74. fromV: null,
  75. toV: null,
  76. start_ts: null,
  77. end_ts: null,
  78. },
  79. },
  80. diff: null,
  81. })
  82. }
  83. autoSelectRecentUpdates() {
  84. if (this.$scope.history.updates.length === 0) {
  85. return
  86. }
  87. this.$scope.history.updates[0].selectedTo = true
  88. let indexOfLastUpdateNotByMe = 0
  89. for (let i = 0; i < this.$scope.history.updates.length; i++) {
  90. const update = this.$scope.history.updates[i]
  91. if (this._updateContainsUserId(update, this.$scope.user.id)) {
  92. break
  93. }
  94. indexOfLastUpdateNotByMe = i
  95. }
  96. return (this.$scope.history.updates[
  97. indexOfLastUpdateNotByMe
  98. ].selectedFrom = true)
  99. }
  100. fetchNextBatchOfUpdates() {
  101. let url = `/project/${this.ide.project_id}/updates?min_count=${this.BATCH_SIZE}`
  102. if (this.$scope.history.nextBeforeTimestamp != null) {
  103. url += `&before=${this.$scope.history.nextBeforeTimestamp}`
  104. }
  105. this.$scope.history.loading = true
  106. return this.ide.$http.get(url).then(response => {
  107. const { data } = response
  108. this._loadUpdates(data.updates)
  109. this.$scope.history.nextBeforeTimestamp = data.nextBeforeTimestamp
  110. if (data.nextBeforeTimestamp == null) {
  111. this.$scope.history.atEnd = true
  112. }
  113. return (this.$scope.history.loading = false)
  114. })
  115. }
  116. reloadDiff() {
  117. let { diff } = this.$scope.history
  118. const { updates, doc } = this.$scope.history.selection
  119. const { fromV, toV, start_ts, end_ts } =
  120. this._calculateRangeFromSelection()
  121. if (doc == null) {
  122. return
  123. }
  124. if (
  125. diff != null &&
  126. diff.doc === doc &&
  127. diff.fromV === fromV &&
  128. diff.toV === toV
  129. ) {
  130. return
  131. }
  132. this.$scope.history.diff = diff = {
  133. fromV,
  134. toV,
  135. start_ts,
  136. end_ts,
  137. doc,
  138. error: false,
  139. pathname: doc.name,
  140. }
  141. if (!doc.deleted) {
  142. diff.loading = true
  143. let url = `/project/${this.$scope.project_id}/doc/${diff.doc.id}/diff`
  144. if (diff.fromV != null && diff.toV != null) {
  145. url += `?from=${diff.fromV}&to=${diff.toV}`
  146. }
  147. return this.ide.$http
  148. .get(url)
  149. .then(response => {
  150. const { data } = response
  151. diff.loading = false
  152. const { text, highlights } = this._parseDiff(data)
  153. diff.text = text
  154. return (diff.highlights = highlights)
  155. })
  156. .catch(function () {
  157. diff.loading = false
  158. return (diff.error = true)
  159. })
  160. } else {
  161. diff.deleted = true
  162. diff.restoreInProgress = false
  163. diff.restoreDeletedSuccess = false
  164. return (diff.restoredDocNewId = null)
  165. }
  166. }
  167. restoreDeletedDoc(doc) {
  168. const url = `/project/${this.$scope.project_id}/doc/${doc.id}/restore`
  169. return this.ide.$http.post(url, {
  170. name: doc.name,
  171. _csrf: window.csrfToken,
  172. })
  173. }
  174. restoreDiff(diff) {
  175. const url = `/project/${this.$scope.project_id}/doc/${diff.doc.id}/version/${diff.fromV}/restore`
  176. return this.ide.$http.post(url, { _csrf: window.csrfToken })
  177. }
  178. _parseDiff(diff) {
  179. let row = 0
  180. let column = 0
  181. const highlights = []
  182. let text = ''
  183. const iterable = diff.diff || []
  184. for (let i = 0; i < iterable.length; i++) {
  185. let endColumn, endRow
  186. const entry = iterable[i]
  187. let content = entry.u || entry.i || entry.d
  188. if (!content) {
  189. content = ''
  190. }
  191. text += content
  192. const lines = content.split('\n')
  193. const startRow = row
  194. const startColumn = column
  195. if (lines.length > 1) {
  196. endRow = startRow + lines.length - 1
  197. endColumn = lines[lines.length - 1].length
  198. } else {
  199. endRow = startRow
  200. endColumn = startColumn + lines[0].length
  201. }
  202. row = endRow
  203. column = endColumn
  204. const range = {
  205. start: {
  206. row: startRow,
  207. column: startColumn,
  208. },
  209. end: {
  210. row: endRow,
  211. column: endColumn,
  212. },
  213. }
  214. if (entry.i != null || entry.d != null) {
  215. const name = displayNameForUser(entry.meta.user)
  216. const date = moment(entry.meta.end_ts).format('Do MMM YYYY, h:mm a')
  217. if (entry.i != null) {
  218. highlights.push({
  219. label: `Added by ${name} on ${date}`,
  220. highlight: range,
  221. hue: ColorManager.getHueForUserId(
  222. entry.meta.user != null ? entry.meta.user.id : undefined
  223. ),
  224. })
  225. } else if (entry.d != null) {
  226. highlights.push({
  227. label: `Deleted by ${name} on ${date}`,
  228. strikeThrough: range,
  229. hue: ColorManager.getHueForUserId(
  230. entry.meta.user != null ? entry.meta.user.id : undefined
  231. ),
  232. })
  233. }
  234. }
  235. }
  236. return { text, highlights }
  237. }
  238. _loadUpdates(updates) {
  239. if (updates == null) {
  240. updates = []
  241. }
  242. let previousUpdate =
  243. this.$scope.history.updates[this.$scope.history.updates.length - 1]
  244. for (const update of Array.from(updates)) {
  245. update.pathnames = [] // Used for display
  246. const object = update.docs || {}
  247. for (const doc_id in object) {
  248. const doc = object[doc_id]
  249. doc.entity = this.ide.fileTreeManager.findEntityById(doc_id, {
  250. includeDeleted: true,
  251. })
  252. update.pathnames.push(doc.entity.name)
  253. }
  254. for (const user of Array.from(update.meta.users || [])) {
  255. if (user != null) {
  256. user.hue = ColorManager.getHueForUserId(user.id)
  257. }
  258. }
  259. if (
  260. previousUpdate == null ||
  261. !moment(previousUpdate.meta.end_ts).isSame(update.meta.end_ts, 'day')
  262. ) {
  263. update.meta.first_in_day = true
  264. }
  265. update.selectedFrom = false
  266. update.selectedTo = false
  267. update.inSelection = false
  268. previousUpdate = update
  269. }
  270. const firstLoad = this.$scope.history.updates.length === 0
  271. this.$scope.history.updates = this.$scope.history.updates.concat(updates)
  272. if (firstLoad) {
  273. return this.autoSelectRecentUpdates()
  274. }
  275. }
  276. _calculateRangeFromSelection() {
  277. let end_ts, start_ts, toV
  278. let fromV = (toV = start_ts = end_ts = null)
  279. const selected_doc_id =
  280. this.$scope.history.selection.doc != null
  281. ? this.$scope.history.selection.doc.id
  282. : undefined
  283. for (const update of Array.from(
  284. this.$scope.history.selection.updates || []
  285. )) {
  286. for (const doc_id in update.docs) {
  287. const doc = update.docs[doc_id]
  288. if (doc_id === selected_doc_id) {
  289. if (fromV != null && toV != null) {
  290. fromV = Math.min(fromV, doc.fromV)
  291. toV = Math.max(toV, doc.toV)
  292. start_ts = Math.min(start_ts, update.meta.start_ts)
  293. end_ts = Math.max(end_ts, update.meta.end_ts)
  294. } else {
  295. ;({ fromV } = doc)
  296. ;({ toV } = doc)
  297. ;({ start_ts } = update.meta)
  298. ;({ end_ts } = update.meta)
  299. }
  300. break
  301. }
  302. }
  303. }
  304. return { fromV, toV, start_ts, end_ts }
  305. }
  306. // Set the track changes selected doc to one of the docs in the range
  307. // of currently selected updates. If we already have a selected doc
  308. // then prefer this one if present.
  309. _selectDocFromUpdates() {
  310. let doc, doc_id
  311. const affected_docs = {}
  312. for (const update of Array.from(this.$scope.history.selection.updates)) {
  313. for (doc_id in update.docs) {
  314. doc = update.docs[doc_id]
  315. affected_docs[doc_id] = doc.entity
  316. }
  317. }
  318. let selected_doc = this.$scope.history.selection.doc
  319. if (selected_doc != null && affected_docs[selected_doc.id] != null) {
  320. // Selected doc is already open
  321. } else {
  322. const doc_ids = Object.keys(affected_docs)
  323. if (doc_ids.length > 0) {
  324. const doc_id = doc_ids[0]
  325. doc = affected_docs[doc_id]
  326. selected_doc = doc
  327. }
  328. }
  329. this.$scope.history.selection.doc = selected_doc
  330. return this.ide.fileTreeManager.selectEntity(selected_doc)
  331. }
  332. _updateContainsUserId(update, user_id) {
  333. for (const user of Array.from(update.meta.users)) {
  334. if ((user != null ? user.id : undefined) === user_id) {
  335. return true
  336. }
  337. }
  338. return false
  339. }
  340. }
  341. HistoryManager.initClass()
  342. return HistoryManager
  343. })()