From aff4b20ec832f9011cf8a9e423265604f33546bf Mon Sep 17 00:00:00 2001 From: keumky2 Date: Tue, 28 Nov 2023 01:37:07 +0900 Subject: [PATCH 1/2] keep selection style when clicking empty editor --- packages/lexical/src/LexicalEvents.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/lexical/src/LexicalEvents.ts b/packages/lexical/src/LexicalEvents.ts index 36c2bfed985..4bad89fbdf0 100644 --- a/packages/lexical/src/LexicalEvents.ts +++ b/packages/lexical/src/LexicalEvents.ts @@ -11,6 +11,7 @@ import type {NodeKey} from './LexicalNode'; import type {ElementNode} from './nodes/LexicalElementNode'; import type {TextNode} from './nodes/LexicalTextNode'; +import {$isRootTextContentEmpty} from '@lexical/text'; import { CAN_USE_BEFORE_INPUT, IS_ANDROID, @@ -334,7 +335,10 @@ function onSelectionChange( if (anchor.type === 'text') { selection.format = anchorNode.getFormat(); selection.style = anchorNode.getStyle(); - } else if (anchor.type === 'element') { + } else if ( + anchor.type === 'element' && + !$isRootTextContentEmpty(editor.isComposing(), false) + ) { selection.format = 0; selection.style = ''; } From ae305d69d6a77503a430c14222c72668ea7300fd Mon Sep 17 00:00:00 2001 From: keumky2 Date: Mon, 4 Dec 2023 23:47:06 +0900 Subject: [PATCH 2/2] refactor: eliminate @lexical/text dependency in lexicalEvent.ts --- packages/lexical/src/LexicalEvents.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/lexical/src/LexicalEvents.ts b/packages/lexical/src/LexicalEvents.ts index 4bad89fbdf0..104576ddbf3 100644 --- a/packages/lexical/src/LexicalEvents.ts +++ b/packages/lexical/src/LexicalEvents.ts @@ -11,7 +11,6 @@ import type {NodeKey} from './LexicalNode'; import type {ElementNode} from './nodes/LexicalElementNode'; import type {TextNode} from './nodes/LexicalTextNode'; -import {$isRootTextContentEmpty} from '@lexical/text'; import { CAN_USE_BEFORE_INPUT, IS_ANDROID, @@ -324,6 +323,10 @@ function onSelectionChange( const [lastFormat, lastStyle, lastOffset, lastKey, timeStamp] = collapsedSelectionFormat; + const root = $getRoot(); + const isRootTextContentEmpty = + editor.isComposing() === false && root.getTextContent() === ''; + if ( currentTimeStamp < timeStamp + 200 && anchor.offset === lastOffset && @@ -335,10 +338,7 @@ function onSelectionChange( if (anchor.type === 'text') { selection.format = anchorNode.getFormat(); selection.style = anchorNode.getStyle(); - } else if ( - anchor.type === 'element' && - !$isRootTextContentEmpty(editor.isComposing(), false) - ) { + } else if (anchor.type === 'element' && !isRootTextContentEmpty) { selection.format = 0; selection.style = ''; }