Browse Source

Implement ALTO file export

file-operations
Tissevert 8 months ago
parent
commit
80d40f0652
7 changed files with 68 additions and 5 deletions
  1. +5
    -0
      css/toolbar/menu.css
  2. +1
    -1
      index.html
  3. +3
    -2
      js/Toolbar/Menu.js
  4. +19
    -0
      js/Toolbar/Page.js
  5. +7
    -1
      js/XML.js
  6. +26
    -1
      js/XML/ALTO.js
  7. +7
    -0
      js/XML/ALTO/Element.js

+ 5
- 0
css/toolbar/menu.css View File

@ -32,3 +32,8 @@
background: #777;
color: #fff;
}
.menu ul li.disabled {
color: #888;
background: #ddd;
}

+ 1
- 1
index.html View File

@ -12,7 +12,7 @@
<span>Fichier</span>
<ul>
<li id="loadALTO">Charger des fichiers ALTO</li>
<li id="saveALTO">Exporter la page courante modifiée</li>
<li id="saveALTO" class="disabled">Exporter la page courante modifiée</li>
<li id="loadScoria">Importer des scories</li>
<li id="saveScoria">Exporter les scories</li>
</ul>


+ 3
- 2
js/Toolbar/Menu.js View File

@ -32,8 +32,9 @@ function toggleMenu(menu) {
};
}
function closeMenu() {
if(selectedMenu != undefined) {
function closeMenu(e) {
if(selectedMenu != undefined
&& !(e != undefined && e.target.classList.contains('disabled'))) {
selectedMenu.classList.remove('open');
selectedMenu = null;
windowCatchAll.className = '';


+ 19
- 0
js/Toolbar/Page.js View File

@ -1,6 +1,8 @@
import File;
import * as GUI from GUI;
import * as Async from UnitJS.Async;
import XML;
import * as ALTO from XML.ALTO;
import View;
var files = null;
@ -14,12 +16,29 @@ GUI.loadALTO.addEventListener('click', function() {
GUI.fileNumber.value = 0;
syncNumber();
updateNumber();
GUI.saveALTO.classList.remove('disabled');
return Async.wrap();
}
)
);
});
GUI.saveALTO.addEventListener('click', function() {
if(!GUI.saveALTO.classList.contains('disabled')) {
var file = files[GUI.fileNumber.value];
Async.run(
Async.bind(
File.load(file),
Async.map(XML.parse),
Async.map(function(ALTOFile) {
var payload = encodeURIComponent(XML.serialize(ALTO.edit(ALTOFile)));
File.save('data:text/xml,' + payload, file.name);
})
)
);
}
});
GUI.fileNumber.addEventListener('change', updateNumber);
window.addEventListener('keydown', function(e) {
switch(e.keyCode) {


+ 7
- 1
js/XML.js View File

@ -1,7 +1,8 @@
return {
appendTo: appendTo,
onNodes: onNodes,
parse: parse
parse: parse,
serialize: serialize
}
function appendTo(element) {
@ -18,3 +19,8 @@ function parse(input) {
var domReader = new DOMParser();
return domReader.parseFromString(input, 'text/xml');
}
function serialize(xmlDoc) {
var serializer = new XMLSerializer();
return serializer.serializeToString(xmlDoc);
}

+ 26
- 1
js/XML/ALTO.js View File

@ -6,7 +6,8 @@ import * as Dom from UnitJS.Dom;
import {appendTo, onNodes} from XML;
return {
display: display,
edit: edit,
display: display
};
function display(ALTODoc) {
@ -57,3 +58,27 @@ function display(ALTODoc) {
};
}
}
function edit(ALTODoc) {
var printSpace = ALTODoc.querySelector('Page PrintSpace');
editElement(printSpace);
return ALTODoc;
}
function editElement(xmlElement) {
if(xmlElement.tagName == 'String') {
return !isScoria(xmlElement.getAttribute('ID'));
} else {
editChildren(xmlElement, Element.schema[xmlElement.tagName]);
return xmlElement.children.length > 0;
}
}
function editChildren(xmlElement, childTag) {
for(var i = 0; i < xmlElement.children.length; i++) {
var child = xmlElement.children[i];
if(child.tagName == childTag && !editElement(child)) {
xmlElement.removeChild(child);
}
}
}

+ 7
- 0
js/XML/ALTO/Element.js View File

@ -4,9 +4,16 @@ var printSpace = {
pxHeight: null
};
var schema = {
'PrintSpace': 'TextBlock',
'TextBlock': 'TextLine',
'TextLine': 'String'
};
return {
make: make,
printSpace: printSpace,
schema: schema,
setPrintSpace: setPrintSpace
};


Loading…
Cancel
Save