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 {