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
|
|
|
|
2020-06-03 01:04:38 +00:00
|
|
|
function findBreakpointsString(text, breakpoints, index) {
|
2017-04-17 02:11:45 +00:00
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
|
|
const char = text.charAt(i);
|
|
|
|
|
|
|
|
if (char === ' ') {
|
2020-06-03 01:04:38 +00:00
|
|
|
breakpoints.push({ end: i + index, next: i + 1 + index });
|
|
|
|
} else if (i !== text.length - 1 && (char === '-' || char === '?')) {
|
|
|
|
breakpoints.push({ end: i + 1 + index, next: i + 1 + index });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findBreakpoints(text) {
|
|
|
|
const breakpoints = [];
|
|
|
|
let length = 0;
|
|
|
|
|
|
|
|
if (typeof text === 'string') {
|
|
|
|
findBreakpointsString(text, breakpoints, length);
|
|
|
|
length = text.length;
|
|
|
|
} else if (Array.isArray(text)) {
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
|
|
const node = text[i];
|
|
|
|
|
|
|
|
if (typeof node === 'string') {
|
|
|
|
findBreakpointsString(node, breakpoints, length);
|
|
|
|
length += node.length;
|
|
|
|
} else {
|
|
|
|
findBreakpointsString(node.props.children, breakpoints, length);
|
|
|
|
length += node.props.children.length;
|
|
|
|
}
|
2017-04-17 02:11:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 01:04:38 +00:00
|
|
|
return [breakpoints, length];
|
2017-04-17 02:11:45 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|