-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add (rough) functionality for inline embedded resources
- Loading branch information
1 parent
a237d46
commit 5212942
Showing
11 changed files
with
321 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
packages/rich-text/src/helpers/newResourceEntitySelectorConfigFromRichTextField.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/rich-text/src/plugins/EmbeddedResourceInline/FetchingWrappedInlineResourceCard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as React from 'react'; | ||
|
||
import { Entry } from '@contentful/app-sdk'; | ||
import { InlineEntryCard, MenuItem, Text } from '@contentful/f36-components'; | ||
import { ResourceLink, ResourceInfo, useResource } from '@contentful/field-editor-reference'; | ||
import { entityHelpers } from '@contentful/field-editor-shared'; | ||
import { FieldAppSDK } from '@contentful/field-editor-shared'; | ||
import { INLINES } from '@contentful/rich-text-types'; | ||
|
||
const { getEntryTitle, getEntryStatus } = entityHelpers; | ||
|
||
interface FetchingWrappedInlineResourceCardProps { | ||
link: ResourceLink['sys']; | ||
sdk: FieldAppSDK; | ||
isSelected: boolean; | ||
isDisabled: boolean; | ||
onRemove: (event: React.MouseEvent<Element, MouseEvent>) => void; | ||
onEntityFetchComplete?: VoidFunction; | ||
} | ||
|
||
export function FetchingWrappedInlineResourceCard(props: FetchingWrappedInlineResourceCardProps) { | ||
const { link, onEntityFetchComplete } = props; | ||
const { data, status: requestStatus } = useResource(link.linkType, link.urn); | ||
|
||
React.useEffect(() => { | ||
if (requestStatus === 'success') { | ||
onEntityFetchComplete?.(); | ||
} | ||
}, [onEntityFetchComplete, requestStatus]); | ||
|
||
if (requestStatus === 'error') { | ||
return ( | ||
<InlineEntryCard | ||
title="Entry missing or inaccessible" | ||
testId={INLINES.EMBEDDED_RESOURCE} | ||
isSelected={props.isSelected} | ||
/> | ||
); | ||
} | ||
|
||
if (requestStatus === 'loading' || data === undefined) { | ||
return <InlineEntryCard isLoading />; | ||
} | ||
|
||
const { resource: entry, contentType, defaultLocaleCode } = data as ResourceInfo<Entry>; | ||
|
||
const title = getEntryTitle({ | ||
entry, | ||
contentType, | ||
defaultLocaleCode, | ||
localeCode: defaultLocaleCode, | ||
defaultTitle: 'Untitled', | ||
}); | ||
const status = getEntryStatus(entry?.sys); | ||
|
||
return ( | ||
<InlineEntryCard | ||
testId={INLINES.EMBEDDED_RESOURCE} | ||
isSelected={props.isSelected} | ||
title={`${data?.contentType.name}: ${title}`} | ||
status={status} | ||
actions={[ | ||
<MenuItem key="remove" onClick={props.onRemove} disabled={props.isDisabled} testId="delete"> | ||
Remove | ||
</MenuItem>, | ||
]} | ||
> | ||
<Text>{title}</Text> | ||
</InlineEntryCard> | ||
); | ||
} |
56 changes: 56 additions & 0 deletions
56
packages/rich-text/src/plugins/EmbeddedResourceInline/LinkedResourceInline.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React from 'react'; | ||
|
||
import { ResourceLink } from '@contentful/field-editor-reference'; | ||
import { useSelected, useReadOnly } from 'slate-react'; | ||
|
||
import { useContentfulEditor } from '../../ContentfulEditorProvider'; | ||
import { Element, findNodePath, removeNodes, RenderElementProps } from '../../internal'; | ||
import { useSdkContext } from '../../SdkProvider'; | ||
import { useLinkTracking } from '../links-tracking'; | ||
import { LinkedInlineWrapper } from '../shared/LinkedInlineWrapper'; | ||
import { FetchingWrappedInlineResourceCard } from './FetchingWrappedInlineResourceCard'; | ||
|
||
export type LinkedResourceInlineProps = { | ||
element: Element & { | ||
data: { | ||
target: ResourceLink; | ||
}; | ||
}; | ||
attributes: Pick<RenderElementProps, 'attributes'>; | ||
children: Pick<RenderElementProps, 'children'>; | ||
}; | ||
|
||
export function LinkedResourceInline(props: LinkedResourceInlineProps) { | ||
const { attributes, children, element } = props; | ||
const { onEntityFetchComplete } = useLinkTracking(); | ||
const isSelected = useSelected(); | ||
const editor = useContentfulEditor(); | ||
const sdk = useSdkContext(); | ||
const isDisabled = useReadOnly(); | ||
const link = element.data.target.sys; | ||
|
||
function handleRemoveClick() { | ||
if (!editor) return; | ||
const pathToElement = findNodePath(editor, element); | ||
removeNodes(editor, { at: pathToElement }); | ||
} | ||
|
||
return ( | ||
<LinkedInlineWrapper | ||
attributes={attributes} | ||
link={element.data.target} | ||
card={ | ||
<FetchingWrappedInlineResourceCard | ||
sdk={sdk} | ||
link={link} | ||
isDisabled={isDisabled} | ||
isSelected={isSelected} | ||
onRemove={handleRemoveClick} | ||
onEntityFetchComplete={onEntityFetchComplete} | ||
/> | ||
} | ||
> | ||
{children} | ||
</LinkedInlineWrapper> | ||
); | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/rich-text/src/plugins/EmbeddedResourceInline/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { FieldAppSDK } from '@contentful/app-sdk'; | ||
import { INLINES } from '@contentful/rich-text-types'; | ||
|
||
import { PlatePlugin } from '../../internal'; | ||
import { getWithEmbeddedEntryInlineEvents } from '../shared/EmbeddedInlineUtil'; | ||
import { LinkedResourceInline } from './LinkedResourceInline'; | ||
|
||
export function createEmbeddedResourceInlinePlugin(sdk: FieldAppSDK): PlatePlugin { | ||
const htmlAttributeName = 'data-embedded-resource-inline-id'; | ||
|
||
return { | ||
key: INLINES.EMBEDDED_RESOURCE, | ||
type: INLINES.EMBEDDED_RESOURCE, | ||
isElement: true, | ||
isInline: true, | ||
isVoid: true, | ||
component: LinkedResourceInline, | ||
options: { | ||
hotkey: 'mod+shift+p', | ||
}, | ||
handlers: { | ||
onKeyDown: getWithEmbeddedEntryInlineEvents(INLINES.EMBEDDED_RESOURCE, sdk), | ||
}, | ||
deserializeHtml: { | ||
rules: [ | ||
{ | ||
validAttribute: htmlAttributeName, | ||
}, | ||
], | ||
withoutChildren: true, | ||
getNode: (el) => ({ | ||
type: INLINES.EMBEDDED_RESOURCE, | ||
children: [{ text: '' }], | ||
data: { | ||
target: { | ||
sys: { | ||
urn: el.getAttribute('data-entity-id'), | ||
linkType: el.getAttribute('data-entity-type'), | ||
type: 'ResourceLink', | ||
}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.