) => {
let temp = { parentDivs: [], spans: [] }
nodes.forEach((div) => {
div.childNodes.forEach((child) => {
if (child.nodeName == 'SPAN') {
//@ts-ignore
temp.parentDivs.push(div)
//@ts-ignore
child.id = "highlighted"
//@ts-ignore
temp.spans.push(child)
child.addEventListener("mouseover", this.onEnter); //adds mouseover annotation handler
}
})
})
return temp;
}
/**
* when the cursor enters the highlight, it pops out annotation. ONLY WORKS FOR SINGLE DIV LINES
*/
@action
onEnter = (e: any) => {
let span: HTMLSpanElement = e.toElement;
let index: any;
this._pageInfo.divs.forEach((obj: any) => {
obj.spans.forEach((element: any) => {
if (element == span) {
if (!index) {
index = this._pageInfo.divs.indexOf(obj);
}
}
})
})
if (this._pageInfo.anno.length >= index + 1) {
if (this._currAnno.length == 0) {
this._currAnno.push(this._pageInfo.anno[index]);
}
} else {
if (this._currAnno.length == 0) { //if there are no current annotation
let div = span.offsetParent;
//@ts-ignore
let divX = div.style.left
//@ts-ignore
let divY = div.style.top
//slicing "px" from the end
divX = divX.slice(0, divX.length - 2); //gets X of the DIV element (parent of Span)
divY = divY.slice(0, divY.length - 2); //gets Y of the DIV element (parent of Span)
let annotation =
this._pageInfo.anno.push(annotation);
this._currAnno.push(annotation);
}
}
}
/**
* highlight function for highlighting actual text. This works fine.
*/
highlight = (color: string) => {
if (window.getSelection()) {
try {
if (!document.execCommand("hiliteColor", false, color)) {
this.makeEditableAndHighlight(color);
}
} catch (ex) {
this.makeEditableAndHighlight(color)
}
}
}
/**
* controls the area highlighting (stickies) Kinda temporary
*/
onPointerDown = (e: React.PointerEvent) => {
if (this._toolOn) {
let mouse = e.nativeEvent;
this.initX = mouse.offsetX;
this.initY = mouse.offsetY;
}
}
/**
* controls area highlighting and partially highlighting. Kinda temporary
*/
@action
onPointerUp = (e: React.PointerEvent) => {
if (this._highlightToolOn) {
this.highlight("rgba(76, 175, 80, 0.3)"); //highlights to this default color.
this._highlightToolOn = false;
}
if (this._toolOn) {
let mouse = e.nativeEvent;
let finalX = mouse.offsetX;
let finalY = mouse.offsetY;
let width = Math.abs(finalX - this.initX); //width
let height = Math.abs(finalY - this.initY); //height
//these two if statements are bidirectional dragging. You can drag from any point to another point and generate sticky
if (finalX < this.initX) {
this.initX = finalX;
}
if (finalY < this.initY) {
this.initY = finalY;
}
if (this._mainDiv.current) {
let sticky =
this._pageInfo.area.push(sticky);
}
this._toolOn = false;
}
this._interactive = true;
}
/**
* starts drawing the line when user presses down.
*/
onDraw = () => {
if (this._currTool != null) {
this._currTool.style.backgroundColor = "grey";
}
if (this._drawTool.current) {
this._currTool = this._drawTool.current;
if (this._drawToolOn) {
this._drawToolOn = false;
this._pdfCanvas.removeEventListener("pointerdown", this.drawDown);
this._pdfCanvas.removeEventListener("pointerup", this.drawUp);
this._pdfCanvas.removeEventListener("pointermove", this.drawMove);
this._drawTool.current.style.backgroundColor = "grey";
} else {
this._drawToolOn = true;
this._pdfCanvas.addEventListener("pointerdown", this.drawDown);
this._drawTool.current.style.backgroundColor = "cyan";
}
}
}
/**
* for changing color (for ink/pen)
*/
onColorChange = (e: React.PointerEvent) => {
if (e.currentTarget.innerHTML == "Red") {
this._currColor = "red";
} else if (e.currentTarget.innerHTML == "Blue") {
this._currColor = "blue";
} else if (e.currentTarget.innerHTML == "Green") {
this._currColor = "green";
} else if (e.currentTarget.innerHTML == "Black") {
this._currColor = "black";
}
}
/**
* For highlighting (text drag highlighting)
*/
onHighlight = () => {
this._drawToolOn = false;
if (this._currTool != null) {
this._currTool.style.backgroundColor = "grey";
}
if (this._highlightTool.current) {
this._currTool = this._drawTool.current;
if (this._highlightToolOn) {
this._highlightToolOn = false;
this._highlightTool.current.style.backgroundColor = "grey";
} else {
this._highlightToolOn = true;
this._highlightTool.current.style.backgroundColor = "orange";
}
}
}
@action
saveThumbnail = () => {
setTimeout(() => {
var me = this;
htmlToImage.toPng(this._mainDiv.current!,
{ width: me.props.doc.GetNumber(KeyStore.NativeWidth, 0), height: me.props.doc.GetNumber(KeyStore.NativeHeight, 0), quality: 0.5 })
.then(function (dataUrl: string) {
me.props.doc.SetData(KeyStore.Thumbnail, new URL(dataUrl), ImageField);
})
.catch(function (error: any) {
console.error('oops, something went wrong!', error);
});
}, 1000);
}
@action
onLoaded = (page: any) => {
if (this._mainDiv.current) {
this._mainDiv.current.childNodes.forEach((element) => {
if (element.nodeName == "DIV") {
element.childNodes[0].childNodes.forEach((e) => {
if (e instanceof HTMLCanvasElement) {
this._pdfCanvas = e;
this._pdfContext = e.getContext("2d")
}
})
}
})
}
// bcz: the number of pages should really be set when the document is imported.
this.props.doc.SetNumber(KeyStore.NumPages, page._transport.numPages);
if (this._perPageInfo.length == 0) { //Makes sure it only runs once
this._perPageInfo = [...Array(page._transport.numPages)]
}
this._loaded = true;
}
@action
setScaling = (r: any) => {
// bcz: the nativeHeight should really be set when the document is imported.
// also, the native dimensions could be different for different pages of the PDF
// so this design is flawed.
var nativeWidth = this.props.doc.GetNumber(KeyStore.NativeWidth, 0);
if (!this.props.doc.GetNumber(KeyStore.NativeHeight, 0)) {
this.props.doc.SetNumber(KeyStore.NativeHeight, nativeWidth * r.entry.height / r.entry.width);
}
if (!this.props.doc.GetT(KeyStore.Thumbnail, ImageField)) {
this.saveThumbnail();
}
}
@computed
get pdfContent() {
let page = this.curPage;
if (page == 0)
page = 1;
const renderHeight = 2400;
let pdfUrl = this.props.doc.GetT(this.props.fieldKey, PDFField);
let xf = this.props.doc.GetNumber(KeyStore.NativeHeight, 0) / renderHeight;
return ;
}
@computed
get pdfRenderer() {
let proxy = this._loaded ? (null) : this.imageProxyRenderer;
let pdfUrl = this.props.doc.GetT(this.props.fieldKey, PDFField);
if ((!this._interactive && proxy) || !pdfUrl || pdfUrl == FieldWaiting) {
return proxy;
}
return [
this._pageInfo.area.filter(() => this._pageInfo.area).map((element: any) => element),
this._currAnno.map((element: any) => element),
{this.pdfContent}
{proxy}
];
}
@computed
get imageProxyRenderer() {
let field = this.props.doc.Get(KeyStore.Thumbnail);
if (field) {
let path = field == FieldWaiting ? "https://image.flaticon.com/icons/svg/66/66163.svg" :
field instanceof ImageField ? field.Data.href : "http://cs.brown.edu/people/bcz/prairie.jpg";
return
;
}
return (null);
}
render() {
return (
{this.pdfRenderer}
);
}
}