Add support for mutiple notes to web app

This commit is contained in:
Jonatan Heyman 2024-10-29 16:47:55 +01:00
parent 74e4c6347b
commit 2c31f0170a

View File

@ -75,6 +75,26 @@ if (settingsData !== null) {
} }
const NOTE_KEY_PREFIX = "heynote-library__"
function noteKey(path) {
return NOTE_KEY_PREFIX + path
}
function getNoteMetadata(content) {
const firstSeparator = content.indexOf("\n∞∞∞")
if (firstSeparator === -1) {
return null
}
try {
const metadata = JSON.parse(content.slice(0, firstSeparator).trim())
return {"name": metadata.name}
} catch (e) {
return {}
}
}
const Heynote = { const Heynote = {
platform: platform, platform: platform,
defaultFontFamily: "Hack", defaultFontFamily: "Hack",
@ -82,17 +102,28 @@ const Heynote = {
buffer: { buffer: {
async load(path) { async load(path) {
const content = localStorage.getItem(path) //console.log("loading", path)
return content === null ? "\n∞∞∞text-a\n" : content const content = localStorage.getItem(noteKey(path))
return content === null ? '{"formatVersion":"1.0.0","name":"Scratch"}\n∞∞∞text-a\n' : content
}, },
async save(path, content) { async save(path, content) {
console.log("saving", path, content) //console.log("saving", path, content)
localStorage.setItem(path, content) localStorage.setItem(noteKey(path), content)
}, },
async create(path, content) { async create(path, content) {
throw Exception("Not implemented") localStorage.setItem(noteKey(path), content)
},
async delete(path) {
localStorage.removeItem(noteKey(path))
},
async move(path, newPath) {
const content = localStorage.getItem(noteKey(path))
localStorage.setItem(noteKey(newPath), content)
localStorage.removeItem(noteKey(path))
}, },
async saveAndQuit(contents) { async saveAndQuit(contents) {
@ -100,15 +131,36 @@ const Heynote = {
}, },
async exists(path) { async exists(path) {
return true return localStorage.getItem(noteKey(path)) !== null
}, },
async getList(path) { async getList(path) {
return [{"path":"buffer.txt", "metadata":{}}] //return {"scratch.txt": {name:"Scratch"}}
const notes = {}
for (let [key, content] of Object.entries(localStorage)) {
if (key.startsWith(NOTE_KEY_PREFIX)) {
const path = key.slice(NOTE_KEY_PREFIX.length)
notes[path] = getNoteMetadata(content)
}
}
return notes
}, },
async getDirectoryList() { async getDirectoryList() {
return [] const directories = new Set()
for (let key in localStorage) {
if (key.startsWith(NOTE_KEY_PREFIX)) {
const path = key.slice(NOTE_KEY_PREFIX.length)
const parts = path.split("/")
if (parts.length > 1) {
for (let i = 1; i < parts.length; i++) {
directories.add(parts.slice(0, i).join("/"))
}
}
}
}
//console.log("directories", directories)
return [...directories]
}, },
async close(path) { async close(path) {
@ -147,7 +199,7 @@ const Heynote = {
set: (mode) => { set: (mode) => {
localStorage.setItem("theme", mode) localStorage.setItem("theme", mode)
themeCallback(mode) themeCallback(mode)
console.log("set theme to", mode) //console.log("set theme to", mode)
}, },
get: async () => { get: async () => {
const theme = localStorage.getItem("theme") || "system" const theme = localStorage.getItem("theme") || "system"