HEX
Server: Apache
System: Linux www 5.10.0-10-amd64 #1 SMP Debian 5.10.84-1 (2021-12-08) x86_64
User: root (0)
PHP: 8.2.1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals
Upload Files
File: /mnt/drbd/www/edenhouse/wp-content/plugins/essential-blocks/blocks/advanced-tabs/src/helpers.js
/**
 * WordPress dependencies
 */
import { __ } from "@wordpress/i18n";
import {
    InspectorControls,
    MediaUpload,
    useBlockProps,
    RichText,
    InnerBlocks,
} from "@wordpress/block-editor";
import { useEffect, useState, useRef } from "@wordpress/element";
import { select, dispatch } from "@wordpress/data";
import { IconButton } from "@wordpress/components";
import { createBlock } from "@wordpress/blocks";

const { times } = lodash;

//
export const getBlocksMethods = () => {
    const { getBlock, getBlocks } = select("core/block-editor");
    const {
        moveBlockToPosition,
        updateBlockAttributes,
        insertBlock,
        replaceInnerBlocks,
    } = dispatch("core/block-editor");

    return {
        getBlock,
        getBlocks,
        moveBlockToPosition,
        updateBlockAttributes,
        insertBlock,
        replaceInnerBlocks,
    };
};

//
export const resetTabsOrder = ({ clientId, setAttributes, tabTitles }) => {
    const {
        //
        updateBlockAttributes,
    } = dispatch("core/block-editor");

    const innerBlocks = select("core/block-editor").getBlocks(clientId);

    const newTabTitles = tabTitles.map((item, index) => ({
        ...item,
        id: `${index + 1}`,
    }));

    //
    setAttributes({
        tabTitles: newTabTitles,
    });

    //
    times(innerBlocks.length, (n) => {
        updateBlockAttributes(innerBlocks[n].clientId, {
            tabId: `${n + 1}`,
        });
    });
};

//
export const addTab = ({
    clientId,
    tabChildCount,
    setAttributes,
    tabTitles,
    blockId,
    handleTabTitleClick,
}) => {
    // const thisBlock = getBlock(clientId);
    const innerBlocks = [...select("core/block-editor").getBlocks(clientId)];
    const maxId = tabTitles.reduce(
        (acc, curr) => Math.max(parseInt(acc), parseInt(curr.id)),
        0
    );
    const tabId = `${maxId + 1}`;

    const newBlock = createBlock("essential-blocks/tab", {
        tabId,
        tabParentId: `${blockId}`,
    });

    innerBlocks.splice(innerBlocks.length, 0, newBlock);
    dispatch("core/block-editor")
        .replaceInnerBlocks(clientId, innerBlocks)
        .then(() => {
            //
            setAttributes({
                tabTitles: [
                    ...tabTitles,
                    {
                        text: `Tab Title ${parseInt(innerBlocks.length)}`,
                        id: tabId,
                        media: "icon",
                        icon: "fas fa-home",
                    },
                ],
                tabChildCount: tabChildCount + 1,
            });
            handleTabTitleClick(tabId);
        });

    //
};