From 0cc0d388b4c9aef942b53af0288d8241ade85297 Mon Sep 17 00:00:00 2001 From: Triston Armstrong Date: Fri, 11 Oct 2024 13:56:45 -0400 Subject: [PATCH] feat(text): fix text focus and text wrap previously the text elements werent relieving their focus up until actually clicking another element on the page. This now allows cilcking anywhere on the page regardless of element, relieving the focus and shifting focus to another text element if one is clicked. This also fixes the persistant text wrapping, replacing the
tags with chars and wrapping the text wth a pre tag to maintain all new lines --- src/components/TextItem.tsx | 55 +++++++++++++++++++++++++++++-------- src/styles.css | 3 ++ 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/components/TextItem.tsx b/src/components/TextItem.tsx index ca9b46a..a7078d4 100644 --- a/src/components/TextItem.tsx +++ b/src/components/TextItem.tsx @@ -101,8 +101,9 @@ export function TextItem({ key: itemKey, data: item }: TextItem.TextCardProps) { } function _handleContentInput(e: InputEvent) { - console.log("event: ", e) - texts.updateTextProperty(itemKey, 'contents', (e.target as any).textContent) + const el = e.target as HTMLParagraphElement & Pick + const val = el.innerHTML.split('
').join('\n') + texts.updateTextProperty(itemKey, 'contents', val) updateLocalStorage() } @@ -111,25 +112,57 @@ export function TextItem({ key: itemKey, data: item }: TextItem.TextCardProps) { pRef.current.textContent = item.contents }, []) + useEffect(() => { + function _handleClick(e: MouseEvent) { + const target = e.target as HTMLElement + + if (target === elRef.current) return + if (elRef.current?.contains(target)) return + + const classes = '.text-container, .text-p, .text-pre, .text-close' + if (target.closest(classes)) return + if (target.matches(classes)) return + + focusedItem.value = null + document.removeEventListener('mousedown', _handleClick) + } + + if (focusedItem.value !== itemKey) return + document.addEventListener('mousedown', _handleClick) + return () => { + document.removeEventListener('mousedown', _handleClick) + } + }, [focusedItem.value]) + + const outline = `${focusedItem.value === item.id ? '1px solid' : ''}` + const fontSize = `${item.fontSize / 6}px` + const zIndex = `${focusedItem.value == itemKey ? LayerEnum.CARD_ELEVATED : LayerEnum.CARD}` + return (
-
+
         

-
+ //@ts-expect-error + oninput={_handleContentInput} + contentEditable + className={'text-p inline-block px-2 w-full select-none drop-shadow relative'}> +

+ @@ -158,7 +191,7 @@ function CloseIcon({ item, cb }: CloseIcon.Props) { stroke-width="2" stroke-linecap="round" stroke-linejoin="round" - className="cursor-pointer w-4 h-4 absolute top-[-8px] right-[-8px]" + className="text-close cursor-pointer w-4 h-4 absolute top-[-8px] right-[-8px]" style={{ display: focusedItem.value === item.id ? 'unset' : 'none' }} diff --git a/src/styles.css b/src/styles.css index ef9368e..6373614 100644 --- a/src/styles.css +++ b/src/styles.css @@ -11,6 +11,9 @@ margin: 0; padding: 0; box-sizing: border-box; + :focus { + outline: none; + } } html {