mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-07-25 16:37:22 +02:00
Convert PDFPageProxy.prototype.getTextContent to an asynchronous method
This is a tiny bit shorter, which cannot hurt.
This commit is contained in:
parent
7077b2a998
commit
c1b824f2e5
@ -1739,7 +1739,7 @@ class PDFPageProxy {
|
|||||||
* @returns {Promise<TextContent>} A promise that is resolved with a
|
* @returns {Promise<TextContent>} A promise that is resolved with a
|
||||||
* {@link TextContent} object that represents the page's text content.
|
* {@link TextContent} object that represents the page's text content.
|
||||||
*/
|
*/
|
||||||
getTextContent(params = {}) {
|
async getTextContent(params = {}) {
|
||||||
if (this._transport._htmlForXfa) {
|
if (this._transport._htmlForXfa) {
|
||||||
// TODO: We need to revisit this once the XFA foreground patch lands and
|
// TODO: We need to revisit this once the XFA foreground patch lands and
|
||||||
// only do this for non-foreground XFA.
|
// only do this for non-foreground XFA.
|
||||||
@ -1747,28 +1747,18 @@ class PDFPageProxy {
|
|||||||
}
|
}
|
||||||
const readableStream = this.streamTextContent(params);
|
const readableStream = this.streamTextContent(params);
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
const textContent = {
|
||||||
function pump() {
|
items: [],
|
||||||
reader.read().then(function ({ value, done }) {
|
styles: Object.create(null),
|
||||||
if (done) {
|
lang: null,
|
||||||
resolve(textContent);
|
};
|
||||||
return;
|
|
||||||
}
|
|
||||||
textContent.lang ??= value.lang;
|
|
||||||
Object.assign(textContent.styles, value.styles);
|
|
||||||
textContent.items.push(...value.items);
|
|
||||||
pump();
|
|
||||||
}, reject);
|
|
||||||
}
|
|
||||||
|
|
||||||
const reader = readableStream.getReader();
|
for await (const value of readableStream) {
|
||||||
const textContent = {
|
textContent.lang ??= value.lang;
|
||||||
items: [],
|
Object.assign(textContent.styles, value.styles);
|
||||||
styles: Object.create(null),
|
textContent.items.push(...value.items);
|
||||||
lang: null,
|
}
|
||||||
};
|
return textContent;
|
||||||
pump();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user