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

Is it possible to change the selection area initial position? #210

Closed
Polcsi opened this issue Sep 26, 2023 · 2 comments
Closed

Is it possible to change the selection area initial position? #210

Polcsi opened this issue Sep 26, 2023 · 2 comments
Labels
feature request New feature requested

Comments

@Polcsi
Copy link

Polcsi commented Sep 26, 2023

Feature request or a solution for this issue! Currently there is no feature in the library for this issue or I could not find one. I hope there is an optimal solution for this problem.

Issue

I would like to change the area-selection initial position inside of onBeforeStart event handler.

Description about issue:

  1. When the user moves the mouse over one item then there will displayed a button at the bottom left corner.
    image
  2. User will be able to copy the current item properties to the newly selected items by selecting a range.
    image
  3. And I want to change the area-selection start position to the center of the item when user starts copying.
    image

Desired solution

This is how I imagine the solution for this issue:

const onBeforeStart = ({ event }: SelectionEvent) => {
        if (isCopy.current === true) {
           // Change area selection initial position
          // ...
        }

        // Preventing select from right click, middle mouse or left click
        return allowedButtons.current.includes(event.buttons);
    };
@simonwep
Copy link
Owner

That's a really cool idea, will definitely look into it :)

@simonwep simonwep added the feature request New feature requested label Sep 27, 2023
@simonwep
Copy link
Owner

simonwep commented Jan 16, 2025

Hey ho! It's been a while, but this is now possible with the following functions:

  • selection.getSelectables() - to get a list of all elements that are currently selectable.
  • selection.setAreaLocation({...}) - to adjust the location at any time.

Translated into code this may look like something like this:

selection
    .on('start', (evt) => {
        // There may not be an event if the selection was triggered programmatically
        const startElement = evt.event?.target;

        // Make sure the target is a selectable element and the user started the
        // selection inside an selectable element, and not just the boundaries
        if (startElement instanceof HTMLElement && selection.getSelectables().includes(startElement)) {
            const startElementRect = startElement.getBoundingClientRect();

            // "attach" the selection area to the top left corner of the element
            selection.setAreaLocation({
                x1: startElementRect.x,
                y1: startElementRect.y
            });
        }
    })

Which would result in something like this:

Note

Keep in mind that translating the area also may change the selection itself if you move it too much, in the gif below it is exact at the corner, keeping the source element still selected at any time.

Image

The feature will be available starting with v3.9.0 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature requested
Projects
None yet
Development

No branches or pull requests

2 participants