|
| 1 | +const lzScript = document.createElement('script'); |
| 2 | +lzScript.src = 'https://cdn.jsdelivr.net/npm/[email protected]/libs/lz-string.min.js'; |
| 3 | +document.head.appendChild(lzScript); |
| 4 | + |
| 5 | +function compress(json) { |
| 6 | + return window.LZString.compressToBase64(JSON.stringify(json)) |
| 7 | + .replace(/\+/g, '-') |
| 8 | + .replace(/\//g, '_') |
| 9 | + .replace(/=+$/, ''); |
| 10 | +} |
| 11 | + |
| 12 | +export async function initCodeSandbox(indexJsPath, ...filesPathes) { |
| 13 | + const response = await fetch(indexJsPath); |
| 14 | + const txtData = await response.text(); |
| 15 | + const indexJsContent = txtData.split('//##REMOVE##')[0]; |
| 16 | + const additionalJsFiles = {}; |
| 17 | + const resourcesFiles = filesPathes |
| 18 | + .filter(path => path.indexOf('data/') === 0) |
| 19 | + // eslint-disable-next-line arrow-body-style |
| 20 | + .map(path => ({ |
| 21 | + [path]: { |
| 22 | + 'isBinary': true, |
| 23 | + content: `https://openlayers.org/ol-cesium/examples/${path}` |
| 24 | + } |
| 25 | + })); |
| 26 | + const jsFiles = filesPathes.filter(path => !path.startsWith('data/')); |
| 27 | + |
| 28 | + for (const filePath of jsFiles) { |
| 29 | + const responseFile = await fetch(filePath); |
| 30 | + const txtDataFile = await responseFile.text(); |
| 31 | + |
| 32 | + additionalJsFiles[filePath.replace('./', '').replace('rawjs', '')] = {content: txtDataFile}; |
| 33 | + } |
| 34 | + |
| 35 | + initCodeSandboxButton({indexJsContent, additionalJsFiles, resourcesFiles}); |
| 36 | +} |
| 37 | + |
| 38 | +function initCodeSandboxButton(options) { |
| 39 | + const {indexJsContent, additionalJsFiles, resourcesFiles} = options; |
| 40 | + |
| 41 | + let indexHtmlContent = ''; |
| 42 | + const button = document.getElementById('sandbox-button'); |
| 43 | + const form = document.querySelector('#sandbox-form'); |
| 44 | + |
| 45 | + if (!button || !form) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + const divExampleCodeSource = document.createElement('div'); |
| 50 | + divExampleCodeSource.innerHTML = document.getElementById('example-html-source').innerHTML; |
| 51 | + divExampleCodeSource.querySelectorAll('.clear-map-sandbox').forEach(map => map.innerHTML = ''); |
| 52 | + indexHtmlContent = divExampleCodeSource.innerHTML; |
| 53 | + |
| 54 | + const indexHtml = ` |
| 55 | +<!DOCTYPE html> |
| 56 | +<html> |
| 57 | + <head> |
| 58 | + <title>Ol-Cesium example in sandbox</title> |
| 59 | + <meta charset="UTF-8" /> |
| 60 | + <link rel="stylesheet" href="node_modules/ol/ol.css"> |
| 61 | + <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> |
| 62 | + <script src="https://cesium.com/downloads/cesiumjs/releases/1.122/Build/Cesium/Cesium.js"></script> |
| 63 | + <style> |
| 64 | + .ol-popup { |
| 65 | + position: absolute; |
| 66 | + background-color: white; |
| 67 | + -webkit-filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2)); |
| 68 | + filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2)); |
| 69 | + padding: 15px; |
| 70 | + border-radius: 10px; |
| 71 | + border: 1px solid #cccccc; |
| 72 | + bottom: 12px; |
| 73 | + left: -50px; |
| 74 | + min-width: 280px; |
| 75 | + } |
| 76 | + .ol-popup:after, .ol-popup:before { |
| 77 | + top: 100%; |
| 78 | + border: solid transparent; |
| 79 | + content: " "; |
| 80 | + height: 0; |
| 81 | + width: 0; |
| 82 | + position: absolute; |
| 83 | + pointer-events: none; |
| 84 | + } |
| 85 | + .ol-popup:after { |
| 86 | + border-top-color: white; |
| 87 | + border-width: 10px; |
| 88 | + left: 48px; |
| 89 | + margin-left: -10px; |
| 90 | + } |
| 91 | + .ol-popup:before { |
| 92 | + border-top-color: #cccccc; |
| 93 | + border-width: 11px; |
| 94 | + left: 48px; |
| 95 | + margin-left: -11px; |
| 96 | + } |
| 97 | + .ol-popup-closer { |
| 98 | + text-decoration: none; |
| 99 | + position: absolute; |
| 100 | + top: 2px; |
| 101 | + right: 8px; |
| 102 | + } |
| 103 | + .ol-popup-closer:after { |
| 104 | + content: "✖"; |
| 105 | + } |
| 106 | + .popover-content { |
| 107 | + min-width: 180px; |
| 108 | + } |
| 109 | + code { |
| 110 | + padding: 2px 4px; |
| 111 | + font-size: 90%; |
| 112 | + color: #c7254e; |
| 113 | + background-color: #f9f2f4; |
| 114 | + border-radius: 4px; |
| 115 | + } |
| 116 | + </style> |
| 117 | + </head> |
| 118 | + <body> |
| 119 | + ${indexHtmlContent} |
| 120 | + </body> |
| 121 | + <script type="module" src="/index.js"></script> |
| 122 | + <script src="https://code.jquery.com/jquery-2.2.3.min.js"></script> |
| 123 | + <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> |
| 124 | +</html>`; |
| 125 | + const parameters = { |
| 126 | + template: 'parcel', |
| 127 | + files: { |
| 128 | + 'package.json': { |
| 129 | + content: { |
| 130 | + 'source': 'index.html', |
| 131 | + 'main': 'index.html', |
| 132 | + 'scripts': { |
| 133 | + 'build': 'vite build', |
| 134 | + 'start': 'npm run build && serve dist', |
| 135 | + }, |
| 136 | + 'devDependencies': { |
| 137 | + 'serve': '14.2.3', |
| 138 | + 'vite': '^3.2.3', |
| 139 | + '@babel/core': '^7.24.4', |
| 140 | + '@babel/plugin-proposal-class-properties': '^7.18.6' |
| 141 | + }, |
| 142 | + 'dependencies': { |
| 143 | + 'olcs': '2.20.0', |
| 144 | + 'proj4': '2.11.0', |
| 145 | + 'cesium': '1.122.0', |
| 146 | + 'ol': '10.1.0' |
| 147 | + } |
| 148 | + }, |
| 149 | + }, |
| 150 | + '.babelrc': { |
| 151 | + content: '{ "plugins": ["@babel/plugin-proposal-class-properties"] }' |
| 152 | + }, |
| 153 | + 'index.js': { |
| 154 | + content: indexJsContent, |
| 155 | + }, |
| 156 | + 'index.html': { |
| 157 | + content: indexHtml |
| 158 | + }, |
| 159 | + ...resourcesFiles.reduce((acc, curr) => { |
| 160 | + const key = Object.keys(curr)[0]; // Récupérer la clé de l'objet |
| 161 | + acc[key] = curr[key]; // Ajouter la propriété à l'objet accumulé |
| 162 | + return acc; |
| 163 | + }, {}), |
| 164 | + ...additionalJsFiles |
| 165 | + } |
| 166 | + }; |
| 167 | + |
| 168 | + button.onclick = function(event) { |
| 169 | + event.preventDefault(); |
| 170 | + form.parameters.value = compress(parameters); |
| 171 | + form.submit(); |
| 172 | + }; |
| 173 | +} |
| 174 | + |
| 175 | +// dans npm prepare : extraire les versions à utiliser et faire un fichier json avec ces versions |
| 176 | +// Ce fichier json doit etre lu, faire un fetch de ce fichier |
| 177 | +// ce fichier ne peut pas etre commit |
| 178 | +// à rajouter dans gitinore |
0 commit comments