{"version":3,"file":"media_controller-IjS1zbMP.js","sources":["../../../app/javascript/controllers/editor/media_controller.js"],"sourcesContent":["import ApplicationController from \"../application_controller\"\n\nexport default class extends ApplicationController {\n static values = {\n inputName: String,\n multiple: { type: Boolean, default: false },\n media: Object\n }\n\n static targets = [\"fileInput\", \"template\", \"button\", \"attachment\"]\n\n static classes = ['width']\n\n initialize() {\n this.files = []\n super.initialize()\n }\n\n onFileInputChange() {\n this.files = Array.from(this.fileInputTarget.files)\n\n const fileMaxSizeTooMuch = this.files.find(file => {\n const type = file.type.split(\"/\")[0]\n\n if(['image', 'video', 'audio'].includes(type)) {\n return this.mediaValue[type].max_size < file.size\n } else {\n return this.mediaValue.document.max_size < file.size\n }\n })\n\n\n if(fileMaxSizeTooMuch) {\n const type = fileMaxSizeTooMuch.type.split(\"/\")[0]\n const mediaType = ['image', 'audio', 'video'].includes(type) ? type : 'document'\n\n this.showToast({\n message: this.translations.compose.tools.attachment.size_out_of_bounds.replace('%{limit}', this.byteToMegabyte(this.mediaValue[mediaType].max_size)),\n type: \"error\"\n })\n } else {\n this.files.forEach(file => this.createAttachmentElement(file))\n }\n\n this.fileInputTarget.value = null\n this.files = []\n }\n\n openFileDialog() {\n this.fileInputTarget.click()\n }\n\n updateMediaRestrictions({ detail }) {\n this.fileInputTarget.accept = detail.accept\n this.mediaValue = detail.media\n }\n\n setAttachmentFromTemplate({ detail: attachment }) {\n if(!attachment) return\n this.createAttachmentElementFromTemplate(attachment)\n }\n\n // private\n\n createAttachmentElementFromTemplate(data) {\n const element = this.attachmentElement()\n\n if(data.content_type.startsWith('image/')) {\n const thumbnail = document.createElement('img')\n thumbnail.src = data.url\n this.addClass(thumbnail, ...this.widthClasses, 'h-28', 'rounded-md', 'object-cover')\n\n element.querySelector('main').innerHTML = ''\n element.querySelector('main').appendChild(thumbnail)\n } else {\n element.querySelector('main').classList.add(...this.widthClasses, 'h-20', 'rounded-md', 'bg-gray-200', 'p-1')\n element.querySelector('p[data-attachment-name]').innerText = data.filename\n }\n\n const input = document.createElement('input')\n input.type = 'hidden'\n input.name = `${this.inputNameValue}[signed_id]`\n input.value = data.signed_id\n\n element.setAttribute(\"data-editor--attached-media-upload-value\", false)\n\n element.appendChild(input)\n this.appendElement(element)\n }\n\n byteToMegabyte(bytes) {\n return Math.ceil(bytes / 1024 / 1024)\n }\n\n createAttachmentElement(file) {\n const element = this.attachmentElement()\n\n if(file.type.startsWith(\"image/\")) {\n const thumbnail = document.createElement(\"img\")\n thumbnail.src = URL.createObjectURL(file)\n this.addClass(thumbnail, ...this.widthClasses, \"h-28\", \"rounded-md\", \"object-cover\")\n\n element.querySelector(\"main\").innerHTML = \"\"\n element.querySelector(\"main\").appendChild(thumbnail)\n } else {\n element.querySelector(\"main\").classList.add(...this.widthClasses, \"h-20\", \"rounded-md\", \"bg-gray-200\", \"p-1\")\n element.querySelector(\"p[data-attachment-name]\").innerText = file.name\n }\n\n const dataTransfer = new DataTransfer()\n\n dataTransfer.items.add(file)\n element.querySelector(\"input\").files = dataTransfer.files\n\n element.setAttribute(\"data-compose--toolbar-target\", \"attachment\")\n element.setAttribute(\"data-editor--attached-media-upload-value\", true)\n\n element.setAttribute('data-file', file)\n\n this.appendElement(element)\n }\n\n appendElement(element) {\n if(this.inputNameValue) {\n element.setAttribute(\"data-editor--attached-media-input-name-value\", this.inputNameValue)\n }\n\n this.show(element)\n\n if(document.querySelector(\"media-container ul\")) {\n if(!this.multipleValue) {\n document.querySelector(\"media-container ul\").innerHTML = \"\"\n }\n\n document.querySelector(\"media-container ul\").appendChild(element)\n }\n\n this.dispatch('attachment:added', {\n detail: element\n })\n }\n\n attachmentElement() {\n const element = this.templateTarget.cloneNode(true)\n element.removeAttribute(\"hidden\")\n element.setAttribute(\"data-controller\", \"editor--attached-media\")\n element.setAttribute(\"data-editor--media-target\", \"attachment\")\n\n return element\n }\n\n enable() {\n super.enable(this.buttonTarget)\n\n if(document.querySelector('media-container')) {\n this.dispatch('enabled', {\n target: document.querySelector('media-container')\n })\n }\n }\n\n disable() {\n super.disable(this.buttonTarget)\n\n if(document.querySelector('media-container')) {\n this.dispatch('disabled', {\n target: document.querySelector('media-container')\n })\n }\n }\n}\n"],"names":["media_controller","ApplicationController","fileMaxSizeTooMuch","file","type","mediaType","detail","attachment","data","element","thumbnail","input","bytes","dataTransfer","__publicField"],"mappings":"uNAEe,MAAKA,UAASC,CAAsB,CAWjD,YAAa,CACX,KAAK,MAAQ,CAAA,EACb,MAAM,WAAU,CACpB,CAEE,mBAAoB,CAClB,KAAK,MAAQ,MAAM,KAAK,KAAK,gBAAgB,KAAK,EAElD,MAAMC,EAAqB,KAAK,MAAM,KAAKC,GAAQ,CACjD,MAAMC,EAAOD,EAAK,KAAK,MAAM,GAAG,EAAE,CAAC,EAEnC,MAAG,CAAC,QAAS,QAAS,OAAO,EAAE,SAASC,CAAI,EACnC,KAAK,WAAWA,CAAI,EAAE,SAAWD,EAAK,KAEtC,KAAK,WAAW,SAAS,SAAWA,EAAK,IAEnD,CAAA,EAGD,GAAGD,EAAoB,CACrB,MAAME,EAAOF,EAAmB,KAAK,MAAM,GAAG,EAAE,CAAC,EAC3CG,EAAY,CAAC,QAAS,QAAS,OAAO,EAAE,SAASD,CAAI,EAAIA,EAAO,WAEtE,KAAK,UAAU,CACb,QAAS,KAAK,aAAa,QAAQ,MAAM,WAAW,mBAAmB,QAAQ,WAAY,KAAK,eAAe,KAAK,WAAWC,CAAS,EAAE,QAAQ,CAAC,EACnJ,KAAM,OACP,CAAA,CACP,MACM,KAAK,MAAM,QAAQF,GAAQ,KAAK,wBAAwBA,CAAI,CAAC,EAG/D,KAAK,gBAAgB,MAAQ,KAC7B,KAAK,MAAQ,CAAA,CACjB,CAEE,gBAAiB,CACf,KAAK,gBAAgB,MAAK,CAC9B,CAEE,wBAAwB,CAAE,OAAAG,GAAU,CAClC,KAAK,gBAAgB,OAASA,EAAO,OACrC,KAAK,WAAaA,EAAO,KAC7B,CAEE,0BAA0B,CAAE,OAAQC,GAAc,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