Skip to content

Commit 14e5fb3

Browse files
committed
fix for polygon points, add br in cloze strings, & toogle masks button
- fix shapes at edges - toggle masks button to show/hide masks - hide clozes string, it contains <br> - set height for div container (used 'relative' in css)
1 parent 06b7ba7 commit 14e5fb3

File tree

7 files changed

+58
-22
lines changed

7 files changed

+58
-22
lines changed

ftl/core/notetypes.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ notetypes-occlusion = Occlusion
4343
notetypes-occlusion-mask = Mask
4444
notetypes-occlusion-note = Note
4545
notetypes-comments-field = Comments
46+
notetypes-toggle-masks = Toggle Masks
4647
notetypes-image-occlusion-name = Image Occlusion
4748
notetypes-hide-all-guess-one = Hide All, Guess One
4849
notetypes-hide-one-guess-one = Hide One, Guess One

rslib/src/image_occlusion/imagedata.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl Collection {
208208
let comments = tr.notetypes_comments_field();
209209
nt.add_field(comments.as_ref());
210210
let qfmt = format!(
211-
"{{{{cloze:{}}}}}
211+
"<div style=\"display: none\">{{{{cloze:{}}}}}</div>
212212
<div id=container>
213213
{{{{{}}}}}
214214
<canvas id=\"canvas\" class=\"image-occlusion-canvas\"></canvas>
@@ -227,8 +227,18 @@ try {{
227227
tr.notetypes_error_loading_image_occlusion(),
228228
);
229229
let afmt = format!(
230-
"{{{{{}}}}}\n{}\n{{{{{}}}}}\n{{{{{}}}}}",
231-
header, qfmt, back_extra, comments
230+
"{{{{{}}}}}
231+
{}
232+
<button id=\"toggle\">{}</button>
233+
<br>
234+
{{{{{}}}}}
235+
<br>
236+
{{{{{}}}}}",
237+
header,
238+
qfmt,
239+
tr.notetypes_toggle_masks(),
240+
back_extra,
241+
comments,
232242
);
233243
nt.add_template(nt.name.clone(), qfmt, afmt);
234244
nt

rslib/src/image_occlusion/imageocclusion.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright: Ankitects Pty Ltd and contributors
22
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
33

4+
use std::fmt::Write;
5+
46
// split following
57
// text = "rect:399.01,99.52,167.09,33.78:fill=#0a2cee:stroke=1"
68
// with
@@ -52,12 +54,12 @@ pub fn get_image_cloze_data(text: &str) -> String {
5254
}
5355
"points" => {
5456
if !values[1].is_empty() {
55-
let points_vec: Vec<&str> = values[1].split(' ').collect();
5657
let mut point_str = String::new();
57-
for point in &points_vec {
58-
let xy: Vec<&str> = point.split(',').collect();
59-
point_str.push_str(&format!("[{},{}],", xy[0], xy[1]));
58+
for point_pair in values[1].split(' ') {
59+
let Some((x, y)) = point_pair.split_once(',') else { continue };
60+
write!(&mut point_str, "[{},{}],", x, y).unwrap();
6061
}
62+
// remove the trailing comma
6163
point_str.pop();
6264
if !point_str.is_empty() {
6365
result.push_str(&format!("data-points=\"[{}]\" ", point_str));

ts/image-occlusion/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const getCloze = (object, index, relativePos, hideInactive): string => {
8383
});
8484

8585
clozeData += `:hideinactive=${hideInactive}`;
86-
clozeData = `{{c${index + 1}::image-occlusion${clozeData}}}\n`;
86+
clozeData = `{{c${index + 1}::image-occlusion${clozeData}}}<br>`;
8787
return clozeData;
8888
};
8989

ts/image-occlusion/tools/tool-ellipse.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export const drawEllipse = (canvas: fabric.Canvas): void => {
4848
const pointer = canvas.getPointer(o.e);
4949
let rx = Math.abs(origX - pointer.x) / 2;
5050
let ry = Math.abs(origY - pointer.y) / 2;
51+
const x = pointer.x;
52+
const y = pointer.y;
5153

5254
if (rx > ellipse.strokeWidth) {
5355
rx -= ellipse.strokeWidth / 2;
@@ -56,30 +58,30 @@ export const drawEllipse = (canvas: fabric.Canvas): void => {
5658
ry -= ellipse.strokeWidth / 2;
5759
}
5860

59-
if (pointer.x < origX) {
61+
if (x < origX) {
6062
ellipse.set({ originX: "right" });
6163
} else {
6264
ellipse.set({ originX: "left" });
6365
}
6466

65-
if (pointer.y < origY) {
67+
if (y < origY) {
6668
ellipse.set({ originY: "bottom" });
6769
} else {
6870
ellipse.set({ originY: "top" });
6971
}
7072

7173
// do not draw outside of canvas
72-
if (pointer.x < ellipse.strokeWidth) {
73-
rx = origX / 2;
74+
if (x < ellipse.strokeWidth) {
75+
rx = (origX + ellipse.strokeWidth + 0.5) / 2;
7476
}
75-
if (pointer.y < ellipse.strokeWidth) {
76-
ry = origY / 2;
77+
if (y < ellipse.strokeWidth) {
78+
ry = (origY + ellipse.strokeWidth + 0.5) / 2;
7779
}
78-
if (pointer.x >= canvas.width - ellipse.strokeWidth) {
79-
rx = (canvas.width - origX) / 2;
80+
if (x >= canvas.width - ellipse.strokeWidth) {
81+
rx = (canvas.width - origX) / 2 - ellipse.strokeWidth + 0.5;
8082
}
81-
if (pointer.y > canvas.height - ellipse.strokeWidth) {
82-
ry = (canvas.height - origY) / 2;
83+
if (y > canvas.height - ellipse.strokeWidth) {
84+
ry = (canvas.height - origY) / 2 - ellipse.strokeWidth + 0.5;
8385
}
8486

8587
ellipse.set({ rx: rx, ry: ry });

ts/image-occlusion/tools/tool-rect.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ export const drawRectangle = (canvas: fabric.Canvas): void => {
6363

6464
// do not draw outside of canvas
6565
if (x < rect.strokeWidth) {
66-
x = 0;
66+
x = -rect.strokeWidth + 0.5;
6767
}
6868
if (y < rect.strokeWidth) {
69-
y = 0;
69+
y = -rect.strokeWidth + 0.5;
7070
}
7171
if (x >= canvas.width - rect.strokeWidth) {
72-
x = canvas.width;
72+
x = canvas.width - rect.strokeWidth + 0.5;
7373
}
7474
if (y >= canvas.height - rect.strokeWidth) {
75-
y = canvas.height;
75+
y = canvas.height - rect.strokeWidth + 0.5;
7676
}
7777

7878
rect.set({

ts/reviewer/image_occlusion.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ export function setupImageCloze(): void {
1515
const size = limitSize({ width: image.naturalWidth, height: image.naturalHeight });
1616
canvas.width = size.width;
1717
canvas.height = size.height;
18+
19+
// set height for div container (used 'relative' in css)
20+
const container = document.getElementById("container") as HTMLDivElement;
21+
container.style.height = `${image.height}px`;
22+
23+
// setup button for toggle image occlusion
24+
const button = document.getElementById("toggle");
25+
if (button) {
26+
button.addEventListener("click", toggleMasks);
27+
}
28+
1829
drawShapes(ctx);
1930
}
2031

@@ -171,3 +182,13 @@ function getShapeProperty(): {
171182
};
172183
}
173184
}
185+
186+
const toggleMasks = (): void => {
187+
const canvas = document.getElementById("canvas") as HTMLCanvasElement;
188+
const display = canvas.style.display;
189+
if (display === "none") {
190+
canvas.style.display = "unset";
191+
} else {
192+
canvas.style.display = "none";
193+
}
194+
};

0 commit comments

Comments
 (0)