فهرست منبع

fix(dashboard): Page block fixes (#3955)

Michael Bromley 2 ماه پیش
والد
کامیت
9c28effddd

+ 3 - 2
docs/docs/guides/extending-the-dashboard/customizing-pages/page-blocks.md

@@ -26,7 +26,7 @@ defineDashboardExtension({
             location: {
             location: {
                 // This is the pageId of the page where this block will be
                 // This is the pageId of the page where this block will be
                 pageId: 'product-detail',
                 pageId: 'product-detail',
-                // can be "main" or "side"
+                // can be "main", "side" or "full"
                 column: 'side',
                 column: 'side',
                 position: {
                 position: {
                     // Blocks are positioned relative to existing blocks on
                     // Blocks are positioned relative to existing blocks on
@@ -90,10 +90,11 @@ position: {
 
 
 ## Block Columns
 ## Block Columns
 
 
-Blocks can be placed in two columns:
+Blocks can be placed in three columns:
 
 
 - **`main`**: The main content area (wider column on the left)
 - **`main`**: The main content area (wider column on the left)
 - **`side`**: The sidebar area (narrower column on the right)
 - **`side`**: The sidebar area (narrower column on the right)
+- **`full`**: Takes up the full horizontal width. This is mostly useful for adding blocks to list pages
 
 
 ## Context Data
 ## Context Data
 
 

+ 1 - 1
packages/dashboard/src/lib/framework/extension-api/types/layout.ts

@@ -79,7 +79,7 @@ export type PageBlockPosition = { blockId: string; order: 'before' | 'after' | '
 export type PageBlockLocation = {
 export type PageBlockLocation = {
     pageId: string;
     pageId: string;
     position: PageBlockPosition;
     position: PageBlockPosition;
-    column: 'main' | 'side';
+    column: 'main' | 'side' | 'full';
 };
 };
 
 
 /**
 /**

+ 30 - 42
packages/dashboard/src/lib/framework/layout-engine/page-layout.tsx

@@ -242,89 +242,77 @@ export function PageLayout({ children, className }: Readonly<PageLayoutProps>) {
 
 
             // sort the blocks to make sure we have the correct order
             // sort the blocks to make sure we have the correct order
             const arrangedExtensionBlocks = matchingExtensionBlocks.sort((a, b) => {
             const arrangedExtensionBlocks = matchingExtensionBlocks.sort((a, b) => {
-                const orderPriority = { 'before': 1, 'replace': 2, 'after': 3 };
+                const orderPriority = { before: 1, replace: 2, after: 3 };
                 return orderPriority[a.location.position.order] - orderPriority[b.location.position.order];
                 return orderPriority[a.location.position.order] - orderPriority[b.location.position.order];
-            })
-
-            // get the length of blocks with the "before" position to know when to insert the child block
-            const beforeExtensionBlocksLength = arrangedExtensionBlocks.filter(
-                block => block.location.position.order === 'before',
-            ).length;
+            });
 
 
             const replacementBlockExists = arrangedExtensionBlocks.some(
             const replacementBlockExists = arrangedExtensionBlocks.some(
                 block => block.location.position.order === 'replace',
                 block => block.location.position.order === 'replace',
-            )
+            );
 
 
             let childBlockInserted = false;
             let childBlockInserted = false;
             if (matchingExtensionBlocks.length > 0) {
             if (matchingExtensionBlocks.length > 0) {
                 for (const extensionBlock of arrangedExtensionBlocks) {
                 for (const extensionBlock of arrangedExtensionBlocks) {
-
                     let extensionBlockShouldRender = true;
                     let extensionBlockShouldRender = true;
                     if (typeof extensionBlock?.shouldRender === 'function') {
                     if (typeof extensionBlock?.shouldRender === 'function') {
                         extensionBlockShouldRender = extensionBlock.shouldRender(page);
                         extensionBlockShouldRender = extensionBlock.shouldRender(page);
                     }
                     }
 
 
                     // Insert child block before the first non-"before" block
                     // Insert child block before the first non-"before" block
-                    if (!childBlockInserted && !replacementBlockExists && extensionBlock.location.position.order !== 'before') {
-                        finalChildArray.push(childBlock)
-                        childBlockInserted = true
-                      }
+                    if (
+                        !childBlockInserted &&
+                        !replacementBlockExists &&
+                        extensionBlock.location.position.order !== 'before'
+                    ) {
+                        finalChildArray.push(childBlock);
+                        childBlockInserted = true;
+                    }
+
+                    const isFullWidth = extensionBlock.location.column === 'full';
+                    const BlockComponent = isFullWidth ? FullWidthPageBlock : PageBlock;
 
 
                     const ExtensionBlock =
                     const ExtensionBlock =
                         extensionBlock.component && extensionBlockShouldRender ? (
                         extensionBlock.component && extensionBlockShouldRender ? (
-                            <PageBlock
+                            <BlockComponent
                                 key={extensionBlock.id}
                                 key={extensionBlock.id}
                                 column={extensionBlock.location.column}
                                 column={extensionBlock.location.column}
                                 blockId={extensionBlock.id}
                                 blockId={extensionBlock.id}
                                 title={extensionBlock.title}
                                 title={extensionBlock.title}
                             >
                             >
                                 {<extensionBlock.component context={page} />}
                                 {<extensionBlock.component context={page} />}
-                            </PageBlock>
+                            </BlockComponent>
                         ) : undefined;
                         ) : undefined;
 
 
-                    if(extensionBlockShouldRender && ExtensionBlock) {
-                        finalChildArray.push(ExtensionBlock)
+                    if (extensionBlockShouldRender && ExtensionBlock) {
+                        finalChildArray.push(ExtensionBlock);
                     }
                     }
                 }
                 }
 
 
                 // If all blocks were "before", insert child block at the end
                 // If all blocks were "before", insert child block at the end
                 if (!childBlockInserted && !replacementBlockExists) {
                 if (!childBlockInserted && !replacementBlockExists) {
-                    finalChildArray.push(childBlock)
+                    finalChildArray.push(childBlock);
                 }
                 }
-
             } else {
             } else {
                 finalChildArray.push(childBlock);
                 finalChildArray.push(childBlock);
             }
             }
         }
         }
     }
     }
 
 
+    const fullWidthBlocks = finalChildArray.filter(
+        child => isPageBlock(child) && isOfType(child, FullWidthPageBlock),
+    );
+    const mainBlocks = finalChildArray.filter(child => isPageBlock(child) && child.props.column === 'main');
+    const sideBlocks = finalChildArray.filter(child => isPageBlock(child) && child.props.column === 'side');
+
     return (
     return (
         <div className={cn('w-full space-y-4', className, '@container/layout')}>
         <div className={cn('w-full space-y-4', className, '@container/layout')}>
             {isDesktop ? (
             {isDesktop ? (
                 <div className="grid grid-cols-1 gap-4 @3xl/layout:grid-cols-4">
                 <div className="grid grid-cols-1 gap-4 @3xl/layout:grid-cols-4">
-                    {finalChildArray.map((child, index) => {
-                        const key = child.props?.blockId ?? `block-${index}`;
-                        if(isPageBlock(child )) {
-                            if (isOfType(child, FullWidthPageBlock)) {
-                                return (
-                                    <div key={key} className="@md/layout:col-span-5 space-y-4">{child}</div>
-                                );
-                            }
-
-                            if (child.props.column === 'main') {
-                                return (
-                                    <div key={key} className="@3xl/layout:col-span-3 space-y-4">{child}</div>
-                                )
-                            }
-
-                            if (child.props.column === 'side') {
-                                return (
-                                    <div key={key} className="@3xl/layout:col-span-1 space-y-4">{child}</div>
-                                )
-                            }
-                        }
-                        return null
-                    })}
+                    {fullWidthBlocks.length > 0 && (
+                        <div className="@md/layout:col-span-5 space-y-4">{fullWidthBlocks}</div>
+                    )}
+                    <div className="@3xl/layout:col-span-3 space-y-4">{mainBlocks}</div>
+                    <div className="@3xl/layout:col-span-1 space-y-4">{sideBlocks}</div>
                 </div>
                 </div>
             ) : (
             ) : (
                 <div className="space-y-4">{finalChildArray}</div>
                 <div className="space-y-4">{finalChildArray}</div>