UNPKG

@wordpress/widget-dashboard

Version:

Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.

1 lines • 15.3 kB
{"version":3,"file":"widget-settings.mjs","names":["styles"],"sources":["../../../../style-runtime/src/index.ts","../../../src/components/widget-settings/widget-settings.module.css","../../../src/components/widget-settings/widget-settings.tsx"],"sourcesContent":["type GlobalScopeWithStyleRuntime = typeof globalThis & {\n\t// This global is shared by separately bundled copies of this package.\n\t// Keep its shape backward compatible after release.\n\t__wpStyleRuntime?: {\n\t\tdocuments: Map< Document, number >;\n\t\tstyles: Map< string, string >;\n\t\tinjectedStyles: WeakMap< Document, Set< string > >;\n\t};\n};\n\nconst STYLE_HASH_ATTRIBUTE = 'data-wp-hash';\n\n/**\n * Returns the shared style runtime registry.\n *\n * The registry is stored on `globalThis` so separately bundled copies of this\n * package can coordinate through the same document and style maps.\n *\n * @return The shared runtime registry.\n */\nfunction getRuntime() {\n\tconst globalScope = globalThis as GlobalScopeWithStyleRuntime;\n\n\tif ( globalScope.__wpStyleRuntime ) {\n\t\treturn globalScope.__wpStyleRuntime;\n\t}\n\n\tglobalScope.__wpStyleRuntime = {\n\t\tdocuments: new Map(),\n\t\tstyles: new Map(),\n\t\tinjectedStyles: new WeakMap(),\n\t};\n\n\tif ( typeof document !== 'undefined' ) {\n\t\tregisterDocument( document );\n\t}\n\n\treturn globalScope.__wpStyleRuntime;\n}\n\n/**\n * Checks whether a document already contains a style tag for a hash.\n *\n * @param targetDocument Document to inspect.\n * @param hash Stable hash for the transformed CSS.\n *\n * @return Whether the style hash already exists in the document.\n */\nfunction documentContainsStyleHash(\n\ttargetDocument: Document,\n\thash: string\n): boolean {\n\tif ( ! targetDocument.head ) {\n\t\treturn false;\n\t}\n\n\tfor ( const style of targetDocument.head.querySelectorAll(\n\t\t`style[${ STYLE_HASH_ATTRIBUTE }]`\n\t) ) {\n\t\tif ( style.getAttribute( STYLE_HASH_ATTRIBUTE ) === hash ) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n\n/**\n * Injects a registered style into a document, unless that document already\n * contains a style tag for the same hash.\n *\n * @param targetDocument Document to inject the style into.\n * @param hash Stable hash for the transformed CSS.\n * @param css CSS text to inject.\n */\nfunction injectStyle( targetDocument: Document, hash: string, css: string ) {\n\tif ( ! targetDocument.head ) {\n\t\treturn;\n\t}\n\n\tconst runtime = getRuntime();\n\tlet injectedStyles = runtime.injectedStyles.get( targetDocument );\n\n\tif ( ! injectedStyles ) {\n\t\tinjectedStyles = new Set();\n\t\truntime.injectedStyles.set( targetDocument, injectedStyles );\n\t}\n\n\tif ( injectedStyles.has( hash ) ) {\n\t\treturn;\n\t}\n\n\t// Older generated CSS module output can still inject matching style tags\n\t// after this document's cache is created, so keep the DOM as the fallback\n\t// source of truth on cache misses.\n\tif ( documentContainsStyleHash( targetDocument, hash ) ) {\n\t\tinjectedStyles.add( hash );\n\t\treturn;\n\t}\n\n\tconst style = targetDocument.createElement( 'style' );\n\tstyle.setAttribute( STYLE_HASH_ATTRIBUTE, hash );\n\tstyle.appendChild( targetDocument.createTextNode( css ) );\n\ttargetDocument.head.appendChild( style );\n\tinjectedStyles.add( hash );\n}\n\n/**\n * Registers a document as a style injection target.\n *\n * Existing registered styles are replayed into the document immediately.\n * Documents are reference-counted so multiple providers can safely register the\n * same document without one cleanup removing it while another registration is\n * still active.\n *\n * @param targetDocument Document to receive registered styles.\n * @return Cleanup function that unregisters this document registration.\n */\nexport function registerDocument( targetDocument: Document ) {\n\tconst runtime = getRuntime();\n\n\truntime.documents.set(\n\t\ttargetDocument,\n\t\t( runtime.documents.get( targetDocument ) ?? 0 ) + 1\n\t);\n\n\tfor ( const [ hash, css ] of runtime.styles ) {\n\t\tinjectStyle( targetDocument, hash, css );\n\t}\n\n\treturn () => {\n\t\tconst count = runtime.documents.get( targetDocument );\n\n\t\tif ( count === undefined ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( count <= 1 ) {\n\t\t\truntime.documents.delete( targetDocument );\n\t\t\treturn;\n\t\t}\n\n\t\truntime.documents.set( targetDocument, count - 1 );\n\t};\n}\n\n/**\n * Registers a style and injects it into all registered documents.\n *\n * The hash is used as the deduplication key, so calling this repeatedly with\n * the same hash will not add duplicate style tags to a document.\n * Registered styles are retained for the lifetime of the page so they can be\n * replayed into documents that are registered later.\n *\n * @param hash Stable hash for the transformed CSS.\n * @param css CSS text to inject.\n */\nexport function registerStyle( hash: string, css: string ) {\n\tconst runtime = getRuntime();\n\n\truntime.styles.set( hash, css );\n\n\tfor ( const targetDocument of runtime.documents.keys() ) {\n\t\tinjectStyle( targetDocument, hash, css );\n\t}\n}\n","import { registerStyle } from '@wordpress/style-runtime';\nif (typeof process === 'undefined' || process.env.NODE_ENV !== 'test') {\n\tregisterStyle(\"6f6df0127e\", \".e059677fe334dcbe__popup{margin-block-start:var(--wp-admin--admin-bar--height,0)}\");\n}\nexport default {\"popup\":\"e059677fe334dcbe__popup\"};\n","import { DataForm } from '@wordpress/dataviews';\nimport type { Field, Form } from '@wordpress/dataviews';\nimport { useCallback, useEffect, useMemo, useState } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\n// Dashboard is still experimental.\nimport {\n\t// eslint-disable-next-line @wordpress/use-recommended-components\n\tButton,\n\t// eslint-disable-next-line @wordpress/use-recommended-components\n\tDrawer,\n} from '@wordpress/ui';\nimport { useDashboardInternalContext } from '../../context/dashboard-context';\nimport { useDashboardUIContext } from '../../context/ui-context';\nimport { getWidgetSettingsTitle } from './utils';\nimport styles from './widget-settings.module.css';\nimport type { WidgetAttributeValues } from '../../types';\n\n/**\n * Side drawer that edits one instance's attributes, mounted once at the\n * dashboard root. It resolves the active instance from `settingsWidgetUuid`\n * in the UI context (set by the per-instance settings trigger), renders the\n * type's declarative `attributes` through `DataForm`, and enters from the\n * inline-end edge.\n *\n * Edits write to the staging layer, so they preview live behind the drawer\n * and are published on Save or reverted on any other exit. Available in\n * normal mode only; the settings entry point is hidden while the layout is\n * being edited.\n */\nexport function WidgetSettings(): React.ReactNode {\n\tconst {\n\t\tlayout,\n\t\tonLayoutChange,\n\t\twidgetTypes,\n\t\tcommit,\n\t\tcancel: cancelStaging,\n\t\thasUncommittedChanges,\n\t\tcanPerform,\n\t} = useDashboardInternalContext();\n\tconst { settingsWidgetUuid, setSettingsWidgetUuid } =\n\t\tuseDashboardUIContext();\n\n\t// A trigger composed by the host can ask for any instance; the surface\n\t// itself checks the policy, so a denied `edit` never opens it.\n\tconst requestedWidget = settingsWidgetUuid\n\t\t? layout.find( ( instance ) => instance.uuid === settingsWidgetUuid )\n\t\t: undefined;\n\tconst open =\n\t\t!! requestedWidget &&\n\t\tcanPerform( {\n\t\t\toperation: 'edit',\n\t\t\twidget: requestedWidget,\n\t\t\twidgetType: widgetTypes.find(\n\t\t\t\t( type ) => type.name === requestedWidget.type\n\t\t\t),\n\t\t} );\n\n\t// Keep the last opened instance resolved while the drawer animates\n\t// closed so its form and title don't blank out mid-transition. While\n\t// open the live `settingsWidgetUuid` wins, so opening shows no stale\n\t// frame.\n\tconst [ lastWidgetUuid, setLastWidgetUuid ] = useState< string | null >(\n\t\tsettingsWidgetUuid\n\t);\n\tuseEffect( () => {\n\t\tif ( settingsWidgetUuid ) {\n\t\t\tsetLastWidgetUuid( settingsWidgetUuid );\n\t\t}\n\t}, [ settingsWidgetUuid ] );\n\n\tconst activeUuid = settingsWidgetUuid ?? lastWidgetUuid;\n\tconst widget = activeUuid\n\t\t? layout.find( ( instance ) => instance.uuid === activeUuid )\n\t\t: undefined;\n\tconst widgetType = widget\n\t\t? widgetTypes.find( ( type ) => type.name === widget.type )\n\t\t: undefined;\n\n\tconst fields = useMemo< Field< WidgetAttributeValues >[] >(\n\t\t() =>\n\t\t\t( widgetType?.attributes ??\n\t\t\t\t[] ) as Field< WidgetAttributeValues >[],\n\t\t[ widgetType?.attributes ]\n\t);\n\n\tconst form = useMemo< Form >(\n\t\t() => ( {\n\t\t\tlayout: { type: 'regular', labelPosition: 'top' },\n\t\t\tfields: fields.map( ( field ) => field.id ),\n\t\t} ),\n\t\t[ fields ]\n\t);\n\n\tconst handleChange = useCallback(\n\t\t( edits: Record< string, unknown > ) => {\n\t\t\tif ( ! widget ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tonLayoutChange(\n\t\t\t\tlayout.map( ( instance ) =>\n\t\t\t\t\tinstance.uuid === widget.uuid\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t...instance,\n\t\t\t\t\t\t\t\tattributes: {\n\t\t\t\t\t\t\t\t\t...( instance.attributes as object ),\n\t\t\t\t\t\t\t\t\t...edits,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t: instance\n\t\t\t\t)\n\t\t\t);\n\t\t},\n\t\t[ layout, onLayoutChange, widget ]\n\t);\n\n\tconst close = useCallback(\n\t\t() => setSettingsWidgetUuid( null ),\n\t\t[ setSettingsWidgetUuid ]\n\t);\n\n\tconst handleSave = useCallback( () => {\n\t\tcommit();\n\t\tclose();\n\t}, [ commit, close ] );\n\n\tconst handleOpenChange = useCallback(\n\t\t( nextOpen: boolean ) => {\n\t\t\t// Any path out of the drawer other than Save discards the\n\t\t\t// staged edits.\n\t\t\tif ( ! nextOpen ) {\n\t\t\t\tcancelStaging();\n\t\t\t\tclose();\n\t\t\t}\n\t\t},\n\t\t[ cancelStaging, close ]\n\t);\n\n\t// A denied `edit` closes the surface by prop, outside `handleOpenChange`.\n\t// Discard its staged edits the same way, so a later grant reopens clean.\n\tuseEffect( () => {\n\t\tif ( requestedWidget && ! open ) {\n\t\t\tcancelStaging();\n\t\t\tclose();\n\t\t}\n\t}, [ requestedWidget, open, cancelStaging, close ] );\n\n\tconst hasForm = !! widget && !! widgetType && fields.length > 0;\n\n\tif ( ! hasForm ) {\n\t\treturn null;\n\t}\n\n\tconst title = getWidgetSettingsTitle( widgetType );\n\tconst data = ( widget?.attributes ??\n\t\twidgetType?.example?.attributes ??\n\t\t{} ) as WidgetAttributeValues;\n\n\treturn (\n\t\t<Drawer.Root\n\t\t\topen={ open }\n\t\t\tonOpenChange={ handleOpenChange }\n\t\t\tswipeDirection=\"right\"\n\t\t\tmodal={ false }\n\t\t\tdisablePointerDismissal\n\t\t>\n\t\t\t<Drawer.Popup size=\"medium\" className={ styles.popup }>\n\t\t\t\t<Drawer.Header>\n\t\t\t\t\t<Drawer.Title>{ title }</Drawer.Title>\n\t\t\t\t\t<Drawer.CloseIconButton />\n\t\t\t\t</Drawer.Header>\n\n\t\t\t\t<Drawer.Content>\n\t\t\t\t\t<DataForm< WidgetAttributeValues >\n\t\t\t\t\t\tdata={ data }\n\t\t\t\t\t\tfields={ fields }\n\t\t\t\t\t\tform={ form }\n\t\t\t\t\t\tonChange={ handleChange }\n\t\t\t\t\t/>\n\t\t\t\t</Drawer.Content>\n\n\t\t\t\t<Drawer.Footer>\n\t\t\t\t\t<Button\n\t\t\t\t\t\tvariant=\"minimal\"\n\t\t\t\t\t\ttone=\"brand\"\n\t\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\t\tonClick={ () => handleOpenChange( false ) }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ __( 'Cancel' ) }\n\t\t\t\t\t</Button>\n\t\t\t\t\t<Button\n\t\t\t\t\t\tvariant=\"solid\"\n\t\t\t\t\t\ttone=\"brand\"\n\t\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\t\tonClick={ handleSave }\n\t\t\t\t\t\tdisabled={ ! hasUncommittedChanges }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ __( 'Save' ) }\n\t\t\t\t\t</Button>\n\t\t\t\t</Drawer.Footer>\n\t\t\t</Drawer.Popup>\n\t\t</Drawer.Root>\n\t);\n}\n"],"mappings":";;;;;;;;;AAUA,MAAM,uBAAuB;;;;;;;;;AAU7B,SAAS,aAAa;CACrB,MAAM,cAAc;CAEpB,IAAK,YAAY,kBAChB,OAAO,YAAY;CAGpB,YAAY,mBAAmB;EAC9B,2BAAW,IAAI,IAAI;EACnB,wB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