fix #253: trim long text without space (#256)

This commit is contained in:
lenhanphung
2025-10-27 17:53:01 +07:00
committed by GitHub
parent cdaed8a7c1
commit 2355ee8771
3 changed files with 50 additions and 9 deletions
+25 -8
View File
@@ -4,7 +4,7 @@ import AccordionSummary from "@mui/material/AccordionSummary";
import Typography from "@mui/material/Typography"; import Typography from "@mui/material/Typography";
import { useAppDispatch, useAppSelector } from "../../app/hooks"; import { useAppDispatch, useAppSelector } from "../../app/hooks";
import AddIcon from "@mui/icons-material/Add"; import AddIcon from "@mui/icons-material/Add";
import { useState } from "react"; import { useState, useMemo } from "react";
import CalendarPopover from "./CalendarModal"; import CalendarPopover from "./CalendarModal";
import { Calendars } from "../../features/Calendars/CalendarTypes"; import { Calendars } from "../../features/Calendars/CalendarTypes";
import MoreVertIcon from "@mui/icons-material/MoreVert"; import MoreVertIcon from "@mui/icons-material/MoreVert";
@@ -15,6 +15,7 @@ import CalendarSearch from "./CalendarSearch";
import { Divider, ListItem, Menu, MenuItem } from "@mui/material"; import { Divider, ListItem, Menu, MenuItem } from "@mui/material";
import { removeCalendarAsync } from "../../features/Calendars/CalendarSlice"; import { removeCalendarAsync } from "../../features/Calendars/CalendarSlice";
import { DeleteCalendarDialog } from "./DeleteCalendarDialog"; import { DeleteCalendarDialog } from "./DeleteCalendarDialog";
import { trimLongTextWithoutSpace } from "../../utils/textUtils";
function CalendarAccordion({ function CalendarAccordion({
title, title,
@@ -223,6 +224,11 @@ function CalendarSelector({
setDeletePopupOpen(false); setDeletePopupOpen(false);
handleClose(); handleClose();
}; };
const trimmedName = useMemo(
() => trimLongTextWithoutSpace(calendars[id].name),
[calendars, id]
);
return ( return (
<> <>
<ListItem <ListItem
@@ -243,7 +249,14 @@ function CalendarSelector({
}, },
}} }}
> >
<label> <label
style={{
display: "flex",
alignItems: "center",
maxWidth: "calc(100% - 40px)",
overflow: "hidden",
}}
>
<Checkbox <Checkbox
sx={{ sx={{
color: calendars[id].color?.light, color: calendars[id].color?.light,
@@ -253,13 +266,17 @@ function CalendarSelector({
checked={selectedCalendars.includes(id)} checked={selectedCalendars.includes(id)}
onChange={() => handleCalendarToggle(id)} onChange={() => handleCalendarToggle(id)}
/> />
{calendars[id].name} <span
style={{
overflow: "hidden",
textOverflow: "ellipsis",
wordBreak: "break-word",
}}
>
{trimmedName}
</span>
</label> </label>
<IconButton <IconButton className="MoreBtn" onClick={handleClick}>
className="MoreBtn"
onClick={handleClick}
style={{ alignSelf: "end" }}
>
<MoreVertIcon /> <MoreVertIcon />
</IconButton> </IconButton>
</ListItem> </ListItem>
+3 -1
View File
@@ -655,7 +655,9 @@ export default function EventPreviewModal({
height: 12, height: 12,
}} }}
/> />
<Typography>{calendar.name}</Typography> <Typography sx={{ wordBreak: "break-word" }}>
{calendar.name}
</Typography>
</Box> </Box>
</ResponsiveDialog> </ResponsiveDialog>
<EditModeDialog <EditModeDialog
+22
View File
@@ -0,0 +1,22 @@
export function trimLongTextWithoutSpace(text: string): string {
const words = text.split(/\s+/);
const hasLongWord = words.some((word) => word.length > 25);
if (!hasLongWord) {
return text;
}
let result = "";
for (const word of words) {
if (word.length > 25) {
const remaining = 20 - result.length;
result += word.substring(0, Math.max(remaining - 3, 5)) + "...";
break;
} else {
result += (result ? " " : "") + word;
}
}
return result;
}