2022-12-09 14:43:47 +00:00
|
|
|
import tinycolor from "tinycolor2";
|
|
|
|
|
import { darkenColor } from "widgets/WidgetUtils";
|
|
|
|
|
import {
|
|
|
|
|
createMessage,
|
|
|
|
|
ADMIN_BRANDING_LOGO_SIZE_ERROR,
|
|
|
|
|
ADMIN_BRANDING_LOGO_FORMAT_ERROR,
|
|
|
|
|
ADMIN_BRANDING_FAVICON_SIZE_ERROR,
|
|
|
|
|
ADMIN_BRANDING_FAVICON_FORMAT_ERROR,
|
|
|
|
|
ADMIN_BRANDING_FAVICON_DIMENSION_ERROR,
|
|
|
|
|
} from "@appsmith/constants/messages";
|
2023-05-19 18:37:06 +00:00
|
|
|
import { toast } from "design-system";
|
2023-04-10 07:02:31 +00:00
|
|
|
import { ASSETS_CDN_URL } from "constants/ThirdPartyConstants";
|
|
|
|
|
import { getAssetUrl } from "@appsmith/utils/airgapHelpers";
|
2023-09-21 10:07:41 +00:00
|
|
|
import { LightModeTheme } from "@design-system/theming";
|
2022-12-09 14:43:47 +00:00
|
|
|
|
|
|
|
|
const FAVICON_MAX_WIDTH = 32;
|
|
|
|
|
const FAVICON_MAX_HEIGHT = 32;
|
2023-09-21 10:07:41 +00:00
|
|
|
const DEFAULT_BRANDING_PRIMARY_COLOR = "#E15615";
|
|
|
|
|
export const APPSMITH_BRAND_PRIMARY_COLOR =
|
|
|
|
|
getComputedStyle(document.documentElement).getPropertyValue(
|
|
|
|
|
"--ads-v2-color-bg-brand",
|
|
|
|
|
) || DEFAULT_BRANDING_PRIMARY_COLOR;
|
2023-05-19 18:37:06 +00:00
|
|
|
export const APPSMITH_BRAND_BG_COLOR = "#F1F5F9";
|
2023-04-10 07:02:31 +00:00
|
|
|
export const APPSMITH_BRAND_FAVICON_URL = getAssetUrl(
|
|
|
|
|
`${ASSETS_CDN_URL}/appsmith-favicon-orange.ico`,
|
|
|
|
|
);
|
|
|
|
|
export const APPSMITH_BRAND_LOGO_URL = getAssetUrl(
|
|
|
|
|
`${ASSETS_CDN_URL}/appsmith-logo-no-margin.png`,
|
|
|
|
|
);
|
2022-12-09 14:43:47 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* create brand colors from primary color
|
|
|
|
|
*
|
2023-05-19 18:37:06 +00:00
|
|
|
* @param brand
|
2022-12-09 14:43:47 +00:00
|
|
|
*/
|
|
|
|
|
export function createBrandColorsFromPrimaryColor(
|
|
|
|
|
brand: string = DEFAULT_BRANDING_PRIMARY_COLOR,
|
|
|
|
|
) {
|
|
|
|
|
const hsl = tinycolor(brand).toHsl();
|
|
|
|
|
const hue = hsl.h;
|
|
|
|
|
const saturation = hsl.s;
|
2023-09-21 10:07:41 +00:00
|
|
|
// initialize light theme
|
|
|
|
|
const lightTheme = new LightModeTheme(brand);
|
2022-12-09 14:43:47 +00:00
|
|
|
|
|
|
|
|
let textColor = "#000";
|
|
|
|
|
const isReadable = tinycolor.isReadable(brand, "#000", {
|
|
|
|
|
level: "AAA",
|
|
|
|
|
size: "small",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// if the brand color is not readable or the color is appsmith orange, use white as the text color
|
|
|
|
|
if (isReadable === false || brand === APPSMITH_BRAND_PRIMARY_COLOR) {
|
|
|
|
|
textColor = "#fff";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let bgColor = `#${tinycolor(`hsl ${hue} ${saturation} ${98}}`).toHex()}`;
|
|
|
|
|
|
|
|
|
|
// if the primary color is appsmith orange, use gray shade for the bg color
|
|
|
|
|
if (brand === APPSMITH_BRAND_PRIMARY_COLOR) {
|
2023-05-19 18:37:06 +00:00
|
|
|
bgColor = APPSMITH_BRAND_BG_COLOR;
|
2022-12-09 14:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const disabledColor = `#${tinycolor(
|
|
|
|
|
`hsl ${hue} ${saturation} ${92}}`,
|
|
|
|
|
).toHex()}`;
|
2023-05-19 18:37:06 +00:00
|
|
|
const hoverColor =
|
|
|
|
|
brand === APPSMITH_BRAND_PRIMARY_COLOR
|
|
|
|
|
? getComputedStyle(document.documentElement).getPropertyValue(
|
|
|
|
|
"--ads-v2-color-bg-brand-emphasis",
|
|
|
|
|
)
|
|
|
|
|
: darkenColor(brand);
|
2022-12-09 14:43:47 +00:00
|
|
|
|
2023-09-21 10:07:41 +00:00
|
|
|
// get active color from color algorithm
|
|
|
|
|
const activeColor =
|
|
|
|
|
brand === APPSMITH_BRAND_PRIMARY_COLOR
|
|
|
|
|
? getComputedStyle(document.documentElement).getPropertyValue(
|
|
|
|
|
"--ads-v2-color-bg-brand-emphasis-plus",
|
|
|
|
|
)
|
|
|
|
|
: lightTheme.bgAccentActive.toString({ format: "hex" });
|
2022-12-09 14:43:47 +00:00
|
|
|
return {
|
|
|
|
|
primary: brand,
|
|
|
|
|
background: bgColor,
|
|
|
|
|
hover: hoverColor,
|
2023-09-21 10:07:41 +00:00
|
|
|
active: activeColor,
|
2022-12-09 14:43:47 +00:00
|
|
|
font: textColor,
|
|
|
|
|
disabled: disabledColor,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* validates the uploaded logo file
|
|
|
|
|
*
|
|
|
|
|
* checks:
|
|
|
|
|
* 1. file size max 2MB
|
2023-01-26 08:43:24 +00:00
|
|
|
* 2. file type - jpg, or png
|
2022-12-09 14:43:47 +00:00
|
|
|
*
|
|
|
|
|
* @param e
|
|
|
|
|
* @param callback
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const logoImageValidator = (
|
|
|
|
|
e: React.ChangeEvent<HTMLInputElement>,
|
|
|
|
|
callback?: (e: React.ChangeEvent<HTMLInputElement>) => void,
|
|
|
|
|
) => {
|
|
|
|
|
const file = e.target.files?.[0];
|
|
|
|
|
|
|
|
|
|
// case 1: no file selected
|
|
|
|
|
if (!file) return false;
|
|
|
|
|
|
|
|
|
|
// case 2: file size > 2mb
|
|
|
|
|
if (file.size > 2 * 1024 * 1024) {
|
2023-05-19 18:37:06 +00:00
|
|
|
toast.show(createMessage(ADMIN_BRANDING_LOGO_SIZE_ERROR), {
|
|
|
|
|
kind: "error",
|
2022-12-09 14:43:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 05:00:08 +00:00
|
|
|
// case 3: check image type
|
2023-01-26 08:43:24 +00:00
|
|
|
const validTypes = ["image/jpeg", "image/png"];
|
2022-12-09 14:43:47 +00:00
|
|
|
|
|
|
|
|
if (!validTypes.includes(file.type)) {
|
2023-05-19 18:37:06 +00:00
|
|
|
toast.show(createMessage(ADMIN_BRANDING_LOGO_FORMAT_ERROR), {
|
|
|
|
|
kind: "error",
|
2022-12-09 14:43:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 05:00:08 +00:00
|
|
|
// case 4: check image dimension
|
2022-12-09 14:43:47 +00:00
|
|
|
const image = new Image();
|
|
|
|
|
image.src = window.URL.createObjectURL(file);
|
|
|
|
|
|
|
|
|
|
callback && callback(e);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* validates the uploaded favicon file
|
|
|
|
|
*
|
|
|
|
|
* checks:
|
|
|
|
|
* 1. file size max 2MB
|
|
|
|
|
* 2. file type - jpg, ico or png
|
|
|
|
|
* 3. file dimensions - height, width = [32, 32]
|
|
|
|
|
*
|
|
|
|
|
* @param e
|
|
|
|
|
* @param callback
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const faivconImageValidator = (
|
|
|
|
|
e: React.ChangeEvent<HTMLInputElement>,
|
|
|
|
|
callback?: (e: React.ChangeEvent<HTMLInputElement>) => void,
|
|
|
|
|
) => {
|
|
|
|
|
const file = e.target.files?.[0];
|
|
|
|
|
|
|
|
|
|
// case 1: no file selected
|
|
|
|
|
if (!file) return false;
|
|
|
|
|
|
2023-08-24 05:00:08 +00:00
|
|
|
// case 2: file size > 1mb
|
|
|
|
|
if (file.size > 1 * 1024 * 1024) {
|
2023-05-19 18:37:06 +00:00
|
|
|
toast.show(createMessage(ADMIN_BRANDING_FAVICON_SIZE_ERROR), {
|
|
|
|
|
kind: "error",
|
2022-12-09 14:43:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 05:00:08 +00:00
|
|
|
// case 3: check image type
|
2022-12-09 14:43:47 +00:00
|
|
|
const validTypes = [
|
|
|
|
|
"image/jpeg",
|
|
|
|
|
"image/png",
|
|
|
|
|
"image/vnd.microsoft.icon",
|
|
|
|
|
"image/x-icon",
|
|
|
|
|
"image/x-image",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (!validTypes.includes(file.type)) {
|
2023-05-19 18:37:06 +00:00
|
|
|
toast.show(createMessage(ADMIN_BRANDING_FAVICON_FORMAT_ERROR), {
|
|
|
|
|
kind: "error",
|
2022-12-09 14:43:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 05:00:08 +00:00
|
|
|
// case 4: check image dimension
|
2022-12-09 14:43:47 +00:00
|
|
|
const image = new Image();
|
|
|
|
|
image.src = window.URL.createObjectURL(file);
|
|
|
|
|
|
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
|
|
|
image.onload = function () {
|
2022-12-09 14:43:47 +00:00
|
|
|
const height = image.naturalHeight;
|
|
|
|
|
const width = image.naturalWidth;
|
|
|
|
|
|
|
|
|
|
window.URL.revokeObjectURL(image.src);
|
|
|
|
|
|
|
|
|
|
if (height > FAVICON_MAX_HEIGHT || width > FAVICON_MAX_WIDTH) {
|
2023-05-19 18:37:06 +00:00
|
|
|
toast.show(createMessage(ADMIN_BRANDING_FAVICON_DIMENSION_ERROR), {
|
|
|
|
|
kind: "error",
|
2022-12-09 14:43:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback && callback(e);
|
|
|
|
|
};
|
|
|
|
|
};
|