2021-09-21 07:55:56 +00:00
|
|
|
import React from "react";
|
2020-04-05 06:34:14 +00:00
|
|
|
import { GridDefaults } from "constants/WidgetConstants";
|
2020-12-30 07:31:20 +00:00
|
|
|
import lottie from "lottie-web";
|
2021-02-11 06:36:07 +00:00
|
|
|
import confetti from "assets/lottie/binding.json";
|
2021-09-24 09:17:58 +00:00
|
|
|
import welcomeConfetti from "assets/lottie/welcome-confetti.json";
|
2021-02-11 06:36:07 +00:00
|
|
|
import successAnimation from "assets/lottie/success-animation.json";
|
2020-12-14 18:48:13 +00:00
|
|
|
import {
|
|
|
|
|
DATA_TREE_KEYWORDS,
|
2022-09-17 17:40:28 +00:00
|
|
|
DEDICATED_WORKER_GLOBAL_SCOPE_IDENTIFIERS,
|
2020-12-14 18:48:13 +00:00
|
|
|
JAVASCRIPT_KEYWORDS,
|
|
|
|
|
} from "constants/WidgetValidation";
|
2022-11-20 06:12:32 +00:00
|
|
|
import { get, set, isNil, has, uniq } from "lodash";
|
2022-11-03 16:39:51 +00:00
|
|
|
import { Workspace } from "@appsmith/constants/workspaceConstants";
|
2021-07-19 15:28:41 +00:00
|
|
|
import {
|
|
|
|
|
isPermitted,
|
|
|
|
|
PERMISSION_TYPE,
|
2022-10-20 06:03:33 +00:00
|
|
|
} from "@appsmith/utils/permissionHelpers";
|
2021-10-04 15:34:37 +00:00
|
|
|
import moment from "moment";
|
2022-02-03 05:52:14 +00:00
|
|
|
import { extraLibrariesNames, isDynamicValue } from "./DynamicBindingUtils";
|
2021-12-27 06:55:54 +00:00
|
|
|
import { ApiResponse } from "api/ApiResponses";
|
2022-02-03 05:52:14 +00:00
|
|
|
import { DSLWidget } from "widgets/constants";
|
|
|
|
|
import * as Sentry from "@sentry/react";
|
2022-03-25 10:43:26 +00:00
|
|
|
import { matchPath } from "react-router";
|
|
|
|
|
import {
|
2022-07-11 04:06:29 +00:00
|
|
|
BUILDER_CUSTOM_PATH,
|
2022-03-25 10:43:26 +00:00
|
|
|
BUILDER_PATH,
|
|
|
|
|
BUILDER_PATH_DEPRECATED,
|
2022-07-11 04:06:29 +00:00
|
|
|
VIEWER_CUSTOM_PATH,
|
2022-03-25 10:43:26 +00:00
|
|
|
VIEWER_PATH,
|
|
|
|
|
VIEWER_PATH_DEPRECATED,
|
|
|
|
|
} from "constants/routes";
|
|
|
|
|
import history from "./history";
|
2022-09-17 17:40:28 +00:00
|
|
|
import { APPSMITH_GLOBAL_FUNCTIONS } from "components/editorComponents/ActionCreator/constants";
|
2021-08-16 11:03:27 +00:00
|
|
|
|
2019-09-25 17:24:23 +00:00
|
|
|
export const snapToGrid = (
|
|
|
|
|
columnWidth: number,
|
|
|
|
|
rowHeight: number,
|
|
|
|
|
x: number,
|
|
|
|
|
y: number,
|
|
|
|
|
) => {
|
2020-01-16 11:46:21 +00:00
|
|
|
const snappedX = Math.round(x / columnWidth);
|
|
|
|
|
const snappedY = Math.round(y / rowHeight);
|
2019-09-19 22:25:37 +00:00
|
|
|
return [snappedX, snappedY];
|
|
|
|
|
};
|
2019-10-29 12:02:58 +00:00
|
|
|
|
|
|
|
|
export const formatBytes = (bytes: string | number) => {
|
|
|
|
|
if (!bytes) return;
|
|
|
|
|
const value = typeof bytes === "string" ? parseInt(bytes) : bytes;
|
|
|
|
|
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
|
|
|
|
|
if (value === 0) return "0 bytes";
|
|
|
|
|
const i = parseInt(String(Math.floor(Math.log(value) / Math.log(1024))));
|
|
|
|
|
if (i === 0) return bytes + " " + sizes[i];
|
|
|
|
|
return (value / Math.pow(1024, i)).toFixed(1) + " " + sizes[i];
|
|
|
|
|
};
|
2019-11-13 07:00:25 +00:00
|
|
|
|
|
|
|
|
export const getAbsolutePixels = (size?: string | null) => {
|
|
|
|
|
if (!size) return 0;
|
|
|
|
|
const _dex = size.indexOf("px");
|
|
|
|
|
if (_dex === -1) return 0;
|
|
|
|
|
return parseInt(size.slice(0, _dex), 10);
|
|
|
|
|
};
|
2019-12-23 12:16:33 +00:00
|
|
|
|
|
|
|
|
export const Directions: { [id: string]: string } = {
|
|
|
|
|
UP: "up",
|
|
|
|
|
DOWN: "down",
|
|
|
|
|
LEFT: "left",
|
|
|
|
|
RIGHT: "right",
|
2020-06-02 11:48:54 +00:00
|
|
|
RIGHT_BOTTOM: "RIGHT_BOTTOM",
|
2019-12-23 12:16:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type Direction = typeof Directions[keyof typeof Directions];
|
2021-08-12 05:45:38 +00:00
|
|
|
const SCROLL_THRESHOLD = 20;
|
2020-03-27 09:02:11 +00:00
|
|
|
|
|
|
|
|
export const getScrollByPixels = function(
|
2021-08-12 05:45:38 +00:00
|
|
|
elem: {
|
|
|
|
|
top: number;
|
|
|
|
|
height: number;
|
|
|
|
|
},
|
2020-03-27 09:02:11 +00:00
|
|
|
scrollParent: Element,
|
2021-08-12 05:45:38 +00:00
|
|
|
child: Element,
|
|
|
|
|
): {
|
|
|
|
|
scrollAmount: number;
|
|
|
|
|
speed: number;
|
|
|
|
|
} {
|
2020-03-27 09:02:11 +00:00
|
|
|
const scrollParentBounds = scrollParent.getBoundingClientRect();
|
2021-08-12 05:45:38 +00:00
|
|
|
const scrollChildBounds = child.getBoundingClientRect();
|
2020-04-05 06:34:14 +00:00
|
|
|
const scrollAmount =
|
2021-08-12 05:45:38 +00:00
|
|
|
2 *
|
|
|
|
|
GridDefaults.CANVAS_EXTENSION_OFFSET *
|
|
|
|
|
GridDefaults.DEFAULT_GRID_ROW_HEIGHT;
|
|
|
|
|
const topBuff =
|
|
|
|
|
elem.top + scrollChildBounds.top > 0
|
|
|
|
|
? elem.top +
|
|
|
|
|
scrollChildBounds.top -
|
|
|
|
|
SCROLL_THRESHOLD -
|
|
|
|
|
scrollParentBounds.top
|
|
|
|
|
: 0;
|
|
|
|
|
const bottomBuff =
|
|
|
|
|
scrollParentBounds.bottom -
|
|
|
|
|
(elem.top + elem.height + scrollChildBounds.top + SCROLL_THRESHOLD);
|
|
|
|
|
if (topBuff < SCROLL_THRESHOLD) {
|
|
|
|
|
const speed = Math.max(
|
|
|
|
|
(SCROLL_THRESHOLD - topBuff) / (2 * SCROLL_THRESHOLD),
|
|
|
|
|
0.1,
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
scrollAmount: 0 - scrollAmount,
|
|
|
|
|
speed,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (bottomBuff < SCROLL_THRESHOLD) {
|
|
|
|
|
const speed = Math.max(
|
|
|
|
|
(SCROLL_THRESHOLD - bottomBuff) / (2 * SCROLL_THRESHOLD),
|
|
|
|
|
0.1,
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
scrollAmount,
|
|
|
|
|
speed,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
scrollAmount: 0,
|
|
|
|
|
speed: 0,
|
|
|
|
|
};
|
2020-03-27 09:02:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const scrollElementIntoParentCanvasView = (
|
2021-08-12 05:45:38 +00:00
|
|
|
el: {
|
|
|
|
|
top: number;
|
|
|
|
|
height: number;
|
|
|
|
|
} | null,
|
2020-03-27 09:02:11 +00:00
|
|
|
parent: Element | null,
|
2021-08-12 05:45:38 +00:00
|
|
|
child: Element | null,
|
2020-03-27 09:02:11 +00:00
|
|
|
) => {
|
|
|
|
|
if (el) {
|
|
|
|
|
const scrollParent = parent;
|
2021-08-12 05:45:38 +00:00
|
|
|
if (scrollParent && child) {
|
|
|
|
|
const { scrollAmount: scrollBy } = getScrollByPixels(
|
|
|
|
|
el,
|
|
|
|
|
scrollParent,
|
|
|
|
|
child,
|
|
|
|
|
);
|
2020-03-27 09:02:11 +00:00
|
|
|
if (scrollBy < 0 && scrollParent.scrollTop > 0) {
|
|
|
|
|
scrollParent.scrollBy({ top: scrollBy, behavior: "smooth" });
|
|
|
|
|
}
|
|
|
|
|
if (scrollBy > 0) {
|
|
|
|
|
scrollParent.scrollBy({ top: scrollBy, behavior: "smooth" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-06-18 07:46:53 +00:00
|
|
|
|
2021-12-07 09:45:18 +00:00
|
|
|
export function hasClass(ele: HTMLElement, cls: string) {
|
|
|
|
|
return ele.classList.contains(cls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addClass(ele: HTMLElement, cls: string) {
|
|
|
|
|
if (!hasClass(ele, cls)) ele.classList.add(cls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function removeClass(ele: HTMLElement, cls: string) {
|
|
|
|
|
if (hasClass(ele, cls)) {
|
|
|
|
|
ele.classList.remove(cls);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 10:35:15 +00:00
|
|
|
export const removeSpecialChars = (value: string, limit?: number) => {
|
2020-06-18 07:46:53 +00:00
|
|
|
const separatorRegex = /\W+/;
|
|
|
|
|
return value
|
|
|
|
|
.split(separatorRegex)
|
|
|
|
|
.join("_")
|
|
|
|
|
.slice(0, limit || 30);
|
|
|
|
|
};
|
Feature/entity browse (#220)
# New Feature: Entity Explorer
- Entities are actions (apis and queries), datasources, pages, and widgets
- With this new feature, all entities in the application will be available
to view in the new entity explorer sidebar
- All existing application features from the api sidebar, query sidebar, datasource sidebar and pages sidebar
now are avialable on the entity explorer sidebar
- Users are now able to quickly switch to any entity in the application from the entity explorer sidebar.
- Users can also search all entities in the application from the new sidebar. Use cmd + f or ctrl + f to focus on the search input
- Users can rename entities from the new sidebar
- Users can also perform contextual actions on these entities like set a page as home page, copy/move actions, delete entity, etc from the context menu available alongside the entities in the sidebar
- Users can view the properties of the entities in the sidebar, as well as copy bindings to use in the application.
2020-08-10 08:52:45 +00:00
|
|
|
|
2021-09-21 07:55:56 +00:00
|
|
|
export const flashElement = (
|
|
|
|
|
el: HTMLElement,
|
|
|
|
|
flashTimeout = 1000,
|
2021-12-07 09:45:18 +00:00
|
|
|
flashClass = "flash",
|
2021-09-21 07:55:56 +00:00
|
|
|
) => {
|
2021-12-07 09:45:18 +00:00
|
|
|
if (!el) return;
|
|
|
|
|
addClass(el, flashClass);
|
Feature/entity browse (#220)
# New Feature: Entity Explorer
- Entities are actions (apis and queries), datasources, pages, and widgets
- With this new feature, all entities in the application will be available
to view in the new entity explorer sidebar
- All existing application features from the api sidebar, query sidebar, datasource sidebar and pages sidebar
now are avialable on the entity explorer sidebar
- Users are now able to quickly switch to any entity in the application from the entity explorer sidebar.
- Users can also search all entities in the application from the new sidebar. Use cmd + f or ctrl + f to focus on the search input
- Users can rename entities from the new sidebar
- Users can also perform contextual actions on these entities like set a page as home page, copy/move actions, delete entity, etc from the context menu available alongside the entities in the sidebar
- Users can view the properties of the entities in the sidebar, as well as copy bindings to use in the application.
2020-08-10 08:52:45 +00:00
|
|
|
setTimeout(() => {
|
2021-12-07 09:45:18 +00:00
|
|
|
removeClass(el, flashClass);
|
2021-09-21 07:55:56 +00:00
|
|
|
}, flashTimeout);
|
Feature/entity browse (#220)
# New Feature: Entity Explorer
- Entities are actions (apis and queries), datasources, pages, and widgets
- With this new feature, all entities in the application will be available
to view in the new entity explorer sidebar
- All existing application features from the api sidebar, query sidebar, datasource sidebar and pages sidebar
now are avialable on the entity explorer sidebar
- Users are now able to quickly switch to any entity in the application from the entity explorer sidebar.
- Users can also search all entities in the application from the new sidebar. Use cmd + f or ctrl + f to focus on the search input
- Users can rename entities from the new sidebar
- Users can also perform contextual actions on these entities like set a page as home page, copy/move actions, delete entity, etc from the context menu available alongside the entities in the sidebar
- Users can view the properties of the entities in the sidebar, as well as copy bindings to use in the application.
2020-08-10 08:52:45 +00:00
|
|
|
};
|
|
|
|
|
|
2021-08-25 05:00:31 +00:00
|
|
|
/**
|
|
|
|
|
* flash elements with a background color
|
|
|
|
|
*
|
|
|
|
|
* @param id
|
2021-09-21 07:55:56 +00:00
|
|
|
* @param timeout
|
|
|
|
|
* @param flashTimeout
|
|
|
|
|
* @param flashColor
|
2021-08-25 05:00:31 +00:00
|
|
|
*/
|
2021-09-21 07:55:56 +00:00
|
|
|
export const flashElementsById = (
|
|
|
|
|
id: string | string[],
|
|
|
|
|
timeout = 0,
|
|
|
|
|
flashTimeout?: number,
|
2021-12-07 09:45:18 +00:00
|
|
|
flashClass?: string,
|
2021-09-21 07:55:56 +00:00
|
|
|
) => {
|
2021-08-25 05:00:31 +00:00
|
|
|
let ids: string[] = [];
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(id)) {
|
|
|
|
|
ids = ids.concat(id);
|
|
|
|
|
} else {
|
|
|
|
|
ids = ids.concat([id]);
|
|
|
|
|
}
|
Feature/entity browse (#220)
# New Feature: Entity Explorer
- Entities are actions (apis and queries), datasources, pages, and widgets
- With this new feature, all entities in the application will be available
to view in the new entity explorer sidebar
- All existing application features from the api sidebar, query sidebar, datasource sidebar and pages sidebar
now are avialable on the entity explorer sidebar
- Users are now able to quickly switch to any entity in the application from the entity explorer sidebar.
- Users can also search all entities in the application from the new sidebar. Use cmd + f or ctrl + f to focus on the search input
- Users can rename entities from the new sidebar
- Users can also perform contextual actions on these entities like set a page as home page, copy/move actions, delete entity, etc from the context menu available alongside the entities in the sidebar
- Users can view the properties of the entities in the sidebar, as well as copy bindings to use in the application.
2020-08-10 08:52:45 +00:00
|
|
|
|
2021-08-25 05:00:31 +00:00
|
|
|
ids.forEach((id) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
const el = document.getElementById(id);
|
|
|
|
|
|
2021-12-07 09:45:18 +00:00
|
|
|
if (el) flashElement(el, flashTimeout, flashClass);
|
2021-08-25 05:00:31 +00:00
|
|
|
}, timeout);
|
|
|
|
|
});
|
Feature/entity browse (#220)
# New Feature: Entity Explorer
- Entities are actions (apis and queries), datasources, pages, and widgets
- With this new feature, all entities in the application will be available
to view in the new entity explorer sidebar
- All existing application features from the api sidebar, query sidebar, datasource sidebar and pages sidebar
now are avialable on the entity explorer sidebar
- Users are now able to quickly switch to any entity in the application from the entity explorer sidebar.
- Users can also search all entities in the application from the new sidebar. Use cmd + f or ctrl + f to focus on the search input
- Users can rename entities from the new sidebar
- Users can also perform contextual actions on these entities like set a page as home page, copy/move actions, delete entity, etc from the context menu available alongside the entities in the sidebar
- Users can view the properties of the entities in the sidebar, as well as copy bindings to use in the application.
2020-08-10 08:52:45 +00:00
|
|
|
};
|
2020-08-27 10:35:15 +00:00
|
|
|
|
2022-10-17 15:16:38 +00:00
|
|
|
/**
|
|
|
|
|
* Scrolls to the widget of WidgetId without any animantion.
|
|
|
|
|
* @param widgetId
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const quickScrollToWidget = (widgetId?: string) => {
|
|
|
|
|
if (!widgetId) return;
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
const el = document.getElementById(widgetId);
|
2022-11-15 06:20:18 +00:00
|
|
|
const canvas = document.getElementById("canvas-viewport");
|
|
|
|
|
|
|
|
|
|
if (el && canvas && !isElementVisibleInContainer(el, canvas)) {
|
2022-11-28 05:44:31 +00:00
|
|
|
el.scrollIntoView({
|
|
|
|
|
block: "nearest",
|
|
|
|
|
behavior: "smooth",
|
|
|
|
|
});
|
2022-10-17 15:16:38 +00:00
|
|
|
}
|
2022-11-15 06:20:18 +00:00
|
|
|
}, 200);
|
2022-10-17 15:16:38 +00:00
|
|
|
};
|
|
|
|
|
|
2022-11-15 06:20:18 +00:00
|
|
|
// Checks if the element in a container is visible or not.
|
|
|
|
|
// Can be used to decide if scroll is needed
|
|
|
|
|
function isElementVisibleInContainer(
|
|
|
|
|
element: HTMLElement,
|
|
|
|
|
container: HTMLElement,
|
|
|
|
|
) {
|
|
|
|
|
const elementRect = element.getBoundingClientRect();
|
|
|
|
|
const containerRect = container.getBoundingClientRect();
|
|
|
|
|
return (
|
2022-11-28 05:44:31 +00:00
|
|
|
((elementRect.top > containerRect.top &&
|
|
|
|
|
elementRect.top < containerRect.bottom) ||
|
|
|
|
|
(elementRect.bottom < containerRect.bottom &&
|
|
|
|
|
elementRect.bottom > containerRect.top)) &&
|
|
|
|
|
((elementRect.left > containerRect.left &&
|
|
|
|
|
elementRect.left < containerRect.right) ||
|
|
|
|
|
(elementRect.right < containerRect.right &&
|
|
|
|
|
elementRect.right > containerRect.left))
|
2022-11-15 06:20:18 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 10:35:15 +00:00
|
|
|
export const resolveAsSpaceChar = (value: string, limit?: number) => {
|
2021-06-12 15:05:26 +00:00
|
|
|
// ensures that all special characters are disallowed
|
|
|
|
|
// while allowing all utf-8 characters
|
|
|
|
|
const removeSpecialCharsRegex = /`|\~|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\+|\=|\[|\{|\]|\}|\||\\|\'|\<|\,|\.|\>|\?|\/|\""|\;|\:|\s/;
|
2020-10-19 05:18:27 +00:00
|
|
|
const duplicateSpaceRegex = /\s+/;
|
2020-08-27 10:35:15 +00:00
|
|
|
return value
|
2021-06-12 15:05:26 +00:00
|
|
|
.split(removeSpecialCharsRegex)
|
|
|
|
|
.join(" ")
|
2020-10-19 05:18:27 +00:00
|
|
|
.split(duplicateSpaceRegex)
|
2020-08-27 10:35:15 +00:00
|
|
|
.join(" ")
|
|
|
|
|
.slice(0, limit || 30);
|
|
|
|
|
};
|
2020-09-16 10:28:01 +00:00
|
|
|
|
2022-07-20 07:13:23 +00:00
|
|
|
export const PLATFORM_OS = {
|
|
|
|
|
MAC: "MAC",
|
|
|
|
|
IOS: "IOS",
|
|
|
|
|
LINUX: "LINUX",
|
|
|
|
|
ANDROID: "ANDROID",
|
|
|
|
|
WINDOWS: "WINDOWS",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const platformOSRegex = {
|
|
|
|
|
[PLATFORM_OS.MAC]: /mac.*/i,
|
|
|
|
|
[PLATFORM_OS.IOS]: /(?:iphone|ipod|ipad|Pike v.*)/i,
|
|
|
|
|
[PLATFORM_OS.LINUX]: /(?:linux.*)/i,
|
|
|
|
|
[PLATFORM_OS.ANDROID]: /android.*|aarch64|arm.*/i,
|
|
|
|
|
[PLATFORM_OS.WINDOWS]: /win.*/i,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getPlatformOS = () => {
|
|
|
|
|
const browserPlatform =
|
|
|
|
|
typeof navigator !== "undefined" ? navigator.platform : null;
|
|
|
|
|
if (browserPlatform) {
|
|
|
|
|
const platformOSList = Object.entries(platformOSRegex);
|
|
|
|
|
const platform = platformOSList.find(([, regex]) =>
|
|
|
|
|
regex.test(browserPlatform),
|
|
|
|
|
);
|
|
|
|
|
return platform ? platform[0] : null;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const isMacOrIOS = () => {
|
|
|
|
|
const platformOS = getPlatformOS();
|
|
|
|
|
return platformOS === PLATFORM_OS.MAC || platformOS === PLATFORM_OS.IOS;
|
2020-09-16 10:28:01 +00:00
|
|
|
};
|
2020-10-26 07:00:01 +00:00
|
|
|
|
2022-08-03 06:02:23 +00:00
|
|
|
export const getBrowserInfo = () => {
|
|
|
|
|
const userAgent =
|
|
|
|
|
typeof navigator !== "undefined" ? navigator.userAgent : null;
|
|
|
|
|
|
|
|
|
|
if (userAgent) {
|
|
|
|
|
let specificMatch;
|
|
|
|
|
let match =
|
|
|
|
|
userAgent.match(
|
|
|
|
|
/(opera|chrome|safari|firefox|msie|CriOS|trident(?=\/))\/?\s*(\d+)/i,
|
|
|
|
|
) || [];
|
|
|
|
|
|
|
|
|
|
// browser
|
|
|
|
|
if (/CriOS/i.test(match[1])) match[1] = "Chrome";
|
|
|
|
|
|
|
|
|
|
if (match[1] === "Chrome") {
|
|
|
|
|
specificMatch = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
|
|
|
|
|
if (specificMatch) {
|
|
|
|
|
const opera = specificMatch.slice(1);
|
|
|
|
|
return {
|
|
|
|
|
browser: opera[0].replace("OPR", "Opera"),
|
|
|
|
|
version: opera[1],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
specificMatch = userAgent.match(/\b(Edg)\/(\d+)/);
|
|
|
|
|
if (specificMatch) {
|
|
|
|
|
const edge = specificMatch.slice(1);
|
|
|
|
|
return {
|
|
|
|
|
browser: edge[0].replace("Edg", "Edge (Chromium)"),
|
|
|
|
|
version: edge[1],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// version
|
|
|
|
|
match = match[2]
|
|
|
|
|
? [match[1], match[2]]
|
|
|
|
|
: [navigator.appName, navigator.appVersion, "-?"];
|
|
|
|
|
const version = userAgent.match(/version\/(\d+)/i);
|
|
|
|
|
|
|
|
|
|
version && match.splice(1, 1, version[1]);
|
|
|
|
|
|
|
|
|
|
return { browser: match[0], version: match[1] };
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-26 07:00:01 +00:00
|
|
|
/**
|
|
|
|
|
* Removes the trailing slashes from the path
|
|
|
|
|
* @param path
|
|
|
|
|
* @example
|
|
|
|
|
* ```js
|
|
|
|
|
* let trimmedUrl = trimTrailingSlash('/url/')
|
|
|
|
|
* console.log(trimmedUrl) //will output /url
|
|
|
|
|
* ```
|
|
|
|
|
* @example
|
|
|
|
|
* ```js
|
|
|
|
|
* let trimmedUrl = trimTrailingSlash('/yet-another-url//')
|
|
|
|
|
* console.log(trimmedUrl) // will output /yet-another-url
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
export const trimTrailingSlash = (path: string) => {
|
|
|
|
|
const trailingUrlRegex = /\/+$/;
|
|
|
|
|
return path.replace(trailingUrlRegex, "");
|
|
|
|
|
};
|
2020-11-09 11:30:34 +00:00
|
|
|
|
|
|
|
|
/**
|
2020-11-10 10:28:21 +00:00
|
|
|
* checks if ellipsis is active
|
|
|
|
|
* this function is meant for checking the existence of ellipsis by CSS.
|
|
|
|
|
* Since ellipsis by CSS are not part of DOM, we are checking with scroll width\height and offsetidth\height.
|
|
|
|
|
* ScrollWidth\ScrollHeight is always greater than the offsetWidth\OffsetHeight when ellipsis made by CSS is active.
|
2022-02-23 23:53:49 +00:00
|
|
|
* Using clientWidth to fix this https://stackoverflow.com/a/21064102/8692954
|
2020-11-09 11:30:34 +00:00
|
|
|
* @param element
|
|
|
|
|
*/
|
|
|
|
|
export const isEllipsisActive = (element: HTMLElement | null) => {
|
2022-03-24 07:44:53 +00:00
|
|
|
return element && element.clientWidth < element.scrollWidth;
|
2020-11-09 11:30:34 +00:00
|
|
|
};
|
2020-11-12 12:05:19 +00:00
|
|
|
|
2022-03-24 07:44:53 +00:00
|
|
|
export const isVerticalEllipsisActive = (element: HTMLElement | null) => {
|
|
|
|
|
return element && element.clientHeight < element.scrollHeight;
|
|
|
|
|
};
|
2020-11-12 12:05:19 +00:00
|
|
|
/**
|
|
|
|
|
* converts array to sentences
|
|
|
|
|
* for e.g - ['Pawan', 'Abhinav', 'Hetu'] --> 'Pawan, Abhinav and Hetu'
|
|
|
|
|
*
|
|
|
|
|
* @param arr string[]
|
|
|
|
|
*/
|
|
|
|
|
export const convertArrayToSentence = (arr: string[]) => {
|
|
|
|
|
return arr.join(", ").replace(/,\s([^,]+)$/, " and $1");
|
|
|
|
|
};
|
2020-11-23 09:27:00 +00:00
|
|
|
|
|
|
|
|
/**
|
2020-12-14 18:48:13 +00:00
|
|
|
* checks if the name is conflicting with
|
2020-11-23 09:27:00 +00:00
|
|
|
* 1. API names,
|
|
|
|
|
* 2. Queries name
|
|
|
|
|
* 3. Javascript reserved names
|
|
|
|
|
* 4. Few internal function names that are in the evaluation tree
|
|
|
|
|
*
|
|
|
|
|
* return if false name conflicts with anything from the above list
|
|
|
|
|
*
|
|
|
|
|
* @param name
|
|
|
|
|
* @param invalidNames
|
|
|
|
|
*/
|
|
|
|
|
export const isNameValid = (
|
|
|
|
|
name: string,
|
|
|
|
|
invalidNames: Record<string, any>,
|
|
|
|
|
) => {
|
2020-12-14 18:48:13 +00:00
|
|
|
return !(
|
2022-09-17 17:40:28 +00:00
|
|
|
has(JAVASCRIPT_KEYWORDS, name) ||
|
|
|
|
|
has(DATA_TREE_KEYWORDS, name) ||
|
|
|
|
|
has(DEDICATED_WORKER_GLOBAL_SCOPE_IDENTIFIERS, name) ||
|
|
|
|
|
has(APPSMITH_GLOBAL_FUNCTIONS, name) ||
|
|
|
|
|
has(extraLibrariesNames, name) ||
|
|
|
|
|
has(invalidNames, name)
|
2020-12-14 18:48:13 +00:00
|
|
|
);
|
2020-11-23 09:27:00 +00:00
|
|
|
};
|
2020-12-30 07:31:20 +00:00
|
|
|
|
2021-04-23 05:43:13 +00:00
|
|
|
/*
|
|
|
|
|
* Filter out empty items from an array
|
|
|
|
|
* for e.g - ['Pawan', undefined, 'Hetu'] --> ['Pawan', 'Hetu']
|
|
|
|
|
*
|
|
|
|
|
* @param array any[]
|
|
|
|
|
*/
|
|
|
|
|
export const removeFalsyEntries = (arr: any[]): any[] => {
|
|
|
|
|
return arr.filter(Boolean);
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-11 06:25:00 +00:00
|
|
|
/**
|
|
|
|
|
* checks if variable passed is of type string or not
|
|
|
|
|
*
|
|
|
|
|
* for e.g -> 'Pawan' -> true
|
|
|
|
|
* ['Pawan', 'Goku'] -> false
|
|
|
|
|
* { name: "Pawan"} -> false
|
|
|
|
|
*/
|
|
|
|
|
export const isString = (str: any) => {
|
|
|
|
|
return typeof str === "string" || str instanceof String;
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-13 12:03:39 +00:00
|
|
|
/**
|
|
|
|
|
* Returns substring between two set of strings
|
|
|
|
|
* eg ->
|
|
|
|
|
* getSubstringBetweenTwoWords("abcdefgh", "abc", "fgh") -> de
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
export const getSubstringBetweenTwoWords = (
|
|
|
|
|
str: string,
|
|
|
|
|
startWord: string,
|
|
|
|
|
endWord: string,
|
|
|
|
|
) => {
|
|
|
|
|
const endIndexOfStartWord = str.indexOf(startWord) + startWord.length;
|
|
|
|
|
const startIndexOfEndWord = str.lastIndexOf(endWord);
|
|
|
|
|
|
|
|
|
|
if (startIndexOfEndWord < endIndexOfStartWord) return "";
|
|
|
|
|
|
|
|
|
|
return str.substring(startIndexOfEndWord, endIndexOfStartWord);
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-30 07:31:20 +00:00
|
|
|
export const playOnboardingAnimation = () => {
|
2021-02-11 06:36:07 +00:00
|
|
|
playLottieAnimation("#root", confetti);
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-24 09:17:58 +00:00
|
|
|
export const playWelcomeAnimation = (container: string) => {
|
|
|
|
|
playLottieAnimation(container, welcomeConfetti);
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-11 06:36:07 +00:00
|
|
|
export const playOnboardingStepCompletionAnimation = () => {
|
|
|
|
|
playLottieAnimation(".onboarding-step-indicator", successAnimation, {
|
|
|
|
|
"background-color": "white",
|
|
|
|
|
padding: "60px",
|
|
|
|
|
});
|
|
|
|
|
};
|
2020-12-30 07:31:20 +00:00
|
|
|
|
2021-02-11 06:36:07 +00:00
|
|
|
const playLottieAnimation = (
|
|
|
|
|
selector: string,
|
|
|
|
|
animation: any,
|
|
|
|
|
styles?: any,
|
|
|
|
|
) => {
|
|
|
|
|
const container: Element = document.querySelector(selector) as Element;
|
|
|
|
|
|
|
|
|
|
if (!container) return;
|
2020-12-30 07:31:20 +00:00
|
|
|
const el = document.createElement("div");
|
|
|
|
|
Object.assign(el.style, {
|
|
|
|
|
position: "absolute",
|
|
|
|
|
left: 0,
|
|
|
|
|
right: 0,
|
|
|
|
|
top: 0,
|
|
|
|
|
bottom: 0,
|
|
|
|
|
"z-index": 99,
|
|
|
|
|
width: "100%",
|
|
|
|
|
height: "100%",
|
2021-02-11 06:36:07 +00:00
|
|
|
...styles,
|
2020-12-30 07:31:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
container.appendChild(el);
|
|
|
|
|
|
|
|
|
|
const animObj = lottie.loadAnimation({
|
|
|
|
|
container: el,
|
2021-02-11 06:36:07 +00:00
|
|
|
animationData: animation,
|
2020-12-30 07:31:20 +00:00
|
|
|
loop: false,
|
|
|
|
|
});
|
2021-02-11 06:36:07 +00:00
|
|
|
|
2020-12-30 07:31:20 +00:00
|
|
|
const duration = (animObj.totalFrames / animObj.frameRate) * 1000;
|
|
|
|
|
|
|
|
|
|
animObj.play();
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
container.removeChild(el);
|
|
|
|
|
}, duration);
|
|
|
|
|
};
|
2021-03-08 08:24:12 +00:00
|
|
|
|
|
|
|
|
export const getSelectedText = () => {
|
|
|
|
|
if (typeof window.getSelection === "function") {
|
|
|
|
|
const selectionObj = window.getSelection();
|
|
|
|
|
return selectionObj && selectionObj.toString();
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-03-11 04:55:37 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* calculates and returns the scrollwidth
|
|
|
|
|
*
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const scrollbarWidth = () => {
|
|
|
|
|
const scrollDiv = document.createElement("div");
|
|
|
|
|
scrollDiv.setAttribute(
|
|
|
|
|
"style",
|
|
|
|
|
"width: 100px; height: 100px; overflow: scroll; position:absolute; top:-9999px;",
|
|
|
|
|
);
|
|
|
|
|
document.body.appendChild(scrollDiv);
|
|
|
|
|
const scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
|
|
|
|
|
document.body.removeChild(scrollDiv);
|
|
|
|
|
return scrollbarWidth;
|
|
|
|
|
};
|
2021-05-18 13:54:40 +00:00
|
|
|
|
|
|
|
|
// Flatten object
|
|
|
|
|
// From { isValid: false, settings: { color: false}}
|
|
|
|
|
// To { isValid: false, settings.color: false}
|
|
|
|
|
export const flattenObject = (data: Record<string, any>) => {
|
|
|
|
|
const result: Record<string, any> = {};
|
2022-03-10 05:38:50 +00:00
|
|
|
|
2021-05-18 13:54:40 +00:00
|
|
|
function recurse(cur: any, prop: any) {
|
|
|
|
|
if (Object(cur) !== cur) {
|
|
|
|
|
result[prop] = cur;
|
|
|
|
|
} else if (Array.isArray(cur)) {
|
|
|
|
|
for (let i = 0, l = cur.length; i < l; i++)
|
|
|
|
|
recurse(cur[i], prop + "[" + i + "]");
|
|
|
|
|
if (cur.length == 0) result[prop] = [];
|
|
|
|
|
} else {
|
|
|
|
|
let isEmpty = true;
|
|
|
|
|
for (const p in cur) {
|
|
|
|
|
isEmpty = false;
|
|
|
|
|
recurse(cur[p], prop ? prop + "." + p : p);
|
|
|
|
|
}
|
|
|
|
|
if (isEmpty && prop) result[prop] = {};
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-10 05:38:50 +00:00
|
|
|
|
2021-05-18 13:54:40 +00:00
|
|
|
recurse(data, "");
|
|
|
|
|
return result;
|
|
|
|
|
};
|
2021-06-18 07:42:57 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* renames key in object
|
|
|
|
|
*
|
|
|
|
|
* @param object
|
|
|
|
|
* @param key
|
|
|
|
|
* @param newKey
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const renameKeyInObject = (object: any, key: string, newKey: string) => {
|
|
|
|
|
if (object[key]) {
|
|
|
|
|
set(object, newKey, object[key]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return object;
|
|
|
|
|
};
|
2021-07-19 15:28:41 +00:00
|
|
|
|
2022-06-15 15:37:41 +00:00
|
|
|
// Can be used to check if the user has developer role access to workspace
|
|
|
|
|
export const getCanCreateApplications = (currentWorkspace: Workspace) => {
|
|
|
|
|
const userWorkspacePermissions = currentWorkspace.userPermissions || [];
|
2021-07-19 15:28:41 +00:00
|
|
|
const canManage = isPermitted(
|
2022-06-15 15:37:41 +00:00
|
|
|
userWorkspacePermissions,
|
2021-08-06 09:53:05 +00:00
|
|
|
PERMISSION_TYPE.CREATE_APPLICATION,
|
2021-07-19 15:28:41 +00:00
|
|
|
);
|
|
|
|
|
return canManage;
|
|
|
|
|
};
|
2021-07-29 08:49:46 +00:00
|
|
|
|
|
|
|
|
export const getIsSafeRedirectURL = (redirectURL: string) => {
|
|
|
|
|
try {
|
|
|
|
|
return new URL(redirectURL).origin === window.location.origin;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-08-16 11:03:27 +00:00
|
|
|
|
2021-09-23 11:46:47 +00:00
|
|
|
export const stopClickEventPropagation = (
|
|
|
|
|
e: React.MouseEvent<HTMLDivElement, MouseEvent>,
|
|
|
|
|
) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-04 15:34:37 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* Get text for how much time before an action happened
|
|
|
|
|
* Eg: 1 Month, 12 Seconds
|
|
|
|
|
*
|
|
|
|
|
* @param date 2021-09-08T14:14:12Z
|
|
|
|
|
*
|
|
|
|
|
*/
|
2022-03-10 05:38:50 +00:00
|
|
|
export const howMuchTimeBeforeText = (
|
|
|
|
|
date: string,
|
|
|
|
|
options: { lessThanAMinute: boolean } = { lessThanAMinute: false },
|
|
|
|
|
) => {
|
2021-10-04 15:34:37 +00:00
|
|
|
if (!date || !moment.isMoment(moment(date))) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-10 05:38:50 +00:00
|
|
|
const { lessThanAMinute } = options;
|
|
|
|
|
|
2021-10-04 15:34:37 +00:00
|
|
|
const now = moment();
|
|
|
|
|
const checkDate = moment(date);
|
|
|
|
|
const years = now.diff(checkDate, "years");
|
|
|
|
|
const months = now.diff(checkDate, "months");
|
|
|
|
|
const days = now.diff(checkDate, "days");
|
|
|
|
|
const hours = now.diff(checkDate, "hours");
|
|
|
|
|
const minutes = now.diff(checkDate, "minutes");
|
|
|
|
|
const seconds = now.diff(checkDate, "seconds");
|
|
|
|
|
if (years > 0) return `${years} yr${years > 1 ? "s" : ""}`;
|
|
|
|
|
else if (months > 0) return `${months} mth${months > 1 ? "s" : ""}`;
|
|
|
|
|
else if (days > 0) return `${days} day${days > 1 ? "s" : ""}`;
|
|
|
|
|
else if (hours > 0) return `${hours} hr${hours > 1 ? "s" : ""}`;
|
|
|
|
|
else if (minutes > 0) return `${minutes} min${minutes > 1 ? "s" : ""}`;
|
2022-03-10 05:38:50 +00:00
|
|
|
else
|
|
|
|
|
return lessThanAMinute
|
|
|
|
|
? "less than a minute"
|
|
|
|
|
: `${seconds} sec${seconds > 1 ? "s" : ""}`;
|
2021-10-04 15:34:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* Truncate string and append given string in the end
|
|
|
|
|
* eg: Flint Lockwood Diatonic Super Mutating Dynamic Food Replicator
|
|
|
|
|
* -> Flint...
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
export const truncateString = (
|
|
|
|
|
str: string,
|
|
|
|
|
limit: number,
|
|
|
|
|
appendStr = "...",
|
|
|
|
|
) => {
|
|
|
|
|
if (str.length <= limit) return str;
|
|
|
|
|
let _subString = str.substring(0, limit);
|
|
|
|
|
_subString = _subString.trim() + appendStr;
|
|
|
|
|
return _subString;
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-21 07:55:56 +00:00
|
|
|
/**
|
|
|
|
|
* returns the modText ( ctrl or command ) based on the user machine
|
|
|
|
|
*
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
2022-07-20 07:13:23 +00:00
|
|
|
export const modText = () => (isMacOrIOS() ? <span>⌘</span> : "Ctrl +");
|
|
|
|
|
export const altText = () => (isMacOrIOS() ? <span>⌥</span> : "Alt +");
|
|
|
|
|
export const shiftText = () =>
|
|
|
|
|
isMacOrIOS() ? <span>⇪</span> : "Shift +";
|
2021-10-18 14:03:44 +00:00
|
|
|
|
2022-03-14 03:24:53 +00:00
|
|
|
export const undoShortCut = () => <span>{modText()} Z</span>;
|
2021-12-07 09:45:18 +00:00
|
|
|
|
|
|
|
|
export const redoShortCut = () =>
|
2022-07-20 07:13:23 +00:00
|
|
|
isMacOrIOS() ? (
|
2021-12-07 09:45:18 +00:00
|
|
|
<span>
|
2022-03-14 03:24:53 +00:00
|
|
|
{modText()} {shiftText()} Z
|
2021-12-07 09:45:18 +00:00
|
|
|
</span>
|
|
|
|
|
) : (
|
2022-03-14 03:24:53 +00:00
|
|
|
<span>{modText()} Y</span>
|
2021-12-07 09:45:18 +00:00
|
|
|
);
|
|
|
|
|
|
2021-10-18 14:03:44 +00:00
|
|
|
/**
|
|
|
|
|
* @returns the original string after trimming the string past `?`
|
|
|
|
|
*/
|
|
|
|
|
export const trimQueryString = (value = "") => {
|
|
|
|
|
const index = value.indexOf("?");
|
|
|
|
|
if (index === -1) return value;
|
|
|
|
|
return value.slice(0, index);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* returns the value in the query string for a key
|
|
|
|
|
*/
|
|
|
|
|
export const getSearchQuery = (search = "", key: string) => {
|
|
|
|
|
const params = new URLSearchParams(search);
|
|
|
|
|
return decodeURIComponent(params.get(key) || "");
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-23 08:01:46 +00:00
|
|
|
/*
|
|
|
|
|
* unfocus all window selection
|
|
|
|
|
*
|
|
|
|
|
* @param document
|
|
|
|
|
* @param window
|
|
|
|
|
*/
|
|
|
|
|
export function unFocus(document: Document, window: Window) {
|
|
|
|
|
if (document.getSelection()) {
|
|
|
|
|
document.getSelection()?.empty();
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
window.getSelection()?.removeAllRanges();
|
|
|
|
|
// eslint-disable-next-line no-empty
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-27 06:55:54 +00:00
|
|
|
|
|
|
|
|
export function getLogToSentryFromResponse(response?: ApiResponse) {
|
|
|
|
|
return response && response?.responseMeta?.status >= 500;
|
|
|
|
|
}
|
2022-01-18 07:52:24 +00:00
|
|
|
|
2022-05-04 09:45:57 +00:00
|
|
|
const BLACKLIST_COLORS = ["#ffffff"];
|
|
|
|
|
const HEX_REGEX = /#[0-9a-fA-F]{6}/gi;
|
|
|
|
|
const RGB_REGEX = /rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)/gi;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* extract colors from string
|
|
|
|
|
*
|
|
|
|
|
* @param text
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export function extractColorsFromString(text: string) {
|
|
|
|
|
const colors = new Set();
|
|
|
|
|
|
|
|
|
|
[...(text.match(RGB_REGEX) || []), ...(text.match(HEX_REGEX) || [])]
|
|
|
|
|
.filter((d) => BLACKLIST_COLORS.indexOf(d.toLowerCase()) === -1)
|
|
|
|
|
.forEach((color) => {
|
|
|
|
|
colors.add(color.toLowerCase());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Array.from(colors) as Array<string>;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 07:52:24 +00:00
|
|
|
/*
|
|
|
|
|
* Function to merge property pane config of a widget
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
export const mergeWidgetConfig = (target: any, source: any) => {
|
|
|
|
|
const sectionMap: Record<string, any> = {};
|
|
|
|
|
|
|
|
|
|
target.forEach((section: { sectionName: string }) => {
|
|
|
|
|
sectionMap[section.sectionName] = section;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
source.forEach((section: { sectionName: string; children: any[] }) => {
|
|
|
|
|
const targetSection = sectionMap[section.sectionName];
|
|
|
|
|
|
|
|
|
|
if (targetSection) {
|
|
|
|
|
Array.prototype.push.apply(targetSection.children, section.children);
|
|
|
|
|
} else {
|
|
|
|
|
target.push(section);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return target;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getLocale = () => {
|
|
|
|
|
return navigator.languages?.[0] || "en-US";
|
|
|
|
|
};
|
2022-02-03 05:52:14 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Function to check if the DynamicBindingPathList is valid
|
|
|
|
|
* @param currentDSL
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const captureInvalidDynamicBindingPath = (
|
|
|
|
|
currentDSL: Readonly<DSLWidget>,
|
|
|
|
|
) => {
|
|
|
|
|
//Get the dynamicBindingPathList of the current DSL
|
|
|
|
|
const dynamicBindingPathList = get(currentDSL, "dynamicBindingPathList");
|
|
|
|
|
dynamicBindingPathList?.forEach((dBindingPath) => {
|
|
|
|
|
const pathValue = get(currentDSL, dBindingPath.key); //Gets the value for the given dynamic binding path
|
|
|
|
|
/**
|
|
|
|
|
* Checks if dynamicBindingPathList contains a property path that doesn't have a binding
|
|
|
|
|
*/
|
|
|
|
|
if (!isDynamicValue(pathValue)) {
|
|
|
|
|
Sentry.captureException(
|
|
|
|
|
new Error(
|
|
|
|
|
`INVALID_DynamicPathBinding_CLIENT_ERROR: Invalid dynamic path binding list: ${currentDSL.widgetName}.${dBindingPath.key}`,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (currentDSL.children) {
|
|
|
|
|
currentDSL.children.map(captureInvalidDynamicBindingPath);
|
|
|
|
|
}
|
|
|
|
|
return currentDSL;
|
|
|
|
|
};
|
2022-03-21 07:57:25 +00:00
|
|
|
|
2022-06-21 13:57:34 +00:00
|
|
|
/**
|
|
|
|
|
* Function to handle undefined returned in case of using [].find()
|
|
|
|
|
* @param result
|
|
|
|
|
* @param errorMessage
|
|
|
|
|
* @returns the result if not undefined or throws an Error
|
|
|
|
|
*/
|
|
|
|
|
export function shouldBeDefined<T>(
|
|
|
|
|
result: T | undefined | null,
|
|
|
|
|
errorMessage: string,
|
|
|
|
|
): T {
|
|
|
|
|
if (result === undefined || result === null) {
|
|
|
|
|
throw new TypeError(errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-03-21 07:57:25 +00:00
|
|
|
/*
|
|
|
|
|
* Check if a value is null / undefined / empty string
|
|
|
|
|
*
|
|
|
|
|
* @param value: any
|
|
|
|
|
*/
|
|
|
|
|
export const isEmptyOrNill = (value: any) => {
|
|
|
|
|
return isNil(value) || (isString(value) && value === "");
|
2022-03-26 11:00:31 +00:00
|
|
|
};
|
|
|
|
|
|
2022-03-25 10:43:26 +00:00
|
|
|
export const isURLDeprecated = (url: string) => {
|
|
|
|
|
return !!matchPath(url, {
|
|
|
|
|
path: [
|
|
|
|
|
trimQueryString(BUILDER_PATH_DEPRECATED),
|
|
|
|
|
trimQueryString(VIEWER_PATH_DEPRECATED),
|
|
|
|
|
],
|
|
|
|
|
strict: false,
|
|
|
|
|
exact: false,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getUpdatedRoute = (
|
|
|
|
|
path: string,
|
|
|
|
|
params: Record<string, string>,
|
|
|
|
|
) => {
|
|
|
|
|
let updatedPath = path;
|
|
|
|
|
const match = matchPath<{ applicationSlug: string; pageSlug: string }>(path, {
|
|
|
|
|
path: [trimQueryString(BUILDER_PATH), trimQueryString(VIEWER_PATH)],
|
|
|
|
|
strict: false,
|
|
|
|
|
exact: false,
|
|
|
|
|
});
|
|
|
|
|
if (match?.params) {
|
|
|
|
|
const { applicationSlug, pageSlug } = match?.params;
|
2022-07-11 04:06:29 +00:00
|
|
|
if (params.customSlug) {
|
|
|
|
|
updatedPath = updatedPath.replace(
|
|
|
|
|
`${applicationSlug}/${pageSlug}`,
|
|
|
|
|
`${params.customSlug}-`,
|
|
|
|
|
);
|
|
|
|
|
return updatedPath;
|
|
|
|
|
}
|
2022-03-25 10:43:26 +00:00
|
|
|
if (params.applicationSlug)
|
|
|
|
|
updatedPath = updatedPath.replace(
|
|
|
|
|
applicationSlug,
|
|
|
|
|
params.applicationSlug,
|
|
|
|
|
);
|
|
|
|
|
if (params.pageSlug)
|
|
|
|
|
updatedPath = updatedPath.replace(pageSlug, `${params.pageSlug}-`);
|
2022-07-11 04:06:29 +00:00
|
|
|
return updatedPath;
|
|
|
|
|
}
|
|
|
|
|
const matchCustomPath = matchPath<{ customSlug: string }>(path, {
|
|
|
|
|
path: [BUILDER_CUSTOM_PATH, VIEWER_CUSTOM_PATH],
|
|
|
|
|
});
|
|
|
|
|
if (matchCustomPath?.params) {
|
|
|
|
|
const { customSlug } = matchCustomPath.params;
|
|
|
|
|
if (params.customSlug) {
|
|
|
|
|
updatedPath = updatedPath.replace(
|
|
|
|
|
`${customSlug}`,
|
|
|
|
|
`${params.customSlug}-`,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
updatedPath = updatedPath.replace(
|
|
|
|
|
`${customSlug}`,
|
|
|
|
|
`${params.applicationSlug}/${params.pageSlug}-`,
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-03-25 10:43:26 +00:00
|
|
|
}
|
|
|
|
|
return updatedPath;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const updateSlugNamesInURL = (params: Record<string, string>) => {
|
|
|
|
|
const { pathname, search } = window.location;
|
2022-04-04 11:11:52 +00:00
|
|
|
// Do not update old URLs
|
|
|
|
|
if (isURLDeprecated(pathname)) return;
|
2022-03-25 10:43:26 +00:00
|
|
|
const newURL = getUpdatedRoute(pathname, params);
|
|
|
|
|
history.replace(newURL + search);
|
2022-03-21 07:57:25 +00:00
|
|
|
};
|
2022-07-11 04:06:29 +00:00
|
|
|
|
2022-08-03 06:02:23 +00:00
|
|
|
/**
|
|
|
|
|
* Function to get valid supported mimeType for different browsers
|
|
|
|
|
* @param media "video" | "audio"
|
|
|
|
|
* @returns mimeType string
|
|
|
|
|
*/
|
|
|
|
|
export const getSupportedMimeTypes = (media: "video" | "audio") => {
|
|
|
|
|
const videoTypes = ["webm", "ogg", "mp4", "x-matroska"];
|
|
|
|
|
const audioTypes = ["webm", "ogg", "mp3", "x-matroska"];
|
|
|
|
|
const codecs = [
|
|
|
|
|
"should-not-be-supported",
|
|
|
|
|
"vp9",
|
|
|
|
|
"vp9.0",
|
|
|
|
|
"vp8",
|
|
|
|
|
"vp8.0",
|
|
|
|
|
"avc1",
|
|
|
|
|
"av1",
|
|
|
|
|
"h265",
|
|
|
|
|
"h.265",
|
|
|
|
|
"h264",
|
|
|
|
|
"h.264",
|
|
|
|
|
"opus",
|
|
|
|
|
"pcm",
|
|
|
|
|
"aac",
|
|
|
|
|
"mpeg",
|
|
|
|
|
"mp4a",
|
|
|
|
|
];
|
|
|
|
|
const supported: Array<string> = [];
|
|
|
|
|
const isSupported = MediaRecorder.isTypeSupported;
|
|
|
|
|
const types = media === "video" ? videoTypes : audioTypes;
|
|
|
|
|
|
|
|
|
|
types.forEach((type: string) => {
|
|
|
|
|
const mimeType = `${media}/${type}`;
|
|
|
|
|
// without codecs
|
|
|
|
|
isSupported(mimeType) && supported.push(mimeType);
|
|
|
|
|
|
|
|
|
|
// with codecs
|
|
|
|
|
codecs.forEach((codec) =>
|
|
|
|
|
[
|
|
|
|
|
`${mimeType};codecs=${codec}`,
|
|
|
|
|
`${mimeType};codecs=${codec.toUpperCase()}`,
|
|
|
|
|
].forEach(
|
|
|
|
|
(variation) => isSupported(variation) && supported.push(variation),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
return supported[0];
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-11 04:06:29 +00:00
|
|
|
export function AutoBind(target: any, _: string, descriptor: any) {
|
|
|
|
|
if (typeof descriptor.value === "function")
|
|
|
|
|
descriptor.value = descriptor.value.bind(target);
|
|
|
|
|
return descriptor;
|
|
|
|
|
}
|
2022-11-20 06:12:32 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add item to an array which could be undefined
|
|
|
|
|
* @param arr1 Base Array (could be undefined)
|
|
|
|
|
* @param item Item to add to array
|
|
|
|
|
* @param makeUnique Should make sure array has unique entries
|
|
|
|
|
* @returns array which includes items from arr1 and item
|
|
|
|
|
*/
|
|
|
|
|
export function pushToArray(
|
|
|
|
|
item: unknown,
|
|
|
|
|
arr1?: unknown[],
|
|
|
|
|
makeUnique = false,
|
|
|
|
|
) {
|
|
|
|
|
if (Array.isArray(arr1)) arr1.push(item);
|
|
|
|
|
else return [item];
|
|
|
|
|
|
|
|
|
|
if (makeUnique) return uniq(arr1);
|
|
|
|
|
return arr1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add items to array which could be undefined
|
|
|
|
|
* @param arr1 Base Array (could be undefined)
|
|
|
|
|
* @param items Items to add to arr1
|
|
|
|
|
* @param makeUnique Should make sure array has unique entries
|
|
|
|
|
* @returns array which contains items from arr1 and items
|
|
|
|
|
*/
|
|
|
|
|
export function concatWithArray(
|
|
|
|
|
items: unknown[],
|
|
|
|
|
arr1?: unknown[],
|
|
|
|
|
makeUnique = false,
|
|
|
|
|
) {
|
|
|
|
|
let finalArr: unknown[] = [];
|
|
|
|
|
if (Array.isArray(arr1)) finalArr = arr1.concat(items);
|
|
|
|
|
else finalArr = finalArr.concat(items);
|
|
|
|
|
|
|
|
|
|
if (makeUnique) return uniq(finalArr);
|
|
|
|
|
return finalArr;
|
|
|
|
|
}
|