Skip to content

Commit

Permalink
add middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dobri1408 committed Aug 5, 2024
1 parent 96794da commit 2480fad
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
18 changes: 9 additions & 9 deletions src/Arcgis/Layer/Layer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ class $Layer extends EventEmitter {
this.#layer.when(async () => {
const data = await this.#layer.queryExtent();
if (!$map.view) return;
$map.view.goTo(data.extent).then(() => {
if (!$map.view) return;
const homeWidget = $map.view.ui.find('Home');
if (!homeWidget) return;
homeWidget.viewpoint = new $map.modules.AgViewpoint({
center: $map.view.center,
zoom: $map.view.zoom,
});
});
// $map.view.goTo(data.extent).then(() => {
// if (!$map.view) return;
// const homeWidget = $map.view.ui.find('Home');
// if (!homeWidget) return;
// homeWidget.viewpoint = new $map.modules.AgViewpoint({
// center: $map.view.center,
// zoom: $map.view.zoom,
// });
// });
});
}

Expand Down
10 changes: 7 additions & 3 deletions src/Widgets/VisualizationWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,13 @@ const VisualizationWidget = (props) => {
if (updating || !$map.current.view) return;
debounce(
async () => {
console.log(
await $map.current.view.takeScreenshot({ format: 'png' }),
);
const preview = await $map.current.view.takeScreenshot({
format: 'png',
});
props.onChange(props.id, {
...value,
preview: preview.dataUrl,
});
},
300,
'visualization-widget-screenshot',
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ArcgisViewpointWidget from './Widgets/ArcgisViewpointWidget';
import VisualizationViewWidget from './Widgets/VisualizationViewWidget';

import VisualizationView from './Views/VisualizationView';
import { preview_image } from './middlewares/preview_image';

const applyConfig = (config) => {
config.settings.allowed_cors_destinations = [
Expand Down Expand Up @@ -63,6 +64,10 @@ const applyConfig = (config) => {
],
'id',
);
config.settings.storeExtenders = [
...(config.settings.storeExtenders || []),
preview_image,
];

config.views.contentTypesViews.map_visualization = VisualizationView;

Expand Down
66 changes: 66 additions & 0 deletions src/middlewares/preview_image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
CREATE_CONTENT,
UPDATE_CONTENT,
} from '@plone/volto/constants/ActionTypes';

export const preview_image = (middlewares) => [
(store) => (next) => async (action) => {
if (![CREATE_CONTENT, UPDATE_CONTENT].includes(action.type)) {
return next(action);
}
const state = store.getState();
const contentData = state.content.data;
console.log({ action });
if (
!contentData ||
contentData['@type'] !== 'map_visualization' ||
contentData.preview_image_saved ||
!action?.request?.data?.map_visualization_data?.preview
) {
return next(action);
}

if (
contentData?.preview_image &&
contentData?.preview_image?.filename !==
'preview_image_generated_map_simple.png'
) {
return next(action);
}

try {
const previewImage = {
preview_image: {
data: action.request.data.map_visualization_data.preview.split(
',',
)[1],
encoding: 'base64',
'content-type': 'image/png',
filename: 'preview_image_generated_map_simple.png',
},
preview_image_saved: true,
};

const mapVisualizationData = {
...action.request.data.map_visualization_data,
};
delete mapVisualizationData.preview;

return next({
...action,
request: {
...action.request,
data: {
...action.request.data,
...previewImage,
map_visualization_data: mapVisualizationData,
},
},
});
} catch (error) {
console.error(error);
return next(action);
}
},
...middlewares,
];

0 comments on commit 2480fad

Please sign in to comment.