2022-10-18 14:52:24 +00:00
|
|
|
import react, { memo, useRef } from 'react';
|
2022-10-17 18:52:10 +00:00
|
|
|
import { range, isEqual } from 'lodash';
|
2022-10-17 10:04:25 +00:00
|
|
|
|
2022-10-17 16:17:13 +00:00
|
|
|
import { Rectangle, TileFactory, TileKeyObject } from './types';
|
2022-10-17 15:15:24 +00:00
|
|
|
import tileUri from './uris';
|
2022-10-17 10:04:25 +00:00
|
|
|
|
2022-10-20 14:58:33 +00:00
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
2022-10-20 14:21:27 +00:00
|
|
|
export const thisIsAModule = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2022-10-20 14:58:33 +00:00
|
|
|
|
2022-10-20 14:21:27 +00:00
|
|
|
declare global {
|
|
|
|
var cacheForTiledLayer: any;
|
|
|
|
}
|
|
|
|
|
2022-10-20 14:58:33 +00:00
|
|
|
//export {};
|
2022-10-20 14:21:27 +00:00
|
|
|
|
|
|
|
globalThis.cacheForTiledLayer = new Map();
|
|
|
|
|
2022-10-17 10:04:25 +00:00
|
|
|
export interface TiledLayerProperties {
|
|
|
|
/** The key of the first (ie top/left) tile */
|
2022-10-17 11:47:35 +00:00
|
|
|
keyObject: TileKeyObject;
|
|
|
|
/** The current viewport expressed in tiles coordinates */
|
2022-10-19 16:35:20 +00:00
|
|
|
viewPort?: Rectangle;
|
2022-10-17 16:17:13 +00:00
|
|
|
/** The factory to create tiles */
|
|
|
|
tileFactory: TileFactory;
|
2022-10-17 10:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-17 15:48:30 +00:00
|
|
|
* A lazily loaded layer of tiles.
|
2022-10-17 11:47:35 +00:00
|
|
|
*
|
|
|
|
* This component is rather dumb and is mainly a sparse array of tiles.
|
2022-10-17 15:48:30 +00:00
|
|
|
*
|
2022-10-17 11:47:35 +00:00
|
|
|
* New tiles are added to the array when the viewport is updated and they stay in the array until
|
2022-10-19 16:35:20 +00:00
|
|
|
* the component is destroyed.
|
2022-10-17 10:04:25 +00:00
|
|
|
*
|
2022-10-17 15:48:30 +00:00
|
|
|
* This component has no need to know the number nor the size of its tiles: tiles can be added when needed and
|
|
|
|
* its unit is the tile size (the parent component needs to transform its enclosing SVG group to adapt its units)
|
2022-10-18 14:52:24 +00:00
|
|
|
*
|
2022-10-20 14:58:33 +00:00
|
|
|
* The `globalThis.cacheForTiledLayer` global variable is used as a cache to store tiles without being subject
|
|
|
|
* to re-initialization when components are unmounted/remounted.
|
|
|
|
*
|
|
|
|
* This cache is a map of map, the first key identifying the `<TiledLayer` and the second one for `<Tile>`s.
|
|
|
|
*
|
|
|
|
* Idea stolen [on the web](https://dev.to/tiagof/react-re-mounting-vs-re-rendering-lnh)
|
|
|
|
*
|
|
|
|
* TODO: cache housekeeping
|
|
|
|
*
|
2022-10-17 21:04:58 +00:00
|
|
|
* TODO: test tiles'X and Y boundaries.
|
2022-10-20 14:58:33 +00:00
|
|
|
*
|
2022-10-19 16:35:20 +00:00
|
|
|
* TODO: remove tileFactory to facilitate memoisation.
|
2022-10-17 15:48:30 +00:00
|
|
|
*
|
2022-10-17 10:04:25 +00:00
|
|
|
*/
|
2022-10-20 14:21:27 +00:00
|
|
|
export const TiledLayer: react.FC<TiledLayerProperties> = (
|
|
|
|
props: TiledLayerProperties
|
|
|
|
) => {
|
|
|
|
// console.log(`Rendering TiledLayer: ${JSON.stringify(props)}`);
|
|
|
|
|
|
|
|
const key = tileUri({
|
|
|
|
provider: props.keyObject.provider,
|
|
|
|
zoomLevel: props.keyObject.zoomLevel,
|
|
|
|
});
|
2022-10-17 15:15:24 +00:00
|
|
|
|
2022-10-20 14:21:27 +00:00
|
|
|
const tiles: any = globalThis.cacheForTiledLayer.get(key) ?? new Map();
|
|
|
|
if (props.viewPort !== undefined) {
|
|
|
|
range(props.viewPort.topLeft.y, props.viewPort.bottomRight.y + 1).forEach(
|
|
|
|
(row) => {
|
|
|
|
range(
|
|
|
|
props.viewPort!.topLeft.x,
|
|
|
|
props.viewPort!.bottomRight.x + 1
|
|
|
|
).forEach((col) => {
|
|
|
|
const keyObject = {
|
|
|
|
provider: props.keyObject.provider,
|
|
|
|
zoomLevel: props.keyObject.zoomLevel,
|
|
|
|
x: props.keyObject.x + col,
|
|
|
|
y: props.keyObject.y + row,
|
|
|
|
};
|
|
|
|
const key = tileUri(keyObject);
|
|
|
|
if (!tiles.has(key)) {
|
|
|
|
tiles.set(
|
|
|
|
key,
|
|
|
|
<g key={key} transform={`translate(${col}, ${row})`}>
|
|
|
|
{props.tileFactory(keyObject)}
|
|
|
|
</g>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
globalThis.cacheForTiledLayer.set(key, tiles);
|
|
|
|
}
|
2022-10-17 15:15:24 +00:00
|
|
|
|
2022-10-20 14:21:27 +00:00
|
|
|
return <>{Array.from(tiles.values())}</>;
|
|
|
|
};
|
2022-10-17 10:04:25 +00:00
|
|
|
|
|
|
|
export default TiledLayer;
|