Skip to content

Commit

Permalink
Remove block relationships in document field (#4398)
Browse files Browse the repository at this point in the history
* Remove block relationships in document field

* Remove block relationship in example
  • Loading branch information
emmatown authored Nov 26, 2020
1 parent d74d7c9 commit 81cef4a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 61 deletions.
7 changes: 0 additions & 7 deletions examples-next/basic/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,6 @@ export const lists = createSchema({
labelField: 'name',
listKey: 'User',
},
manyBlockMentions: {
kind: 'block',
label: 'Many mentions',
labelField: 'name',
listKey: 'User',
many: true,
},
featuredAuthors: {
kind: 'prop',
labelField: 'name',
Expand Down
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

0 comments on commit 81cef4a

Please sign in to comment.