2022-10-26 23:12:29 +02:00
|
|
|
document.body.dataset.editHost = window.location.protocol
|
|
|
|
+ '//edit.'
|
|
|
|
+ window.location.host
|
|
|
|
document.body.dataset.editUrl = document.body.dataset.editHost
|
2022-11-21 11:34:14 +01:00
|
|
|
+ '/'
|
2022-10-26 23:12:29 +02:00
|
|
|
+ file.replace('.md', '')
|
|
|
|
|
|
|
|
const topSection = window.location.pathname.split('/')[1]
|
|
|
|
if (topSection) document.body.classList.add(topSection)
|
|
|
|
|
|
|
|
// Differentiate users and authors
|
2022-11-13 16:50:48 +01:00
|
|
|
if ('pages' === topSection) {
|
2022-10-26 23:12:29 +02:00
|
|
|
document.body.classList.add('author')
|
2022-11-13 16:50:48 +01:00
|
|
|
} else {
|
2022-11-14 18:10:59 +01:00
|
|
|
// Special URL which does not require authentication but returns browser's headers
|
|
|
|
// thus we can deduce if user is authenticated
|
|
|
|
fetch('/author')
|
|
|
|
.then(result => { if (result.headers.get('authorization')) document.body.classList.add('author') })
|
2022-11-13 16:50:48 +01:00
|
|
|
}
|
2022-10-26 23:12:29 +02:00
|
|
|
|
|
|
|
// Setup a "property" attribute, to send to the backend for insertion/update/deletion
|
|
|
|
document.querySelectorAll('body > header h1, main article header h1')
|
|
|
|
.forEach(node => node.dataset.property = 'title')
|
|
|
|
document.querySelectorAll('body > header > div > div > h2')
|
|
|
|
.forEach(node => node.dataset.property = 'description')
|
|
|
|
|
|
|
|
document.querySelectorAll('body [data-property]')
|
|
|
|
.forEach(node => {
|
|
|
|
node.contentEditable = true
|
|
|
|
node.onfocus = () => preparePropertyEdition(node)
|
|
|
|
node.onblur = () => updateProperty(node)
|
|
|
|
})
|
|
|
|
|
|
|
|
setupChoiceEdition = () => {
|
|
|
|
document.querySelectorAll('body.author .ac_choice label.text, body.author .ac_choice input[type=text]')
|
|
|
|
.forEach(node => {
|
|
|
|
node.contentEditable = true
|
|
|
|
node.dataset.property = `ac_choices`
|
|
|
|
node.onfocus = () => prepareChoiceEdition(node)
|
|
|
|
node.onblur = () => updateChoice(node)
|
|
|
|
node.removeAttribute('for')
|
|
|
|
})
|
|
|
|
|
|
|
|
document.querySelectorAll('body.author .new_choice label.text, body.author .new_choice input')
|
|
|
|
.forEach(node => node.onblur = () => createChoice(node.closest('div.ac_choice')))
|
|
|
|
|
|
|
|
document.querySelectorAll('body.author .ac_choice button.delete')
|
|
|
|
.forEach(node => node.onclick = () => deleteChoice(node))
|
|
|
|
|
|
|
|
window.getSelection().removeAllRanges()
|
|
|
|
const range = document.createRange()
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('paste', (e) => {
|
|
|
|
if (!e.isContentEditable) return
|
|
|
|
|
|
|
|
e.preventDefault()
|
|
|
|
const text = (e.originalEvent || e).clipboardData.getData('text/plain')
|
|
|
|
window.document.execCommand('insertText', false, text)
|
|
|
|
})
|
|
|
|
|
|
|
|
setupChoiceEdition()
|
|
|
|
|
|
|
|
tinymce.init({
|
|
|
|
browser_spellcheck: true,
|
|
|
|
entity_encoding: 'raw',
|
|
|
|
inline: true,
|
|
|
|
language: navigator.language,
|
|
|
|
language_url: '/js/langs/' + navigator.language + '.js',
|
|
|
|
menubar: false,
|
|
|
|
plugins: [ 'lists', 'advlist', 'autolink', 'link', 'image', 'media',
|
|
|
|
'charmap', 'insertdatetime', 'preview', 'table', 'fullscreen',
|
|
|
|
'searchreplace', 'insertdatetime', 'visualblocks', 'visualchars',
|
|
|
|
'wordcount', 'code', 'importcss' ],
|
|
|
|
init_instance_callback: (editor) => editor.on('blur', (e) => {
|
|
|
|
fetch(document.body.dataset.editUrl + '.content', {
|
|
|
|
method: 'PUT',
|
|
|
|
body: tinymce.activeEditor.getContent().replaceAll('<p>', '\n').replaceAll('</p>', '')
|
|
|
|
})
|
|
|
|
.catch(error => console.error('Error editing property', error))
|
|
|
|
}),
|
|
|
|
selector: '#content',
|
|
|
|
toolbar1: 'save cut copy paste | undo redo | image media charmap insertdatetime | searchreplace code visualblocks fullscreen',
|
|
|
|
toolbar2: 'removeformat bold italic strikethrough forecolor backcolor | bullist numlist outdent indent | alignleft aligncenter alignright alignjustify',
|
|
|
|
toolbar_mode: 'wrap'
|
|
|
|
})
|