-
-
Notifications
You must be signed in to change notification settings - Fork 99
Add a context menu in webmap #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jedrek0429
wants to merge
27
commits into
granny:v3
Choose a base branch
from
jedrek0429:feat/hijack-context-menu
base: v3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
af631b9
Add a context menu in webmap
jedrek0429 3d07eca
Merge branch 'v3' into feat/hijack-context-menu
granny 0077e66
whitespace
granny 7abf48e
Merge branch 'granny:v3' into feat/hijack-context-menu
jedrek0429 94fa8ce
Add more configurability and custom html for the context menus
jedrek0429 a85b820
Apply suggestions from code review
jedrek0429 fb379ea
Merge branch 'v3' into feat/hijack-context-menu
granny 9af1026
make the context menu items a button
granny a8dc6a6
make contextmenu a flexbox
granny 693aa6f
make each coordinate a variable
granny ce169e1
take inspiration from LiveAtlas' contextmenu
granny 3b859f5
remove custom html
granny 1378fef
remove unused onRemove parameter
granny 6d2c66c
remove unused import
granny 3b0cf8b
Merge branch 'v3' into feat/hijack-context-menu
granny adb03e1
update copy-coords value in lang file
granny c6b7bba
update copy-coords value in lang file x2
granny 5649794
Merge branch 'v3' into feat/hijack-context-menu
granny b2e3077
move display flex into leaflet-control-contextmenu class
granny d8aa273
use String constructor
granny 26450a2
remove unused import
granny c6bdd76
remove dash from id
granny eff01b7
content -> context
granny ac770d0
bleugh
granny 78ef803
Merge branch 'v3' into feat/hijack-context-menu
granny cdd801c
replace the switch case with a map that "gets" the item if set in config
granny c1ba1fb
whitespace
granny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import * as L from "leaflet"; | ||
| import {Pl3xMap} from "../Pl3xMap"; | ||
| import {ContextMenuCustomHtml, ContextMenuItemType} from "../settings/WorldSettings"; | ||
|
|
||
| export default class ContextMenuControl extends L.Control { | ||
| private readonly _pl3xmap: Pl3xMap; | ||
| private _dom: HTMLDivElement = L.DomUtil.create('div'); | ||
| private _customHtml: ContextMenuCustomHtml; | ||
|
|
||
|
|
||
| constructor(pl3xmap: Pl3xMap) { | ||
| super(); | ||
| this._pl3xmap = pl3xmap; | ||
| this._customHtml = pl3xmap.worldManager.currentWorld?.settings.ui.contextMenu.customHtml ?? new ContextMenuCustomHtml(); | ||
| if (this._pl3xmap.worldManager.currentWorld?.settings.ui.contextMenu.enabled) { | ||
| this._init(); | ||
| } | ||
| } | ||
|
|
||
| private _init(): void { | ||
| this._pl3xmap.map.on('contextmenu', this._show, this); | ||
| this._pl3xmap.map.on('click', this._hide, this); | ||
| if (this._customHtml.enabled) { | ||
| const style = L.DomUtil.create('style', 'leaflet-control-contextmenu-custom-style', document.head); | ||
| style.innerHTML = this._customHtml.css; | ||
| } | ||
| } | ||
|
|
||
| onAdd(): HTMLDivElement { | ||
| this._dom = L.DomUtil.create('div', 'leaflet-control leaflet-control-contextmenu'); | ||
| this._dom.dataset.label = this._pl3xmap.settings!.lang.contextMenu.label; | ||
| if (this._customHtml.enabled) { | ||
| this._dom.innerHTML = this._customHtml.html; | ||
| } | ||
| return this._dom; | ||
| } | ||
|
|
||
| private _show(event: L.LeafletMouseEvent): void { | ||
| L.DomEvent.stopPropagation(event); | ||
| if (!this._customHtml.enabled) { | ||
| this._dom.innerHTML = ''; | ||
| this._getItems(event).forEach((item) => { | ||
| const menuItem = L.DomUtil.create('div', 'leaflet-control-contextmenu-item', this._dom); | ||
| menuItem.innerHTML = item.label; | ||
| L.DomEvent.on(menuItem, 'click', (e) => { | ||
| L.DomEvent.stopPropagation(e); | ||
| item.callback(); | ||
| this._hide(); | ||
| }); | ||
| }); | ||
| } | ||
| this._dom.style.display = 'block'; | ||
| this._dom.style.left = event.containerPoint.x + 'px'; | ||
| this._dom.style.top = event.containerPoint.y + 'px'; | ||
| } | ||
|
|
||
| private _hide(): void { | ||
| this._dom.style.display = 'none'; | ||
| } | ||
|
|
||
| private _getItems(e: L.LeafletMouseEvent): Map<string, { label: string, callback: () => void }> { | ||
| const {x, y, z} = this._pl3xmap.controlManager.coordsControl ?? {x: 0, y: 0, z: 0}; | ||
| const coords = `(${x}, ${y ?? '???'}, ${z})`; | ||
| const world = this._pl3xmap.worldManager.currentWorld; | ||
| const settings = world?.settings.ui.contextMenu; | ||
| const items: Map<string, { label: string, callback: () => void }> = new Map(); | ||
|
|
||
| settings?.items.forEach((item) => { | ||
| switch (item) { | ||
| case ContextMenuItemType.copyCoords: | ||
| items.set('copyCoords', { | ||
| label: `${this._pl3xmap.settings!.lang.contextMenu.copyCoords} ${coords}`, | ||
| callback: () => navigator.clipboard.writeText(coords), | ||
| }); | ||
| break; | ||
| case ContextMenuItemType.copyLink: | ||
| items.set('copyLink', { | ||
| label: this._pl3xmap.settings!.lang.contextMenu.copyLink, | ||
| callback: () => navigator.clipboard.writeText( | ||
| window.location.href + | ||
| this._pl3xmap.controlManager.linkControl?.getUrlFromCoords( | ||
| x, | ||
| z, | ||
| this._pl3xmap.map.getCurrentZoom(), | ||
| world | ||
| ) | ||
| ), | ||
| }); | ||
| break; | ||
| case ContextMenuItemType.centerMap: | ||
| items.set('centerMap', { | ||
| label: this._pl3xmap.settings!.lang.contextMenu.centerMap, | ||
| callback: () => this._pl3xmap.map.panTo(e.latlng), | ||
| }); | ||
| break; | ||
| } | ||
| }); | ||
|
|
||
| return items; | ||
| } | ||
| } | ||
jedrek0429 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.