Skip to content

Commit 142a369

Browse files
committed
fix(types): better typing for the send messages (was only partial correct)
1 parent 404802b commit 142a369

File tree

7 files changed

+35
-37
lines changed

7 files changed

+35
-37
lines changed

src/constants.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
export const DATA_CURR_FIELD_ID = 'current-data-contentful-field-id';
2-
export const DATA_CURR_ENTRY_ID = 'current-data-contentful-entry-id';
3-
export const DATA_CURR_LOCALE = 'current-data-contentful-locale';
1+
import { TagAttributes } from './types';
2+
3+
export const DATA_CURR_FIELD_ID = `current-${TagAttributes.FIELD_ID}`;
4+
export const DATA_CURR_ENTRY_ID = `current-${TagAttributes.ENTRY_ID}`;
5+
export const DATA_CURR_LOCALE = `current-${TagAttributes.LOCALE}`;
46
export const TOOLTIP_CLASS = 'contentful-tooltip';
57

68
export const TOOLTIP_HEIGHT = 32;

src/field-tagging.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,9 @@ export class FieldTagging {
112112
if (fieldId && entryId && locale) {
113113
sendMessageToEditor({
114114
action: 'TAGGED_FIELD_CLICKED',
115-
data: {
116-
fieldId,
117-
entryId,
118-
locale,
119-
},
115+
fieldId,
116+
entryId,
117+
locale,
120118
});
121119
}
122120
}

src/graphql/__tests__/entries.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ describe('Update GraphQL Entry', () => {
128128
});
129129
expect(sendMessageToEditor).toHaveBeenCalledWith({
130130
action: 'ENTITY_NOT_KNOWN',
131-
data: {
132-
referenceEntityId: '18kDTlnJNnDIJf6PsXE5Mr',
133-
},
131+
referenceEntityId: '18kDTlnJNnDIJf6PsXE5Mr',
134132
});
135133
});
136134

@@ -347,9 +345,7 @@ describe('Update GraphQL Entry', () => {
347345
});
348346
expect(sendMessageToEditor).toHaveBeenCalledWith({
349347
action: 'ENTITY_NOT_KNOWN',
350-
data: {
351-
referenceEntityId: '3JqLncpMbnZYrCPebujXhK',
352-
},
348+
referenceEntityId: '3JqLncpMbnZYrCPebujXhK',
353349
});
354350
});
355351

src/graphql/entries.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ function updateReferenceField(
110110
// where we can calculate the typename on the next update message.
111111
sendMessageToEditor({
112112
action: 'ENTITY_NOT_KNOWN',
113-
data: {
114-
referenceEntityId: updatedReference.sys.id,
115-
},
113+
referenceEntityId: updatedReference.sys.id,
116114
});
117115
return null;
118116
}

src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ export class ContentfulLivePreview {
2929

3030
sendMessageToEditor({
3131
action: 'IFRAME_CONNECTED',
32-
data: {
33-
connected: true,
34-
tags: document.querySelectorAll(`[${TagAttributes.ENTRY_ID}]`).length,
35-
},
32+
connected: true,
33+
tags: document.querySelectorAll(`[${TagAttributes.ENTRY_ID}]`).length,
3634
});
3735

3836
return Promise.resolve(ContentfulLivePreview.fieldTagging);

src/types.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ export type LivePreviewProps = {
88
locale: string | null | undefined;
99
};
1010

11-
export enum TagAttributes {
11+
export const enum TagAttributes {
1212
FIELD_ID = 'data-contentful-field-id',
1313
ENTRY_ID = 'data-contentful-entry-id',
1414
LOCALE = 'data-contentful-locale',
1515
}
1616

17-
// TODO: can we add sys and optional typename to the Entity?
17+
// TODO: this has kind of overlap with CollectionItem, can we combine them?
1818
export type Entity = Record<string, unknown>;
1919
export type Argument = Entity | Entity[];
2020
export type SubscribeCallback = (data: Argument) => void;
@@ -31,17 +31,23 @@ export interface CollectionItem {
3131

3232
type IframeConnectedMessage = {
3333
action: 'IFRAME_CONNECTED';
34-
data: { connected: true; tags: number };
34+
connected: true;
35+
tags: number;
3536
};
3637
type TaggedFieldClickMessage = {
3738
action: 'TAGGED_FIELD_CLICKED';
38-
data: { fieldId: string; entryId: string; locale: string };
39+
fieldId: string;
40+
entryId: string;
41+
locale: string;
3942
};
4043
type UnknownEntityMessage = {
4144
action: 'ENTITY_NOT_KNOWN';
42-
data: { referenceEntityId: string };
45+
referenceEntityId: string;
4346
};
44-
4547
export type EditorMessage = IframeConnectedMessage | TaggedFieldClickMessage | UnknownEntityMessage;
48+
export type MessageFromSDK = EditorMessage & {
49+
from: 'live-preview';
50+
location: string;
51+
};
4652

4753
export class EntryReferenceMap extends Map<string, EntryProps | AssetProps> {}

src/utils.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import type { EditorMessage } from './types';
1+
import type { EditorMessage, MessageFromSDK } from './types';
22

33
/**
44
* Sends the given message to the editor
55
* enhances it with the information necessary to be accepted
66
*/
7-
export function sendMessageToEditor({ action, data }: EditorMessage): void {
7+
export function sendMessageToEditor(data: EditorMessage): void {
8+
const message: MessageFromSDK = {
9+
...data,
10+
from: 'live-preview',
11+
location: window.location.href,
12+
};
13+
814
window.top?.postMessage(
9-
{
10-
from: 'live-preview',
11-
location: window.location.href,
12-
action,
13-
...data,
14-
},
15-
// TODO: check if there is any security risk with this
16-
'*'
15+
message,
16+
'*' // TODO: check if there is any security risk with this
1717
);
1818
}
1919

0 commit comments

Comments
 (0)