Skip to content

Commit

Permalink
Fixes bug in setTitle where the title for another chapter would change.
Browse files Browse the repository at this point in the history
The issue is in onBlur. I *think* the issue is similar to
lovasoa/react-contenteditable#161

where an outdated version is firing with the wrong chapterid. Bug can be
repro-d by something like:
1. go to chapter A.
2. click into the title and copy.
3. don't blur, just go to another chapter.
This should then cause a blur and cause a bug.

If you save using Enter or cmd+s, this seems to not happen.
  • Loading branch information
egonSchiele committed Aug 19, 2023
1 parent 7b60be6 commit 58d9bdc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,10 @@ export default function Editor({ settings }: { settings: t.UserSettings }) {
<ContentEditable
value={currentChapterTitle}
className={`${titleFontSize} mb-md tracking-wide font-normal text-darkest dark:text-lightest mx-auto text-center w-full mt-lg ${fontClass}`}
/* // This is needed so the first block gets focus when we hit enter
// This is needed so the first block gets focus when we hit enter
onClick={() => {
dispatch(librarySlice.actions.setActiveTextIndex(-1));
}} */
}}
onSubmit={(title) => {
dispatch(
librarySlice.actions.setTitle({
Expand Down
48 changes: 32 additions & 16 deletions src/components/ContentEditable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function ContentEditable({
onClick = () => {},
}) {
const [content, setContent] = React.useState(value);

const [edited, setEdited] = React.useState(false);
const handleChange = (evt) => {
const value = evt.target.innerHTML.replace(/<br>|&nbsp;/g, " ").trim();
setContent(value);
Expand Down Expand Up @@ -42,32 +42,48 @@ export default function ContentEditable({
}, [div.current]);

const onKeyDown = (evt) => {
if ((evt.metaKey && evt.code === "KeyS") || evt.key === "Enter") {
if (
(evt.metaKey && evt.code === "KeyS") ||
evt.key === "Enter" ||
evt.key === "Tab"
) {
if (document.activeElement === div.current) {
evt.preventDefault();
setEdited(false);
console.warn("submitting", content);
onSubmit(content);
if (nextFocus) {
nextFocus();
}
}
} else {
setEdited(true);
}
};

return (
<div
className={`${DEFAULT_CLASSES} ${className}`}
contentEditable
suppressContentEditableWarning
onBlur={handleSubmit}
onKeyDown={onKeyDown}
style={style}
onInput={handleChange}
data-selector={selector}
onClick={onClick}
ref={div}
>
{value}
</div>
<>
<div className={` relative ${className}`}>
<div
className={` ${DEFAULT_CLASSES} ${className}`}
contentEditable
suppressContentEditableWarning
/* onBlur={handleSubmit} */
onKeyDown={onKeyDown}
style={style}
onInput={handleChange}
data-selector={selector}
onClick={onClick}
ref={div}
>
{value}
</div>
{edited && (
<span className="mx-none text-xs text-gray-500 absolute top-0 right-0 uppercase">
Enter to save
</span>
)}
</div>
</>
);
}

0 comments on commit 58d9bdc

Please sign in to comment.