Source

atoms/story-elements/embed/embed.tsx

  1. import React from "react";
  2. import { StoryElementProps } from "../types";
  3. import { Iframe } from "../../iframe";
  4. import atob from "atob-utf-8";
  5. import { withStoryAndConfig } from "../../../context";
  6. import get from "lodash.get";
  7. export const getIframeContent = (str, regex) => {
  8. if (/iframe/.test(str)) {
  9. const regExecArray = regex.exec(str);
  10. return regExecArray && regExecArray.length > 0 ? regExecArray[0] : null;
  11. }
  12. return null;
  13. };
  14. export const DefaultEmbed = ({ element }: StoryElementProps) => {
  15. const embedData = element["embed-js"] ? atob(element["embed-js"]) : "";
  16. const src = getIframeContent(embedData, /(?<=src=["']).*?(?=[*"'])/);
  17. const scrolling = getIframeContent(embedData, /(?<=scrolling=["']).*?(?=[*"'])/);
  18. const title = element.subtype || element.title || "";
  19. return src ? <Iframe src={src} scrolling={scrolling} title={title} /> : null;
  20. };
  21. export const getIframeSourceURL = (str: string): string | null => {
  22. if (/iframe/.test(str)) {
  23. const regExecArray = /(?<=src=").*?(?=[*"])/.exec(str);
  24. return regExecArray && regExecArray.length > 0 ? regExecArray[0] : null;
  25. }
  26. return null;
  27. };
  28. export const EmbedBase = ({ element, story, config }: StoryElementProps) => {
  29. const embedRender = get(config, ["opts", "render", "storyElementRender", "embedRender"], null);
  30. return embedRender ? embedRender({ story, config, element }) : <DefaultEmbed element={element} />;
  31. };
  32. /**
  33. * Embed is a story element.
  34. * The look of the Embed can be changed using the render prop embedRender. In case embedRender is passed in the config, it is rendered. Otherwise a default Embed is rendered.
  35. *
  36. * @param {Object} params object containing parameters passed to the render prop
  37. * @param {Object} params.story story object
  38. * @param {Object} params.config config object
  39. * @param {Object} params.element the story element. This is same as the story element found in the story API response
  40. *
  41. * ```javascript
  42. * ...
  43. * ampRoutes(app, {
  44. * render: {
  45. * storyElementRender: {
  46. * embedRender: ({ story, config, element }) => <MyCustomEmbed story={story} config={config} storyElement={element} />
  47. * }
  48. * }
  49. * })
  50. * ...
  51. * ```
  52. *
  53. * @category StoryElements
  54. * @module Embed
  55. * @component
  56. */
  57. export const Embed = withStoryAndConfig(EmbedBase);