plate icon indicating copy to clipboard operation
plate copied to clipboard

Alignment Issue : When whole text is selected it always shows left aligned

Open kumarajay0412 opened this issue 2 months ago • 9 comments

Description

Alignment Issue : When whole text is selected it always shows left aligned

Steps to Reproduce

go to https://platejs.org/ and try selecting whole sentence which is center aligned and when you select all it shows left aligned

https://github.com/udecode/plate/assets/66991625/599a1d9d-0f1f-420b-aeb9-dbe951a7e6af

Funding

  • You can sponsor this specific effort via a Polar.sh pledge below
  • We receive the pledge once the issue is completed & verified
Fund with Polar

kumarajay0412 avatar May 06 '24 06:05 kumarajay0412

I think this issue is caused by isCollapsed function which is using range.isCollapsed from slate

  isCollapsed(range: Range): boolean {
    const { anchor, focus } = range
    return Point.equals(anchor, focus)
  },

the problem is arising because our anchor and focus are equal but we have different offsets which is making isCollapsed false

kumarajay0412 avatar May 06 '24 07:05 kumarajay0412

Yes, for the sake of reducing the complexity of the work, we haven't considered the expanded situation temporarily.

felixfeng33 avatar May 06 '24 08:05 felixfeng33

@felixfeng33 can i create a PR for this fix by adding below function

const isCollapsedInternalFunction = (range?: Range | null) => {

  // Early return for null or undefined range
  if (!range ||!range.anchor ||!range.focus) {
    return false;
  }

  // Destructure the anchor and focus properties for easier access
  const { anchor, focus } = range;
  const { anchor, focus } = range;

  // Check if both anchor and focus exist and have the same path coordinates
  if (anchor && focus && anchor.path.length > 0 && focus.path.length > 0) {
    return anchor.path[0] === focus.path[0] && anchor.path[1] === focus.path[1];
  }
};

kumarajay0412 avatar May 06 '24 09:05 kumarajay0412

I think you can check the parent node props in the else statement.

felixfeng33 avatar May 06 '24 10:05 felixfeng33

In your solution could I selection cross block? image

felixfeng33 avatar May 06 '24 10:05 felixfeng33

yes you can do cross selection in blocks as alignment is always block level

I have a question if a selection has first block center next block left what should be the overall alignment to be shown on top in mostly cases we show first block selection details can we do the same here

kumarajay0412 avatar May 06 '24 11:05 kumarajay0412

It's better the overall alignment is none.

felixfeng33 avatar May 06 '24 11:05 felixfeng33

I think we had better do some thing here since it's have more readability· if (isCollapsed(editor.selection)) { }else { do some check here }

felixfeng33 avatar May 06 '24 11:05 felixfeng33

We should check the value of all selected blocks using getNodeEntries, and return true only if every block has the same value. One different value makes it false (like marks).

zbeyens avatar May 06 '24 12:05 zbeyens

Hey @zbeyens pls look into the PR that i opened regarding above bug

kumarajay0412 avatar May 20 '24 21:05 kumarajay0412