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

Add easy access to current block data object #387

Open
mrsunshine opened this issue May 10, 2024 · 7 comments
Open

Add easy access to current block data object #387

mrsunshine opened this issue May 10, 2024 · 7 comments

Comments

@mrsunshine
Copy link

Feature request:
Current block data object in the root of your plugin.data or a util function which returns the object.

Use case you want to read data from a field from your current block. At the moment you have the blockUid in your plugin.data and you have t recursively search for the object with the given uid to get your block data object. Would be cool to either have the block object in the root of your plugin.data or a util function which returns the object. so not everybody has to implement his own search block function.

@mrsunshine mrsunshine changed the title Current block data object Add easy access to current block data object May 10, 2024
@eunjae-lee
Copy link
Contributor

Hi @mrsunshine thanks for reaching out to us. If I understand this correctly, do you want a field plugin to be able to access other fields in the current story via blockUid?

@mrsunshine
Copy link
Author

mrsunshine commented May 10, 2024

@eunjae-lee yes. All informations are there but difficult to access.
Imagine you have field A and your field type plugin in the same block. Inside your field type plugin you want to use the value of the field A. Currently you have to use plugin.data.blockUid to recursively search through data.story.content to find the block object with blockUid.
For easy use it would be helpfull to have either:

  1. the block object where the field type plugin on the root level plugin.data.block
  2. or a helper function which returns a block object by UUID

something like this:

...
const blockUid: string = plugin?.data?.blockUid || ''
const contentObj: Object | undefined = data?.story?.content;
const myBlock = searchBlock(contentObj, blockUid);

const searchBlock = (obj: Block, uid: string): Block | null => {
  if (obj._uid === uid) {
    return obj;
  }

  // Check nested objects and arrays
  for (let key of Object.keys(obj)) {
    const value = obj[key];
    if (Array.isArray(value)) {
      for (let item of value) {
        let result = searchBlock(item, uid);
        if (result) {
          return result;
        }
      }
    } else if (typeof value === 'object' && value !== null) {
      let result = searchBlock(value, uid);
      if (result) {
        return result;
      }
    }
  }

  return null; // Return null if the block is not found
}

@eunjae-lee
Copy link
Contributor

@mrsunshine got it. Thanks for the clarification and thanks for providing this code snippet, which can be useful for others looking at this issue in the future 🙂 We will create an internal ticket to discuss about this, but I cannot tell any ETA, and whether or not we will ship this, though. Thanks for your contribution 💙

@marckraw
Copy link

Also we are looking for it quite much, traversing the whole object, especially for bigger sites is not the best idea in terms of performance. any updates @eunjae-lee ? At least if you are going to focus on that at all, then we can think of some other way of handling this.

@Dawntraoz
Copy link
Contributor

@marckraw nothing was planned for this until now, I created a Feature Request for it in the new support system, but I couldn't find the one from Eunjae 🙏 and added your company as the client so it can be prioritised accordingly.

@demetriusfeijoo were you aware of any plans on this topic?

@marckraw
Copy link

@Dawntraoz thank you for handling this :)

@demetriusfeijoo
Copy link
Contributor

Hey @Dawntraoz 🙌

@demetriusfeijoo were you aware of any plans on this topic?

Although I recall this topic, I'm not aware of it being prioritized or evolved on our side yet.

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

No branches or pull requests

5 participants