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

Add optional aspect ratio to url parameters #576

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 13 additions & 2 deletions integrations/toucantoco/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type ToucanRuntimeContext = RuntimeContext<ToucanRuntimeEnvironment>;
const embedBlock = createComponent<{
toucanId?: string;
url?: string;
height?: number;
width?: number;
}>({
componentId: 'embed',

Expand All @@ -41,7 +43,16 @@ const embedBlock = createComponent<{

async render(element, context) {
const { environment } = context;
const { toucanId, url } = element.props;
const { toucanId, url, height, width } = element.props;

function getAspectRatio(height?: number, width?: number): number {
if (height && width && height > 0 && width > 0) {
return width / height;
}
return 1;
}

const aspectRatio = getAspectRatio(height, width);

if (!toucanId || !url) {
return (
Expand Down Expand Up @@ -77,7 +88,7 @@ const embedBlock = createComponent<{
source={{
url,
}}
aspectRatio={1}
aspectRatio={aspectRatio}
/>
</block>
);
Expand Down
7 changes: 6 additions & 1 deletion integrations/toucantoco/src/toucan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ export function extractToucanInfoFromURL(input: string):
| undefined
| {
toucanId?: string;
height?: number;
width?: number;
} {
const url = new URL(input);

// Ignore non-TT URLs
const toucanId = url.searchParams.get('id');
let height = Number(url.searchParams.get('height')) || 100;
let width = Number(url.searchParams.get('width')) || 100;

if (!toucanId || !url.hostname.endsWith('.toucantoco.com')) {
return;
}

return { toucanId };
return { toucanId, height, width };
}