@wordpress/boot 0.19.0 → 0.21.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.
Files changed (174) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +4 -0
  3. package/build-module/components/app/index.mjs.map +2 -2
  4. package/build-module/components/app/router.mjs +100 -4
  5. package/build-module/components/app/router.mjs.map +3 -3
  6. package/build-module/components/app/use-route-title.mjs.map +2 -2
  7. package/build-module/components/canvas/back-button.mjs.map +2 -2
  8. package/build-module/components/canvas/index.mjs +72 -13
  9. package/build-module/components/canvas/index.mjs.map +2 -2
  10. package/build-module/components/canvas/site-preview.mjs +50 -0
  11. package/build-module/components/canvas/site-preview.mjs.map +7 -0
  12. package/build-module/components/canvas/use-navigate-to-entity-record.mjs +122 -0
  13. package/build-module/components/canvas/use-navigate-to-entity-record.mjs.map +7 -0
  14. package/build-module/components/canvas/viewport.mjs +18 -0
  15. package/build-module/components/canvas/viewport.mjs.map +7 -0
  16. package/build-module/components/canvas-renderer/index.mjs.map +2 -2
  17. package/build-module/components/error-boundary/index.mjs +129 -0
  18. package/build-module/components/error-boundary/index.mjs.map +7 -0
  19. package/build-module/components/navigation/drilldown-item/index.mjs +91 -1
  20. package/build-module/components/navigation/drilldown-item/index.mjs.map +3 -3
  21. package/build-module/components/navigation/dropdown-item/index.mjs +99 -12
  22. package/build-module/components/navigation/dropdown-item/index.mjs.map +4 -4
  23. package/build-module/components/navigation/index.mjs +1 -2
  24. package/build-module/components/navigation/index.mjs.map +2 -2
  25. package/build-module/components/navigation/items.mjs.map +2 -2
  26. package/build-module/components/navigation/navigation-item/index.mjs +88 -15
  27. package/build-module/components/navigation/navigation-item/index.mjs.map +3 -3
  28. package/build-module/components/navigation/navigation-screen/index.mjs +111 -38
  29. package/build-module/components/navigation/navigation-screen/index.mjs.map +3 -3
  30. package/build-module/components/navigation/path-matching.mjs.map +2 -2
  31. package/build-module/components/navigation/router-link-item.mjs.map +2 -2
  32. package/build-module/components/navigation/use-sidebar-parent.mjs +1 -1
  33. package/build-module/components/navigation/use-sidebar-parent.mjs.map +2 -2
  34. package/build-module/components/plugin-area/index.mjs +29 -0
  35. package/build-module/components/plugin-area/index.mjs.map +7 -0
  36. package/build-module/components/root/index.mjs +220 -122
  37. package/build-module/components/root/index.mjs.map +3 -3
  38. package/build-module/components/root/single-page.mjs +139 -47
  39. package/build-module/components/root/single-page.mjs.map +3 -3
  40. package/build-module/components/root/use-sync-body-background.mjs.map +2 -2
  41. package/build-module/components/save-button/index.mjs +127 -16
  42. package/build-module/components/save-button/index.mjs.map +3 -3
  43. package/build-module/components/save-panel/index.mjs +91 -1
  44. package/build-module/components/save-panel/index.mjs.map +3 -3
  45. package/build-module/components/save-panel/use-save-shortcut.mjs.map +2 -2
  46. package/build-module/components/sidebar/index.mjs +90 -10
  47. package/build-module/components/sidebar/index.mjs.map +3 -3
  48. package/build-module/index.mjs +0 -16
  49. package/build-module/index.mjs.map +3 -3
  50. package/build-module/lock-unlock.mjs.map +2 -2
  51. package/build-module/store/actions.mjs +8 -0
  52. package/build-module/store/actions.mjs.map +2 -2
  53. package/build-module/store/index.mjs.map +2 -2
  54. package/build-module/store/reducer.mjs +16 -1
  55. package/build-module/store/reducer.mjs.map +2 -2
  56. package/build-module/store/selectors.mjs +9 -0
  57. package/build-module/store/selectors.mjs.map +2 -2
  58. package/build-types/components/app/index.d.ts.map +1 -1
  59. package/build-types/components/app/router.d.ts +0 -3
  60. package/build-types/components/app/router.d.ts.map +1 -1
  61. package/build-types/components/app/use-route-title.d.ts.map +1 -1
  62. package/build-types/components/canvas/back-button.d.ts.map +1 -1
  63. package/build-types/components/canvas/index.d.ts +0 -3
  64. package/build-types/components/canvas/index.d.ts.map +1 -1
  65. package/build-types/components/canvas/site-preview.d.ts +10 -0
  66. package/build-types/components/canvas/site-preview.d.ts.map +1 -0
  67. package/build-types/components/canvas/use-navigate-to-entity-record.d.ts +40 -0
  68. package/build-types/components/canvas/use-navigate-to-entity-record.d.ts.map +1 -0
  69. package/build-types/components/canvas/viewport.d.ts +15 -0
  70. package/build-types/components/canvas/viewport.d.ts.map +1 -0
  71. package/build-types/components/canvas-renderer/index.d.ts.map +1 -1
  72. package/build-types/components/error-boundary/index.d.ts +22 -0
  73. package/build-types/components/error-boundary/index.d.ts.map +1 -0
  74. package/build-types/components/navigation/drilldown-item/index.d.ts.map +1 -1
  75. package/build-types/components/navigation/dropdown-item/index.d.ts +0 -1
  76. package/build-types/components/navigation/dropdown-item/index.d.ts.map +1 -1
  77. package/build-types/components/navigation/index.d.ts.map +1 -1
  78. package/build-types/components/navigation/items.d.ts +0 -3
  79. package/build-types/components/navigation/items.d.ts.map +1 -1
  80. package/build-types/components/navigation/navigation-item/index.d.ts +0 -1
  81. package/build-types/components/navigation/navigation-item/index.d.ts.map +1 -1
  82. package/build-types/components/navigation/navigation-screen/index.d.ts +0 -7
  83. package/build-types/components/navigation/navigation-screen/index.d.ts.map +1 -1
  84. package/build-types/components/navigation/path-matching.d.ts +0 -3
  85. package/build-types/components/navigation/path-matching.d.ts.map +1 -1
  86. package/build-types/components/navigation/router-link-item.d.ts.map +1 -1
  87. package/build-types/components/navigation/use-sidebar-parent.d.ts.map +1 -1
  88. package/build-types/components/plugin-area/index.d.ts +12 -0
  89. package/build-types/components/plugin-area/index.d.ts.map +1 -0
  90. package/build-types/components/root/index.d.ts +0 -1
  91. package/build-types/components/root/index.d.ts.map +1 -1
  92. package/build-types/components/root/single-page.d.ts +0 -1
  93. package/build-types/components/root/single-page.d.ts.map +1 -1
  94. package/build-types/components/root/use-sync-body-background.d.ts +1 -1
  95. package/build-types/components/root/use-sync-body-background.d.ts.map +1 -1
  96. package/build-types/components/save-button/index.d.ts +0 -4
  97. package/build-types/components/save-button/index.d.ts.map +1 -1
  98. package/build-types/components/save-panel/index.d.ts.map +1 -1
  99. package/build-types/components/save-panel/use-save-shortcut.d.ts.map +1 -1
  100. package/build-types/components/sidebar/index.d.ts +0 -1
  101. package/build-types/components/sidebar/index.d.ts.map +1 -1
  102. package/build-types/index.d.ts +0 -5
  103. package/build-types/index.d.ts.map +1 -1
  104. package/build-types/lock-unlock.d.ts.map +1 -1
  105. package/build-types/store/actions.d.ts +15 -5
  106. package/build-types/store/actions.d.ts.map +1 -1
  107. package/build-types/store/index.d.ts.map +1 -1
  108. package/build-types/store/reducer.d.ts +0 -3
  109. package/build-types/store/reducer.d.ts.map +1 -1
  110. package/build-types/store/selectors.d.ts +12 -3
  111. package/build-types/store/selectors.d.ts.map +1 -1
  112. package/build-types/store/types.d.ts +34 -6
  113. package/build-types/store/types.d.ts.map +1 -1
  114. package/package.json +28 -25
  115. package/src/components/app/index.tsx +0 -7
  116. package/src/components/app/router.tsx +19 -18
  117. package/src/components/app/use-route-title.ts +0 -7
  118. package/src/components/canvas/back-button.tsx +0 -3
  119. package/src/components/canvas/index.tsx +117 -22
  120. package/src/components/canvas/site-preview.tsx +64 -0
  121. package/src/components/canvas/use-navigate-to-entity-record.ts +220 -0
  122. package/src/components/canvas/viewport.ts +34 -0
  123. package/src/components/canvas-renderer/index.tsx +0 -7
  124. package/src/components/error-boundary/index.tsx +71 -0
  125. package/src/components/error-boundary/style.module.scss +13 -0
  126. package/src/components/navigation/drilldown-item/index.tsx +2 -13
  127. package/src/components/navigation/dropdown-item/index.tsx +11 -19
  128. package/src/components/navigation/dropdown-item/{style.scss → style.module.scss} +6 -2
  129. package/src/components/navigation/index.tsx +1 -9
  130. package/src/components/navigation/items.tsx +0 -7
  131. package/src/components/navigation/navigation-item/index.tsx +3 -18
  132. package/src/components/navigation/navigation-item/{style.scss → style.module.scss} +7 -14
  133. package/src/components/navigation/navigation-screen/index.tsx +6 -26
  134. package/src/components/navigation/navigation-screen/{style.scss → style.module.scss} +10 -9
  135. package/src/components/navigation/path-matching.ts +0 -3
  136. package/src/components/navigation/router-link-item.tsx +0 -11
  137. package/src/components/navigation/use-sidebar-parent.ts +1 -13
  138. package/src/components/plugin-area/index.tsx +34 -0
  139. package/src/components/root/{style.scss → _layout.module.scss} +71 -40
  140. package/src/components/root/_view-transitions.module.scss +205 -0
  141. package/src/components/root/index.tsx +44 -35
  142. package/src/components/root/single-page.tsx +15 -18
  143. package/src/components/root/style.module.scss +2 -0
  144. package/src/components/root/use-sync-body-background.ts +1 -4
  145. package/src/components/save-button/index.tsx +36 -21
  146. package/src/components/save-button/style.module.scss +10 -0
  147. package/src/components/save-panel/index.tsx +2 -8
  148. package/src/components/save-panel/style.module.scss +7 -0
  149. package/src/components/save-panel/use-save-shortcut.ts +0 -3
  150. package/src/components/sidebar/index.tsx +5 -12
  151. package/src/components/sidebar/style.module.scss +29 -0
  152. package/src/index.tsx +0 -5
  153. package/src/lock-unlock.ts +0 -3
  154. package/src/store/actions.ts +18 -4
  155. package/src/store/index.ts +0 -7
  156. package/src/store/reducer.ts +19 -3
  157. package/src/store/selectors.ts +28 -3
  158. package/src/store/types.ts +37 -6
  159. package/build-style/style-rtl.css +0 -132
  160. package/build-style/style.css +0 -132
  161. package/build-style/view-transitions-rtl.css +0 -189
  162. package/build-style/view-transitions.css +0 -189
  163. package/build-types/components/site-hub/index.d.ts +0 -4
  164. package/build-types/components/site-hub/index.d.ts.map +0 -1
  165. package/build-types/components/site-icon/index.d.ts +0 -9
  166. package/build-types/components/site-icon/index.d.ts.map +0 -1
  167. package/build-types/components/site-icon-link/index.d.ts +0 -8
  168. package/build-types/components/site-icon-link/index.d.ts.map +0 -1
  169. package/build-types/components/user-theme-provider/index.d.ts +0 -6
  170. package/build-types/components/user-theme-provider/index.d.ts.map +0 -1
  171. package/src/components/save-button/style.scss +0 -9
  172. package/src/components/sidebar/style.scss +0 -23
  173. package/src/style.scss +0 -33
  174. package/src/view-transitions.scss +0 -221
@@ -1,11 +1,4 @@
1
- /**
2
- * External dependencies
3
- */
4
1
  import type { ComponentType } from 'react';
5
-
6
- /**
7
- * WordPress dependencies
8
- */
9
2
  import { __ } from '@wordpress/i18n';
10
3
  import { useMemo } from '@wordpress/element';
11
4
  import { Page } from '@wordpress/admin-ui';
@@ -15,11 +8,9 @@ import {
15
8
  } from '@wordpress/route';
16
9
  import { resolveSelect } from '@wordpress/data';
17
10
  import { store as coreStore } from '@wordpress/core-data';
18
-
19
- /**
20
- * Internal dependencies
21
- */
22
11
  import Root from '../root';
12
+ import styles from '../root/style.module.scss';
13
+ import ErrorBoundary from '../error-boundary';
23
14
  import type { Route, RouteConfig, RouteLoaderContext } from '../../store/types';
24
15
  import { unlock } from '../../lock-unlock';
25
16
 
@@ -37,7 +28,7 @@ const {
37
28
  // Not found component
38
29
  function NotFoundComponent() {
39
30
  return (
40
- <div className="boot-layout__stage">
31
+ <div className={ styles.stage }>
41
32
  <Page title={ __( 'Route not found' ) } hasPadding>
42
33
  { __( "The page you're looking for does not exist" ) }
43
34
  </Page>
@@ -105,10 +96,16 @@ function createRouteFromDefinition( route: Route, parentRoute: AnyRoute ) {
105
96
  inspector = await routeConfig.inspector( context );
106
97
  }
107
98
 
99
+ let stage = true;
100
+ if ( routeConfig.stage ) {
101
+ stage = await routeConfig.stage( context );
102
+ }
103
+
108
104
  return {
109
105
  ...( loaderData as any ),
110
106
  canvas: canvasData,
111
107
  inspector,
108
+ stage,
112
109
  title: titleData,
113
110
  routeContentModule: route.content_module,
114
111
  };
@@ -127,19 +124,23 @@ function createRouteFromDefinition( route: Route, parentRoute: AnyRoute ) {
127
124
 
128
125
  return createLazyRoute( route.path )( {
129
126
  component: function RouteComponent() {
130
- const { inspector: showInspector } =
127
+ const { inspector: showInspector, stage: showStage } =
131
128
  useLoaderData( { from: route.path } ) ?? {};
132
129
 
133
130
  return (
134
131
  <>
135
- { Stage && (
136
- <div className="boot-layout__stage">
137
- <Stage />
132
+ { Stage && showStage && (
133
+ <div className={ styles.stage }>
134
+ <ErrorBoundary>
135
+ <Stage />
136
+ </ErrorBoundary>
138
137
  </div>
139
138
  ) }
140
139
  { Inspector && showInspector && (
141
- <div className="boot-layout__inspector">
142
- <Inspector />
140
+ <div className={ styles.inspector }>
141
+ <ErrorBoundary>
142
+ <Inspector />
143
+ </ErrorBoundary>
143
144
  </div>
144
145
  ) }
145
146
  </>
@@ -1,6 +1,3 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
1
  import { useEffect, useRef } from '@wordpress/element';
5
2
  import { useSelect } from '@wordpress/data';
6
3
  import { store as coreStore, type UnstableBase } from '@wordpress/core-data';
@@ -8,10 +5,6 @@ import { __, sprintf } from '@wordpress/i18n';
8
5
  import { speak } from '@wordpress/a11y';
9
6
  import { decodeEntities } from '@wordpress/html-entities';
10
7
  import { privateApis as routePrivateApis } from '@wordpress/route';
11
-
12
- /**
13
- * Internal dependencies
14
- */
15
8
  import { unlock } from '../../lock-unlock';
16
9
 
17
10
  const { useLocation, useMatches } = unlock( routePrivateApis );
@@ -1,6 +1,3 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
1
  import { Button } from '@wordpress/components';
5
2
  import { chevronLeft, chevronRight } from '@wordpress/icons';
6
3
  import { __, isRTL } from '@wordpress/i18n';
@@ -1,15 +1,17 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { useState, useEffect } from '@wordpress/element';
1
+ import { useState, useEffect, useMemo } from '@wordpress/element';
5
2
  import { Spinner } from '@wordpress/components';
6
- import { useNavigate } from '@wordpress/route';
7
-
8
- /**
9
- * Internal dependencies
10
- */
3
+ import { useNavigate, useSearch } from '@wordpress/route';
4
+ import { __ } from '@wordpress/i18n';
5
+ import { useSelect } from '@wordpress/data';
6
+ import { store as coreStore } from '@wordpress/core-data';
7
+ import { store as bootStore } from '../../store';
11
8
  import type { CanvasData } from '../../store/types';
12
9
  import BootBackButton from './back-button';
10
+ import SitePreview from './site-preview';
11
+ import useNavigateToEntityRecord, {
12
+ useActionPerformed,
13
+ } from './use-navigate-to-entity-record';
14
+ import { getDeviceType } from './viewport';
13
15
 
14
16
  interface CanvasProps {
15
17
  canvas: CanvasData;
@@ -25,6 +27,88 @@ interface CanvasProps {
25
27
  export default function Canvas( { canvas }: CanvasProps ) {
26
28
  const [ Editor, setEditor ] = useState< any >( null );
27
29
  const navigate = useNavigate();
30
+ const { onNavigateToEntityRecord, onNavigateToPreviousEntityRecord } =
31
+ useNavigateToEntityRecord();
32
+ const onActionPerformed = useActionPerformed( canvas.postType );
33
+
34
+ /*
35
+ * An entity can be asked to be edited at a particular width — a navigation
36
+ * overlay meant for mobile — and carries the width it was left at when
37
+ * returned to. Anything else opens at the default.
38
+ */
39
+ const viewport = useSearch( {
40
+ strict: false,
41
+ select: ( search ) => ( search as { viewport?: string } ).viewport,
42
+ } ) as string | undefined;
43
+
44
+ /*
45
+ * Where clicking a previewed canvas goes, resolved the same way the editor
46
+ * resolves anywhere else it sends you to edit an entity, and whether it
47
+ * should go anywhere at all: a trashed entity has to be restored before it
48
+ * can be edited.
49
+ *
50
+ * The record is the one the editor loads for this canvas, so reading it
51
+ * here costs no request of its own.
52
+ */
53
+ /*
54
+ * A route that names no entity leaves the editor to resolve one, which it
55
+ * does from the block templates only a block theme has. `undefined` while
56
+ * the theme is still being read.
57
+ */
58
+ const hasEntity = !! ( canvas.postType && canvas.postId );
59
+ const isBlockTheme = useSelect(
60
+ ( select ) =>
61
+ (
62
+ select( coreStore ).getCurrentTheme() as
63
+ | { is_block_theme?: boolean }
64
+ | undefined
65
+ )?.is_block_theme,
66
+ []
67
+ );
68
+
69
+ const { editLink, isTrashed } = useSelect(
70
+ ( select ) => {
71
+ if ( ! canvas.postType || ! canvas.postId ) {
72
+ return { editLink: undefined, isTrashed: false };
73
+ }
74
+
75
+ const record = select( coreStore ).getEntityRecord(
76
+ 'postType',
77
+ canvas.postType,
78
+ canvas.postId
79
+ ) as { status?: string } | undefined;
80
+
81
+ return {
82
+ editLink: select( bootStore ).getEntityLink(
83
+ canvas.postType,
84
+ canvas.postId
85
+ ),
86
+ isTrashed: record?.status === 'trash',
87
+ };
88
+ },
89
+ [ canvas.postType, canvas.postId ]
90
+ );
91
+
92
+ /*
93
+ * Memoized because the editor provider pushes these settings into the store
94
+ * whenever their identity changes, and the callbacks have to stay stable for
95
+ * the block inspector to keep offering the same affordance.
96
+ */
97
+ const settings = useMemo(
98
+ () => ( {
99
+ isPreviewMode: canvas.isPreview,
100
+ styles: canvas.isPreview
101
+ ? [ { css: 'body{min-height:100vh;}' } ]
102
+ : [],
103
+ onNavigateToEntityRecord,
104
+ onNavigateToPreviousEntityRecord,
105
+ } ),
106
+ [
107
+ canvas.isPreview,
108
+ onNavigateToEntityRecord,
109
+ onNavigateToPreviousEntityRecord,
110
+ ]
111
+ );
28
112
 
29
113
  useEffect( () => {
30
114
  // Dynamically import the lazy-editor module
@@ -38,8 +122,14 @@ export default function Canvas( { canvas }: CanvasProps ) {
38
122
  } );
39
123
  }, [] );
40
124
 
41
- // Show spinner while loading the editor module
42
- if ( ! Editor ) {
125
+ // Nothing for the editor to open, so show the site the route configures.
126
+ if ( ! hasEntity && isBlockTheme === false ) {
127
+ return <SitePreview />;
128
+ }
129
+
130
+ // Show spinner while loading the editor module, and until it is known which
131
+ // of the two this canvas is, so the wrong one is never shown first.
132
+ if ( ! Editor || ( ! hasEntity && isBlockTheme === undefined ) ) {
43
133
  return (
44
134
  <div
45
135
  style={ {
@@ -74,33 +164,38 @@ export default function Canvas( { canvas }: CanvasProps ) {
74
164
  <Editor
75
165
  postType={ canvas.postType }
76
166
  postId={ canvas.postId }
77
- settings={ {
78
- isPreviewMode: canvas.isPreview,
79
- styles: canvas.isPreview
80
- ? [ { css: 'body{min-height:100vh;}' } ]
81
- : [],
82
- } }
167
+ settings={ settings }
83
168
  backButton={ backButton }
169
+ onActionPerformed={ onActionPerformed }
170
+ initialViewport={ getDeviceType( viewport ) }
84
171
  />
85
172
  </div>
86
- { canvas.isPreview && canvas.editLink && (
173
+ { canvas.isPreview && editLink && (
87
174
  <div
88
- onClick={ () => navigate( { to: canvas.editLink } ) }
175
+ onClick={
176
+ isTrashed
177
+ ? undefined
178
+ : () => navigate( { to: editLink } )
179
+ }
89
180
  onKeyDown={ ( e ) => {
181
+ if ( isTrashed ) {
182
+ return;
183
+ }
90
184
  if ( e.key === 'Enter' || e.key === ' ' ) {
91
185
  e.preventDefault();
92
- navigate( { to: canvas.editLink } );
186
+ navigate( { to: editLink } );
93
187
  }
94
188
  } }
95
189
  style={ {
96
190
  position: 'absolute',
97
191
  inset: 0,
98
- cursor: 'pointer',
192
+ cursor: isTrashed ? 'default' : 'pointer',
99
193
  zIndex: 1,
100
194
  } }
101
195
  role="button"
102
196
  tabIndex={ 0 }
103
- aria-label="Click to edit"
197
+ aria-disabled={ isTrashed }
198
+ aria-label={ __( 'Edit' ) }
104
199
  />
105
200
  ) }
106
201
  </div>
@@ -0,0 +1,64 @@
1
+ import { __ } from '@wordpress/i18n';
2
+ import { useSelect } from '@wordpress/data';
3
+ import { store as coreStore } from '@wordpress/core-data';
4
+ import { focus } from '@wordpress/dom';
5
+ import { addQueryArgs } from '@wordpress/url';
6
+
7
+ /**
8
+ * Renders the site's front end in an iframe.
9
+ *
10
+ * Shown where the canvas has no entity to edit, so that a route still says
11
+ * something about the site it is configuring.
12
+ *
13
+ * @return The preview, or nothing until the site's address is known.
14
+ */
15
+ export default function SitePreview() {
16
+ const siteUrl = useSelect( ( select ) => {
17
+ const siteData = select( coreStore ).getEntityRecord(
18
+ 'root',
19
+ '__unstableBase'
20
+ ) as { home?: unknown } | undefined;
21
+ return siteData?.home;
22
+ }, [] );
23
+
24
+ // `home` comes from the REST index. Without a usable URL the iframe would
25
+ // resolve its `src` against the current admin page and embed that instead,
26
+ // so render nothing until there is one.
27
+ if ( typeof siteUrl !== 'string' || ! siteUrl ) {
28
+ return null;
29
+ }
30
+
31
+ return (
32
+ <iframe
33
+ src={ addQueryArgs( siteUrl, {
34
+ // Parameter for hiding the admin bar.
35
+ wp_site_preview: 1,
36
+ } ) }
37
+ title={ __( 'Site Preview' ) }
38
+ style={ {
39
+ display: 'block',
40
+ width: '100%',
41
+ height: '100%',
42
+ backgroundColor: '#fff',
43
+ } }
44
+ onLoad={ ( event ) => {
45
+ const iframeDocument = ( event.target as HTMLIFrameElement )
46
+ .contentDocument;
47
+
48
+ // Not readable when the site is served from another origin.
49
+ if ( ! iframeDocument ) {
50
+ return;
51
+ }
52
+
53
+ // Make interactive elements unclickable.
54
+ focus.focusable
55
+ .find( iframeDocument.documentElement )
56
+ .forEach( ( element ) => {
57
+ element.style.pointerEvents = 'none';
58
+ element.tabIndex = -1;
59
+ element.setAttribute( 'aria-hidden', 'true' );
60
+ } );
61
+ } }
62
+ />
63
+ );
64
+ }
@@ -0,0 +1,220 @@
1
+ import { useCallback, useMemo } from '@wordpress/element';
2
+ import { useDispatch, useRegistry, useSelect } from '@wordpress/data';
3
+ import { store as coreStore } from '@wordpress/core-data';
4
+ import { store as editorStore } from '@wordpress/editor';
5
+ import { store as noticesStore } from '@wordpress/notices';
6
+ import { decodeEntities } from '@wordpress/html-entities';
7
+ import { __, sprintf } from '@wordpress/i18n';
8
+ import {
9
+ useNavigate,
10
+ useSearch,
11
+ privateApis as routePrivateApis,
12
+ } from '@wordpress/route';
13
+ import { store as bootStore } from '../../store';
14
+ import { isValidViewport } from './viewport';
15
+ import { unlock } from '../../lock-unlock';
16
+
17
+ const { useCanGoBack, useRouter } = unlock( routePrivateApis );
18
+
19
+ /*
20
+ * The route tree is assembled from routes registered at runtime, so TanStack has
21
+ * no search schema to infer and types `search` against a placeholder. Describe
22
+ * the two calls this module makes instead.
23
+ */
24
+ type Navigate = ( options: {
25
+ to?: string;
26
+ replace?: boolean;
27
+ search?: (
28
+ previous: Record< string, unknown >
29
+ ) => Record< string, unknown >;
30
+ } ) => void;
31
+
32
+ interface NavigateParams {
33
+ postType: string;
34
+ postId: string;
35
+ viewport?: string;
36
+ }
37
+
38
+ /**
39
+ * Builds the editor's entity navigation callbacks.
40
+ *
41
+ * The editor uses these to move between an entity and one nested inside it —
42
+ * a template part or a synced pattern reached from the block inspector. They
43
+ * are passed through the editor settings, where `onNavigateToEntityRecord`
44
+ * gates the inspector's edit affordance and `onNavigateToPreviousEntityRecord`
45
+ * gates the document bar's back button.
46
+ *
47
+ * @return The two callbacks, for spreading into the editor settings.
48
+ */
49
+ export default function useNavigateToEntityRecord() {
50
+ const navigate = useNavigate() as Navigate;
51
+ const search = useSearch( { strict: false } ) as { focusMode?: unknown };
52
+ const registry = useRegistry();
53
+ const getEntityLink = useSelect(
54
+ ( select ) => select( bootStore ).getEntityLink,
55
+ []
56
+ );
57
+ const router = useRouter();
58
+ const canGoBack = useCanGoBack();
59
+
60
+ const onNavigateToEntityRecord = useCallback(
61
+ ( params: NavigateParams ) => {
62
+ /*
63
+ * Stash the block selected in the entity being left, so returning to
64
+ * it can restore the user's place. The edits hold the external
65
+ * client ID, which `onChangeSelection` has already resolved.
66
+ */
67
+ const currentPostType = (
68
+ registry.select( editorStore ) as any
69
+ ).getCurrentPostType();
70
+ const currentPostId = (
71
+ registry.select( editorStore ) as any
72
+ ).getCurrentPostId();
73
+ const edits = registry
74
+ .select( coreStore )
75
+ .getEntityRecordEdits(
76
+ 'postType',
77
+ currentPostType,
78
+ currentPostId
79
+ ) as { selection?: { selectionStart?: { clientId?: string } } };
80
+ const selectedBlock = edits?.selection?.selectionStart?.clientId;
81
+
82
+ if ( selectedBlock ) {
83
+ navigate( {
84
+ search: ( previous: Record< string, unknown > ) => ( {
85
+ ...previous,
86
+ selectedBlock,
87
+ } ),
88
+ replace: true,
89
+ } );
90
+ }
91
+
92
+ /*
93
+ * An entity can ask to be edited at a particular width — a
94
+ * navigation overlay meant for mobile. The width is where the entity
95
+ * opens, not something the entity being left carries away: a width
96
+ * set from the device preview is view state, and an entity that asks
97
+ * for none opens at the default.
98
+ */
99
+ const requestedViewport = params.viewport?.toLowerCase();
100
+ const hasRequestedViewport = isValidViewport( requestedViewport );
101
+
102
+ /*
103
+ * `focusMode` marks the entity as one navigated into rather than
104
+ * opened directly, which is what offers the way back out. The search
105
+ * is replaced rather than extended so the block stashed above stays
106
+ * with the entity it belongs to.
107
+ */
108
+ const to = getEntityLink( params.postType, params.postId );
109
+
110
+ if ( to ) {
111
+ navigate( {
112
+ to,
113
+ search: () => ( {
114
+ focusMode: true,
115
+ ...( hasRequestedViewport
116
+ ? { viewport: requestedViewport }
117
+ : {} ),
118
+ } ),
119
+ } );
120
+ }
121
+ },
122
+ [ navigate, registry, getEntityLink ]
123
+ );
124
+
125
+ /*
126
+ * Only entities navigated into offer a way back out. The editor reads this
127
+ * as the entity being a focused one, giving it the document bar's back
128
+ * button and the surrounding padding, so an entity opened directly — from a
129
+ * list, say — must not have it. `canGoBack` covers reloading such a URL,
130
+ * where the marker survives but the history to return to does not.
131
+ */
132
+ const isFocusMode = !! search.focusMode;
133
+ const onNavigateToPreviousEntityRecord = useMemo(
134
+ () =>
135
+ isFocusMode && canGoBack ? () => router.history.back() : undefined,
136
+ [ isFocusMode, canGoBack, router ]
137
+ );
138
+
139
+ return { onNavigateToEntityRecord, onNavigateToPreviousEntityRecord };
140
+ }
141
+
142
+ interface ActionItem {
143
+ id: string | number;
144
+ type: string;
145
+ title?: string | { rendered?: string };
146
+ }
147
+
148
+ /**
149
+ * Builds the callback the editor runs after a post action.
150
+ *
151
+ * Actions taken from inside the editor act on the entity being edited, so
152
+ * trashing or deleting one leaves the canvas showing something that is no
153
+ * longer there, and duplicating one gives no way to reach the copy.
154
+ *
155
+ * @param postType Post type rendered in the canvas.
156
+ * @return The callback, for passing to the editor.
157
+ */
158
+ export function useActionPerformed( postType?: string ) {
159
+ const navigate = useNavigate() as Navigate;
160
+ const { createSuccessNotice } = useDispatch( noticesStore );
161
+ const getEntityLink = useSelect(
162
+ ( select ) => select( bootStore ).getEntityLink,
163
+ []
164
+ );
165
+
166
+ return useCallback(
167
+ ( actionId: string, items: ActionItem[] ) => {
168
+ switch ( actionId ) {
169
+ case 'move-to-trash':
170
+ case 'delete-post': {
171
+ const to = postType && getEntityLink( postType );
172
+
173
+ if ( to ) {
174
+ navigate( { to, search: () => ( {} ) } );
175
+ }
176
+ break;
177
+ }
178
+
179
+ case 'duplicate-post': {
180
+ const newItem = items[ 0 ];
181
+ const to = getEntityLink( newItem.type, newItem.id );
182
+
183
+ if ( ! to ) {
184
+ break;
185
+ }
186
+
187
+ const title =
188
+ typeof newItem.title === 'string'
189
+ ? newItem.title
190
+ : newItem.title?.rendered;
191
+
192
+ /*
193
+ * The action has already announced the copy under this id.
194
+ * Reissuing it under the same id replaces that notice rather
195
+ * than stacking a second one, and adds the way to reach it.
196
+ */
197
+ createSuccessNotice(
198
+ sprintf(
199
+ // translators: %s: Title of the created post, e.g: "Hello world".
200
+ __( '"%s" successfully created.' ),
201
+ decodeEntities( title ?? '' ) || __( '(no title)' )
202
+ ),
203
+ {
204
+ type: 'snackbar',
205
+ id: 'duplicate-post-action',
206
+ actions: [
207
+ {
208
+ label: __( 'Edit' ),
209
+ onClick: () => navigate( { to } ),
210
+ },
211
+ ],
212
+ }
213
+ );
214
+ break;
215
+ }
216
+ }
217
+ },
218
+ [ navigate, createSuccessNotice, getEntityLink, postType ]
219
+ );
220
+ }
@@ -0,0 +1,34 @@
1
+ const DEFAULT_DEVICE_TYPE = 'Desktop';
2
+
3
+ /*
4
+ * Lowercase in the URL, PascalCase in the editor store. Duplicated in
5
+ * packages/edit-site/src/components/block-editor/viewport.js, in the editor's
6
+ * preview-dropdown choices, and in the block editor's block-visibility
7
+ * constants. Update all four when adding a viewport type.
8
+ */
9
+ const VALID_VIEWPORTS = [ 'desktop', 'tablet', 'mobile' ];
10
+
11
+ /**
12
+ * Whether a viewport is one an entity can be asked to be edited at.
13
+ *
14
+ * @param viewport Viewport, as written in the URL.
15
+ * @return Whether the editor has a width for it.
16
+ */
17
+ export function isValidViewport( viewport?: string ): viewport is string {
18
+ return !! viewport && VALID_VIEWPORTS.includes( viewport.toLowerCase() );
19
+ }
20
+
21
+ /**
22
+ * The device type a `viewport` search param names.
23
+ *
24
+ * @param viewport Viewport, as written in the URL.
25
+ * @return Device type, Desktop for a viewport the editor has no width for.
26
+ */
27
+ export function getDeviceType( viewport?: string ) {
28
+ if ( ! isValidViewport( viewport ) ) {
29
+ return DEFAULT_DEVICE_TYPE;
30
+ }
31
+
32
+ const value = viewport.toLowerCase();
33
+ return value.charAt( 0 ).toUpperCase() + value.slice( 1 );
34
+ }
@@ -1,11 +1,4 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
1
  import { useState, useEffect } from '@wordpress/element';
5
-
6
- /**
7
- * Internal dependencies
8
- */
9
2
  import Canvas from '../canvas';
10
3
  import type { CanvasData } from '../../store/types';
11
4
 
@@ -0,0 +1,71 @@
1
+ import type { ReactNode } from 'react';
2
+ import { Component } from '@wordpress/element';
3
+ import { __ } from '@wordpress/i18n';
4
+ import { Button } from '@wordpress/components';
5
+ import { EmptyState } from '@wordpress/ui';
6
+ import { useCopyToClipboard } from '@wordpress/compose';
7
+ import { caution } from '@wordpress/icons';
8
+ import styles from './style.module.scss';
9
+
10
+ interface Props {
11
+ children: ReactNode;
12
+ }
13
+
14
+ interface State {
15
+ error: Error | null;
16
+ }
17
+
18
+ function CopyErrorButton( { error }: { error: Error } ) {
19
+ const ref = useCopyToClipboard( () => error.stack ?? String( error ) );
20
+
21
+ return (
22
+ <Button __next40pxDefaultSize variant="secondary" ref={ ref }>
23
+ { __( 'Copy error' ) }
24
+ </Button>
25
+ );
26
+ }
27
+
28
+ /**
29
+ * Contains a failure to one surface, so a screen keeps working around it.
30
+ *
31
+ * Wrapping each surface separately means a stage that throws leaves the
32
+ * navigation, the canvas and the way to save reachable, rather than taking the
33
+ * whole screen down with it.
34
+ */
35
+ export default class ErrorBoundary extends Component< Props, State > {
36
+ constructor( props: Props ) {
37
+ super( props );
38
+ this.state = { error: null };
39
+ }
40
+
41
+ static getDerivedStateFromError( error: Error ): State {
42
+ return { error };
43
+ }
44
+
45
+ render() {
46
+ const { error } = this.state;
47
+
48
+ if ( ! error ) {
49
+ return this.props.children;
50
+ }
51
+
52
+ return (
53
+ <div className={ styles[ 'error-boundary' ] }>
54
+ <EmptyState.Root>
55
+ <EmptyState.Icon icon={ caution } />
56
+ <EmptyState.Title>
57
+ { __( 'Something went wrong' ) }
58
+ </EmptyState.Title>
59
+ <EmptyState.Description>
60
+ { __(
61
+ 'This part of the screen could not be displayed. Reloading the page may help.'
62
+ ) }
63
+ </EmptyState.Description>
64
+ <EmptyState.Actions>
65
+ <CopyErrorButton error={ error } />
66
+ </EmptyState.Actions>
67
+ </EmptyState.Root>
68
+ </div>
69
+ );
70
+ }
71
+ }