refactor: page.post.vue to composition api #55

Closed
norm wants to merge 1 commit from (deleted):refactor/page.post.vue into main

View file

@ -8,84 +8,69 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent, PropType } from 'vue'; import { defineProps, watch } from 'vue';
import MkTextarea from '../form/textarea.vue'; import MkTextarea from '../form/textarea.vue';
import MkButton from '../ui/button.vue'; import MkButton from '../ui/button.vue';
import { apiUrl } from '@/config'; import { apiUrl } from '@/config';
import * as os from '@/os'; import * as os from '@/os';
import { PostBlock } from '@/scripts/hpml/block'; import { PostBlock } from '@/scripts/hpml/block';
import { Hpml } from '@/scripts/hpml/evaluator'; import { Hpml } from '@/scripts/hpml/evaluator';
import { $i } from '@/account';
import { defaultStore } from '@/store';
export default defineComponent({ const props = defineProps<{
components: { block: PostBlock;
MkTextarea, hpml: Hpml;
MkButton, }>();
},
props: {
block: {
type: Object as PropType<PostBlock>,
required: true
},
hpml: {
type: Object as PropType<Hpml>,
required: true
}
},
data() {
return {
text: this.hpml.interpolate(this.block.text),
posted: false,
posting: false,
};
},
watch: {
'hpml.vars': {
handler() {
this.text = this.hpml.interpolate(this.block.text);
},
deep: true
}
},
methods: {
upload() {
const promise = new Promise((ok) => {
const canvas = this.hpml.canvases[this.block.canvasId];
canvas.toBlob(blob => {
const formData = new FormData();
formData.append('file', blob);
if (this.$store.state.uploadFolder) {
formData.append('folderId', this.$store.state.uploadFolder);
}
fetch(apiUrl + '/drive/files/create', { let text = $ref(props.hpml.interpolate(props.block.text));
method: 'POST', let posted = $ref(false);
body: formData, let posting = $ref(false);
headers: {
authorization: `Bearer ${this.$i.token}`, watch(props.hpml.vars, () => {
}, text = props.hpml.interpolate(props.block.text);
}) }, {
.then(response => response.json()) deep: true,
.then(f => {
ok(f);
});
});
});
os.promiseDialog(promise);
return promise;
},
async post() {
this.posting = true;
const file = this.block.attachCanvasImage ? await this.upload() : null;
os.apiWithDialog('notes/create', {
text: this.text === '' ? null : this.text,
fileIds: file ? [file.id] : undefined,
}).then(() => {
this.posted = true;
});
}
}
}); });
function upload(): Promise<any> {
const promise = new Promise((ok) => {
const canvas = props.hpml.canvases[props.block.canvasId];
canvas.toBlob(blob => {
const formData = new FormData();
formData.append('file', blob);
if (defaultStore.state.uploadFolder) {
formData.append('folderId', defaultStore.state.uploadFolder);
}
fetch(apiUrl + '/drive/files/create', {
method: 'POST',
body: formData,
headers: {
authorization: `Bearer ${$i.token}`,
},
})
.then(response => response.json())
.then(f => {
ok(f);
});
});
});
os.promiseDialog(promise);
return promise;
}
async function post(): Promise<void> {
posting = true;
const file = props.block.attachCanvasImage ? await upload() : null;
os.apiWithDialog('notes/create', {
text: text === '' ? null : text,
fileIds: file ? [file.id] : undefined,
}).then(() => {
posted = true;
});
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>