Compare commits
No commits in common. "12b629756f6229134cca9e4e42f1510a69cc4fc5" and "45cbd4934562184c8250b1a3b392de37da03aa4c" have entirely different histories.
12b629756f
...
45cbd49345
4 changed files with 20 additions and 93 deletions
10
main.go
10
main.go
|
|
@ -132,7 +132,6 @@ func main() { // {{{
|
||||||
http.HandleFunc("/notes2", pageNotes2)
|
http.HandleFunc("/notes2", pageNotes2)
|
||||||
http.HandleFunc("/login", pageLogin)
|
http.HandleFunc("/login", pageLogin)
|
||||||
http.HandleFunc("/sync", pageSync)
|
http.HandleFunc("/sync", pageSync)
|
||||||
http.HandleFunc("/offline", pageOffline)
|
|
||||||
|
|
||||||
http.HandleFunc("/user/authenticate", AuthManager.AuthenticationHandler)
|
http.HandleFunc("/user/authenticate", AuthManager.AuthenticationHandler)
|
||||||
|
|
||||||
|
|
@ -227,15 +226,6 @@ func pageServiceWorker(w http.ResponseWriter, r *http.Request) { // {{{
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} // }}}
|
} // }}}
|
||||||
func pageOffline(w http.ResponseWriter, r *http.Request) { // {{{
|
|
||||||
page := NewPage("offline")
|
|
||||||
|
|
||||||
err := Webengine.Render(page, w, r)
|
|
||||||
if err != nil {
|
|
||||||
w.Write([]byte(err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} // }}}
|
|
||||||
func pageLogin(w http.ResponseWriter, r *http.Request) { // {{{
|
func pageLogin(w http.ResponseWriter, r *http.Request) { // {{{
|
||||||
page := NewPage("login")
|
page := NewPage("login")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,6 @@ export class N2NodeUI extends CustomHTMLElement {
|
||||||
|
|
||||||
_mbus.subscribe('NODE_UI_OPEN', event => {
|
_mbus.subscribe('NODE_UI_OPEN', event => {
|
||||||
this.node = event.detail.data
|
this.node = event.detail.data
|
||||||
this.showMarkdown(true)
|
|
||||||
this.render()
|
this.render()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -69,8 +68,10 @@ export class N2NodeUI extends CustomHTMLElement {
|
||||||
this.elNodeMarkdown.innerHTML = this.marked.parse(this.elNodeContent.value)
|
this.elNodeMarkdown.innerHTML = this.marked.parse(this.elNodeContent.value)
|
||||||
}// }}}
|
}// }}}
|
||||||
takeFocus() {// {{{
|
takeFocus() {// {{{
|
||||||
|
console.log('taking focus', this.showMarkdown())
|
||||||
if (this.showMarkdown()) {
|
if (this.showMarkdown()) {
|
||||||
this.elNodeMarkdown.focus()
|
this.elNodeMarkdown.focus()
|
||||||
|
console.log(this.elNodeMarkdown)
|
||||||
} else
|
} else
|
||||||
this.elNodeContent.focus()
|
this.elNodeContent.focus()
|
||||||
}// }}}
|
}// }}}
|
||||||
|
|
@ -82,12 +83,7 @@ export class N2NodeUI extends CustomHTMLElement {
|
||||||
return this.node?.isModified()
|
return this.node?.isModified()
|
||||||
}// }}}
|
}// }}}
|
||||||
showMarkdown(state) {// {{{
|
showMarkdown(state) {// {{{
|
||||||
// No point in showing markdown if there is no data.
|
switch (state) {
|
||||||
// If there is no data, it will show a blank page regardless, and the user will most
|
|
||||||
// likely want to edit content, which can't be done in markdown.
|
|
||||||
const show = this.node?.content().trim() !== '' && state
|
|
||||||
|
|
||||||
switch (show) {
|
|
||||||
case true:
|
case true:
|
||||||
this.elNodeMarkdown.innerHTML = this.marked.parse(this.elNodeContent.value)
|
this.elNodeMarkdown.innerHTML = this.marked.parse(this.elNodeContent.value)
|
||||||
this.elIconMarkdown.src = `/images/${_VERSION}/icon_markdown.svg`
|
this.elIconMarkdown.src = `/images/${_VERSION}/icon_markdown.svg`
|
||||||
|
|
|
||||||
|
|
@ -2,41 +2,27 @@ const CACHE_NAME = 'notes2-{{ .VERSION }}'
|
||||||
const CACHED_ASSETS = [
|
const CACHED_ASSETS = [
|
||||||
'/',
|
'/',
|
||||||
'/notes2',
|
'/notes2',
|
||||||
'/offline',
|
|
||||||
|
|
||||||
'/css/{{ .VERSION }}/main.css',
|
'/css/{{ .VERSION }}/main.css',
|
||||||
'/css/{{ .VERSION }}/markdown.css',
|
|
||||||
'/css/{{ .VERSION }}/notes2.css',
|
'/css/{{ .VERSION }}/notes2.css',
|
||||||
'/css/{{ .VERSION }}/theme.css',
|
|
||||||
|
|
||||||
'/images/{{ .VERSION }}/collapsed.svg',
|
'/js/{{ .VERSION }}/lib/fullcalendar.min.js',
|
||||||
'/images/{{ .VERSION }}/expanded.svg',
|
'/js/{{ .VERSION }}/lib/node_modules/marked/marked.min.js',
|
||||||
'/images/{{ .VERSION }}/icon_markdown_hollow.svg',
|
'/js/{{ .VERSION }}/lib/sjcl.js',
|
||||||
'/images/{{ .VERSION }}/icon_markdown.svg',
|
|
||||||
'/images/{{ .VERSION }}/icon_refresh.svg',
|
|
||||||
'/images/{{ .VERSION }}/icon_save_disabled.svg',
|
|
||||||
'/images/{{ .VERSION }}/icon_search.svg',
|
|
||||||
'/images/{{ .VERSION }}/leaf.svg',
|
|
||||||
'/images/{{ .VERSION }}/logo.svg',
|
|
||||||
|
|
||||||
'/js/{{ .VERSION }}/api.mjs',
|
'/js/{{ .VERSION }}/api.mjs',
|
||||||
'/js/{{ .VERSION }}/app.mjs',
|
|
||||||
'/js/{{ .VERSION }}/checklist.mjs',
|
|
||||||
'/js/{{ .VERSION }}/crypto.mjs',
|
|
||||||
'/js/{{ .VERSION }}/key.mjs',
|
|
||||||
'/js/{{ .VERSION }}/lib/custom_html_element.mjs',
|
|
||||||
'/js/{{ .VERSION }}/lib/fullcalendar.min.js',
|
|
||||||
'/js/{{ .VERSION }}/lib/node_modules/marked/lib/marked.esm.js',
|
|
||||||
'/js/{{ .VERSION }}/lib/node_modules/marked/marked.min.js',
|
|
||||||
'/js/{{ .VERSION }}/lib/node_modules/marked-token-position/lib/index.esm.js',
|
|
||||||
'/js/{{ .VERSION }}/lib/sjcl.js',
|
|
||||||
'/js/{{ .VERSION }}/marked_position.mjs',
|
|
||||||
'/js/{{ .VERSION }}/mbus.mjs',
|
|
||||||
'/js/{{ .VERSION }}/node.mjs',
|
'/js/{{ .VERSION }}/node.mjs',
|
||||||
'/js/{{ .VERSION }}/node_store.mjs',
|
'/js/{{ .VERSION }}/node_store.mjs',
|
||||||
'/js/{{ .VERSION }}/notes2.mjs',
|
'/js/{{ .VERSION }}/notes2.mjs',
|
||||||
'/js/{{ .VERSION }}/sync.mjs',
|
'/js/{{ .VERSION }}/sync.mjs',
|
||||||
'/js/{{ .VERSION }}/tree.mjs',
|
'/js/{{ .VERSION }}/key.mjs',
|
||||||
|
'/js/{{ .VERSION }}/crypto.mjs',
|
||||||
|
'/js/{{ .VERSION }}/checklist.mjs',
|
||||||
|
|
||||||
|
'/images/{{ .VERSION }}/logo.svg',
|
||||||
|
'/images/{{ .VERSION }}/leaf.svg',
|
||||||
|
'/images/{{ .VERSION }}/collapsed.svg',
|
||||||
|
'/images/{{ .VERSION }}/expanded.svg',
|
||||||
]
|
]
|
||||||
|
|
||||||
async function precache() {
|
async function precache() {
|
||||||
|
|
@ -46,48 +32,11 @@ async function precache() {
|
||||||
|
|
||||||
async function fetchAsset(event) {
|
async function fetchAsset(event) {
|
||||||
try {
|
try {
|
||||||
const cache = await caches.open(CACHE_NAME)
|
return await fetch(event.request)
|
||||||
const match = await cache.match(event.request)
|
|
||||||
|
|
||||||
if (match !== undefined) {
|
|
||||||
// -----------------------------------------------
|
|
||||||
// This page is precached - return it immediately.
|
|
||||||
// -----------------------------------------------
|
|
||||||
console.debug('From cache', event.request.url)
|
|
||||||
return match
|
|
||||||
} else {
|
|
||||||
// ---------------------------------------------------------------
|
|
||||||
// Not in cache - send it for an online request/browser cache hit.
|
|
||||||
// ---------------------------------------------------------------
|
|
||||||
console.debug('From network', event.request.url)
|
|
||||||
const resp = await fetch(event.request)
|
|
||||||
|
|
||||||
// This will trigger on an HTTP error such as 502.
|
|
||||||
if (!resp.ok) {
|
|
||||||
console.error('HTTP error', resp.status)
|
|
||||||
|
|
||||||
// When JSON is expected, return that instead of the offline HTML page.
|
|
||||||
return await offline(event, `${resp.status} ${resp.statusText}`)
|
|
||||||
}
|
|
||||||
return resp
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// An error here is something like a DNS problem, not a regular HTTP problem.
|
|
||||||
console.error('Network error', e, event.request.url)
|
|
||||||
return await offline(event, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function offline(event, errText) {
|
|
||||||
if (event.request.headers.get('X-JSON')) {
|
|
||||||
return new Response('{ "OK": false, "Error": "Network is offline"}', { headers: { 'Content-Type': 'application/json' } })
|
|
||||||
}
|
|
||||||
|
|
||||||
const cache = await caches.open(CACHE_NAME)
|
const cache = await caches.open(CACHE_NAME)
|
||||||
const offline = await cache.match('/offline')
|
return cache.match(event.request)
|
||||||
let body = await offline.text()
|
}
|
||||||
body = body.replace('||ERROR||', errText)
|
|
||||||
return new Response(body, { headers: { 'Content-Type': 'text/html' } })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function cleanupCache() {
|
async function cleanupCache() {
|
||||||
|
|
@ -100,22 +49,18 @@ async function cleanupCache() {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.addEventListener('install', event => {
|
self.addEventListener('install', event => {
|
||||||
console.debug('SERVICE WORKER: install')
|
console.log('SERVICE WORKER: install')
|
||||||
self.skipWaiting()
|
self.skipWaiting()
|
||||||
event.waitUntil(precache())
|
event.waitUntil(precache())
|
||||||
})
|
})
|
||||||
|
|
||||||
self.addEventListener('activate', event => {
|
self.addEventListener('activate', event => {
|
||||||
console.debug('SERVICE WORKER: activate')
|
console.log('SERVICE WORKER: activate')
|
||||||
self.clients.claim()
|
self.clients.claim()
|
||||||
event.waitUntil(cleanupCache())
|
event.waitUntil(cleanupCache())
|
||||||
})
|
})
|
||||||
|
|
||||||
self.addEventListener('fetch', event => {
|
self.addEventListener('fetch', event => {
|
||||||
// console.debug('SERVICE WORKER: fetch', event.request.url)
|
//console.log('SERVICE WORKER: fetch')
|
||||||
|
|
||||||
if ({{ .DevMode }})
|
|
||||||
return event
|
|
||||||
|
|
||||||
event.respondWith(fetchAsset(event))
|
event.respondWith(fetchAsset(event))
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{{ define "page" }}
|
|
||||||
<div>Site is offline.</div>
|
|
||||||
<div>||ERROR||</div>
|
|
||||||
{{ end }}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue