install-button.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import type { FlashState } from "./const";
  2. import type { EwtInstallDialog } from "./install-dialog";
  3. import { connect } from "./connect";
  4. export class InstallButton extends HTMLElement {
  5. public static isSupported = "serial" in navigator;
  6. public static isAllowed = window.isSecureContext;
  7. private static style = `
  8. button {
  9. position: relative;
  10. cursor: pointer;
  11. font-size: 14px;
  12. font-weight: 500;
  13. padding: 10px 24px;
  14. color: var(--esp-tools-button-text-color, #fff);
  15. background-color: var(--esp-tools-button-color, #03a9f4);
  16. border: none;
  17. border-radius: var(--esp-tools-button-border-radius, 9999px);
  18. }
  19. button::before {
  20. content: " ";
  21. position: absolute;
  22. top: 0;
  23. bottom: 0;
  24. left: 0;
  25. right: 0;
  26. opacity: 0.2;
  27. border-radius: var(--esp-tools-button-border-radius, 9999px);
  28. }
  29. button:hover::before {
  30. background-color: rgba(255,255,255,.8);
  31. }
  32. button:focus {
  33. outline: none;
  34. }
  35. button:focus::before {
  36. background-color: white;
  37. }
  38. button:active::before {
  39. background-color: grey;
  40. }
  41. :host([active]) button {
  42. color: rgba(0, 0, 0, 0.38);
  43. background-color: rgba(0, 0, 0, 0.12);
  44. box-shadow: none;
  45. cursor: unset;
  46. pointer-events: none;
  47. }
  48. .hidden {
  49. display: none;
  50. }`;
  51. public manifest?: string;
  52. public eraseFirst?: boolean;
  53. public hideProgress?: boolean;
  54. public showLog?: boolean;
  55. public logConsole?: boolean;
  56. public state?: FlashState;
  57. public renderRoot?: ShadowRoot;
  58. public overrides: EwtInstallDialog["overrides"];
  59. public connectedCallback() {
  60. if (this.renderRoot) {
  61. return;
  62. }
  63. this.renderRoot = this.attachShadow({ mode: "open" });
  64. if (!InstallButton.isSupported || !InstallButton.isAllowed) {
  65. this.toggleAttribute("install-unsupported", true);
  66. this.renderRoot.innerHTML = !InstallButton.isAllowed
  67. ? "<slot name='not-allowed'>You can only install ESP devices on HTTPS websites or on the localhost.</slot>"
  68. : "<slot name='unsupported'>Your browser does not support installing things on ESP devices. Use Google Chrome or Microsoft Edge.</slot>";
  69. return;
  70. }
  71. this.toggleAttribute("install-supported", true);
  72. const slot = document.createElement("slot");
  73. slot.addEventListener("click", async (ev) => {
  74. ev.preventDefault();
  75. connect(this);
  76. });
  77. slot.name = "activate";
  78. const button = document.createElement("button");
  79. button.innerText = "Connect";
  80. slot.append(button);
  81. if (
  82. "adoptedStyleSheets" in Document.prototype &&
  83. "replaceSync" in CSSStyleSheet.prototype
  84. ) {
  85. const sheet = new CSSStyleSheet();
  86. sheet.replaceSync(InstallButton.style);
  87. this.renderRoot.adoptedStyleSheets = [sheet];
  88. } else {
  89. const styleSheet = document.createElement("style");
  90. styleSheet.innerText = InstallButton.style;
  91. this.renderRoot.append(styleSheet);
  92. }
  93. this.renderRoot.append(slot);
  94. }
  95. }
  96. function defineInstallButton() {
  97. customElements.define("esp-web-install-button", InstallButton);
  98. }
  99. async function polyfillWebSerial() {
  100. const { serial } = await import("web-serial-polyfill");
  101. // @ts-ignore-next-line
  102. navigator.serial = serial;
  103. InstallButton.isSupported = true;
  104. defineInstallButton();
  105. }
  106. if (!navigator.serial && navigator.usb) {
  107. polyfillWebSerial();
  108. } else {
  109. defineInstallButton();
  110. }