From 74436150810ae1d1adb73a41cd27780722d0aadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Wed, 20 Nov 2024 17:13:15 +0100 Subject: [PATCH] Implement API --- packages/editor/README.md | 24 ++++++++++++++++ packages/editor/src/dataviews/api.js | 41 ++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/packages/editor/README.md b/packages/editor/README.md index 07405d0d51c3d2..bc00e15c8fb892 100644 --- a/packages/editor/README.md +++ b/packages/editor/README.md @@ -1602,6 +1602,18 @@ _Parameters_ - _name_ `string`: Entity name. - _config_ `Action`: Action configuration. +### registerEntityField + +Registers a new DataViews field. + +This is an experimental API and is subject to change. it's only available in the Gutenberg plugin for now. + +_Parameters_ + +- _kind_ `string`: Entity kind. +- _name_ `string`: Entity name. +- _config_ `Field`: Field configuration. + ### RichText > **Deprecated** since 5.3, use `wp.blockEditor.RichText` instead. @@ -1697,6 +1709,18 @@ _Parameters_ - _name_ `string`: Entity name. - _actionId_ `string`: Action ID. +### unregisterEntityField + +Unregisters a DataViews field. + +This is an experimental API and is subject to change. it's only available in the Gutenberg plugin for now. + +_Parameters_ + +- _kind_ `string`: Entity kind. +- _name_ `string`: Entity name. +- _fieldId_ `string`: Field ID. + ### UnsavedChangesWarning Warns the user if there are unsaved changes before leaving the editor. Compatible with Post Editor and Site Editor. diff --git a/packages/editor/src/dataviews/api.js b/packages/editor/src/dataviews/api.js index 130a69bba754c7..e03b9ef35ac758 100644 --- a/packages/editor/src/dataviews/api.js +++ b/packages/editor/src/dataviews/api.js @@ -11,6 +11,7 @@ import { store as editorStore } from '../store'; /** * @typedef {import('@wordpress/dataviews').Action} Action + * @typedef {import('@wordpress/dataviews').Field} Field */ /** @@ -53,3 +54,43 @@ export function unregisterEntityAction( kind, name, actionId ) { _unregisterEntityAction( kind, name, actionId ); } } + +/** + * Registers a new DataViews field. + * + * This is an experimental API and is subject to change. + * it's only available in the Gutenberg plugin for now. + * + * @param {string} kind Entity kind. + * @param {string} name Entity name. + * @param {Field} config Field configuration. + */ +export function registerEntityField( kind, name, config ) { + const { registerEntityField: _registerEntityField } = unlock( + dispatch( editorStore ) + ); + + if ( globalThis.IS_GUTENBERG_PLUGIN ) { + _registerEntityField( kind, name, config ); + } +} + +/** + * Unregisters a DataViews field. + * + * This is an experimental API and is subject to change. + * it's only available in the Gutenberg plugin for now. + * + * @param {string} kind Entity kind. + * @param {string} name Entity name. + * @param {string} fieldId Field ID. + */ +export function unregisterEntityField( kind, name, fieldId ) { + const { unregisterEntityField: _unregisterEntityField } = unlock( + dispatch( editorStore ) + ); + + if ( globalThis.IS_GUTENBERG_PLUGIN ) { + _unregisterEntityField( kind, name, fieldId ); + } +}