Skip to content

Commit

Permalink
Fix issues related to multiple newlines being created
Browse files Browse the repository at this point in the history
  • Loading branch information
Eroxl committed Jan 21, 2023
1 parent 44024ed commit 2414261
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions web/src/components/blocks/MathBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,6 @@ const MathBlock = (props: EditableText) => {
setCurrentValue(value);
}, [value]);

useEffect(() => {
const handleClickOutside = (e: MouseEvent) => {
const target = e.target as HTMLElement;

if (!isEditing || !e.target) return;

if (target.id === blockID || target.id === `preview-${blockID}`) return;


setIsEditing(false);
editBlock([blockID], 'math', { value: currentValue }, page);
};

document.addEventListener('click', handleClickOutside);

return () => {
document.removeEventListener('click', handleClickOutside);
};
}, [isEditing, blockID]);

const switchToEditing = () => {
setIsEditing(true);

Expand All @@ -54,7 +34,7 @@ const MathBlock = (props: EditableText) => {
const range = document.createRange();
const sel = window.getSelection();

range.setStart(element.childNodes[0], element.textContent?.length || 0);
range.setStart(element.childNodes[0] || element, element.textContent?.length || 0);
range.collapse(true);

sel?.removeAllRanges();
Expand All @@ -67,7 +47,7 @@ const MathBlock = (props: EditableText) => {

return (
<div
className="min-h-[1.2em] w-full"
className="min-h-[1.2em] w-full h-full flex items-center justify-center"
onClick={(isEditing || !currentValue) ? undefined : switchToEditing}
id={`${blockID}-container`}
role={(isEditing || !currentValue) ? 'textbox' : 'button'}
Expand All @@ -76,7 +56,7 @@ const MathBlock = (props: EditableText) => {
{isEditing
? (
<span
className="min-h-[1.2em] outline-none whitespace-pre-wrap w-full flex"
className="min-h-[1.2em] outline-none whitespace-pre-wrap w-full"
role="textbox"
tabIndex={0}
contentEditable
Expand All @@ -96,7 +76,10 @@ const MathBlock = (props: EditableText) => {
e.preventDefault();
e.currentTarget.blur();
addBlockAtIndex(index + 1, page, pageData, setPageData);
} else if (e.code === 'Backspace' && (e.currentTarget.innerText === '' || e.currentTarget.innerText === '\n')) {
} else if (e.code === 'Backspace' && window.getSelection()?.anchorOffset === 0) {
setCurrentBlockType('text');
editBlock([blockID], 'text', undefined, page);
} else if (e.code === 'Backspace' && e.currentTarget.innerText === '') {
e.preventDefault();
e.currentTarget.blur();
setCurrentBlockType('text');
Expand All @@ -108,7 +91,15 @@ const MathBlock = (props: EditableText) => {
navigator.clipboard.writeText(`$$ ${window.getSelection()?.toString() || ''}`);
}}
>
{currentValue}
{
(() => {
if (currentValue !== '\n') {
return (
currentValue
);
}
})()
}
</span>
)
: (
Expand Down

0 comments on commit 2414261

Please sign in to comment.