Skip to content

Commit

Permalink
Add support for mutiple notes to web app
Browse files Browse the repository at this point in the history
  • Loading branch information
heyman committed Oct 29, 2024
1 parent 3b33043 commit 61ba9b6
Showing 1 changed file with 61 additions and 9 deletions.
70 changes: 61 additions & 9 deletions webapp/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,40 +75,92 @@ 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",
defaultFontSize: isMobileDevice ? 16 : 12,

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) {

},

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) {
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 61ba9b6

Please sign in to comment.