HistoryManager.js 11 KB

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