Skip to content

Commit

Permalink
Merge pull request #77 from walkframe/feature/plugin
Browse files Browse the repository at this point in the history
feat: PluginBase
  • Loading branch information
righ authored Nov 17, 2024
2 parents ec596d9 + fd3a3b8 commit 9f6370b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/components/GridSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ export function GridSheet({
}, 1000);
return () => window.clearInterval(intervalId);
}, []);
React.useEffect(() => {
if (options.sheetHeight) {
setSheetHeight(options.sheetHeight);
}
}, [options.sheetHeight]);
React.useEffect(() => {
if (options.sheetWidth) {
setSheetWidth(options.sheetWidth);
}
}, [options.sheetWidth]);

const { onChange, onSelect, mode = 'light' } = options;
return (
<Context.Provider value={{ store, dispatch }}>
Expand Down
51 changes: 51 additions & 0 deletions src/components/PluginBase.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from 'react';

import { StoreType } from '../types';
import { Dispatcher } from '../store';

export type PluginContextType = {
provided: boolean;
store?: StoreType;
dispatch?: Dispatcher;
setStore: (store: StoreType) => void;
setDispatch: (dispatch: Dispatcher) => void;
};

export const PluginContext = React.createContext({} as PluginContextType);

export function useInitialPluginContext(): PluginContextType {
const [store, setStore] = React.useState<StoreType | undefined>(undefined);
const [dispatch, setDispatch] = React.useState<Dispatcher>();
return {
provided: true,
store,
dispatch,
setStore,
setDispatch,
};
}

export function usePluginContext(): [boolean, PluginContextType] {
const ctx = React.useContext(PluginContext);
if (ctx?.provided == null) {
return [false, ctx];
}
return [true, ctx];
}

export function usePluginDispatch() {
const dispatch = React.useContext(PluginContext);
if (!dispatch) {
return undefined;
}
return dispatch;
}

type Props = {
children: React.ReactNode;
context: PluginContextType;
};

export function PluginBase({ children, context }: Props) {
return <PluginContext.Provider value={context}>{children}</PluginContext.Provider>;
}
6 changes: 5 additions & 1 deletion src/components/SheetProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export function useSheetDispatch() {
return dispatch;
}

export function SheetProvider({ children }: { children: React.ReactNode }) {
type Props = {
children: React.ReactNode;
};

export function SheetProvider({ children }: Props) {
const [mounted, setMounted] = React.useState(false);
const [version, setVersion] = React.useState(0);
const head = React.useRef(1);
Expand Down
10 changes: 10 additions & 0 deletions src/components/StoreInitializer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {

import { HEADER_HEIGHT, HEADER_WIDTH } from '../constants';
import { useSheetContext } from './SheetProvider';
import { usePluginContext } from './PluginBase';

export const StoreInitializer: React.FC<Props> = ({ options = {} }) => {
const {
Expand Down Expand Up @@ -79,5 +80,14 @@ export const StoreInitializer: React.FC<Props> = ({ options = {} }) => {
}
}, [onSave]);

const [pluginProvided, pluginContext] = usePluginContext();
React.useEffect(() => {
if (!pluginProvided) {
return;
}
pluginContext.setStore(store);
pluginContext.setDispatch(() => dispatch);
}, [store, dispatch]);

return <></>;
};

0 comments on commit 9f6370b

Please sign in to comment.