Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove block relationships in document field #4398

Merged
merged 2 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 18 additions & 43 deletions packages-next/fields-document/src/DocumentEditor/relationship.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ export type Relationships = Record<
kind: 'inline';
label: string;
}
| {
kind: 'block';
label: string;
many: boolean;
}
| {
kind: 'prop';
many: boolean;
Expand All @@ -68,16 +63,12 @@ export function useDocumentFieldRelationships() {
export const DocumentFieldRelationshipsProvider = DocumentFieldRelationshipsContext.Provider;

export function withRelationship(relationships: Relationships, editor: ReactEditor) {
const { isVoid, isInline } = editor;
const { isVoid } = editor;
editor.isVoid = element => {
return element.type === 'relationship' || isVoid(element);
};
editor.isInline = element => {
return (
(element.type === 'relationship' &&
relationships[element.relationship as string]?.kind === 'inline') ||
isInline(element)
);
return element.type === 'relationship';
};
return editor;
}
Expand Down Expand Up @@ -119,18 +110,16 @@ export function RelationshipElement({ attributes, children, element }: RenderEle
return (
<span
{...attributes}
css={
relationship.kind === 'inline' && {
display: 'inline-flex',
alignItems: 'center',
}
}
css={{
display: 'inline-flex',
alignItems: 'center',
}}
>
<span
contentEditable={false}
css={{
userSelect: 'none',
width: relationship.kind === 'inline' ? 200 : '100%',
width: 200,
display: 'inline-block',
paddingLeft: 4,
paddingRight: 4,
Expand All @@ -142,31 +131,17 @@ export function RelationshipElement({ attributes, children, element }: RenderEle
controlShouldRenderValue
isDisabled={false}
list={keystone.adminMeta.lists[relationship.listKey]}
state={
relationship.kind === 'block' && relationship.many
? {
kind: 'many',
value: (element.data as any) || [],
onChange(value) {
Transforms.setNodes(
editor,
{ data: value },
{ at: ReactEditor.findPath(editor, element) }
);
},
}
: {
kind: 'one',
value: (element.data as any) || null,
onChange(value) {
Transforms.setNodes(
editor,
{ data: value },
{ at: ReactEditor.findPath(editor, element) }
);
},
}
}
state={{
kind: 'one',
value: (element.data as any) || null,
onChange(value) {
Transforms.setNodes(
editor,
{ data: value },
{ at: ReactEditor.findPath(editor, element) }
);
},
}}
/>
) : (
'Invalid relationship'
Expand Down
5 changes: 0 additions & 5 deletions packages-next/fields-document/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ type RelationshipsConfig = Record<
kind: 'inline';
label: string;
}
| {
kind: 'block';
label: string;
many?: true;
}
| {
kind: 'prop';
many?: true;
Expand Down
8 changes: 2 additions & 6 deletions packages-next/fields-document/src/relationship-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ export function removeRelationshipData(nodes: Node[]): Node[] {
if (node.type === 'relationship') {
return {
...node,
data: Array.isArray(node.data)
? node.data.filter(x => x.id != null)
: (node.data as any)?.id != null
? { id: (node.data as any).id }
: null,
data: (node.data as any)?.id != null ? { id: (node.data as any).id } : null,
};
}
if (node.type === 'component-block') {
Expand Down Expand Up @@ -50,7 +46,7 @@ export function addRelationshipData(
gqlNames: (listKey: string) => GqlNames
): Promise<Node[]> {
let fetchData = async (relationship: Relationships[string], data: any) => {
if (relationship.kind !== 'inline' && relationship.many) {
if (relationship.kind === 'prop' && relationship.many) {
const ids = Array.isArray(data) ? data.filter(item => item.id != null).map(x => x.id) : [];

if (ids.length) {
Expand Down