Skip to content

Commit

Permalink
feat: show environment in resource inline block (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
veu authored Nov 2, 2023
1 parent 330725a commit 3e897e7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { entityHelpers } from '@contentful/field-editor-shared';
import { FieldAppSDK } from '@contentful/field-editor-shared';
import { INLINES } from '@contentful/rich-text-types';

import { truncateTitle } from '../../plugins/shared/utils';

const { getEntryTitle, getEntryStatus } = entityHelpers;

interface FetchingWrappedResourceInlineCardProps {
Expand Down Expand Up @@ -51,13 +53,14 @@ export function FetchingWrappedResourceInlineCard(props: FetchingWrappedResource
localeCode: defaultLocaleCode,
defaultTitle: 'Untitled',
});
const status = getEntryStatus(entry?.sys);
const truncatedTitle = truncateTitle(title, 40);
const status = getEntryStatus(entry.sys);

return (
<InlineEntryCard
testId={INLINES.EMBEDDED_RESOURCE}
isSelected={props.isSelected}
title={`${data.contentType.name}: ${title} (Space: ${space.name})`}
title={`${contentType.name}: ${truncatedTitle} (Space: ${space.name} – Env.: ${entry.sys.environment.sys.id})`}
status={status}
actions={[
<MenuItem key="remove" onClick={props.onRemove} disabled={props.isDisabled} testId="delete">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as React from 'react';
import { useResource } from '@contentful/field-editor-reference';
import { ResourceLink } from '@contentful/rich-text-types';

import { truncateTitle } from './utils';
import { truncateTitle } from '../../plugins/shared/utils';

type ResourceEntityInfoProps = {
target: ResourceLink;
Expand Down
16 changes: 1 addition & 15 deletions packages/rich-text/src/plugins/Hyperlink/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,14 @@ import { isAncestorEmpty } from '@udecode/plate-common';
import { getText } from '../../internal/queries';
import { NodeEntry } from '../../internal/types';
import { PlateEditor } from '../../internal/types';
import { truncateTitle } from '../../plugins/shared/utils';
import { FetchedEntityData } from './useEntityInfo';

export const hasText = (editor: PlateEditor, entry: NodeEntry) => {
const [node, path] = entry;
return !isAncestorEmpty(editor, node as any) && getText(editor, path).trim() !== '';
};

export function truncateTitle(str: string, length: number) {
if (typeof str === 'string' && str.length > length) {
return (
str &&
str
.substr(0, length + 1) // +1 to look ahead and be replaced below.
// Get rid of orphan letters but not one letter words (I, a, 2).
// Try to not have “.” as last character to avoid awkward “....”.
.replace(/(\s+\S(?=\S)|\s*)\.?.$/, '…')
);
}

return str;
}

export function getEntityInfo(data?: FetchedEntityData) {
if (!data) {
return '';
Expand Down
15 changes: 15 additions & 0 deletions packages/rich-text/src/plugins/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@ const isResourceLink = (link: EntityLink | EntryLink | ResourceLink): link is Re

export const getLinkEntityId = (link: EntityLink | ResourceLink): string =>
isResourceLink(link) ? link.sys.urn : link.sys.id;

export function truncateTitle(str: string, length: number) {
if (typeof str === 'string' && str.length > length) {
return (
str &&
str
.substr(0, length + 1) // +1 to look ahead and be replaced below.
// Get rid of orphan letters but not one letter words (I, a, 2).
// Try to not have “.” as last character to avoid awkward “....”.
.replace(/(\s+\S(?=\S)|\s*)\.?.$/, '…')
);
}

return str;
}

0 comments on commit 3e897e7

Please sign in to comment.