diff --git a/webapp/bridge.js b/webapp/bridge.js index 00db2e9..8978935 100644 --- a/webapp/bridge.js +++ b/webapp/bridge.js @@ -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 = { platform: platform, defaultFontFamily: "Hack", @@ -82,17 +102,28 @@ const Heynote = { buffer: { async load(path) { - const content = localStorage.getItem(path) - return content === null ? "\n∞∞∞text-a\n" : content + //console.log("loading", path) + const content = localStorage.getItem(noteKey(path)) + return content === null ? '{"formatVersion":"1.0.0","name":"Scratch"}\n∞∞∞text-a\n' : content }, async save(path, content) { - console.log("saving", path, content) - localStorage.setItem(path, content) + //console.log("saving", path, content) + localStorage.setItem(noteKey(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) { @@ -100,15 +131,36 @@ const Heynote = { }, async exists(path) { - return true + return localStorage.getItem(noteKey(path)) !== null }, 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() { - 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) { @@ -147,7 +199,7 @@ const Heynote = { set: (mode) => { localStorage.setItem("theme", mode) themeCallback(mode) - console.log("set theme to", mode) + //console.log("set theme to", mode) }, get: async () => { const theme = localStorage.getItem("theme") || "system"