Skip to content

Commit 398ed48

Browse files
authored
Merge pull request #1129 from AlitaBernachot/icon-edit-code-in-sandbox-vite
Icon edit code in sandbox
2 parents 9a5997c + f44a4b2 commit 398ed48

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1286
-721
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v 2.22.0
4+
5+
* Changes
6+
* Port to CesiumJS 1.122.
7+
* Allow to live edit examples.
8+
39
## v 2.21.0
410

511
* Changes

examples/3dtiles.html

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
<?xml version='1.0' encoding='UTF-8'?>
21
<!DOCTYPE HTML>
3-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
2+
<html lang="en">
43
<head>
54
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
65
<meta name="robots" content="index, all" />
7-
<title>olcesium 3d tiles example</title>
6+
<title>Ol-Cesium | 3d tiles example</title>
87
<link rel="stylesheet" href="../node_modules/ol/ol.css" type="text/css">
8+
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css" crossorigin="anonymous">
99
</head>
1010
<body>
11-
<div id="map" style="width:600px;height:400px;"></div>
12-
<input id="enable" type="button" value="Enable/disable" />
11+
<div style="text-align: right;">
12+
<button id="sandbox-button"><i class="fab fa-codepen fa-lg"></i> Edit in sandbox</button>
13+
<form method="POST" id="sandbox-form" target="_blank" action="https://codesandbox.io/api/v1/sandboxes/define">
14+
<input id="sandbox-params" type="hidden" name="parameters">
15+
</form>
16+
</div>
17+
<div id="example-html-source">
18+
<div>
19+
<div class="clear-map-sandbox" id="mapCesium" style="width:600px;height:400px;float:left;"></div>
20+
</div>
21+
<input id="enable" type="button" value="Enable/disable" />
22+
</div>
1323
<script src="./inject_ol_cesium.js"></script>
1424
<script type="module" src="./3dtiles.js"></script>
1525
</body>

examples/3dtiles.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {defaults as olControlDefaults} from 'ol/control.js';
55
import olSourceOSM from 'ol/source/OSM.js';
66
import olLayerTile from 'ol/layer/Tile.js';
77
import olMap from 'ol/Map.js';
8-
import {OLCS_ION_TOKEN} from './_common.js';
98

10-
Cesium.Ion.defaultAccessToken = OLCS_ION_TOKEN;
9+
const Cesium = window.Cesium;
10+
1111
const ol2d = new olMap({
1212
layers: [
1313
new olLayerTile({
@@ -39,3 +39,8 @@ document.getElementById('enable').addEventListener('click', () => ol3d.setEnable
3939
scene.camera.flyTo({
4040
destination: Cesium.Cartesian3.fromDegrees(6.54254, 46.50802, 1000.0)
4141
});
42+
43+
//##REMOVE## Keep this tag, split code here for code sandbox
44+
45+
import {initCodeSandbox} from './_code-sandbox.js';
46+
initCodeSandbox('rawjs/3dtiles.js', 'rawjs/_proj21781.js');

examples/_code-sandbox.js

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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

examples/_common.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/buildings.html

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
<?xml version='1.0' encoding='UTF-8'?>
21
<!DOCTYPE HTML>
3-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
2+
<html lang="en">
43
<head>
54
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
65
<meta name="robots" content="index, all" />
7-
<title>olcesium buildings example</title>
6+
<title>Ol-Cesium | Buildings example</title>
87
<link rel="stylesheet" href="../node_modules/ol/ol.css" type="text/css"></link>
8+
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css" crossorigin="anonymous">
99
</head>
10+
1011
<body>
11-
<div id="map2d" style="width:600px;height:400px;float:left;"></div>
12-
<div id="map3d" style="width:600px;height:400px;float:left;position:relative;"></div>
13-
<div>A building may be highlighted by clicking it on the OpenLayers map.
14-
<br/>It gets automatically selected in Cesium.
15-
<br/>If you see flickering in Cesium, please check your graphic card drivers.</div>
16-
<script src="./inject_ol_cesium.js"></script>
17-
<script type="module" src="./buildings.js"></script> </body>
12+
<div style="text-align: right;">
13+
<button id="sandbox-button"><i class="fab fa-codepen fa-lg"></i> Edit in sandbox</button>
14+
<form method="POST" id="sandbox-form" target="_blank" action="https://codesandbox.io/api/v1/sandboxes/define">
15+
<input id="sandbox-params" type="hidden" name="parameters">
16+
</form>
17+
</div>
18+
19+
<div id="example-html-source">
20+
<div class="clear-map-sandbox" id="map2d" style="width:600px;height:400px;float:left;"></div>
21+
<div class="clear-map-sandbox" id="mapCesium" style="width:600px;height:400px;float:left;position:relative;"></div>
22+
<div>
23+
A building may be highlighted by clicking it on the OpenLayers map.
24+
<br/>It gets automatically selected in Cesium.
25+
<br/>If you see flickering in Cesium, please check your graphic card drivers.
26+
</div>
27+
</div>
28+
29+
<script src="./inject_ol_cesium.js"></script>
30+
<script type="module" src="./buildings.js"></script>
31+
</body>
1832
</html>

examples/buildings.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const map = new olMap({
4747
vector.set('olcs_shadows', true);
4848

4949

50-
const ol3d = new OLCesium({map, target: 'map3d'});
50+
const ol3d = new OLCesium({map, target: 'mapCesium'});
5151
ol3d.setEnabled(true);
5252

5353
// Be aware that enabling the following properties can impact performance
@@ -89,3 +89,8 @@ map.on('click', (e) => {
8989
selectedFeature.setStyle(selectionStyle);
9090
}
9191
});
92+
93+
//##REMOVE## Keep this tag, split code here for code sandbox
94+
95+
import {initCodeSandbox} from './_code-sandbox.js';
96+
initCodeSandbox('rawjs/buildings.js', 'data/geojson/buildings.geojson');

examples/customProj.html

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
<?xml version='1.0' encoding='UTF-8'?>
2-
<!DOCTYPE HTML>
3-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
1+
<!DOCTYPE html>
2+
<html lang="en">
43
<head>
54
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
65
<meta name="robots" content="index, all" />
7-
<title>Custom projection example</title>
6+
<title>Ol-Cesium | Custom projection example</title>
87
<link rel="stylesheet" href="../node_modules/ol/ol.css" type="text/css">
8+
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css" crossorigin="anonymous">
99
</head>
10+
1011
<body>
11-
<div id="map" style="width:600px;height:400px;"></div>
12-
<input id="enable" type="button" value="Enable/disable" />
12+
<div style="text-align: right;">
13+
<button id="sandbox-button"><i class="fab fa-codepen fa-lg"></i> Edit in sandbox</button>
14+
<form method="POST" id="sandbox-form" target="_blank" action="https://codesandbox.io/api/v1/sandboxes/define">
15+
<input id="sandbox-params" type="hidden" name="parameters">
16+
</form>
17+
</div>
18+
19+
<div id="example-html-source">
20+
<div class="clear-map-sandbox" id="mapCesium" style="width:600px;height:400px;"></div>
21+
<input id="enable" type="button" value="Enable/disable" />
22+
</div>
1323

1424
<script src="./inject_ol_cesium.js"></script>
15-
<script type="module" src="./customProj.js"></script> </body>
25+
<script type="module" src="./customProj.js"></script>
26+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/libs/lz-string.min.js"></script>
27+
</body>
1628
</html>

examples/customProj.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import olLayerTile from 'ol/layer/Tile.js';
77
import olMap from 'ol/Map.js';
88
import {get as getProjection} from 'ol/proj.js';
99
import './_proj21781.js';
10-
import {OLCS_ION_TOKEN} from './_common.js';
1110

1211
const customProjSource = new olSourceImageWMS({
1312
attributions: '© <a href="http://www.geo.admin.ch/internet/geoportal/' +
@@ -20,7 +19,7 @@ const customProjSource = new olSourceImageWMS({
2019

2120
customProjSource.set('olcs_projection', getProjection('EPSG:3857'));
2221

23-
Cesium.Ion.defaultAccessToken = OLCS_ION_TOKEN;
22+
const Cesium = window.Cesium;
2423
const ol2d = new olMap({
2524
layers: [
2625
new olLayerTile({
@@ -30,7 +29,7 @@ const ol2d = new olMap({
3029
source: customProjSource
3130
})
3231
],
33-
target: 'map',
32+
target: 'mapCesium',
3433
view: new olView({
3534
center: [860434.6266531206, 6029479.0044273855],
3635
zoom: 6
@@ -43,8 +42,12 @@ const ol3d = new OLCesium({
4342
return Cesium.JulianDate.now();
4443
}
4544
});
46-
const scene = ol3d.getCesiumScene();
47-
Cesium.createWorldTerrainAsync().then(tp => scene.terrainProvider = tp);
45+
ol3d.getCesiumScene();
4846
ol3d.setEnabled(true);
4947

5048
document.getElementById('enable').addEventListener('click', () => ol3d.setEnabled(!ol3d.getEnabled()));
49+
50+
//##REMOVE## Keep this tag, split code here for code sandbox
51+
52+
import {initCodeSandbox} from './_code-sandbox.js';
53+
initCodeSandbox('rawjs/customProj.js', 'rawjs/_proj21781.js');

0 commit comments

Comments
 (0)