2016-02-16 21:43:25 +00:00
|
|
|
const lineHeight = 24;
|
2017-05-07 20:19:15 +00:00
|
|
|
const userListWidth = 200;
|
|
|
|
const smallScreen = 600;
|
2017-05-12 08:51:37 +00:00
|
|
|
|
2017-04-17 02:11:45 +00:00
|
|
|
export function findBreakpoints(text) {
|
|
|
|
const breakpoints = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
|
|
const char = text.charAt(i);
|
|
|
|
|
|
|
|
if (char === ' ') {
|
|
|
|
breakpoints.push({ end: i, next: i + 1 });
|
|
|
|
} else if (char === '-' && i !== text.length - 1) {
|
|
|
|
breakpoints.push({ end: i + 1, next: i + 1 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return breakpoints;
|
|
|
|
}
|
|
|
|
|
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;
|
2017-05-07 20:19:15 +00:00
|
|
|
let height = lineHeight + 8;
|
2017-03-29 21:45:09 +00:00
|
|
|
|
2017-05-07 20:19:15 +00:00
|
|
|
if (message.channel && windowWidth > smallScreen) {
|
2017-05-26 06:20:00 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-17 02:11:45 +00:00
|
|
|
const breaks = message.breakpoints;
|
2016-02-16 21:43:25 +00:00
|
|
|
let prevBreak = 0;
|
|
|
|
let prevPos = 0;
|
|
|
|
|
2017-04-17 02:11:45 +00:00
|
|
|
for (let i = 0; i < breaks.length; i++) {
|
2018-04-05 23:46:22 +00:00
|
|
|
if (pad + (breaks[i].end - prevBreak) * charWidth >= wrapWidth) {
|
2017-04-17 02:11:45 +00:00
|
|
|
prevBreak = prevPos;
|
|
|
|
pad = indent;
|
|
|
|
height += lineHeight;
|
|
|
|
}
|
2016-02-16 21:43:25 +00:00
|
|
|
|
2017-04-17 02:11:45 +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) {
|
2017-04-17 02:11:45 +00:00
|
|
|
height += lineHeight;
|
2016-02-16 21:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return height;
|
|
|
|
}
|