Skip to content

Commit

Permalink
⚡️ Transfer arraybuffer directly in pdf reader postMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Jun 25, 2024
1 parent ec1f076 commit 84ea731
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/pages/reader/pdf.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default {
return {
isLoading: false,
isIframeReady: false,
base64FileData: null,
fileData: null,
};
},
computed: {
Expand Down Expand Up @@ -60,38 +60,41 @@ export default {
if (event.origin !== this.iframeOrigin) return;
if (event.data === 'ready') {
this.isIframeReady = true;
if (this.base64FileData) {
if (this.fileData) {
const buffer = this.fileData;
this.$refs.pdfIframe?.contentWindow?.postMessage(
JSON.stringify({
action: 'openBase64File',
{
action: 'openArrayBufferFile',
data: {
data: this.base64FileData,
data: buffer,
name: this.nftName,
},
}),
this.iframeOrigin
},
this.iframeOrigin,
[buffer]
);
this.base64FileData = null;
this.fileData = null;
}
}
},
async initRendition() {
try {
this.isLoading = true;
const buffer = await this.getFileBuffer('reader-pdf');
this.base64FileData = Buffer.from(buffer).toString('base64');
if (this.isIframeReady) {
this.$refs.pdfIframe?.contentWindow?.postMessage(
JSON.stringify({
action: 'openBase64File',
{
action: 'openArrayBufferFile',
data: {
data: this.base64FileData,
data: buffer,
name: this.nftName,
},
}),
this.iframeOrigin
},
this.iframeOrigin,
[buffer]
);
this.base64FileData = null;
} else {
this.fileData = buffer;
}
this.isLoading = false;
} catch (err) {
Expand Down

0 comments on commit 84ea731

Please sign in to comment.