pdf-to-markdown/oldSrc/store.js

65 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-01-04 19:09:37 +01:00
import Page from './models/Page.js';
2017-01-03 22:46:51 +01:00
// Holds the state of the application
export default {
state: {
uploaded: false,
2017-01-04 19:09:37 +01:00
pagesToUpload: 0,
uploadedPages: 0,
rawPages: [],
pages: [],
},
preparePageUpload: function(numPages) {
this.state.pagesToUpload = numPages;
for (var i = 0; i <= numPages; i++) {
this.state.rawPages.push(new Page({
index: i
}));
}
},
uploadPage: function(pageIndex, textItems) {
this.state.rawPages[pageIndex].textItems = textItems;
this.state.uploadedPages++;
if (this.state.uploadedPages == this.state.pagesToUpload) {
this.state.rawPages.map(rawPage => {
var text = '';
var line;
var lineY;
2017-01-06 10:07:06 +01:00
// console.debug("Page " + rawPage.index + "-------");
2017-01-04 19:09:37 +01:00
rawPage.textItems.forEach(textItem => {
2017-01-09 21:24:12 +01:00
// console.debug(textItem);
2017-01-06 10:07:06 +01:00
const yRounded = Math.round(textItem.y);
2017-01-04 19:09:37 +01:00
if (!line) {
2017-01-06 10:07:06 +01:00
// console.debug("First line: " + textItem.text);
lineY = yRounded;
2017-01-04 19:09:37 +01:00
line = textItem.text;
} else {
2017-01-06 10:07:06 +01:00
if (yRounded === lineY) {
// console.debug("Add to line: " + line + " / " + textItem.text);
2017-01-04 19:09:37 +01:00
line += textItem.text;
} else {
2017-01-06 10:07:06 +01:00
// console.debug("Start line: " + line + " / " + textItem.text);
2017-01-04 19:09:37 +01:00
text += line + '\n';
line = textItem.text;
2017-01-06 10:07:06 +01:00
lineY = yRounded;
2017-01-04 19:09:37 +01:00
}
}
});
text += line;
this.state.pages.push(text);
});
// this.state.pages = pages;
this.state.uploaded = true;
}
2017-01-03 22:46:51 +01:00
},
upload: function(pages) {
2017-01-09 21:24:12 +01:00
// console.debug("Store: upload");
2017-01-03 22:46:51 +01:00
this.state.uploaded = true;
this.state.pages = pages;
},
2017-01-04 19:09:37 +01:00
2017-01-03 22:46:51 +01:00
}