Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix distorted execution tree in complex queries and jpg download #216

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@
},
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.32",
"@fortawesome/free-solid-svg-icons": "^5.15.1",
"@fortawesome/free-solid-svg-icons": "^6.6.0",
"@fortawesome/react-fontawesome": "^0.1.18",
"@handsontable/react": "^3.1.2",
"handsontable": "^10.0.0",
"html2canvas": "^1.4.1",
"railroad-diagrams": "^1.0.0",
"react-toastify": "^5.5.0",
"react-zoom-pan-pinch": "^3.6.1",
"sass": "^1.49.9",
"serialize-javascript": "^3.1.0",
"svgo": "^1.3.2",
Expand Down
38 changes: 34 additions & 4 deletions src/calc2/components/editorBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1173,15 +1173,45 @@ export class EditorBase extends React.Component<Props, State> {

switch(mode) {
case 'jpg':
const imgDiv = document.getElementsByClassName('ra-tree')[0] as HTMLElement;
if(imgDiv) {
html2canvas(imgDiv).then(canvas => {
const dataUrl = canvas.toDataURL('image/jpeg', 1.0);
const images = document.getElementsByClassName('ra-tree') as HTMLCollectionOf<HTMLElement>;
let imgDiv;
// Find the visible tree image
for (let i = 0; i < images.length; i++) {
// Check if the element is visible
if (images[i].offsetParent !== null) {
imgDiv = images[i] as HTMLElement;
break;
}
}
if (!imgDiv) {
return;
}
const treeElement = imgDiv.cloneNode(true);
document.body.appendChild(treeElement);

// const imgDiv = document.getElementsByClassName('tree')[0] as HTMLElement;
if(treeElement) {
// bug fix for html2canvas
const nodes = (treeElement as HTMLElement).querySelectorAll(".tree li:only-child");
if (nodes && nodes instanceof NodeList) {
for (let i = 1; i < nodes.length; i++) {
const node = nodes[i];
(node as HTMLElement).style.setProperty("top", "-20px");
}
}

html2canvas(treeElement as HTMLElement, {
// better quality
scale: 2,
}).then(canvas => {
document.body.removeChild(treeElement);
const dataUrl = canvas.toDataURL('image/jpeg');
const d = document.createElement('a');
d.href = dataUrl;
d.download = 'result.jpg';
document.body.appendChild(d);
d.click();
document.body.removeChild(d)
});
}
else {
Expand Down
169 changes: 162 additions & 7 deletions src/calc2/components/raTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import classNames from 'classnames';
import { RANode, RANodeBinary, RANodeUnary } from 'db/exec/RANode';
import * as React from 'react';
import { t } from 'calc2/i18n';
import Button from 'reactstrap/es/Button';
import { IconProp } from '@fortawesome/fontawesome-svg-core';
import { faSearchMinus, faSearchPlus, faRefresh, faDownLeftAndUpRightToCenter } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { TransformWrapper, TransformComponent, useControls, ReactZoomPanPinchRef } from "react-zoom-pan-pinch";

require('./raTree.scss');
// require('./raTreeFamilyTree.scss');
Expand Down Expand Up @@ -145,14 +150,164 @@ export class RaTree extends React.Component<Props> {
);
};

return (
<div className="ra-tree">
<div className="tree">
<ul>
{rec(root)}
</ul>
const Controls = () => {
const { zoomIn, zoomOut, resetTransform, centerView } = useControls();

return (
<div className="pan-zoom-controls">
<style>{`
.pan-zoom-controls button {
background: transparent;
border: none;
border-radius: 4px;
height: 28px;
padding: 3px 7px;
}

.pan-zoom-controls button:focus {
box-shadow: none;
}

.pan-zoom-controls button:disabled {
background: transparent;
opacity: 0.5;
cursor: not-allowed;
}
`}</style>
<Button className="zoom-in" title={t('calc.editors.ra.button-zoom-in')} color="" onClick={() => zoomIn(0.1)}>
<span><FontAwesomeIcon icon={faSearchPlus as IconProp} /></span>
</Button>
<Button className="zoom-out" title={t('calc.editors.ra.button-zoom-out')} color="" onClick={() => zoomOut(0.1)}>
<span><FontAwesomeIcon icon={faSearchMinus as IconProp} /></span>
</Button>
<Button className="zoom-reset" title={t('calc.editors.ra.button-zoom-reset')} color="" onClick={() => {
resetTransform();
centerView(1);
} }>
<span><FontAwesomeIcon icon={faRefresh as IconProp} /></span>
</Button>
<Button className="center-view" title={t('calc.editors.ra.button-zoom-center')} color="" onClick={() => {
const containers = document.getElementsByClassName('ra-result') as HTMLCollectionOf<HTMLElement>;
let containerElement;
// Find the first visible tree
for (let i = 0; i < containers.length; i++) {
// Check if the element is visible
if (containers[i].offsetParent !== null) {
containerElement = containers[i] as HTMLElement;
break;
}
}

if (!containerElement) {
return;
}

const newScale =
(containerElement.querySelector('.react-transform-wrapper') as HTMLElement).offsetWidth /
(containerElement.querySelector('.ra-tree') as HTMLElement).offsetWidth;

// if the new scale is less than or equal to 1, zoom out to fit
if (newScale <= 1) {
centerView(newScale);
}
else {
centerView(1);
}
} }>
<span><FontAwesomeIcon icon={faDownLeftAndUpRightToCenter as IconProp} /></span>
</Button>
</div>
</div>
);
};

return (
<TransformWrapper
centerOnInit={true}
centerZoomedOut={true}
minScale={0.1}
maxScale={1}
wheel={ { disabled: true } }
pinch={ { disabled: true } }
doubleClick={ { disabled: true } }
zoomAnimation={ { disabled: true } }
alignmentAnimation={ { disabled: true } }
velocityAnimation={ { disabled: true } }
onTransformed={(ref: ReactZoomPanPinchRef, state: {
scale: number;
positionX: number;
positionY: number
}) => {
const containers = document.getElementsByClassName('ra-result') as HTMLCollectionOf<HTMLElement>;
let containerElement;
// Find the first visible tree
for (let i = 0; i < containers.length; i++) {
// Check if the element is visible
if (containers[i].offsetParent !== null) {
containerElement = containers[i] as HTMLElement;
break;
}
}

if (containerElement) {
const controlElement = containerElement.querySelector('.pan-zoom-controls');
const zoom = parseFloat(state.scale.toString())*100;
const minScale =
(containerElement.querySelector('.react-transform-wrapper') as HTMLElement).offsetWidth /
(containerElement.querySelector('.ra-tree') as HTMLElement).offsetWidth;

if (controlElement) {
const zoomIn = controlElement.querySelector('.zoom-in');
if (zoomIn) {
if (zoom === 100) {
(zoomIn as HTMLButtonElement).disabled = true;
}
else (zoomIn as HTMLButtonElement).disabled = false;
}

const zoomOut = controlElement.querySelector('.zoom-out');
if (zoomOut) {
if (zoom <= minScale*100) {
(zoomOut as HTMLButtonElement).disabled = true;
}
else (zoomOut as HTMLButtonElement).disabled = false;
}

const zoomReset = controlElement.querySelector('.zoom-reset');
const zoomCenter = controlElement.querySelector('.center-view');
if (zoomReset && zoomCenter) {
if (minScale >= 1) {
(zoomReset as HTMLButtonElement).disabled = true;
(zoomCenter as HTMLButtonElement).disabled = true;
}
else {
(zoomReset as HTMLButtonElement).disabled = false;
(zoomCenter as HTMLButtonElement).disabled = false;
}
}
}
}
}}
>
{({ zoomIn, zoomOut, resetTransform, ...rest }) => (
<>
<Controls />
<TransformComponent
wrapperStyle={{
width: "100%",
zoom: '100%',
}}
>
<div className="ra-tree" style={ { width: 'fit-content' }}>
<div className="tree" style={ { width: 'max-content' }}>
<ul>
{rec(root)}
</ul>
</div>
</div>
</TransformComponent>
</>
)}
</TransformWrapper>
);
}
}
2 changes: 1 addition & 1 deletion src/locales/.json

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
"calc.editors.ra.button-download-csv": "Ergebnis (CSV)",
"calc.editors.ra.button-download-jpg": "Ergebnis (JPG)",
"calc.editors.ra.button-download-query": "Query",
"calc.editors.ra.button-zoom-in": "Hereinzoomen",
"calc.editors.ra.button-zoom-out": "Herauszoomen",
"calc.editors.ra.button-zoom-reset": "Auf Standard-Zoomstufe zur\u00fccksetzen",
"calc.editors.ra.button-zoom-center": "Ansicht einpassen",
"calc.editors.ra.toolbar.duplicate-elimination": "Eliminierung von Duplikaten",
"calc.editors.ra.toolbar.duplicate-elimination-content": "<b class=\"math\">∂</b> <b>(</b> A <b>)</b>\n<br><b>delta</b> A",
"calc.editors.ra.toolbar.projection": "Projektion",
Expand Down Expand Up @@ -339,6 +343,10 @@
"calc.editors.ra.button-download-csv": "Ergebnis (CSV)",
"calc.editors.ra.button-download-jpg": "Ergebnis (JPG)",
"calc.editors.ra.button-download-query": "Query",
"calc.editors.ra.button-zoom-in": "Hereinzoomen",
"calc.editors.ra.button-zoom-out": "Herauszoomen",
"calc.editors.ra.button-zoom-reset": "Auf Standard-Zoomstufe zurücksetzen",
"calc.editors.ra.button-zoom-center": "Ansicht einpassen",
"calc.editors.ra.toolbar.duplicate-elimination": "Eliminierung von Duplikaten",
"calc.editors.ra.toolbar.duplicate-elimination-content": "<b class=\"math\">∂</b> <b>(</b> A <b>)</b>\n<br><b>delta</b> A",
"calc.editors.ra.toolbar.projection": "Projektion",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ export const langDE: Partial<typeof langEN> = {
'calc.editors.ra.button-download-csv': 'Ergebnis (CSV)',
'calc.editors.ra.button-download-jpg': 'Ergebnis (JPG)',
'calc.editors.ra.button-download-query': 'Query',
'calc.editors.ra.button-zoom-in': 'Hereinzoomen',
'calc.editors.ra.button-zoom-out': 'Herauszoomen',
'calc.editors.ra.button-zoom-reset': 'Auf Standard-Zoomstufe zurücksetzen',
'calc.editors.ra.button-zoom-center': 'Ansicht einpassen',
'calc.editors.ra.toolbar.duplicate-elimination': 'Eliminierung von Duplikaten',
'calc.editors.ra.toolbar.projection': 'Projektion',
'calc.editors.ra.toolbar.selection': 'Selektion',
Expand Down
8 changes: 8 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
"calc.editors.ra.button-download-csv": "Result (CSV)",
"calc.editors.ra.button-download-jpg": "Result (JPG)",
"calc.editors.ra.button-download-query": "Query",
"calc.editors.ra.button-zoom-in": "Zoom in",
"calc.editors.ra.button-zoom-out": "Zoom out",
"calc.editors.ra.button-zoom-reset": "Reset zoom",
"calc.editors.ra.button-zoom-center": "Zoom to fit",
"calc.editors.ra.toolbar.duplicate-elimination": "duplicate elimination",
"calc.editors.ra.toolbar.duplicate-elimination-content": "<b class=\"math\">∂</b> <b>(</b> A <b>)</b>\n<br><b>delta</b> A",
"calc.editors.ra.toolbar.projection": "projection",
Expand Down Expand Up @@ -337,6 +341,10 @@
"calc.editors.ra.button-download-csv": "Result (CSV)",
"calc.editors.ra.button-download-jpg": "Result (JPG)",
"calc.editors.ra.button-download-query": "Query",
"calc.editors.ra.button-zoom-in": "Zoom in",
"calc.editors.ra.button-zoom-out": "Zoom out",
"calc.editors.ra.button-zoom-reset": "Reset zoom",
"calc.editors.ra.button-zoom-center": "Zoom to fit",
"calc.editors.ra.toolbar.duplicate-elimination": "duplicate elimination",
"calc.editors.ra.toolbar.duplicate-elimination-content": "<b class=\"math\">∂</b> <b>(</b> A <b>)</b>\n<br><b>delta</b> A",
"calc.editors.ra.toolbar.projection": "projection",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ export const langEN = {
'calc.editors.ra.button-download-csv': 'Result (CSV)',
'calc.editors.ra.button-download-jpg': 'Result (JPG)',
'calc.editors.ra.button-download-query': 'Query',
'calc.editors.ra.button-zoom-in': 'Zoom in',
'calc.editors.ra.button-zoom-out': 'Zoom out',
'calc.editors.ra.button-zoom-reset': 'Reset zoom',
'calc.editors.ra.button-zoom-center': 'Zoom to fit',
'calc.editors.ra.toolbar.duplicate-elimination': 'duplicate elimination',
'calc.editors.ra.toolbar.duplicate-elimination-content': [
'<b class="math">∂</b> <b>(</b> A <b>)</b>',
Expand Down
8 changes: 8 additions & 0 deletions src/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
"calc.editors.ra.button-download-csv": "Resultado (CSV)",
"calc.editors.ra.button-download-jpg": "Resultado (JPG)",
"calc.editors.ra.button-download-query": "Query",
"calc.editors.ra.button-zoom-in": "Acercar",
"calc.editors.ra.button-zoom-out": "Alejar",
"calc.editors.ra.button-zoom-reset": "Restablecer zoom",
"calc.editors.ra.button-zoom-center": "Ajustar a la vista",
"calc.editors.ra.toolbar.duplicate-elimination": "eliminaci\u00f3n de duplicados",
"calc.editors.ra.toolbar.duplicate-elimination-content": "<b class=\"math\">∂</b> <b>(</b> A <b>)</b>\n<br><b>delta</b> A",
"calc.editors.ra.toolbar.projection": "proyecci\u00f3n",
Expand Down Expand Up @@ -338,6 +342,10 @@
"calc.editors.ra.button-download-csv": "Resultado (CSV)",
"calc.editors.ra.button-download-jpg": "Resultado (JPG)",
"calc.editors.ra.button-download-query": "Query",
"calc.editors.ra.button-zoom-in": "Acercar",
"calc.editors.ra.button-zoom-out": "Alejar",
"calc.editors.ra.button-zoom-reset": "Restablecer zoom",
"calc.editors.ra.button-zoom-center": "Ajustar a la vista",
"calc.editors.ra.toolbar.duplicate-elimination": "eliminación de duplicados",
"calc.editors.ra.toolbar.duplicate-elimination-content": "<b class=\"math\">∂</b> <b>(</b> A <b>)</b>\n<br><b>delta</b> A",
"calc.editors.ra.toolbar.projection": "proyección",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ export const langES: Partial<typeof langEN> = {
'calc.editors.ra.button-download': 'Descargar',
'calc.editors.ra.button-download-csv': 'Resultado (CSV)',
'calc.editors.ra.button-download-query': 'Query',
'calc.editors.ra.button-zoom-in': 'Acercar',
'calc.editors.ra.button-zoom-out': 'Alejar',
'calc.editors.ra.button-zoom-reset': 'Restablecer zoom',
'calc.editors.ra.button-zoom-center': 'Ajustar a la vista',
'calc.editors.ra.toolbar.duplicate-elimination': 'eliminación de duplicados',
'calc.editors.ra.toolbar.duplicate-elimination-content': [
'<b class="math">∂</b> <b>(</b> A <b>)</b>',
Expand Down
8 changes: 8 additions & 0 deletions src/locales/kr.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
"calc.editors.ra.button-download-csv": "결과 (CSV)",
"calc.editors.ra.button-download-jpg": "결과 (JPG)",
"calc.editors.ra.button-download-query": "쿼리",
"calc.editors.ra.button-zoom-in": "줌인",
"calc.editors.ra.button-zoom-out": "줌아웃",
"calc.editors.ra.button-zoom-reset": "줌초기화",
"calc.editors.ra.button-zoom-center": "뷰에 맞추기",
"calc.editors.ra.toolbar.duplicate-elimination": "중복 제거",
"calc.editors.ra.toolbar.duplicate-elimination-content": "<b class=\"math\">∂</b> <b>(</b> A <b>)</b>\n<br><b>delta</b> A",
"calc.editors.ra.toolbar.projection": "\ud504\ub85c\uc81d\uc158",
Expand Down Expand Up @@ -336,6 +340,10 @@
"calc.editors.ra.button-download-csv": "결과 (CSV)",
"calc.editors.ra.button-download-jpg": "결과 (JPG)",
"calc.editors.ra.button-download-query": "쿼리",
"calc.editors.ra.button-zoom-in": "줌인",
"calc.editors.ra.button-zoom-out": "줌아웃",
"calc.editors.ra.button-zoom-reset": "줌초기화",
"calc.editors.ra.button-zoom-center": "뷰에 맞추기",
"calc.editors.ra.toolbar.duplicate-elimination": "중복 제거",
"calc.editors.ra.toolbar.duplicate-elimination-content": "<b class=\"math\">∂</b> <b>(</b> A <b>)</b>\n<br><b>delta</b> A",
"calc.editors.ra.toolbar.projection": "프로젝션",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/kr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export const langKR = {
'calc.editors.ra.button-download-csv': '결과 (CSV)',
'calc.editors.ra.button-download-jpg': '결과 (JPG)',
'calc.editors.ra.button-download-query': '쿼리',
'calc.editors.ra.button-zoom-in': '줌인',
'calc.editors.ra.button-zoom-out': '줌아웃',
'calc.editors.ra.button-zoom-reset': '줌리셋',
'calc.editors.ra.button-zoom-center': '화면에 맞추기',
'calc.editors.ra.toolbar.duplicate-elimination': '중복 제거',
'calc.editors.ra.toolbar.duplicate-elimination-content[0]':
'<b class="math">∂</b> <b>(</b> A <b>)</b>',
Expand Down
Loading