Skip to content

Commit

Permalink
feat: send tagged field info on click (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
MayaGillilan authored Feb 28, 2023
1 parent fdf3802 commit af0bb86
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/features/tagging/getLivePreviewProps.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, it, expect } from 'vitest';
import { describe, it, expect, vi } from 'vitest';

import { getLivePreviewProps } from './getLivePreviewProps';

describe('getLivePreviewProps', () => {
Expand All @@ -16,6 +17,7 @@ describe('getLivePreviewProps', () => {
'data-contentful-field-id': fieldId,
'data-contentful-entry-id': entryId,
'data-contentful-locale': locale,
onClick: vi.spyOn(result, 'onClick'),
});
});
});
22 changes: 19 additions & 3 deletions src/features/tagging/getLivePreviewProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@ export type LivePreviewProps = {
locale: string | null | undefined;
};

enum TagAttributes {
FIELD_ID = 'data-contentful-field-id',
ENTRY_ID = 'data-contentful-entry-id',
LOCALE = 'data-contentful-locale',
}

export const getLivePreviewProps = ({ fieldId, entryId, locale }: LivePreviewProps) => {
return {
'data-contentful-field-id': fieldId,
'data-contentful-entry-id': entryId,
'data-contentful-locale': locale,
[TagAttributes.FIELD_ID]: fieldId,
[TagAttributes.ENTRY_ID]: entryId,
[TagAttributes.LOCALE]: locale,
onClick: () =>
window.top?.postMessage(
{
fieldId,
entryId,
locale,
},
//todo: check if there is any security risk with this
'*'
),
};
};

0 comments on commit af0bb86

Please sign in to comment.