-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[lexical-list] Bullet item color matches text color #7024
base: main
Are you sure you want to change the base?
Changes from all commits
7080a79
af615a0
6b7f32c
5869d83
5622672
f9f3cc2
e8c3774
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,15 @@ import type {LexicalCommand, LexicalEditor} from 'lexical'; | |
|
||
import {mergeRegister} from '@lexical/utils'; | ||
import { | ||
$getSelection, | ||
$isRangeSelection, | ||
$isTextNode, | ||
COMMAND_PRIORITY_EDITOR, | ||
COMMAND_PRIORITY_LOW, | ||
createCommand, | ||
INSERT_PARAGRAPH_COMMAND, | ||
SELECTION_CHANGE_COMMAND, | ||
TextNode, | ||
} from 'lexical'; | ||
|
||
import { | ||
|
@@ -58,6 +64,21 @@ export const REMOVE_LIST_COMMAND: LexicalCommand<void> = createCommand( | |
'REMOVE_LIST_COMMAND', | ||
); | ||
|
||
function $checkSelectionListener(): boolean { | ||
const selection = $getSelection(); | ||
if ($isRangeSelection(selection) && selection.isCollapsed()) { | ||
const node = selection.anchor.getNode(); | ||
if ( | ||
$isListItemNode(node) && | ||
node.isEmpty() && | ||
selection.style !== node.getStyle() | ||
) { | ||
node.setStyle(selection.style); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
export function registerList(editor: LexicalEditor): () => void { | ||
const removeListener = mergeRegister( | ||
editor.registerCommand( | ||
|
@@ -97,6 +118,29 @@ export function registerList(editor: LexicalEditor): () => void { | |
}, | ||
COMMAND_PRIORITY_LOW, | ||
), | ||
editor.registerCommand( | ||
SELECTION_CHANGE_COMMAND, | ||
$checkSelectionListener, | ||
COMMAND_PRIORITY_EDITOR, | ||
), | ||
Comment on lines
+121
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This updates the style when a new ListItemNode is created based on the selection |
||
editor.registerNodeTransform(ListItemNode, (node) => { | ||
const firstChild = node.getFirstChild(); | ||
if (firstChild && $isTextNode(firstChild)) { | ||
const style = firstChild.getStyle(); | ||
if (node.getStyle() !== style) { | ||
node.setStyle(style); | ||
} | ||
} | ||
}), | ||
editor.registerNodeTransform(TextNode, (node) => { | ||
const listItemParentNode = node.getParent(); | ||
if ( | ||
$isListItemNode(listItemParentNode) && | ||
node.is(listItemParentNode.getFirstChild()) | ||
) { | ||
listItemParentNode.markDirty(); | ||
} | ||
}), | ||
Comment on lines
+126
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we are only looking at the first child to propagate the style to the parent ListItemNode. It could probably be generalized a bit to work with element nodes too, I was just following your template here. |
||
); | ||
return removeListener; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import { | |
$isTextNode, | ||
$isTokenOrSegmented, | ||
BaseSelection, | ||
ElementNode, | ||
LexicalEditor, | ||
LexicalNode, | ||
Point, | ||
|
@@ -241,7 +242,7 @@ export function $addNodeStyle(node: TextNode): void { | |
} | ||
|
||
function $patchStyle( | ||
target: TextNode | RangeSelection, | ||
target: TextNode | RangeSelection | ElementNode, | ||
patch: Record< | ||
string, | ||
| string | ||
|
@@ -263,7 +264,7 @@ function $patchStyle( | |
} | ||
return styles; | ||
}, | ||
{...prevStyles} || {}, | ||
{...prevStyles}, | ||
); | ||
const newCSSText = getCSSFromStyleObject(newStyles); | ||
target.setStyle(newCSSText); | ||
|
@@ -285,12 +286,16 @@ export function $patchStyleText( | |
| null | ||
| (( | ||
currentStyleValue: string | null, | ||
target: TextNode | RangeSelection, | ||
target: TextNode | RangeSelection | ElementNode, | ||
) => string) | ||
>, | ||
): void { | ||
if (selection.isCollapsed() && $isRangeSelection(selection)) { | ||
$patchStyle(selection, patch); | ||
const emptyNode = selection.anchor.getNode(); | ||
if ($isElementNode(emptyNode) && emptyNode.isEmpty()) { | ||
$patchStyle(emptyNode, patch); | ||
} | ||
Comment on lines
+295
to
+298
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the more fundamental fix, we want to patch an empty element's style when the selection is collapsed on it or else there will be a disagreement of which style is correct since LexicalEvents has the selection's style follow the node's |
||
} else { | ||
$forEachSelectedTextNode((textNode) => { | ||
$patchStyle(textNode, patch); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -337,31 +337,27 @@ function onSelectionChange( | |
anchor.offset === lastOffset && | ||
anchor.key === lastKey | ||
) { | ||
selection.format = lastFormat; | ||
selection.style = lastStyle; | ||
$updateSelectionFormatStyle(selection, lastFormat, lastStyle); | ||
} else { | ||
if (anchor.type === 'text') { | ||
invariant( | ||
$isTextNode(anchorNode), | ||
'Point.getNode() must return TextNode when type is text', | ||
); | ||
selection.format = anchorNode.getFormat(); | ||
selection.style = anchorNode.getStyle(); | ||
$updateSelectionFormatStyleFromNode(selection, anchorNode); | ||
} else if (anchor.type === 'element' && !isRootTextContentEmpty) { | ||
invariant( | ||
$isElementNode(anchorNode), | ||
'Point.getNode() must return ElementNode when type is element', | ||
); | ||
const lastNode = anchor.getNode(); | ||
selection.style = ''; | ||
if ( | ||
// This previously applied to all ParagraphNode | ||
lastNode.isEmpty() | ||
) { | ||
selection.format = lastNode.getTextFormat(); | ||
selection.style = lastNode.getTextStyle(); | ||
$updateSelectionFormatStyleFromNode(selection, lastNode); | ||
} else { | ||
selection.format = 0; | ||
$updateSelectionFormatStyle(selection, 0, ''); | ||
} | ||
} | ||
} | ||
|
@@ -411,6 +407,27 @@ function onSelectionChange( | |
}); | ||
} | ||
|
||
function $updateSelectionFormatStyle( | ||
selection: RangeSelection, | ||
format: number, | ||
style: string, | ||
) { | ||
if (selection.format !== format || selection.style !== style) { | ||
selection.format = format; | ||
selection.style = style; | ||
selection.dirty = true; | ||
} | ||
} | ||
Comment on lines
+410
to
+420
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was just a clean up to make sure that everything is set properly and marked dirty as appropriate |
||
|
||
function $updateSelectionFormatStyleFromNode( | ||
selection: RangeSelection, | ||
node: TextNode | ElementNode, | ||
) { | ||
const format = node.getFormat(); | ||
const style = node.getStyle(); | ||
$updateSelectionFormatStyle(selection, format, style); | ||
} | ||
|
||
// This is a work-around is mainly Chrome specific bug where if you select | ||
// the contents of an empty block, you cannot easily unselect anything. | ||
// This results in a tiny selection box that looks buggy/broken. This can | ||
|
@@ -578,12 +595,11 @@ function onBeforeInput(event: InputEvent, editor: LexicalEditor): void { | |
if ($isRangeSelection(selection)) { | ||
const anchorNode = selection.anchor.getNode(); | ||
anchorNode.markDirty(); | ||
selection.format = anchorNode.getFormat(); | ||
invariant( | ||
$isTextNode(anchorNode), | ||
'Anchor node must be a TextNode', | ||
); | ||
selection.style = anchorNode.getStyle(); | ||
$updateSelectionFormatStyleFromNode(selection, anchorNode); | ||
} | ||
} else { | ||
$setCompositionKey(null); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the element's style attribute in createDOM and updateDOM was part of the fix here