Skip to content

Commit

Permalink
feat: improve resourcesfiles loading
Browse files Browse the repository at this point in the history
  • Loading branch information
AlitaBernachot committed Sep 8, 2024
1 parent 2cf78d0 commit 27be623
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 29 deletions.
43 changes: 19 additions & 24 deletions examples/_code-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,30 @@ export async function initCodeSandbox(indexJsPath, ...filesPathes) {
indexJsContent = indexJsContent.replaceAll(/(olcs\/.*?).ts('?;?)/ig, '$1.js$2');

const additionalJsFiles = {};
const resourcesFiles = filesPathes
.filter(path => path.indexOf('data/') === 0)
// eslint-disable-next-line arrow-body-style
.map(path => ({
[path]: {
'isBinary': true,
content: `https://openlayers.org/ol-cesium/examples/${path}`
}
}));
const jsFiles = filesPathes.filter(path => path.indexOf('data/') !== 0);

for (const filePath of filesPathes) {
for (const filePath of jsFiles) {
const responseFile = await fetch(filePath);
const txtDataFile = await responseFile.text();

additionalJsFiles[filePath.replace('./', '').replace('rawjs', '')] = {content: txtDataFile};
}

initCodeSandboxButton({indexJsContent, additionalJsFiles});
initCodeSandboxButton({indexJsContent, additionalJsFiles, resourcesFiles});
}

function initCodeSandboxButton(options) {
const {indexJsContent, additionalJsFiles} = options;
const {indexJsContent, additionalJsFiles, resourcesFiles} = options;

let indexHtmlContent = '';
const button = document.getElementById('sandbox-button');
const form = document.querySelector('#sandbox-form');
Expand All @@ -39,7 +50,6 @@ function initCodeSandboxButton(options) {
divExampleCodeSource.querySelectorAll('.clear-map-sandbox').forEach(map => map.innerHTML = '');
indexHtmlContent = divExampleCodeSource.innerHTML;


const indexHtml = `
<!DOCTYPE html>
<html>
Expand Down Expand Up @@ -139,32 +149,17 @@ function initCodeSandboxButton(options) {
'.babelrc': {
content: '{ "plugins": ["@babel/plugin-proposal-class-properties"] }'
},
'data/geojson/countries.geojson': {
'isBinary': true,
content: 'https://openlayers.org/ol-cesium/examples/data/geojson/countries.geojson'
},
'data/geojson/buildings.geojson': {
'isBinary': true,
content: 'https://openlayers.org/ol-cesium/examples/data/geojson/buildings.geojson'
},
'data/geojson/vector_data.geojson': {
'isBinary': true,
content: 'https://openlayers.org/ol-cesium/examples/data/geojson/vector_data.geojson'
},
'data/icon.png': {
'isBinary': true,
content: 'https://openlayers.org/ol-cesium/examples/data/icon.png'
},
'data/Box.gltf': {
'isBinary': true,
content: 'https://openlayers.org/ol-cesium/examples/data/Box.gltf'
},
'index.js': {
content: indexJsContent,
},
'index.html': {
content: indexHtml
},
...resourcesFiles.reduce((acc, curr) => {
const key = Object.keys(curr)[0]; // Récupérer la clé de l'objet
acc[key] = curr[key]; // Ajouter la propriété à l'objet accumulé
return acc;
}, {}),
...additionalJsFiles
}
};
Expand Down
2 changes: 1 addition & 1 deletion examples/buildings.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ map.on('click', (e) => {
//##REMOVE## Keep this tag, split code here for code sandbox

import {initCodeSandbox} from './_code-sandbox.js';
initCodeSandbox('rawjs/buildings.js');
initCodeSandbox('rawjs/buildings.js', 'data/geojson/buildings.geojson');
2 changes: 1 addition & 1 deletion examples/groundvectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ ol3d.enableAutoRenderLoop();
//##REMOVE## Keep this tag, split code here for code sandbox

import {initCodeSandbox} from './_code-sandbox.js';
initCodeSandbox('rawjs/groundvectors.js', './data/geojson/ground_vector_data.geojson');
initCodeSandbox('rawjs/groundvectors.js', 'data/geojson/ground_vector_data.geojson');

// faire à la demande
// définir un objet
Expand Down
2 changes: 1 addition & 1 deletion examples/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ map.on('click', (e) => {
//##REMOVE## Keep this tag, split code here for code sandbox

import {initCodeSandbox} from './_code-sandbox.js';
initCodeSandbox('rawjs/selection.js');
initCodeSandbox('rawjs/selection.js', 'data/geojson/countries.geojson');

2 changes: 1 addition & 1 deletion examples/tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ setInterval(() => {
//##REMOVE## Keep this tag, split code here for code sandbox

import {initCodeSandbox} from './_code-sandbox.js';
initCodeSandbox('rawjs/tracking.js');
initCodeSandbox('rawjs/tracking.js', 'data/icon.png');
2 changes: 1 addition & 1 deletion examples/vectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,4 @@ ol3d.enableAutoRenderLoop();
//##REMOVE## Keep this tag, split code here for code sandbox

import {initCodeSandbox} from './_code-sandbox.js';
initCodeSandbox('rawjs/vectors.js');
initCodeSandbox('rawjs/vectors.js', 'data/geojson/vector_data.geojson', 'data/Box.gltf', 'data/icon.png');

0 comments on commit 27be623

Please sign in to comment.