-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP (remove this commit!): multi notes support
- Loading branch information
Showing
20 changed files
with
976 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import fs from "fs" | ||
import os from "node:os" | ||
import { join, dirname, basename } from "path" | ||
|
||
import * as jetpack from "fs-jetpack"; | ||
import { app, ipcMain, dialog } from "electron" | ||
|
||
|
||
const untildify = (pathWithTilde) => { | ||
const homeDir = os.homedir() | ||
return homeDir ? pathWithTilde.replace(/^~(?=$|\/|\\)/, homeDir) : pathWithTilde | ||
} | ||
|
||
|
||
export class FileLibrary { | ||
constructor(basePath) { | ||
this.basePath = fs.realpathSync(untildify(basePath)) | ||
this.jetpack = jetpack.cwd(this.basePath) | ||
this.files = {}; | ||
this.watcher = null; | ||
this.contentSaved = false | ||
this.onChangeCallback = null | ||
|
||
if (jetpack.exists(this.basePath) !== "dir") { | ||
throw new Error(`Invalid base path: ${this.basePath}`) | ||
} | ||
|
||
this.setupWatcher() | ||
} | ||
|
||
async exists(path) { | ||
return this.jetpack.exists(path) === "file" | ||
} | ||
|
||
async load(path) { | ||
if (this.files[path]) { | ||
return this.files[path].read() | ||
} | ||
const fullPath = fs.realpathSync(join(this.basePath, path)) | ||
this.files[path] = new Buffer({fullPath, library:this}) | ||
return await this.files[path].read() | ||
} | ||
|
||
async save(path, content) { | ||
if (!this.files[path]) { | ||
throw new Error(`File not loaded: ${path}`) | ||
} | ||
return await this.files[path].save(content) | ||
} | ||
|
||
async listFiles() { | ||
return await this.jetpack.findAsync(this.basePath, { | ||
matching: "*.txt", | ||
recursive: true, | ||
}) | ||
} | ||
|
||
setupWatcher() { | ||
if (!this.watcher) { | ||
this.watcher = fs.watch( | ||
this.basePath, | ||
{ | ||
persistent: true, | ||
recursive: true, | ||
encoding: "utf8", | ||
}, | ||
async (eventType, changedPath) => { | ||
console.log("File changed", eventType, changedPath) | ||
for (const [path, buffer] of Object.entries(this.files)) { | ||
if (changedPath === basename(path)) { | ||
const content = await buffer.read() | ||
if (buffer._lastSavedContent !== content) { | ||
this.onChangeCallback(path, content) | ||
} | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} | ||
|
||
closeFile(path) { | ||
if (this.files[path]) { | ||
delete this.files[path] | ||
} | ||
} | ||
|
||
close() { | ||
for (const buffer of Object.values(this.files)) { | ||
this.closeFile(buffer.filePath) | ||
} | ||
this.stopWatcher() | ||
} | ||
|
||
stopWatcher() { | ||
if (this.watcher) { | ||
this.watcher.close() | ||
this.watcher = null | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
export class Buffer { | ||
constructor({fullPath, library}) { | ||
this.fullPath = fullPath | ||
this._lastSavedContent = null | ||
this.library = library | ||
} | ||
|
||
async read() { | ||
return await this.library.jetpack.read(this.fullPath, 'utf8') | ||
} | ||
|
||
async save(content) { | ||
this._lastSavedContent = content | ||
const saveResult = await this.library.jetpack.write(this.fullPath, content, { | ||
atomic: true, | ||
mode: '600', | ||
}) | ||
return saveResult | ||
} | ||
|
||
exists() { | ||
return jetpack.exists(this.fullPath) === "file" | ||
} | ||
} | ||
|
||
|
||
export function setupFileLibraryEventHandlers(library, win) { | ||
ipcMain.handle('buffer:load', async (event, path) => { | ||
console.log("buffer:load", path) | ||
return await library.load(path) | ||
}); | ||
|
||
|
||
ipcMain.handle('buffer:save', async (event, path, content) => { | ||
return await library.save(path, content) | ||
}); | ||
|
||
ipcMain.handle('buffer:listFiles', async (event) => { | ||
return await library.listFiles() | ||
}); | ||
|
||
ipcMain.handle('buffer:exists', async (event, path) => { | ||
return await library.exists(path) | ||
}); | ||
|
||
ipcMain.handle('buffer:close', async (event, path) => { | ||
return await library.closeFile(path) | ||
}); | ||
|
||
ipcMain.handle('buffer:saveAndQuit', async (event, contents) => { | ||
library.stopWatcher() | ||
for (const [path, content] of contents) { | ||
await library.save(path, content) | ||
} | ||
library.contentSaved = true | ||
app.quit() | ||
}) | ||
|
||
library.onChangeCallback = (path, content) => { | ||
win.webContents.send("buffer:change", path, content) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.