tycho-components 0.2.4-SNAPSHOT-2 → 0.2.4-SNAPSHOT-4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,6 @@
3
3
  flex-direction: column;
4
4
  justify-content: center;
5
5
  align-items: center;
6
- cursor: pointer;
7
6
  width: 100%;
8
7
 
9
8
  > span {
@@ -10,7 +10,7 @@ type Props = {
10
10
  renderWithInfo?: boolean;
11
11
  placeholder?: string;
12
12
  onClickExpression?: () => void;
13
- onExpand?: () => void;
13
+ onExpand?: (reset: () => void) => void;
14
14
  onReadyCustom?: (thisCy: Core) => void;
15
15
  };
16
16
  export default function TreeView({ struct, expression, selector, translations, wheelSensitivity, renderWithInfo, placeholder, onClickExpression, onExpand, onReadyCustom, }: Props): import("react/jsx-runtime").JSX.Element;
@@ -2,6 +2,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { faDiagramProject, faDownload, faExpand, faInfoCircle, faUpRightAndDownLeftFromCenter, } from '@fortawesome/free-solid-svg-icons';
3
3
  import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
4
4
  import { saveAs } from 'file-saver';
5
+ import html2canvas from 'html2canvas';
5
6
  import { useEffect, useState } from 'react';
6
7
  import { useTranslation } from 'react-i18next';
7
8
  import AppPlaceholder from '../AppPlaceholder';
@@ -15,6 +16,7 @@ export default function TreeView({ struct, expression, selector = 'canvas-tree',
15
16
  const [cy, setCy] = useState(null);
16
17
  const [showInfo, setShowInfo] = useState(renderWithInfo);
17
18
  const [invalid, setInvalid] = useState();
19
+ const [expandedExternally, setExpandedExternally] = useState(false);
18
20
  const load = () => {
19
21
  setCy(null);
20
22
  const element = document.getElementById(selector);
@@ -52,9 +54,20 @@ export default function TreeView({ struct, expression, selector = 'canvas-tree',
52
54
  saveAs(blob, 'sentence.psd');
53
55
  };
54
56
  const generateImage = () => {
55
- if (cy) {
56
- saveAs(cy.jpg({ full: true }), 'tree.jpg');
57
- }
57
+ if (!cy)
58
+ return;
59
+ const cyContainer = cy.container();
60
+ if (!cyContainer)
61
+ return;
62
+ html2canvas(cyContainer, {
63
+ backgroundColor: 'white',
64
+ }).then((canvas) => {
65
+ canvas.toBlob((blob) => {
66
+ if (blob === null)
67
+ return;
68
+ saveAs(blob, 'tree.jpg');
69
+ }, 'image/jpeg', 1.0);
70
+ });
58
71
  };
59
72
  const reset = () => {
60
73
  if (cy) {
@@ -80,15 +93,28 @@ export default function TreeView({ struct, expression, selector = 'canvas-tree',
80
93
  setShowInfo(newVal);
81
94
  applyInfo(cy, struct, newVal);
82
95
  };
96
+ const handleExpand = () => {
97
+ setExpandedExternally(true);
98
+ onExpand && onExpand(reset);
99
+ };
83
100
  useEffect(() => {
84
101
  setInvalid(undefined);
85
102
  load();
86
103
  }, [struct]);
104
+ useEffect(() => {
105
+ if (expandedExternally) {
106
+ const id = setTimeout(() => {
107
+ reset();
108
+ setExpandedExternally(false);
109
+ }, 50);
110
+ return () => clearTimeout(id);
111
+ }
112
+ }, [expandedExternally]);
87
113
  if (invalid) {
88
114
  return (_jsx(AppPlaceholder, { text: placeholder || t('placeholder.sentence.notparsed') }));
89
115
  }
90
116
  return (_jsxs("div", { className: "tree-view-container", children: [cy &&
91
- getButtons(generateImage, reset, onClickExpression || downloadPsd, toggleInfo, onExpand, expression)
117
+ getButtons(generateImage, reset, onClickExpression || downloadPsd, toggleInfo, handleExpand, onExpand, expression)
92
118
  .filter((btn) => btn.condition)
93
119
  .map((btn, i) => {
94
120
  const right = 12 + i * 44;
@@ -96,7 +122,7 @@ export default function TreeView({ struct, expression, selector = 'canvas-tree',
96
122
  }), _jsx("div", { id: selector, className: "canvas-tree" }), showInfo && (_jsxs("div", { className: "info", children: [_jsx("span", { children: SentenceUtils.getAsText(struct) }), translations &&
97
123
  Object.entries(translations).map(([k, v]) => (_jsxs("div", { className: "translation", children: [_jsxs("b", { children: [k, ":"] }), _jsx("span", { children: v })] }, k))), struct.parsed && (_jsxs("span", { className: "date", children: [t('date.parsed'), ' ', DateUtils.formatDateTime(struct.parsed, 'dd/MM/yyyy HH:mm:ss')] }))] }))] }));
98
124
  }
99
- const getButtons = (generateImage, reset, downloadPsd, toggleInfo, onExpand, expression) => [
125
+ const getButtons = (generateImage, reset, downloadPsd, toggleInfo, handleExpand, onExpand, expression) => [
100
126
  {
101
127
  condition: true,
102
128
  title: 'button.download.tree',
@@ -119,7 +145,7 @@ const getButtons = (generateImage, reset, downloadPsd, toggleInfo, onExpand, exp
119
145
  {
120
146
  condition: !!onExpand,
121
147
  title: 'button.expand.tree',
122
- onClick: onExpand,
148
+ onClick: handleExpand,
123
149
  icon: faUpRightAndDownLeftFromCenter,
124
150
  },
125
151
  {
@@ -1,2 +1,3 @@
1
+ import cytoscape from 'cytoscape';
1
2
  declare const defaultStylesheet: Array<cytoscape.StylesheetCSS>;
2
3
  export default defaultStylesheet;
@@ -13,7 +13,6 @@ const defaultStylesheet = [
13
13
  {
14
14
  selector: 'node',
15
15
  style: {
16
- content: 'data(label)',
17
16
  'background-color': '#FFF',
18
17
  width: '50px',
19
18
  height: '40px',
@@ -21,7 +20,6 @@ const defaultStylesheet = [
21
20
  'text-max-width': 120,
22
21
  'text-valign': 'center',
23
22
  'text-halign': 'center',
24
- 'font-size': 18,
25
23
  shape: 'rectangle',
26
24
  cursor: 'pointer',
27
25
  },
@@ -31,9 +31,9 @@ export default class CytoscapeTreeConverter {
31
31
  const attrLines = Object.values(token.attributes)
32
32
  .map((v) => v)
33
33
  .join('\n');
34
- return `${token.v}\n${attrLines}`;
34
+ return `<span class="token-value">${token.v}</span><span class="token-attributes">${attrLines}</span>`;
35
35
  }
36
- return token.v;
36
+ return `<span class="token-value">${token.v}</span>`;
37
37
  }
38
38
  validate(struct) {
39
39
  if (!struct.chunks || struct.chunks.length === 0) {
@@ -1,5 +1,7 @@
1
1
  import cytoscape from 'cytoscape';
2
2
  import defaultStylesheet from './CytoscapeStylesheet';
3
+ import nodeHtmlLabel from 'cytoscape-node-edge-html-label';
4
+ nodeHtmlLabel(cytoscape);
3
5
  const init = ({ selector, tree, dagre = false, stylesheet = defaultStylesheet, wheelSensitivity = 1.5, onReady, }) => {
4
6
  destroy(selector);
5
7
  const cy = cytoscape({
@@ -32,6 +34,18 @@ const init = ({ selector, tree, dagre = false, stylesheet = defaultStylesheet, w
32
34
  pixelRatio: 1,
33
35
  });
34
36
  cy.ready(() => {
37
+ if (!cy._htmlLabelsApplied) {
38
+ // @ts-ignore (no types)
39
+ cy.nodeHtmlLabel([
40
+ {
41
+ query: 'node',
42
+ halign: 'center',
43
+ valign: 'center',
44
+ tpl: (data) => `<div class="token-data">${data.label}</div>`,
45
+ },
46
+ ]);
47
+ cy._htmlLabelsApplied = true; // mark as applied
48
+ }
35
49
  cy.fit();
36
50
  cy.center();
37
51
  dagre && cy.layout({ name: 'dagre' }).run();
@@ -40,4 +40,18 @@
40
40
  @include helper-medium-1;
41
41
  }
42
42
  }
43
+
44
+ .token-data {
45
+ text-align: center;
46
+ .token-value {
47
+ display: block;
48
+ font-weight: bolder;
49
+ font-size: 18px;
50
+ }
51
+
52
+ .token-attributes {
53
+ display: block;
54
+ color: red;
55
+ }
56
+ }
43
57
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tycho-components",
3
3
  "private": false,
4
- "version": "0.2.4-SNAPSHOT-2",
4
+ "version": "0.2.4-SNAPSHOT-4",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "exports": {
@@ -24,17 +24,19 @@
24
24
  "axios": "^1.7.7",
25
25
  "classnames": "^2.5.1",
26
26
  "cytoscape": "^3.28.1",
27
+ "cytoscape-node-edge-html-label": "^1.0.6",
27
28
  "date-fns": "^2.29.1",
28
29
  "date-fns-tz": "^1.3.6",
29
30
  "file-saver": "^2.0.5",
31
+ "html2canvas": "^1.4.1",
30
32
  "js-cookie": "^3.0.5",
31
33
  "react-colorful": "^5.6.1",
32
34
  "react-easy-edit": "^2.0.0",
33
- "react-simple-keyboard": "^3.8.66",
34
- "simple-keyboard-layouts": "^3.4.82",
35
35
  "react-loading": "^2.0.3",
36
+ "react-simple-keyboard": "^3.8.66",
36
37
  "react-switch": "^7.1.0",
37
- "react-toastify": "^9.1.3"
38
+ "react-toastify": "^9.1.3",
39
+ "simple-keyboard-layouts": "^3.4.82"
38
40
  },
39
41
  "peerDependencies": {
40
42
  "@emotion/react": "^11.13.3",
@@ -64,7 +66,7 @@
64
66
  "@storybook/react": "^7.5.3",
65
67
  "@storybook/react-vite": "^7.5.3",
66
68
  "@storybook/testing-library": "^0.2.2",
67
- "@types/cytoscape": "^3.19.15",
69
+ "@types/cytoscape": "^3.21.9",
68
70
  "@types/file-saver": "^2.0.5",
69
71
  "@types/js-cookie": "^3.0.6",
70
72
  "@types/react": "^18.2.0",