refactor: component viewer (#2377)

This commit is contained in:
Mo
2023-07-28 07:08:52 -05:00
committed by GitHub
parent 3de6af445b
commit c7fb0d2aca
9 changed files with 84 additions and 40 deletions

View File

@@ -7,7 +7,7 @@ import {
ComponentInterface,
SubscriptionManagerEvent,
} from '@standardnotes/snjs'
import { FunctionComponent, useCallback, useEffect, useRef, useState } from 'react'
import { FunctionComponent, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { observer } from 'mobx-react-lite'
import OfflineRestricted from '@/Components/ComponentView/OfflineRestricted'
import UrlMissing from '@/Components/ComponentView/UrlMissing'
@@ -178,6 +178,37 @@ const IframeFeatureView: FunctionComponent<Props> = ({ onLoad, componentViewer,
}
}, [application, requestReload, componentViewer, uiFeature])
const sandboxAttributes = useMemo(() => {
const attributes = [
'allow-scripts',
'allow-top-navigation-by-user-activation',
'allow-popups',
'allow-modals',
'allow-forms',
'allow-downloads',
]
if (uiFeature.isNativeFeature) {
attributes.push('allow-popups-to-escape-sandbox')
}
if (application.isNativeMobileWeb()) {
/**
* Native mobile web serves native components through the file:// protocol.
* Native mobile web also does not use localStorage, unlike the web app.
* So, components served through the file:// (precompiled editors) will be treated
* as same origin as the parent app, but will not have meaningful access.
*
* Third party components will have a non-file:// origin, and thus don't need this attribute.
*/
if (uiFeature.isNativeFeature) {
attributes.push('allow-same-origin')
}
}
return attributes
}, [application, uiFeature])
return (
<>
{hasIssueLoading && (
@@ -208,7 +239,7 @@ const IframeFeatureView: FunctionComponent<Props> = ({ onLoad, componentViewer,
data-component-viewer-id={componentViewer.identifier}
frameBorder={0}
src={componentViewer.url || ''}
sandbox="allow-scripts allow-top-navigation-by-user-activation allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-modals allow-forms allow-downloads"
sandbox={sandboxAttributes.join(' ')}
>
Loading
</iframe>