5 * @copyright 2018 - Ken Grimes
6 * @summmary live edit front-end for fediblog
10 const apiURL
= document
.location
.origin
+ '/aa/api/'
11 const baseURL
= document
.location
.origin
+ '/aa/'
13 const logout
= () => {
14 document
.cookie
= 'live-edit=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'
15 window
.location
.reload(true)
18 const doDelete
= (btn
) => {
19 const element
= btn
.parentElement
20 const fileName
= 'content/' + element
.id
21 if (window
.confirm(`Do you really want to delete ${element.id}?`))
22 fetch(apiURL
+ fileName
, {credentials
: 'same-origin', method
: 'DELETE'}).then((resp
) => {
24 window
.location
.href
= window
.location
.href
+ ".."
25 else if (resp
.status
=== 401)
32 const doCancel
= (btn
) => {
33 const element
= btn
.parentElement
34 element
.innerHTML
= element
.oldHTML
37 const doCommit
= (btn
) => {
38 const element
= btn
.parentElement
39 const fileName
= 'content/' + element
.id
40 const textArea
= element
.getElementsByTagName('textarea').item(0)
41 fetch(apiURL
+ fileName
, {credentials
: 'same-origin', method
: 'POST', body
: textArea
.value
}).then((resp
) => {
43 window
.location
.reload(true)
44 else if (resp
.status
=== 401)
51 const doCommitPost
= (btn
) => {
52 const element
= btn
.parentElement
53 const nameBox
= element
.getElementsByTagName('input').item(0)
54 if (nameBox
.value
=== '') {
55 window
.alert('Filename is required')
58 const section
= window
.location
.href
.match(new RegExp(baseURL
+ '(.*/)'))[1]
59 const fileName
= 'content/' + section
+ nameBox
.value
+ '.md'
60 const textArea
= element
.getElementsByTagName('textarea').item(0)
61 fetch(apiURL
+ fileName
, {credentials
: 'same-origin', method
: 'POST', body
: textArea
.value
}).then((resp
) => {
63 window
.location
.reload(true)
64 else if (resp
.status
=== 401)
71 const doPost
= (btn
) => {
72 const element
= btn
.parentElement
73 element
.oldHTML
= element
.innerHTML
75 '<button onclick=doCommitPost(this)>commit</button>' +
76 '<button onclick=doCancel(this)>cancel</button><br>' +
77 'Filename: <input type="text" />' +
78 '<textarea cols=80 rows=60></textarea>'
81 const doEdit
= (btn
) => {
82 const element
= btn
.parentElement
83 const fileName
= 'content/' + element
.id
84 fetch(apiURL
+ fileName
, {credentials
: 'same-origin'}).then((resp
) => {
85 if (resp
.status
=== 401)
88 resp
.text().then((txt
) => {
89 element
.oldHTML
= element
.innerHTML
91 '<button onclick=doCommit(this)>commit</button>' +
92 '<button onclick=doDelete(this)>delete</button>' +
93 '<button onclick=doCancel(this)>cancel</button><br>' +
94 '<textarea cols=80 rows=60>' +
103 const editTools
= () => {
104 const article
= document
.body
.getElementsByTagName('article').item(0)
106 '<button onclick="doEdit(this)">edit</button>' +
107 '<button onclick="doPost(this)">new post</button><br>' +
111 window
.onload
= () => {
112 if (document
.cookie
.match('live-edit=true'))