From 93d046a24cea2f03229db1cc7af697ff63cde3ea Mon Sep 17 00:00:00 2001 From: komkida91 Date: Tue, 24 Feb 2026 16:21:25 +0100 Subject: [PATCH] fix(builder): improve menu anchor targeting and free-drop placement --- elementor/templates/elementor_builder.html | 48 +++++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/elementor/templates/elementor_builder.html b/elementor/templates/elementor_builder.html index cd24155..b4e9b31 100644 --- a/elementor/templates/elementor_builder.html +++ b/elementor/templates/elementor_builder.html @@ -1030,23 +1030,36 @@ const state = { `; } const manualItems = Array.isArray(block.data?.items) ? block.data.items.map((x)=>String(x || "").trim()).filter(Boolean) : []; + const contentBlocks = state.blocks + .filter((b)=>b.type!=="menu") + .map((b, i)=>{ + const title = (b.data && (b.data.title || b.data.text || b.type)) || b.type; + return { id: b.id, label: String(title).slice(0, 24), index: i+1 }; + }); + const norm = (v)=>String(v || "") + .toLowerCase() + .normalize("NFD") + .replace(/[\u0300-\u036f]/g, "") + .replace(/[^a-z0-9]+/g, " ") + .trim(); const items = manualItems.length ? (manualItems.length >= 3 - ? manualItems.map((label, i)=>({ id: `menu_item_${i+1}`, label: String(label).slice(0, 24), index: i+1 })) + ? manualItems.map((label, i)=>{ + const key = norm(label); + const exact = contentBlocks.find((b)=>norm(b.label) === key); + const byIndex = contentBlocks[i]; + const target = exact || byIndex || null; + return { id: target ? target.id : "", label: String(label).slice(0, 24), index: i+1 }; + }) : []) - : state.blocks - .filter(b=>b.type!=="menu") - .map((b, i)=>{ - const title = (b.data && (b.data.title || b.data.text || b.type)) || b.type; - return { id: b.id, label: String(title).slice(0, 24), index: i+1 }; - }); + : contentBlocks; const safeItems = items.length ? items : [ { id: "menu_item_1", label: "Inicio", index: 1 }, { id: "menu_item_2", label: "Productos", index: 2 }, { id: "menu_item_3", label: "Contacto", index: 3 } ]; - const links = safeItems.map(it=>`${escapeHtml(it.label)}`).join(""); - const mobileLinks = safeItems.map(it=>`${escapeHtml(it.label)}`).join(""); + const links = safeItems.map(it=>`${escapeHtml(it.label)}`).join(""); + const mobileLinks = safeItems.map(it=>`${escapeHtml(it.label)}`).join(""); const siteName = String(state.settings.site_name || "GKACHELE"); const initial = escapeHtml(siteName.slice(0, 1).toUpperCase()); const logo = state.settings.logo_url @@ -2142,6 +2155,20 @@ const state = { } function wirePreviewDrop(){ const canvas=document.getElementById("previewCanvas"); + const getDropPosition = (block, clientX, clientY)=>{ + const rect = canvas.getBoundingClientRect(); + const widthPct = Math.max(20, Math.min(100, Number(block?.data?.width || 60))); + const estWidth = Math.max(120, (rect.width * widthPct) / 100); + const estHeight = Math.max(80, Number(block?.data?.height || 160)); + const left = snap(clientX - rect.left - (estWidth / 2)); + const top = snap(clientY - rect.top - (estHeight / 2)); + const maxX = Math.max(0, rect.width - estWidth); + const maxY = Math.max(0, rect.height - estHeight); + return { + x: Math.max(0, Math.min(maxX, left)), + y: Math.max(0, Math.min(maxY, top)) + }; + }; canvas.addEventListener("dragover",(e)=>{ e.preventDefault(); if (!state.settings.free_drag){ @@ -2159,8 +2186,7 @@ const state = { const b={ id: makeId(), type, data: defaultData(type) }; if (typeof b.data.width === "undefined") b.data.width = snapBlockWidth(type, 100); if (state.settings.free_drag){ - const rect = canvas.getBoundingClientRect(); - b.pos = { x: Math.max(0, e.clientX - rect.left - 20), y: Math.max(0, e.clientY - rect.top - 20) }; + b.pos = getDropPosition(b, e.clientX, e.clientY); } else { const container=getCanvasContainer(canvas); if (!container) return;