HistoryManager.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 {
  120. fromV,
  121. toV,
  122. start_ts,
  123. end_ts
  124. } = this._calculateRangeFromSelection()
  125. if (doc == null) {
  126. return
  127. }
  128. if (
  129. diff != null &&
  130. diff.doc === doc &&
  131. diff.fromV === fromV &&
  132. diff.toV === toV
  133. ) {
  134. return
  135. }
  136. this.$scope.history.diff = diff = {
  137. fromV,
  138. toV,
  139. start_ts,
  140. end_ts,
  141. doc,
  142. error: false,
  143. pathname: doc.name
  144. }
  145. if (!doc.deleted) {
  146. diff.loading = true
  147. let url = `/project/${this.$scope.project_id}/doc/${diff.doc.id}/diff`
  148. if (diff.fromV != null && diff.toV != null) {
  149. url += `?from=${diff.fromV}&to=${diff.toV}`
  150. }
  151. return this.ide.$http
  152. .get(url)
  153. .then(response => {
  154. const { data } = response
  155. diff.loading = false
  156. const { text, highlights } = this._parseDiff(data)
  157. diff.text = text
  158. return (diff.highlights = highlights)
  159. })
  160. .catch(function() {
  161. diff.loading = false
  162. return (diff.error = true)
  163. })
  164. } else {
  165. diff.deleted = true
  166. diff.restoreInProgress = false
  167. diff.restoreDeletedSuccess = false
  168. return (diff.restoredDocNewId = null)
  169. }
  170. }
  171. restoreDeletedDoc(doc) {
  172. const url = `/project/${this.$scope.project_id}/doc/${doc.id}/restore`
  173. return this.ide.$http.post(url, {
  174. name: doc.name,
  175. _csrf: window.csrfToken
  176. })
  177. }
  178. restoreDiff(diff) {
  179. const url = `/project/${this.$scope.project_id}/doc/${diff.doc.id}/version/${diff.fromV}/restore`
  180. return this.ide.$http.post(url, { _csrf: window.csrfToken })
  181. }
  182. _parseDiff(diff) {
  183. let row = 0
  184. let column = 0
  185. const highlights = []
  186. let text = ''
  187. const iterable = diff.diff || []
  188. for (let i = 0; i < iterable.length; i++) {
  189. var endColumn, endRow
  190. const entry = iterable[i]
  191. let content = entry.u || entry.i || entry.d
  192. if (!content) {
  193. content = ''
  194. }
  195. text += content
  196. const lines = content.split('\n')
  197. const startRow = row
  198. const startColumn = column
  199. if (lines.length > 1) {
  200. endRow = startRow + lines.length - 1
  201. endColumn = lines[lines.length - 1].length
  202. } else {
  203. endRow = startRow
  204. endColumn = startColumn + lines[0].length
  205. }
  206. row = endRow
  207. column = endColumn
  208. const range = {
  209. start: {
  210. row: startRow,
  211. column: startColumn
  212. },
  213. end: {
  214. row: endRow,
  215. column: endColumn
  216. }
  217. }
  218. if (entry.i != null || entry.d != null) {
  219. const name = displayNameForUser(entry.meta.user)
  220. const date = moment(entry.meta.end_ts).format('Do MMM YYYY, h:mm a')
  221. if (entry.i != null) {
  222. highlights.push({
  223. label: `Added by ${name} on ${date}`,
  224. highlight: range,
  225. hue: ColorManager.getHueForUserId(
  226. entry.meta.user != null ? entry.meta.user.id : undefined
  227. )
  228. })
  229. } else if (entry.d != null) {
  230. highlights.push({
  231. label: `Deleted by ${name} on ${date}`,
  232. strikeThrough: range,
  233. hue: ColorManager.getHueForUserId(
  234. entry.meta.user != null ? entry.meta.user.id : undefined
  235. )
  236. })
  237. }
  238. }
  239. }
  240. return { text, highlights }
  241. }
  242. _loadUpdates(updates) {
  243. if (updates == null) {
  244. updates = []
  245. }
  246. let previousUpdate = this.$scope.history.updates[
  247. this.$scope.history.updates.length - 1
  248. ]
  249. for (let update of Array.from(updates)) {
  250. update.pathnames = [] // Used for display
  251. const object = update.docs || {}
  252. for (let doc_id in object) {
  253. const doc = object[doc_id]
  254. doc.entity = this.ide.fileTreeManager.findEntityById(doc_id, {
  255. includeDeleted: true
  256. })
  257. update.pathnames.push(doc.entity.name)
  258. }
  259. for (let user of Array.from(update.meta.users || [])) {
  260. if (user != null) {
  261. user.hue = ColorManager.getHueForUserId(user.id)
  262. }
  263. }
  264. if (
  265. previousUpdate == null ||
  266. !moment(previousUpdate.meta.end_ts).isSame(update.meta.end_ts, 'day')
  267. ) {
  268. update.meta.first_in_day = true
  269. }
  270. update.selectedFrom = false
  271. update.selectedTo = false
  272. update.inSelection = false
  273. previousUpdate = update
  274. }
  275. const firstLoad = this.$scope.history.updates.length === 0
  276. this.$scope.history.updates = this.$scope.history.updates.concat(updates)
  277. if (firstLoad) {
  278. return this.autoSelectRecentUpdates()
  279. }
  280. }
  281. _calculateRangeFromSelection() {
  282. let end_ts, start_ts, toV
  283. let fromV = (toV = start_ts = end_ts = null)
  284. const selected_doc_id =
  285. this.$scope.history.selection.doc != null
  286. ? this.$scope.history.selection.doc.id
  287. : undefined
  288. for (let update of Array.from(
  289. this.$scope.history.selection.updates || []
  290. )) {
  291. for (let doc_id in update.docs) {
  292. const doc = update.docs[doc_id]
  293. if (doc_id === selected_doc_id) {
  294. if (fromV != null && toV != null) {
  295. fromV = Math.min(fromV, doc.fromV)
  296. toV = Math.max(toV, doc.toV)
  297. start_ts = Math.min(start_ts, update.meta.start_ts)
  298. end_ts = Math.max(end_ts, update.meta.end_ts)
  299. } else {
  300. ;({ fromV } = doc)
  301. ;({ toV } = doc)
  302. ;({ start_ts } = update.meta)
  303. ;({ end_ts } = update.meta)
  304. }
  305. break
  306. }
  307. }
  308. }
  309. return { fromV, toV, start_ts, end_ts }
  310. }
  311. // Set the track changes selected doc to one of the docs in the range
  312. // of currently selected updates. If we already have a selected doc
  313. // then prefer this one if present.
  314. _selectDocFromUpdates() {
  315. let doc, doc_id
  316. const affected_docs = {}
  317. for (let update of Array.from(this.$scope.history.selection.updates)) {
  318. for (doc_id in update.docs) {
  319. doc = update.docs[doc_id]
  320. affected_docs[doc_id] = doc.entity
  321. }
  322. }
  323. let selected_doc = this.$scope.history.selection.doc
  324. if (selected_doc != null && affected_docs[selected_doc.id] != null) {
  325. // Selected doc is already open
  326. } else {
  327. for (doc_id in affected_docs) {
  328. doc = affected_docs[doc_id]
  329. selected_doc = doc
  330. break
  331. }
  332. }
  333. this.$scope.history.selection.doc = selected_doc
  334. return this.ide.fileTreeManager.selectEntity(selected_doc)
  335. }
  336. _updateContainsUserId(update, user_id) {
  337. for (let user of Array.from(update.meta.users)) {
  338. if ((user != null ? user.id : undefined) === user_id) {
  339. return true
  340. }
  341. }
  342. return false
  343. }
  344. }
  345. HistoryManager.initClass()
  346. return HistoryManager
  347. })()