Skip to content
Merged
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
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,12 @@
"markdown_source": "../packages/warning/README.md",
"parent": "packages"
},
{
"title": "@wordpress/widgets",
"slug": "packages-widgets",
"markdown_source": "../packages/widgets/README.md",
"parent": "packages"
},
{
"title": "@wordpress/wordcount",
"slug": "packages-wordcount",
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"@wordpress/url": "file:packages/url",
"@wordpress/viewport": "file:packages/viewport",
"@wordpress/warning": "file:packages/warning",
"@wordpress/widgets": "file:packages/widgets",
"@wordpress/wordcount": "file:packages/wordcount"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/customize-widgets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@wordpress/is-shallow-equal": "file:../is-shallow-equal",
"@wordpress/keycodes": "file:../keycodes",
"@wordpress/media-utils": "file:../media-utils",
"@wordpress/widgets": "file:../widgets",
"classnames": "^2.2.6",
"lodash": "^4.17.19"
},
Expand Down
49 changes: 0 additions & 49 deletions packages/customize-widgets/src/components/move-to-sidebar/index.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ export default function SidebarBlockEditor( {
}

return {
...blockEditorSettings,
__experimentalSetIsInserterOpened: setIsInserterOpened,
mediaUpload: mediaUploadBlockEditor,
};
}, [] );
}, [ hasUploadPermissions, blockEditorSettings ] );
const parentContainer = document.getElementById(
'customize-theme-controls'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,33 @@ export default class SidebarAdapter {
return widgetId;
}

_removeWidget( widget ) {
const settingId = widgetIdToSettingId( widget.id );
this.allSettings.remove( settingId );
}

_updateWidget( widget ) {
const prevWidget = this.getWidget( widget.id );

// Bail out update.
// Bail out update if nothing changed.
if ( prevWidget === widget ) {
return widget.id;
}

const settingId = widgetIdToSettingId( widget.id );
this.allSettings( settingId ).set( widget.instance );
// TODO: what about the other stuff?
// Update existing setting if only the widget's instance changed.
if (
prevWidget.idBase &&
widget.idBase &&
prevWidget.idBase === widget.idBase
) {
const settingId = widgetIdToSettingId( widget.id );
this.allSettings( settingId ).set( widget.instance );
return widget.id;
}

return widget.id;
// Otherwise delete and re-create.
this._removeWidget( widget );
return this._createWidget( widget );
}

getWidget( widgetId ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ function getWidgetId( block ) {
function blockToWidget( block, existingWidget = null ) {
let widget;

if ( block.name === 'core/legacy-widget' ) {
const isValidLegacyWidgetBlock =
block.name === 'core/legacy-widget' &&
( block.attributes.id || block.attributes.instance );

if ( isValidLegacyWidgetBlock ) {
if ( block.attributes.id ) {
// Widget that does not extend WP_Widget.
widget = {
Expand Down Expand Up @@ -168,7 +172,7 @@ export default function useSidebarBlockEditor( sidebar ) {

// Bail out updates by returning the previous widgets.
// Deep equality is necessary until the block editor's internals changes.
if ( isEqual( nextBlock, prevBlock ) ) {
if ( isEqual( nextBlock, prevBlock ) && prevWidget ) {
return prevWidget;
}

Expand Down
12 changes: 4 additions & 8 deletions packages/customize-widgets/src/filters/move-to-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ import { without } from 'lodash';
import { BlockControls } from '@wordpress/block-editor';
import { createHigherOrderComponent } from '@wordpress/compose';
import { addFilter } from '@wordpress/hooks';
import { MoveToWidgetArea } from '@wordpress/widgets';

const { wp } = window;

/**
* Internal dependencies
*/
import MoveToSidebar from '../components/move-to-sidebar';

/**
* Use the customize API to get the setting for the sidebar.
* This returns a function that acts as a setter/getter in one for widget ids
Expand Down Expand Up @@ -83,9 +79,9 @@ const withMoveToSidebarToolbarItem = createHigherOrderComponent(
<>
<BlockEdit { ...props } />
<BlockControls>
<MoveToSidebar
sidebars={ getSidebars() }
currentSidebar={ currentSidebar }
<MoveToWidgetArea
widgetAreas={ getSidebars() }
currentWidgetArea={ currentSidebar }
onSelect={ ( newSidebarId ) => {
moveToSidebar(
widgetId,
Expand Down
3 changes: 3 additions & 0 deletions packages/customize-widgets/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
__experimentalGetCoreBlocks,
__experimentalRegisterExperimentalCoreBlocks,
} from '@wordpress/block-library';
import { registerLegacyWidgetVariations } from '@wordpress/widgets';

/**
* Internal dependencies
Expand Down Expand Up @@ -34,6 +35,8 @@ export function initialize( editorName, blockEditorSettings ) {
} );
}

registerLegacyWidgetVariations( blockEditorSettings );

wp.customize.sectionConstructor.sidebar = getSidebarSection();
wp.customize.controlConstructor.sidebar_block_editor = getSidebarControl(
blockEditorSettings
Expand Down
1 change: 1 addition & 0 deletions packages/edit-widgets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@wordpress/reusable-blocks": "file:../reusable-blocks",
"@wordpress/server-side-render": "file:../server-side-render",
"@wordpress/url": "file:../url",
"@wordpress/widgets": "file:../widgets",
"classnames": "^2.2.5",
"lodash": "^4.17.19",
"rememo": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-widgets/src/filters/move-to-widget-area.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { BlockControls } from '@wordpress/block-editor';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { addFilter } from '@wordpress/hooks';
import { MoveToWidgetArea } from '@wordpress/widgets';

/**
* Internal dependencies
*/
import MoveToWidgetArea from '../components/move-to-widget-area';
import { store as editWidgetsStore } from '../store';

const withMoveToWidgetAreaToolbarItem = createHigherOrderComponent(
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-widgets/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
__experimentalRegisterExperimentalCoreBlocks,
} from '@wordpress/block-library';
import { __experimentalFetchLinkSuggestions as fetchLinkSuggestions } from '@wordpress/core-data';
import { registerLegacyWidgetVariations } from '@wordpress/widgets';

/**
* Internal dependencies
Expand All @@ -20,7 +21,6 @@ import './store';
import './filters';
import * as widgetArea from './blocks/widget-area';
import Layout from './components/layout';
import registerLegacyWidgetVariations from './register-legacy-widget-variations';

/**
* Initializes the block editor in the widgets screen.
Expand Down
1 change: 1 addition & 0 deletions packages/widgets/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 5 additions & 0 deletions packages/widgets/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->

## Unreleased

Initial release.
17 changes: 17 additions & 0 deletions packages/widgets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Widgets

This package contains common functionality used by the widgets block editor in the Widgets screen and the Customizer.

> This package is meant to be used only with WordPress core. Feel free to use it in your own project but please keep in mind that it might never get fully documented.

## Installation

Install the module

```bash
npm install @wordpress/widgets
```

_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for ES2015+ such as IE browsers then using [core-js](https://github.com/zloirock/core-js) will add polyfills for these methods._

<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
32 changes: 32 additions & 0 deletions packages/widgets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@wordpress/widgets",
"version": "1.0.0-prerelease",
"description": "Functionality used by the widgets block editor in the Widgets screen and the Customizer.",
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
"keywords": [ "wordpress" ],
"homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/packages/widgets/README.md",
"repository": {
"type": "git",
"url": "https://github.com/WordPress/gutenberg.git",
"directory": "packages/widgets"
},
"bugs": {
"url": "https://github.com/WordPress/gutenberg/issues"
},
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"dependencies": {
"@babel/runtime": "^7.13.10",
"@wordpress/blocks": "file:../blocks",
"@wordpress/components": "file:../components",
"@wordpress/core-data": "file:../core-data",
"@wordpress/data": "file:../data",
"@wordpress/i18n": "file:../i18n",
"@wordpress/icons": "file:../icons"
},
"publishConfig": {
"access": "public"
}
}
1 change: 1 addition & 0 deletions packages/widgets/src/components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as MoveToWidgetArea } from './move-to-widget-area';
2 changes: 2 additions & 0 deletions packages/widgets/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './components';
export { default as registerLegacyWidgetVariations } from './register-legacy-widget-variations';