Skip to content
Open
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
11 changes: 8 additions & 3 deletions packages/core/components/Puck/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type {
Metadata,
AsFieldProps,
DefaultComponentProps,
ComponentData,
HotkeyState,
} from "../../types";

import { SidebarSection } from "../SidebarSection";
Expand Down Expand Up @@ -124,6 +124,7 @@ type PuckProps<
};
initialHistory?: InitialHistory;
metadata?: Metadata;
hotkeys?: Partial<HotkeyState>
};

const propsContext = createContext<Partial<PuckProps>>({});
Expand Down Expand Up @@ -159,6 +160,7 @@ function PuckProvider<
metadata,
onAction,
fieldTransforms,
hotkeys = { enabled: true },
} = usePropsContext();

const iframe: IframeConfig = useMemo(
Expand Down Expand Up @@ -330,6 +332,7 @@ function PuckProvider<
onAction,
metadata,
fieldTransforms: loadedFieldTransforms,
hotkeys,
};
},
[
Expand All @@ -342,6 +345,7 @@ function PuckProvider<
onAction,
metadata,
loadedFieldTransforms,
hotkeys.enabled
]
);

Expand Down Expand Up @@ -431,6 +435,7 @@ function PuckLayout<
const rightSideBarVisible = useAppStore(
(s) => s.state.ui.rightSideBarVisible
);
const enabledHotkeys = useAppStore((s) => s.hotkeys.enabled);

const {
width: leftWidth,
Expand Down Expand Up @@ -498,11 +503,11 @@ function PuckLayout<
if (ready && iframe.enabled) {
const frameDoc = getFrame();

if (frameDoc) {
if (frameDoc && enabledHotkeys) {
return monitorHotkeys(frameDoc);
}
}
}, [ready, iframe.enabled]);
}, [ready, iframe.enabled, enabledHotkeys]);

usePreviewModeHotkeys();

Expand Down
8 changes: 7 additions & 1 deletion packages/core/lib/use-hotkey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect } from "react";
import { create } from "zustand";
import { subscribeWithSelector } from "zustand/middleware";
import { useAppStore } from "../store";

const keys = [
"ctrl",
Expand Down Expand Up @@ -168,7 +169,12 @@ export const monitorHotkeys = (doc: Document) => {
};

export const useMonitorHotkeys = () => {
useEffect(() => monitorHotkeys(document), []);
const enabledHotkeys = useAppStore((s) => s.hotkeys.enabled);

useEffect(() => {
if (!enabledHotkeys) return;
return monitorHotkeys(document);
}, [enabledHotkeys]);
};

export const useHotkey = (combo: KeyMapStrict, cb: Function) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ComponentData,
RootDataWithProps,
ResolveDataTrigger,
HotkeyState,
} from "../types";
import { createReducer, PuckAction } from "../reducer";
import { getItem } from "../lib/data/get-item";
Expand Down Expand Up @@ -87,6 +88,7 @@ export type AppStore<
nodes: NodesSlice;
permissions: PermissionsSlice;
fieldTransforms: FieldTransforms;
hotkeys: Partial<HotkeyState>;
};

export type AppStoreApi = StoreApi<AppStore>;
Expand All @@ -113,6 +115,7 @@ export const createAppStore = (initialAppStore?: Partial<AppStore>) =>
iframe: {},
metadata: {},
fieldTransforms: {},
hotkeys: { enabled: true },
...initialAppStore,
fields: createFieldsSlice(set, get),
history: createHistorySlice(set, get),
Expand Down
4 changes: 4 additions & 0 deletions packages/core/types/AppState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export type ItemWithId = {

export type ArrayState = { items: ItemWithId[]; openId: string };

export type HotkeyState = {
enabled: boolean;
}

export type UiState = {
leftSideBarVisible: boolean;
rightSideBarVisible: boolean;
Expand Down