| @@ -1,22 +1,23 @@ | ||
| 1 | 1 | // Plain-DOM (not React) — mouseover-driven, runs outside the React |
| 2 | 2 | // commit cycle to avoid dropped clicks under fast pointer movement. |
| 3 | + | |
| 4 | +import { track } from '@shared/lib/track'; | |
| 3 | 5 | import { __ } from '@wordpress/i18n'; |
| 4 | 6 | import { useEditModeStore } from '../state/edit-mode'; |
| 5 | 7 | import { useQuickEditStore } from '../state/store'; |
| 6 | -import { isAgentEligibleForTarget } from './agent-gate'; | |
| 8 | +import { whenAnimationsSettle } from './after-animations'; | |
| 9 | +import { escapesDynamicBlock, isAgentEligibleForTarget } from './agent-gate'; | |
| 7 | 10 | import { |
| 8 | 11 | askAiAboutElement, |
| 9 | 12 | hasAgentBlockSelected, |
| 10 | 13 | isAgentAvailable, |
| 11 | - isAgentSidebarOpen, | |
| 12 | - stageAgentBlock, | |
| 13 | 14 | subscribeToAgentBlock, |
| 14 | 15 | } from './ask-ai'; |
| 15 | 16 | import { prefetchBlockSource } from './block-source-cache'; |
| 16 | 17 | import { decideClickAction } from './click-rule'; |
| 17 | 18 | import { resolveTarget } from './dom'; |
| 18 | -import { track } from './insights'; | |
| 19 | +import { needsContrastRing } from './over-media'; | |
| 19 | 20 | import { hasQuickEditModalFor } from './quick-edit-handlers'; |
| 20 | 21 | import { hasSaver, saveSelected } from './save-bridge'; |
| 21 | 22 | import { |
| 22 | 23 | getTranslatedContext, |
| @@ -41,8 +42,12 @@ | ||
| 41 | 42 | // Body-level fixed overlay rather than an outline on each block: |
| 42 | 43 | // outline overhang would clip inside an ancestor's overflow:hidden, |
| 43 | 44 | // and a single repositioning overlay gets the smooth-expand feel |
| 44 | 45 | // for free via CSS transitions. |
| 46 | +// Must match DOMHighlighter's minimum, or the outline resizes on click. | |
| 47 | +const MIN_OUTLINE_SIZE = 10; | |
| 48 | +const framed = (size) => Math.max(size, MIN_OUTLINE_SIZE); | |
| 49 | + | |
| 45 | 50 | const ensureOutline = () => { |
| 46 | 51 | if (hoverOutline) return hoverOutline; |
| 47 | 52 | hoverOutline = document.createElement('div'); |
| 48 | 53 | hoverOutline.className = |
| @@ -56,10 +61,11 @@ | ||
| 56 | 61 | // afford to animate: every scroll event would reset the transition target |
| 57 | 62 | // while the outline is still en route, so the outline visibly trails the |
| 58 | 63 | // content during a drag-scroll ("stays fixed in screen"). Hover-driven |
| 59 | 64 | // updates (block A → block B) keep the spring animation. |
| 60 | -const showOutline = (el, { instant = false } = {}) => { | |
| 61 | - const overlay = ensureOutline(); | |
| 65 | +let dropSettle = () => {}; | |
| 66 | + | |
| 67 | +const positionOutline = (overlay, el, instant) => { | |
| 62 | 68 | if (instant) { |
| 63 | 69 | overlay.style.transition = 'none'; |
| 64 | 70 | } else if (overlay.style.transition === 'none') { |
| 65 | 71 | overlay.style.transition = ''; |
| @@ -64,17 +70,30 @@ | ||
| 64 | 70 | } else if (overlay.style.transition === 'none') { |
| 65 | 71 | overlay.style.transition = ''; |
| 66 | 72 | } |
| 67 | 73 | const r = el.getBoundingClientRect(); |
| 68 | - overlay.style.top = `${r.top}px`; | |
| 69 | - overlay.style.left = `${r.left}px`; | |
| 70 | - overlay.style.width = `${r.width}px`; | |
| 71 | - overlay.style.height = `${r.height}px`; | |
| 74 | + const width = framed(r.width); | |
| 75 | + const height = framed(r.height); | |
| 76 | + overlay.style.top = `${r.top - (height - r.height) / 2}px`; | |
| 77 | + overlay.style.left = `${r.left - (width - r.width) / 2}px`; | |
| 78 | + overlay.style.width = `${width}px`; | |
| 79 | + overlay.style.height = `${height}px`; | |
| 80 | +}; | |
| 81 | + | |
| 82 | +const showOutline = (el, { instant = false } = {}) => { | |
| 83 | + const overlay = ensureOutline(); | |
| 84 | + positionOutline(overlay, el, instant); | |
| 85 | + overlay.classList.toggle('is-over-media', needsContrastRing(el)); | |
| 72 | 86 | overlay.classList.add('is-visible'); |
| 87 | + dropSettle(); | |
| 88 | + dropSettle = whenAnimationsSettle(el, () => | |
| 89 | + positionOutline(overlay, el, true), | |
| 90 | + ); | |
| 73 | 91 | debugLog(instant ? 'showOutline (instant)' : 'showOutline', el); |
| 74 | 92 | }; |
| 75 | 93 | |
| 76 | 94 | const hideOutline = () => { |
| 95 | + dropSettle(); | |
| 77 | 96 | hoverOutline?.classList.remove('is-visible'); |
| 78 | 97 | debugLog('hideOutline'); |
| 79 | 98 | }; |
| 80 | 99 | |
| @@ -87,17 +106,43 @@ | ||
| 87 | 106 | const PART_ATTR = 'data-extendify-part-block-id'; |
| 88 | 107 | const PRODUCT_ATTR = 'data-extendify-quick-edit-product-id'; |
| 89 | 108 | const WPFORM_FIELD_ATTR = 'data-extendify-quick-edit-wpform-field-id'; |
| 90 | 109 | const MEDIATEXT_MEDIA_ATTR = 'data-extendify-quick-edit-mediatext-media'; |
| 110 | +const PART_SLUG_ATTR = 'data-extendify-part-slug'; | |
| 91 | 111 | |
| 112 | +// A synced pattern's blocks live in the wp_block post the id names; Quick | |
| 113 | +// Edit's save only ever writes the container, so it can't reach them. | |
| 114 | +const isSyncedPatternId = (id) => /^block:\d+:\d+$/.test(String(id ?? '')); | |
| 115 | + | |
| 116 | +// DOMHighlighter's class; a sync listener can't read a React prop. | |
| 117 | +export const isAgentWorking = () => | |
| 118 | + !!document.querySelector('.wp-site-blocks.extendify-agent-working'); | |
| 119 | + | |
| 120 | +const isAgentWaiting = () => | |
| 121 | + !!document.querySelector('.wp-site-blocks.extendify-agent-waiting'); | |
| 122 | + | |
| 123 | +const hasAgentTask = () => | |
| 124 | + !!document.querySelector('.wp-site-blocks.extendify-agent-task'); | |
| 125 | + | |
| 92 | 126 | // Resolve the live DOM node for the currently-staged agent block, so the |
| 93 | 127 | // click + hover gates can carve out "inside the staged block." Returns |
| 94 | 128 | // null when no block is staged or its node has detached from the tree. |
| 129 | +// Without the slug an id matches another part's block of the same number, and | |
| 130 | +// a click inside the staged block reads as an outside-click. | |
| 95 | 131 | const stagedBlockEl = () => { |
| 96 | 132 | const block = useQuickEditStore.getState().agentBlock; |
| 97 | 133 | if (!block?.id) return null; |
| 98 | 134 | const attr = block.target || POST_ATTR; |
| 99 | - return document.querySelector(`[${attr}="${CSS.escape(String(block.id))}"]`); | |
| 135 | + const slug = block.source?.partSlug || null; | |
| 136 | + const matches = [ | |
| 137 | + ...document.querySelectorAll(`[${attr}="${CSS.escape(String(block.id))}"]`), | |
| 138 | + ]; | |
| 139 | + const inScope = matches.filter( | |
| 140 | + (el) => | |
| 141 | + (el.closest(`[${PART_SLUG_ATTR}]`)?.getAttribute(PART_SLUG_ATTR) ?? | |
| 142 | + null) === slug, | |
| 143 | + ); | |
| 144 | + return inScope[0] ?? matches[0] ?? null; | |
| 100 | 145 | }; |
| 101 | 146 | |
| 102 | 147 | // Resolve the committed selection's live DOM node. buildTarget stashes |
| 103 | 148 | // the element reference on the descriptor; if the block has been swapped |
| @@ -200,9 +245,9 @@ | ||
| 200 | 245 | // a cover's inner-container) so the bar resolves to the nearest editable |
| 201 | 246 | // parent. Without this, hovering the middle of a hero cover that |
| 202 | 247 | // surfaces post-title returned blockType=null and — combined with the |
| 203 | 248 | // template-part source gating Ask AI off — produced no bar at all. |
| 204 | -const buildTarget = (el) => { | |
| 249 | +const buildTarget = (el, fromEl = el) => { | |
| 205 | 250 | let current = resolveTarget(el); |
| 206 | 251 | let safety = 5; |
| 207 | 252 | while ( |
| 208 | 253 | current && |
| @@ -213,8 +258,11 @@ | ||
| 213 | 258 | const next = resolveTarget(current.el.parentElement); |
| 214 | 259 | if (!next) return current; |
| 215 | 260 | current = next; |
| 216 | 261 | } |
| 262 | + if (current && escapesDynamicBlock(fromEl, current.el)) { | |
| 263 | + return { ...current, dynamicInterior: true }; | |
| 264 | + } | |
| 217 | 265 | return current; |
| 218 | 266 | }; |
| 219 | 267 | |
| 220 | 268 | // Picker blocks keep the bar visible because the dropdown anchors |
| @@ -233,18 +281,33 @@ | ||
| 233 | 281 | // pill shows the error inline instead, and Ask AI stays reachable. |
| 234 | 282 | const isTranslatedTextBlock = (target) => |
| 235 | 283 | isTranslatedRender() && isTextBearing(target?.blockType); |
| 236 | 284 | |
| 237 | -// Which pills a target would surface (without mounting the bar). Click rule | |
| 238 | -// (Option 7) needs this to decide between opening QE directly, today's | |
| 239 | -// sticky commit, and the silent agent stage. Exported so keyboard-entry | |
| 240 | -// gates Enter on the same signal the hover bar uses. | |
| 285 | +// Which pills a target would surface (without mounting the bar). A target | |
| 286 | +// with no pills isn't worth pinning. Exported so keyboard-entry gates Enter | |
| 287 | +// on the same signal the hover bar uses. | |
| 241 | 288 | export const pillContextFor = (target) => { |
| 242 | 289 | const quickEditEnabled = !!window.extQuickEditData?.quickEditEnabled; |
| 290 | + // A synced pattern is addressed off whichever tagger stamped it, so both | |
| 291 | + // id spaces have to be checked or the pill returns on pages. | |
| 292 | + const compositeId = | |
| 293 | + target?.el?.getAttribute?.(PART_ATTR) ?? | |
| 294 | + target?.el?.getAttribute?.(POST_ATTR); | |
| 295 | + // An open editor blocks sending, so it would stall the agent's task. | |
| 243 | 296 | const quickEditable = |
| 244 | - quickEditEnabled && hasQuickEditModalFor(target?.blockType); | |
| 297 | + quickEditEnabled && | |
| 298 | + !hasAgentTask() && | |
| 299 | + hasQuickEditModalFor(target?.blockType) && | |
| 300 | + !isSyncedPatternId(compositeId); | |
| 245 | 301 | const sourceKind = target?.source?.kind ?? null; |
| 246 | - const agentSupportedSource = sourceKind === 'post' || sourceKind === null; | |
| 302 | + // A ref-nav item routes Quick Edit's save through wp_navigation, but the | |
| 303 | + // agent reaches it through the id the part tagger stamped. | |
| 304 | + const agentSupportedSource = | |
| 305 | + sourceKind === 'post' || | |
| 306 | + sourceKind === 'template-part' || | |
| 307 | + sourceKind === null || | |
| 308 | + (sourceKind === 'wp-navigation' && | |
| 309 | + !!target?.el?.getAttribute?.('data-extendify-part-block-id')); | |
| 247 | 310 | const aiAvailable = |
| 248 | 311 | isAgentAvailable() && |
| 249 | 312 | agentSupportedSource && |
| 250 | 313 | isAgentEligibleForTarget(target); |
| @@ -250,9 +313,9 @@ | ||
| 250 | 313 | isAgentEligibleForTarget(target); |
| 251 | 314 | return { quickEditable, aiAvailable }; |
| 252 | 315 | }; |
| 253 | 316 | |
| 254 | -// Exported for keyboard-entry to bypass the bar's click handler. | |
| 317 | +// Test seam for the Quick Edit pill's handler, which isn't exported. | |
| 255 | 318 | export const editTarget = (target) => onEditClick(target); |
| 256 | 319 | |
| 257 | 320 | const onEditClick = (target) => { |
| 258 | 321 | const store = useQuickEditStore.getState(); |
| @@ -272,12 +335,20 @@ | ||
| 272 | 335 | // Snapshot the bar rect before clearing — ImagePicker anchors to it. |
| 273 | 336 | const anchorRect = hoverBar?.getBoundingClientRect() ?? null; |
| 274 | 337 | const placement = hoverBar?.dataset.extendifyQuickEditPlacement ?? 'above'; |
| 275 | 338 | |
| 276 | - if (!isPickerType(target.blockType)) clearBar(); | |
| 339 | + const isPicker = isPickerType(target.blockType); | |
| 340 | + if (!isPicker) clearBar(); | |
| 341 | + // Order matters: clearing the commit first drops a picker's anchor bar. | |
| 342 | + store.setSelected({ ...target, anchorRect, anchorPlacement: placement }); | |
| 277 | 343 | store.setCommittedSelection(null); |
| 278 | - store.setSelected({ ...target, anchorRect, anchorPlacement: placement }); | |
| 279 | - track('quick_edit_clicked', { blockType: target.blockType }); | |
| 344 | + | |
| 345 | + if (!isPicker) { | |
| 346 | + track('quick_edit_action', { | |
| 347 | + element: target.blockType, | |
| 348 | + type: 'quick_edit', | |
| 349 | + }); | |
| 350 | + } | |
| 280 | 351 | }; |
| 281 | 352 | |
| 282 | 353 | const onAiClick = (el) => { |
| 283 | 354 | // clearSelected before clearBar: when QE was clicked first on a |
| @@ -288,14 +359,16 @@ | ||
| 288 | 359 | const store = useQuickEditStore.getState(); |
| 289 | 360 | store.clearSelected(); |
| 290 | 361 | clearBar(); |
| 291 | 362 | store.setCommittedSelection(null); |
| 292 | - track('ask_ai_clicked', { matched: !!resolveTarget(el)?.blockType }); | |
| 363 | + track('quick_edit_action', { | |
| 364 | + element: resolveTarget(el)?.blockType ?? null, | |
| 365 | + type: 'ask_ai', | |
| 366 | + }); | |
| 293 | 367 | askAiAboutElement(el); |
| 294 | 368 | }; |
| 295 | 369 | |
| 296 | -// Exported for keyboard-entry to route Enter on an Ask-AI-only block | |
| 297 | -// straight to the agent, mirroring the Ask AI pill's click handler. | |
| 370 | +// The Ask AI pill's handler, for callers holding an element, not a pill. | |
| 298 | 371 | export const askAiTarget = (el) => onAiClick(el); |
| 299 | 372 | |
| 300 | 373 | // Exported for keyboard-entry's focus-driven mount/dismiss. |
| 301 | 374 | export const showBar = (el) => renderBar(el); |
| @@ -300,16 +373,17 @@ | ||
| 300 | 373 | // Exported for keyboard-entry's focus-driven mount/dismiss. |
| 301 | 374 | export const showBar = (el) => renderBar(el); |
| 302 | 375 | export const hideBar = () => clearBar(); |
| 303 | 376 | |
| 304 | -const renderBar = (el) => { | |
| 377 | +const renderBar = (el, fromEl = el) => { | |
| 305 | 378 | // While an agent block is staged, the hover bar is intentionally |
| 306 | 379 | // hidden — only DOMHighlighter's X-close indicator is shown. |
| 307 | 380 | // Defense in depth for any caller (a re-render, the keyboard |
| 308 | 381 | // entry's showBar) that might otherwise paint a stale bar. |
| 309 | 382 | if (hasAgentBlockSelected()) return; |
| 383 | + if (isAgentWorking()) return; | |
| 310 | 384 | |
| 311 | - const target = buildTarget(el); | |
| 385 | + const target = buildTarget(el, fromEl); | |
| 312 | 386 | const { quickEditable, aiAvailable } = pillContextFor(target); |
| 313 | 387 | // Bail BEFORE clearing the current bar — when the cursor traverses |
| 314 | 388 | // from a renderable block to an UNSUPPORTED tagged ancestor (e.g. a |
| 315 | 389 | // tagged group with too many inner tagged blocks, or a tagged |
| @@ -387,9 +461,13 @@ | ||
| 387 | 461 | aiBtn.type = 'button'; |
| 388 | 462 | aiBtn.className = 'extendify-quick-edit-pill extendify-quick-edit-pill-ai'; |
| 389 | 463 | aiBtn.setAttribute('data-extendify-quick-edit-pill', ''); |
| 390 | 464 | aiBtn.innerHTML = '<span aria-hidden="true">✦</span>'; |
| 391 | - aiBtn.append(__('Ask AI', 'extendify-local')); | |
| 465 | + // translators: Button on a page block that hands it to the AI agent, which asked the user to pick one. | |
| 466 | + const shareLabel = __('Share with agent', 'extendify-local'); | |
| 467 | + aiBtn.append( | |
| 468 | + isAgentWaiting() ? shareLabel : __('Ask AI', 'extendify-local'), | |
| 469 | + ); | |
| 392 | 470 | aiBtn.addEventListener('mousedown', stopMouseDown); |
| 393 | 471 | aiBtn.addEventListener('click', (ev) => { |
| 394 | 472 | ev.preventDefault(); |
| 395 | 473 | ev.stopPropagation(); |
| @@ -397,13 +475,58 @@ | ||
| 397 | 475 | }); |
| 398 | 476 | bar.appendChild(aiBtn); |
| 399 | 477 | } |
| 400 | 478 | |
| 479 | + const closeBtn = document.createElement('button'); | |
| 480 | + closeBtn.type = 'button'; | |
| 481 | + closeBtn.className = | |
| 482 | + 'extendify-quick-edit-pill extendify-quick-edit-pill-close'; | |
| 483 | + closeBtn.setAttribute('data-extendify-quick-edit-pill', ''); | |
| 484 | + closeBtn.setAttribute('aria-label', __('Dismiss', 'extendify-local')); | |
| 485 | + closeBtn.innerHTML = '<span aria-hidden="true">✕</span>'; | |
| 486 | + closeBtn.addEventListener('mousedown', stopMouseDown); | |
| 487 | + closeBtn.addEventListener('click', (ev) => { | |
| 488 | + ev.preventDefault(); | |
| 489 | + ev.stopPropagation(); | |
| 490 | + useQuickEditStore.getState().setCommittedSelection(null); | |
| 491 | + clearBar(); | |
| 492 | + }); | |
| 493 | + bar.appendChild(closeBtn); | |
| 494 | + | |
| 401 | 495 | document.body.appendChild(bar); |
| 402 | 496 | hoverBar = bar; |
| 497 | + syncDismissState(); | |
| 403 | 498 | positionBar(bar, anchorEl); |
| 404 | 499 | }; |
| 405 | 500 | |
| 501 | +// Nothing to dismiss until a click pins the bar; hover ends with the pointer. | |
| 502 | +const syncDismissState = () => { | |
| 503 | + const closeBtn = hoverBar?.querySelector('.extendify-quick-edit-pill-close'); | |
| 504 | + if (!closeBtn) return; | |
| 505 | + closeBtn.disabled = !useQuickEditStore.getState().committedSelection; | |
| 506 | +}; | |
| 507 | + | |
| 508 | +// preventScroll: the pill is already on screen; scrolling to it jumps the page. | |
| 509 | +const focusFirstPill = () => { | |
| 510 | + hoverBar | |
| 511 | + ?.querySelector('.extendify-quick-edit-pill') | |
| 512 | + ?.focus({ preventScroll: true }); | |
| 513 | +}; | |
| 514 | + | |
| 515 | +export const pinTarget = (el, target = buildTarget(el)) => { | |
| 516 | + const { quickEditable, aiAvailable } = pillContextFor(target); | |
| 517 | + const store = useQuickEditStore.getState(); | |
| 518 | + if (!quickEditable && !aiAvailable) { | |
| 519 | + store.setCommittedSelection(null); | |
| 520 | + clearBar(); | |
| 521 | + return; | |
| 522 | + } | |
| 523 | + renderBar(el); | |
| 524 | + store.setCommittedSelection(target); | |
| 525 | + syncDismissState(); | |
| 526 | + focusFirstPill(); | |
| 527 | +}; | |
| 528 | + | |
| 406 | 529 | // Walk up to the innermost tagged ancestor. resolveTarget then derives |
| 407 | 530 | // blockType from that element's wp-block-* class; the pill renderer |
| 408 | 531 | // decides which pills (if any) to show. Lighter than resolveTarget — |
| 409 | 532 | // onMouseOver hot path doesn't need the full descriptor. |
| @@ -426,8 +549,9 @@ | ||
| 426 | 549 | |
| 427 | 550 | const onMouseOver = (e) => { |
| 428 | 551 | if (!useEditModeStore.getState().on) return; |
| 429 | 552 | if (useQuickEditStore.getState().selected) return; |
| 553 | + if (isAgentWorking()) return; | |
| 430 | 554 | // Sticky modes hard-suppress all hover-driven bar movement. |
| 431 | 555 | // - agentBlock staged: the bar is intentionally hidden; only |
| 432 | 556 | // DOMHighlighter's X-close is shown. To re-engage Ask AI on the |
| 433 | 557 | // same block, the user clicks X-close (clears agentBlock) then |
| @@ -443,9 +567,9 @@ | ||
| 443 | 567 | } |
| 444 | 568 | const el = findTagged(e.target); |
| 445 | 569 | if (el === hoverTarget) return; |
| 446 | 570 | if (!el) return; |
| 447 | - renderBar(el); | |
| 571 | + renderBar(el, e.target); | |
| 448 | 572 | }; |
| 449 | 573 | |
| 450 | 574 | const onScrollOrResize = () => { |
| 451 | 575 | if (!hoverTarget) return; |
| @@ -492,24 +616,18 @@ | ||
| 492 | 616 | |
| 493 | 617 | const onDocClickCapture = (e) => { |
| 494 | 618 | if (!useEditModeStore.getState().on) return; |
| 495 | 619 | if (e.target?.closest?.(QE_INTERIOR)) return; |
| 620 | + // Below QE_INTERIOR, or a run also deadens the sidebar and canvas. | |
| 621 | + if (isAgentWorking()) return; | |
| 496 | 622 | |
| 497 | 623 | // Implicit close on the QE text-edit canvas: clicks outside the canvas |
| 498 | 624 | // while it's open save the in-flight edits instead of discarding them. |
| 499 | - // `alsoClear: false` only when the click will open QE on a different | |
| 500 | - // block (the `select` branch's `quickEditable` cell); otherwise save | |
| 501 | - // clears so the canvas unmounts. Without that distinction the click | |
| 502 | - // would race: save's `clearSelected(null)` would overwrite the new | |
| 503 | - // block's `setSelected(B)`. `hasSaver()` is false for picker blocks | |
| 504 | - // (image / cover) — they save synchronously on pick and never | |
| 505 | - // register. Fall through either way so the existing agentBlock-clear + | |
| 506 | - // clear-bar branches still run. | |
| 625 | + // `hasSaver()` is false for picker blocks (image / cover) — they save | |
| 626 | + // synchronously on pick and never register. Fall through either way so | |
| 627 | + // the existing agentBlock-clear + clear-bar branches still run. | |
| 507 | 628 | if (hasSaver() && useQuickEditStore.getState().selected) { |
| 508 | - const tagged = findTagged(e.target); | |
| 509 | - const willOpenQE = | |
| 510 | - !!tagged && hasQuickEditModalFor(buildTarget(tagged)?.blockType); | |
| 511 | - saveSelected({ alsoClear: !willOpenQE }); | |
| 629 | + saveSelected(); | |
| 512 | 630 | } |
| 513 | 631 | |
| 514 | 632 | // Soft selection: while a block is staged for Ask AI, clicks INSIDE |
| 515 | 633 | // the staged block route natively (anchor navigates, form control |
| @@ -559,64 +677,23 @@ | ||
| 559 | 677 | switch (result.action) { |
| 560 | 678 | case 'select': { |
| 561 | 679 | e.preventDefault(); |
| 562 | 680 | e.stopPropagation(); |
| 563 | - // stopPropagation above blocks ImagePicker's bubble-phase | |
| 564 | - // outside-click — without this clear its menu lingers (issue 19). | |
| 565 | 681 | const store = useQuickEditStore.getState(); |
| 566 | - if ( | |
| 567 | - store.selected && | |
| 568 | - isPickerType(store.selected.blockType) && | |
| 569 | - store.selected.el !== result.el | |
| 570 | - ) { | |
| 682 | + // Only pickers reach this — other canvases cover the block they | |
| 683 | + // opened on. | |
| 684 | + if (store.selected?.el === result.el) { | |
| 571 | 685 | store.clearSelected(); |
| 572 | - } | |
| 573 | - const target = buildTarget(result.el); | |
| 574 | - const { quickEditable, aiAvailable } = pillContextFor(target); | |
| 575 | - | |
| 576 | - // Click semantics by pill count + agent-open state: | |
| 577 | - // QE-only → open QE menu directly (collapsed gesture). | |
| 578 | - // AI-only + closed → today's sticky commit (the one path that | |
| 579 | - // keeps committedSelection alive). | |
| 580 | - // AI-only + open → silently stage agentBlock (bridge). | |
| 581 | - // Both pills → open QE menu directly; bridge agentBlock | |
| 582 | - // too when the agent sidebar is open. The | |
| 583 | - // Ask AI button now lives on the QE bar | |
| 584 | - // chrome (BlockTextEditor.jsx), so the | |
| 585 | - // collapsed click no longer hides Ask AI. | |
| 586 | - // Picker-type blocks (image, cover) are | |
| 587 | - // exempt from the silent stage — the | |
| 588 | - // hover bar stays mounted for them and | |
| 589 | - // keeps the Ask AI pill, so the user | |
| 590 | - // escalates explicitly rather than seeing | |
| 591 | - // both the picker dropdown AND the | |
| 592 | - // agent's X-close at once. | |
| 593 | - // Tagged but neither → clear (no outline on a block the user | |
| 594 | - // can't act on). | |
| 595 | - if (quickEditable) { | |
| 596 | - renderBar(result.el); | |
| 597 | - onEditClick(target); | |
| 598 | - if ( | |
| 599 | - aiAvailable && | |
| 600 | - isAgentSidebarOpen() && | |
| 601 | - !isPickerType(target.blockType) | |
| 602 | - ) { | |
| 603 | - stageAgentBlock(result.el); | |
| 604 | - } | |
| 605 | - return; | |
| 606 | - } | |
| 607 | - if (aiAvailable && isAgentSidebarOpen()) { | |
| 608 | - useQuickEditStore.getState().setCommittedSelection(null); | |
| 686 | + store.setCommittedSelection(null); | |
| 609 | 687 | clearBar(); |
| 610 | - stageAgentBlock(result.el); | |
| 611 | 688 | return; |
| 612 | 689 | } |
| 613 | - if (aiAvailable) { | |
| 614 | - useQuickEditStore.getState().setCommittedSelection(target); | |
| 615 | - renderBar(result.el); | |
| 616 | - return; | |
| 690 | + // stopPropagation above blocks ImagePicker's bubble-phase | |
| 691 | + // outside-click — without this clear its menu lingers (issue 19). | |
| 692 | + if (store.selected && isPickerType(store.selected.blockType)) { | |
| 693 | + store.clearSelected(); | |
| 617 | 694 | } |
| 618 | - clearBar(); | |
| 695 | + pinTarget(result.el); | |
| 619 | 696 | return; |
| 620 | 697 | } |
| 621 | 698 | case 'clear': |
| 622 | 699 | clearBar(); |
| @@ -628,8 +705,9 @@ | ||
| 628 | 705 | |
| 629 | 706 | let unsubEditMode = null; |
| 630 | 707 | let unsubSelected = null; |
| 631 | 708 | let unsubAgentBlock = null; |
| 709 | +let workingObserver = null; | |
| 632 | 710 | let unsubCommitted = null; |
| 633 | 711 | |
| 634 | 712 | export const attach = () => { |
| 635 | 713 | if (attached) return; |
| @@ -637,12 +715,8 @@ | ||
| 637 | 715 | document.addEventListener('mouseover', onMouseOver, true); |
| 638 | 716 | window.addEventListener('scroll', onScrollOrResize, true); |
| 639 | 717 | window.addEventListener('resize', onScrollOrResize); |
| 640 | 718 | document.addEventListener('click', onDocClickCapture, true); |
| 641 | - // Warm the agent-sidebar state cache so the sync click rule has fresh | |
| 642 | - // state by the time the user clicks. The dynamic import resolves on | |
| 643 | - // the microtask queue; user clicks are seconds-later in real use. | |
| 644 | - isAgentSidebarOpen(); | |
| 645 | 719 | |
| 646 | 720 | unsubEditMode = useEditModeStore.subscribe((state) => { |
| 647 | 721 | if (!state.on) { |
| 648 | 722 | useQuickEditStore.getState().setCommittedSelection(null); |
| @@ -657,9 +731,12 @@ | ||
| 657 | 731 | let lastCommitted = useQuickEditStore.getState().committedSelection; |
| 658 | 732 | unsubCommitted = useQuickEditStore.subscribe((state) => { |
| 659 | 733 | const prev = lastCommitted; |
| 660 | 734 | lastCommitted = state.committedSelection; |
| 661 | - if (prev && !state.committedSelection) clearBar(); | |
| 735 | + if (!prev || state.committedSelection) return; | |
| 736 | + // A picker dropdown anchors to the bar; clearing here orphans it. | |
| 737 | + if (isPickerType(state.selected?.blockType)) return; | |
| 738 | + clearBar(); | |
| 662 | 739 | }); |
| 663 | 740 | // Picker dropdown anchors to the bar; keep it visible for those. |
| 664 | 741 | // On the non-null → null transition (Esc / Cancel / Save closes the |
| 665 | 742 | // canvas) the bar is re-rendered on the previously edited element |
| @@ -699,13 +776,30 @@ | ||
| 699 | 776 | } |
| 700 | 777 | if (!prev?.el || !document.body.contains(prev.el)) return; |
| 701 | 778 | if (!useEditModeStore.getState().on) return; |
| 702 | 779 | if (hoverTarget === prev.el && hoverBar) return; |
| 780 | + // A save resolves after a newer pin; re-rendering here steals its bar. | |
| 781 | + const committed = useQuickEditStore.getState().committedSelection; | |
| 782 | + if (committed?.el && committed.el !== prev.el) return; | |
| 703 | 783 | renderBar(prev.el); |
| 704 | 784 | }); |
| 705 | 785 | unsubAgentBlock = subscribeToAgentBlock((hasBlock) => { |
| 706 | 786 | if (hasBlock) clearBar(); |
| 707 | 787 | }); |
| 788 | + // A bar mounted before the run began stays clickable otherwise. | |
| 789 | + workingObserver = new MutationObserver(() => { | |
| 790 | + if (!isAgentWorking()) return; | |
| 791 | + // Otherwise hover stays suppressed after the workflow ends. | |
| 792 | + useQuickEditStore.getState().setCommittedSelection(null); | |
| 793 | + clearBar(); | |
| 794 | + }); | |
| 795 | + const root = document.querySelector('.wp-site-blocks'); | |
| 796 | + if (root) { | |
| 797 | + workingObserver.observe(root, { | |
| 798 | + attributes: true, | |
| 799 | + attributeFilter: ['class'], | |
| 800 | + }); | |
| 801 | + } | |
| 708 | 802 | }; |
| 709 | 803 | |
| 710 | 804 | export const detach = () => { |
| 711 | 805 | if (!attached) return; |
| @@ -716,12 +810,14 @@ | ||
| 716 | 810 | document.removeEventListener('click', onDocClickCapture, true); |
| 717 | 811 | unsubEditMode?.(); |
| 718 | 812 | unsubSelected?.(); |
| 719 | 813 | unsubAgentBlock?.(); |
| 814 | + workingObserver?.disconnect(); | |
| 720 | 815 | unsubCommitted?.(); |
| 721 | 816 | unsubEditMode = null; |
| 722 | 817 | unsubSelected = null; |
| 723 | 818 | unsubAgentBlock = null; |
| 819 | + workingObserver = null; | |
| 724 | 820 | unsubCommitted = null; |
| 725 | 821 | clearBar(); |
| 726 | 822 | removeOutline(); |
| 727 | 823 | }; |