Skip to content

Commit 4020c3a

Browse files
committed
truncate text with ellipsis
1 parent 75a054a commit 4020c3a

File tree

1 file changed

+36
-0
lines changed
  • services/static-webserver/client/source/class/osparc/wrapper

1 file changed

+36
-0
lines changed

services/static-webserver/client/source/class/osparc/wrapper/Svg.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,42 @@ qx.Class.define("osparc.wrapper.Svg", {
319319
.move(padding + iconSize + 8, ((bubbleHeight - titleFontSize) / 2) - 3);
320320
bubble.add(label);
321321

322+
// Compute available width for text
323+
const availableWidth = bubbleWidth - padding * 2 - iconSize - 8;
324+
325+
// Helper: truncate text with ellipsis
326+
const fitTextWithEllipsis = (fullText, maxWidth) => {
327+
let text = fullText;
328+
label.text(text);
329+
if (label.bbox().width <= maxWidth) {
330+
return text
331+
};
332+
333+
const ellipsis = '…';
334+
let low = 0;
335+
let high = text.length;
336+
337+
// Binary search for the max fitting length
338+
while (low < high) {
339+
let mid = Math.floor((low + high) / 2);
340+
label.text(text.slice(0, mid) + ellipsis);
341+
if (label.bbox().width <= maxWidth) {
342+
low = mid + 1;
343+
} else {
344+
high = mid;
345+
}
346+
}
347+
348+
return text.slice(0, low - 1) + ellipsis;
349+
}
350+
351+
// Truncate if needed
352+
const fittedText = fitTextWithEllipsis(title, availableWidth);
353+
label.text(fittedText);
354+
355+
// Move label to proper position
356+
label.move(padding + iconSize + 8, ((bubbleHeight - titleFontSize) / 2) - 3);
357+
322358
return bubble;
323359
},
324360

0 commit comments

Comments
 (0)