diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2024-10-12 15:51:37 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2024-10-12 16:44:28 +0800 |
| commit | 21e033b45df705aaf3ed5d2cf71a7a838ca04d2b (patch) | |
| tree | 5a5a9d504c4765eb6d23e8d724b8fcbdf5cc9902 /src | |
| parent | 72704e3f5d5f4cbe00640be875955d4649f30780 (diff) | |
feat: highlight targets when hovering DocLink
Diffstat (limited to 'src')
| -rw-r--r-- | src/dumbyUtils.mjs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/dumbyUtils.mjs b/src/dumbyUtils.mjs index 24699b3..a350ee3 100644 --- a/src/dumbyUtils.mjs +++ b/src/dumbyUtils.mjs | |||
| @@ -198,15 +198,26 @@ export const createGeoLink = (link) => { | |||
| 198 | export const createDocLink = link => { | 198 | export const createDocLink = link => { |
| 199 | link.classList.add('with-leader-line', 'doclink') | 199 | link.classList.add('with-leader-line', 'doclink') |
| 200 | link.lines = [] | 200 | link.lines = [] |
| 201 | const label = decodeURIComponent(link.href.split('#')[1]) | ||
| 202 | const selector = link.title.split('=>')[1] ?? '#' + label | ||
| 201 | 203 | ||
| 202 | link.onmouseover = () => { | 204 | link.onmouseover = () => { |
| 203 | const label = decodeURIComponent(link.href.split('#')[1]) | ||
| 204 | const selector = link.title.split('=>')[1] ?? '#' + label | ||
| 205 | const targets = document.querySelectorAll(selector) | 205 | const targets = document.querySelectorAll(selector) |
| 206 | 206 | ||
| 207 | targets.forEach(target => { | 207 | targets.forEach(target => { |
| 208 | if (!target?.checkVisibility()) return | 208 | if (!target?.checkVisibility()) return |
| 209 | 209 | ||
| 210 | // highlight selected target | ||
| 211 | target.dataset.style = target.style.cssText | ||
| 212 | const rect = target.getBoundingClientRect() | ||
| 213 | const isTiny = rect.width < 100 || rect.height < 100 | ||
| 214 | if (isTiny) { | ||
| 215 | target.style.background = 'lightPink' | ||
| 216 | } else { | ||
| 217 | target.style.outline = 'lightPink 6px dashed' | ||
| 218 | } | ||
| 219 | |||
| 220 | // point to selected target | ||
| 210 | const line = new LeaderLine({ | 221 | const line = new LeaderLine({ |
| 211 | start: link, | 222 | start: link, |
| 212 | end: target, | 223 | end: target, |
| @@ -224,6 +235,13 @@ export const createDocLink = link => { | |||
| 224 | link.onmouseout = () => { | 235 | link.onmouseout = () => { |
| 225 | link.lines.forEach(line => line.remove()) | 236 | link.lines.forEach(line => line.remove()) |
| 226 | link.lines.length = 0 | 237 | link.lines.length = 0 |
| 238 | |||
| 239 | // resume targets from highlight | ||
| 240 | const targets = document.querySelectorAll(selector) | ||
| 241 | targets.forEach(target => { | ||
| 242 | target.style.cssText = target.dataset.style | ||
| 243 | delete target.dataset.style | ||
| 244 | }) | ||
| 227 | } | 245 | } |
| 228 | } | 246 | } |
| 229 | 247 | ||