|
|
@@ -4,7 +4,7 @@ |
|
|
|
|
|
|
|
<section class="form inputs"> |
|
|
|
<h3>Avatar</h3> |
|
|
|
<canvas class="displayer" width="200" height="200" ref="canvas"></canvas> |
|
|
|
<canvas class="displayer" width="200" height="200" ref="avatar"></canvas> |
|
|
|
<input type="file" |
|
|
|
@change="e => uploadAvatar(e.target.files[0])" |
|
|
|
/> |
|
|
@@ -14,9 +14,12 @@ |
|
|
|
|
|
|
|
<section class="form inputs"> |
|
|
|
<h3>Letterhead</h3> |
|
|
|
<span>Header as appears on PDFs.</span> |
|
|
|
<img src="/assets/image/mintberry.jpg" alt="avatar"> |
|
|
|
<canvas class="displayer" width="200" height="200" ref="letterhead"></canvas> |
|
|
|
<input type="file" |
|
|
|
@change="e => uploadLetterhead(e.target.files[0])" |
|
|
|
/> |
|
|
|
<button>Upload</button> |
|
|
|
<label class="error">{{avatarError}}</label> |
|
|
|
</section> |
|
|
|
|
|
|
|
<section class="form inputs special"> |
|
|
@@ -60,13 +63,14 @@ |
|
|
|
import { ref, watch, onMounted } from "vue" |
|
|
|
import Dialog from "./dialog.vue" |
|
|
|
|
|
|
|
let avatar = ref(null) |
|
|
|
let avatar = ref(null) // the canvas element |
|
|
|
let letterhead = ref(null) // the canvas element |
|
|
|
let ready = ref(false) |
|
|
|
let avatarChanged = ref(false) |
|
|
|
let avatarError = ref('') |
|
|
|
const canvas = ref(null) |
|
|
|
let letterheadError = ref('') |
|
|
|
const props = defineProps(['user', 'token']) |
|
|
|
const emit = defineEmits(['updateAvatar']) |
|
|
|
const emit = defineEmits(['updateAvatar', 'updateLetterhead']) |
|
|
|
|
|
|
|
function save() { |
|
|
|
} |
|
|
@@ -77,7 +81,7 @@ function check() { |
|
|
|
|
|
|
|
function uploadAvatar(blob) { |
|
|
|
changeAvatar(blob)?.then(() => { |
|
|
|
canvas.value.toBlob(b => { |
|
|
|
avatar.value.toBlob(b => { |
|
|
|
fetch(`/api/user/avatar`, |
|
|
|
{method: 'POST', |
|
|
|
body: b, |
|
|
@@ -92,13 +96,34 @@ function uploadAvatar(blob) { |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
canvas.value.toBlob(b => { |
|
|
|
avatar.value.toBlob(b => { |
|
|
|
// uploadAvatar(b) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
function uploadLetterhead(blob) { |
|
|
|
changeLetterhead(blob)?.then(() => { |
|
|
|
letterhead.value.toBlob(b => { |
|
|
|
fetch(`/api/user/letterhead`, |
|
|
|
{method: 'POST', |
|
|
|
body: b, |
|
|
|
headers: { |
|
|
|
"Accept": "application/json", |
|
|
|
"Authorization": `Bearer ${props.token}`, |
|
|
|
}, |
|
|
|
}).then(resp => { |
|
|
|
if (resp.ok) {emit('updateLetterhead')} |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
letterhead.value.toBlob(b => { |
|
|
|
// uploadAvatar(b) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function changeAvatar(blob) { |
|
|
|
const validTypes = ['image/jpeg', 'image/png'] |
|
|
|
|
|
|
@@ -109,18 +134,38 @@ function changeAvatar(blob) { |
|
|
|
|
|
|
|
avatarError.value = '' |
|
|
|
|
|
|
|
avatar.value = blob |
|
|
|
return createImageBitmap(blob, |
|
|
|
{resizeWidth: 200, resizeHeight: 200, resizeQuality: 'medium'}). |
|
|
|
then((img) => { |
|
|
|
avatar.value = img |
|
|
|
canvas.value.getContext("2d").drawImage(img, 0, 0, 200, 200) |
|
|
|
avatar.value.getContext("2d").drawImage(img, 0, 0, 200, 200) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
function changeLetterhead(blob) { |
|
|
|
const validTypes = ['image/jpeg', 'image/png'] |
|
|
|
|
|
|
|
if (!validTypes.includes(blob.type)) { |
|
|
|
letterheadError.value = 'Image must be JPEG of PNG format' |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
letterheadError.value = '' |
|
|
|
|
|
|
|
return createImageBitmap(blob, |
|
|
|
{resizeWidth: 200, resizeHeight: 200, resizeQuality: 'medium'}). |
|
|
|
then((img) => { |
|
|
|
letterhead.value.getContext("2d").drawImage(img, 0, 0, 200, 200) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
watch(props.user, () => { |
|
|
|
if (!props.user.avatar) return |
|
|
|
changeAvatar(props.user.avatar) |
|
|
|
if (props.user.avatar) { |
|
|
|
changeAvatar(props.user.avatar) |
|
|
|
} |
|
|
|
|
|
|
|
if (props.user.letterhead) { |
|
|
|
changeLetterhead(props.user.letterhead) |
|
|
|
} |
|
|
|
}, {immediate: true}) |
|
|
|
|
|
|
|
</script> |