Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 72 additions & 39 deletions main/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { v4 as uuid } from 'uuid'
import path from 'path'
import { toJSON } from 'usfm-js'
import { markRepeatedWords } from '@texttree/translation-words-helpers'
const fs = require('fs')
const fs = require('fs-extra')

import i18next from '../next-i18next.config.js'
import { localeStore } from './helpers/user-store'

Expand Down Expand Up @@ -110,7 +111,9 @@ const getNotesWithType = (type) => {
let dictionaryLS

ipcMain.on('set-project-folder', (event, id) => {
personalNotesLS = new Store({ name: 'personal-notes', cwd: `projects/${id}` })
const appDataPath = app.getPath('userData')

personalNotesLS = new Store({ name: 'personal-notes', cwd: appDataPath })
dictionaryLS = new Store({ name: 'dictionary', cwd: `projects/${id}` })
teamNotesLS = new Store({ name: 'team-notes', cwd: `projects/${id}` })
})
Expand Down Expand Up @@ -193,11 +196,13 @@ ipcMain.on('import-note', (event, projectid, note, type) => {
is_folder,
parent_id,
}
fs.writeFileSync(
path.join(projectUrl, projectid, type, id + '.json'),
JSON.stringify(data, null, 2),
{ encoding: 'utf-8' }
)

const noteTypePath = path.join(app.getPath('userData'), type)
fs.mkdirSync(noteTypePath, { recursive: true })

fs.writeFileSync(path.join(noteTypePath, id + '.json'), JSON.stringify(data, null, 2), {
encoding: 'utf-8',
})
event.returnValue = id
event.sender.send('notify', 'Updated')
})
Expand Down Expand Up @@ -285,14 +290,15 @@ ipcMain.on('get-notes', (event, type) => {
ipcMain.on('get-notes-with-data', (event, projectid, type) => {
let notes = []
const notesLS = getNotesWithType(type)
const noteTypePath = path.join(app.getPath('userData'), type)
fs.mkdirSync(noteTypePath, { recursive: true })

for (const noteId in notesLS.store) {
if (Object.hasOwnProperty.call(notesLS.store, noteId)) {
const note = notesLS.store[noteId]
const newNote = fs.readFileSync(
path.join(projectUrl, projectid, type, note.id + '.json'),
{ encoding: 'utf-8' }
)
const newNote = fs.readFileSync(path.join(noteTypePath, note.id + '.json'), {
encoding: 'utf-8',
})
const { data } = JSON.parse(newNote)
notes.push({ ...note, data })
}
Expand Down Expand Up @@ -322,54 +328,65 @@ ipcMain.on('add-note', (event, projectid, noteid, isfolder, sorting, type) => {
version: '2.29.1',
},
}
fs.writeFileSync(
path.join(projectUrl, projectid, type, noteid + '.json'),
JSON.stringify(data, null, 2),
{ encoding: 'utf-8' }
)

const noteTypePath = path.join(app.getPath('userData'), type)
fs.mkdirSync(noteTypePath, { recursive: true })
const filePath = path.join(noteTypePath, noteid + '.json')

fs.writeFileSync(filePath, JSON.stringify(data, null, 2), { encoding: 'utf-8' })

event.returnValue = noteid
event.sender.send('notify', 'Updated')
})

ipcMain.on('update-note', (event, projectid, note, type) => {
const noteTypePath = path.join(app.getPath('userData'), type)
fs.mkdirSync(noteTypePath, { recursive: true })
const filePath = path.join(noteTypePath, note.id + '.json')

const notesLS = getNotesWithType(type)
notesLS.set(`${note.id}.title`, note.title)
fs.writeFileSync(
path.join(projectUrl, projectid, type, note.id + '.json'),
JSON.stringify(note, null, 2),
{ encoding: 'utf-8' }
)
fs.writeFileSync(filePath, JSON.stringify(note, null, 2), { encoding: 'utf-8' })
event.returnValue = note.id
event.sender.send('notify', 'Updated')
})

ipcMain.on('rename-note', (event, projectid, title, noteid, type) => {
const noteTypePath = path.join(app.getPath('userData'), type)
fs.mkdirSync(noteTypePath, { recursive: true })
const filePath = path.join(noteTypePath, noteid + '.json')

const notesLS = getNotesWithType(type)

notesLS.set(`${noteid}.title`, title)
const data = fs.readFileSync(path.join(projectUrl, projectid, type, noteid + '.json'), {
const data = fs.readFileSync(filePath, {
encoding: 'utf-8',
})
const newNote = JSON.parse(data)

fs.writeFileSync(
path.join(projectUrl, projectid, type, noteid + '.json'),
JSON.stringify({ ...newNote, title }, null, 2),
{ encoding: 'utf-8' }
)
fs.writeFileSync(filePath, JSON.stringify({ ...newNote, title }, null, 2), {
encoding: 'utf-8',
})
event.returnValue = noteid
event.sender.send('notify', 'Updated')
})

ipcMain.on('get-note', (event, projectid, noteid, type) => {
const data = fs.readFileSync(path.join(projectUrl, projectid, type, noteid + '.json'), {
const noteTypePath = path.join(app.getPath('userData'), type)
fs.mkdirSync(noteTypePath, { recursive: true })
const filePath = path.join(noteTypePath, noteid + '.json')

const data = fs.readFileSync(filePath, {
encoding: 'utf-8',
})
event.returnValue = data
event.sender.send('notify', 'Loaded')
})

ipcMain.on('remove-note', (event, projectid, noteid, type) => {
const noteTypePath = path.join(app.getPath('userData'), type)
fs.mkdirSync(noteTypePath, { recursive: true })

const notesLS = getNotesWithType(type)
let notes = { ...notesLS.store }

Expand Down Expand Up @@ -401,7 +418,7 @@ ipcMain.on('remove-note', (event, projectid, noteid, type) => {
notesToDelete.forEach((id) => {
delete notes[id]

fs.rmSync(path.join(projectUrl, projectid, type, id + '.json'), {
fs.rmSync(path.join(noteTypePath, id + '.json'), {
force: true,
})
})
Expand Down Expand Up @@ -856,6 +873,21 @@ ipcMain.on('add-project', async (event, url) => {
}
}

const movePersonalNotesToCentralStorage = async (sourcePath, projectId) => {
const centralNotesPath = path.join(app.getPath('userData'), 'personal-notes')
fs.mkdirSync(centralNotesPath, { recursive: true })

const files = await fs.promises.readdir(sourcePath)

console.log({ files })

for (const file of files) {
const sourceFilePath = path.join(sourcePath, file)
const destFilePath = path.join(centralNotesPath, `${projectId}_${file}`)
await fs.promises.move(sourceFilePath, destFilePath, { overwrite: true })
}
}

if (url) {
try {
const projects = storeProjects.get('projects') || []
Expand All @@ -869,6 +901,12 @@ ipcMain.on('add-project', async (event, url) => {

validateProjectStructure(tempDir)

const personalNotesPath = path.join(tempDir, 'personal-notes')
if (fs.existsSync(personalNotesPath)) {
await movePersonalNotesToCentralStorage(personalNotesPath, id)
await fs.remove(personalNotesPath)
}

const finalDir = path.join(projectUrl, id)
await fs.promises.rename(tempDir, finalDir)

Expand Down Expand Up @@ -952,7 +990,7 @@ ipcMain.on('update-project-name', (event, projectId, newName) => {
event.sender.send('project-name-updated', projectId, newName)
})

ipcMain.on('delete-project', (event, projectId) => {
ipcMain.on('delete-project', async (event, projectId) => {
const projectsFilePath = path.join(app.getPath('userData'), 'projects.json')
const projectsData = JSON.parse(fs.readFileSync(projectsFilePath, 'utf8'))

Expand All @@ -963,16 +1001,11 @@ ipcMain.on('delete-project', (event, projectId) => {
fs.writeFileSync(projectsFilePath, JSON.stringify(projectsData, null, 2))

const projectDirPath = path.join(projectUrl, projectId)
fs.rm(projectDirPath, { recursive: true }, (err) => {
if (err) {
console.error(`Error when deleting project folder: ${err}`)
event.sender.send('notify', 'Error deleting project')
return
}
event.sender.send('notify', 'Project deleted')
await fs.promises.rm(projectDirPath, { recursive: true })

event.sender.send('projects-updated', projectsData.projects)
})
event.sender.send('notify', 'Project deleted')

event.sender.send('projects-updated', projectsData.projects)
})

ipcMain.handle('dialog:openFile', handleFileOpen)
Expand Down