IMPLEMENTED: Auto-import images on template import

Hello everyone,

last year I built a plug-in to convert Figma auto-layout designs to Elementor. It actually was a pretty cool plug-in, but due to Elementor’s discutable direction, I ceased all the work on it. Then I found again my new home at Bricks, which I’m very thankful for and I’d like to give back to you guys, by building a Figma to Bricks plugin. Since the builder uses JSON to copy/paste designs, it should be possible to translate Figma layouts into the JSON which would then simply be pasted into Bricks.

I just want to understand what would the interest be around here, do you use Figma? Would you be interested to have a such a plugin?

32 Likes

This would be fantastic! :slight_smile:

1 Like

for sure, that would be crazy time saving!

1 Like

It would be awesome!

1 Like

Of course that would be fantastic! Ask in the FB group, I think you will get also some feedback there :wink:

1 Like

It would be huge. It would put Smellamentor further behind. :slightly_smiling_face:

1 Like

Hello everyone, I started working on this. I’ll update you guys on the progress. So far it looks like it should actually be simpler to do than in Elementor as Bricks has a flat structure of the JSON that is being copied and pasted, opposed to Elementor, which had a JSON tree object.

My only question as of now is… Do images get copied between pages (domains)? Could someone from the Bricks team confirm/debunk this for me? I’ve been trying to understand how it works. In Elementor, this happened automatically - the images simply got downloaded given the URL. Any info about this @timmse? Or someone else?

I found some seemingly old/dead threads like this one where it’s discussed, but I don’t see any “fixes” in the changelog since when this feature was released.

When I built this for Elementor, I spun up an Express server, that downloaded the images from Figma into each user’s folder and then Elementor downloaded them from my server to the Wordpress Instance where I pasted it.

1 Like

Seems like unfortunately this is still in the backlog. I’m wondering whether it would be worth to build this with the core “image” feature absent. I will have to consider this.

Hi @broberto,

Good coincidence! I recently had to build a hierarchical structure from our current flat array structure for a different task. Although it hasn’t been tested extensively, it might help you build the hierarchical structure from our flat array. Here’s the function you can use:

/**
 * Builds a hierarchical tree structure from a flat array of Bricks elements.
 *
 * The tree structure is used to process each element in a depth-first manner to maintain the hierarchy.
 *
 * @param array $elements
 * @return array
 */
function build_elements_tree( $elements ) {
    // Validate input
    if ( ! is_array( $elements ) || empty( $elements ) ) {
        return [];
    }

    $tree           = [];
    $indexed        = [];
    $children_order = [];

    // First pass: Index elements and store children order
    foreach ( $elements as $element ) {
        if ( ! isset( $element['id'] ) || ! is_string( $element['id'] ) ) {
            continue;
        }

        // Index the element and initialize its children array
        $indexed[ $element['id'] ]             = $element;
        $indexed[ $element['id'] ]['children'] = [];

        // Store the original children order if available
        if ( isset( $element['children'] ) && is_array( $element['children'] ) ) {
            $children_order[ $element['id'] ] = array_values( array_filter( $element['children'], 'is_string' ) );
        }
    }

    // Second pass: Build the hierarchical tree
    foreach ( $elements as $element ) {
        if ( ! isset( $element['id'] )) continue; // Skip invalid elements

        if ( ! empty( $element['parent'] ) && isset( $indexed[ $element['parent'] ] ) ) {
            // Add as child to parent
            $indexed[ $element['parent'] ]['children'][] = &$indexed[ $element['id'] ];
        } else {
            // Add to root of tree if no parent or parent not found
            $tree[] = &$indexed[ $element['id'] ];
        }
    }

    // Sort children based on the preserved order
    sort_children( $tree, $children_order );

    return $tree;
}

/**
 * Recursively sorts children of elements based on the original order.
 *
 * @param array $elements Array of elements to sort.
 * @param array $children_order Original order of children for each element.
 */
function sort_children( &$elements, $children_order ) {
    if ( ! is_array( $elements ) ) {
        return;
    }

    foreach ( $elements as &$element ) {
        if ( ! isset( $element['id'] ) || ! is_array( $element['children'] ) ) {
            continue; // Skip invalid elements
        }

        if ( ! empty( $element['children'] ) && isset( $children_order[ $element['id'] ] ) ) {
            $order = array_flip( $children_order[ $element['id'] ] );
            usort(
                $element['children'],
                function( $a, $b ) use ( $order ) {
                    if ( ! isset( $a['id'] ) || ! isset( $b['id'] ) ) {
                        return 0; // Don't change order for invalid elements
                    }
                    $pos_a = isset( $order[ $a['id'] ] ) ? $order[ $a['id'] ] : PHP_INT_MAX;
                    $pos_b = isset( $order[ $b['id'] ] ) ? $order[ $b['id'] ] : PHP_INT_MAX;
                    return $pos_a - $pos_b;
                }
            );
        }
        // Recursively sort children of this element
        sort_children( $element['children'], $children_order );
    }
}

Here’s an example of how you may use it:

add_action( 'wp', 'build_elements_tree_example' );
function build_elements_tree_example() {
	// Log the ID
	$post_id = get_the_ID();

	// Ensure we have a valid post ID
	if ( $post_id ) {
		// Get Bricks data:
		$bricks_elements = \Bricks\Database::get_data( $post_id, 'content' );

		// Build the elements tree
		$elements_tree = build_elements_tree( $bricks_elements );

		// Log the tree
		error_log( print_r( $elements_tree, true ) );
	}
}

Please note that this code is still not in production, but we might ship it as a helper function as part of the Bricks core. Not sure if this is what you’re looking for exactly or what you’d like to do, but maybe this might be helpful in some way. If it’s something you need, you can use it.

Regarding this issue: WIP: Cross-Site Copy/Paste Doesn't Copy Pseudo CSS - #5 by timmse, yes, that’s still a WIP. As a temporary workaround, if you’re working on a WordPress plugin, you can upload the image to the media library and then populate the properties in Bricks accordingly. To help you understand the structure of the image element, here are some details:

If the image is from the WordPress library, this is the general structure for it:

{
    "id": "rpndgb",
    "name": "image",
    "parent": 0,
    "children": [],
    "settings": {
        "image": {
            "id": 56559,
            "filename": "v2osk-1z2niibpg5a-scaled.jpg",
            "size": "large",
            "full": "https://bricks.local/wp-content/uploads/2024/06/v2osk-1z2niibpg5a-scaled.jpg",
            "url": "https://bricks.local/wp-content/uploads/2024/06/v2osk-1z2niibpg5a-1024x610.jpg"
        }
    }
}

The image property contains the settings for the image control. So this would apply to other elements as well.

If it’s an external URL, then this would be the structure:

{
    "id": "rpndgb",
    "name": "image",
    "parent": 0,
    "children": [],
    "settings": {
        "image": {
            "url": "https://fastly.picsum.photos/id/862/5000/3333.jpg?hmac=MVCRoFdvRCHO_-QazYiihAuaFWJh3SP2eVBG4oCkvqM",
            "external": true,
            "filename": "3333.jpg?hmac=MVCRoFdvRCHO_-QazYiihAuaFWJh3SP2eVBG4oCkvqM"
        }
    }
}

I hope that helps!

3 Likes

Hey, first of all, thank you for reaching out to me, it really means a lot - at Elementor they didn’t even bother solving my Github (bug) Issues lol.

The way I wanted to approach this, is by building a parser on the Figma’s end. They offer an extensive JS API that I’ve already used to make this happen with the other builders.

I also was brainstorming a Wordpress plugin actually, that would download and upload the images first but that kind of felt to me like a lot of friction to put on users UX wise, so I’m still kind of weighing the pros and cons of doing this. The ideal scenario would be having this implemented natively, but I understand it is in the pipeline. I’d definitely would like to avoid doing the image uploading work myself, as I’d like Bricks to handle this and from what I understood, it’s in the making anyway.

I yet have to decide whether going for the plugin even without having the images feature - especially time wise, but I definitely might. I just need to consider it carefully, but I’m really motivated by the engagement I got here. But thanks, this helps me a lot!

Do you have by any chance an ETA regarding the fix of the images issue?

1 Like

This actually helped me a lot, as now I can just go and place some placeholders - at least for now. An ideal scenario might be that I could pass an external URL (or mock Bricks to think that it’s a WP url) so that it downloads the image from my API.

1 Like

Yes, that’s the reason why webflow is a great tool - one part of that is the figma plugin therefore bricks needs a figma plugin, too. (with the same or better functionality)

1 Like

I did this once, I might as well be able to do it again ahah :smile:

1 Like

I’m glad :slight_smile:

Unfortunately not. But if I had to guess & for transparency, given our current priorities and tasks, it is unlikely to be implemented soon.

Good luck with your project :four_leaf_clover:

I’ve been searching forexactly this. I’d pay for such a plugin and strongly believe Bricks would gain a lot of users.

In the meantime, I’d appreciate if someone can point me to documentation regarding how to create pastable designs a la “renmoe” library etc.

I’m hoping to bring a first pre-alpha prototype within a few weeks :slight_smile: Stay tuned

3 Likes

We’ve got … rectangles :construction:

5 Likes

Congratulations, celebrate rectangles! :partying_face:

1 Like

Hey @broberto!

This plugin looks really promising :heart_eyes:

I am a web designer, so I use Figma quite a lot. If you need any help testing the plugin, let me know :blush:

1 Like

I definitely will need testers :slight_smile: especially people who know Figma and use autolayout, as this plugin will run on autolayouts, similarly to Webflow’s plugin.

2 Likes