|
| 1 | +(function(){ |
| 2 | + var html2canvas; |
| 3 | + var title = ""; |
| 4 | + |
| 5 | + var takeScreenshot = function(el, ignore, onclone){ |
| 6 | + return html2canvas(el, { |
| 7 | + allowTaint: true, |
| 8 | + useCORS: true, |
| 9 | + logging: false, |
| 10 | + onclone: onclone, |
| 11 | + removeContainer: false, |
| 12 | + ignoreElements: function(e){ |
| 13 | + for (var i = 0; i < ignore.length; i++){ |
| 14 | + if (e.classList.contains(ignore[i])) return true; |
| 15 | + } |
| 16 | + return false; |
| 17 | + } |
| 18 | + }).then(function(canvas) { |
| 19 | + var link = document.createElement('a'); |
| 20 | + link.href = canvas.toDataURL(); |
| 21 | + link.download = (title ? title.replace(/\s+/g, '-').replace(/[^0-9a-zA-Z\-_]+/g, '') + '-' : '') + 'snapshot.png'; |
| 22 | + link.click(); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + var registerApiActionButton = function(apiActionButton, getTitle, getContainer, ignore = [], onclone = function(doc){ return doc; }){ |
| 27 | + apiActionButton([ |
| 28 | + 'snapshot/node_modules/html2canvas/dist/html2canvas.min.js' |
| 29 | + ], function(options, h2c){ |
| 30 | + html2canvas = h2c; |
| 31 | + |
| 32 | + var btnRef = null; |
| 33 | + title = getTitle(options); |
| 34 | + |
| 35 | + var handleClick = function(){ |
| 36 | + if (!btnRef) return; |
| 37 | + var icon = btnRef.querySelector("i"); |
| 38 | + icon.className = "fa fa-circle-notch fa-spin"; |
| 39 | + btnRef.disabled = true; |
| 40 | + |
| 41 | + takeScreenshot(getContainer(options), |
| 42 | + ignore, |
| 43 | + onclone |
| 44 | + ).then(function(){ |
| 45 | + icon.className = "fa fa-camera"; |
| 46 | + btnRef.disabled = false; |
| 47 | + }); |
| 48 | + }; |
| 49 | + |
| 50 | + return React.createElement( |
| 51 | + "button", |
| 52 | + { |
| 53 | + type: "button", |
| 54 | + className: "btn btn-sm btn-secondary", |
| 55 | + title: "Take snapshot", |
| 56 | + style: {padding: "5px 9px"}, |
| 57 | + ref: function(el){ btnRef = el; }, |
| 58 | + onClick: handleClick |
| 59 | + }, |
| 60 | + React.createElement("i", { className: "fa fa-camera" }, ""), |
| 61 | + "" |
| 62 | + ); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + registerApiActionButton(PluginsAPI.Map.addActionButton, function(options){ |
| 67 | + var mapTitle = options.map._container.parentElement.parentElement.parentElement.querySelector(".map-title"); |
| 68 | + return mapTitle ? mapTitle.innerText.trim() : ""; |
| 69 | + }, function(options){ |
| 70 | + return options.map._container; |
| 71 | + }, |
| 72 | + ["leaflet-popup-pane", "leaflet-control-container"], |
| 73 | + function(doc){ |
| 74 | + var overlay = doc.querySelector(".leaflet-overlay-pane .leaflet-zoom-animated"); |
| 75 | + if (!overlay) return doc; |
| 76 | + |
| 77 | + var match; |
| 78 | + if (overlay.style.transform.indexOf("translate3d") === 0) match = overlay.style.transform.match(/translate3d\((-?\d+)px,\s*(-?\d+)px/); |
| 79 | + else if (overlay.style.transform.indexOf("matrix") === 0) match = overlay.style.transform.match(/matrix\([\d\s,.-]+,\s*(-?\d+),\s*(-?\d+)\)/); |
| 80 | + |
| 81 | + if (match) { |
| 82 | + overlay.style.top = parseInt(match[2]) + "px"; |
| 83 | + overlay.style.left = parseInt(match[1]) + "px"; |
| 84 | + overlay.style.transform = ""; |
| 85 | + } |
| 86 | + |
| 87 | + return doc; |
| 88 | + }); |
| 89 | + registerApiActionButton(PluginsAPI.ModelView.addActionButton, function(options){ |
| 90 | + var modelTitle = options.viewer.renderArea.parentElement.parentElement.parentElement.parentElement.querySelector(".model-title"); |
| 91 | + return modelTitle ? modelTitle.innerText.trim() : ""; |
| 92 | + }, function(options){ |
| 93 | + return options.viewer.renderArea; |
| 94 | + }, |
| 95 | + ["quick_buttons_container"]); |
| 96 | +})(); |
| 97 | + |
0 commit comments