-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close #154 While Sketch provide neither their oEmbed endpoint nor developer document about embedding, it seems their URL schema is simple and easy to generate embed URL from it.
- Loading branch information
Showing
4 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
packages/storybook-addon-designs/src/register/components/Sketch.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** @jsx jsx */ | ||
import { Link, Placeholder } from "@storybook/components"; | ||
import { jsx } from "@storybook/theming"; | ||
import { FC, Fragment, ReactNode, useMemo } from "react"; | ||
|
||
import { SketchConfig, IFrameConfigBase } from "../../config"; | ||
|
||
import { IFrame } from "./IFrame"; | ||
|
||
interface ParseSucceeded<T> { | ||
valid: true; | ||
|
||
data: T; | ||
} | ||
|
||
interface ParseFailed { | ||
valid: false; | ||
|
||
error: ReactNode; | ||
} | ||
|
||
type Parser<T> = (url: URL) => ParseSucceeded<T> | ParseFailed; | ||
|
||
export const v1UrlParser: Parser<IFrameConfigBase> = (url) => { | ||
if (url.protocol !== "https:") { | ||
return { | ||
valid: false, | ||
error: ( | ||
<Fragment> | ||
Expected HTTPS link, received <code>{url.protocol}</code>. | ||
</Fragment> | ||
), | ||
}; | ||
} | ||
|
||
if (url.hostname !== "www.sketch.com") { | ||
return { | ||
valid: false, | ||
error: ( | ||
<Fragment> | ||
Expected a hostname <code>www.sketch.com</code>, received{" "} | ||
<code>{url.hostname}</code> | ||
</Fragment> | ||
), | ||
}; | ||
} | ||
|
||
const malformedUrlErrorMessage = ( | ||
<Fragment> | ||
Expected pathname <code>{"/s/<string>/a/<string>"}</code>, received{" "} | ||
<code>{url.pathname}</code>. | ||
</Fragment> | ||
); | ||
|
||
const pathSegments = url.pathname.split("/").slice(1); | ||
if (pathSegments.length < 4) { | ||
return { | ||
valid: false, | ||
error: malformedUrlErrorMessage, | ||
}; | ||
} | ||
|
||
if (pathSegments[0] === "embed") { | ||
return { | ||
valid: true, | ||
data: { | ||
url: url.href, | ||
offscreen: false, | ||
}, | ||
}; | ||
} | ||
|
||
const [s, sid, a, aid] = pathSegments; | ||
if (s !== "s" || !sid || a !== "a" || !aid) { | ||
return { | ||
valid: false, | ||
error: malformedUrlErrorMessage, | ||
}; | ||
} | ||
|
||
return { | ||
valid: true, | ||
data: { | ||
url: `https://www.sketch.com/embed/s/${sid}/a/${aid}`, | ||
offscreen: false, | ||
}, | ||
}; | ||
}; | ||
|
||
interface Props { | ||
config: SketchConfig; | ||
} | ||
|
||
export const Sketch: FC<Props> = ({ config }) => { | ||
const result = useMemo(() => { | ||
const parsed = v1UrlParser(new URL(config.url)); | ||
if (!parsed.valid) { | ||
return parsed; | ||
} | ||
|
||
return { | ||
...parsed, | ||
data: { | ||
...config, | ||
...parsed.data, | ||
}, | ||
}; | ||
}, [config]); | ||
|
||
if (!result.valid) { | ||
return ( | ||
<Placeholder> | ||
<Fragment>Invalid Sketch URL</Fragment> | ||
<Fragment>{result.error}</Fragment> | ||
</Placeholder> | ||
); | ||
} | ||
|
||
return <IFrame defer config={result.data} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters