Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion routes/identity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
},
"dependencies": {
"@wordpress/admin-ui": "file:../../packages/admin-ui",
"@wordpress/components": "file:../../packages/components",
"@wordpress/core-data": "file:../../packages/core-data",
"@wordpress/data": "file:../../packages/data",
"@wordpress/dataviews": "file:../../packages/dataviews",
"@wordpress/element": "file:../../packages/element",
"@wordpress/fields": "file:../../packages/fields",
"@wordpress/html-entities": "file:../../packages/html-entities",
"@wordpress/i18n": "file:../../packages/i18n"
"@wordpress/i18n": "file:../../packages/i18n",
"@wordpress/lazy-editor": "file:../../packages/lazy-editor"
}
}
41 changes: 39 additions & 2 deletions routes/identity/stage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useSelect, useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { DataForm, type Field, type Form } from '@wordpress/dataviews';
import { MediaEdit } from '@wordpress/fields';
import { loadEditorAssets } from '@wordpress/lazy-editor';
import { useEffect, useState } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';

type SiteSettings = {
Expand All @@ -13,6 +15,41 @@ type SiteSettings = {
site_icon?: number;
};

/*
* The media fields open the WordPress media modal, which needs the media
* assets (`wp.media` and friends) that the editor canvas loads lazily. This
* screen mounts a preview canvas, so they normally arrive moments after the
* screen does — but a fast click would still beat them and crash the route.
* Gate the field's edit component on the shared editor assets.
*
* A stopgap owned by the route, like the identical wrapper in
* `routes/post-list`: ultimately a field should be able to declare this kind
* of asset dependency itself; once that exists, remove this wrapper.
*/
function MediaEditWithEditorAssets( props: any ) {
const [ isReady, setIsReady ] = useState(
() => !! ( window as any ).wp?.media
);
useEffect( () => {
if ( ! isReady ) {
loadEditorAssets().then( () => setIsReady( true ) );
}
}, [ isReady ] );

// Render the field right away — only opening the modal needs the
// assets — and keep it inert until they have loaded.
return (
<div
aria-busy={ ! isReady || undefined }
style={ ! isReady ? { opacity: 0.6 } : undefined }
// @ts-expect-error inert not typed properly
inert={ ! isReady ? 'true' : undefined }
>
<MediaEdit { ...props } />
</div>
);
}

const fields: Field< SiteSettings >[] = [
{
id: 'title',
Expand Down Expand Up @@ -40,7 +77,7 @@ const fields: Field< SiteSettings >[] = [
"Displays in your site's layout via the Site Logo block."
),
placeholder: __( 'Choose logo' ),
Edit: MediaEdit,
Edit: MediaEditWithEditorAssets,
setValue: ( { value } ) => ( {
site_logo: value ?? 0,
} ),
Expand All @@ -53,7 +90,7 @@ const fields: Field< SiteSettings >[] = [
'Shown in browser tabs, bookmarks, and mobile apps. It should be square and at least 512 by 512 pixels.'
),
placeholder: __( 'Choose icon' ),
Edit: MediaEdit,
Edit: MediaEditWithEditorAssets,
setValue: ( { value } ) => ( {
site_icon: value ?? 0,
} ),
Expand Down
5 changes: 4 additions & 1 deletion routes/identity/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
},
"references": [
{ "path": "../../packages/admin-ui/tsconfig.build.json" },
{ "path": "../../packages/components/tsconfig.build.json" },
{ "path": "../../packages/core-data/tsconfig.build.json" },
{ "path": "../../packages/element/tsconfig.build.json" },
{ "path": "../../packages/data" },
{ "path": "../../packages/dataviews/tsconfig.build.json" },
{ "path": "../../packages/fields/tsconfig.build.json" },
{ "path": "../../packages/html-entities/tsconfig.build.json" },
{ "path": "../../packages/i18n/tsconfig.build.json" }
{ "path": "../../packages/i18n/tsconfig.build.json" },
{ "path": "../../packages/lazy-editor" }
],
"include": [ "**/*.ts", "**/*.tsx" ],
"exclude": [ "build", "node_modules" ]
Expand Down
5 changes: 3 additions & 2 deletions routes/post-list/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
"@wordpress/core-data": "file:../../packages/core-data",
"@wordpress/data": "file:../../packages/data",
"@wordpress/dataviews": "file:../../packages/dataviews",
"@wordpress/element": "file:../../packages/element",
"@wordpress/editor": "file:../../packages/editor",
"@wordpress/element": "file:../../packages/element",
"@wordpress/fields": "file:../../packages/fields",
"@wordpress/i18n": "file:../../packages/i18n",
"@wordpress/icons": "file:../../packages/icons",
"@wordpress/routes-lock-unlock": "file:../lock-unlock",
"@wordpress/lazy-editor": "file:../../packages/lazy-editor",
"@wordpress/route": "file:../../packages/route",
"@wordpress/routes-lock-unlock": "file:../lock-unlock",
"@wordpress/views": "file:../../packages/views"
}
}
45 changes: 45 additions & 0 deletions routes/post-list/quick-edit-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,49 @@ import {
} from '@wordpress/components';
import { useEffect, useMemo, useState } from '@wordpress/element';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { loadEditorAssets } from '@wordpress/lazy-editor';
import { unlock } from '@wordpress/routes-lock-unlock';

const { usePostFields, PostCardPanel } = unlock( editorPrivateApis );

/*
* The featured image field opens the WordPress media modal, which needs the
* media assets (`wp.media` and friends) that the editor canvas loads but this
* screen never does — without them, opening the field crashes the route.
* Wrap the field's edit component so the shared editor assets are loaded
* first.
*
* This is a stopgap owned by the route because the route is what knows the
* screen is asset-less. Ultimately a field should be able to declare this
* kind of asset dependency itself so every DataForm consumer gets it for
* free; once that exists, remove this wrapper.
*/
function withEditorAssets( FieldEdit: any ) {
return function EditWithEditorAssets( props: any ) {
const [ isReady, setIsReady ] = useState(
() => !! ( window as any ).wp?.media
);
useEffect( () => {
if ( ! isReady ) {
loadEditorAssets().then( () => setIsReady( true ) );
}
}, [ isReady ] );

// Render the field right away — only opening the modal needs the
// assets — and keep it inert until they have loaded.
return (
<div
aria-busy={ ! isReady || undefined }
style={ ! isReady ? { opacity: 0.6 } : undefined }
// @ts-expect-error inert not typed properly
inert={ ! isReady ? 'true' : undefined }
>
<FieldEdit { ...props } />
</div>
);
};
}

const fieldsWithBulkEditSupport = [ 'status', 'date', 'author', 'discussion' ];

interface QuickEditModalProps {
Expand Down Expand Up @@ -91,6 +130,12 @@ export function QuickEditModal( {
readOnly: ! canSwitchTemplate,
};
}
if ( field.id === 'featured_media' && field.Edit ) {
return {
...field,
Edit: withEditorAssets( field.Edit ),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oandregal as you work on field collections later, this is something to keep in mind, fields shouldn't assume editor assets are loaded, most dataviews pages don't need an editor (and all its JS). For this particular field, I think the new media editor might solve the issue though.

};
}

return field;
} ),
Expand Down
1 change: 1 addition & 0 deletions routes/post-list/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
{ "path": "../../packages/i18n/tsconfig.build.json" },
{ "path": "../../packages/icons/tsconfig.build.json" },
{ "path": "../../packages/keycodes/tsconfig.build.json" },
{ "path": "../../packages/lazy-editor" },
{ "path": "../../packages/notices/tsconfig.build.json" },
{ "path": "../../packages/route" },
{ "path": "../../packages/views/tsconfig.build.json" }
Expand Down
Loading