From 9ada6f005fd854b220e2c0aa1b8d6066630e117c Mon Sep 17 00:00:00 2001 From: Ayobami Date: Wed, 2 Jul 2025 15:49:49 +0100 Subject: [PATCH] ISSUE 1: add selected doc to canvas editor --- src/components/Builder/Builder.jsx | 164 ++++++++++++--------- src/components/DockBuilder/DockBuilder.jsx | 42 +++--- 2 files changed, 115 insertions(+), 91 deletions(-) diff --git a/src/components/Builder/Builder.jsx b/src/components/Builder/Builder.jsx index 67d72e6..85e4982 100644 --- a/src/components/Builder/Builder.jsx +++ b/src/components/Builder/Builder.jsx @@ -5,13 +5,13 @@ import { fabric } from "fabric"; import { GrayMaterial, PerforatedMaterial, - WoodgrainMaterial + WoodgrainMaterial, } from "Assets/images"; import { DockPanelCategories, MaterialType, DockPanelCategoryMap, - CylinderType + CylinderType, } from "Utils/constants"; import { Chevron } from "Assets/svgs"; import MkdSDK from "Utils/MkdSDK"; @@ -21,7 +21,7 @@ import { getMaterial, getRampsCategory, getWedgesAndRampsMaterial, - getWedgesCategory + getWedgesCategory, } from "Utils/utils"; const sdk = new MkdSDK(); @@ -32,7 +32,7 @@ export const Builder = ({ editor }) => { DockPanelCategories.RollIn ); const rampsInitialState = { - data: [] + data: [], }; // const [ramps, setRamps] = useState(null); const [activeLiftRange, setActiveLiftRange] = useState(null); @@ -44,7 +44,7 @@ export const Builder = ({ editor }) => { wedges: [], ramps: [], selectedRamps: [], - selectedWedges: [] + selectedWedges: [], }); const [boatLift, setBoatLift] = useState([]); const [liftRanges, setLiftRanges] = useState([]); @@ -60,56 +60,80 @@ export const Builder = ({ editor }) => { [activeMaterial] ); - const onDockSelect = useCallback((dock) => { - if (!editor) { - return; - } - const editorHeight = editor.getHeight(); - const division = editorHeight / oneFeet - 4; + const onDockSelect = useCallback( + (dock) => { + if (!editor) { + return; + } + const editorHeight = editor.getHeight(); + const division = editorHeight / oneFeet - 4; - let imageTopViewURL; - let materials; - let category; - if (["wedges", "ramps"].includes(dock?.type)) { - imageTopViewURL = (dock?.top_view).replace("%20", "+"); + let imageTopViewURL; + let materials; + let category; + if (["wedges", "ramps"].includes(dock?.type)) { + imageTopViewURL = (dock?.top_view).replace("%20", "+"); + materials = getWedgesAndRampsMaterial(dock?.material); + } else { + imageTopViewURL = dock?.top_view; + materials = getMaterial(dock?.materials); + } - materials = getWedgesAndRampsMaterial(dock?.material); - } else { - imageTopViewURL = dock?.top_view; - materials = getMaterial(dock?.materials); - } + if (["ramps"].includes(dock?.type)) { + category = getRampsCategory(dock?.category); + } else if (["wedges"].includes(dock?.type)) { + category = getWedgesCategory(dock?.category); + } else { + category = getCategory(dock?.category); + } - if (["ramps"].includes(dock?.type)) { - category = getRampsCategory(dock?.category); - } else if (["wedges"].includes(dock?.type)) { - category = getWedgesCategory(dock?.category); - } else { - category = getCategory(dock?.category); - } + const dockData = { + itemName: activeDockCategory, + image: dock?.image, + category: category, + length: dock?.length, + materials: materials, + top_view: dock?.top_view, + width: dock?.width, + lift_range: dock?.lift_range, + model: dock?.model, + no_of_cylinders: dock?.no_of_cylinders, + name: dock?.name, + thumbnail: dock?.thumbnail, + weight_capacity: dock?.weight_capacity, + }; - const dockData = { - itemName: activeDockCategory, - image: dock?.image, - category: category, - length: dock?.length, - materials: materials, - top_view: dock?.top_view, - width: dock?.width, - lift_range: dock?.lift_range, - model: dock?.model, - no_of_cylinders: dock?.no_of_cylinders, - name: dock?.name, - thumbnail: dock?.thumbnail, - weight_capacity: dock?.weight_capacity - }; + // Add dock to editor as an image + const imageUrl = imageTopViewURL || dock?.image || dock?.thumbnail; - // TODO: Add dock to editor - // TODO: object which is the image should have the dockData, snapAngle of 45, snapThreshold of 5 - // TODO: image should be scaled down by scaleFactor - // TODO: image should be positioned at the top left of the editor - // TODO: image should be added to the editor - // TODO: render the editor - }, []); + if (!imageUrl) return; + + // Add dock to editor + fabric.Image.fromURL( + imageUrl, + function (img) { + img.set({ + left: 0, //Note that this starting pos is behind the sidebar + top: 0, + scaleX: scaleFactor, + scaleY: scaleFactor, + snapAngle: 45, // TODO: snapAngle + snapThreshold: 5, // TODO: snapThreshold + dockData: dockData, // TODO: attach dockData + selectable: true, + hasControls: true, + }); + + // Add to editor and make active + editor.add(img); // TODO: add to editor + editor.setActiveObject(img); + editor.requestRenderAll(); // TODO: render editor + }, + { crossOrigin: "anonymous" } + ); + }, + [editor, activeDockCategory] + ); const getItems = useCallback((table) => { // console.log( category, materials ); @@ -145,8 +169,8 @@ export const Builder = ({ editor }) => { ? [ ...result?.model.map((item) => ({ ...item, - type: "boatlifts" - })) + type: "boatlifts", + })), ] : [] ); @@ -158,8 +182,8 @@ export const Builder = ({ editor }) => { ? [ ...result?.model.map((item) => ({ ...item, - type: "accessories" - })) + type: "accessories", + })), ] : [] ); @@ -171,10 +195,10 @@ export const Builder = ({ editor }) => { ? [ ...result?.model.map((item) => ({ ...item, - type: "wedges" - })) + type: "wedges", + })), ] - : [] + : [], })); case Tables.Ramps: // return console.log( result ) @@ -182,7 +206,7 @@ export const Builder = ({ editor }) => { ...prev, ramps: result?.model ? [...result?.model.map((item) => ({ ...item, type: "ramps" }))] - : [] + : [], })); // return setRamps(() => [...result?.model]); } @@ -231,7 +255,7 @@ export const Builder = ({ editor }) => { [ DockPanelCategories.RollIn, DockPanelCategories.Floating, - DockPanelCategories.Sectional + DockPanelCategories.Sectional, ].includes(activeDockCategory) ) { // console.log( activeDockCategory, activeMaterial ) @@ -296,7 +320,7 @@ export const Builder = ({ editor }) => { : "bg-gray-200" }`} > - GreyMaterial + GreyMaterial
{ PerforatedMaterial
@@ -327,7 +351,7 @@ export const Builder = ({ editor }) => { WoodgrainMaterial @@ -353,7 +377,7 @@ export const Builder = ({ editor }) => { > {dock.length ? ( <> - +
{dock?.map((dockItem, index) => (