corvée(dépendances) ajoute Carbon Fields

This commit is contained in:
gcch 2024-08-09 18:45:01 +02:00
commit 62368587e5
459 changed files with 72750 additions and 26 deletions

View file

@ -0,0 +1,25 @@
/**
* External dependencies.
*/
import { compose } from '@wordpress/compose';
import { addFilter } from '@wordpress/hooks';
/**
* Carbon Fields dependencies.
*/
import { withFilters } from '@carbon-fields/core';
/**
* Internal dependencies.
*/
import withContainer from '../hocs/with-container';
/**
* Extends the containers with necessary hooks.
*/
addFilter( 'carbon-fields.register-container-type', 'carbon-fields/metaboxes', ( type, context, component ) => {
return compose(
withContainer,
withFilters( `carbon-fields.${ type }.${ context }` )
)( component );
} );

View file

@ -0,0 +1,81 @@
/**
* External dependencies.
*/
import { render, createRoot } from '@wordpress/element';
import { select } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { forEach } from 'lodash';
/**
* Internal dependencies.
*/
import './hooks';
import './widget';
import './term-meta';
import './theme-options';
import './user-meta';
import Container from '../components/container';
import { getContainerType, registerContainerType } from './registry';
import { registerContainerRoot } from './root-registry';
/**
* Registers the containers.
*/
[
'post_meta',
'term_meta',
'user_meta',
'comment_meta',
'network',
'theme_options',
'nav_menu_item',
'widget'
].forEach( ( type ) => registerContainerType( type, Container ) );
/**
* Renders the given container.
*
* @param {Object} container
* @param {string} context
* @return {void}
*/
export function renderContainer( container, context ) {
const node = document.querySelector( `.container-${ container.id }` );
const Component = getContainerType( container.type, context );
if ( node ) {
const NodeComponent = <Component id={ container.id } />;
if ( createRoot ) {
const nodeRoot = createRoot( node );
nodeRoot.render( NodeComponent );
registerContainerRoot( container.id, nodeRoot );
} else {
render(
NodeComponent,
node,
() => {
node.dataset.mounted = true;
}
);
}
} else {
// eslint-disable-next-line no-console
console.error( sprintf( __( 'Could not find DOM element for container "%1$s".', 'carbon-fields-ui' ), container.id ) );
}
}
/**
* Initializes the containers.
*
* @param {string} context
* @return {void}
*/
export default function initializeContainers( context ) {
const containers = select( 'carbon-fields/metaboxes' ).getContainers();
forEach( containers, ( container ) => {
renderContainer( container, context );
} );
}

View file

@ -0,0 +1,13 @@
/**
* External dependencies.
*/
import { createRegistry } from '@carbon-fields/core';
export const {
registerContainerType,
getContainerType
} = createRegistry( 'container', [
'classic',
'gutenberg'
] );

View file

@ -0,0 +1,28 @@
const rootRegistry = {};
export function registerContainerRoot( containerId, root ) {
rootRegistry[ containerId ] = {
createdAt: Math.floor(Date.now() / 1000),
...root,
unmount() {
// Fix issues with race condition by delaying
// the onLoad unmounting of containers
// they would be unmounted later
if ( parseFloat( window.cf.config.wp_version ) >= 6.2 ) {
const currentTime = Math.floor(Date.now() / 1000);
if ( currentTime - rootRegistry[ containerId ].createdAt >= 3 ) {
root.unmount();
delete rootRegistry[ containerId ];
}
} else {
root.unmount();
delete rootRegistry[ containerId ];
}
}
};
}
export function getContainerRoot( containerId ) {
return rootRegistry[ containerId ] || null;
}

View file

@ -0,0 +1,68 @@
/**
* External dependencies.
*/
import _ from 'lodash';
import { addFilter } from '@wordpress/hooks';
import { dispatch } from '@wordpress/data';
import { withEffects } from 'refract-callbag';
import { pipe, filter } from 'callbag-basics';
/**
* Internal dependencies.
*/
import fromEventPattern from '../../utils/from-event-pattern';
import { normalizePreloadedState } from '../../store/helpers';
/**
* The function that controls the stream of side effects.
*
* @return {Object}
*/
function aperture() {
return pipe(
fromEventPattern(
( handler ) => window.jQuery( document ).on( 'ajaxSuccess', handler ),
( handler ) => window.jQuery( document ).off( 'ajaxSuccess', handler ),
( e, xhr, options, data ) => ( {
options,
data
} )
),
filter( ( { options, data } ) => {
return options.data
&& options.data.indexOf( 'carbon_fields_container' ) > -1
&& options.data.indexOf( 'add-tag' ) > -1
&& ! data.documentElement.querySelector( 'wp_error' );
} )
);
}
/**
* The function that causes the side effects.
*
* @param {Object} props
* @return {Function}
*/
function handler( props ) {
return function() {
// Collects identifiers of current fields so we can remove them later.
const oldFieldIds = _.map( props.container.fields, 'id' );
// Get a fresh copy of the container and fields.
const { containers, fields } = normalizePreloadedState( _.get( window.cf, 'preloaded.containers', [] ) );
const container = _.find( containers, [ 'id', props.id ] );
const containerFields = _.filter( fields, [ 'container_id', props.id ] );
// Replace the container and add the new fields.
const { updateState, removeFields } = dispatch( 'carbon-fields/metaboxes' );
updateState(
_.keyBy( [ container ], 'id' ),
_.keyBy( containerFields, 'id' )
);
removeFields( oldFieldIds );
};
}
addFilter( 'carbon-fields.term_meta.classic', 'carbon-fields/metaboxes', withEffects( aperture, { handler } ) );

View file

@ -0,0 +1,52 @@
/**
* External dependencies.
*/
import { addFilter } from '@wordpress/hooks';
import { withEffects } from 'refract-callbag';
import { map, pipe } from 'callbag-basics';
import fromEvent from 'callbag-from-event';
/**
* Internal dependencies.
*/
import './style.scss';
/**
* The function that controls the stream of side effects.
*
* @return {Object}
*/
function aperture() {
return pipe(
fromEvent( window, 'scroll' ),
map( () => window.jQuery( window ).scrollTop() )
);
}
/**
* The function that causes the side effects.
*
* @param {Object} props
* @return {Function}
*/
function handler() {
return function( windowTopOffset ) {
const $container = window.jQuery( '.carbon-box:first' );
const $panel = window.jQuery( '#postbox-container-1' );
const $bar = window.jQuery( '#wpadminbar' );
const offset = $bar.height() + 10;
const threshold = $container.offset().top - offset;
// In some situations the threshold is negative number because
// the container element isn't rendered yet.
if ( threshold > 0 ) {
$panel
.toggleClass( 'fixed', windowTopOffset >= threshold )
.css( 'top', offset );
}
};
}
addFilter( 'carbon-fields.theme_options.classic', 'carbon-fields/metaboxes', withEffects( aperture, { handler } ) );

View file

@ -0,0 +1,10 @@
/* ==========================================================================
Theme Options
========================================================================== */
#postbox-container-1.fixed {
.carbon-theme-options #post-body.columns-2 &,
.carbon-network #post-body.columns-2 & {
position: fixed; right: 0; margin-right: 20px;
}
}

View file

@ -0,0 +1,4 @@
/**
* Internal dependencies.
*/
import './style.scss';

View file

@ -0,0 +1,7 @@
/* ==========================================================================
User Meta
========================================================================== */
.cf-container-user-meta {
max-width: 600px;
}

View file

@ -0,0 +1,50 @@
/**
* External dependencies.
*/
import { select } from '@wordpress/data';
import { addFilter } from '@wordpress/hooks';
import { withEffects } from 'refract-callbag';
/**
* Internal dependencies.
*/
import './style.scss';
/**
* Carbon Fields dependencies.
*/
import { fromSelector } from '@carbon-fields/core';
/**
* The function that controls the stream of side effects.
*
* @return {Object}
*/
function aperture() {
return fromSelector( select( 'carbon-fields/metaboxes' ).isFieldUpdated );
}
/**
* The function that causes the side effects.
*
* @param {Object} props
* @return {Function}
*/
function handler( props ) {
return function( { action } ) {
if ( ! action ) {
return;
}
const { container } = props;
const { payload } = action;
if ( container.fields.map( ( field ) => field.id ).indexOf( payload.fieldId ) >= 0 ) {
const $carbonContainer = window.jQuery( `.container-${ container.id }` );
$carbonContainer.closest( '.widget-inside' ).trigger( 'change' );
}
};
}
addFilter( 'carbon-fields.widget.classic', 'carbon-fields/metaboxes', withEffects( aperture, { handler } ) );

View file

@ -0,0 +1,31 @@
/* ==========================================================================
Widget
========================================================================== */
.cf-container-widget {
margin-bottom: 13px;
.cf-field {
margin: 1em 0 0;
padding: 0;
border-width: 0;
}
.cf-field + .cf-field {
border-top-width: 0;
}
.cf-complex__group-body {
border-width: 1px 1px 1px 1px;
margin-top: 0;
}
.cf-complex__group-body .cf-field {
padding: 12px;
}
.cf-complex__group-body .cf-field + .cf-field {
border-width: 1px 0 0 0;
padding-top: 1em;
}
}