dispatch/client/js/utils/messageHeight.js

66 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-02-16 21:43:25 +00:00
const lineHeight = 24;
const userListWidth = 200;
const smallScreen = 600;
2017-05-12 08:51:37 +00:00
2020-06-30 11:24:23 +00:00
export function findBreakpoints(blocks) {
const breakpoints = [];
let length = 0;
2020-06-30 11:24:23 +00:00
for (let j = 0; j < blocks.length; j++) {
const {text} = blocks[j];
for (let i = 0; i < text.length; i++) {
2020-06-30 11:24:23 +00:00
const char = text.charAt(i);
2020-06-30 11:24:23 +00:00
if (char === ' ') {
breakpoints.push({ end: length + i, next: length + i + 1 });
} else if (i !== text.length - 1 && (char === '-' || char === '?')) {
breakpoints.push({ end: length + i + 1, next: length + i + 1 });
}
}
2020-06-30 11:24:23 +00:00
length += text.length;
}
return [breakpoints, length];
}
2018-04-05 23:46:22 +00:00
export function messageHeight(
message,
wrapWidth,
charWidth,
indent = 0,
windowWidth
) {
2016-02-16 21:43:25 +00:00
let pad = (6 + (message.from ? message.from.length + 1 : 0)) * charWidth;
let height = lineHeight + 8;
if (message.channel && windowWidth > smallScreen) {
wrapWidth -= userListWidth;
2016-02-16 21:43:25 +00:00
}
2018-04-05 23:46:22 +00:00
if (pad + message.length * charWidth < wrapWidth) {
2016-02-16 21:43:25 +00:00
return height;
}
const breaks = message.breakpoints;
2016-02-16 21:43:25 +00:00
let prevBreak = 0;
let prevPos = 0;
for (let i = 0; i < breaks.length; i++) {
2018-04-05 23:46:22 +00:00
if (pad + (breaks[i].end - prevBreak) * charWidth >= wrapWidth) {
prevBreak = prevPos;
pad = indent;
height += lineHeight;
}
2016-02-16 21:43:25 +00:00
prevPos = breaks[i].next;
}
2016-02-16 21:43:25 +00:00
2018-04-05 23:46:22 +00:00
if (pad + (message.length - prevBreak) * charWidth >= wrapWidth) {
height += lineHeight;
2016-02-16 21:43:25 +00:00
}
return height;
}