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

Organise block utils #211

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { versionCompare } from '../utils';
import { versionCompare } from '../../utils';

const { createBlock } = wp.blocks;

Expand Down
18 changes: 1 addition & 17 deletions private/src/scripts/editor/blocks/columns/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import classNames from 'classnames';

import { findBlockType } from '../../utils';
import './row-column.jsx';
import DisplayComponent from './DisplayComponent.jsx';

Expand Down Expand Up @@ -46,23 +47,6 @@ registerBlockType('amnesty-core/block-row', {
),
});

const findBlockType = (blocks, type) => {
let found = [];

// eslint-disable-next-line no-restricted-syntax
for (const block of blocks) {
if (block.name === type) {
found.push(block.clientId);
}

if (block.innerBlocks.length) {
found = [...found, ...findBlockType(block.innerBlocks, type)];
}
}

return found;
};

wp.domReady(() => {
const select = wp.data.select('core/block-editor');
const dispatch = wp.data.dispatch('core/block-editor');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { httpsOnly } from '../utils';
import { httpsOnly } from '../../utils';

const { delay } = lodash;
const { Button, TextControl } = wp.components;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { httpsOnly } from '../utils';
import { httpsOnly } from '../../utils';

const { Button, TextControl } = wp.components;
const { Fragment, useState } = wp.element;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { httpsOnly } from '../utils';
import { httpsOnly } from '../../utils';

const { BlockAlignmentToolbar, BlockControls } = wp.blockEditor;
const { Placeholder, TextControl } = wp.components;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import BlockImageSelector from './components/BlockImageSelector.jsx';
import MediaMetadata from '../../components/MediaMetadata.jsx';
import MediaMetadataVisibilityControls from '../../components/MediaMetadataVisibilityControls.jsx';
import PostFeaturedVideo from '../../components/PostFeaturedVideo.jsx';
import { fetchMediaData } from '../utils';
import { fetchMediaData } from '../../utils';

const { InnerBlocks, InspectorControls, RichText, URLInputButton } = wp.blockEditor;
const { PanelBody, SelectControl } = wp.components;
Expand Down
26 changes: 2 additions & 24 deletions private/src/scripts/editor/blocks/hero/replaceHeaders.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
import { findBlockType } from '../../utils';

const { omit } = lodash;
const { createBlock } = wp.blocks;

/**
* Recursively search blocks on page for a specific block type
*
* @param {Array} blocks the list of blocks to search
* @param {String} type the block type to find
*
* @returns {Array}
*/
const findBlockType = (blocks, type) => {
let found = [];

Array.from(blocks).forEach((block) => {
if (block.name === type) {
found.push(block.clientId);
}

if (block.innerBlocks.length) {
found = [...found, ...findBlockType(block.innerBlocks, type)];
}
});

return found;
};

const metaKeyToAttributeMap = {
_hero_alignment: 'align',
_hero_background: 'background',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classnames from 'classnames';
import { httpsOnly } from '../utils';
import { httpsOnly } from '../../utils';

const { BlockAlignmentToolbar, BlockControls, InspectorControls, RichText } = wp.blockEditor;
const { Button, PanelBody, TextControl } = wp.components;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { httpsOnly } from '../utils';
import { httpsOnly } from '../../utils';

const { InspectorControls, RichText, BlockControls, AlignmentToolbar } = wp.blockEditor;
const { Button, PanelBody, Placeholder, TextControl } = wp.components;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import classnames from 'classnames';
import PostMediaSelector from '../../components/PostMediaSelector.jsx';
import { randId } from '../utils';
import { randId } from '../../utils';

const { map, omit } = lodash;
const { InspectorControls, RichText, URLInputButton } = wp.blockEditor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classnames from 'classnames';
import { brToP, trimBr } from '../utils';
import { brToP, trimBr } from '../../utils';

const { __ } = wp.i18n;

Expand Down
2 changes: 1 addition & 1 deletion private/src/scripts/editor/blocks/slider/build-slide.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classnames from 'classnames';
import { brToP, trimBr } from '../utils';
import { brToP, trimBr } from '../../utils';

const { __ } = wp.i18n;

Expand Down
2 changes: 1 addition & 1 deletion private/src/scripts/editor/blocks/slider/deprecated.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import classnames from 'classnames';
import blockAttributes from './attributes';
import { trimBr } from '../utils';
import { trimBr } from '../../utils';
import SlideBuilder from './build-slide.jsx';
import buildSlideDeprecated from './build-slide-deprecated.jsx';

Expand Down
207 changes: 0 additions & 207 deletions private/src/scripts/editor/blocks/utils.js

This file was deleted.

2 changes: 1 addition & 1 deletion private/src/scripts/editor/plugins/pop-in/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './block-restrictions';

import { validateBool } from '../../blocks/utils';
import { validateBool } from '../../utils';

const { ToggleControl } = wp.components;
const { compose, ifCondition } = wp.compose;
Expand Down
33 changes: 33 additions & 0 deletions private/src/scripts/editor/utils/apis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable import/prefer-default-export */

/**
* Retrieve media data and pass response to a callback
*
* @param {Number} id the media to fetch
* @param {Function} callback the callback to execute
* @param {Object} state previous data, if any
*
* @return {Promise}
*/
export function fetchMediaData(id, callback, state = {}) {
if (!id) {
return Promise.resolve(callback({}));
}

if (state?.id === id) {
return Promise.resolve(callback(state));
}

return wp
.apiFetch({
path: `/wp/v2/media/${id}?_fields=id,source_url,caption,description&context=edit`,
})
.then((media) =>
callback({
id: media.id,
url: media.source_url,
caption: media.caption.raw,
copyright: media.description.raw,
}),
);
}
Loading