5 * @copyright 2018 - Ken Grimes
6 * @summmary live edit front-end for fediblog
10 const apiURL
= document
.location
.origin
+ '/api/'
11 const baseURL
= document
.location
.origin
13 const addButton
= (anchor
, name
, fn
) => {
14 const button
= document
.createElement('button')
15 const buttonText
= document
.createTextNode(name
)
16 button
.appendChild(buttonText
)
17 button
.onclick
= () => fn(button
)
18 if (anchor
.firstChild
)
19 anchor
.insertBefore(button
,anchor
.firstChild
)
21 anchor
.appendChild(button
)
25 const logout
= () => {
26 document
.cookie
= 'live-edit=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'
27 window
.location
.reload(true)
30 const doDelete
= (btn
) => {
31 const element
= btn
.parentElement
32 const fileName
= 'content/' + element
.id
33 if (window
.confirm(`Do you really want to delete ${element.id}?`))
34 fetch(apiURL
+ fileName
, {credentials
: 'same-origin', method
: 'DELETE'}).then((resp
) => {
36 window
.location
.href
= window
.location
.href
+ ".."
37 else if (resp
.status
=== 401)
44 const doCancel
= (btn
) => {
45 const element
= btn
.parentElement
46 element
.innerHTML
= element
.oldHTML
49 const doCommit
= (btn
) => {
50 const element
= btn
.parentElement
51 const fileName
= 'content/' + element
.id
52 const textArea
= element
.getElementsByTagName('textarea').item(0)
53 fetch(apiURL
+ fileName
, {credentials
: 'same-origin', method
: 'POST', body
: textArea
.value
}).then((resp
) => {
55 window
.location
.reload(true)
56 else if (resp
.status
=== 401)
63 const doCommitPost
= (btn
) => {
64 const element
= btn
.parentElement
65 const nameBox
= element
.getElementsByTagName('input').item(0)
66 if (nameBox
.value
=== '') {
67 window
.alert('Filename is required')
70 const secMatch
= window
.location
.href
.match(new RegExp(baseURL
+ '(.*/)'))
71 const section
= secMatch
? secMatch
[1] : ''
72 const fileName
= 'content/' + section
+ nameBox
.value
+ '.md'
73 const textArea
= element
.getElementsByTagName('textarea').item(0)
74 fetch(apiURL
+ fileName
, {credentials
: 'same-origin', method
: 'POST', body
: textArea
.value
}).then((resp
) => {
76 window
.location
.reload(true)
77 else if (resp
.status
=== 401)
84 const doPost
= (btn
) => {
85 const element
= btn
.parentElement
86 element
.oldHTML
= element
.innerHTML
88 '<button onclick=doCommitPost(this)>commit</button>' +
89 '<button onclick=doCancel(this)>cancel</button><br>' +
90 'Filename: <input type="text" />' +
91 '<textarea cols=80 rows=60></textarea>'
94 const doEdit
= (btn
) => {
95 const element
= btn
.parentElement
96 const fileName
= 'content/' + element
.id
97 fetch(apiURL
+ fileName
, {credentials
: 'same-origin'}).then((resp
) => {
98 if (resp
.status
=== 401)
101 resp
.text().then((txt
) => {
102 element
.oldHTML
= element
.innerHTML
104 '<button onclick=doCommit(this)>commit</button>' +
105 '<button onclick=doDelete(this)>delete</button>' +
106 '<button onclick=doCancel(this)>cancel</button><br>' +
107 '<textarea cols=80 rows=60>' +
116 const editTools
= () => {
117 const article
= document
.body
.getElementsByTagName('article').item(0)
118 addButton(article
, 'new page', doPost
)
119 addButton(article
, 'edit', doEdit
)