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
|
|
|
import type { AppState } from "@appsmith/reducers";
|
2023-03-04 07:25:54 +00:00
|
|
|
import { getColorWithOpacity } from "constants/DefaultTheme";
|
|
|
|
|
import { WIDGET_PADDING } from "constants/WidgetConstants";
|
2023-09-11 15:55:11 +00:00
|
|
|
import type { CSSProperties, DragEventHandler, ReactNode } from "react";
|
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
|
|
|
import React, { useMemo, useRef } from "react";
|
2019-09-22 20:25:05 +00:00
|
|
|
import styled from "styled-components";
|
2020-01-06 11:02:22 +00:00
|
|
|
import { useSelector } from "react-redux";
|
2022-09-14 06:55:08 +00:00
|
|
|
import {
|
|
|
|
|
isCurrentWidgetFocused,
|
|
|
|
|
isWidgetSelected,
|
|
|
|
|
} from "selectors/widgetSelectors";
|
2023-01-28 02:17:06 +00:00
|
|
|
import { SelectionRequestType } from "sagas/WidgetSelectUtils";
|
2023-03-04 07:25:54 +00:00
|
|
|
import { useWidgetSelection } from "utils/hooks/useWidgetSelection";
|
2023-10-19 20:27:40 +00:00
|
|
|
import type { SetDraggingStateActionPayload } from "utils/hooks/dragResizeHooks";
|
2023-03-04 07:25:54 +00:00
|
|
|
import {
|
|
|
|
|
useShowTableFilterPane,
|
|
|
|
|
useWidgetDragResize,
|
|
|
|
|
} from "utils/hooks/dragResizeHooks";
|
2023-10-06 10:07:43 +00:00
|
|
|
import { getShouldAllowDrag } from "selectors/widgetDragSelectors";
|
2023-11-03 17:13:36 +00:00
|
|
|
import { combinedPreviewModeSelector } from "selectors/editorSelectors";
|
2023-12-27 10:35:41 +00:00
|
|
|
import { getAnvilSpaceDistributionStatus } from "layoutSystems/anvil/integrations/selectors";
|
2019-04-02 16:12:08 +00:00
|
|
|
|
2024-03-13 06:23:49 +00:00
|
|
|
const DraggableWrapper = styled.div<{ draggable: boolean }>`
|
2019-10-02 19:42:25 +00:00
|
|
|
display: block;
|
2020-03-06 09:33:20 +00:00
|
|
|
flex-direction: column;
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
2020-03-27 09:02:11 +00:00
|
|
|
user-select: none;
|
2024-03-13 06:23:49 +00:00
|
|
|
cursor: ${(props) => (props.draggable ? "grab" : "unset")};
|
2019-09-30 03:25:14 +00:00
|
|
|
`;
|
|
|
|
|
|
2023-10-19 20:27:40 +00:00
|
|
|
export interface DraggableComponentProps {
|
2023-09-11 15:55:11 +00:00
|
|
|
widgetId: string;
|
|
|
|
|
parentId?: string;
|
|
|
|
|
isFlexChild?: boolean;
|
|
|
|
|
resizeDisabled?: boolean;
|
|
|
|
|
type: string;
|
|
|
|
|
children: ReactNode;
|
2023-10-19 20:27:40 +00:00
|
|
|
generateDragState: (
|
2024-03-13 06:23:49 +00:00
|
|
|
e: React.DragEvent,
|
2023-10-19 20:27:40 +00:00
|
|
|
draggableRef: HTMLElement,
|
|
|
|
|
) => SetDraggingStateActionPayload;
|
|
|
|
|
dragDisabled: boolean;
|
2023-10-11 07:35:24 +00:00
|
|
|
}
|
2023-02-03 05:47:40 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
// Widget Boundaries which is shown to indicate the boundaries of the widget
|
2020-01-08 05:19:01 +00:00
|
|
|
const WidgetBoundaries = styled.div`
|
2020-01-16 11:46:21 +00:00
|
|
|
z-index: 0;
|
2020-03-06 09:33:20 +00:00
|
|
|
width: calc(100% + ${WIDGET_PADDING - 2}px);
|
|
|
|
|
height: calc(100% + ${WIDGET_PADDING - 2}px);
|
|
|
|
|
position: absolute;
|
2020-01-08 05:19:01 +00:00
|
|
|
border: 1px dashed
|
2020-12-24 04:32:25 +00:00
|
|
|
${(props) => getColorWithOpacity(props.theme.colors.textAnchor, 0.5)};
|
2020-01-10 12:15:54 +00:00
|
|
|
pointer-events: none;
|
2023-02-03 05:47:40 +00:00
|
|
|
top: 0;
|
|
|
|
|
left: 0;
|
2020-01-08 05:19:01 +00:00
|
|
|
`;
|
|
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
function DraggableComponent(props: DraggableComponentProps) {
|
2020-03-27 09:02:11 +00:00
|
|
|
// Dispatch hook handy to set a widget as focused/selected
|
2021-05-13 08:35:39 +00:00
|
|
|
const { focusWidget, selectWidget } = useWidgetSelection();
|
2023-10-06 10:07:43 +00:00
|
|
|
|
|
|
|
|
const shouldAllowDrag = useSelector(getShouldAllowDrag);
|
2020-03-27 09:02:11 +00:00
|
|
|
// Dispatch hook handy to set any `DraggableComponent` as dragging/ not dragging
|
|
|
|
|
// The value is boolean
|
2023-03-04 07:25:54 +00:00
|
|
|
const { setDraggingState } = useWidgetDragResize();
|
2021-08-12 05:45:38 +00:00
|
|
|
const showTableFilterPane = useShowTableFilterPane();
|
2022-09-14 06:55:08 +00:00
|
|
|
|
|
|
|
|
const isSelected = useSelector(isWidgetSelected(props.widgetId));
|
2020-03-27 09:02:11 +00:00
|
|
|
// This state tels us which widget is focused
|
|
|
|
|
// The value is the widgetId of the focused widget.
|
2022-09-14 06:55:08 +00:00
|
|
|
const isFocused = useSelector(isCurrentWidgetFocused(props.widgetId));
|
2020-01-20 09:00:37 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
// This state tells us whether a `ResizableComponent` is resizing
|
2020-01-20 09:00:37 +00:00
|
|
|
const isResizing = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.widgetDragResize.isResizing,
|
|
|
|
|
);
|
2020-03-27 09:02:11 +00:00
|
|
|
|
2023-12-27 10:35:41 +00:00
|
|
|
// This state tells us whether space redistribution is in process
|
|
|
|
|
const isDistributingSpace = useSelector(getAnvilSpaceDistributionStatus);
|
|
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
// This state tells us whether a `DraggableComponent` is dragging
|
2020-01-20 09:00:37 +00:00
|
|
|
const isDragging = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.widgetDragResize.isDragging,
|
|
|
|
|
);
|
2020-01-06 11:02:22 +00:00
|
|
|
|
2023-02-24 13:21:56 +00:00
|
|
|
const isDraggingSibling = useSelector(
|
|
|
|
|
(state) =>
|
|
|
|
|
state.ui.widgetDragResize?.dragDetails?.draggedOn === props.parentId,
|
|
|
|
|
);
|
|
|
|
|
|
2023-11-03 17:13:36 +00:00
|
|
|
const isPreviewMode = useSelector(combinedPreviewModeSelector);
|
2023-10-19 15:14:10 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
// True when any widget is dragging or resizing, including this one
|
2020-03-04 08:10:40 +00:00
|
|
|
const isResizingOrDragging = !!isResizing || !!isDragging;
|
2022-09-14 06:55:08 +00:00
|
|
|
const isCurrentWidgetDragging = isDragging && isSelected;
|
|
|
|
|
const isCurrentWidgetResizing = isResizing && isSelected;
|
2023-03-04 07:25:54 +00:00
|
|
|
const showBoundary =
|
|
|
|
|
!props.isFlexChild && (isCurrentWidgetDragging || isDraggingSibling);
|
2023-02-03 05:47:40 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
// When mouse is over this draggable
|
2024-03-13 06:23:49 +00:00
|
|
|
const handleMouseOver = (e: React.MouseEvent) => {
|
2020-03-27 09:02:11 +00:00
|
|
|
focusWidget &&
|
|
|
|
|
!isResizingOrDragging &&
|
2022-09-14 06:55:08 +00:00
|
|
|
!isFocused &&
|
2023-12-27 10:35:41 +00:00
|
|
|
!isDistributingSpace &&
|
2021-08-09 05:35:01 +00:00
|
|
|
!props.resizeDisabled &&
|
2023-10-19 15:14:10 +00:00
|
|
|
!isPreviewMode &&
|
2024-03-13 06:23:49 +00:00
|
|
|
focusWidget(props.widgetId, e.metaKey);
|
2020-03-27 09:02:11 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
|
};
|
2023-09-11 15:55:11 +00:00
|
|
|
|
2024-01-12 05:23:47 +00:00
|
|
|
const handleMouseLeave = () => {
|
|
|
|
|
// on leaving a widget, we reset the focused widget
|
|
|
|
|
focusWidget && focusWidget();
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
// Display this draggable based on the current drag state
|
2021-08-12 05:45:38 +00:00
|
|
|
const dragWrapperStyle: CSSProperties = {
|
2023-03-04 07:25:54 +00:00
|
|
|
display: !props.isFlexChild && isCurrentWidgetDragging ? "none" : "block",
|
2020-03-27 09:02:11 +00:00
|
|
|
};
|
2021-08-12 05:45:38 +00:00
|
|
|
const dragBoundariesStyle: React.CSSProperties = useMemo(() => {
|
|
|
|
|
return {
|
|
|
|
|
opacity: !isResizingOrDragging || isCurrentWidgetResizing ? 0 : 1,
|
|
|
|
|
};
|
|
|
|
|
}, [isResizingOrDragging, isCurrentWidgetResizing]);
|
|
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
const classNameForTesting = `t--draggable-${props.type
|
2020-02-27 03:56:30 +00:00
|
|
|
.split("_")
|
|
|
|
|
.join("")
|
|
|
|
|
.toLowerCase()}`;
|
2020-03-06 09:33:20 +00:00
|
|
|
|
2023-10-19 20:27:40 +00:00
|
|
|
const allowDrag = !props.dragDisabled && shouldAllowDrag;
|
2020-03-27 09:02:11 +00:00
|
|
|
const className = `${classNameForTesting}`;
|
2021-08-12 05:45:38 +00:00
|
|
|
const draggableRef = useRef<HTMLDivElement>(null);
|
2023-09-11 15:55:11 +00:00
|
|
|
const onDragStart: DragEventHandler = (e) => {
|
2021-08-12 05:45:38 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
2021-09-22 07:18:46 +00:00
|
|
|
// allowDrag check is added as react jest test simulation is not respecting default behaviour
|
|
|
|
|
// of draggable=false and triggering onDragStart. allowDrag condition check is purely for the test cases.
|
|
|
|
|
if (allowDrag && draggableRef.current && !(e.metaKey || e.ctrlKey)) {
|
2022-09-14 06:55:08 +00:00
|
|
|
if (!isFocused) return;
|
2021-09-22 07:18:46 +00:00
|
|
|
|
2022-09-14 06:55:08 +00:00
|
|
|
if (!isSelected) {
|
2023-01-28 02:17:06 +00:00
|
|
|
selectWidget(SelectionRequestType.One, [props.widgetId]);
|
2021-08-12 05:45:38 +00:00
|
|
|
}
|
|
|
|
|
showTableFilterPane();
|
2023-10-19 20:27:40 +00:00
|
|
|
const draggingState = props.generateDragState(e, draggableRef.current);
|
|
|
|
|
setDraggingState(draggingState);
|
2021-08-12 05:45:38 +00:00
|
|
|
}
|
|
|
|
|
};
|
2020-01-24 10:44:15 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
return (
|
|
|
|
|
<DraggableWrapper
|
|
|
|
|
className={className}
|
2022-09-14 06:55:08 +00:00
|
|
|
data-testid={isSelected ? "t--selected" : ""}
|
2021-08-12 05:45:38 +00:00
|
|
|
draggable={allowDrag}
|
|
|
|
|
onDragStart={onDragStart}
|
2024-01-12 05:23:47 +00:00
|
|
|
onMouseLeave={handleMouseLeave}
|
2021-04-28 10:28:39 +00:00
|
|
|
onMouseOver={handleMouseOver}
|
2021-08-12 05:45:38 +00:00
|
|
|
ref={draggableRef}
|
|
|
|
|
style={dragWrapperStyle}
|
2020-03-27 09:02:11 +00:00
|
|
|
>
|
2023-09-11 15:55:11 +00:00
|
|
|
{props.children}
|
2023-02-24 13:21:56 +00:00
|
|
|
{showBoundary && (
|
|
|
|
|
<WidgetBoundaries
|
|
|
|
|
className={`widget-boundary-${props.widgetId}`}
|
|
|
|
|
style={dragBoundariesStyle}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2020-03-27 09:02:11 +00:00
|
|
|
</DraggableWrapper>
|
2019-09-17 10:09:00 +00:00
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2019-04-02 16:12:08 +00:00
|
|
|
|
2019-09-17 10:09:00 +00:00
|
|
|
export default DraggableComponent;
|