mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-24 21:12:38 -04:00
64ffd66fa4
[web] notify the editor after restoring a deleted doc GitOrigin-RevId: 4bd316958827c83a56a82f9ff72f5ff803d32631
45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
const { callbackify } = require('util')
|
|
const Path = require('path')
|
|
const ProjectEntityHandler = require('./ProjectEntityHandler')
|
|
const EditorController = require('../Editor/EditorController')
|
|
|
|
// generate a new name based on the original, with an optional label.
|
|
// e.g. origname-20210101-122345.tex (default)
|
|
// origname-restored-20210101-122345.tex (label="restored")
|
|
function generateRestoredName(docName, label) {
|
|
const formattedTimestamp = new Date()
|
|
.toISOString()
|
|
.replace('T', '-')
|
|
.replace(/[^0-9-]/g, '')
|
|
const extension = Path.extname(docName)
|
|
const basename =
|
|
Path.basename(docName, extension) + (label ? `-${label}` : '')
|
|
return `${basename}-${formattedTimestamp}${extension}`
|
|
}
|
|
|
|
async function restoreDeletedDoc(projectId, docId, docName, userId) {
|
|
const deletedDoc = await ProjectEntityHandler.promises.getDoc(
|
|
projectId,
|
|
docId,
|
|
{ include_deleted: true }
|
|
)
|
|
const deletedDocName = generateRestoredName(docName)
|
|
// Create the doc and emit a websocket message.
|
|
return await EditorController.promises.addDocWithRanges(
|
|
projectId,
|
|
null,
|
|
`${deletedDocName}`,
|
|
deletedDoc.lines,
|
|
deletedDoc.ranges,
|
|
null,
|
|
userId
|
|
)
|
|
}
|
|
|
|
module.exports = {
|
|
restoreDeletedDoc: callbackify(restoreDeletedDoc),
|
|
generateRestoredName,
|
|
promises: {
|
|
restoreDeletedDoc,
|
|
},
|
|
}
|