diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2024-10-02 15:07:01 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2024-10-02 15:07:01 +0800 |
| commit | 045e55ce0547544e064f09f87bd1f75d7fa088b0 (patch) | |
| tree | 7170010388ddaaf3be7cab647f427ad398bf4b5a /src | |
| parent | 061a9e7de3f3426225f73e3cbd59942c1ceae2c8 (diff) | |
feat: prevent menu show outside of window
Diffstat (limited to 'src')
| -rw-r--r-- | src/MenuItem.mjs | 3 | ||||
| -rw-r--r-- | src/css/dumbymap.css | 3 | ||||
| -rw-r--r-- | src/dumbymap.mjs | 4 | ||||
| -rw-r--r-- | src/utils.mjs | 7 |
4 files changed, 14 insertions, 3 deletions
diff --git a/src/MenuItem.mjs b/src/MenuItem.mjs index e1cb582..a7724d3 100644 --- a/src/MenuItem.mjs +++ b/src/MenuItem.mjs | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | import { shiftByWindow } from './utils.mjs' | ||
| 2 | |||
| 1 | class Item extends window.HTMLDivElement { | 3 | class Item extends window.HTMLDivElement { |
| 2 | constructor ({ text, innerHTML, onclick, style }) { | 4 | constructor ({ text, innerHTML, onclick, style }) { |
| 3 | super() | 5 | super() |
| @@ -35,6 +37,7 @@ class Folder extends window.HTMLDivElement { | |||
| 35 | .querySelectorAll('.sub-menu') | 37 | .querySelectorAll('.sub-menu') |
| 36 | .forEach(sub => sub.remove()) | 38 | .forEach(sub => sub.remove()) |
| 37 | this.appendChild(submenu) | 39 | this.appendChild(submenu) |
| 40 | shiftByWindow(submenu) | ||
| 38 | } | 41 | } |
| 39 | } | 42 | } |
| 40 | } | 43 | } |
diff --git a/src/css/dumbymap.css b/src/css/dumbymap.css index 3294e40..b3ff877 100644 --- a/src/css/dumbymap.css +++ b/src/css/dumbymap.css | |||
| @@ -267,7 +267,6 @@ p a[href^='http']::after, | |||
| 267 | padding: 0.5rem; | 267 | padding: 0.5rem; |
| 268 | 268 | ||
| 269 | position: relative; | 269 | position: relative; |
| 270 | z-index: 9999; | ||
| 271 | 270 | ||
| 272 | cursor: pointer; | 271 | cursor: pointer; |
| 273 | text-wrap: nowrap; | 272 | text-wrap: nowrap; |
| @@ -299,7 +298,7 @@ p a[href^='http']::after, | |||
| 299 | max-height: 40vh; | 298 | max-height: 40vh; |
| 300 | 299 | ||
| 301 | position: absolute; | 300 | position: absolute; |
| 302 | z-index: 100; | 301 | z-index: 10000; |
| 303 | 302 | ||
| 304 | border: 2px solid gray; | 303 | border: 2px solid gray; |
| 305 | border-radius: 6px; | 304 | border-radius: 6px; |
diff --git a/src/dumbymap.mjs b/src/dumbymap.mjs index 0e12fb4..7e04501 100644 --- a/src/dumbymap.mjs +++ b/src/dumbymap.mjs | |||
| @@ -5,7 +5,7 @@ import MarkdownItFrontMatter from 'markdown-it-front-matter' | |||
| 5 | import MarkdownItTocDoneRight from 'markdown-it-toc-done-right' | 5 | import MarkdownItTocDoneRight from 'markdown-it-toc-done-right' |
| 6 | import LeaderLine from 'leader-line' | 6 | import LeaderLine from 'leader-line' |
| 7 | import { renderWith, defaultAliases, parseConfigsFromYaml } from 'mapclay' | 7 | import { renderWith, defaultAliases, parseConfigsFromYaml } from 'mapclay' |
| 8 | import { onRemove, animateRectTransition, throttle } from './utils' | 8 | import { onRemove, animateRectTransition, throttle, shiftByWindow } from './utils' |
| 9 | import { Layout, SideBySide, Overlay } from './Layout' | 9 | import { Layout, SideBySide, Overlay } from './Layout' |
| 10 | import * as utils from './dumbyUtils' | 10 | import * as utils from './dumbyUtils' |
| 11 | import * as menuItem from './MenuItem' | 11 | import * as menuItem from './MenuItem' |
| @@ -498,6 +498,8 @@ export const generateMaps = (container, { delay, mapCallback }) => { | |||
| 498 | menu.appendChild(menuItem.pickBlockItem(dumbymap)) | 498 | menu.appendChild(menuItem.pickBlockItem(dumbymap)) |
| 499 | menu.appendChild(menuItem.pickLayoutItem(dumbymap)) | 499 | menu.appendChild(menuItem.pickLayoutItem(dumbymap)) |
| 500 | } | 500 | } |
| 501 | |||
| 502 | shiftByWindow(menu) | ||
| 501 | } | 503 | } |
| 502 | 504 | ||
| 503 | // Remove menu when click outside | 505 | // Remove menu when click outside |
diff --git a/src/utils.mjs b/src/utils.mjs index d9ed2d7..c9a2457 100644 --- a/src/utils.mjs +++ b/src/utils.mjs | |||
| @@ -87,3 +87,10 @@ export function throttle (func, delay) { | |||
| 87 | return func.call(context, ...args) | 87 | return func.call(context, ...args) |
| 88 | } | 88 | } |
| 89 | } | 89 | } |
| 90 | |||
| 91 | export const shiftByWindow = element => { | ||
| 92 | const rect = element.getBoundingClientRect() | ||
| 93 | const offsetX = window.innerWidth - rect.left - rect.width | ||
| 94 | const offsetY = window.innerHeight - rect.top - rect.height | ||
| 95 | element.style.transform = `translate(${offsetX < 0 ? offsetX : 0}px, ${offsetY < 0 ? offsetY : 0}px)` | ||
| 96 | } | ||