Skip to content

Commit

Permalink
Enhance notes with a me folder
Browse files Browse the repository at this point in the history
  • Loading branch information
javalikescript committed Dec 21, 2024
1 parent 86bdfb0 commit 5390cdc
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 37 deletions.
2 changes: 1 addition & 1 deletion extensions/web-notes/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "Web Notes",
"description": "Expose web notes add-on",
"dependencies": ["web-base"],
"version": "1.0",
"version": "1.1",
"script": "web-notes.lua"
}
67 changes: 39 additions & 28 deletions extensions/web-notes/web-notes.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,60 @@
define(['./web-notes.xml', './web-note.xml', './web-draw.xml'], function(notesTemplate, noteTemplate, drawTemplate) {

function readLink(note) {
return fetch('/user-notes/' + note.name).then(assertIsOk).then(function(response) {
return response.text();
}).then(function(content) {
note.url = content;
});
}
var notesPath = '/user-notes/';

var notesVue = new Vue({
template: notesTemplate,
data: {
notes: []
notes: [],
path: ''
},
methods: {
onShow: function() {
onShow: function(path) {
if (!path) {
path = '';
}
this.notes = [];
this.path = path;
if (path === '' && app.user && app.user.logged) {
this.notes.push({name: 'me', type: 'dir'});
}
var self = this;
self.notes = [];
return fetch('/user-notes/', {
return fetch(notesPath + path, {
headers: {
"Accept": 'application/json'
}
}).then(rejectIfNotOk).then(function(response) {
return response.json();
}).then(function(response) {
}).then(rejectIfNotOk).then(getResponseJson).then(function(response) {
if (isArrayWithItems(response)) {
self.notes = response.filter(function(note) {
var notes = response.filter(function(note) {
return !note.isDir;
}).map(function(note) {
if (endsWith(note.name, '.txt')) {
if (note.isDir) {
note.type = 'dir';
} else if (endsWith(note.name, '.txt')) {
note.type = 'text';
} else if (endsWith(note.name, '.png')) {
note.type = 'draw';
} else if (endsWith(note.name, '.lnk')) {
note.type = 'link';
readLink(note);
fetch(path + note.name).then(getResponseText).then(function(content) {
note.url = content;
});
}
return note;
});
self.notes = self.notes.concat(notes);
}
});
},
openNote: function(note) {
if (note.type === 'text') {
app.toPage('note', note.name);
var path = this.path + note.name;
console.info('openning note "' + path + '"');
if (note.type === 'dir') {
app.toPage('notes', path + '/');
} else if (note.type === 'text') {
app.toPage('note', path);
} else if (note.type === 'draw') {
app.toPage('draw', note.name);
app.toPage('draw', path);
} else if ((note.type === 'link') && note.url) {
open(note.url, '_blank');
}
Expand All @@ -56,40 +65,42 @@ define(['./web-notes.xml', './web-note.xml', './web-draw.xml'], function(notesTe
var noteVue = new Vue({
template: noteTemplate,
data: {
path: '',
name: '',
newName: false,
text: ''
},
methods: {
onShow: function(name) {
this.name = name;
onShow: function(path) {
this.path = path;
this.name = basename(path);
this.newName = false;
this.text = '';
var self = this;
return fetch('/user-notes/' + this.name).then(rejectIfNotOk).then(function(response) {
return response.text();
}).then(function(text) {
return fetch(notesPath + this.path).then(rejectIfNotOk).then(getResponseText).then(function(text) {
self.text = text;
});
},
onRename: function () {
var self = this;
this.onDelete().then(function() {
self.name = self.newName + '.txt';
var dir = basename(self.path, true);
self.path = dir ? dir + '/' + self.name : self.name;
return self.onSave();
}).then(function() {
self.newName = false;
});
},
onDelete: function () {
return fetch('/user-notes/' + this.name, {
return fetch(notesPath + this.path, {
method: 'DELETE'
}).then(assertIsOk).then(function() {
toaster.toast('Note deleted');
});
},
onSave: function () {
return fetch('/user-notes/' + this.name, {
return fetch(notesPath + this.path, {
method: 'PUT',
body: this.text
}).then(assertIsOk).then(function() {
Expand Down Expand Up @@ -191,7 +202,7 @@ define(['./web-notes.xml', './web-note.xml', './web-draw.xml'], function(notesTe
}
});

addPageComponent(notesVue, 'fa-sticky-note');
addPageComponent(notesVue, 'sticky-note');
addPageComponent(noteVue);
addPageComponent(drawVue);

Expand Down
25 changes: 18 additions & 7 deletions extensions/web-notes/web-notes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ webBaseAddons.registerAddonExtension(extension)

local function checkDir(dir)
if not dir:isDirectory() then
if not dir:mkdir() then
if dir:mkdir() then
logger:info('Created directory "%s"', dir)
else
logger:warn('Unable to create the directory "%s"', dir)
end
end
Expand All @@ -20,16 +22,25 @@ end

extension:subscribeEvent('startup', function()
local engine = extension:getEngine()
local notesDir = File:new(engine:getWorkDirectory(), 'notes')
local handler = FileHttpHandler:new(checkDir(notesDir), 'rwl')
local notesDir = checkDir(File:new(engine:getWorkDirectory(), 'notes'))
local sharedNotesDir = checkDir(File:new(notesDir, '_shared'))
local handler = FileHttpHandler:new(notesDir, 'rwl')
local lastUserName
function handler:findFile(exchange, path)
local session = exchange:getSession()
local userDir = self.rootFile
local userDir = sharedNotesDir
if session and session.attributes.user then
local dirName = Url.encodePercent(session.attributes.user.name)
userDir = checkDir(File:new(userDir, dirName))
local userName = session.attributes.user.name
local dirName = Url.encodePercent(userName)
userDir = File:new(self.rootFile, dirName)
if userName ~= lastUserName then
lastUserName = userName
checkDir(userDir)
end
end
logger:finer('file is "%s" / "%s"', userDir, path)
return File:new(userDir, path)
end
extension:addContext('/user%-notes/(.*)', handler)
extension:addContext('/user%-notes/(.*)', FileHttpHandler:new(sharedNotesDir, 'rwl'))
extension:addContext('/user%-notes/me/(.*)', handler)
end)
6 changes: 5 additions & 1 deletion extensions/web-notes/web-notes.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<app-page id="notes" title="Notes">
<template slot="bar-right">
<button v-on:click="app.toPage('draw')" title="Open draw"><i class="fas fa-paint-brush"></i></button>
<button v-on:click="app.toPage('note', 'New note.txt')" title="Create new note"><i class="fa fa-plus"></i></button>
<button v-on:click="app.toPage('note', path + 'New note.txt')" title="Create new note"><i class="fa fa-plus"></i></button>
<button v-on:click="onShow()" title="Refresh"><i class="fas fa-sync"></i></button>
</template>
<article class="tiles">
<div class="tile" v-on:click="openNote(note)" v-for="note in notes">
<p>{{ note.name }}</p>
<p class="tile-value">
<i v-if="note.type === 'dir'" class="fas fa-folder"></i>
<i v-else class="fas fa-file"></i>
</p>
</div>
</article>
</app-page>

0 comments on commit 5390cdc

Please sign in to comment.