Skip to content

paste outside existing grid boundaries #48

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

Open
joechrz opened this issue Aug 22, 2018 · 4 comments
Open

paste outside existing grid boundaries #48

joechrz opened this issue Aug 22, 2018 · 4 comments

Comments

@joechrz
Copy link
Collaborator

joechrz commented Aug 22, 2018

We're dealing with a use case where the grid columns are fixed (the "schema") but a user can add/remove/update any of the rows. One of the use-cases we want this to handle is the situation where someone kept a list of data in a spreadsheet somewhere and they want to paste it into the grid.

As is, it looks like the paste command doesn't allow pasting outside the col/row descriptor boundaries:
https://github.com/gridgrid/grid/blob/next/src/modules/copy-paste/index.ts#L167

Wanted to start an issue to discuss the best way to handle this and/or implement a way for additional row/column descriptors to be created on-demand in response to a paste event.

@joechrz
Copy link
Collaborator Author

joechrz commented Aug 23, 2018

Couple things we could do here. I think I like the idea of adding paste events the most since it doesn't make assumptions and doesn't force you to throw UI code in the data layer.

Auto-Create Descriptors

Provide an option on the AbstractRowColModel to auto-create new descriptors on paste. To make this work we'd either need a defaultDescriptorOptions or a callback to configure the descriptor.

grid.rowModel.enableAutoCreateDescriptors = function (options: Callback | DefaultDescriptorOptions) {
   ...
}

Add a setOutOfBounds method to the DataModel

Basically throws the responsibility of handling out-of-bounds data to whoever provides the data model. I think I like this one the most since you get pretty granular control over how out-of-bounds data is handled, but I don't know if modifying the col/row descriptors in a data model function would cause issues. Also feels weird to be modifying the grid structure from the data model

{
    ...,
    setOutOfBounds: (row: number, col: number, value: any) {
        if (row > this.rowModel.length()) {
            grid.rowModel.add(newRowDescriptor)
        }
        // forward to the original set function
        dataModel.set(row, col, value)
    }

Improve Copy/Paste API

Provide grid events for beforePaste (and I guess afterPaste for symmetry) with information about paste coordinates and pasted data size. Adjust the descriptors for the grid in beforePaste

// is this even how you attach events?
grid.on("paste", (row: number, col: number, data: any[][]) => {
    if (row + data.length > grid.rowModel.length()) {
        // make new descriptors
    }

    // prevent paste with `return false`?
    return true
})

@scamden
Copy link
Member

scamden commented Aug 23, 2018

ya I dig the event api the most. tbh you could just capture the paste event as a work around that doesn't require changes.

window.addEventListener('paste', (e) => {

  if (e.target.classList.has('grid-textarea')) {
    //do things
  }
}, true)

@joechrz
Copy link
Collaborator Author

joechrz commented Aug 24, 2018

hey look at that. this does in fact work for what I need

I can hack around it for now (ish) but ideally we should do the paste pre-processing (parsing/excel format transform/etc.) and then make the parsed data available through an api somewhere.

@scamden
Copy link
Member

scamden commented Aug 25, 2018 via email

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

No branches or pull requests

2 participants