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

Simplify block creation #350

Open
arumsey opened this issue May 16, 2024 · 0 comments
Open

Simplify block creation #350

arumsey opened this issue May 16, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@arumsey
Copy link
Collaborator

arumsey commented May 16, 2024

Is your feature request related to a problem? Please describe.
Currently, if you want to create a block you need to use the createTable function. Since every Helix block must follow a specific pattern however, createTable requires code to be duplicated during an import for every block that needs to be created.

Adding a createBlock function on top of createTable can abstract away this duplication and reduce block creation down to its bare essentials which is a block name and either an object of name/value pairs or a two-dimensional array of rows and columns.

In addition metadata creation should be split up in to metadata object creation and the actual metadata block creation. That way an import could leverage the default object and add to it instead of having to duplicate the same code for metadata such as title and description.

Describe the solution you'd like

  static createBlock(document, { name, variants = [], cells: data }) {
    const headerRow = variants.length ? [`${name} (${variants.join(', ')})`] : [name];
    let blockRows = data;
    if (!Array.isArray(data)) {
      blockRows = Object.entries(data).map(([key, value]) => {
        if (Array.isArray(value)) {
          value = value.map((v) => {
            const p = document.createElement('p');
            p.innerHTML = v;
            return p;
          });
        } else {
          value = [value];
        }
        return [key, value];
      });
    }
    return DOMUtils.createTable([headerRow, ...blockRows], document);
  }

Describe alternatives you've considered
None, other than status quo and duplicated code.

Additional context
This feature is also important for a Declarative Transformations API being proposed for the Helix Importer that has the potential for import scripts to be written without any code and just a JSON object.

@arumsey arumsey added the enhancement New feature or request label May 16, 2024
arumsey added a commit to arumsey/helix-importer that referenced this issue May 16, 2024
- add `createBlock` function
- move metadata object creation to `getMetadata` function
kptdobe pushed a commit that referenced this issue May 22, 2024
- add `createBlock` function
- move metadata object creation to `getMetadata` function
adobe-bot pushed a commit that referenced this issue May 22, 2024
# [3.4.0](v3.3.8...v3.4.0) (2024-05-22)

### Features

* simplify block creation [#350](#350) ([#351](#351)) ([b766954](b766954))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant