|
| 1 | +import { html } from 'lit' |
| 2 | +import { ref } from 'lit/directives/ref.js' |
| 3 | +import { parse, serialize, NamedNode, LiveStore } from 'rdflib' |
| 4 | +import { widgets, icons } from 'solid-ui' |
| 5 | +import { getStatusSection } from './StatusSection' |
| 6 | +import { applyResponseHeaders, checkSyntax, getResponseHeaders, happy, refresh, setControlVisible, setEdited, setUnedited } from './helpers' |
| 7 | +import { SourcePaneState } from './types' |
| 8 | +import { compactable } from './compactableFormats' |
| 9 | + |
| 10 | +function mountButton (host: HTMLElement, button: HTMLElement) { |
| 11 | + host.replaceChildren(button) |
| 12 | +} |
| 13 | + |
| 14 | +export function renderHeader (store: LiveStore, subject: NamedNode, sourcePaneState: SourcePaneState) { |
| 15 | + async function saveBack (store: LiveStore, subject: NamedNode, sourcePaneState: SourcePaneState) { |
| 16 | + const fetcher = store.fetcher |
| 17 | + const textArea = document.querySelector('.sourcePaneTextArea') as HTMLTextAreaElement |
| 18 | + const data = textArea.value |
| 19 | + const { contentType, eTag } = sourcePaneState |
| 20 | + if (!checkSyntax(store, subject, data, contentType, subject)) { |
| 21 | + setEdited(sourcePaneState) |
| 22 | + const { showError } = getStatusSection() |
| 23 | + showError('Syntax error: fix the document before saving.') |
| 24 | + return |
| 25 | + } |
| 26 | + const options: { data: string; contentType: string | undefined; headers?: { 'if-match': string } } = { data, contentType } |
| 27 | + if (eTag) options.headers = { 'if-match': eTag } // avoid overwriting changed files -> status 412 |
| 28 | + try { |
| 29 | + const response = await fetcher.webOperation('PUT', subject.uri, options) |
| 30 | + if (!happy(response, 'PUT')) return |
| 31 | + /// @@ show edited: make save button disabled until edited again. |
| 32 | + try { |
| 33 | + const response = await fetcher.webOperation('HEAD', subject.uri) // , defaultFetchHeaders()) |
| 34 | + if (!happy(response, 'HEAD')) return |
| 35 | + applyResponseHeaders(sourcePaneState, getResponseHeaders(store, subject, response)) |
| 36 | + setUnedited(subject, sourcePaneState) |
| 37 | + } catch (err) { |
| 38 | + throw err |
| 39 | + } |
| 40 | + } catch (err: any) { |
| 41 | + const { showError } = getStatusSection() |
| 42 | + showError('Error saving back: ' + err) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + function setEditable (sourcePaneState: SourcePaneState) { |
| 47 | + const textArea = document.querySelector('.sourcePaneTextArea') as HTMLTextAreaElement |
| 48 | + const cancelButton = document.querySelector('.sourcePaneCancelButton') as HTMLElement |
| 49 | + const saveButton = document.querySelector('.sourcePaneSaveButton') as HTMLElement |
| 50 | + const myEditButton = document.querySelector('.sourcePaneEditButton') as HTMLElement |
| 51 | + const myCompactButton = document.querySelector('.sourcePaneCompactButton') as HTMLElement |
| 52 | + if (sourcePaneState.broken) return |
| 53 | + sourcePaneState.editing = true |
| 54 | + setControlVisible(cancelButton, true) // not logically needed but may be comforting |
| 55 | + setControlVisible(saveButton, false) |
| 56 | + setControlVisible(myEditButton, false) |
| 57 | + setControlVisible(myCompactButton, false) // do not allow compact while editing |
| 58 | + if (textArea) textArea.removeAttribute('readonly') |
| 59 | + } |
| 60 | + |
| 61 | + function compactHandler (store: LiveStore, subject: NamedNode, sourcePaneState: SourcePaneState) { |
| 62 | + const { contentType } = sourcePaneState |
| 63 | + const compactContentType = contentType?.split(';')[0] |
| 64 | + const textArea = document.querySelector('.sourcePaneTextArea') as HTMLTextAreaElement |
| 65 | + const cancelButton = document.querySelector('.sourcePaneCancelButton') as HTMLElement |
| 66 | + const { showError } = getStatusSection() |
| 67 | + |
| 68 | + if (compactContentType && compactable[compactContentType]) { |
| 69 | + try { |
| 70 | + parse(textArea.value, store, subject.uri, compactContentType) |
| 71 | + // for jsonld serialize which is a Promise. New rdflib |
| 72 | + const serialized = Promise.resolve(serialize(store.sym(subject.uri), store, subject.uri, compactContentType)) |
| 73 | + serialized.then(result => { |
| 74 | + if (typeof result === 'string') textArea.value = result /*return div*/ |
| 75 | + }) |
| 76 | + setControlVisible(cancelButton, true) |
| 77 | + } catch (e: any) { |
| 78 | + showError(String(e)) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return html` |
| 84 | + <header class="sourcePaneHeader sourcePaneControls"> |
| 85 | + <span class="sourcePaneControlHost" ${ref((host: Element | undefined) => { |
| 86 | + if (host instanceof HTMLElement) { |
| 87 | + mountButton(host, createEditButton(host.ownerDocument, () => setEditable(sourcePaneState))) |
| 88 | + } |
| 89 | + })}></span> |
| 90 | + <span class="sourcePaneControlHost" ${ref((host: Element | undefined) => { |
| 91 | + if (host instanceof HTMLElement) { |
| 92 | + mountButton(host, createCompactButton(host.ownerDocument, () => compactHandler(store, subject, sourcePaneState))) |
| 93 | +
|
| 94 | + } |
| 95 | + })}></span> |
| 96 | + <span class="sourcePaneControlHost" ${ref((host: Element | undefined) => { |
| 97 | + if (host instanceof HTMLElement) { |
| 98 | + mountButton(host, createCancelButton(host.ownerDocument, () => refresh(store, subject, sourcePaneState))) |
| 99 | + } |
| 100 | + })}></span> |
| 101 | + <span class="sourcePaneControlHost" ${ref((host: Element | undefined) => { |
| 102 | + if (host instanceof HTMLElement) { |
| 103 | + mountButton(host, createSaveButton(host.ownerDocument, () => saveBack(store, subject, sourcePaneState))) |
| 104 | + } |
| 105 | + })}></span> |
| 106 | + </header> |
| 107 | + ` |
| 108 | +} |
| 109 | + |
| 110 | +export function createEditButton (dom: Document, onClick?: EventListener) { |
| 111 | + const myEditButton = widgets.button( |
| 112 | + dom, |
| 113 | + icons.iconBase + 'noun_253504.svg', |
| 114 | + 'Edit' |
| 115 | + ) |
| 116 | + myEditButton.classList.add('sourcePaneEditButton') |
| 117 | + if (onClick) { |
| 118 | + myEditButton.addEventListener('click', onClick) |
| 119 | + } |
| 120 | + return myEditButton |
| 121 | +} |
| 122 | + |
| 123 | +export function createCompactButton (dom: Document, onClick?: EventListener) { |
| 124 | + const myCompactButton = widgets.button( |
| 125 | + dom, |
| 126 | + undefined, |
| 127 | + 'Compact', |
| 128 | + { needsBorder: true } |
| 129 | + ) |
| 130 | + myCompactButton.classList.add('sourcePaneCompactButton') |
| 131 | + if (onClick) { |
| 132 | + myCompactButton.addEventListener('click', onClick) |
| 133 | + } |
| 134 | + return myCompactButton |
| 135 | +} |
| 136 | + |
| 137 | +export function createCancelButton (dom: Document, onClick?: EventListener) { |
| 138 | + const myCancelButton = widgets.cancelButton(dom) |
| 139 | + myCancelButton.classList.add('sourcePaneCancelButton') |
| 140 | + if (onClick) { |
| 141 | + myCancelButton.addEventListener('click', onClick) |
| 142 | + } |
| 143 | + return myCancelButton |
| 144 | +} |
| 145 | + |
| 146 | +export function createSaveButton (dom: Document, onClick?: EventListener) { |
| 147 | + const mySaveButton = widgets.continueButton(dom) |
| 148 | + mySaveButton.classList.add('sourcePaneSaveButton') |
| 149 | + if (onClick) { |
| 150 | + mySaveButton.addEventListener('click', onClick) |
| 151 | + } |
| 152 | + return mySaveButton |
| 153 | +} |
0 commit comments