diff options
Diffstat (limited to 'src/dumbymap.mjs')
-rw-r--r-- | src/dumbymap.mjs | 67 |
1 files changed, 31 insertions, 36 deletions
diff --git a/src/dumbymap.mjs b/src/dumbymap.mjs index 4329bbf..a35a2d6 100644 --- a/src/dumbymap.mjs +++ b/src/dumbymap.mjs | |||
@@ -563,84 +563,79 @@ export const generateMaps = (container, { | |||
563 | ) | 563 | ) |
564 | 564 | ||
565 | /** Drag/Drop on map for new GeoLink */ | 565 | /** Drag/Drop on map for new GeoLink */ |
566 | const pointByArrow = document.createElement('div') | ||
567 | pointByArrow.className = 'point-by-arrow' | ||
568 | container.appendChild(pointByArrow) | ||
569 | container.ondragstart = () => false | ||
566 | container.onmousedown = (e) => { | 570 | container.onmousedown = (e) => { |
567 | // Check should start drag event for GeoLink | 571 | // Check should start drag event for GeoLink |
568 | const selection = document.getSelection() | 572 | const selection = document.getSelection() |
569 | if (e.which !== 1 || selection.type !== 'Range') return | 573 | if (e.which !== 1 || selection.type !== 'Range') return |
570 | 574 | ||
575 | // Check if click is inside selection | ||
571 | const range = selection.getRangeAt(0) | 576 | const range = selection.getRangeAt(0) |
572 | const originContent = range.cloneContents() | ||
573 | const rect = range.getBoundingClientRect() | 577 | const rect = range.getBoundingClientRect() |
574 | const mouseInRange = e.x < rect.right && e.x > rect.left && e.y < rect.bottom && e.y > rect.top | 578 | const mouseInRange = e.clientX < rect.right && e.clientX > rect.left && e.clientY < rect.bottom && e.clientY > rect.top |
575 | if (!mouseInRange) return | 579 | if (!mouseInRange) return |
576 | 580 | ||
581 | // link placeholder when dragging | ||
582 | container.classList.add('dragging-geolink') | ||
577 | const geoLink = document.createElement('a') | 583 | const geoLink = document.createElement('a') |
578 | geoLink.textContent = range.toString() | 584 | geoLink.textContent = range.toString() |
579 | geoLink.classList.add('with-leader-line', 'geolink', 'drag') | 585 | geoLink.classList.add('with-leader-line', 'geolink', 'drag') |
586 | |||
587 | // Replace current content with link | ||
588 | const originContent = range.cloneContents() | ||
589 | const resumeContent = () => { | ||
590 | range.deleteContents() | ||
591 | range.insertNode(originContent) | ||
592 | } | ||
580 | range.deleteContents() | 593 | range.deleteContents() |
581 | range.insertNode(geoLink) | 594 | range.insertNode(geoLink) |
582 | 595 | ||
583 | const lineEnd = document.createElement('div') | 596 | // Add leader-line |
584 | lineEnd.className = 'arrow-head' | ||
585 | const offsetRect = container.getBoundingClientRect() | ||
586 | lineEnd.style.cssText = ` | ||
587 | position: absolute; | ||
588 | padding: 5px; | ||
589 | left: ${e.clientX}px; | ||
590 | top: ${e.clientY}px; | ||
591 | transform: translate(-${offsetRect.left + 5}px, -${offsetRect.top + 5}px);` | ||
592 | container.appendChild(lineEnd) | ||
593 | |||
594 | const line = new LeaderLine({ | 597 | const line = new LeaderLine({ |
595 | start: geoLink, | 598 | start: geoLink, |
596 | end: lineEnd, | 599 | end: pointByArrow, |
597 | path: 'magnet', | 600 | path: 'magnet', |
598 | }) | 601 | }) |
599 | 602 | ||
600 | function onMouseMove (event) { | 603 | // Update leader-line with mouse move |
601 | lineEnd.style.left = event.clientX + 'px' | 604 | container.onmousemove = (event) => { |
602 | lineEnd.style.top = event.clientY + 'px' | 605 | const rect = container.getBoundingClientRect() |
606 | pointByArrow.style.left = `${event.clientX - rect.left}px` | ||
607 | pointByArrow.style.top = `${event.clientY - rect.top}px` | ||
603 | line.position() | 608 | line.position() |
604 | 609 | ||
605 | // TODO Scroll dumbymap.htmlHolder when cursor is at upper/lower side | 610 | // TODO Scroll dumbymap.htmlHolder when cursor is at upper/lower side |
606 | } | 611 | } |
612 | container.onmousemove(e) | ||
607 | 613 | ||
608 | container.classList.add('dragging-geolink') | 614 | // Handler for dragend |
609 | container.onmousemove = onMouseMove | 615 | container.onmouseup = (e) => { |
610 | container.onmouseup = function (e) { | ||
611 | container.classList.remove('dragging-geolink') | 616 | container.classList.remove('dragging-geolink') |
612 | container.onmousemove = null | 617 | container.onmousemove = null |
613 | container.onmouseup = null | 618 | container.onmouseup = null |
614 | geoLink.classList.remove('drag') | 619 | geoLink.classList.remove('drag') |
615 | line?.remove() | 620 | line.remove() |
616 | lineEnd.remove() | ||
617 | const resumeContent = () => { | ||
618 | range.deleteContents() | ||
619 | range.insertNode(originContent) | ||
620 | } | ||
621 | 621 | ||
622 | const map = document.elementFromPoint(e.clientX, e.clientY) | 622 | const map = document.elementFromPoint(e.clientX, e.clientY) |
623 | .closest('.mapclay[data-render="fulfilled"]') | 623 | .closest('.mapclay[data-render="fulfilled"]') |
624 | if (!map) { | 624 | if (!map) { |
625 | resumeContent('map/selection') | 625 | resumeContent() |
626 | return | 626 | return |
627 | } | 627 | } |
628 | 628 | ||
629 | const refLink = utils.addAnchorByPoint({ | 629 | const marker = utils.addMarkerByPoint({ point: [e.clientX, e.clientY], map }) |
630 | defaultName: geoLink.textContent, | 630 | if (!marker) { |
631 | point: e, | 631 | resumeContent() |
632 | map, | ||
633 | }) | ||
634 | if (!refLink) { | ||
635 | resumeContent('reflink') | ||
636 | return | 632 | return |
637 | } | 633 | } |
638 | 634 | ||
639 | geoLink.href = refLink.link | 635 | geoLink.href = `geo:${marker.dataset.xy.split(',').reverse()}` |
640 | utils.createGeoLink(geoLink) | 636 | utils.createGeoLink(geoLink) |
641 | } | 637 | } |
642 | } | 638 | } |
643 | container.ondragstart = () => false | ||
644 | 639 | ||
645 | return Object.seal(dumbymap) | 640 | return Object.seal(dumbymap) |
646 | } | 641 | } |