@wordpress/editor 14.28.0 → 14.28.1-next.0f6f9d12c.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/components/document-bar/index.js +1 -1
- package/build/components/document-bar/index.js.map +1 -1
- package/build/hooks/index.js +1 -0
- package/build/hooks/index.js.map +1 -1
- package/build/hooks/navigation-link-view-button.js +76 -0
- package/build/hooks/navigation-link-view-button.js.map +1 -0
- package/build-module/components/document-bar/index.js +1 -1
- package/build-module/components/document-bar/index.js.map +1 -1
- package/build-module/hooks/index.js +1 -0
- package/build-module/hooks/index.js.map +1 -1
- package/build-module/hooks/navigation-link-view-button.js +74 -0
- package/build-module/hooks/navigation-link-view-button.js.map +1 -0
- package/build-style/style-rtl.css +2 -1
- package/build-style/style.css +2 -1
- package/build-types/hooks/navigation-link-view-button.d.ts +2 -0
- package/build-types/hooks/navigation-link-view-button.d.ts.map +1 -0
- package/package.json +37 -37
- package/src/components/document-bar/index.js +1 -1
- package/src/components/post-url/style.scss +2 -1
- package/src/hooks/index.js +1 -0
- package/src/hooks/navigation-link-view-button.js +100 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { addFilter } from '@wordpress/hooks';
|
|
5
|
+
import { createHigherOrderComponent } from '@wordpress/compose';
|
|
6
|
+
import { useCallback } from '@wordpress/element';
|
|
7
|
+
import { __ } from '@wordpress/i18n';
|
|
8
|
+
import {
|
|
9
|
+
__unstableBlockToolbarLastItem as BlockToolbarLastItem,
|
|
10
|
+
store as blockEditorStore,
|
|
11
|
+
useBlockEditingMode,
|
|
12
|
+
} from '@wordpress/block-editor';
|
|
13
|
+
import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
|
|
14
|
+
import { useSelect } from '@wordpress/data';
|
|
15
|
+
|
|
16
|
+
// Target blocks that should have the View button.
|
|
17
|
+
const SUPPORTED_BLOCKS = [ 'core/navigation-link', 'core/navigation-submenu' ];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Component that renders the View button for navigation blocks.
|
|
21
|
+
*
|
|
22
|
+
* @param {Object} props Component props.
|
|
23
|
+
* @param {Object} props.attributes Block attributes.
|
|
24
|
+
* @return {JSX.Element|null} The View button component or null if not applicable.
|
|
25
|
+
*/
|
|
26
|
+
function NavigationViewButton( { attributes } ) {
|
|
27
|
+
const { kind, id, type } = attributes;
|
|
28
|
+
const blockEditingMode = useBlockEditingMode();
|
|
29
|
+
|
|
30
|
+
const onNavigateToEntityRecord = useSelect(
|
|
31
|
+
( select ) =>
|
|
32
|
+
select( blockEditorStore ).getSettings().onNavigateToEntityRecord,
|
|
33
|
+
[]
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const onViewPage = useCallback( () => {
|
|
37
|
+
if (
|
|
38
|
+
kind === 'post-type' &&
|
|
39
|
+
type === 'page' &&
|
|
40
|
+
id &&
|
|
41
|
+
onNavigateToEntityRecord
|
|
42
|
+
) {
|
|
43
|
+
onNavigateToEntityRecord( {
|
|
44
|
+
postId: id,
|
|
45
|
+
postType: type,
|
|
46
|
+
} );
|
|
47
|
+
}
|
|
48
|
+
}, [ kind, id, type, onNavigateToEntityRecord ] );
|
|
49
|
+
|
|
50
|
+
// Only show for page-type links, when navigation is available, and when in contentOnly mode.
|
|
51
|
+
if (
|
|
52
|
+
kind !== 'post-type' ||
|
|
53
|
+
type !== 'page' ||
|
|
54
|
+
! id ||
|
|
55
|
+
! onNavigateToEntityRecord ||
|
|
56
|
+
blockEditingMode !== 'contentOnly'
|
|
57
|
+
) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<BlockToolbarLastItem>
|
|
63
|
+
<ToolbarGroup>
|
|
64
|
+
<ToolbarButton
|
|
65
|
+
name="view"
|
|
66
|
+
title={ __( 'View' ) }
|
|
67
|
+
onClick={ onViewPage }
|
|
68
|
+
>
|
|
69
|
+
{ __( 'View' ) }
|
|
70
|
+
</ToolbarButton>
|
|
71
|
+
</ToolbarGroup>
|
|
72
|
+
</BlockToolbarLastItem>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Higher-order component that adds the View button to navigation blocks.
|
|
78
|
+
*/
|
|
79
|
+
const withNavigationViewButton = createHigherOrderComponent(
|
|
80
|
+
( BlockEdit ) => ( props ) => {
|
|
81
|
+
const isSupportedBlock = SUPPORTED_BLOCKS.includes( props.name );
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<>
|
|
85
|
+
<BlockEdit key="edit" { ...props } />
|
|
86
|
+
{ props.isSelected && isSupportedBlock && (
|
|
87
|
+
<NavigationViewButton { ...props } />
|
|
88
|
+
) }
|
|
89
|
+
</>
|
|
90
|
+
);
|
|
91
|
+
},
|
|
92
|
+
'withNavigationViewButton'
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
// Register the filter.
|
|
96
|
+
addFilter(
|
|
97
|
+
'editor.BlockEdit',
|
|
98
|
+
'core/editor/with-navigation-view-button',
|
|
99
|
+
withNavigationViewButton
|
|
100
|
+
);
|