Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace window data #1018

Merged
merged 8 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions cypress/e2e/demo.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ context("WM", () => {
it("should maximize/restore window", () => {
cy.contains("with title").click();
cy.get("[data-testid='window-frame']").should("be.visible").as("window");
cy.get("@window").contains("scroll to bottom").should("be.enabled");
cy.document().matchImage({ screenshotConfig });
cy.get("@window").contains("with title").dblclick();
cy.document().matchImage({ screenshotConfig });
cy.get("button[name='zoom']").click();
cy.document().matchImage({ screenshotConfig });
cy.get("@window").contains("close all").click();
});

it("should prevent window from going offscreen", () => {
Expand Down
1 change: 1 addition & 0 deletions src/demo/DebugButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function DebugButtons({ currentId }: { currentId?: WindowId }): JSX.Eleme
{ title: "lazy loaded content", kind: DemoWindowKind.lazy },
{ title: "initial size", kind: DemoWindowKind.lazy, width: 700, height: 700 },
{ title: "minimal size (overflow fixed)", kind: DemoWindowKind.danger, width: 7000, height: 7000 },
{ title: `fixed id`, id: "fixed", parent: null },
],
[],
);
Expand Down
2 changes: 1 addition & 1 deletion src/store/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ThunkAction } from "./reducerWithThunk";

export type Action =
| { type: "CLEAR_WINDOWS" }
| { type: "OPEN_WINDOW"; windowData: WindowType<any, any> }
| { type: "OPEN_WINDOW"; windowData: WindowType }
| { type: "CLOSE_WINDOW"; id: WindowId }
| { type: "FOCUS_WINDOW"; id: WindowId };

Expand Down
4 changes: 2 additions & 2 deletions src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export function openWindow<Kind extends number | string = any, Meta = never>({
export function closeWindow(id: string = null): WMAction {
return async (dispatch, getState) => {
const state = getState();
dispatch({ type: "CLOSE_WINDOW", id });
await Promise.all(
getWindows(state)
.filter(({ parent }) => parent === id)
.filter((w) => w.parent === id && w.id !== id && w.id !== w.parent)
.map(({ id }) => dispatch(closeWindow(id))),
);
return await dispatch({ type: "CLOSE_WINDOW", id });
};
}
12 changes: 7 additions & 5 deletions src/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ export function withoutId<T extends { id: I }, I = string>(arr: T[], id: T["id"]
return arr.filter((el) => id !== el.id);
}

const windows: Reducer<WindowType<any, any>[], Action> = (windows = [], action) => {
const windows: Reducer<WindowType[], Action> = (windows = [], action) => {
switch (action.type) {
case "CLEAR_WINDOWS":
return [];
case "OPEN_WINDOW":
const existingWindow = windows.find((w) => w.id === action.windowData.id);
if (existingWindow) {
return windows.map((window) => (window === existingWindow ? action.windowData : window));
}
return [...windows, action.windowData];
case "CLOSE_WINDOW":
return withoutId(windows, action.id);
Expand All @@ -26,13 +30,11 @@ const order: Reducer<WindowId[], Action> = (state = [], action) => {
case "CLEAR_WINDOWS":
return [];
case "OPEN_WINDOW":
return uniq([action.windowData.id, ...state]);
return state.includes(action.windowData.id) ? state : uniq([action.windowData.id, ...state]);
case "CLOSE_WINDOW":
return without(state, action.id);
case "FOCUS_WINDOW":
if (action.id !== state[0]) {
return uniq([action.id, ...state]);
}
return action.id !== state[0] ? uniq([action.id, ...state]) : state;
default:
return state;
}
Expand Down