Skip to content
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

Fix getting style via $getSelectionStyleValueForProperty when selection is collapsed #5082

Merged

Conversation

Piliuta
Copy link
Contributor

@Piliuta Piliuta commented Oct 4, 2023

Fixes #4491

Note: This PR focuses on a fix inside $getSelectionStyleValueForProperty and not on the RangeSelection style property (which I believe should be a long-term fix).

Context:

style property on the RangeSelection was implemented in #3863

however, it was not implemented for the case when selection is not collapsed

if (selection.isCollapsed()) {
// Badly interpreted range selection when collapsed - #1482
if (
domSelection.type === 'Range' &&
domSelection.anchorNode === domSelection.focusNode
) {
selection.dirty = true;
}
// If we have marked a collapsed selection format, and we're
// within the given time range – then attempt to use that format
// instead of getting the format from the anchor node.
const windowEvent = getWindow(editor).event;
const currentTimeStamp = windowEvent
? windowEvent.timeStamp
: performance.now();
const [lastFormat, lastStyle, lastOffset, lastKey, timeStamp] =
collapsedSelectionFormat;
if (
currentTimeStamp < timeStamp + 200 &&
anchor.offset === lastOffset &&
anchor.key === lastKey
) {
selection.format = lastFormat;
selection.style = lastStyle;
} else {
if (anchor.type === 'text') {
selection.format = anchorNode.getFormat();
selection.style = anchorNode.getStyle();
} else if (anchor.type === 'element') {
selection.format = 0;
selection.style = '';
}
}
} else {
let combinedFormat = IS_ALL_FORMATTING;
let hasTextNodes = false;
const nodes = selection.getNodes();
const nodesLength = nodes.length;
for (let i = 0; i < nodesLength; i++) {
const node = nodes[i];
if ($isTextNode(node)) {
// TODO: what about style?

Since $getSelectionStyleValueForProperty relies on this property, it does not return the correct value in cases when the selection is not collapsed.

Problem

This method is often used for reflecting current values on toolbars, but it does not work in half (range selection) cases.

@vercel
Copy link

vercel bot commented Oct 4, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 4, 2023 11:56am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 4, 2023 11:56am

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 4, 2023
Copy link
Contributor

@acywatson acywatson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! Sorry I missed all the comments on that issue.

@acywatson acywatson merged commit 816ef98 into facebook:main Oct 4, 2023
42 of 43 checks passed
@Piliuta Piliuta deleted the fix-getting-selection-style-when-collapsed branch October 4, 2023 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: $getSelectionStyleValueForProperty(selection, 'font-size') not updating correctly
3 participants