import Clipboard from "clipboard";
import { queenApi } from "queenjs";

export const clipboard = {
  copy(text: string) {
    return new Promise((resolve) => {
      const btn = document.createElement("button");
      btn.style.display = "none";
      btn.setAttribute("data-clipboard-text", text);
      document.body.appendChild(btn);
      const clip = new Clipboard(btn);
      clip.on("success", function (e) {
        e.clearSelection();
        document.body.removeChild(btn);
        queenApi.messageSuccess("复制成功");
        resolve(true);
      });
      clip.on("error", function () {
        document.body.removeChild(btn);
        queenApi.messageError("复制失败");
        resolve(false);
      });
      btn.click();
    });
  },
};
