Skip to content

Commit 8dcfe21

Browse files
authored
feat: download bed mesh image (#1656)
Signed-off-by: Pedro Lamas <[email protected]>
1 parent 2f9eaf0 commit 8dcfe21

22 files changed

+48
-68
lines changed

src/components/settings/GeneralSettings.vue

+5-13
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,9 @@ import { readFileAsTextAsync } from '@/util/file-system-entry'
285285
import { EventBus } from '@/eventBus'
286286
import { isFluiddContent, toFluiddContent } from '@/util/fluidd-content'
287287
import { getAllLocales } from '@/plugins/i18n'
288+
import downloadUrl from '@/util/download-url'
288289
289-
@Component({
290-
components: {}
291-
})
290+
@Component({})
292291
export default class GeneralSettings extends Mixins(StateMixin) {
293292
@Ref('instanceName')
294293
readonly instanceNameElement!: VInput
@@ -630,17 +629,10 @@ export default class GeneralSettings extends Mixins(StateMixin) {
630629
const backupData = toFluiddContent('settings-backup', data)
631630
const backupDataAsString = JSON.stringify(backupData)
632631
633-
const link = document.createElement('a')
632+
const filename = `backup-fluidd-v${import.meta.env.VERSION}-${this.instanceName}.json`
633+
const url = `data:text/plain;charset=utf-8,${encodeURIComponent(backupDataAsString)}`
634634
635-
link.href = `data:text/plain;charset=utf-8,${encodeURIComponent(backupDataAsString)}`
636-
link.download = `backup-fluidd-v${import.meta.env.VERSION}-${this.instanceName}.json`
637-
link.target = '_blank'
638-
639-
document.body.appendChild(link)
640-
641-
link.click()
642-
643-
document.body.removeChild(link)
635+
downloadUrl(filename, url)
644636
}
645637
} catch (e) {
646638
consola.error('[Settings] backup failed', e)

src/components/widgets/bedmesh/BedMeshCard.vue

+5-16
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,21 @@
1919
{{ $t('app.general.btn.calibrate') }}
2020
</app-btn>
2121

22-
<v-tooltip
23-
v-if="canCopyImage"
24-
bottom
25-
>
22+
<v-tooltip bottom>
2623
<template #activator="{ on, attrs }">
2724
<app-btn
2825
v-bind="attrs"
2926
icon
3027
:disabled="!hasMeshLoaded"
3128
v-on="on"
32-
@click="copyImage()"
29+
@click="downloadImage()"
3330
>
3431
<v-icon dense>
3532
$screenshot
3633
</v-icon>
3734
</app-btn>
3835
</template>
39-
<span>{{ $t('app.bedmesh.tooltip.copy_image') }}</span>
36+
<span>{{ $t('app.bedmesh.tooltip.download_image') }}</span>
4037
</v-tooltip>
4138

4239
<app-btn
@@ -228,16 +225,8 @@ export default class BedMeshCard extends Mixins(StateMixin, ToolheadMixin, Brows
228225
return this.$typedGetters['mesh/getCurrentMeshData']
229226
}
230227
231-
get canCopyImage () {
232-
return (
233-
typeof navigator.clipboard === 'object' &&
234-
typeof navigator.clipboard.write === 'function' &&
235-
typeof ClipboardItem === 'function'
236-
)
237-
}
238-
239-
copyImage () {
240-
this.bedMeshChart.copyImage()
228+
downloadImage () {
229+
this.bedMeshChart.downloadImage()
241230
}
242231
}
243232
</script>

src/components/widgets/bedmesh/BedMeshChart.vue

+11-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type { ECharts, EChartsOption, GraphicComponentOption } from 'echarts'
2525
import { merge, cloneDeepWith } from 'lodash-es'
2626
import BrowserMixin from '@/mixins/browser'
2727
import type { BedSize } from '@/store/printer/types'
28+
import downloadUrl from '@/util/download-url'
2829
2930
@Component({})
3031
export default class BedMeshChart extends Mixins(BrowserMixin) {
@@ -241,16 +242,18 @@ export default class BedMeshChart extends Mixins(BrowserMixin) {
241242
return opts
242243
}
243244
244-
async copyImage () {
245-
const image = await fetch(this.chart.getDataURL({ type: 'png', backgroundColor: '#262629' }))
246-
247-
const blob = await image.blob()
245+
async downloadImage () {
246+
const url = this.chart.getDataURL({
247+
type: 'png',
248+
backgroundColor: '#262629'
249+
})
248250
249-
const data = [
250-
new ClipboardItem({ 'image/png': blob })
251-
]
251+
const filename = [
252+
'bedmesh',
253+
this.$typedState.printer.printer.bed_mesh?.profile_name
254+
].filter(x => x).join('-')
252255
253-
await navigator.clipboard.write(data)
256+
downloadUrl(filename, url)
254257
}
255258
}
256259
</script>

src/locales/de.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ app:
2222
delete: Profil löschen
2323
load: Profil laden
2424
save: Fügt das Mesh Profil der printer.cfg hinzu
25-
copy_image: Bett-Mesh-Abbildung kopieren
2625
chart:
2726
label:
2827
current: Aktuell

src/locales/en.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ app:
2222
delete: Delete Profile
2323
load: Load Profile
2424
save: Commits calibrated profile to printer.cfg
25-
copy_image: Copy bed mesh image
25+
download_image: Download bed mesh image
2626
chart:
2727
label:
2828
current: Actual

src/locales/es.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ app:
2222
delete: Elimina el perfil
2323
load: Cargar el perfil
2424
save: Guarda el perfil de calibracion en el archivo printer.cfg
25-
copy_image: Copiar imagen de la malla de cama
2625
chart:
2726
label:
2827
current:

src/locales/fr.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ app:
2424
delete: Supprime le profil
2525
load: Charger le profil
2626
save: Enregistrer le profil calibré dans 'printer.cfg'
27-
copy_image: Copier l'image du bed mesh
2827
chart:
2928
label:
3029
current: Courant

src/locales/hu.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ app:
2222
delete: Profil törlése
2323
load: Profil betöltése
2424
save: A kalibrált profil a printer.cfg fájlba íródik
25-
copy_image: Ágyhálós kép másolása
2625
chart:
2726
label:
2827
current: Aktuális

src/locales/it.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ app:
2222
delete: Elimina profilo
2323
load: Carica profilo
2424
save: Salva il profilo calibrato in printer.cfg
25-
copy_image: Copia immagine mesh
2625
chart:
2726
label:
2827
current: Effettivo

src/locales/ja.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ app:
2828
not_loaded: メッシュを読込しない
2929
tooltip:
3030
calibrate: 新しいキャリブレーションを開始し、プロファイル'default'として保存します
31-
copy_image: ベッドメッシュ画像をコピー
3231
delete: プロファイルを削除します。
3332
load: プロファイルを読込み
3433
save: キャリブレーションされたプロファイルをprinter.cfgに書込みします。

src/locales/ko.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ app:
2020
delete: 프로파일을 삭제합니다
2121
load: 프로파일 불러오기
2222
save: 캘리브레이션이 된 프로파일을 printer.cfg에 적용합니다
23-
copy_image: 베드 메쉬 이미지 복사
2423
chart:
2524
label:
2625
current: 현재 온도

src/locales/nl.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ app:
2222
delete: Verwijder profiel
2323
load: Laad profiel
2424
save: Slaat afgesteld profiel op naar printer.cfg
25-
copy_image: Kopieer bed mesh afbeelding
2625
chart:
2726
label:
2827
current: Huidig

src/locales/pl.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ app:
2121
delete: Usuń profil
2222
load: Wczytaj profil
2323
save: Zapisuje skalibrowany profil do pliku printer.cfg
24-
copy_image: Skopiuj obraz siatki stołu
2524
chart:
2625
label:
2726
current: Aktualnie

src/locales/pt.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ app:
2222
delete: Eliminar perfil
2323
load: Carregar perfil
2424
save: Guard perfil calibrado no ficheiro printer.cfg
25-
copy_image: Copiar imagem da malha da cama
2625
chart:
2726
label:
2827
current: Corrente

src/locales/pt_BR.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ app:
1818
not_loaded: Nenhuma malha carregada
1919
tooltip:
2020
load: Carregar perfil
21-
copy_image: Copiar imagem da malha da cama
2221
calibrate: Começa uma nova calibração, salvando o perfil como 'default'
2322
delete: Apagar perfil
2423
save: Salva perfil calibrado no arquivo printer.cfg

src/locales/ru.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ app:
2222
delete: Удалить профиль
2323
load: Загрузить профиль
2424
save: Сохранить данные откалиброванного профиля в printer.cfg
25-
copy_image: Скопировать изображение сетки кровати
2625
chart:
2726
label:
2827
current: Текущая

src/locales/sl.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ app:
2222
delete: Zbriši profil
2323
load: Naloži profil
2424
save: Shrani kalibriran profil v printer.cfg.
25-
copy_image: Kopiraj sliko posteljne mreže
2625
chart:
2726
label:
2827
current: Dejansko

src/locales/ta.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,6 @@ app:
598598
என்று சேமிக்கிறது
599599
delete: சுயவிவரத்தை நீக்கு
600600
load: சுயவிவரத்தை ஏற்றவும்
601-
copy_image: படுக்கை கண்ணி படத்தை நகலெடுக்கவும்
602601
label:
603602
active: செயலில்
604603
base: காரம்

src/locales/tr.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ app:
2323
delete: Profili Sil
2424
load: Profil Yükle
2525
save: Kalibre edilmiş profili printer.cfg'ye kaydeder
26-
copy_image: Yatak örgüsü görüntüsünü kopyala
2726
chart:
2827
label:
2928
current: Gerçek

src/locales/uk.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ app:
2222
delete: Видалити профіль
2323
load: Завантажити профіль
2424
save: Зберегти калібрований профіль в printer.cfg
25-
copy_image: Скопіюйте сітчасте зображення ліжка
2625
chart:
2726
label:
2827
current: Поточне значення

src/mixins/files.ts

+4-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { FileWithPath } from '@/types'
77
import { consola } from 'consola'
88
import { v4 as uuidv4 } from 'uuid'
99
import type { AppUser } from '@/store/auth/types'
10+
import downloadUrl from '@/util/download-url'
1011

1112
@Component
1213
export default class FilesMixin extends Vue {
@@ -147,20 +148,9 @@ export default class FilesMixin extends Vue {
147148
*/
148149
async downloadFile (filename: string, path: string) {
149150
// Grab a oneshot.
150-
try {
151-
const url = await this.createFileUrlWithToken(filename, path)
152-
153-
// Create a link, handle its click - and finally remove it again.
154-
const link = document.createElement('a')
155-
link.href = url
156-
link.setAttribute('download', filename)
157-
link.setAttribute('target', '_blank')
158-
document.body.appendChild(link)
159-
link.click()
160-
document.body.removeChild(link)
161-
} catch {
162-
// Likely a 401.
163-
}
151+
const url = await this.createFileUrlWithToken(filename, path)
152+
153+
downloadUrl(filename, url)
164154
}
165155

166156
/**

src/util/download-url.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { consola } from 'consola'
2+
3+
const downloadUrl = (filename: string, url: string) => {
4+
try {
5+
// Create a link, handle its click - and finally remove it again.
6+
const link = document.createElement('a')
7+
8+
link.href = url
9+
link.download = filename
10+
link.target = '_blank'
11+
12+
document.body.appendChild(link)
13+
14+
link.click()
15+
16+
document.body.removeChild(link)
17+
} catch (error) {
18+
consola.error('[DownloadUrl] error', error)
19+
}
20+
}
21+
22+
export default downloadUrl

0 commit comments

Comments
 (0)