forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DataViews: Bootstrap Actions Extensibility API (WordPress#62052)
Co-authored-by: youknowriad <[email protected]> Co-authored-by: ntsekouras <[email protected]> Co-authored-by: mcsf <[email protected]> Co-authored-by: fabiankaegy <[email protected]> Co-authored-by: Mamaduka <[email protected]>
- Loading branch information
1 parent
47ee312
commit 1cab14c
Showing
13 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as DataViews } from './dataviews'; | ||
export { VIEW_LAYOUTS } from './layouts'; | ||
export { filterSortAndPaginate } from './filter-and-sort-data-view'; | ||
export type * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { dispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../lock-unlock'; | ||
import { store as editorStore } from '../store'; | ||
|
||
/** | ||
* @typedef {import('@wordpress/dataviews').Action} Action | ||
*/ | ||
|
||
/** | ||
* Registers a new DataViews action. | ||
* | ||
* 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 {Action} config Action configuration. | ||
*/ | ||
|
||
export function registerEntityAction( kind, name, config ) { | ||
const { registerEntityAction: _registerEntityAction } = unlock( | ||
dispatch( editorStore ) | ||
); | ||
|
||
if ( globalThis.IS_GUTENBERG_PLUGIN ) { | ||
_registerEntityAction( kind, name, config ); | ||
} | ||
} | ||
|
||
/** | ||
* Unregisters a DataViews action. | ||
* | ||
* 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} actionId Action ID. | ||
*/ | ||
export function unregisterEntityAction( kind, name, actionId ) { | ||
const { unregisterEntityAction: _unregisterEntityAction } = unlock( | ||
dispatch( editorStore ) | ||
); | ||
|
||
if ( globalThis.IS_GUTENBERG_PLUGIN ) { | ||
_unregisterEntityAction( kind, name, actionId ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import type { Action, AnyItem } from '@wordpress/dataviews'; | ||
|
||
export function registerEntityAction< Item extends AnyItem >( | ||
kind: string, | ||
name: string, | ||
config: Action< Item > | ||
) { | ||
return { | ||
type: 'REGISTER_ENTITY_ACTION' as const, | ||
kind, | ||
name, | ||
config, | ||
}; | ||
} | ||
|
||
export function unregisterEntityAction( | ||
kind: string, | ||
name: string, | ||
actionId: string | ||
) { | ||
return { | ||
type: 'UNREGISTER_ENTITY_ACTION' as const, | ||
kind, | ||
name, | ||
actionId, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { State } from './reducer'; | ||
|
||
export function getEntityActions( state: State, kind: string, name: string ) { | ||
return state.actions[ kind ]?.[ name ] ?? []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { combineReducers } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import type { Action } from '@wordpress/dataviews'; | ||
|
||
type ReduxAction = | ||
| ReturnType< typeof import('./private-actions').registerEntityAction > | ||
| ReturnType< typeof import('./private-actions').unregisterEntityAction >; | ||
|
||
export type ActionState = Record< string, Record< string, Action< any >[] > >; | ||
export type State = { | ||
actions: ActionState; | ||
}; | ||
|
||
function actions( state: ActionState = {}, action: ReduxAction ) { | ||
switch ( action.type ) { | ||
case 'REGISTER_ENTITY_ACTION': | ||
return { | ||
...state, | ||
[ action.kind ]: { | ||
[ action.name ]: [ | ||
...( state[ action.kind ]?.[ action.name ] ?? [] ), | ||
action.config, | ||
], | ||
}, | ||
}; | ||
case 'UNREGISTER_ENTITY_ACTION': { | ||
return { | ||
...state, | ||
[ action.kind ]: ( | ||
state[ action.kind ]?.[ action.name ] ?? [] | ||
).filter( ( _action ) => _action.id !== action.actionId ), | ||
}; | ||
} | ||
} | ||
|
||
return state; | ||
} | ||
|
||
export default combineReducers( { | ||
actions, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/tsconfig.json", | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"declarationDir": "build-types" | ||
}, | ||
"references": [ | ||
{ "path": "../a11y" }, | ||
{ "path": "../api-fetch" }, | ||
{ "path": "../blob" }, | ||
{ "path": "../block-editor" }, | ||
{ "path": "../components" }, | ||
{ "path": "../compose" }, | ||
{ "path": "../core-data" }, | ||
{ "path": "../data" }, | ||
{ "path": "../dataviews" }, | ||
{ "path": "../date" }, | ||
{ "path": "../deprecated" }, | ||
{ "path": "../dom" }, | ||
{ "path": "../element" }, | ||
{ "path": "../hooks" }, | ||
{ "path": "../html-entities" }, | ||
{ "path": "../i18n" }, | ||
{ "path": "../icons" }, | ||
{ "path": "../keycodes" }, | ||
{ "path": "../notices" }, | ||
{ "path": "../plugins" }, | ||
{ "path": "../private-apis" }, | ||
{ "path": "../rich-text" }, | ||
{ "path": "../url" }, | ||
{ "path": "../warning" }, | ||
{ "path": "../wordcount" } | ||
], | ||
"include": [ "src/**/*.ts", "src/**/*.tsx" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters