Skip to content

Commit

Permalink
Improve aliased containers inspecting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil committed Sep 19, 2024
1 parent bd7f8fa commit 7d6dcf6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
26 changes: 26 additions & 0 deletions Plugin/View/AliasHints.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace KingfisherDirect\BetterDebugHints\Plugin\View;

use Magento\Framework\View\Element\AbstractBlock;

class AliasHints
{
public function aroundGetChildHtml(AbstractBlock $block, \Closure $proceed, string $alias = '', $useCache = true)
{
$html = $proceed($alias, $useCache);

if (!$alias || !$html) {
return $html;
}

$layout = $block->getLayout();
$name = $block->getNameInLayout();
$childName = $layout->getChildName($name, $alias);

return
"<script type='text/mage-debug' data-mage-debug-position='start' data-mage-debug='$childName'></script>".
$html.
"<script type='text/mage-debug' data-mage-debug-position='end' data-mage-debug='$childName'></script>";
}
}
3 changes: 2 additions & 1 deletion Plugin/View/LayoutHints.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ private function getBlockInfo(AbstractBlock $block)
'template' => $block->getTemplateFile(),
'moduleName' => $block->getModuleName(),
'nameInLayout' => $block->getNameInLayout(),
'cacheKeyInfo' => @$block->getCacheKeyInfo()
'cacheKeyInfo' => @$block->getCacheKeyInfo(),
'cacheLifetime' => $block->getCacheLifetime()
];
}

Expand Down
4 changes: 4 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
<plugin name="KingfisherDirect_BetterDebugHints::layoutHints" type="KingfisherDirect\BetterDebugHints\Plugin\View\LayoutHints"/>
</type>

<type name="Magento\Framework\View\Element\AbstractBlock">
<plugin name="KingfisherDirect_BetterDebugHints::aliasHints" type="KingfisherDirect\BetterDebugHints\Plugin\View\AliasHints"/>
</type>

</config>
26 changes: 18 additions & 8 deletions view/base/web/js/LayoutHints.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ define([
}

inspect (element) {
var inspectable = this.findInspectable(element)
var { element: inspectable } = this.findInspectable(element)

if (!inspectable) {
console.error("No element found")
Expand All @@ -88,7 +88,7 @@ define([
continue
}

return element
return { element, debugger: type }
}
} while (element = element.parentElement)
}
Expand All @@ -106,7 +106,7 @@ define([
highlights.clear()

var elementUnderMouse = document.elementsFromPoint(event.clientX, event.clientY).shift()
var closestHighlightable = this.findInspectable(elementUnderMouse)
var { element: closestHighlightable } = this.findInspectable(elementUnderMouse)

if (!closestHighlightable) {
return
Expand Down Expand Up @@ -162,10 +162,8 @@ define([

const highlightOverlay = highlights.create(highlightedEl, content)

if (highlightedEl === element) {
highlightOverlay.addEventListener("click", event => this.onClickHighlight(element, event))
highlightOverlay.addEventListener("contextmenu", event => this.onRightClickHighlight(element, event))
}
highlightOverlay.addEventListener("click", event => this.onClickHighlight(element, event))
highlightOverlay.addEventListener("contextmenu", event => this.onRightClickHighlight(element, event))
}
}

Expand Down Expand Up @@ -198,11 +196,23 @@ define([
highlights.clear()
this.removeMouseTracker()

const inspectable = this.findInspectable(element)

// offload parent element searching to current debugger
const parentInspectables = inspectable && typeof this.debuggers[inspectable.debugger]?.parentInspectableElements === 'function'
? this.debuggers[inspectable.debugger].parentInspectableElements(element)
: []

// only consider it it's parent in debugger, but sibbling in HTML DOM
if (parentInspectables[0] && parentInspectables[0].parentElement === element.parentElement) {
return this.highlight(parentInspectables[0])
}

if (!element.parentElement) {
return
}

var parentInspectable = this.findInspectable(element.parentElement)
var { element: parentInspectable } = this.findInspectable(element.parentElement)

if (!parentInspectable) {
return
Expand Down
16 changes: 13 additions & 3 deletions view/base/web/js/debug/MageLayoutDebugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ define([], function () {
return { layout: element.mageLayout }
}

parentInspectableElements (element) {
const inspectable = element.mageLayout

if (!inspectable || !inspectable.parent) {
return
}

return inspectable.parent.elements
}

getHighlightsData (element) {
const layout = element.mageLayout
const badges = layout.name ? [layout.name] : layout.handles
Expand All @@ -120,10 +130,10 @@ define([], function () {
<div>Class: <code style="background: transparent">${layout.block.class}</code></div>
<div>Template: <code style="background: transparent">${layout.block.template}</code></div>
`
}

if (layout.moduleName) {
content += '<p><small>${layout.moduleName}</small></p>'
if (layout.block.cacheLifetime) {
content += `<div>Cache Lifetime: <code style="background: transparent">${layout.block.cacheLifetime}</code></div>`
}
}

return layout.elements.map(element => {
Expand Down

0 comments on commit 7d6dcf6

Please sign in to comment.