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

Documentation: Iframe Drop target #1175

Open
SOG-web opened this issue Aug 16, 2024 · 0 comments
Open

Documentation: Iframe Drop target #1175

SOG-web opened this issue Aug 16, 2024 · 0 comments
Labels
triage New issues that needs consideration

Comments

@SOG-web
Copy link

SOG-web commented Aug 16, 2024

please am trying to make an iframe my drop target, is it possible with sandpack

import {
  SandpackLayout,
  SandpackCodeEditor,
  SandpackPreview,
  SandpackStack,
  useSandpack,
  SandpackConsole,
} from "@codesandbox/sandpack-react";
import { SandpackFileExplorer } from "sandpack-file-explorer";
import { autocompletion, completionKeymap } from "@codemirror/autocomplete";
import { useEffect, useRef } from "react";
import CustomSandPackProvider from "./sandpack-provider";
import {
  dropTargetForElements,
  monitorForElements,
} from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
import invariant from "tiny-invariant";

export default function CustomSandPack() {
  const { sandpack, listen } = useSandpack();
  const { files, activeFile, clients } = sandpack;
  const previewRef = useRef(null);

  useEffect(() => {
    if (previewRef.current && clients[previewRef.current["clientId"]]) {
      const el = clients[previewRef.current["clientId"]].iframe;
      invariant(el);

      return dropTargetForElements({
        element: el,
        getData: () => ({ id: "root" }),
        onDragEnter: ({ source }) => {
          console.log("source", source);
        },
        onDragLeave: () => {
          console.log("leave");
        },
        onDrop: () => {
          console.log("drop");
        },
        canDrop: ({ source }) => {
          console.log("source", source);
          return true;
        },
      });
    }
  }, [previewRef, clients]);

  useEffect(() => {
    return monitorForElements({
      onDrop: (data) => {
        console.log("Dropped", data);
      },
      onDragStart: ({ source }) => {
        console.log("Drag Start", source, location);
      },
    });
  }, [clients]);

  useEffect(() => {
    console.log(files[activeFile].code);
  }, [sandpack, activeFile, files]);
  return (
    // <SandpackThemeProvider theme={nightOwl}>
    <SandpackStack>
      <SandpackLayout
        style={{
          display: "flex",
          minWidth: "100%",
          flexDirection: "row",
        }}
      >
        <div
          style={{
            display: "flex",
            minHeight: "300px",
            height: "100dvh",
            minWidth: "600px",
            backgroundColor: `var(--sp-colors-surface1)`,
          }}
        >
          <div
            style={{
              minWidth: 150,
              maxWidth: "300px",
              overflow: "hidden",
            }}
          >
            <SandpackFileExplorer />
          </div>
          <div style={{ flex: "min-content" }}>
            <SandpackCodeEditor
              extensions={[autocompletion()]}
              extensionsKeymap={[completionKeymap]}
              wrapContent
              style={{
                minHeight: "100%",
                maxHeight: "100%",
                overflow: "auto",
              }}
              showTabs
              closableTabs
              showInlineErrors
              showLineNumbers
              showRunButton
            />
          </div>
        </div>
        <div
          style={{
            overflow: "auto",
            height: "100dvh",
            backgroundColor: "white",
          }}
          className="bg-white flex flex-col flex-1"
        >
          <SandpackPreview
            ref={previewRef}
            style={{
              overflow: "auto",
              height: "80%",
              width: "100%",
              backgroundColor: "white",
            }}
            showNavigator
            showOpenNewtab
            showOpenInCodeSandbox={false}
            actionsChildren={
              <button onClick={() => window.alert("Bug reported!")}>
                Report bug
              </button>
            }
          />
          <SandpackConsole className="h-[20%] w-[100%] text-white" />
        </div>
      </SandpackLayout>
    </SandpackStack>
    // </SandpackThemeProvider>
  );
}

export function Sandpack() {
  return (
    <CustomSandPackProvider>
      <CustomSandPack />
    </CustomSandPackProvider>
  );
}
@SOG-web SOG-web added the triage New issues that needs consideration label Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage New issues that needs consideration
Projects
None yet
Development

No branches or pull requests

1 participant