Skip to content

Commit

Permalink
feat(cloud-editor): use resize observer to wait for non-zero containe…
Browse files Browse the repository at this point in the history
…r size
  • Loading branch information
nd0ut committed Mar 10, 2023
1 parent 6f59e6a commit 5686965
Showing 1 changed file with 6 additions and 23 deletions.
29 changes: 6 additions & 23 deletions blocks/CloudImageEditor/src/CloudEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,14 @@ export class CloudEditor extends Block {
* @returns {Promise<void>}
*/
_waitForSize() {
return new Promise((resolve, reject) => {
let timeout = 1000;
let start = Date.now();

let callback = () => {
// there could be problem when element disconnected and connected again between ticks
if (!this.isConnected) {
clearInterval(interval);
reject();
return;
}
if (Date.now() - start > timeout) {
clearInterval(interval);
reject(new Error('[cloud-image-editor] timout waiting for non-zero container size'));
return;
}
let { width, height } = this.getBoundingClientRect();

if (width > 0 && height > 0) {
clearInterval(interval);
return new Promise((resolve) => {
const resizeObserver = new ResizeObserver(([element]) => {
if (element.contentRect.width > 0 && element.contentRect.height > 0) {
resolve();
resizeObserver.disconnect();
}
};
let interval = setInterval(callback, 50);
callback();
});
resizeObserver.observe(this);
});
}

Expand Down

0 comments on commit 5686965

Please sign in to comment.