Skip to content

iframe Import ​

Integration Flow ​

  1. Load the import page through an iframe.
html
<iframe src="${domain_name}/app/import"></iframe>
  1. Generate a file file_key through OpenAPI. See Create File, then pass the data to Pixso.

  2. Call Pixso through postMessage.

js
iframe.contentWindow.postMessage({
    name: "/file/convert";
    type: "msg-api";
    data: {
        files: [{
            fileKey: string,
            name: "pixso.pix",
            buffer: ArrayBuffer,
            isHtml: true // Whether to import an Axure file as an HTML file.
	}],
    };
}, *);
  1. Listen for file import status changes. When the status of the corresponding file_key is success, the import has succeeded.
js
window.addEventListener("message", (event) => {
  if (event.data.name === "/file/convert") {
    const {
      data: { status, message, progress },
    } = event.data;
    // do something
  }
});

API Type Definitions ​

ts
// External page sends file information to Pixso.
interface FileConvertReqMsg {
  name: "/file/convert";
  type: "msg-api";
  data: {
    files: {
      fileKey: string; // fileKey created by the backend.
      name: string; // imported file name. The file extension is required.
      buffer: ArrayBuffer; // imported file data.
      isHtml?: boolean; // When importing an Axure file, whether to import it as HTML. This option has no effect on other file types.
      libraryType?: "library" | "design" | "normal"; // imported file type. Default is normal. library means resource library, design means design file. Only valid for Sketch files. file_key must follow the specification.
    }[];
  };
}

// Pixso returns file import status to the external page. Every file status change triggers a notification.
interface FileConvertResMsg {
  name: "/file/convert";
  type: "msg-event";
  data: {
    fileKey: string;
    status: "loading" | "success" | "error";
    // When status is loading, message is a progress enum value.
    // When status is error, message is the error reason.
    message?: ProgreesMessage | string;
    progress?: string; // returned when status is loading.
  };
}

ProgreesMessage Enum ​

EnumDescriptionProgress format
NONEnonenone
DECOMPRESSdecompresspercentage
PARSE_FILEparsepercentage
UPLOAD_PREVIEWupload cover imagepercentage
UPLOAD_FILEupload filepercentage
UPLOAD_IMAGESupload imagesprogress (1/n)

Progress Aggregation ​

You can aggregate stage progress into a single progress bar by referring to the following code.

ts
const progressMap = new Map<string, string>();
const updateProgress = (
  fileKey: string,
  message: ProgreesMessage,
  progress: string
) => {
  const ratio = {
    DECOMPRESS: 0.1,
    PARSE_FILE: 0.5,
    UPLOAD_PREVIEW: 0.1,
    UPLOAD_FILE: 0.1,
    UPLOAD_IMAGES: 0.2,
  };

  switch (message) {
    case "DECOMPRESS":
      const cur = ratio.DECOMPRESS * parseFloat(progress);
      progressMap.set(fileKey, cur.toFixed(2) + "%");
      break;
    case "PARSE_FILE":
      const cur = ratio.DECOMPRESS + ratio.PARSE_FILE * parseFloat(progress);
      progressMap.set(fileKey, cur.toFixed(2) + "%");
      break;
    case "UPLOAD_PREVIEW":
      const cur =
        ratio.DECOMPRESS +
        ratio.PARSE_FILE +
        ratio.UPLOAD_PREVIEW * parseFloat(progress);
      progressMap.set(fileKey, cur.toFixed(2) + "%");
      break;
    case "UPLOAD_FILE":
      const cur =
        ratio.DECOMPRESS +
        ratio.PARSE_FILE +
        ratio.UPLOAD_PREVIEW +
        ratio.UPLOAD_FILE * parseFloat(progress);
      progressMap.set(fileKey, cur.toFixed(2) + "%");
      break;
    case "UPLOAD_IMAGES":
      const [_, total] = progress.split("/");
      const cur =
        ratio.DECOMPRESS +
        ratio.PARSE_FILE +
        ratio.UPLOAD_PREVIEW +
        ratio.UPLOAD_FILE +
        ratio.UPLOAD_IMAGES * (1 / total) * 100;
      progressMap.set(fileKey, cur.toFixed(2) + "%");
      break;
    default:
      break;
  }
};

Sketch Import ​

Normal Sketch Import ​

Generate a file file_key through OpenAPI, then import it.

ts
postMessage({
    name: "/file/convert";
    type: "msg-api";
    data: {
        files: [{
            fileKey: string,
            name: "normal.sketch", // The file extension must be .sketch.
            buffer: ArrayBuffer,
	}],
    };
}, *);

Sketch Design File Import ​

Generate a file file_key through OpenAPI. Compared with normal import, this mode preserves instance associations.

ts
postMessage({
    name: "/file/convert";
    type: "msg-api";
    data: {
        files: [{
            fileKey: string,
            name: "design.sketch",
            buffer: ArrayBuffer,
			libraryType: "design"
	}],
    };
}, *);

Sketch Library Import ​

  1. Parse the Sketch library to obtain the unique library file_key and the isExists flag.
ts
postMessage({
    name: "/file/parse";
    type: "msg-api";
    data: {
       name: "library.sketch",
       buffer: ArrayBuffer,
    };
}, *);
  1. If isExists is true, the library already exists and continuing will overwrite it. If isExists is false, create the file_key parsed in step 1 through OpenAPI.

  2. Import the Sketch file as a library. Set libraryType to library.

ts
postMessage({
    name: "/file/convert";
    type: "msg-api";
    data: {
        files: [{
            fileKey: string,
            name: "library.sketch",
            buffer: ArrayBuffer,
			libraryType: "library"
	}],
    };
}, *);