Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[Chore]: Technical: Translate utils (dataset-utils and export-utils) (#1734)
Maksim Suslov55a7b51060e6
diff --git a/src/constants/default-settings.ts b/src/constants/default-settings.tsindex ba53f00e5..7b9ff42d5 100644--- a/src/constants/default-settings.ts+++ b/src/constants/default-settings.ts@@ -676,57 +676,81 @@ export const EXPORT_IMG_RATIOS = keyMirror({CUSTOM: null});-export const EXPORT_IMG_RATIO_OPTIONS = [- {- id: EXPORT_IMG_RATIOS.SCREEN,- label: 'modal.exportImage.ratioOriginalScreen',- getSize: (screenW, screenH) => ({width: screenW, height: screenH})- },- {- id: EXPORT_IMG_RATIOS.CUSTOM,- hidden: true,- label: 'modal.exportImage.ratioCustom',- getSize: (mapW, mapH) => ({width: mapW, height: mapH})- },- {- id: EXPORT_IMG_RATIOS.FOUR_BY_THREE,- label: 'modal.exportImage.ratio4_3',- getSize: (screenW, screenH) => ({- width: screenW,- height: Math.round(screenW * 0.75)- })- },- {- id: EXPORT_IMG_RATIOS.SIXTEEN_BY_NINE,- label: 'modal.exportImage.ratio16_9',- getSize: (screenW, screenH) => ({- width: screenW,- height: Math.round(screenW * 0.5625)- })- }+export type ImageRatioOption = {+ id: keyof typeof EXPORT_IMG_RATIOS;+ label: string;+ hidden?: boolean;+ getSize: (screenW: number, screenH: number) => {width: number; height: number};+};++export const ScreenRatioOption: ImageRatioOption = {+ id: EXPORT_IMG_RATIOS.SCREEN,+ label: 'modal.exportImage.ratioOriginalScreen',+ getSize: (screenW, screenH) => ({width: screenW, height: screenH})+};+export const CustomRatioOption: ImageRatioOption = {+ id: EXPORT_IMG_RATIOS.CUSTOM,+ hidden: true,+ label: 'modal.exportImage.ratioCustom',+ getSize: (mapW, mapH) => ({width: mapW, height: mapH})+};+export const FourByThreeRatioOption: ImageRatioOption = {+ id: EXPORT_IMG_RATIOS.FOUR_BY_THREE,+ label: 'modal.exportImage.ratio4_3',+ getSize: (screenW, screenH) => ({+ width: screenW,+ height: Math.round(screenW * 0.75)+ })+};+export const SixteenByNineRatioOption: ImageRatioOption = {+ id: EXPORT_IMG_RATIOS.SIXTEEN_BY_NINE,+ label: 'modal.exportImage.ratio16_9',+ getSize: (screenW, screenH) => ({+ width: screenW,+ height: Math.round(screenW * 0.5625)+ })+};++export const EXPORT_IMG_RATIO_OPTIONS: ReadonlyArray<ImageRatioOption> = [+ ScreenRatioOption,+ CustomRatioOption,+ FourByThreeRatioOption,+ SixteenByNineRatioOption];-export const EXPORT_IMG_RESOLUTION_OPTIONS = [- {- id: RESOLUTIONS.ONE_X,- label: '1x',- available: true,- scale: 1,- getSize: (screenW, screenH) => ({- width: screenW,- height: screenH- })- },- {- id: RESOLUTIONS.TWO_X,- label: '2x',- available: true,- scale: 2,- getSize: (screenW, screenH) => ({- width: screenW * 2,- height: screenH * 2- })- }+export type ImageResolutionOption = {+ id: keyof typeof RESOLUTIONS;+ label: string;+ available: boolean;+ scale: number;+ getSize: (screenW: number, screenH: number) => {width: number; height: number};+};++export const OneXResolutionOption: ImageResolutionOption = {+ id: RESOLUTIONS.ONE_X,+ label: '1x',+ available: true,+ scale: 1,+ getSize: (screenW, screenH) => ({+ width: screenW,+ height: screenH+ })+};++export const TwoXResolutionOption: ImageResolutionOption = {+ id: RESOLUTIONS.TWO_X,+ label: '2x',+ available: true,+ scale: 2,+ getSize: (screenW, screenH) => ({+ width: screenW * 2,+ height: screenH * 2+ })+};++export const EXPORT_IMG_RESOLUTION_OPTIONS: ReadonlyArray<ImageResolutionOption> = [+ OneXResolutionOption,+ TwoXResolutionOption];export const EXPORT_DATA_TYPE = keyMirror({@@ -777,18 +801,22 @@ export const EXPORT_HTML_MAP_MODES = keyMirror({});// Export map options-export const EXPORT_MAP_FORMAT_OPTIONS = Object.entries(EXPORT_MAP_FORMATS).map((entry: [string, any]) => ({- id: entry[0],- label: entry[1].toLowerCase(),- available: true-}));--export const EXPORT_HTML_MAP_MODE_OPTIONS = Object.entries(EXPORT_HTML_MAP_MODES).map((entry: [string, any]) => ({- id: entry[0],- label: `modal.exportMap.html.${entry[1].toLowerCase()}`,- available: true,- url: getHTMLMapModeTileUrl(entry[1])-}));+export const EXPORT_MAP_FORMAT_OPTIONS = Object.entries(EXPORT_MAP_FORMATS).map(+ (entry: [string, any]) => ({+ id: entry[0],+ label: entry[1].toLowerCase(),+ available: true+ })+);++export const EXPORT_HTML_MAP_MODE_OPTIONS = Object.entries(EXPORT_HTML_MAP_MODES).map(+ (entry: [string, any]) => ({+ id: entry[0],+ label: `modal.exportMap.html.${entry[1].toLowerCase()}`,+ available: true,+ url: getHTMLMapModeTileUrl(entry[1])+ })+);export const DEFAULT_UUID_COUNT = 6;diff --git a/src/reducers/ui-state-updaters.ts b/src/reducers/ui-state-updaters.tsindex 55ace235c..e1269f166 100644--- a/src/reducers/ui-state-updaters.ts+++ b/src/reducers/ui-state-updaters.ts@@ -32,7 +32,7 @@ import {} from 'constants/default-settings';import {LOCALE_CODES} from 'localization/locales';import {createNotification, errorNotification} from 'utils/notifications-utils';-import {calculateExportImageSize} from '../utils/export-utils.js';+import {calculateExportImageSize} from '../utils/export-utils';import {payload_, apply_, compose_} from './composer-helpers';import ActionTypes from '../constants/action-types';@@ -40,8 +40,8 @@ import * as UiStateActions from 'actions/ui-state-actions';import {KeplerGlInitPayload, LoadFilesErrUpdaterAction} from '../actions';export type ExportImage = {- ratio: string;- resolution: string;+ ratio: keyof typeof EXPORT_IMG_RATIOS;+ resolution: keyof typeof RESOLUTIONS;legend: boolean;mapH: number;mapW: number;@@ -527,6 +527,9 @@ export const setExportImageSettingUpdater = (...state,exportImage: {...updated,+ // @ts-expect-error+ // TODO: calculateExportImageSize does not return imageSize.zoomOffset,+ // do we need take this value from current state, or return defaul value = 0imageSize}};diff --git a/src/utils/dataset-utils.d.ts b/src/utils/dataset-utils.d.tsdeleted file mode 100644index f0277caab..000000000--- a/src/utils/dataset-utils.d.ts+++ /dev/null@@ -1,10 +0,0 @@-import {Datasets, Field} from '../reducers/vis-state-updaters';-import {ProtoDataset} from '../actions';-import {RGBColor} from 'reducers/types';--export function createNewDataEntry(data: ProtoDataset, datasets?: Datasets): Datasets;--export function getNewDatasetColor(ds: Datasets): RGBColor;--export const datasetColorMaker: Generator<RGBColor>;-export function findDefaultColorField(dataset: Dataset): false | Field;diff --git a/src/utils/dataset-utils.js b/src/utils/dataset-utils.tssimilarity index 92%rename from src/utils/dataset-utils.jsrename to src/utils/dataset-utils.tsindex 0e3406fba..eee71dd68 100644--- a/src/utils/dataset-utils.js+++ b/src/utils/dataset-utils.ts@@ -22,7 +22,11 @@ import {hexToRgb} from './color-utils';import uniq from 'lodash.uniq';import {ALL_FIELD_TYPES} from 'constants/default-settings';import {validateInputData} from 'processors/data-processor';-import KeplerTable from './table-utils/kepler-table';+import KeplerTable, {Field} from './table-utils/kepler-table';++import {Datasets} from '../reducers/vis-state-updaters';+import {ProtoDataset} from '../actions';+import {RGBColor} from 'reducers/types';// apply a color for each dataset// to use as label colors@@ -38,9 +42,8 @@ const datasetColors = [/*** Random color generator- * @return {Generator<import('reducers/types').RGBColor>}*/-function* generateColor() {+function* generateColor(): Generator<RGBColor> {let index = 0;while (index < datasetColors.length + 1) {if (index === datasetColors.length) {@@ -53,7 +56,7 @@ function* generateColor() {export const datasetColorMaker = generateColor();/** @type {typeof import('./dataset-utils').getNewDatasetColor} */-export function getNewDatasetColor(datasets) {+export function getNewDatasetColor(datasets: Datasets): RGBColor {const presetColors = datasetColors.map(String);const usedColors = uniq(Object.values(datasets).map(d => String(d.color))).filter(c =>presetColors.includes(c)@@ -74,9 +77,11 @@ export function getNewDatasetColor(datasets) {/*** Take datasets payload from addDataToMap, create datasets entry save to visState- * @type {typeof import('./dataset-utils').createNewDataEntry}*/-export function createNewDataEntry({info, data, ...opts}, datasets = {}) {+export function createNewDataEntry(+ {info, data, ...opts}: ProtoDataset,+ datasets: Datasets = {}+): Datasets {const validatedData = validateInputData(data);if (!validatedData) {return {};@@ -182,7 +187,7 @@ const METRIC_DEFAULT_FIELDS = [** @param dataset*/-export function findDefaultColorField({fields, fieldPairs = []}) {+export function findDefaultColorField({fields, fieldPairs = []}: KeplerTable): null | Field {const fieldsWithoutExcluded = fields.filter(field => {if (field.type !== ALL_FIELD_TYPES.real && field.type !== ALL_FIELD_TYPES.integer) {// Only select numeric fields.@@ -243,6 +248,7 @@ export function findDefaultColorField({fields, fieldPairs = []}) {}// Finally, order based on the order in the datasets columns+ // @ts-expect-errorreturn left.index - right.index;});diff --git a/src/utils/export-utils.d.ts b/src/utils/export-utils.d.tsdeleted file mode 100644index 392fca05f..000000000--- a/src/utils/export-utils.d.ts+++ /dev/null@@ -1,18 +0,0 @@-import {ExportImage} from 'reducers/ui-state-updaters';--export const downloadFile: (fileBlob: Blob, fileName: string) => void;-export const exportJson: (state: any, options: object) => void;-export const exportHtml: (state: any, options: object) => void;-export const exportData: (state: any, options: object) => void;-export const exportMap: (state: any, options?: object) => void;-export const exportImage: (uiStateExportImage: ExportImage, fileName?: string) => void;-export const convertToPng: (sourceElem: HTMLElement, options: object) => Promise;-export const getScaleFromImageSize: (- imageW: number,- imageH: number,- mapW: number,- mapH: number-) => number;-export const dataURItoBlob: (dataURI: string) => Blob;--export const calculateExportImageSize: ({mapW, mapH, ratio, resolution}) => any;diff --git a/src/utils/export-utils.js b/src/utils/export-utils.tssimilarity index 82%rename from src/utils/export-utils.jsrename to src/utils/export-utils.tsindex 5a0c625e7..c8ecf23c2 100644--- a/src/utils/export-utils.js+++ b/src/utils/export-utils.ts@@ -18,7 +18,6 @@// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN// THE SOFTWARE.-// @ts-nocheckimport domtoimage from 'utils/dom-to-image';import {Blob, URL, atob, Uint8Array, ArrayBuffer, document} from 'global/window';import {@@ -26,7 +25,9 @@ import {EXPORT_IMG_RATIO_OPTIONS,RESOLUTIONS,EXPORT_IMG_RATIOS,- EXPORT_DATA_TYPE+ EXPORT_DATA_TYPE,+ FourByThreeRatioOption,+ OneXResolutionOption} from 'constants/default-settings';import {exportMapToHTML} from 'templates/export-map-html';import {formatCsv} from 'processors/data-processor';@@ -34,6 +35,7 @@ import get from 'lodash.get';import {set, generateHashId} from 'utils/utils';import {createIndexedDataContainer} from './table-utils/data-container-utils';+import {ExportImage} from 'reducers/ui-state-updaters';/*** Default file names@@ -45,17 +47,17 @@ export const DEFAULT_DATA_NAME = 'kepler.gl';/*** Default json export settings- * @type {{hasData: boolean}}*/export const DEFAULT_EXPORT_JSON_SETTINGS = {hasData: true};-const defaultResolution = EXPORT_IMG_RESOLUTION_OPTIONS.find(op => op.id === RESOLUTIONS.ONE_X);+const defaultResolution = OneXResolutionOption;-const defaultRatio = EXPORT_IMG_RATIO_OPTIONS.find(op => op.id === EXPORT_IMG_RATIOS.FOUR_BY_THREE);+const defaultRatio = FourByThreeRatioOption;-export function isMSEdge(window) {+export function isMSEdge(window: Window): boolean {+ // @ts-ignore msSaveOrOpenBlob was a proprietary addition to the Navigator object, added by Microsoft for Internet Explorer.return Boolean(window.navigator && window.navigator.msSaveOrOpenBlob);}@@ -69,7 +71,17 @@ export function getScaleFromImageSize(imageW = 0, imageH = 0, mapW = 0, mapH = 0return base / mapBase;}-export function calculateExportImageSize({mapW, mapH, ratio, resolution}) {+export function calculateExportImageSize({+ mapW,+ mapH,+ ratio,+ resolution+}: {+ mapW: number;+ mapH: number;+ ratio: keyof typeof EXPORT_IMG_RATIOS;+ resolution: keyof typeof RESOLUTIONS;+}) {if (mapW <= 0 || mapH <= 0) {return null;}@@ -83,7 +95,7 @@ export function calculateExportImageSize({mapW, mapH, ratio, resolution}) {const {width: imageW, height: imageH} = ratioItem.getSize(scaledWidth, scaledHeight);- const {scale} = ratioItem.id === EXPORT_IMG_RATIOS.CUSTOM ? {} : resolutionItem;+ const {scale} = ratioItem.id === EXPORT_IMG_RATIOS.CUSTOM ? {scale: undefined} : resolutionItem;return {scale,@@ -92,11 +104,11 @@ export function calculateExportImageSize({mapW, mapH, ratio, resolution}) {};}-export function convertToPng(sourceElem, options) {+export function convertToPng(sourceElem: HTMLElement, options) {return domtoimage.toPng(sourceElem, options);}-export function dataURItoBlob(dataURI) {+export function dataURItoBlob(dataURI: string): Blob {const binary = atob(dataURI.split(',')[1]);// separate out the mime component@@ -118,9 +130,9 @@ export function dataURItoBlob(dataURI) {return new Blob([ab], {type: mimeString});}-export function downloadFile(fileBlob, fileName) {+export function downloadFile(fileBlob: Blob, fileName: string) {if (isMSEdge(window)) {- window.navigator.msSaveOrOpenBlob(fileBlob, fileName);+ (window.navigator as any).msSaveOrOpenBlob(fileBlob, fileName);} else {const url = URL.createObjectURL(fileBlob);@@ -137,10 +149,9 @@ export function downloadFile(fileBlob, fileName) {/*** Whether color is rgb- * @type {typeof import('./export-utils').exportImage}* @returns*/-export function exportImage(uiStateExportImage, filename = DEFAULT_IMAGE_NAME) {+export function exportImage(uiStateExportImage: ExportImage, filename = DEFAULT_IMAGE_NAME) {const {imageDataUri} = uiStateExportImage;if (imageDataUri) {const file = dataURItoBlob(imageDataUri);@@ -152,7 +163,9 @@ export function exportToJsonString(data) {try {return JSON.stringify(data);} catch (e) {- return e.description;+ if (e instanceof TypeError) return e.message;+ // Non-Standard Error Object Property+ return (e as any).description;}}@@ -173,7 +186,7 @@ export function getMapJSON(state, options = DEFAULT_EXPORT_JSON_SETTINGS) {return mapToSave;}-export function exportJson(state, options = {}) {+export function exportJson(state, options: any = {}) {const map = getMapJSON(state, options);const fileBlob = new Blob([exportToJsonString(map)], {type: 'application/json'});@@ -231,9 +244,9 @@ export function exportData(state, options) {});}-export function exportMap(state, options) {+export function exportMap(state, options = DEFAULT_EXPORT_JSON_SETTINGS) {const {imageDataUri} = state.uiState.exportImage;- const thumbnail = imageDataUri ? dataURItoBlob(imageDataUri) : null;+ const thumbnail: Blob | null = imageDataUri ? dataURItoBlob(imageDataUri) : null;const mapToSave = getMapJSON(state, options);return {