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

keep-text-in-occlusion #3277

Merged
merged 4 commits into from
Jul 20, 2024
Merged
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
40 changes: 25 additions & 15 deletions ts/routes/image-occlusion/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async function setupImageOcclusionInner(setupOptions?: SetupImageOcclusionOption
window.addEventListener("keydown", (event) => {
const button = document.getElementById("toggle");
if (button && button.style.display !== "none" && event.key === "M") {
toggleMasks();
toggleMasks(setupOptions);
}
});
oneTimeSetupDone = true;
Expand All @@ -169,7 +169,7 @@ async function setupImageOcclusionInner(setupOptions?: SetupImageOcclusionOption
const button = document.getElementById("toggle");
if (button) {
if (document.querySelector("[data-occludeinactive=\"1\"]")) {
button.addEventListener("click", toggleMasks);
button.addEventListener("click", () => toggleMasks(setupOptions));
} else {
button.style.display = "none";
}
Expand All @@ -182,13 +182,16 @@ function drawShapes(
canvas: HTMLCanvasElement,
onWillDrawShapes?: DrawShapesFilter,
onDidDrawShapes?: DrawShapesCallback,
allowedShapes?: Array<typeof Shape>,
): void {
const context: CanvasRenderingContext2D = canvas.getContext("2d")!;
const size = canvas;
const filterByAllowedShapes = (el: Shape) =>
(allowedShapes && allowedShapes.length > 0) ? allowedShapes.some(s => el instanceof s) : true;

let activeShapes = extractShapesFromRenderedClozes(".cloze");
let inactiveShapes = extractShapesFromRenderedClozes(".cloze-inactive");
let highlightShapes = extractShapesFromRenderedClozes(".cloze-highlight");
let activeShapes = extractShapesFromRenderedClozes(".cloze").filter(filterByAllowedShapes);
let inactiveShapes = extractShapesFromRenderedClozes(".cloze-inactive").filter(filterByAllowedShapes);
let highlightShapes = extractShapesFromRenderedClozes(".cloze-highlight").filter(filterByAllowedShapes);
let properties = getShapeProperties();

const processed = onWillDrawShapes?.({ activeShapes, inactiveShapes, highlightShapes, properties }, context);
Expand Down Expand Up @@ -230,7 +233,12 @@ function drawShapes(
});
}

onDidDrawShapes?.({ activeShapes, inactiveShapes, highlightShapes, properties }, context);
onDidDrawShapes?.({
activeShapes,
inactiveShapes,
highlightShapes,
properties,
}, context);
}

interface DrawShapeParameters {
Expand Down Expand Up @@ -452,14 +460,16 @@ function getShapeProperties(): ShapeProperties {
}
}

const toggleMasks = (): void => {
const canvas = document.getElementById(
"image-occlusion-canvas",
) as HTMLCanvasElement;
const display = canvas.style.display;
if (display === "none") {
canvas.style.display = "unset";
} else {
canvas.style.display = "none";
let hide = false;
const toggleMasks = (setupOptions?: SetupImageOcclusionOptions): void => {
const canvas = document.getElementById("image-occlusion-canvas") as HTMLCanvasElement;
const context = canvas.getContext("2d")!;

hide = !hide;
context.clearRect(0, 0, canvas.width, canvas.height);
if (hide) {
drawShapes(canvas, setupOptions?.onWillDrawShapes, setupOptions?.onDidDrawShapes, [Text]);
return;
}
drawShapes(canvas, setupOptions?.onWillDrawShapes, setupOptions?.onDidDrawShapes);
};