|
| 1 | +((global, factory) => { |
| 2 | + "use strict"; |
| 3 | + typeof exports === "object" && typeof module !== "undefined" |
| 4 | + ? (module.exports = factory( |
| 5 | + require("global/window"), |
| 6 | + require("global/document") |
| 7 | + )) |
| 8 | + : typeof define === "function" && define.amd |
| 9 | + ? define(["global/window", "global/document"], factory) |
| 10 | + : factory(global.window, global.document); |
| 11 | + try { |
| 12 | + global.RdcEnv = document.createElement("rdc-env"); |
| 13 | + global.document.body.appendChild(global.RdcEnv); |
| 14 | + } catch (error) { |
| 15 | + console.error('📢对不起:RdcEnv组件加载失败!\n\t解决方案1:将引入的<script src="./red-env.js">标签的位置移到<body>标签的最后面!\n\t解决方案2:在引入的<script async src="./rdc-env.js">标签中,加上async属性!\n', error); |
| 16 | + } |
| 17 | +})( |
| 18 | + typeof window !== "undefined" ? window : globalThis || global || self, |
| 19 | + (win, doc) => { |
| 20 | + "use strict"; |
| 21 | + win.customElements.define( |
| 22 | + "rdc-env", |
| 23 | + class extends HTMLElement { |
| 24 | + static get observedAttributes() { |
| 25 | + return { |
| 26 | + env: String, |
| 27 | + obj: { |
| 28 | + type: Number, |
| 29 | + readonly: Boolean, |
| 30 | + }, |
| 31 | + }; |
| 32 | + } |
| 33 | + constructor(ops) { |
| 34 | + super(ops); |
| 35 | + this.oStyle = doc.createElement("style"); |
| 36 | + this.oDialog = doc.createElement("dialog"); |
| 37 | + this.render(); |
| 38 | + } |
| 39 | + create() { |
| 40 | + this.oDialog.open = true; |
| 41 | + this.oStyle.type = "text/css"; |
| 42 | + this.oStyle.innerHTML = `.env-dialog{position:fixed;top:0;left:0;display:flex;justify-content:center;align-items:center;pointer-events:none;width:100vw;height:20px;color:#fff;border:0;background:rgba(0, 200, 0, 0.5);}`; |
| 43 | + this.oDialog.className = "env-dialog"; |
| 44 | + this.oDialog.innerText = `开发环境!`; |
| 45 | + return this; |
| 46 | + } |
| 47 | + render() { |
| 48 | + this.create(); |
| 49 | + const shadow = this.attachShadow({ mode: "open" }); |
| 50 | + shadow.appendChild(this.oStyle); |
| 51 | + shadow.appendChild(this.oDialog); |
| 52 | + return shadow; |
| 53 | + } |
| 54 | + show(env) { |
| 55 | + const arr = location.origin.match(/dev|test/g); |
| 56 | + this.oDialog.innerText = `${env ? env : arr ? arr[0] : "•••"} 环境!`; |
| 57 | + this.style.display = "block"; |
| 58 | + } |
| 59 | + hide() { |
| 60 | + this.style.display = "none"; |
| 61 | + } |
| 62 | + connectedCallback() { |
| 63 | + this.addEventListener("click", () => { |
| 64 | + this.hide(); |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | + ); |
| 69 | + } |
| 70 | +); |
0 commit comments