tycho-components 0.40.5 → 0.40.7
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.
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EdgeDefinition, NodeDefinition, Position } from 'cytoscape';
|
|
2
2
|
import { Conllu, ConlluToken, Struct } from '../../configs';
|
|
3
|
+
import { ConlluViewerDependency, ConlluViewerGraph, ConlluViewerItem } from './ConlluViewerGraph';
|
|
3
4
|
export type CytoscapeTree = {
|
|
4
5
|
nodes: NodeDefinition[];
|
|
5
6
|
edges: EdgeDefinition[];
|
|
@@ -15,5 +16,10 @@ declare const ConlluUtils: {
|
|
|
15
16
|
isNotEmptyConllu: (conllu: Conllu) => boolean;
|
|
16
17
|
deleteToken: (conllu: Conllu, tokenIndex: number) => Conllu;
|
|
17
18
|
getConlluTranslationFromStruct: (struct: Struct | undefined, lang: string) => string;
|
|
19
|
+
generateImageFromSVG: (svgElement: SVGSVGElement) => void;
|
|
20
|
+
isActiveDependency: (dep: ConlluViewerDependency, activeItem: ConlluViewerItem | undefined) => boolean;
|
|
21
|
+
isActiveItem: (item: ConlluViewerItem, activeItem: ConlluViewerItem | undefined) => boolean;
|
|
22
|
+
calculateAnchor: (graph: ConlluViewerGraph, i1: number, i2: number, lvl: number) => number;
|
|
23
|
+
escapeHTML: (str: string) => string;
|
|
18
24
|
};
|
|
19
25
|
export default ConlluUtils;
|
|
@@ -1,3 +1,49 @@
|
|
|
1
|
+
import { saveAs } from 'file-saver';
|
|
2
|
+
const escapeHTML = (str) => {
|
|
3
|
+
return str || '';
|
|
4
|
+
// return str.replace(/[&<>"'`]/g, (match) => {
|
|
5
|
+
// const escapeMap: Record<string, string> = {
|
|
6
|
+
// '&': '&',
|
|
7
|
+
// '<': '<',
|
|
8
|
+
// '>': '>',
|
|
9
|
+
// '"': '"',
|
|
10
|
+
// "'": ''',
|
|
11
|
+
// '`': '`',
|
|
12
|
+
// };
|
|
13
|
+
// return escapeMap[match];
|
|
14
|
+
// });
|
|
15
|
+
};
|
|
16
|
+
const calculateAnchor = (graph, i1, i2, lvl) => {
|
|
17
|
+
const a = graph.anchors[i1];
|
|
18
|
+
if (a.length === 1) {
|
|
19
|
+
if (i1 < i2) {
|
|
20
|
+
return 0.75;
|
|
21
|
+
}
|
|
22
|
+
return 0.25;
|
|
23
|
+
}
|
|
24
|
+
let n = 0;
|
|
25
|
+
for (let i = 0; i < a.length; i++) {
|
|
26
|
+
const v = a[i];
|
|
27
|
+
if (v.dist === i2 - i1 && v.level === lvl) {
|
|
28
|
+
n = i;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return (n + 0.5) / a.length;
|
|
33
|
+
};
|
|
34
|
+
const isActiveDependency = (dep, activeItem) => {
|
|
35
|
+
if (!activeItem)
|
|
36
|
+
return false;
|
|
37
|
+
return (dep.headpos === Number(activeItem.here) ||
|
|
38
|
+
dep.end === Number(activeItem.here));
|
|
39
|
+
};
|
|
40
|
+
const isActiveItem = (item, activeItem) => {
|
|
41
|
+
if (!activeItem)
|
|
42
|
+
return false;
|
|
43
|
+
return (activeItem.here === item.there ||
|
|
44
|
+
activeItem.there === item.here ||
|
|
45
|
+
activeItem.lineno === item.lineno);
|
|
46
|
+
};
|
|
1
47
|
const convertStructToConllu = (struct) => {
|
|
2
48
|
return {
|
|
3
49
|
uid: struct.uid,
|
|
@@ -224,6 +270,37 @@ const extractConlluTokens = (conllText) => {
|
|
|
224
270
|
const getConlluTranslationFromStruct = (struct, lang) => {
|
|
225
271
|
return struct?.attributes?.[lang] || '';
|
|
226
272
|
};
|
|
273
|
+
const generateImageFromSVG = (svgElement) => {
|
|
274
|
+
const canvas = document.createElement('canvas');
|
|
275
|
+
const context = canvas.getContext('2d');
|
|
276
|
+
if (!context) {
|
|
277
|
+
throw new Error('Unable to get canvas context');
|
|
278
|
+
}
|
|
279
|
+
const { width, height } = svgElement.getBBox();
|
|
280
|
+
canvas.width = width;
|
|
281
|
+
canvas.height = height;
|
|
282
|
+
// Convert SVG element to data URL
|
|
283
|
+
const svgData = new XMLSerializer().serializeToString(svgElement);
|
|
284
|
+
const svgBlob = new Blob([svgData], {
|
|
285
|
+
type: 'image/svg+xml;charset=utf-8',
|
|
286
|
+
});
|
|
287
|
+
const url = URL.createObjectURL(svgBlob);
|
|
288
|
+
const img = new Image();
|
|
289
|
+
img.crossOrigin = 'anonymous'; // handle cross-origin issues
|
|
290
|
+
img.onload = async () => {
|
|
291
|
+
context.fillStyle = 'white';
|
|
292
|
+
context.fillRect(0, 0, width, height);
|
|
293
|
+
context.drawImage(img, 0, 0, width, height);
|
|
294
|
+
URL.revokeObjectURL(url);
|
|
295
|
+
// Convert canvas to JPEG blob
|
|
296
|
+
canvas.toBlob((blob) => {
|
|
297
|
+
if (blob === null)
|
|
298
|
+
return;
|
|
299
|
+
saveAs(blob, 'conllu.jpg');
|
|
300
|
+
}, 'image/jpeg', 1.0);
|
|
301
|
+
};
|
|
302
|
+
img.src = url;
|
|
303
|
+
};
|
|
227
304
|
const ConlluUtils = {
|
|
228
305
|
convertToSentence,
|
|
229
306
|
convertToConllu,
|
|
@@ -234,5 +311,10 @@ const ConlluUtils = {
|
|
|
234
311
|
isNotEmptyConllu,
|
|
235
312
|
deleteToken,
|
|
236
313
|
getConlluTranslationFromStruct,
|
|
314
|
+
generateImageFromSVG,
|
|
315
|
+
isActiveDependency,
|
|
316
|
+
isActiveItem,
|
|
317
|
+
calculateAnchor,
|
|
318
|
+
escapeHTML,
|
|
237
319
|
};
|
|
238
320
|
export default ConlluUtils;
|