Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mod/library card textarea #41

Merged
merged 5 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ const LibraryCard = (props) => {
style={contentContainerStyle}
>
<ContentTextarea
className="library-card-textarea"
isSelected={isSelected}
lib={true}
className={`library-card-textarea ${isSelected ? "selected" : ""}`}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you pull out the code from ContentTextarea into LibraryCard? (And delete ContentTextarea).

value={cardText}
saveValue={(v) => dispatch(actions.updCardText(cardId, v))}
setEditingParent={setEditingCard}
Expand Down
23 changes: 18 additions & 5 deletions src/components/Library/LibrarySearch/LibraryCard/LibraryCard.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,23 @@ $round-radius: 12px;
display: flex;
flex-flow: column nowrap;

textarea {
cursor: auto;
}
.library-card-textarea {
flex: 1 1 auto;
box-sizing: border-box;
width: 100%;
display: block; /* Fallback for non-webkit */
display: -webkit-box;

max-width: 100%;
margin: 0 auto;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;

margin: 0;
padding: 15px 0px 15px 15px;
padding: 15px 8px 15px 15px;
border: 0;
border-radius: $round-radius;
overflow-y: auto;
resize: none;

background-color: white;
Expand All @@ -82,6 +89,12 @@ $round-radius: 12px;
border-radius: $round-radius;
}
}

.library-card-textarea.selected {
flex: 1 1 auto;
overflow-y: auto;
-webkit-line-clamp: inherit;
}
}

@keyframes library-card-blink {
Expand Down
60 changes: 2 additions & 58 deletions src/components/UI/Inputs/ContentTextarea.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useRef } from "react";
import React, { useState, useEffect } from "react";

import { CARD_FONT_SIZE } from "../../../shared/constants/fontSize";

Expand All @@ -9,16 +9,12 @@ import { CARD_FONT_SIZE } from "../../../shared/constants/fontSize";
const ContentTextarea = ({
className,
styles,
isSelected,
lib = false,
value,
saveValue,
setEditingParent,
}) => {
const [textareaValue, setTextareaValue] = useState("");
const [editing, setEditing] = useState(false);
const [textSlice, setSlice] = useState(textareaValue);
const textareaRef = useRef(null);

useEffect(() => {
setTextareaValue(value);
Expand Down Expand Up @@ -60,60 +56,8 @@ const ContentTextarea = ({
}
};

//slice content
//if text content is greater than 300 char
//slice + ...
let maxLines = lib && !isSelected ? 4 : 1000;

useEffect(() => {
if (textareaRef.current) {
// const lineHeight = parseFloat(
// window.getComputedStyle(textareaRef.current).lineHeight
// );
const lineHeight = 20;
const maxHeight = lineHeight * maxLines;

// Split the text into words
const words = textareaValue.split(" ");

// Estimate line breaks based on word wrapping
let currentLine = [];
const lines = [currentLine];
let currentLineHeight = 20;

for (const word of words) {
const wordWidth = word.length * lineHeight; // Estimate word width based on characters
if (currentLineHeight + wordWidth <= maxHeight) {
currentLine.push(word);
currentLineHeight += wordWidth;
} else {
currentLine = [word];
lines.push(currentLine);
currentLineHeight = wordWidth;
}
}

// Truncate text if it exceeds the maximum height
if (lines.length > maxLines) {
lines.splice(maxLines);
}

const truncatedText =
lines.map((line) => line.join(" ")).join(" ") +
(lib && !isSelected ? "..." : "");

setSlice(truncatedText);
}
}, [textareaValue, maxLines]);

const slice =
textareaValue.length > 300 && lib
? textareaValue.slice(0, 300) + "..."
: textareaValue;

return (
<textarea
ref={textareaRef}
className={className}
onBlur={endEdit}
onChange={(e) => setTextareaValue(e.target.value)}
Expand All @@ -125,7 +69,7 @@ const ContentTextarea = ({
readOnly={!editing}
style={styles}
type="text"
value={textareaValue ? (isSelected ? textareaValue : textSlice) : ""}
value={textareaValue}
/>
);
};
Expand Down
Loading