-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate Word Cloud visualization to React (#3930)
- Loading branch information
1 parent
300f3f6
commit cc48de0
Showing
13 changed files
with
509 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* global ResizeObserver */ | ||
|
||
function observeNative(node, callback) { | ||
if ((typeof ResizeObserver === 'function') && node) { | ||
const observer = new ResizeObserver(() => callback()); // eslint-disable-line compat/compat | ||
observer.observe(node); | ||
return () => observer.disconnect(); | ||
} | ||
return null; | ||
} | ||
|
||
const items = new Map(); | ||
|
||
function checkItems() { | ||
if (items.size > 0) { | ||
items.forEach((item, node) => { | ||
const bounds = node.getBoundingClientRect(); | ||
// convert to int (because these numbers needed for comparisons), but preserve 1 decimal point | ||
const width = Math.round(bounds.width * 10); | ||
const height = Math.round(bounds.height * 10); | ||
|
||
if ( | ||
(item.width !== width) || | ||
(item.height !== height) | ||
) { | ||
item.width = width; | ||
item.height = height; | ||
item.callback(node); | ||
} | ||
}); | ||
|
||
setTimeout(checkItems, 100); | ||
} | ||
} | ||
|
||
function observeFallback(node, callback) { | ||
if (node && !items.has(node)) { | ||
const shouldTrigger = items.size === 0; | ||
items.set(node, { callback }); | ||
if (shouldTrigger) { | ||
checkItems(); | ||
} | ||
return () => items.delete(node); | ||
} | ||
return null; | ||
} | ||
|
||
export default function observe(node, callback) { | ||
return observeNative(node, callback) || observeFallback(node, callback) || (() => {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.