2019-08-24 19:28:12 -07:00
|
|
|
<div class="media-alt-editor">
|
|
|
|
<textarea
|
|
|
|
id="the-media-alt-input-{realm}-{index}"
|
|
|
|
class="media-alt-input"
|
2019-08-25 18:33:44 -07:00
|
|
|
placeholder="Describe for the visually impaired"
|
2019-08-24 19:28:12 -07:00
|
|
|
ref:textarea
|
|
|
|
bind:value=rawText
|
|
|
|
></textarea>
|
|
|
|
<label for="the-media-alt-input-{realm}-{index}" class="sr-only">
|
|
|
|
Describe for the visually impaired
|
|
|
|
</label>
|
2019-08-24 21:23:43 -07:00
|
|
|
<LengthGauge
|
|
|
|
{length}
|
|
|
|
{overLimit}
|
|
|
|
max={mediaAltCharLimit}
|
|
|
|
/>
|
|
|
|
<LengthIndicator
|
|
|
|
{length}
|
|
|
|
{overLimit}
|
|
|
|
max={mediaAltCharLimit}
|
|
|
|
style="width: 100%; text-align: right;"
|
|
|
|
/>
|
2019-08-25 18:33:44 -07:00
|
|
|
<button class="extract-text-button" type="button"
|
|
|
|
on:click="onClick()"
|
|
|
|
disabled={extracting}
|
|
|
|
>
|
|
|
|
<SvgIcon href="{extracting ? '#fa-spinner' : '#fa-magic'}"
|
|
|
|
className="extract-text-svg {extracting ? 'spin' : ''}"
|
|
|
|
/>
|
|
|
|
<span>
|
|
|
|
{#if extracting}
|
|
|
|
Extracting text…
|
|
|
|
{:else}
|
|
|
|
Extract text from image
|
|
|
|
{/if}
|
|
|
|
</span>
|
|
|
|
</button>
|
2019-08-24 19:28:12 -07:00
|
|
|
</div>
|
|
|
|
<style>
|
|
|
|
.media-alt-editor {
|
|
|
|
display: flex;
|
2019-08-24 21:23:43 -07:00
|
|
|
flex-direction: column;
|
2019-08-24 19:28:12 -07:00
|
|
|
}
|
|
|
|
.media-alt-input {
|
|
|
|
padding: 5px;
|
|
|
|
border: 1px solid var(--input-border);
|
|
|
|
min-height: 75px;
|
|
|
|
resize: none;
|
|
|
|
overflow: hidden;
|
|
|
|
word-wrap: break-word;
|
|
|
|
/* Text must be at least 16px or else iOS Safari zooms in */
|
|
|
|
font-size: 1.2em;
|
|
|
|
max-height: 70vh;
|
|
|
|
}
|
|
|
|
|
2019-08-25 18:33:44 -07:00
|
|
|
.extract-text-button {
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
margin-top: 10px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.extract-text-button span {
|
|
|
|
margin-left: 15px;
|
|
|
|
}
|
|
|
|
|
|
|
|
:global(.extract-text-svg) {
|
|
|
|
fill: var(--button-text);
|
|
|
|
width: 18px;
|
|
|
|
height: 18px;
|
|
|
|
}
|
|
|
|
|
2019-08-24 19:28:12 -07:00
|
|
|
@media (max-height: 767px) {
|
|
|
|
.media-alt-input {
|
|
|
|
max-height: 40vh;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 18:33:44 -07:00
|
|
|
|
|
|
|
@media (min-height: 768px) {
|
|
|
|
.media-alt-input {
|
|
|
|
min-width: 250px;
|
|
|
|
}
|
|
|
|
}
|
2019-08-24 19:28:12 -07:00
|
|
|
</style>
|
|
|
|
<script>
|
|
|
|
import { requestPostAnimationFrame } from '../../../_utils/requestPostAnimationFrame'
|
|
|
|
import { mark, stop } from '../../../_utils/marks'
|
|
|
|
import { autosize } from '../../../_thirdparty/autosize/autosize'
|
|
|
|
import { observe } from 'svelte-extras'
|
|
|
|
import { throttleTimer } from '../../../_utils/throttleTimer'
|
|
|
|
import { get } from '../../../_utils/lodash-lite'
|
|
|
|
import { store } from '../../../_store/store'
|
2019-08-24 21:23:43 -07:00
|
|
|
import { MEDIA_ALT_CHAR_LIMIT } from '../../../_static/media'
|
|
|
|
import LengthGauge from '../../LengthGauge.html'
|
|
|
|
import LengthIndicator from '../../LengthIndicator.html'
|
|
|
|
import { length } from 'stringz'
|
2019-08-25 18:33:44 -07:00
|
|
|
import { runTesseract } from '../../../_utils/runTesseract'
|
|
|
|
import SvgIcon from '../../SvgIcon.html'
|
|
|
|
import { toast } from '../../toast/toast'
|
2019-08-25 21:48:59 -07:00
|
|
|
import { mediaUploadFileCache } from '../../../_utils/mediaUploadFileCache'
|
2019-08-24 19:28:12 -07:00
|
|
|
|
|
|
|
const updateRawTextInStore = throttleTimer(requestPostAnimationFrame)
|
|
|
|
|
|
|
|
export default {
|
|
|
|
oncreate () {
|
|
|
|
this.setupAutosize()
|
|
|
|
this.setupSyncFromStore()
|
|
|
|
this.setupSyncToStore()
|
|
|
|
},
|
|
|
|
ondestroy () {
|
|
|
|
this.teardownAutosize()
|
|
|
|
},
|
|
|
|
store: () => store,
|
|
|
|
data: () => ({
|
2019-08-24 21:23:43 -07:00
|
|
|
rawText: '',
|
2019-08-25 18:33:44 -07:00
|
|
|
mediaAltCharLimit: MEDIA_ALT_CHAR_LIMIT,
|
|
|
|
extracting: false
|
2019-08-24 19:28:12 -07:00
|
|
|
}),
|
2019-08-24 21:23:43 -07:00
|
|
|
computed: {
|
|
|
|
length: ({ rawText }) => length(rawText || ''),
|
2019-08-25 18:33:44 -07:00
|
|
|
overLimit: ({ mediaAltCharLimit, length }) => length > mediaAltCharLimit,
|
2019-08-25 21:48:59 -07:00
|
|
|
url: ({ media, index }) => get(media, [index, 'data', 'url']),
|
|
|
|
mediaId: ({ media, index }) => get(media, [index, 'data', 'id'])
|
2019-08-24 21:23:43 -07:00
|
|
|
},
|
2019-08-24 19:28:12 -07:00
|
|
|
methods: {
|
|
|
|
observe,
|
|
|
|
setupSyncFromStore () {
|
|
|
|
this.observe('media', media => {
|
|
|
|
media = media || []
|
|
|
|
const { index, rawText } = this.get()
|
|
|
|
const text = get(media, [index, 'description'], '')
|
|
|
|
if (rawText !== text) {
|
|
|
|
this.set({ rawText: text })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
setupSyncToStore () {
|
|
|
|
this.observe('rawText', rawText => {
|
|
|
|
updateRawTextInStore(() => {
|
|
|
|
const { realm, index, media } = this.get()
|
|
|
|
if (media[index].description !== rawText) {
|
|
|
|
media[index].description = rawText
|
|
|
|
this.store.setComposeData(realm, { media })
|
|
|
|
this.store.save()
|
|
|
|
}
|
|
|
|
this.fire('resize')
|
|
|
|
})
|
|
|
|
}, { init: false })
|
|
|
|
},
|
|
|
|
setupAutosize () {
|
|
|
|
const textarea = this.refs.textarea
|
|
|
|
requestPostAnimationFrame(() => {
|
|
|
|
mark('autosize()')
|
|
|
|
autosize(textarea)
|
|
|
|
stop('autosize()')
|
|
|
|
})
|
|
|
|
},
|
|
|
|
teardownAutosize () {
|
|
|
|
mark('autosize.destroy()')
|
|
|
|
autosize.destroy(this.refs.textarea)
|
|
|
|
stop('autosize.destroy()')
|
|
|
|
},
|
|
|
|
measure () {
|
|
|
|
autosize.update(this.refs.textarea)
|
2019-08-25 18:33:44 -07:00
|
|
|
},
|
|
|
|
async onClick () {
|
|
|
|
this.set({ extracting: true })
|
|
|
|
try {
|
2019-08-25 21:48:59 -07:00
|
|
|
const { url, mediaId } = this.get()
|
|
|
|
const file = mediaUploadFileCache.get(mediaId)
|
|
|
|
let text
|
|
|
|
if (file) { // Avoid downloading from the network a file that the user *just* uploaded
|
|
|
|
const fileUrl = URL.createObjectURL(file)
|
|
|
|
try {
|
|
|
|
text = await runTesseract(fileUrl)
|
|
|
|
} finally {
|
|
|
|
URL.revokeObjectURL(fileUrl)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
text = await runTesseract(url)
|
|
|
|
}
|
2019-08-25 18:33:44 -07:00
|
|
|
const { media, index, realm } = this.get()
|
|
|
|
if (media[index].description !== text) {
|
|
|
|
media[index].description = text
|
|
|
|
this.store.setComposeData(realm, { media })
|
|
|
|
this.store.save()
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
/* no await */ toast.say(
|
|
|
|
'Unable to extract text. Ensure your instance supports cross-origin resource sharing (CORS) for images.'
|
|
|
|
)
|
|
|
|
} finally {
|
|
|
|
this.set({ extracting: false })
|
|
|
|
}
|
2019-08-24 19:28:12 -07:00
|
|
|
}
|
2019-08-24 21:23:43 -07:00
|
|
|
},
|
|
|
|
components: {
|
|
|
|
LengthGauge,
|
2019-08-25 18:33:44 -07:00
|
|
|
LengthIndicator,
|
|
|
|
SvgIcon
|
2019-08-24 19:28:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|