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

feat: notes | make checkboxes clickable #536

Merged
merged 9 commits into from
Dec 10, 2023
61 changes: 57 additions & 4 deletions src/components/Common/MarkdownEditor/MarkdownEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,51 @@ import {
import rehypeSanitize from "rehype-sanitize";
import "./MarkdownEditor.css";

const compareStrings = (str1, str2) => {
for (let i = 0, j = 0; i < str1.length || j < str2.length; i++, j++) {
if (i >= str1.length) return false;
if (j >= str2.length) return false;
if (str1[i] !== str2[j]) return false;
}
return true;
};
const MarkdownEditor = ({ content, label, previewModeOnly, onCopyChanges }) => {
const [value, setValue] = useState();
const [value, setValue] = useState("");
useEffect(() => {
setValue(content);
}, [content, label]);

if (previewModeOnly)
return <MDEditor.Markdown source={value} style={{ whiteSpace: "normal", backgroundColor: "#000" }} />;

if (previewModeOnly) {
return (
<MDEditor.Markdown
source={value}
style={{ whiteSpace: "normal", backgroundColor: "#000" }}
components={{
input: (props) => {
return <input {...props} disabled={true} />;
},
}}
/>
);
}
const handleChange = (value) => {
setValue(value);
onCopyChanges(label, value);
};

const handleCheckBoxChange = (e) => {
const textOfCheckBox = e.target.parentNode.textContent;
const valueListOfLines = value.split("\n");
const findCheckedBoxLineIndex = valueListOfLines.findIndex((item) => {
console.log(item?.replace(/- \[ \]|- \[[^]]+/, ""), textOfCheckBox.split("\n")[0]);
return compareStrings(item?.replace(/- \[ \]|- \[[^]]+/, ""), textOfCheckBox.split("\n")[0]);
});
valueListOfLines[findCheckedBoxLineIndex] = valueListOfLines[findCheckedBoxLineIndex].replace(
/- \[ \]|- \[[^]]+/,
(match) => (match === "- [ ]" ? "- [X]" : "- [ ]"),
);
handleChange(valueListOfLines.join("\n"));
};
return (
<MarkdownContainer>
<MarkdownLabel>{label}</MarkdownLabel>
Expand All @@ -33,6 +65,27 @@ const MarkdownEditor = ({ content, label, previewModeOnly, onCopyChanges }) => {
paddingLeft: "5px",
paddingRight: "5px",
}}
components={{
input: (props) => {
return (
<input
{...props}
disabled={false}
onChange={(e) => {
if (props.type === "checkbox") {
const isChecked = e.target.hasAttribute("checked");
handleCheckBoxChange(e);
if (isChecked) {
e.target.removeAttribute("checked");
} else {
e.target.setAttribute("checked", "checked");
}
}
}}
/>
);
},
}}
/>
</MarkdownEditorPreviewContainer>
<MarkdownEditorContainer>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Dashboard/Notetaker/NoteDescription.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const NoteDescription = ({ children, onPin, needToAdd, onCloseAddMode, onChangeP
};
const handleClose = () => {
if (needToEdit) {
setNeedToEdit(false);
setShowNote(children);
setNeedToEdit(false);
return;
}
onCloseAddMode(false);
Expand Down