mirror of
				https://github.com/ae-utbm/sith.git
				synced 2025-10-31 00:53:08 +00:00 
			
		
		
		
	Improve tooltips performance
This commit is contained in:
		| @@ -8,6 +8,8 @@ import { type Placement, computePosition } from "@floating-ui/dom"; | |||||||
|  * You can add `-start` and `-end` to all allowed placement values |  * You can add `-start` and `-end` to all allowed placement values | ||||||
|  **/ |  **/ | ||||||
|  |  | ||||||
|  | const tooltips = new Map(); | ||||||
|  |  | ||||||
| function getPlacement(element: HTMLElement): Placement { | function getPlacement(element: HTMLElement): Placement { | ||||||
|   const position = element.getAttribute("position"); |   const position = element.getAttribute("position"); | ||||||
|   if (position) { |   if (position) { | ||||||
| @@ -16,17 +18,20 @@ function getPlacement(element: HTMLElement): Placement { | |||||||
|   return "bottom"; |   return "bottom"; | ||||||
| } | } | ||||||
|  |  | ||||||
| function getTooltip(element: HTMLElement) { | function createTooltip(element: HTMLElement) { | ||||||
|   for (const tooltip of document.body.getElementsByClassName("tooltip")) { |  | ||||||
|     if (tooltip.textContent === element.getAttribute("tooltip")) { |  | ||||||
|       return tooltip as HTMLElement; |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   const tooltip = document.createElement("div"); |   const tooltip = document.createElement("div"); | ||||||
|   document.body.append(tooltip); |   document.body.append(tooltip); | ||||||
|   tooltip.classList.add("tooltip"); |   tooltip.classList.add("tooltip"); | ||||||
|   tooltip.innerText = element.getAttribute("tooltip"); |   tooltip.innerText = element.getAttribute("tooltip"); | ||||||
|  |   tooltips.set(element, tooltip); | ||||||
|  |   return tooltip; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function getTooltip(element: HTMLElement) { | ||||||
|  |   const tooltip = tooltips.get(element); | ||||||
|  |   if (tooltip === undefined) { | ||||||
|  |     return createTooltip(element); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   return tooltip; |   return tooltip; | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user