Sha256: 204489e8fb3e485d9c5bbd009e5eb0c6f76ddcf895a8cd1afaf135354a58deb8
Contents?: true
Size: 1 KB
Versions: 31
Compression:
Stored size: 1 KB
Contents
import { Controller } from "@hotwired/stimulus" const APPLICATION_NAME = "my_application" export default class extends Controller { static values = { title: String, text: String, url: String, file: String } connect() { this.element.hidden = !navigator.canShare } async share() { try { await navigator.share(await this.#getShareData()) } catch(error) { console.warn(error.message); } } async #getShareData() { const data = { title: this.titleValue, text: this.textValue } if (this.urlValue) { data.url = this.urlValue } if (this.fileValue) { data.files = [ await this.#getFileObject()] } return data; } async #getFileObject() { const response = await fetch(this.fileValue) const blob = await response.blob() const randomPrefix = `${APPLICATION_NAME}_${Math.random().toString(36).slice(2)}` const fileName = `${randomPrefix}.${blob.type.split('/').pop()}` return new File([ blob ], fileName, { type: blob.type }) } }
Version data entries
31 entries across 31 versions & 1 rubygems