Skip to content

Commit

Permalink
Add panel for revision description
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgangmm committed Mar 11, 2024
1 parent f1d2cc5 commit 59289f6
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 13 deletions.
81 changes: 76 additions & 5 deletions modules/lib/api/annotations.xql
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,27 @@ declare function anno:find-references($request as map(*)) {
: Merge and optionally save the annotations passed in the request body.
:)
declare function anno:save($request as map(*)) {
let $annotations := $request?body
let $body := $request?body
let $annotations := $body?annotations
return
if ($annotations instance of array(*)) then
let $path := xmldb:decode($request?parameters?path)
let $srcDoc := config:get-document($path)
return
anno:merge-and-save($srcDoc, $path, $annotations)
anno:merge-and-save($srcDoc, $path, $annotations, $body?log)
else
let $result :=
for $path in map:keys($annotations)
let $srcDoc := config:get-document($path)
return
anno:merge-and-save($srcDoc, $path, $annotations($path))
anno:merge-and-save($srcDoc, $path, $annotations($path), $body?log)
return
router:response(200, count(map:keys($annotations)) || ' documents merged')

};

declare function anno:merge-and-save($srcDoc as node(), $path as xs:string, $annotations as array(*)) {
declare function anno:merge-and-save($srcDoc as node(), $path as xs:string, $annotations as array(*),
$log as map(*)?) {
let $hasAccess := sm:has-access(document-uri(root($srcDoc)), "rw-")
return
if (not($hasAccess) and request:get-method() = 'PUT') then
Expand All @@ -62,7 +64,7 @@ declare function anno:merge-and-save($srcDoc as node(), $path as xs:string, $ann
return
map:entry($id, anno:apply($node, $ordered))
)
let $merged := anno:merge($doc, $map) => anno:strip-exist-id()
let $merged := anno:merge($doc, $map) => anno:strip-exist-id() => anno:revision($log)
let $output := document {
$srcDoc/(processing-instruction()|comment()),
$merged
Expand Down Expand Up @@ -111,6 +113,75 @@ declare %private function anno:strip-exist-id($nodes as node()*) {
$node
};

declare function anno:revision($nodes as node()*, $log as map(*)?) {
if (exists($log) and map:contains($log, "message") and $log?message != '') then
anno:add-revision($nodes, $log)
else
$nodes
};

declare %private function anno:add-revision($nodes as node()*, $log as map(*)?) {
for $node in $nodes
return
typeswitch($node)
case document-node() return
document {
anno:add-revision($node/node(), $log)
}
case element(tei:teiHeader) return
if (not($node/tei:revisionDesc)) then
element { node-name($node) } {
$node/@*,
$node/node(),
if ($log?message != "") then
<revisionDesc xmlns="http://www.tei-c.org/ns/1.0">
<listChange>
<change when="{current-dateTime()}" who="{$log?user}" status="{$log?status}">{$log?message}</change>
</listChange>
</revisionDesc>
else
()
}
else
element { node-name($node) } {
$node/@*,
anno:add-revision($node/node(), $log)
}
case element(tei:revisionDesc) return
if (not($node/tei:listChange)) then
element { node-name($node) } {
$node/@*,
$node/node(),
if ($log?message != "") then
<listChange xmlns="http://www.tei-c.org/ns/1.0">
<change when="{current-dateTime()}" who="{$log?user}" status="{$log?status}">{$log?message}</change>
</listChange>
else
()
}
else
element { node-name($node) } {
$node/@*,
anno:add-revision($node/node(), $log)
}
case element(tei:listChange) return
element { node-name($node) } {
$node/@*,
$node/node(),
if ($log?message != "") then
<change xmlns="http://www.tei-c.org/ns/1.0" when="{current-dateTime()}" who="{$log?user}" status="{$log?status}">{$log?message}</change>
else
()
}
case element() return
element { node-name($node) } {
$node/@*,
anno:add-revision($node/node(), $log)
}
default return
$node
};

declare %private function anno:merge($nodes as node()*, $elements as map(*)) {
for $node in $nodes
return
Expand Down
10 changes: 7 additions & 3 deletions resources/css/annotate.css
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ paper-icon-button[data-type="edit"] {
display:block;
}

#commit[nonrelevant] {
display: none;
}

#d-review {
position: fixed;
top: 128px;
Expand Down Expand Up @@ -567,7 +571,7 @@ fx-group fx-control {
padding-left: .5rem;
}

fx-control input, fx-control select {
fx-control input, fx-control textarea, fx-control select {
box-sizing: border-box;
margin: 0 0 .5rem 0;
padding: 0;
Expand All @@ -583,7 +587,7 @@ fx-control label {
/* Text color */
}

fx-control input[type="text"] {
fx-control input[type="text"], fx-control textarea {
width: calc(100% - 2rem);
padding: 12px 16px;
border: 1px solid #ccc;
Expand All @@ -593,7 +597,7 @@ fx-control input[type="text"] {
transition: border-color 0.2s ease;
}

fx-control input[type="text"]:focus {
fx-control input[type="text"]:focus, fx-control textarea:focus {
border-color: #007bff;
/* Change border color on focus */
}
Expand Down
40 changes: 37 additions & 3 deletions resources/scripts/annotations/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ window.addEventListener("WebComponentsReady", () => {
const saveBtn = document.getElementById("form-save");
const refInput = document.querySelectorAll(".form-ref");
const nerDialog = document.getElementById("ner-dialog");
const trackHistory = document.getElementById('commit').hasAttribute('track-history');

let autoSave = false;
let type = "";
let emptyElement = false;
let text = "";
let enablePreview = true;
let currentEntityInfo = null;
let previewOdd = "teipublisher";
let currentUser = null;
const doc = view.getDocument();

function restoreAnnotations(doc, annotations) {
Expand Down Expand Up @@ -307,13 +310,18 @@ window.addEventListener("WebComponentsReady", () => {
*
* @param {any} annotations the current list of annotations
*/
function preview(annotations, doStore) {
function preview(annotations, doStore, changeLog) {
if (doStore) {
document.dispatchEvent(new CustomEvent('reset-panels'));
}
const endpoint = document.querySelector("pb-page").getEndpoint();
const doc = document.getElementById("document1");
document.getElementById("output").code = "";

const data = {
annotations,
log: changeLog
};
return new Promise((resolve, reject) => {
fetch(`${endpoint}/api/annotations/merge/${doc.path}`, {
method: doStore ? "PUT" : "POST",
Expand All @@ -322,7 +330,7 @@ window.addEventListener("WebComponentsReady", () => {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(annotations),
body: JSON.stringify(data)
})
.then((response) => {
if (response.ok) {
Expand Down Expand Up @@ -620,11 +628,33 @@ window.addEventListener("WebComponentsReady", () => {
});
// save document action
const saveDocBtn = document.getElementById("document-save");
saveDocBtn.addEventListener("click", () => preview(view.annotations, true));
saveDocBtn.addEventListener("click", () => {
if (trackHistory) {
document.dispatchEvent(new CustomEvent('pb-before-save', {
detail: {
user: currentUser
}
}));
} else {
preview(view.annotations, true);
}
});
if (saveDocBtn.dataset.shortcut) {
window.hotkeys(saveDocBtn.dataset.shortcut, () => preview(view.annotations, true));
}

document.getElementById('commit').addEventListener('pb-commit', (ev) => {
if (ev.detail.message !== '') {
preview(view.annotations, true, {
user: ev.detail.user,
message: ev.detail.message,
status: ev.detail.status
});
} else {
preview(view.annotations, true);
}
});

// save and download merged TEI to local file
const downloadBtn = document.getElementById('document-download');
if ('showSaveFilePicker' in window) {
Expand Down Expand Up @@ -679,6 +709,7 @@ window.addEventListener("WebComponentsReady", () => {
checkNERAvailable();
});


// todo: what's this for? -> fishes the type and query params from iron-form and opens dialog
document.querySelectorAll('.form-ref [slot="prefix"]').forEach(elem => {
elem.addEventListener("click", () => {
Expand Down Expand Up @@ -775,6 +806,9 @@ window.addEventListener("WebComponentsReady", () => {
}
});

window.pbEvents.subscribe('pb-login', null, (ev) => {
currentUser = ev.detail.user;
});
window.pbEvents.subscribe("pb-authority-select", "transcription", (ev) =>
authoritySelected(ev.detail.properties.ref)
);
Expand Down
55 changes: 53 additions & 2 deletions templates/pages/annotate.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,64 @@
<annotation>false</annotation>
<authority>false</authority>
<occurrences>false</occurrences>
<commit>false</commit>
<markup>true</markup>
<authority-editor></authority-editor>
<authority-forms>person, place</authority-forms>
</data>
</fx-instance>
<fx-instance id="i-commit">
<commit user="" status="feat">
<message></message>
</commit>
</fx-instance>
<fx-bind ref="annotation" relevant="boolean-from-string(.)"></fx-bind>
<fx-bind ref="authority" relevant="boolean-from-string(.)"></fx-bind>
<fx-bind ref="occurrences" relevant="boolean-from-string(.)"></fx-bind>
<fx-bind ref="markup" relevant="boolean-from-string(.)"></fx-bind>
<fx-bind ref="authority-editor" relevant="boolean-from-string(.)"></fx-bind>
<fx-bind ref="commit" relevant="boolean-from-string(.)"></fx-bind>

<fx-function signature="updateAnnotationDetails($ident as xs:string) as xs:boolean"
type="text/javascript">
document.querySelector('iron-form paper-input').value=$ident;
</fx-function>


<fx-function signature="commit($msg as xs:string) as xs:boolean" type="text/javascript">
return testCallback($msg);
</fx-function>
</fx-model>

<fx-group ref="commit" id="commit" track-history="">
<section>
<header>Revision Description</header>
<fx-control ref="instance('i-commit')/@user">
<label>User</label>
</fx-control>
<fx-control ref="instance('i-commit')/message">
<label>Change summary</label>
<textarea class="widget" placeholder="Optional short description"></textarea>
</fx-control>
<fx-control ref="instance('i-commit')/@status">
<label>Status</label>
<select class="widget">
<option value="feat">Enrichment</option>
<option value="fix">Correction</option>
</select>
</fx-control>
<fx-trigger>
<paper-icon-button icon="icons:save" data-i18n="[title]annotations.save"></paper-icon-button>
<!-- emit a pb-commit event to annotations.js -->
<fx-dispatch name="pb-commit" targetid="commit">
<fx-property name="message" expr="instance('i-commit')/message/string()"/>
<fx-property name="user" expr="instance('i-commit')/@user"/>
<fx-property name="status" expr="instance('i-commit')/@status"/>
</fx-dispatch>
<fx-setvalue ref="commit">false</fx-setvalue>
</fx-trigger>
</section>
</fx-group>

<fx-group ref="annotation" id="annotation">
<section>
<header>Annotation Details</header>
Expand Down Expand Up @@ -233,6 +272,8 @@
<fx-setvalue ref="annotation">false</fx-setvalue>
<fx-setvalue ref="authority">false</fx-setvalue>
<fx-setvalue ref="occurrences">false</fx-setvalue>
<fx-setvalue ref="commit">false</fx-setvalue>
<fx-setvalue ref="instance('i-commit')/message" value="''"></fx-setvalue>
<!-- ### if a custom form has been embedded before kick it out -->
<fx-load attach-to="#authority-editor">
<template>
Expand All @@ -245,6 +286,15 @@
<fx-refresh></fx-refresh>
</fx-action>

<!-- Show the revision description panel before sending the document to the server -->
<fx-action event="pb-before-save" target="#document">
<fx-setvalue ref="commit">true</fx-setvalue>
<fx-setvalue ref="instance('i-commit')/@user" value="event('user')"></fx-setvalue>
<fx-setvalue ref="instance('i-commit')/message" value="''"></fx-setvalue>
<fx-update></fx-update>
<fx-refresh></fx-refresh>
</fx-action>

<fx-action event="pb-selection-changed" target="#document" if="event('hasContent')">
<fx-setvalue ref="selected" value="event('selected')"></fx-setvalue>
<fx-setvalue ref="instance()/query" value="instance()/selected"></fx-setvalue>
Expand Down Expand Up @@ -292,6 +342,7 @@
<!-- ### show the annotation form and the authority search -->
<fx-action id="show-annotation" event="show-annotation" target="#document">
<fx-call action="reset-panels"></fx-call>
<fx-message>Revision panel relevant: {commit}</fx-message>
<!-- ### show annotation details panel -->
<fx-setvalue ref="annotation">true</fx-setvalue>
</fx-action>
Expand All @@ -305,7 +356,7 @@
<!-- ### update the input in the iron-form with the text-selection we get from 'query' -->
<fx-call function="updateAnnotationDetails(event('ref'))"></fx-call>
<fx-update></fx-update>
</fx-action>
</fx-action>

<!-- ### remove the custom authority editor form from DOM -->
<fx-action event="hide-authority" target="#document">
Expand Down

0 comments on commit 59289f6

Please sign in to comment.