How to use the codesandbox-api.listen function in codesandbox-api

To help you get started, we’ve selected a few codesandbox-api examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github codesandbox / codesandbox-client / src / sandbox / index.js View on Github external
}

        try {
          dispatch({
            type: 'eval-result',
            error,
            result: transformJSON(result),
          });
        } catch (e) {
          console.error(e);
        }
      }
    }
  }

  listen(handleMessage);

  sendReady();
  setupHistoryListeners();
  setupConsole();

  if (process.env.NODE_ENV === 'test' || isStandalone) {
    // We need to fetch the sandbox ourselves...
    const id = getId();
    window
      .fetch(host + `/api/v1/sandboxes/${id}`)
      .then(res => res.json())
      .then(res => camelizeKeys(res))
      .then(x => {
        const mainModule = findMainModule(
          x.data.modules,
          x.data.directories,
github codesandbox / codesandbox-client / packages / app / src / sandbox / index.js View on Github external
dispatch({
            type: 'transpiler-context',
            data: context,
          });
        } else {
          dispatch({
            type: 'transpiler-context',
            data: {},
          });
        }
      }
    }
  }

  if (!isStandalone) {
    listen(handleMessage);

    sendReady();

    if (!window.opener) {
      // Means we're in the editor
      setupHistoryListeners();
      setupConsole();
      listenForPreviewSecret();
      window.addEventListener('message', ({ data }) => {
        switch (data.type) {
          case 'activate':
            activate(window);
            break;
          default:
            break;
        }
github codesandbox / codesandbox-client / packages / app / src / app / overmind / effects / vscode / editorManager / index.ts View on Github external
private initializeCodeSandboxAPIListener() {
    return listen(({ action, type, code, path, lineNumber, column }: any) => {
      if (type === 'add-extra-lib') {
        // TODO: bring this func back
        // const dtsPath = `${path}.d.ts`;
        // this.monaco.languages.typescript.typescriptDefaults._extraLibs[
        //   `file:///${dtsPath}`
        // ] = code;
        // this.commitLibChanges();
      } else if (action === 'editor.open-module') {
        const options: {
          selection?: { startLineNumber: number; startColumn: number };
        } = {};

        if (lineNumber || column) {
          options.selection = {
            startLineNumber: lineNumber,
            startColumn: column || 0,
github codesandbox / codesandbox-client / packages / app / src / app / components / CodeEditor / VSCode / index.tsx View on Github external
setupTranspilationListener() {
    // @ts-ignore
    return listen(({ type, code, path }) => {
      if (type === 'add-extra-lib') {
        // TODO; bring this func back
        // const dtsPath = `${path}.d.ts`;
        // this.monaco.languages.typescript.typescriptDefaults._extraLibs[
        //   `file:///${dtsPath}`
        // ] = code;
        // this.commitLibChanges();
      }
    });
  }
github codesandbox / codesandbox-client / packages / app / src / app / components / CodeEditor / VSCode / index.js View on Github external
setupTranspilationListener() {
    return listen(({ type, code, path }) => {
      if (type === 'add-extra-lib') {
        const dtsPath = `${path}.d.ts`;
        this.monaco.languages.typescript.typescriptDefaults._extraLibs[
          `file:///${dtsPath}`
        ] = code;
        this.commitLibChanges();
      }
    });
  }
github gitlabhq / gitlabhq / app / assets / javascripts / ide / components / preview / clientside.vue View on Github external
.then(() => {
          this.initManager('#ide-preview', this.sandboxOpts, {
            fileResolver: {
              isFile: p => Promise.resolve(!!this.entries[createPathWithExt(p)]),
              readFile: p => this.loadFileContent(createPathWithExt(p)).then(content => content),
            },
          });

          this.listener = listen(e => {
            switch (e.type) {
              case 'done':
                this.sandpackReady = true;
                break;
              default:
                break;
            }
          });
        });
    },
github codesandbox / codesandbox-client / packages / sandbox-hooks / url-listeners.js View on Github external
sendUrlChange(document.location.href);
          }
          ev.preventDefault();
          ev.stopPropagation();
        }
      },
      true
    );

    pushHistory(pathWithHash(document.location), null);

    setTimeout(() => {
      sendUrlChange(document.location.href);
    });
  }
  return listen(handleMessage);
}
github codesandbox / codesandbox-client / standalone-packages / react-sandpack / src / components / Navigator / Navigator.tsx View on Github external
constructor(props: Props) {
    super(props);
    this.listener = listen(this.handleMessage);

    this.state = {
      browserPath: '/',
      baseUrl: '',
      lastCommittedUrl: '/',
      backwardNavigationStack: [],
      forwardNavigationStack: [],
    };
  }
github codesandbox / codesandbox-client / standalone-packages / sandpack / src / manager / index.ts View on Github external
}

      this.element = element;
      this.iframe = document.createElement('iframe');
      this.initializeElement();
    } else {
      this.element = selector;
      this.iframe = selector;
    }
    this.iframe.setAttribute(
      'sandbox',
      'allow-forms allow-scripts allow-same-origin allow-modals allow-popups allow-presentation'
    );
    this.iframe.src = this.bundlerURL;

    this.listener = listen((message: any) => {
      switch (message.type) {
        case 'initialized': {
          if (this.iframe) {
            if (this.iframe.contentWindow) {
              registerFrame(this.iframe.contentWindow, this.bundlerURL);

              if (this.options.fileResolver) {
                this.fileResolverProtocol = new Protocol(
                  'file-resolver',
                  async (data: { m: 'isFile' | 'readFile'; p: string }) => {
                    if (data.m === 'isFile') {
                      return this.options.fileResolver!.isFile(data.p);
                    }

                    return this.options.fileResolver!.readFile(data.p);
                  },

codesandbox-api

[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![Greenkeeper badge](https://badges.greenkeeper.io/alexjoverm/typescript-library-starter.svg)](https://greenkeeper.io/) [![Travi

MIT
Latest version published 3 years ago

Package Health Score

73 / 100
Full package analysis

Similar packages