2018-11-05 05:06:39 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS206: Consider reworking classes to avoid initClass
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-19 05:02:56 -04:00
|
|
|
import moment from 'moment'
|
|
|
|
import ColorManager from '../colors/ColorManager'
|
|
|
|
import displayNameForUser from './util/displayNameForUser'
|
|
|
|
import './controllers/HistoryListController'
|
|
|
|
import './controllers/HistoryDiffController'
|
|
|
|
import './directives/infiniteScroll'
|
|
|
|
let HistoryManager
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
export default HistoryManager = (function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
HistoryManager = class HistoryManager {
|
|
|
|
static initClass() {
|
|
|
|
this.prototype.BATCH_SIZE = 10
|
|
|
|
}
|
2020-12-15 05:23:54 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
constructor(ide, $scope) {
|
|
|
|
this.ide = ide
|
|
|
|
this.$scope = $scope
|
|
|
|
this.reset()
|
|
|
|
|
|
|
|
this.$scope.toggleHistory = () => {
|
|
|
|
if (this.$scope.ui.view === 'history') {
|
|
|
|
return this.hide()
|
|
|
|
} else {
|
|
|
|
return this.show()
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
|
|
|
|
this.$scope.$watch('history.selection.updates', updates => {
|
|
|
|
if (updates != null && updates.length > 0) {
|
|
|
|
this._selectDocFromUpdates()
|
|
|
|
return this.reloadDiff()
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.$scope.$on('entity:selected', (event, entity) => {
|
|
|
|
if (this.$scope.ui.view === 'history' && entity.type === 'doc') {
|
|
|
|
this.$scope.history.selection.doc = entity
|
|
|
|
return this.reloadDiff()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
show() {
|
|
|
|
this.$scope.ui.view = 'history'
|
|
|
|
return this.reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
hide() {
|
|
|
|
this.$scope.ui.view = 'editor'
|
|
|
|
// Make sure we run the 'open' logic for whatever is currently selected
|
|
|
|
return this.$scope.$emit(
|
|
|
|
'entity:selected',
|
|
|
|
this.ide.fileTreeManager.findSelectedEntity()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
return (this.$scope.history = {
|
|
|
|
updates: [],
|
|
|
|
nextBeforeTimestamp: null,
|
|
|
|
atEnd: false,
|
|
|
|
selection: {
|
|
|
|
updates: [],
|
|
|
|
doc: null,
|
|
|
|
range: {
|
|
|
|
fromV: null,
|
|
|
|
toV: null,
|
|
|
|
start_ts: null,
|
2021-04-27 03:52:58 -04:00
|
|
|
end_ts: null,
|
|
|
|
},
|
2020-05-19 05:02:56 -04:00
|
|
|
},
|
2021-04-27 03:52:58 -04:00
|
|
|
diff: null,
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
autoSelectRecentUpdates() {
|
|
|
|
if (this.$scope.history.updates.length === 0) {
|
|
|
|
return
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.$scope.history.updates[0].selectedTo = true
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
let indexOfLastUpdateNotByMe = 0
|
|
|
|
for (let i = 0; i < this.$scope.history.updates.length; i++) {
|
|
|
|
const update = this.$scope.history.updates[i]
|
|
|
|
if (this._updateContainsUserId(update, this.$scope.user.id)) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
indexOfLastUpdateNotByMe = i
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return (this.$scope.history.updates[
|
|
|
|
indexOfLastUpdateNotByMe
|
|
|
|
].selectedFrom = true)
|
|
|
|
}
|
2020-12-15 05:23:54 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
fetchNextBatchOfUpdates() {
|
2020-12-15 05:23:54 -05:00
|
|
|
let url = `/project/${this.ide.project_id}/updates?min_count=${this.BATCH_SIZE}`
|
2020-05-19 05:02:56 -04:00
|
|
|
if (this.$scope.history.nextBeforeTimestamp != null) {
|
|
|
|
url += `&before=${this.$scope.history.nextBeforeTimestamp}`
|
|
|
|
}
|
|
|
|
this.$scope.history.loading = true
|
|
|
|
return this.ide.$http.get(url).then(response => {
|
|
|
|
const { data } = response
|
|
|
|
this._loadUpdates(data.updates)
|
|
|
|
this.$scope.history.nextBeforeTimestamp = data.nextBeforeTimestamp
|
|
|
|
if (data.nextBeforeTimestamp == null) {
|
|
|
|
this.$scope.history.atEnd = true
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return (this.$scope.history.loading = false)
|
|
|
|
})
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
reloadDiff() {
|
|
|
|
let { diff } = this.$scope.history
|
|
|
|
const { updates, doc } = this.$scope.history.selection
|
|
|
|
const {
|
|
|
|
fromV,
|
|
|
|
toV,
|
|
|
|
start_ts,
|
2021-04-27 03:52:58 -04:00
|
|
|
end_ts,
|
2020-05-19 05:02:56 -04:00
|
|
|
} = this._calculateRangeFromSelection()
|
|
|
|
|
|
|
|
if (doc == null) {
|
|
|
|
return
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (
|
|
|
|
diff != null &&
|
|
|
|
diff.doc === doc &&
|
|
|
|
diff.fromV === fromV &&
|
|
|
|
diff.toV === toV
|
|
|
|
) {
|
|
|
|
return
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.$scope.history.diff = diff = {
|
|
|
|
fromV,
|
|
|
|
toV,
|
|
|
|
start_ts,
|
|
|
|
end_ts,
|
|
|
|
doc,
|
|
|
|
error: false,
|
2021-04-27 03:52:58 -04:00
|
|
|
pathname: doc.name,
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
|
|
|
|
if (!doc.deleted) {
|
|
|
|
diff.loading = true
|
|
|
|
let url = `/project/${this.$scope.project_id}/doc/${diff.doc.id}/diff`
|
|
|
|
if (diff.fromV != null && diff.toV != null) {
|
|
|
|
url += `?from=${diff.fromV}&to=${diff.toV}`
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
|
|
|
|
return this.ide.$http
|
|
|
|
.get(url)
|
|
|
|
.then(response => {
|
|
|
|
const { data } = response
|
|
|
|
diff.loading = false
|
|
|
|
const { text, highlights } = this._parseDiff(data)
|
|
|
|
diff.text = text
|
|
|
|
return (diff.highlights = highlights)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
.catch(function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
diff.loading = false
|
|
|
|
return (diff.error = true)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
diff.deleted = true
|
|
|
|
diff.restoreInProgress = false
|
|
|
|
diff.restoreDeletedSuccess = false
|
|
|
|
return (diff.restoredDocNewId = null)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
restoreDeletedDoc(doc) {
|
|
|
|
const url = `/project/${this.$scope.project_id}/doc/${doc.id}/restore`
|
|
|
|
return this.ide.$http.post(url, {
|
|
|
|
name: doc.name,
|
2021-04-27 03:52:58 -04:00
|
|
|
_csrf: window.csrfToken,
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
restoreDiff(diff) {
|
2020-12-15 05:23:54 -05:00
|
|
|
const url = `/project/${this.$scope.project_id}/doc/${diff.doc.id}/version/${diff.fromV}/restore`
|
2020-05-19 05:02:56 -04:00
|
|
|
return this.ide.$http.post(url, { _csrf: window.csrfToken })
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
_parseDiff(diff) {
|
|
|
|
let row = 0
|
|
|
|
let column = 0
|
|
|
|
const highlights = []
|
|
|
|
let text = ''
|
|
|
|
const iterable = diff.diff || []
|
|
|
|
for (let i = 0; i < iterable.length; i++) {
|
2021-10-26 04:08:56 -04:00
|
|
|
let endColumn, endRow
|
2020-05-19 05:02:56 -04:00
|
|
|
const entry = iterable[i]
|
|
|
|
let content = entry.u || entry.i || entry.d
|
|
|
|
if (!content) {
|
|
|
|
content = ''
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
text += content
|
|
|
|
const lines = content.split('\n')
|
|
|
|
const startRow = row
|
|
|
|
const startColumn = column
|
|
|
|
if (lines.length > 1) {
|
|
|
|
endRow = startRow + lines.length - 1
|
|
|
|
endColumn = lines[lines.length - 1].length
|
|
|
|
} else {
|
|
|
|
endRow = startRow
|
|
|
|
endColumn = startColumn + lines[0].length
|
|
|
|
}
|
|
|
|
row = endRow
|
|
|
|
column = endColumn
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const range = {
|
|
|
|
start: {
|
|
|
|
row: startRow,
|
2021-04-27 03:52:58 -04:00
|
|
|
column: startColumn,
|
2020-05-19 05:02:56 -04:00
|
|
|
},
|
|
|
|
end: {
|
|
|
|
row: endRow,
|
2021-04-27 03:52:58 -04:00
|
|
|
column: endColumn,
|
|
|
|
},
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (entry.i != null || entry.d != null) {
|
|
|
|
const name = displayNameForUser(entry.meta.user)
|
|
|
|
const date = moment(entry.meta.end_ts).format('Do MMM YYYY, h:mm a')
|
|
|
|
if (entry.i != null) {
|
|
|
|
highlights.push({
|
|
|
|
label: `Added by ${name} on ${date}`,
|
|
|
|
highlight: range,
|
|
|
|
hue: ColorManager.getHueForUserId(
|
|
|
|
entry.meta.user != null ? entry.meta.user.id : undefined
|
2021-04-27 03:52:58 -04:00
|
|
|
),
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
} else if (entry.d != null) {
|
|
|
|
highlights.push({
|
|
|
|
label: `Deleted by ${name} on ${date}`,
|
|
|
|
strikeThrough: range,
|
|
|
|
hue: ColorManager.getHueForUserId(
|
|
|
|
entry.meta.user != null ? entry.meta.user.id : undefined
|
2021-04-27 03:52:58 -04:00
|
|
|
),
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return { text, highlights }
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
_loadUpdates(updates) {
|
|
|
|
if (updates == null) {
|
|
|
|
updates = []
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
let previousUpdate = this.$scope.history.updates[
|
|
|
|
this.$scope.history.updates.length - 1
|
|
|
|
]
|
|
|
|
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const update of Array.from(updates)) {
|
2020-05-19 05:02:56 -04:00
|
|
|
update.pathnames = [] // Used for display
|
|
|
|
const object = update.docs || {}
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const doc_id in object) {
|
2020-05-19 05:02:56 -04:00
|
|
|
const doc = object[doc_id]
|
|
|
|
doc.entity = this.ide.fileTreeManager.findEntityById(doc_id, {
|
2021-04-27 03:52:58 -04:00
|
|
|
includeDeleted: true,
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
update.pathnames.push(doc.entity.name)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const user of Array.from(update.meta.users || [])) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (user != null) {
|
|
|
|
user.hue = ColorManager.getHueForUserId(user.id)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (
|
|
|
|
previousUpdate == null ||
|
|
|
|
!moment(previousUpdate.meta.end_ts).isSame(update.meta.end_ts, 'day')
|
|
|
|
) {
|
|
|
|
update.meta.first_in_day = true
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
update.selectedFrom = false
|
|
|
|
update.selectedTo = false
|
|
|
|
update.inSelection = false
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
previousUpdate = update
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const firstLoad = this.$scope.history.updates.length === 0
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.$scope.history.updates = this.$scope.history.updates.concat(updates)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (firstLoad) {
|
|
|
|
return this.autoSelectRecentUpdates()
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
_calculateRangeFromSelection() {
|
|
|
|
let end_ts, start_ts, toV
|
|
|
|
let fromV = (toV = start_ts = end_ts = null)
|
|
|
|
|
|
|
|
const selected_doc_id =
|
|
|
|
this.$scope.history.selection.doc != null
|
|
|
|
? this.$scope.history.selection.doc.id
|
|
|
|
: undefined
|
|
|
|
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const update of Array.from(
|
2020-05-19 05:02:56 -04:00
|
|
|
this.$scope.history.selection.updates || []
|
|
|
|
)) {
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const doc_id in update.docs) {
|
2020-05-19 05:02:56 -04:00
|
|
|
const doc = update.docs[doc_id]
|
|
|
|
if (doc_id === selected_doc_id) {
|
|
|
|
if (fromV != null && toV != null) {
|
|
|
|
fromV = Math.min(fromV, doc.fromV)
|
|
|
|
toV = Math.max(toV, doc.toV)
|
|
|
|
start_ts = Math.min(start_ts, update.meta.start_ts)
|
|
|
|
end_ts = Math.max(end_ts, update.meta.end_ts)
|
|
|
|
} else {
|
|
|
|
;({ fromV } = doc)
|
|
|
|
;({ toV } = doc)
|
|
|
|
;({ start_ts } = update.meta)
|
|
|
|
;({ end_ts } = update.meta)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
break
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return { fromV, toV, start_ts, end_ts }
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
// Set the track changes selected doc to one of the docs in the range
|
|
|
|
// of currently selected updates. If we already have a selected doc
|
|
|
|
// then prefer this one if present.
|
|
|
|
_selectDocFromUpdates() {
|
|
|
|
let doc, doc_id
|
|
|
|
const affected_docs = {}
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const update of Array.from(this.$scope.history.selection.updates)) {
|
2020-05-19 05:02:56 -04:00
|
|
|
for (doc_id in update.docs) {
|
|
|
|
doc = update.docs[doc_id]
|
|
|
|
affected_docs[doc_id] = doc.entity
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
let selected_doc = this.$scope.history.selection.doc
|
|
|
|
if (selected_doc != null && affected_docs[selected_doc.id] != null) {
|
|
|
|
// Selected doc is already open
|
|
|
|
} else {
|
2021-05-13 06:21:35 -04:00
|
|
|
const doc_ids = Object.keys(affected_docs)
|
|
|
|
if (doc_ids.length > 0) {
|
|
|
|
const doc_id = doc_ids[0]
|
2020-05-19 05:02:56 -04:00
|
|
|
doc = affected_docs[doc_id]
|
|
|
|
selected_doc = doc
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.$scope.history.selection.doc = selected_doc
|
|
|
|
return this.ide.fileTreeManager.selectEntity(selected_doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateContainsUserId(update, user_id) {
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const user of Array.from(update.meta.users)) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if ((user != null ? user.id : undefined) === user_id) {
|
|
|
|
return true
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return false
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
HistoryManager.initClass()
|
|
|
|
return HistoryManager
|
2020-12-15 05:23:54 -05:00
|
|
|
})()
|