Skip to content

Commit 8557160

Browse files
committed
Tweak image positioning
1 parent a4683b6 commit 8557160

File tree

1 file changed

+51
-51
lines changed

1 file changed

+51
-51
lines changed

web/js/betterCombos.js

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,53 @@ function getType(node) {
1717

1818
const getImage = (imageId) => document.querySelector(`#${CSS.escape(imageId)}`);
1919

20-
const calculateImagePosition = (el, rootRect, bodyRect) => {
21-
const { x } = el.getBoundingClientRect();
22-
let { top, left } = rootRect;
23-
const { width: bodyWidth, height: bodyHeight } = bodyRect;
24-
25-
const isSpaceRight = x + IMAGE_WIDTH <= bodyWidth;
26-
if (isSpaceRight) {
27-
left += rootRect.width + IMAGE_WIDTH;
28-
}
29-
30-
const isSpaceBelow = rootRect.top + IMAGE_HEIGHT <= bodyHeight;
31-
if (!isSpaceBelow) {
32-
top = bodyHeight - IMAGE_HEIGHT;
33-
}
34-
35-
return { left: Math.round(left), top: Math.round(top) };
20+
const calculateImagePosition = (el, bodyRect) => {
21+
let { top, left, right } = el.getBoundingClientRect();
22+
const { width: bodyWidth, height: bodyHeight } = bodyRect;
23+
24+
const isSpaceRight = right + IMAGE_WIDTH <= bodyWidth;
25+
if (isSpaceRight) {
26+
left = right;
27+
} else {
28+
left -= IMAGE_WIDTH;
29+
}
30+
31+
top = top - IMAGE_HEIGHT / 2;
32+
if (top + IMAGE_HEIGHT > bodyHeight) {
33+
top = bodyHeight - IMAGE_HEIGHT;
34+
}
35+
if (top < 0) {
36+
top = 0;
37+
}
38+
39+
return { left: Math.round(left), top: Math.round(top), isLeft: !isSpaceRight };
3640
};
3741

38-
function showImage(el, imageId, getRootRect) {
39-
const img = getImage(imageId);
40-
if (img) {
41-
const rootRect = getRootRect();
42-
if (!rootRect) return;
42+
function showImage(el, imageId) {
43+
const img = getImage(imageId);
44+
if (img) {
45+
const bodyRect = document.body.getBoundingClientRect();
46+
if (!bodyRect) return;
4347

44-
const bodyRect = document.body.getBoundingClientRect();
45-
if (!bodyRect) return;
48+
const { left, top, isLeft } = calculateImagePosition(el, bodyRect);
4649

47-
const { left, top } = calculateImagePosition(el, rootRect, bodyRect);
50+
img.style.display = "block";
51+
img.style.left = `${left}px`;
52+
img.style.top = `${top}px`;
4853

49-
img.style.display = "block";
50-
img.style.left = `${left}px`;
51-
img.style.top = `${top}px`;
52-
}
54+
if (isLeft) {
55+
img.classList.add("left");
56+
} else {
57+
img.classList.remove("left");
58+
}
59+
}
5360
}
5461

5562
function closeImage(imageId) {
56-
const img = getImage(imageId);
57-
if (img) {
58-
img.style.display = "none";
59-
}
63+
const img = getImage(imageId);
64+
if (img) {
65+
img.style.display = "none";
66+
}
6067
}
6168

6269
app.registerExtension({
@@ -72,13 +79,14 @@ app.registerExtension({
7279
position: absolute;
7380
left: 0;
7481
top: 0;
75-
transform: translate(-100%, 0);
7682
width: ${IMAGE_WIDTH}px;
7783
height: ${IMAGE_HEIGHT}px;
7884
background-size: contain;
79-
background-position: top right;
8085
background-repeat: no-repeat;
81-
filter: brightness(65%);
86+
z-index: 9999;
87+
}
88+
.pysssss-combo-image.left {
89+
background-position: top right;
8290
}
8391
`,
8492
parent: document.body,
@@ -118,27 +126,22 @@ app.registerExtension({
118126
});
119127

120128
function encodeRFC3986URIComponent(str) {
121-
return encodeURIComponent(str).replace(
122-
/[!'()*]/g,
123-
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
124-
);
129+
return encodeURIComponent(str).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
125130
}
126131

127132
// After an element is created for an item, add an image if it has one
128133
contextMenuHook["addItem"].push(function (el, menu, [name, value, options]) {
129134
if (el && isCustomItem(value) && value?.image && !value.submenu) {
130135
const key = `pysssss-image-combo-${name}`;
131136
el.textContent += " *";
132-
$el(`div.pysssss-combo-image`, {
137+
$el("div.pysssss-combo-image", {
133138
id: key,
134139
parent: document.body,
135140
style: {
136141
backgroundImage: `url(/pysssss/view/${encodeRFC3986URIComponent(value.image)})`,
137142
},
138143
});
139-
140-
const getRootRect = () => menu.root?.getBoundingClientRect();
141-
const showHandler = () => showImage(el, key, getRootRect);
144+
const showHandler = () => showImage(el, key);
142145
const closeHandler = () => closeImage(key);
143146

144147
el.addEventListener("mouseenter", showHandler, { passive: true });
@@ -225,18 +228,18 @@ app.registerExtension({
225228
}
226229
}
227230
return false;
228-
}
231+
};
229232
const includesFromMenuItem = function (item) {
230233
if (item.submenu) {
231-
return includesFromMenuItems(item.submenu.options)
234+
return includesFromMenuItems(item.submenu.options);
232235
} else {
233236
return item.content === searchElement.content;
234237
}
235-
}
238+
};
236239

237240
const includes = valuesIncludes.apply(this, arguments) || includesFromMenuItems(this);
238241
return includes;
239-
}
242+
};
240243

241244
return v;
242245
},
@@ -324,7 +327,6 @@ app.registerExtension({
324327
return exampleCb?.apply(this, arguments) ?? exampleList.value;
325328
};
326329

327-
328330
const listExamples = async () => {
329331
exampleList.disabled = true;
330332
exampleList.options.values = ["[none]"];
@@ -386,9 +388,7 @@ app.registerExtension({
386388
img = this.imgs[this.overIndex];
387389
}
388390
if (img) {
389-
const nodes = app.graph._nodes.filter(
390-
(n) => n.comfyClass === LORA_LOADER || n.comfyClass === CHECKPOINT_LOADER
391-
);
391+
const nodes = app.graph._nodes.filter((n) => n.comfyClass === LORA_LOADER || n.comfyClass === CHECKPOINT_LOADER);
392392
if (nodes.length) {
393393
options.unshift({
394394
content: "Save as Preview",

0 commit comments

Comments
 (0)