useKrpano.ts 697 B

1234567891011121314151617181920212223242526
  1. import { useEffect, useRef } from "react";
  2. import { IKrpanoConfig } from "../types";
  3. export const useKrpano = (config: IKrpanoConfig): void => {
  4. const isCreated = useRef(false);
  5. useEffect(() => {
  6. const defaultConfig: Partial<IKrpanoConfig> = {
  7. html5: "auto",
  8. xml: null,
  9. mobilescale: 1,
  10. };
  11. const embedpano = () => {
  12. if (typeof window.embedpano === "function")
  13. window.embedpano(Object.assign({}, defaultConfig, config));
  14. };
  15. if (typeof window.embedpano === "function") {
  16. (config.xml || !isCreated.current) && embedpano();
  17. isCreated.current = true;
  18. } else {
  19. throw new Error("Krpano required");
  20. }
  21. }, [config]);
  22. };