diff options
Diffstat (limited to 'src/dumbymap.mjs')
-rw-r--r-- | src/dumbymap.mjs | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/dumbymap.mjs b/src/dumbymap.mjs index 4cfc484..81a65ca 100644 --- a/src/dumbymap.mjs +++ b/src/dumbymap.mjs | |||
@@ -11,6 +11,7 @@ import * as menuItem from './MenuItem' | |||
11 | import PlainModal from 'plain-modal' | 11 | import PlainModal from 'plain-modal' |
12 | import proj4 from 'proj4' | 12 | import proj4 from 'proj4' |
13 | import { register, fromEPSGCode } from 'ol/proj/proj4' | 13 | import { register, fromEPSGCode } from 'ol/proj/proj4' |
14 | import LeaderLine from 'leader-line' | ||
14 | 15 | ||
15 | /** CSS Selector for main components */ | 16 | /** CSS Selector for main components */ |
16 | const mapBlockSelector = 'pre:has(.language-map)' | 17 | const mapBlockSelector = 'pre:has(.language-map)' |
@@ -587,5 +588,85 @@ export const generateMaps = (container, { | |||
587 | document.removeEventListener('click', actionOutsideMenu), | 588 | document.removeEventListener('click', actionOutsideMenu), |
588 | ) | 589 | ) |
589 | 590 | ||
591 | /** Drag/Drop on map for new GeoLink */ | ||
592 | container.onmousedown = (e) => { | ||
593 | // Check should start drag event for GeoLink | ||
594 | const selection = document.getSelection() | ||
595 | if (e.which !== 1 || selection.type !== 'Range') return | ||
596 | |||
597 | const range = selection.getRangeAt(0) | ||
598 | const originContent = range.cloneContents() | ||
599 | const rect = range.getBoundingClientRect() | ||
600 | const mouseInRange = e.x < rect.right && e.x > rect.left && e.y < rect.bottom && e.y > rect.top | ||
601 | if (!mouseInRange) return | ||
602 | |||
603 | const geoLink = document.createElement('a') | ||
604 | geoLink.textContent = range.toString() | ||
605 | geoLink.classList.add('with-leader-line', 'geolink', 'drag') | ||
606 | range.deleteContents() | ||
607 | range.insertNode(geoLink) | ||
608 | |||
609 | const lineEnd = document.createElement('div') | ||
610 | lineEnd.className = 'arrow-head' | ||
611 | const offsetRect = container.getBoundingClientRect() | ||
612 | lineEnd.style.cssText = ` | ||
613 | position: absolute; | ||
614 | padding: 5px; | ||
615 | left: ${e.clientX}px; | ||
616 | top: ${e.clientY}px; | ||
617 | transform: translate(-${offsetRect.left + 5}px, -${offsetRect.top + 5}px);` | ||
618 | container.appendChild(lineEnd) | ||
619 | |||
620 | const line = new LeaderLine({ | ||
621 | start: geoLink, | ||
622 | end: lineEnd, | ||
623 | path: 'magnet', | ||
624 | }) | ||
625 | |||
626 | function onMouseMove (event) { | ||
627 | lineEnd.style.left = event.clientX + 'px' | ||
628 | lineEnd.style.top = event.clientY + 'px' | ||
629 | line.position() | ||
630 | |||
631 | // TODO Scroll dumbymap.htmlHolder when cursor is at upper/lower side | ||
632 | } | ||
633 | |||
634 | container.classList.add('dragging-geolink') | ||
635 | container.onmousemove = onMouseMove | ||
636 | container.onmouseup = function (e) { | ||
637 | container.classList.remove('dragging-geolink') | ||
638 | container.onmousemove = null | ||
639 | container.onmouseup = null | ||
640 | geoLink.classList.remove('drag') | ||
641 | line?.remove() | ||
642 | lineEnd.remove() | ||
643 | const resumeContent = () => { | ||
644 | range.deleteContents() | ||
645 | range.insertNode(originContent) | ||
646 | } | ||
647 | |||
648 | const map = document.elementFromPoint(e.clientX, e.clientY) | ||
649 | .closest('.mapclay[data-render="fulfilled"]') | ||
650 | if (!map) { | ||
651 | resumeContent('map/selection') | ||
652 | return | ||
653 | } | ||
654 | |||
655 | const refLink = utils.addAnchorByPoint({ | ||
656 | defaultName: geoLink.textContent, | ||
657 | point: e, | ||
658 | map, | ||
659 | }) | ||
660 | if (!refLink) { | ||
661 | resumeContent('reflink') | ||
662 | return | ||
663 | } | ||
664 | |||
665 | geoLink.href = refLink.link | ||
666 | utils.createGeoLink(geoLink) | ||
667 | } | ||
668 | } | ||
669 | container.ondragstart = () => false | ||
670 | |||
590 | return Object.seal(dumbymap) | 671 | return Object.seal(dumbymap) |
591 | } | 672 | } |