Skip to content

Commit 7a88b71

Browse files
committed
work on removing debounce
1 parent 269238f commit 7a88b71

File tree

2 files changed

+52
-31
lines changed

2 files changed

+52
-31
lines changed

demo.html

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
height: 100vh;
1313
margin: 0;
1414
}
15-
#annotated-text {
15+
.annotated-text {
1616
font-size: 2em;
17-
padding: 20px;
17+
padding: 4px;
1818
border: 1px solid #ccc;
1919
cursor: pointer;
20+
margin: 8px;
2021
}
2122
.instructions {
2223
margin-top: 20px;
@@ -26,7 +27,27 @@
2627
</style>
2728
</head>
2829
<body>
29-
<div id="annotated-text">Click me to toggle the annotation.</div>
30+
<div class="annotated-text">Click me to toggle the annotation.</div>
31+
<div class="annotated-text">Click me to toggle the annotation.</div>
32+
<div class="annotated-text">Click me to toggle the annotation.</div>
33+
<div class="annotated-text">Click me to toggle the annotation.</div>
34+
<div class="annotated-text">Click me to toggle the annotation.</div>
35+
<div class="annotated-text">Click me to toggle the annotation.</div>
36+
<div class="annotated-text">Click me to toggle the annotation.</div>
37+
<div class="annotated-text">Click me to toggle the annotation.</div>
38+
<div class="annotated-text">Click me to toggle the annotation.</div>
39+
<div class="annotated-text">Click me to toggle the annotation.</div>
40+
<div class="annotated-text">Click me to toggle the annotation.</div>
41+
<div class="annotated-text">Click me to toggle the annotation.</div>
42+
<div class="annotated-text">Click me to toggle the annotation.</div>
43+
<div class="annotated-text">Click me to toggle the annotation.</div>
44+
<div class="annotated-text">Click me to toggle the annotation.</div>
45+
<div class="annotated-text">Click me to toggle the annotation.</div>
46+
<div class="annotated-text">Click me to toggle the annotation.</div>
47+
<div class="annotated-text">Click me to toggle the annotation.</div>
48+
<div class="annotated-text">Click me to toggle the annotation.</div>
49+
<div class="annotated-text">Click me to toggle the annotation.</div>
50+
<div class="annotated-text">Click me to toggle the annotation.</div>
3051

3152
<div class="instructions">
3253
This demo shows the animated `hide()` method.
@@ -36,21 +57,27 @@
3657
<script>
3758
const { annotate } = RoughNotation;
3859

39-
const el = document.getElementById("annotated-text");
40-
const annotation = annotate(el, {
41-
type: "underline",
42-
color: "red",
43-
animationDuration: 1000,
44-
animateOnHide: true,
45-
});
60+
const els = document.getElementsByClassName("annotated-text");
61+
const annotations = Array.from(els).map((el) =>
62+
annotate(el, {
63+
type: "underline",
64+
color: "red",
65+
animationDuration: 1000,
66+
animateOnHide: true,
67+
})
68+
);
4669

47-
el.addEventListener("click", () => {
48-
if (annotation.isShowing()) {
49-
annotation.hide();
50-
} else {
51-
annotation.show();
52-
}
53-
});
70+
for (let i = 0; i < annotations.length; i++) {
71+
const annotation = annotations[i];
72+
const el = els[i];
73+
el.addEventListener("click", () => {
74+
if (annotation.isShowing()) {
75+
annotation.hide();
76+
} else {
77+
annotation.show();
78+
}
79+
});
80+
}
5481

5582
// Initially show the annotation
5683
annotation.show();

src/rough-notation.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type AnnotationState = "unattached" | "not-showing" | "showing";
1515
class RoughAnnotationImpl implements RoughAnnotation {
1616
private _state: AnnotationState = "unattached";
1717
private _config: RoughAnnotationConfig;
18-
private _resizing = false;
1918
private _ro?: any; // ResizeObserver is not supported in typescript std lib yet
2019
private _seed = randomSeed();
2120

@@ -90,16 +89,8 @@ class RoughAnnotationImpl implements RoughAnnotation {
9089
}
9190

9291
private _resizeListener = () => {
93-
if (!this._resizing) {
94-
this._resizing = true;
95-
setTimeout(() => {
96-
this._resizing = false;
97-
if (this._state === "showing") {
98-
if (this.haveRectsChanged()) {
99-
this.show();
100-
}
101-
}
102-
}, 400);
92+
if (this._state === "showing" && this.haveRectsChanged()) {
93+
this.show();
10394
}
10495
};
10596

@@ -203,7 +194,7 @@ class RoughAnnotationImpl implements RoughAnnotation {
203194
case "unattached":
204195
break;
205196
case "showing":
206-
this.hide();
197+
this.hide(/* force */ true);
207198
if (this._svg) {
208199
this.render(this._svg, true);
209200
}
@@ -217,12 +208,15 @@ class RoughAnnotationImpl implements RoughAnnotation {
217208
}
218209
}
219210

220-
hide(): void {
211+
/**
212+
* @param force - If true, the annotation will be hidden immediately without animation.
213+
*/
214+
hide(force?: boolean): void {
221215
if (!this.isShowing()) {
222216
return;
223217
}
224218
const animate = this.animateOnHide ?? false;
225-
if (this._svg && animate) {
219+
if (this._svg && !force && animate) {
226220
const paths = Array.from(this._svg.querySelectorAll("path"));
227221
if (paths.length > 0) {
228222
const animationDuration =

0 commit comments

Comments
 (0)