From 9e359feadc5e73e15d10a44026eef03c544b1fe7 Mon Sep 17 00:00:00 2001 From: Sebastian Helzle Date: Sat, 26 Oct 2024 11:00:04 +0200 Subject: [PATCH] FEATURE: Use registered secondary media selection screen Resolves: #50 With this change any registered MediaSelectionScreen secondary editor is used instead of the fixed dependency on the media browser. This way the Flowpack.Media.UI will be used when it is installed a project. --- .../LinkTypes/Asset/MediaBrowser.tsx | 105 +++++++++--------- 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/Neos.Ui/core/src/application/LinkTypes/Asset/MediaBrowser.tsx b/Neos.Ui/core/src/application/LinkTypes/Asset/MediaBrowser.tsx index 7ee882f..a74bb4e 100644 --- a/Neos.Ui/core/src/application/LinkTypes/Asset/MediaBrowser.tsx +++ b/Neos.Ui/core/src/application/LinkTypes/Asset/MediaBrowser.tsx @@ -1,66 +1,67 @@ import * as React from 'react'; +import { useEffect, useRef } from 'react'; -import {useRoutes} from '@sitegeist/archaeopteryx-neos-bridge'; +import {useGlobalRegistry} from '@sitegeist/archaeopteryx-neos-bridge'; +import styled from 'styled-components'; interface Props { assetIdentifier: null | string onSelectAsset: (assetIdentifier: string) => void } +const Container = styled.div` + width: calc(-80px - 64px + 100vw); + max-width: 1260px; + + & > div { + padding: 0; + } + + & > iframe { + width: calc(100vw - 160px); + max-width: 100%; + height: calc(100vh - 580px); + position: relative; + } +`; + export const MediaBrowser: React.FC = props => { - const mediaBrowserUri = useRoutes(r => r.core?.modules?.mediaBrowser); + const globalRegistry = useGlobalRegistry(); + const containerRef = useRef(null); + const selectionRef = useRef(null); - React.useEffect(() => { - (window as any).NeosMediaBrowserCallbacks = { - assetChosen: (assetIdentifier: string) => { - props.onSelectAsset(assetIdentifier); - } - }; + const secondaryEditorsRegistry = globalRegistry?.get('inspector')?.get('secondaryEditors') as { + get: (identifier: string) => { + component: React.ComponentType + } + }; + const { component: MediaSelectionScreenComponent } = secondaryEditorsRegistry?.get('Neos.Neos/Inspector/Secondary/Editors/MediaSelectionScreen'); - () => { - (window as any).NeosMediaBrowserCallbacks = {}; - }; - }, [props.onSelectAsset]); + // The standard MediaBrowser of Neos uses an iframe and requires some styles to be applied to the iframe content + const iframe = (containerRef.current?.querySelector('& > iframe') as HTMLIFrameElement); - if (!mediaBrowserUri) { - throw new Error('[Sitegeist.Archaeopteryx]: Could not resolve mediaBrowserUri.'); - } + useEffect(() => { + iframe?.addEventListener('load', (ev) => { + const iframe = (ev.target as HTMLIFrameElement).contentDocument; + if (iframe) { + iframe.body.style.overflowX = 'hidden'; + iframe.body.style.padding = '0'; + iframe.querySelector('form > .neos-footer')?.remove(); + iframe.querySelectorAll('input, select, textarea')?.forEach(input => { + (input as HTMLInputElement).readOnly = true; + }); + } + }); + }, [iframe]); - if (props.assetIdentifier) { - return ( -