Skip to content

Commit

Permalink
feat: addeed live view for slot
Browse files Browse the repository at this point in the history
  • Loading branch information
adolfokrah committed Sep 25, 2024
1 parent 36edce5 commit d2e60f6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 deletions.
32 changes: 26 additions & 6 deletions src/components/exposed-components/slot/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import BlockItem from "@/components/pages/page-content/components/bock-item"
import EmptyPageDroppable from "@/components/pages/page-content/components/emptyPageDroppable"
import { PageBlock } from "@/lib/exposed-types"
import usePageContent from "@/lib/hooks/usePageContent"
import { usePageContentState } from "@/lib/states/usePageContentState"
import { useProjectConfigurationState } from "@/lib/states/useProjectConfigState"
import { cn } from "@/lib/utils"
import { cn, getProjectMode } from "@/lib/utils"

type SlotProps = {
pageBlocks: PageBlock[]
Expand All @@ -14,17 +15,36 @@ type SlotProps = {
}
export default function Slot({pageBlocks, direction, className, propName, pageBlockId}:SlotProps){
const { activePage } = usePageContent();
const { blocks } = useProjectConfigurationState();
const { blocks: builderBlocks } = useProjectConfigurationState();
const {blocks: liveBlocks, globalBlocks} = usePageContentState();
const isLiveMode = getProjectMode() === 'LIVE'


if (pageBlocks.length === 0 && activePage) {
return <EmptyPageDroppable activePage={activePage} propName={propName} pageBlockId={pageBlockId} className="h-full" />;
}


const blocks = isLiveMode ? liveBlocks : builderBlocks

const divClass = cn('relative flex flex-col',className, {
'flex-row': direction === 'horizontal',
})

if(isLiveMode){
return (
<div className={cn(divClass)}>
{ pageBlocks?.map((block) => {
const globalBlock = globalBlocks?.find((b) => b.id === block?.globalBlockId);
const Block = blocks.find((b) => b.Schema.id === (globalBlock?.blockId || block.blockId));
if (!Block) return null;
const inputs = { ...Block.Schema.defaultPropValues, ...block.inputs, ...globalBlock?.inputs };
return <Block key={block.id} {...inputs} />;
})}
</div>
)
}
return (
<div className={cn('relative flex flex-col',className, {
'flex-row': direction === 'horizontal',
})}>
<div className={cn(divClass)}>
{pageBlocks.map((pageBlock, index)=>{
const { blockId } = pageBlock;
const block = blocks.find((block) => block.Schema.id === blockId);
Expand Down
11 changes: 8 additions & 3 deletions src/components/pages/live-page/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function LivePage({
projectId: string;
};
}) {
const { setGlobalBlocks, setTheme, setAllowImageTransformation, setProjectId, setPages } = usePageContentState();
const { setGlobalBlocks, setTheme, setBlocks, blocks, setAllowImageTransformation, setProjectId, setPages } = usePageContentState();
const { setParams } = useParamState();
useEffect(() => {
setAllowImageTransformation(allowImageTransformation || false);
Expand All @@ -30,6 +30,7 @@ export default function LivePage({
setProjectId(projectConfiguration.projectId);
setParams(params);
setPages(pages);
setBlocks(projectConfiguration.blocks)
}, [
setAllowImageTransformation,
setGlobalBlocks,
Expand All @@ -41,13 +42,17 @@ export default function LivePage({
params,
pages,
setPages,
setBlocks
]);



if(blocks.length < 1) return null;

return (
<>
{pageBlocks?.map((block) => {
{ pageBlocks?.map((block) => {
const globalBlock = projectConfiguration?.globalBlocks?.find((b) => b.id === block?.globalBlockId);

const Block = projectConfiguration.blocks.find((b) => b.Schema.id === (globalBlock?.blockId || block.blockId));
if (!Block) return null;
const inputs = { ...Block.Schema.defaultPropValues, ...block.inputs, ...globalBlock?.inputs };
Expand Down
7 changes: 3 additions & 4 deletions src/lib/hooks/useCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Block, Message } from '../types';
import { useProjectConfigurationState } from '../states/useProjectConfigState';
import { v4 as uuidv4 } from 'uuid';
import useUndoAndRedo from './useUndoAndRedo';
import { getSelectedBlockPath, getValueByPath, updateIsSelectedByBlockId, updateValueByPath } from '../utils';
import { getSelectedBlock, getSelectedBlockPath, getValueByPath, updateIsSelectedByBlockId, updateValueByPath } from '../utils';
import useBlockHistory from './useBlockHistory';
import { useTabState } from '../states/useTabsState';
import { useListState } from '../states/useListState';
Expand Down Expand Up @@ -38,14 +38,14 @@ export default function useCanvas() {
const blocks = page.blocks?.[page.activeLanguageLocale] ?? [];
const newBlocks = updateIsSelectedByBlockId(lodash.cloneDeep(blocks), '') as PageBlock[]
const globalBlock = globalBlocks.find((block) => block.id === globalBlockId);
const foundBlock = newBlocks.find((block)=>block.id === pageBlockId)
const foundBlock = getSelectedBlock(newBlocks, pageBlockId)
const copiedBlock = localStorage.getItem('copiedBlock');
const inputs =
fromClipBoard && copiedBlock
? JSON.parse(copiedBlock)?.inputs :
globalBlock?.inputs || block.Schema.defaultPropValues;


if(propName && foundBlock){
const foundPropInput = getValueByPath(foundBlock?.inputs, propName.split('.')) || [];
foundPropInput.splice(position, 0, {
Expand All @@ -71,7 +71,6 @@ export default function useCanvas() {
});
}


// setPages(pages.map((p) => (p.active ? page : p)));
await addBlocksToPageHistory(page.activeLanguageLocale, newBlocks);
if (fromClipBoard) {
Expand Down
6 changes: 5 additions & 1 deletion src/lib/states/usePageContentState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { create } from 'zustand';
import { Page } from './usePagesState';
import { ProjectConfiguration } from '../types';
import { BlockList, ProjectConfiguration } from '../types';
import { TabsState } from './useTabsState';
import { ListStateType } from './useListState';

Expand All @@ -19,6 +19,8 @@ type State = {
setAllowImageTransformation: (allowImageTransformation: boolean) => void;
projectId: string;
setProjectId: (projectId: string) => void;
blocks: BlockList[]
setBlocks: (blocks: BlockList[]) => void
};

export const usePageContentState = create<State>((set) => ({
Expand All @@ -38,4 +40,6 @@ export const usePageContentState = create<State>((set) => ({
setAllowImageTransformation: (allowImageTransformation) => set({ allowImageTransformation }),
projectId: '',
setProjectId: (projectId) => set({ projectId }),
blocks: [],
setBlocks: (blocks) => set({ blocks }),
}));
11 changes: 6 additions & 5 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,12 +808,13 @@ export function getSelectedBlock(obj: any, id?: string): any | null {
const value = obj[key];

// If the current object has the 'isSelected' key and it's true, return this object
if ('isSelected' in obj && obj.isSelected === true) {
if(id && id == obj.id){
return obj;
}

if(id && id == obj.id){
return obj;
}
}else if (!id && 'isSelected' in obj && obj.isSelected === true) {

return obj;
}

// If the value is an object, recursively call the function
if (typeof value === 'object' && value !== null) {
Expand Down

0 comments on commit d2e60f6

Please sign in to comment.