@abi-software/flatmap-viewer 2.4.3 → 2.4.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.
package/README.rst CHANGED
@@ -38,7 +38,7 @@ The map server endpoint is specified as ``MAP_ENDPOINT`` in ``src/main.js``. It
38
38
  Package Installation
39
39
  ====================
40
40
 
41
- * ``npm install @abi-software/flatmap-viewer@2.4.3``
41
+ * ``npm install @abi-software/flatmap-viewer@2.4.4``
42
42
 
43
43
  Documentation
44
44
  -------------
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abi-software/flatmap-viewer",
3
- "version": "2.4.3",
3
+ "version": "2.4.4",
4
4
  "description": "Flatmap viewer using Maplibre GL",
5
5
  "repository": "https://github.com/AnatomicMaps/flatmap-viewer.git",
6
6
  "main": "src/main.js",
@@ -26,6 +26,7 @@
26
26
  "bezier-js": "^6.1.0",
27
27
  "html-es6cape": "^2.0.2",
28
28
  "maplibre-gl": ">=3.6.0",
29
+ "mathjax-full": "^3.2.2",
29
30
  "minisearch": "^2.2.1",
30
31
  "polylabel": "^1.1.0"
31
32
  },
@@ -45,6 +45,7 @@ import {PathControl} from './controls/paths';
45
45
  import {SearchControl} from './controls/search';
46
46
  import {SystemsControl} from './controls/systems';
47
47
  import {TaxonsControl} from './controls/taxons';
48
+ import {latex2Svg} from './mathjax';
48
49
 
49
50
  import * as utils from './utils';
50
51
 
@@ -92,6 +93,31 @@ function expandBounds(bbox1, bbox2, padding)
92
93
 
93
94
  //==============================================================================
94
95
 
96
+ function labelPosition(feature)
97
+ {
98
+ const polygon = feature.geometry.coordinates;
99
+ // Rough heuristic. Area is in km^2; below appears to be good enough.
100
+ const precision = ('area' in feature.properties)
101
+ ? Math.sqrt(feature.properties.area)/500000
102
+ : 0.1;
103
+ return polylabel(polygon, precision);
104
+ }
105
+
106
+ //==============================================================================
107
+
108
+ function getRenderedLabel(properties)
109
+ {
110
+ if (!('renderedLabel' in properties)) {
111
+ const label = ('label' in properties) ? (properties.label.substr(0, 1).toUpperCase()
112
+ + properties.label.substr(1)).replaceAll("\n", "<br/>")
113
+ : '';
114
+ properties.renderedLabel = label.replaceAll(/`\$([^\$]*)\$`/g, math => latex2Svg(math.slice(2, -2)));
115
+ }
116
+ return properties.renderedLabel;
117
+ }
118
+
119
+ //==============================================================================
120
+
95
121
  export class UserInteractions
96
122
  {
97
123
  constructor(flatmap)
@@ -350,9 +376,12 @@ export class UserInteractions
350
376
  __featureEnabled(feature)
351
377
  //=======================
352
378
  {
353
- const state = this._map.getFeatureState(feature);
354
- return (state !== undefined
355
- && (!('hidden' in state) || !state.hidden));
379
+ if (feature.id) {
380
+ const state = this._map.getFeatureState(feature);
381
+ return (state !== undefined
382
+ && (!('hidden' in state) || !state.hidden));
383
+ }
384
+ return false;
356
385
  }
357
386
 
358
387
  featureSelected_(featureId)
@@ -668,7 +697,6 @@ export class UserInteractions
668
697
  tooltips.push(`<div class="feature-error">Warning: ${properties.warning}</div>`)
669
698
  }
670
699
  if ('label' in properties && (!('tooltip' in properties) || properties.tooltip)) {
671
- let tooltip = '';
672
700
  const label = properties.label;
673
701
  const cleanLabel = (label.substr(0, 1).toUpperCase() + label.substr(1)).replaceAll("\n", "<br/>");
674
702
  if (!tooltips.includes(cleanLabel)) {
@@ -690,19 +718,18 @@ export class UserInteractions
690
718
  if ('warning' in properties) {
691
719
  tooltip.push(`<div class="feature-error">Warning: ${properties.warning}</div>`)
692
720
  }
721
+ let renderedLabel;
693
722
  if (('label' in properties || 'hyperlink' in properties)
694
723
  && (forceLabel || !('tooltip' in properties) || properties.tooltip)) {
695
- const label = ('label' in properties) ? (properties.label.substr(0, 1).toUpperCase()
696
- + properties.label.substr(1)).replaceAll("\n", "<br/>")
697
- : '';
724
+ const renderedLabel = getRenderedLabel(properties);
698
725
  if ('hyperlink' in properties) {
699
- if (label === '') {
726
+ if (renderedLabel === '') {
700
727
  tooltip.push(`<a href='${properties.hyperlink}'>${properties.hyperlink}</a>`);
701
728
  } else {
702
- tooltip.push(`<a href='${properties.hyperlink}'>${label}</a></div>`);
729
+ tooltip.push(`<a href='${properties.hyperlink}'>${renderedLabel}</a></div>`);
703
730
  }
704
731
  } else {
705
- tooltip.push(label);
732
+ tooltip.push(renderedLabel);
706
733
  }
707
734
  }
708
735
  return (tooltip.length === 0) ? ''
@@ -1078,13 +1105,7 @@ export class UserInteractions
1078
1105
  ]
1079
1106
  });
1080
1107
  if (features.length > 0) {
1081
- const feature = features[0];
1082
- const polygon = feature.geometry.coordinates;
1083
- // Rough heuristic. Area is in km^2; below appears to be good enough.
1084
- const precision = ('area' in feature.properties)
1085
- ? Math.sqrt(feature.properties.area)/500000
1086
- : 0.1;
1087
- position = polylabel(polygon, precision);
1108
+ position = labelPosition(features[0]);
1088
1109
  }
1089
1110
  }
1090
1111
  this.__markerPositions.set(featureId, position);
package/src/mathjax.js ADDED
@@ -0,0 +1,100 @@
1
+
2
+ /******************************************************************************
3
+
4
+ Flatmap viewer and annotation tool
5
+
6
+ Copyright (c) 2019-2023 David Brooks
7
+
8
+ Licensed under the Apache License, Version 2.0 (the "License");
9
+ you may not use this file except in compliance with the License.
10
+ You may obtain a copy of the License at
11
+
12
+ http://www.apache.org/licenses/LICENSE-2.0
13
+
14
+ Unless required by applicable law or agreed to in writing, software
15
+ distributed under the License is distributed on an "AS IS" BASIS,
16
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ See the License for the specific language governing permissions and
18
+ limitations under the License.
19
+
20
+ ******************************************************************************/
21
+
22
+ /*************************************************************************
23
+ *
24
+ * direct/tex2svg
25
+ *
26
+ * Uses MathJax v3 to convert a TeX string to an SVG string.
27
+ *
28
+ * ----------------------------------------------------------------------
29
+ *
30
+ * Copyright (c) 2018 The MathJax Consortium
31
+ *
32
+ * Licensed under the Apache License, Version 2.0 (the "License");
33
+ * you may not use this file except in compliance with the License.
34
+ * You may obtain a copy of the License at
35
+ *
36
+ * http://www.apache.org/licenses/LICENSE-2.0
37
+ *
38
+ * Unless required by applicable law or agreed to in writing, software
39
+ * distributed under the License is distributed on an "AS IS" BASIS,
40
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41
+ * See the License for the specific language governing permissions and
42
+ * limitations under the License.
43
+ */
44
+
45
+ //==============================================================================
46
+
47
+ const EM_SIZE = 16;
48
+ const EX_SIZE = 8;
49
+
50
+ //==============================================================================
51
+
52
+ // Load the packages needed for MathJax
53
+ import {mathjax} from 'mathjax-full/js/mathjax';
54
+ import {TeX} from 'mathjax-full/js/input/tex';
55
+ import {SVG} from 'mathjax-full/js/output/svg';
56
+ import {browserAdaptor} from 'mathjax-full/js/adaptors/browserAdaptor';
57
+ import {RegisterHTMLHandler} from 'mathjax-full/js/handlers/html';
58
+ import {AllPackages} from 'mathjax-full/js/input/tex/AllPackages'; // required to load `textmacros`
59
+
60
+ //==============================================================================
61
+
62
+ // Minimal CSS needed for stand-alone image
63
+ export const LatexStyleRules = [
64
+ 'svg a{fill:blue;stroke:blue}',
65
+ // Round the corners of filled background rectangles
66
+ '[data-mml-node="mstyle"]>rect[data-bgcolor="true"]{rx: 8%; ry: 12%}',
67
+ '[data-mml-node="merror"]>g{fill:red;stroke:red}',
68
+ '[data-mml-node="merror"]>rect[data-background]{fill:yellow;stroke:none}',
69
+ '[data-frame],[data-line]{stroke-width:70px;fill:none}',
70
+ '.mjx-dashed{stroke-dasharray:140}',
71
+ '.mjx-dotted{stroke-linecap:round;stroke-dasharray:0,140}',
72
+ 'use[data-c]{stroke-width:3px}'
73
+ ].join('');
74
+
75
+ //==============================================================================
76
+
77
+ // Create DOM adaptor and register it for HTML documents
78
+ const adaptor = browserAdaptor();
79
+ RegisterHTMLHandler(adaptor);
80
+
81
+ //==============================================================================
82
+
83
+ const tex = new TeX({packages: AllPackages}); // ['base', 'textmacros']});
84
+ const svg = new SVG({fontCache: 'local'});
85
+ const html = mathjax.document('', {InputJax: tex, OutputJax: svg});
86
+
87
+ //==============================================================================
88
+
89
+ export function latex2Svg(latex)
90
+ {
91
+ const node = html.convert(latex, {
92
+ display: false, // process as inline math
93
+ em: 2*EM_SIZE,
94
+ ex: 2*EX_SIZE,
95
+ });
96
+ let result = adaptor.innerHTML(node);
97
+ return result.replace(/<defs>/, `<defs><style>${LatexStyleRules}</style>`);
98
+ }
99
+
100
+ //==============================================================================