Skip to content

Commit

Permalink
redirect if file deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
crcn committed Dec 28, 2023
1 parent bf269ae commit e71057b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 19 deletions.
1 change: 1 addition & 0 deletions libs/designer/src/domains/api/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ const createEventHandler = (actions: Actions) => {
case "ui/canvasMouseUp": {
return handleCanvasMouseUp(newState, prevState);
}
case "ui/FileNavigatorContextMenuOpened":
case "ui/FileNavigatorItemClicked": {
return handleFileNavigatorItemClicked(event.payload);
}
Expand Down
5 changes: 5 additions & 0 deletions libs/designer/src/domains/ui/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ export type FileNavigatorItemClicked = BaseEvent<
"ui/FileNavigatorItemClicked",
FSItem
>;
export type FileNavigatorContextMenuOpened = BaseEvent<
"ui/FileNavigatorContextMenuOpened",
FSItem
>;

export type InsertElementReleased = BaseEvent<"ui/insertElementReleased", Box>;

Expand All @@ -259,6 +263,7 @@ export type UIEvent =
| AddLayerMenuItemClicked
| BoundsChanged
| CanvasResized
| FileNavigatorContextMenuOpened
| ToolsLayerDrop
| PromptClosed
| FileNavigatorItemClicked
Expand Down
17 changes: 17 additions & 0 deletions libs/designer/src/domains/ui/reducers/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import {
getTargetExprId,
highlightNode,
InsertMode,
newConvertToSlotPrompt,
redirect,
resetCurrentDocument,
} from "@paperclip-ui/designer/src/state";
import { centerTransformZoom } from "@paperclip-ui/designer/src/state/geom";
import { routes } from "@paperclip-ui/designer/src/state/routes";
import { virtHTML } from "@paperclip-ui/proto-ext/lib/virt/html-utils";
import { FileChangedKind } from "@paperclip-ui/proto/lib/generated/service/designer";
import produce from "immer";
import { clamp, mapValues } from "lodash";
import {
Expand Down Expand Up @@ -60,6 +64,19 @@ export const canvasReducer = (state: DesignerState, event: DesignerEvent) => {
});
}

case "designer-engine/serverEvent": {
// If current file is deleted, then direct to blank screen
if (event.payload.fileChanged?.kind === FileChangedKind.DELETED) {
if (
event.payload.fileChanged.path.includes(getCurrentFilePath(state))
) {
return redirect(state, routes.editor());
}
}

return state;
}

case "ui/canvasMouseDown": {
state = produce(state, (newState) => {
newState.canvas.mouseDown = true;
Expand Down
1 change: 1 addition & 0 deletions libs/designer/src/domains/ui/reducers/left-sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const leftSidebarReducer = (
}
});
}
case "ui/FileNavigatorContextMenuOpened":
case "ui/FileNavigatorItemClicked": {
if (event.payload.kind === FSItemKind.File) {
state = redirect(
Expand Down
14 changes: 5 additions & 9 deletions libs/designer/src/state/routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type EditorRedirectInfo = {
filePath: string;
filePath?: string;
nodeId?: string;
declName?: string;
variantIds?: string[];
Expand All @@ -12,15 +12,11 @@ export namespace routes {
nodeId,
declName,
variantIds = [],
}: EditorRedirectInfo) => {
const query = {
file: encodeURIComponent(filePath),
nodeId,
declName,
};

}: EditorRedirectInfo = {}) => {
const params = new URLSearchParams();
params.set("file", filePath);
if (filePath) {
params.set("file", filePath);
}
if (nodeId) {
params.set("nodeId", nodeId);
}
Expand Down
25 changes: 16 additions & 9 deletions libs/designer/src/ui/logic/ContextMenu/controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ import {
MenuItemOption,
} from "../../../modules/shortcuts/base";
import { Portal } from "../Portal";
import { getHistoryStr } from "../../../state";
import { getHistoryStr } from "../../../state";
import { prettyKeyCombo } from "../../../domains/ui/state";
import { ContextMenuDivider, BaseContextMenuProps } from "./context-menu.pc";

export type ContextMenuProps = {
children: React.ReactElement;
menu: () => MenuItem<ShortcutCommand>[];
onOpen?: () => void;
};

export const ContextMenu =
(Base: React.FC<BaseContextMenuProps>) =>
({ children, menu }: ContextMenuProps) => {
({ children, menu, onOpen }: ContextMenuProps) => {
const otherRef = useRef<HTMLElement>();
const ref = otherRef;
const history = useSelector(getHistoryStr);
Expand All @@ -38,6 +39,12 @@ export const ContextMenu =
});
};

useEffect(() => {
if (anchorStyle) {
onOpen?.();
}
}, [anchorStyle]);

const onTargetKeyDown = (event: React.KeyboardEvent<any>) => {
if (children.props.onKeyDown) {
children.props.onKeyDown(event);
Expand Down Expand Up @@ -86,7 +93,7 @@ export const ContextMenu =
})}
<Portal>
{anchorStyle && (
<Base container={{root:{style: anchorStyle}}}>
<Base container={{ root: { style: anchorStyle } }}>
{menu().map((item) => {
if (item.kind === MenuItemKind.Divider) {
return <ContextMenuDivider />;
Expand Down Expand Up @@ -117,12 +124,12 @@ const ContextMenuOption = ({
};
return (
<styles.ContextMenuItem
container={{
root: {
className: cx({ disabled: enabled === false }),
onClick: onSelect
}
}}
container={{
root: {
className: cx({ disabled: enabled === false }),
onClick: onSelect,
},
}}
keyCommand={shortcut ? prettyKeyCombo(shortcut) : null}
>
{label}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ export const FSNavigatorItem = (Base: React.FC<BaseFSItemProps>) =>
setOpen(!open);
}, [open, item.path]);

const onContextMenuOpen = useCallback(() => {
dispatch({ type: "ui/FileNavigatorContextMenuOpened", payload: item });
setOpen(true);
}, [item.path]);

useEffect(() => {
if (selected) {
headerRef.current?.scrollIntoView({
Expand All @@ -313,7 +318,7 @@ export const FSNavigatorItem = (Base: React.FC<BaseFSItemProps>) =>
}

return (
<ContextMenu menu={() => shortcuts}>
<ContextMenu menu={() => shortcuts} onOpen={onContextMenuOpen}>
<div>
<Base
layerHeader={{
Expand Down

0 comments on commit e71057b

Please sign in to comment.