Skip to content

Commit 86aac7d

Browse files
🤖 Merge PR DefinitelyTyped#74262 [@wordpress/block-editor] Add definitions for useDispatch, useSelect, and useBlockEditingMode by @Toberumono
Co-authored-by: Joshua Lipstone <[email protected]>
1 parent 61e5947 commit 86aac7d

File tree

4 files changed

+170
-110
lines changed

4 files changed

+170
-110
lines changed

‎types/wordpress__block-editor/components/index.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@ export { default as BlockEditorProvider } from "./provider";
6464
*/
6565
export { useBlockBindingsUtils } from "./use-block-bindings-utils";
6666
export { useBlockEditContext } from "./use-block-edit-context";
67+
export { useBlockEditingMode } from "./use-block-editing-mode";
6768
export { useBlockProps } from "./use-block-props";
6869
export { useSettings } from "./use-settings";
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export namespace useBlockEditingMode {
2+
type BlockEditingMode = "disabled" | "contentOnly" | "default";
3+
}
4+
5+
/**
6+
* Allows a block to restrict the user interface that is displayed for editing
7+
* that block and its inner blocks.
8+
*
9+
* @example
10+
* ```js
11+
* function MyBlock( { attributes, setAttributes } ) {
12+
* useBlockEditingMode( 'disabled' );
13+
* return <div { ...useBlockProps() }></div>;
14+
* }
15+
* ```
16+
*
17+
* `mode` can be one of three options:
18+
*
19+
* - `'disabled'`: Prevents editing the block entirely, i.e. it cannot be
20+
* selected.
21+
* - `'contentOnly'`: Hides all non-content UI, e.g. auxiliary controls in the
22+
* toolbar, the block movers, block settings.
23+
* - `'default'`: Allows editing the block as normal.
24+
*
25+
* The mode is inherited by all of the block's inner blocks, unless they have
26+
* their own mode.
27+
*
28+
* If called outside of a block context, the mode is applied to all blocks.
29+
*
30+
* @param {?useBlockEditingMode.BlockEditingMode} mode The editing mode to apply. If undefined, the
31+
* current editing mode is not changed.
32+
*
33+
* @return {useBlockEditingMode.BlockEditingMode} The current editing mode.
34+
*/
35+
export function useBlockEditingMode(mode?: useBlockEditingMode.BlockEditingMode): useBlockEditingMode.BlockEditingMode;

‎types/wordpress__block-editor/index.d.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export * from "./utils";
1010
declare module "@wordpress/data" {
1111
function dispatch(key: "core/block-editor"): typeof import("./store/actions");
1212
function select(key: "core/block-editor"): typeof import("./store/selectors");
13+
14+
function useDispatch(key: "core/block-editor"): typeof import("./store/actions");
15+
function useSelect(key: "core/block-editor"): typeof import("./store/selectors");
1316
}
1417

1518
export interface BlockEditorStoreDescriptor extends StoreDescriptor {

‎types/wordpress__block-editor/wordpress__block-editor-tests.tsx‎

Lines changed: 131 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import be from "@wordpress/block-editor";
22
import * as UseBlockProps from "@wordpress/block-editor/components/use-block-props";
33
import { BlockInstance, createBlock } from "@wordpress/blocks";
4-
import { dispatch, select } from "@wordpress/data";
4+
import { dispatch, select, useDispatch, useSelect } from "@wordpress/data";
55
import { useRef } from "react";
66

77
declare const BLOCK_INSTANCE: BlockInstance;
@@ -464,132 +464,150 @@ be.transformStyles(STYLES, ".foobar");
464464
// $ExpectType BlockEditorStoreDescriptor
465465
be.store;
466466

467-
// $ExpectType void
468-
dispatch("core/block-editor").insertBlock(BLOCK_INSTANCE);
469-
dispatch("core/block-editor").insertBlock(BLOCK_INSTANCE, 4);
470-
dispatch("core/block-editor").insertBlock(BLOCK_INSTANCE, 4, "foo");
471-
dispatch("core/block-editor").insertBlock(BLOCK_INSTANCE, 4, "foo", false);
472-
473-
// $ExpectType IterableIterator<void>
474-
dispatch("core/block-editor").insertBlocks([BLOCK_INSTANCE]);
475-
dispatch("core/block-editor").insertBlocks([BLOCK_INSTANCE], 5);
476-
dispatch("core/block-editor").insertBlocks([BLOCK_INSTANCE], 5, "foo");
477-
dispatch("core/block-editor").insertBlocks([BLOCK_INSTANCE], 5, "foo", false);
478-
479-
// $ExpectType void
480-
dispatch("core/block-editor").insertDefaultBlock();
481-
dispatch("core/block-editor").insertDefaultBlock({ foo: "bar" });
482-
dispatch("core/block-editor").insertDefaultBlock({ foo: "bar" }, "foo");
483-
dispatch("core/block-editor").insertDefaultBlock({ foo: "bar" }, "foo", 5);
484-
485-
// $ExpectType void
486-
dispatch("core/block-editor").mergeBlocks("foo", "bar");
487-
488-
// $ExpectType void
489-
dispatch("core/block-editor").moveBlocksUp("foo", "bar");
490-
dispatch("core/block-editor").moveBlocksUp(["foo", "baz"], "bar");
491-
492-
// $ExpectType IterableIterator<void>
493-
dispatch("core/block-editor").moveBlockToPosition("foo", "bar", "baz", 1);
494-
dispatch("core/block-editor").moveBlockToPosition(undefined, "foo", undefined, 5);
495-
dispatch("core/block-editor").moveBlockToPosition(undefined, undefined, undefined, 5);
496-
497-
// $ExpectType void
498-
dispatch("core/block-editor").multiSelect("foo", "bar");
499-
500-
// $ExpectType void
501-
dispatch("core/block-editor").receiveBlocks([BLOCK_INSTANCE]);
502-
503-
// $ExpectType void
504-
dispatch("core/block-editor").removeBlock("foo");
505-
dispatch("core/block-editor").removeBlock("foo", true);
506-
507-
// $ExpectType IterableIterator<void>
508-
dispatch("core/block-editor").removeBlocks("foo");
509-
dispatch("core/block-editor").removeBlocks("foo", false);
510-
dispatch("core/block-editor").removeBlocks(["foo"]);
511-
dispatch("core/block-editor").removeBlocks(["foo"], false);
512-
513-
// $ExpectType void
514-
dispatch("core/block-editor").replaceBlock("foo", BLOCK_INSTANCE);
515-
dispatch("core/block-editor").replaceBlock("foo", [BLOCK_INSTANCE]);
516-
dispatch("core/block-editor").replaceBlock(["foo"], BLOCK_INSTANCE);
517-
dispatch("core/block-editor").replaceBlock(["foo"], [BLOCK_INSTANCE]);
518-
519-
// $ExpectType IterableIterator<void>
520-
dispatch("core/block-editor").replaceBlocks("foo", BLOCK_INSTANCE);
521-
dispatch("core/block-editor").replaceBlocks("foo", [BLOCK_INSTANCE], 3);
522-
dispatch("core/block-editor").replaceBlocks(["foo"], BLOCK_INSTANCE);
523-
dispatch("core/block-editor").replaceBlocks(["foo"], [BLOCK_INSTANCE], 0);
524-
525-
// $ExpectType void
526-
dispatch("core/block-editor").replaceInnerBlocks("foo", [BLOCK_INSTANCE]);
527-
dispatch("core/block-editor").replaceInnerBlocks("foo", [BLOCK_INSTANCE], true);
528-
529-
// $ExpectType void
530-
dispatch("core/block-editor").resetBlocks([BLOCK_INSTANCE]);
531-
532-
// $ExpectType void
533-
dispatch("core/block-editor").selectBlock("foo");
534-
dispatch("core/block-editor").selectBlock("foo", 5);
535-
536-
// $ExpectType void
537-
dispatch("core/block-editor").selectionChange("foo", "bar", 0, 5);
538-
539-
// $ExpectType IterableIterator<void>
540-
dispatch("core/block-editor").selectNextBlock("foo");
541-
542-
// $ExpectType IterableIterator<void>
543-
dispatch("core/block-editor").selectPreviousBlock("foo");
544-
545-
// $ExpectType void
546-
dispatch("core/block-editor").setTemplateValidity(false);
547-
548-
// $ExpectType void
549-
dispatch("core/block-editor").showInsertionPoint();
550-
dispatch("core/block-editor").showInsertionPoint("foo");
551-
dispatch("core/block-editor").showInsertionPoint("foo", 5);
552-
553-
// $ExpectType void
554-
dispatch("core/block-editor").toggleBlockMode("foo");
555-
556-
// $ExpectType void
557-
dispatch("core/block-editor").toggleSelection();
558-
dispatch("core/block-editor").toggleSelection(true);
559-
560-
// $ExpectType void
561-
dispatch("core/block-editor").updateBlock("foo", { attributes: { foo: "bar" }, innerBlocks: [] });
562-
563-
// $ExpectType void
564-
dispatch("core/block-editor").updateBlockAttributes("foo", { foo: "bar" });
565-
566-
// $ExpectType void
567-
dispatch("core/block-editor").updateBlockListSettings("foo", { allowedBlocks: ["core/paragraph"] });
568-
569-
// $ExpectType void
570-
dispatch("core/block-editor").updateSettings({
571-
focusMode: true,
572-
codeEditingEnabled: false,
573-
maxUploadFileSize: 500,
574-
richEditingEnabled: false,
575-
});
467+
for (const dispatchOrUseDispatch of [dispatch, useDispatch]) {
468+
// $ExpectType void
469+
dispatchOrUseDispatch("core/block-editor").insertBlock(BLOCK_INSTANCE);
470+
dispatchOrUseDispatch("core/block-editor").insertBlock(BLOCK_INSTANCE, 4);
471+
dispatchOrUseDispatch("core/block-editor").insertBlock(BLOCK_INSTANCE, 4, "foo");
472+
dispatchOrUseDispatch("core/block-editor").insertBlock(BLOCK_INSTANCE, 4, "foo", false);
473+
474+
// $ExpectType IterableIterator<void>
475+
dispatchOrUseDispatch("core/block-editor").insertBlocks([BLOCK_INSTANCE]);
476+
dispatchOrUseDispatch("core/block-editor").insertBlocks([BLOCK_INSTANCE], 5);
477+
dispatchOrUseDispatch("core/block-editor").insertBlocks([BLOCK_INSTANCE], 5, "foo");
478+
dispatchOrUseDispatch("core/block-editor").insertBlocks([BLOCK_INSTANCE], 5, "foo", false);
479+
480+
// $ExpectType void
481+
dispatchOrUseDispatch("core/block-editor").insertDefaultBlock();
482+
dispatchOrUseDispatch("core/block-editor").insertDefaultBlock({ foo: "bar" });
483+
dispatchOrUseDispatch("core/block-editor").insertDefaultBlock({ foo: "bar" }, "foo");
484+
dispatchOrUseDispatch("core/block-editor").insertDefaultBlock({ foo: "bar" }, "foo", 5);
485+
486+
// $ExpectType void
487+
dispatchOrUseDispatch("core/block-editor").mergeBlocks("foo", "bar");
488+
489+
// $ExpectType void
490+
dispatchOrUseDispatch("core/block-editor").moveBlocksUp("foo", "bar");
491+
dispatchOrUseDispatch("core/block-editor").moveBlocksUp(["foo", "baz"], "bar");
492+
493+
// $ExpectType IterableIterator<void>
494+
dispatchOrUseDispatch("core/block-editor").moveBlockToPosition("foo", "bar", "baz", 1);
495+
dispatchOrUseDispatch("core/block-editor").moveBlockToPosition(undefined, "foo", undefined, 5);
496+
dispatchOrUseDispatch("core/block-editor").moveBlockToPosition(undefined, undefined, undefined, 5);
497+
498+
// $ExpectType void
499+
dispatchOrUseDispatch("core/block-editor").multiSelect("foo", "bar");
500+
501+
// $ExpectType void
502+
dispatchOrUseDispatch("core/block-editor").receiveBlocks([BLOCK_INSTANCE]);
503+
504+
// $ExpectType void
505+
dispatchOrUseDispatch("core/block-editor").removeBlock("foo");
506+
dispatchOrUseDispatch("core/block-editor").removeBlock("foo", true);
507+
508+
// $ExpectType IterableIterator<void>
509+
dispatchOrUseDispatch("core/block-editor").removeBlocks("foo");
510+
dispatchOrUseDispatch("core/block-editor").removeBlocks("foo", false);
511+
dispatchOrUseDispatch("core/block-editor").removeBlocks(["foo"]);
512+
dispatchOrUseDispatch("core/block-editor").removeBlocks(["foo"], false);
513+
514+
// $ExpectType void
515+
dispatchOrUseDispatch("core/block-editor").replaceBlock("foo", BLOCK_INSTANCE);
516+
dispatchOrUseDispatch("core/block-editor").replaceBlock("foo", [BLOCK_INSTANCE]);
517+
dispatchOrUseDispatch("core/block-editor").replaceBlock(["foo"], BLOCK_INSTANCE);
518+
dispatchOrUseDispatch("core/block-editor").replaceBlock(["foo"], [BLOCK_INSTANCE]);
519+
520+
// $ExpectType IterableIterator<void>
521+
dispatchOrUseDispatch("core/block-editor").replaceBlocks("foo", BLOCK_INSTANCE);
522+
dispatchOrUseDispatch("core/block-editor").replaceBlocks("foo", [BLOCK_INSTANCE], 3);
523+
dispatchOrUseDispatch("core/block-editor").replaceBlocks(["foo"], BLOCK_INSTANCE);
524+
dispatchOrUseDispatch("core/block-editor").replaceBlocks(["foo"], [BLOCK_INSTANCE], 0);
525+
526+
// $ExpectType void
527+
dispatchOrUseDispatch("core/block-editor").replaceInnerBlocks("foo", [BLOCK_INSTANCE]);
528+
dispatchOrUseDispatch("core/block-editor").replaceInnerBlocks("foo", [BLOCK_INSTANCE], true);
529+
530+
// $ExpectType void
531+
dispatchOrUseDispatch("core/block-editor").resetBlocks([BLOCK_INSTANCE]);
532+
533+
// $ExpectType void
534+
dispatchOrUseDispatch("core/block-editor").selectBlock("foo");
535+
dispatchOrUseDispatch("core/block-editor").selectBlock("foo", 5);
536+
537+
// $ExpectType void
538+
dispatchOrUseDispatch("core/block-editor").selectionChange("foo", "bar", 0, 5);
539+
540+
// $ExpectType IterableIterator<void>
541+
dispatchOrUseDispatch("core/block-editor").selectNextBlock("foo");
542+
543+
// $ExpectType IterableIterator<void>
544+
dispatchOrUseDispatch("core/block-editor").selectPreviousBlock("foo");
545+
546+
// $ExpectType void
547+
dispatchOrUseDispatch("core/block-editor").setTemplateValidity(false);
548+
549+
// $ExpectType void
550+
dispatchOrUseDispatch("core/block-editor").showInsertionPoint();
551+
dispatchOrUseDispatch("core/block-editor").showInsertionPoint("foo");
552+
dispatchOrUseDispatch("core/block-editor").showInsertionPoint("foo", 5);
553+
554+
// $ExpectType void
555+
dispatchOrUseDispatch("core/block-editor").toggleBlockMode("foo");
556+
557+
// $ExpectType void
558+
dispatchOrUseDispatch("core/block-editor").toggleSelection();
559+
dispatchOrUseDispatch("core/block-editor").toggleSelection(true);
560+
561+
// $ExpectType void
562+
dispatchOrUseDispatch("core/block-editor").updateBlock("foo", { attributes: { foo: "bar" }, innerBlocks: [] });
563+
564+
// $ExpectType void
565+
dispatchOrUseDispatch("core/block-editor").updateBlockAttributes("foo", { foo: "bar" });
566+
567+
// $ExpectType void
568+
dispatchOrUseDispatch("core/block-editor").updateBlockListSettings("foo", { allowedBlocks: ["core/paragraph"] });
569+
570+
// $ExpectType void
571+
dispatchOrUseDispatch("core/block-editor").updateSettings({
572+
focusMode: true,
573+
codeEditingEnabled: false,
574+
maxUploadFileSize: 500,
575+
richEditingEnabled: false,
576+
});
577+
}
576578

577579
// $ExpectType boolean
578580
select("core/block-editor").canInsertBlockType("core/paragraph");
579581
select("core/block-editor").canInsertBlockType("core/paragraph", "foo");
580582

583+
// $ExpectType boolean
584+
useSelect("core/block-editor").canInsertBlockType("core/paragraph");
585+
useSelect("core/block-editor").canInsertBlockType("core/paragraph", "foo");
586+
581587
// $ExpectType string | null
582588
select("core/block-editor").getAdjacentBlockClientId();
583589
select("core/block-editor").getAdjacentBlockClientId("foo");
584590
select("core/block-editor").getAdjacentBlockClientId("foo", -1);
585591
select("core/block-editor").getAdjacentBlockClientId("foo", 1);
586592

593+
// $ExpectType string | null
594+
useSelect("core/block-editor").getAdjacentBlockClientId();
595+
useSelect("core/block-editor").getAdjacentBlockClientId("foo");
596+
useSelect("core/block-editor").getAdjacentBlockClientId("foo", -1);
597+
useSelect("core/block-editor").getAdjacentBlockClientId("foo", 1);
598+
587599
// $ExpectType string[]
588600
select("core/block-editor").getBlockParents("foo");
589601
select("core/block-editor").getBlockParentsByBlockName("foo", ["core/query"]);
590602
select("core/block-editor").getBlockParents("foo", true);
591603
select("core/block-editor").getBlockParentsByBlockName("foo", ["core/query"], true);
592604

605+
// $ExpectType string[]
606+
useSelect("core/block-editor").getBlockParents("foo");
607+
useSelect("core/block-editor").getBlockParentsByBlockName("foo", ["core/query"]);
608+
useSelect("core/block-editor").getBlockParents("foo", true);
609+
useSelect("core/block-editor").getBlockParentsByBlockName("foo", ["core/query"], true);
610+
593611
{
594612
const blockProps: UseBlockProps.Merged & UseBlockProps.Reserved = be.useBlockProps();
595613
blockProps;
@@ -672,3 +690,6 @@ be.useBlockBindingsUtils();
672690

673691
// $ExpectType { name: string; isSelected?: boolean | undefined; clientId: string; layout: unknown; }
674692
be.useBlockEditContext();
693+
694+
// $ExpectType BlockEditingMode
695+
be.useBlockEditingMode();

0 commit comments

Comments
 (0)