jodit-pro
Version:
PRO Version of Jodit Editor
2 lines (1 loc) • 6.1 kB
JavaScript
var g=Object.defineProperty;var p=(l,t)=>g(l,"name",{value:t,configurable:!0});var d,u=function(l,t,e,i){var a=arguments.length,n=a<3?t:i===null?i=Object.getOwnPropertyDescriptor(t,e):i,s;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")n=Reflect.decorate(l,t,e,i);else for(var r=l.length-1;r>=0;r--)(s=l[r])&&(n=(a<3?s(n):a>3?s(t,e,n):s(t,e))||n);return a>3&&n&&Object.defineProperty(t,e,n),n};import{component as C,hook as j}from"jodit/esm/core/decorators/index.js";import{attr as v}from"jodit/esm/core/helpers/utils/attr.js";import{UIElement as F}from"jodit/esm/core/ui/index.js";let o=(d=class extends F{className(){return"UIArtifact"}constructor(t,e){super(t),this.artifact=e}onReady(){this.renderArtifact()}renderArtifact(){const t=this.container;t.innerHTML="",v(t,{"data-type":this.artifact.type,"data-status":this.artifact.status});const e=this.createStatusIndicator();if(t.appendChild(e),this.artifact.status==="pending"||this.artifact.status==="generating"){this.renderLoading();return}if(this.artifact.status==="failed"){this.renderError();return}switch(this.artifact.type){case"image":this.renderImage();break;case"audio":this.renderAudio();break;case"file":this.renderFile();break;case"json":this.renderJSON();break;default:this.renderOther()}}createStatusIndicator(){const t=this.j.c.div(this.getFullElName("status"));t.setAttribute("data-status",this.artifact.status);const e=this.j.c.span(this.getFullElName("status-icon")),i=this.j.c.span(this.getFullElName("status-text"));switch(this.artifact.status){case"pending":e.textContent="\u23F3",i.textContent=this.j.i18n("Pending");break;case"generating":e.textContent="\u{1F504}",i.textContent=this.j.i18n("Generating...");break;case"ready":e.textContent="\u2705",i.textContent=this.j.i18n("Ready");break;case"failed":e.textContent="\u274C",i.textContent=this.j.i18n("Failed");break}return t.appendChild(e),t.appendChild(i),t}renderLoading(){const t=this.j.c.div(this.getFullElName("loader")),e=this.j.c.div(this.getFullElName("spinner")),i=this.j.c.div(this.getFullElName("loader-message"));i.textContent=this.j.i18n("Generating artifact..."),t.appendChild(e),t.appendChild(i),this.container.appendChild(t)}renderError(){var t;const e=this.j.c.div(this.getFullElName("error")),i=this.j.c.div(this.getFullElName("error-icon"));i.textContent="\u274C";const a=this.j.c.div(this.getFullElName("error-message"));a.textContent=((t=this.artifact.metadata)===null||t===void 0?void 0:t.error)||this.j.i18n("Failed to generate artifact"),e.appendChild(i),e.appendChild(a),this.container.appendChild(e)}renderImage(){var t,e;if(!this.artifact.data)return;const i=this.j.c.div(this.getFullElName("content")),a=this.j.c.element("img",{class:this.getFullElName("image")});if(this.artifact.data.kind==="url")a.src=this.artifact.data.url;else if(this.artifact.data.kind==="base64"){const n=this.artifact.mimeType||"image/png";a.src=`data:${n};base64,${this.artifact.data.base64}`}if(a.alt=((t=this.artifact.metadata)===null||t===void 0?void 0:t.alt)||"AI Generated Image",i.appendChild(a),!((e=this.artifact.metadata)===null||e===void 0)&&e.caption){const n=this.j.c.div(this.getFullElName("caption"));n.textContent=this.artifact.metadata.caption,i.appendChild(n)}this.container.appendChild(i)}renderAudio(){if(!this.artifact.data)return;const t=this.j.c.div(this.getFullElName("content")),e=this.j.c.element("audio",{class:this.getFullElName("audio"),controls:"controls"});if(this.artifact.data.kind==="url")e.src=this.artifact.data.url;else if(this.artifact.data.kind==="base64"){const i=this.artifact.mimeType||"audio/mpeg";e.src=`data:${i};base64,${this.artifact.data.base64}`}t.appendChild(e),this.container.appendChild(t)}renderFile(){var t,e,i;if(!this.artifact.data)return;const a=this.j.c.div(this.getFullElName("content")),n=this.j.c.div(this.getFullElName("file-card")),s=this.j.c.div(this.getFullElName("file-icon"));s.textContent="\u{1F4C4}";const r=this.j.c.div(this.getFullElName("file-info")),h=this.j.c.div(this.getFullElName("file-name"));h.textContent=((t=this.artifact.metadata)===null||t===void 0?void 0:t.filename)||"download.file";const f=this.j.c.div(this.getFullElName("file-size"));!((e=this.artifact.metadata)===null||e===void 0)&&e.size&&(f.textContent=this.formatFileSize(this.artifact.metadata.size)),r.appendChild(h),r.appendChild(f);const c=this.j.c.element("a",{class:this.getFullElName("download-btn"),download:((i=this.artifact.metadata)===null||i===void 0?void 0:i.filename)||"download.file"});if(c.textContent=this.j.i18n("Download"),this.artifact.data.kind==="url")c.href=this.artifact.data.url;else if(this.artifact.data.kind==="base64"){const m=this.artifact.mimeType||"application/octet-stream";c.href=`data:${m};base64,${this.artifact.data.base64}`}n.appendChild(s),n.appendChild(r),n.appendChild(c),a.appendChild(n),this.container.appendChild(a)}renderJSON(){if(!this.artifact.data)return;const t=this.j.c.div(this.getFullElName("content")),e=this.j.c.element("pre",{class:this.getFullElName("json")}),i=this.j.c.element("code");let a;if(this.artifact.data.kind==="url")i.textContent=this.j.i18n("JSON data at URL");else if(this.artifact.data.kind==="base64")try{const n=atob(this.artifact.data.base64);a=JSON.parse(n),i.textContent=JSON.stringify(a,null,2)}catch{i.textContent=this.j.i18n("Invalid JSON data")}e.appendChild(i),t.appendChild(e),this.container.appendChild(t)}renderOther(){const t=this.j.c.div(this.getFullElName("content")),e=this.j.c.div(this.getFullElName("placeholder")),i=this.j.c.div(this.getFullElName("placeholder-icon"));i.textContent="\u{1F4E6}";const a=this.j.c.div(this.getFullElName("placeholder-text"));a.textContent=this.j.i18n("Unknown artifact type"),e.appendChild(i),e.appendChild(a),t.appendChild(e),this.container.appendChild(t)}formatFileSize(t){return t<1024?`${t} B`:t<1024*1024?`${(t/1024).toFixed(2)} KB`:t<1024*1024*1024?`${(t/(1024*1024)).toFixed(2)} MB`:`${(t/(1024*1024*1024)).toFixed(2)} GB`}updateArtifact(t){this.artifact=t,this.renderArtifact()}},p(d,"UIArtifact"),d);u([j("ready")],o.prototype,"onReady",null),o=u([C],o);export{o as UIArtifact};