|
| 1 | +function initLiveEdit() { |
| 2 | + function getArticle() { |
| 3 | + let article = document.querySelector('#content > article'); |
| 4 | + |
| 5 | + if (article === null) { |
| 6 | + // If no article element is found the user may have a custom template, so we cannot know which element to edit. |
| 7 | + throw new Error('No article element found, cannot live edit. If you are using a custom template, please make sure to include an article element in the #content container.'); |
| 8 | + } |
| 9 | + |
| 10 | + return article; |
| 11 | + } |
| 12 | + |
| 13 | + function getLiveEditor() { |
| 14 | + return document.querySelector('#live-edit-container'); |
| 15 | + } |
| 16 | + |
| 17 | + function showEditor() { |
| 18 | + article.style.display = 'none'; |
| 19 | + getLiveEditor().style.display = ''; |
| 20 | + focusOnTextarea(); |
| 21 | + } |
| 22 | + |
| 23 | + function hideEditor() { |
| 24 | + article.style.display = ''; |
| 25 | + getLiveEditor().style.display = 'none'; |
| 26 | + } |
| 27 | + |
| 28 | + function focusOnTextarea() { |
| 29 | + const textarea = getLiveEditor().querySelector('textarea'); |
| 30 | + |
| 31 | + textarea.selectionStart = textarea.value.length; |
| 32 | + textarea.focus(); |
| 33 | + } |
| 34 | + |
| 35 | + function switchToEditor() { |
| 36 | + |
| 37 | + function hasEditorBeenSetUp() { |
| 38 | + return getLiveEditor() !== null; |
| 39 | + } |
| 40 | + |
| 41 | + function setupEditor() { |
| 42 | + const template = document.getElementById('live-edit-template'); |
| 43 | + const article = getArticle(); |
| 44 | + let editor = document.importNode(template.content, true); |
| 45 | + article.parentNode.insertBefore(editor, article.nextSibling); |
| 46 | + editor = getLiveEditor(); |
| 47 | + |
| 48 | + // Apply CSS classes from article to editor to match layout |
| 49 | + editor.classList.add(...article.classList); |
| 50 | + |
| 51 | + showEditor(); |
| 52 | + |
| 53 | + document.getElementById('liveEditCancel').addEventListener('click', hideEditor); |
| 54 | + } |
| 55 | + |
| 56 | + if (hasEditorBeenSetUp()) { |
| 57 | + showEditor(); |
| 58 | + } else { |
| 59 | + setupEditor(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + function handleShortcut(event) { |
| 64 | + let isEditorHidden = getLiveEditor() === null || getLiveEditor().style.display === 'none'; |
| 65 | + let isEditorVisible = getLiveEditor() !== null && getLiveEditor().style.display !== 'none'; |
| 66 | + |
| 67 | + if (event.ctrlKey && event.key === 'e') { |
| 68 | + event.preventDefault(); |
| 69 | + |
| 70 | + if (isEditorHidden) { |
| 71 | + switchToEditor(); |
| 72 | + } else { |
| 73 | + hideEditor(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (event.ctrlKey && event.key === 's') { |
| 78 | + if (isEditorVisible) { |
| 79 | + event.preventDefault(); |
| 80 | + |
| 81 | + document.getElementById('liveEditSubmit').click(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (event.key === 'Escape') { |
| 86 | + if (isEditorVisible) { |
| 87 | + event.preventDefault(); |
| 88 | + |
| 89 | + hideEditor(); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + function shortcutsEnabled() { |
| 95 | + return localStorage.getItem('hydephp.live-edit.shortcuts') !== 'false'; |
| 96 | + } |
| 97 | + |
| 98 | + const article = getArticle(); |
| 99 | + |
| 100 | + article.addEventListener('dblclick', switchToEditor); |
| 101 | + |
| 102 | + if (shortcutsEnabled()) { |
| 103 | + document.addEventListener('keydown', handleShortcut); |
| 104 | + } |
| 105 | +} |
0 commit comments