source: node_modules/recharts/es6/util/DOMUtils.js@ a762898

Last change on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 4.8 KB
Line 
1function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
2function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
3function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
4function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
5function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
6import { Global } from './Global';
7import { LRUCache } from './LRUCache';
8var defaultConfig = {
9 cacheSize: 2000,
10 enableCache: true
11};
12var currentConfig = _objectSpread({}, defaultConfig);
13var stringCache = new LRUCache(currentConfig.cacheSize);
14var SPAN_STYLE = {
15 position: 'absolute',
16 top: '-20000px',
17 left: 0,
18 padding: 0,
19 margin: 0,
20 border: 'none',
21 whiteSpace: 'pre'
22};
23var MEASUREMENT_SPAN_ID = 'recharts_measurement_span';
24function createCacheKey(text, style) {
25 // Simple string concatenation for better performance than JSON.stringify
26 var fontSize = style.fontSize || '';
27 var fontFamily = style.fontFamily || '';
28 var fontWeight = style.fontWeight || '';
29 var fontStyle = style.fontStyle || '';
30 var letterSpacing = style.letterSpacing || '';
31 var textTransform = style.textTransform || '';
32 return "".concat(text, "|").concat(fontSize, "|").concat(fontFamily, "|").concat(fontWeight, "|").concat(fontStyle, "|").concat(letterSpacing, "|").concat(textTransform);
33}
34
35/**
36 * Measure text using DOM (accurate but slower)
37 * @param text - The text to measure
38 * @param style - CSS style properties to apply
39 * @returns The size of the text
40 */
41var measureTextWithDOM = (text, style) => {
42 try {
43 var measurementSpan = document.getElementById(MEASUREMENT_SPAN_ID);
44 if (!measurementSpan) {
45 measurementSpan = document.createElement('span');
46 measurementSpan.setAttribute('id', MEASUREMENT_SPAN_ID);
47 measurementSpan.setAttribute('aria-hidden', 'true');
48 document.body.appendChild(measurementSpan);
49 }
50
51 // Apply styles directly without unnecessary object creation
52 Object.assign(measurementSpan.style, SPAN_STYLE, style);
53 measurementSpan.textContent = "".concat(text);
54 var rect = measurementSpan.getBoundingClientRect();
55 return {
56 width: rect.width,
57 height: rect.height
58 };
59 } catch (_unused) {
60 return {
61 width: 0,
62 height: 0
63 };
64 }
65};
66export var getStringSize = function getStringSize(text) {
67 var style = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
68 if (text === undefined || text === null || Global.isSsr) {
69 return {
70 width: 0,
71 height: 0
72 };
73 }
74
75 // If caching is disabled, measure directly
76 if (!currentConfig.enableCache) {
77 return measureTextWithDOM(text, style);
78 }
79 var cacheKey = createCacheKey(text, style);
80 var cachedResult = stringCache.get(cacheKey);
81 if (cachedResult) {
82 return cachedResult;
83 }
84
85 // Measure using DOM
86 var result = measureTextWithDOM(text, style);
87
88 // Store in LRU cache
89 stringCache.set(cacheKey, result);
90 return result;
91};
92
93/**
94 * Configure text measurement behavior
95 * @param config - Partial configuration to apply
96 * @returns void
97 */
98export var configureTextMeasurement = config => {
99 var newConfig = _objectSpread(_objectSpread({}, currentConfig), config);
100 if (newConfig.cacheSize !== currentConfig.cacheSize) {
101 stringCache = new LRUCache(newConfig.cacheSize);
102 }
103 currentConfig = newConfig;
104};
105
106/**
107 * Get current text measurement configuration
108 * @returns Current configuration
109 */
110export var getTextMeasurementConfig = () => _objectSpread({}, currentConfig);
111
112/**
113 * Clear the string size cache. Useful for testing or memory management.
114 * @returns void
115 */
116export var clearStringCache = () => {
117 stringCache.clear();
118};
119
120/**
121 * Get cache statistics for debugging purposes.
122 * @returns Cache statistics including size and max size
123 */
124export var getStringCacheStats = () => ({
125 size: stringCache.size(),
126 maxSize: currentConfig.cacheSize
127});
Note: See TracBrowser for help on using the repository browser.