Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Element.checkVisibility polyfill for Blockly #10241

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pxtblocks/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { initContextMenu } from "./contextMenu";
import { renderCodeCard } from "./codecardRenderer";
import { FieldDropdown } from "./fields/field_dropdown";
import { setDraggableShadowBlocks, setDuplicateOnDragStrategy } from "./plugins/duplicateOnDrag";
import { applyPolyfills } from "./polyfills";


interface BlockDefinition {
Expand Down Expand Up @@ -587,6 +588,8 @@ function init(blockInfo: pxtc.BlocksInfo) {
if (blocklyInitialized) return;
blocklyInitialized = true;

applyPolyfills();

initFieldEditors();
initContextMenu();
initOnStart();
Expand Down
60 changes: 60 additions & 0 deletions pxtblocks/polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// all options default to true
interface CheckVisibilityOptions {
contentVisibilityAuto?: boolean;
opacityProperty?: boolean;
visibilityProperty?: boolean;
checkOpacity?: boolean;
checkVisibilityCSS?: boolean;
}

export function applyPolyfills() {
if (!(Element.prototype as any).checkVisibility) {
(Element.prototype as any).checkVisibility = function checkVisibility(this: Element, options: CheckVisibilityOptions = {}): boolean {
let checkOpacity = true;

if (options.opacityProperty != undefined || options.checkOpacity != undefined) {
checkOpacity = !!(options.opacityProperty || options.checkOpacity);
}

let checkVisibility = true;

if (options.visibilityProperty != undefined || options.checkVisibilityCSS != undefined) {
checkVisibility = !!(options.visibilityProperty || options.checkVisibilityCSS);
}

let checkContentVisibility = true;

if (options.contentVisibilityAuto != undefined) {
checkContentVisibility = !!options.contentVisibilityAuto;
}

const computedStyle = getComputedStyle(this);

// technically, this should also check for contentVisibility === "auto" and then
// traverse the ancestors of this node to see if any have contentVisibility set
// to "hidden", but Blockly doesn't use content-visibility AFAIK
if (
computedStyle.display === "none" ||
(checkOpacity && computedStyle.opacity === "0") ||
(checkVisibility && computedStyle.visibility === "hidden") ||
(checkContentVisibility && (computedStyle as any).contentVisibility === "hidden")
) {
return false;
}

try {
const rec = this.getBoundingClientRect();
if (rec.width === 0 || rec.height === 0) {
return false;
}
}
catch {
// some versions of firefox throw if an element is not in the DOM
// and getBoundingClientRect is called
return false;
}

return true;
}
}
}