2021-02-26 14:50:46 +13:00
|
|
|
import { remote } from "electron"
|
|
|
|
// @ts-ignore
|
|
|
|
import replicationStream from "pouchdb-replication-stream/dist/pouchdb.replication-stream.min.js"
|
|
|
|
// @ts-ignore
|
|
|
|
import load from "pouchdb-load"
|
|
|
|
import PouchDB from "pouchdb"
|
|
|
|
import fs from "fs"
|
|
|
|
import path from "path"
|
2021-05-15 05:06:58 +12:00
|
|
|
import { extend } from "quasar"
|
2021-02-26 14:50:46 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a brand new project and deleted any present data avaiable right now
|
|
|
|
* @param projectName The name of the new project
|
|
|
|
* @praram vueRouter The vue router object
|
|
|
|
*/
|
2021-03-08 11:07:40 +13:00
|
|
|
export const createNewProject = async (projectName: string, vueRouter: any, quasar: any, vueInstance: any) => {
|
2021-02-26 14:50:46 +13:00
|
|
|
await removeCurrentProject()
|
|
|
|
|
2021-05-02 02:31:33 +12:00
|
|
|
if (!window.FA_dbs) {
|
|
|
|
// @ts-ignore
|
|
|
|
window.FA_dbs = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.FA_dbs["project-data"] = new PouchDB("project-data")
|
2021-06-13 11:24:28 +12:00
|
|
|
const newProject = {
|
|
|
|
_id: "projectSetup",
|
|
|
|
projectName: projectName
|
|
|
|
}
|
2021-05-02 02:31:33 +12:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
window.FA_dbs["project-data"].put(newProject)
|
2021-02-26 14:50:46 +13:00
|
|
|
|
2021-03-05 01:51:11 +13:00
|
|
|
/*eslint-disable */
|
|
|
|
// @ts-ignore
|
|
|
|
vueRouter.push({ path: "/" }).catch((e: {name: string}) => {
|
|
|
|
const errorName : string = e.name
|
|
|
|
if (errorName === "NavigationDuplicated") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log(e)
|
|
|
|
})
|
|
|
|
/* eslint-enable */
|
|
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000))
|
|
|
|
|
2021-02-26 14:50:46 +13:00
|
|
|
/*eslint-disable */
|
|
|
|
// @ts-ignore
|
|
|
|
vueRouter.push({ path: "/project" }).catch((e: {name: string}) => {
|
|
|
|
const errorName : string = e.name
|
|
|
|
if (errorName === "NavigationDuplicated") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log(e)
|
|
|
|
})
|
2021-03-08 11:07:40 +13:00
|
|
|
|
|
|
|
quasar.notify({
|
|
|
|
type: 'positive',
|
|
|
|
message: `New project succesfully created`
|
|
|
|
})
|
|
|
|
vueInstance.SSET_resetDocuments()
|
2021-04-28 03:13:46 +12:00
|
|
|
vueInstance.SSET_resetAllDocuments()
|
2021-02-26 14:50:46 +13:00
|
|
|
/* eslint-enable */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open an file dialog asking the use for location where to export the project
|
|
|
|
* @param projectName The name of the project to export
|
|
|
|
*/
|
2021-05-16 00:23:39 +12:00
|
|
|
export const saveProject = (projectName: string, Loading: any, loadingSetup: any, quasar: any) => {
|
2021-02-26 14:50:46 +13:00
|
|
|
remote.dialog.showOpenDialog({
|
|
|
|
properties: ["openDirectory"]
|
|
|
|
}).then(async (result) => {
|
|
|
|
/*eslint-disable */
|
|
|
|
const folderPath = result.filePaths[0]
|
|
|
|
|
2021-03-08 11:07:40 +13:00
|
|
|
if (!folderPath) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Loading.show(loadingSetup)
|
|
|
|
|
2021-02-26 14:50:46 +13:00
|
|
|
PouchDB.plugin(replicationStream.plugin)
|
|
|
|
// @ts-ignore
|
|
|
|
PouchDB.adapter("writableStream", replicationStream.adapters.writableStream)
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
const allDBS = await indexedDB.databases()
|
|
|
|
|
2021-03-18 10:26:08 +13:00
|
|
|
const DBnames: string[] = allDBS
|
|
|
|
.filter((d: {name: string}) => d.name !== '_pouch_fa-settings')
|
|
|
|
.map((db: {name: string}) => {
|
2021-02-26 14:50:46 +13:00
|
|
|
return db.name.replace("_pouch_", "")
|
|
|
|
})
|
|
|
|
|
|
|
|
for (const db of DBnames) {
|
2021-05-01 02:27:23 +12:00
|
|
|
window.FA_dbs[db] = new PouchDB(db)
|
2021-02-26 14:50:46 +13:00
|
|
|
|
2021-04-26 09:01:41 +12:00
|
|
|
if (!fs.existsSync(`${folderPath}`)) {
|
|
|
|
fs.mkdirSync(`${folderPath}`)
|
|
|
|
}
|
|
|
|
|
2021-02-26 14:50:46 +13:00
|
|
|
if (!fs.existsSync(`${folderPath}/${projectName}`)) {
|
|
|
|
fs.mkdirSync(`${folderPath}/${projectName}`)
|
|
|
|
}
|
|
|
|
const ws = fs.createWriteStream(`${folderPath}/${projectName}/${db}.txt`)
|
|
|
|
|
|
|
|
// @ts-ignore
|
2021-05-01 02:27:23 +12:00
|
|
|
await window.FA_dbs[db].dump(ws)
|
2021-04-06 01:28:33 +12:00
|
|
|
|
2021-02-26 14:50:46 +13:00
|
|
|
}
|
2021-03-08 11:07:40 +13:00
|
|
|
|
|
|
|
Loading.hide()
|
|
|
|
quasar.notify({
|
|
|
|
type: 'positive',
|
2021-05-16 00:23:39 +12:00
|
|
|
message: `Project succesfully saved`
|
2021-03-08 11:07:40 +13:00
|
|
|
})
|
2021-02-26 14:50:46 +13:00
|
|
|
/* eslint-enable */
|
|
|
|
}).catch(err => {
|
|
|
|
console.log(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the current project and all its data
|
|
|
|
*/
|
|
|
|
export const removeCurrentProject = async () => {
|
|
|
|
/*eslint-disable */
|
|
|
|
// @ts-ignore
|
|
|
|
const allDBS = await indexedDB.databases()
|
|
|
|
|
2021-03-18 10:26:08 +13:00
|
|
|
const DBnames: string[] = allDBS
|
|
|
|
.filter((d: {name: string}) => d.name !== '_pouch_fa-settings')
|
|
|
|
.map((db: {name: string}) => {
|
2021-02-26 14:50:46 +13:00
|
|
|
return db.name.replace("_pouch_", "")
|
|
|
|
})
|
|
|
|
|
2021-03-18 10:26:08 +13:00
|
|
|
|
2021-02-26 14:50:46 +13:00
|
|
|
for (const db of DBnames) {
|
2021-05-01 02:27:23 +12:00
|
|
|
window.FA_dbs[db] = new PouchDB(db)
|
|
|
|
await window.FA_dbs[db].destroy()
|
2021-02-26 14:50:46 +13:00
|
|
|
}
|
|
|
|
/* eslint-enable */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a dialog to let user pick whatever project they wish to open and lets them select a directory
|
|
|
|
* @param vueRouter The vue router object
|
|
|
|
*/
|
2021-06-13 08:23:20 +12:00
|
|
|
export const loadExistingProject = (vueRouter: any, Loading: any, loadingSetup: any, quasar: any, vueInstance: any) => {
|
2021-02-26 14:50:46 +13:00
|
|
|
/*eslint-disable */
|
|
|
|
remote.dialog.showOpenDialog({
|
|
|
|
properties: ["openDirectory"]
|
|
|
|
}).then(async (result) => {
|
|
|
|
const folderPath = result.filePaths[0]
|
|
|
|
|
|
|
|
if (!folderPath) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-08 11:07:40 +13:00
|
|
|
Loading.show(loadingSetup)
|
|
|
|
|
2021-02-26 14:50:46 +13:00
|
|
|
await removeCurrentProject()
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
PouchDB.plugin({
|
|
|
|
loadIt: load.load
|
|
|
|
})
|
|
|
|
|
|
|
|
const allFiles = fs.readdirSync(folderPath)
|
|
|
|
|
|
|
|
for (const file of allFiles) {
|
|
|
|
const currentDBName = path.parse(file).name
|
2021-05-01 02:27:23 +12:00
|
|
|
window.FA_dbs[currentDBName] = new PouchDB(currentDBName)
|
2021-02-26 14:50:46 +13:00
|
|
|
|
|
|
|
const fileContents = fs.readFileSync(`${folderPath}/${file}`, { encoding: "utf8" })
|
|
|
|
// @ts-ignore
|
2021-05-01 02:27:23 +12:00
|
|
|
await window.FA_dbs[currentDBName].loadIt(fileContents)
|
2021-04-06 01:28:33 +12:00
|
|
|
|
2021-02-26 14:50:46 +13:00
|
|
|
}
|
|
|
|
|
2021-05-15 05:06:58 +12:00
|
|
|
const optionsSnapShot = extend(true, {}, vueInstance.SGET_options)
|
|
|
|
// @ts-ignore
|
|
|
|
optionsSnapShot.legacyFieldsCheck = true
|
|
|
|
// @ts-ignore
|
2021-05-15 11:35:18 +12:00
|
|
|
optionsSnapShot.pre017check = true
|
|
|
|
// @ts-ignore
|
2021-05-15 05:06:58 +12:00
|
|
|
vueInstance.SSET_options(optionsSnapShot)
|
|
|
|
|
2021-03-04 13:27:07 +13:00
|
|
|
/*eslint-disable */
|
|
|
|
// @ts-ignore
|
|
|
|
vueRouter.push({ path: "/" }).catch((e: {name: string}) => {
|
2021-04-20 05:51:44 +12:00
|
|
|
const errorName : string = e.name
|
|
|
|
if (errorName === "NavigationDuplicated") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log(e)
|
|
|
|
})
|
|
|
|
/* eslint-enable */
|
|
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000))
|
|
|
|
|
|
|
|
/*eslint-disable */
|
|
|
|
// @ts-ignore
|
|
|
|
vueRouter.push({ path: "/project" }).catch((e: {name: string}) => {
|
|
|
|
const errorName : string = e.name
|
|
|
|
if (errorName === "NavigationDuplicated") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log(e)
|
|
|
|
})
|
|
|
|
|
|
|
|
quasar.notify({
|
|
|
|
type: 'positive',
|
2021-05-16 00:23:39 +12:00
|
|
|
message: `Project succesfully loaded`
|
2021-04-20 05:51:44 +12:00
|
|
|
})
|
|
|
|
|
|
|
|
vueInstance.SSET_resetDocuments()
|
2021-04-26 09:01:41 +12:00
|
|
|
vueInstance.SSET_resetAllDocuments()
|
2021-04-20 05:51:44 +12:00
|
|
|
/* eslint-enable */
|
|
|
|
}).catch(err => {
|
|
|
|
console.log(err)
|
|
|
|
})
|
|
|
|
/* eslint-enable */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a dialog to let user pick whatever project they wish to open and lets them select a directory
|
|
|
|
* @param vueRouter The vue router object
|
|
|
|
*/
|
|
|
|
export const mergeExistingProject = (vueRouter: any, Loading: any, loadingSetup: any, quasar: any, vueInstance: any) => {
|
|
|
|
/*eslint-disable */
|
|
|
|
remote.dialog.showOpenDialog({
|
|
|
|
properties: ["openDirectory"]
|
|
|
|
}).then(async (result) => {
|
|
|
|
const folderPath = result.filePaths[0]
|
|
|
|
|
|
|
|
if (!folderPath) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Loading.show(loadingSetup)
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
PouchDB.plugin({
|
|
|
|
loadIt: load.load
|
|
|
|
})
|
|
|
|
|
|
|
|
let allFiles = fs.readdirSync(folderPath)
|
|
|
|
allFiles = allFiles.filter(file => file !== 'project-data.txt')
|
|
|
|
|
|
|
|
for (const file of allFiles) {
|
|
|
|
const currentDBName = path.parse(file).name
|
2021-05-01 02:27:23 +12:00
|
|
|
window.FA_dbs[currentDBName] = new PouchDB(currentDBName)
|
2021-04-20 05:51:44 +12:00
|
|
|
|
|
|
|
const fileContents = fs.readFileSync(`${folderPath}/${file}`, { encoding: "utf8" })
|
|
|
|
// @ts-ignore
|
2021-05-01 02:27:23 +12:00
|
|
|
await window.FA_dbs[currentDBName].loadIt(fileContents)
|
2021-04-20 05:51:44 +12:00
|
|
|
}
|
|
|
|
|
2021-05-15 05:06:58 +12:00
|
|
|
|
|
|
|
const optionsSnapShot = extend(true, {}, vueInstance.SGET_options)
|
|
|
|
// @ts-ignore
|
|
|
|
optionsSnapShot.legacyFieldsCheck = true
|
|
|
|
// @ts-ignore
|
2021-05-15 11:35:18 +12:00
|
|
|
optionsSnapShot.pre017check = true
|
|
|
|
// @ts-ignore
|
2021-05-15 05:06:58 +12:00
|
|
|
vueInstance.SSET_options(optionsSnapShot)
|
|
|
|
|
2021-04-20 05:51:44 +12:00
|
|
|
/*eslint-disable */
|
|
|
|
// @ts-ignore
|
|
|
|
vueRouter.push({ path: "/" }).catch((e: {name: string}) => {
|
2021-03-04 13:27:07 +13:00
|
|
|
const errorName : string = e.name
|
|
|
|
if (errorName === "NavigationDuplicated") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log(e)
|
|
|
|
})
|
|
|
|
/* eslint-enable */
|
|
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000))
|
|
|
|
|
2021-02-26 14:50:46 +13:00
|
|
|
/*eslint-disable */
|
|
|
|
// @ts-ignore
|
|
|
|
vueRouter.push({ path: "/project" }).catch((e: {name: string}) => {
|
|
|
|
const errorName : string = e.name
|
|
|
|
if (errorName === "NavigationDuplicated") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log(e)
|
|
|
|
})
|
2021-03-08 11:07:40 +13:00
|
|
|
|
|
|
|
quasar.notify({
|
|
|
|
type: 'positive',
|
2021-04-20 23:58:13 +12:00
|
|
|
message: `Data successfully merged into the project`
|
2021-03-08 11:07:40 +13:00
|
|
|
})
|
|
|
|
|
|
|
|
vueInstance.SSET_resetDocuments()
|
2021-04-26 09:01:41 +12:00
|
|
|
vueInstance.SSET_resetAllDocuments()
|
|
|
|
|
2021-02-26 14:50:46 +13:00
|
|
|
/* eslint-enable */
|
|
|
|
}).catch(err => {
|
|
|
|
console.log(err)
|
|
|
|
})
|
|
|
|
/* eslint-enable */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves current project name
|
|
|
|
*/
|
|
|
|
export const retrieveCurrentProjectName = async () => {
|
2021-05-02 02:31:33 +12:00
|
|
|
if (!window.FA_dbs) {
|
|
|
|
// @ts-ignore
|
|
|
|
window.FA_dbs = {}
|
|
|
|
}
|
|
|
|
window.FA_dbs["project-data"] = new PouchDB("project-data")
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
const projectData = await window.FA_dbs["project-data"].allDocs({ include_docs: true })
|
2021-04-06 01:28:33 +12:00
|
|
|
|
2021-05-02 02:31:33 +12:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
2021-06-13 11:24:28 +12:00
|
|
|
|
|
|
|
const projectName: string = (projectData?.rows[0]?.doc?.projectName) || projectData?.rows[0]?.doc?._id
|
|
|
|
|
|
|
|
return (projectName) || ""
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change current project name
|
|
|
|
*/
|
|
|
|
export const changeCurrentProjectSettings = async (input: {projectName: string}) => {
|
|
|
|
if (!window.FA_dbs) {
|
|
|
|
// @ts-ignore
|
|
|
|
window.FA_dbs = {}
|
|
|
|
}
|
|
|
|
window.FA_dbs["project-data"] = new PouchDB("project-data")
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
const projectData = await window.FA_dbs["project-data"].allDocs({ include_docs: true })
|
|
|
|
|
|
|
|
projectData.rows[0].doc.projectName = input.projectName
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
await window.FA_dbs["project-data"].put(projectData.rows[0].doc)
|
2021-02-26 14:50:46 +13:00
|
|
|
}
|
2021-05-02 02:31:33 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save corkboard update
|
|
|
|
*/
|
|
|
|
export const saveCorkboard = async (input: string) => {
|
|
|
|
if (!window.FA_dbs) {
|
|
|
|
// @ts-ignore
|
|
|
|
window.FA_dbs = {}
|
|
|
|
}
|
|
|
|
window.FA_dbs["project-data"] = new PouchDB("project-data")
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
const projectData = await window.FA_dbs["project-data"].allDocs({ include_docs: true })
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
|
|
projectData.rows[0].doc.corkboardText = input.trim()
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
window.FA_dbs["project-data"].put(projectData.rows[0].doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve corkboard
|
|
|
|
*/
|
|
|
|
export const retrieveCorkboard = async (): Promise<string> => {
|
|
|
|
if (!window.FA_dbs) {
|
|
|
|
// @ts-ignore
|
|
|
|
window.FA_dbs = {}
|
|
|
|
}
|
|
|
|
window.FA_dbs["project-data"] = new PouchDB("project-data")
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
const projectData = await window.FA_dbs["project-data"].allDocs({ include_docs: true })
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
|
|
return (projectData.rows[0]?.doc.corkboardText) || ""
|
|
|
|
}
|
2021-06-13 08:23:20 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update last opened documents
|
|
|
|
*/
|
|
|
|
export const updateLastOpenedDocuments = async (newDocID: string) => {
|
|
|
|
if (!window.FA_dbs) {
|
|
|
|
// @ts-ignore
|
|
|
|
window.FA_dbs = {}
|
|
|
|
}
|
|
|
|
window.FA_dbs["project-data"] = new PouchDB("project-data")
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
const projectData = await window.FA_dbs["project-data"].allDocs({ include_docs: true })
|
|
|
|
|
|
|
|
if (!projectData.rows[0].doc.lastOpenedDocList) {
|
|
|
|
projectData.rows[0].doc.lastOpenedDocList = []
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
projectData.rows[0].doc.lastOpenedDocList = [...new Set([
|
|
|
|
newDocID,
|
|
|
|
...projectData.rows[0].doc.lastOpenedDocList
|
|
|
|
])].slice(0, 50)
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
window.FA_dbs["project-data"].put(projectData.rows[0].doc).catch(() => console.log())
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve last opened document IDListr
|
|
|
|
*/
|
|
|
|
export const retrieveLastOpenedDocuments = async (): Promise<string[]> => {
|
|
|
|
if (!window.FA_dbs) {
|
|
|
|
// @ts-ignore
|
|
|
|
window.FA_dbs = {}
|
|
|
|
}
|
|
|
|
window.FA_dbs["project-data"] = new PouchDB("project-data")
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
const projectData = await window.FA_dbs["project-data"].allDocs({ include_docs: true })
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
|
|
return (projectData.rows[0]?.doc?.lastOpenedDocList) || []
|
|
|
|
}
|