Skip to content

Commit

Permalink
destroy rows
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpkane committed Oct 24, 2024
1 parent 308447a commit 2a753e6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
10 changes: 5 additions & 5 deletions app/packages/spotlight/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
SCROLLBAR_WIDTH,
TWO,
ZERO,
ZOOMING_COEFFICIENT
ZOOMING_COEFFICIENT,
} from "./constants";
import createScrollReader from "./createScrollReader";
import { Load, RowChange } from "./events";
Expand Down Expand Up @@ -107,8 +107,8 @@ export default class Spotlight<K, V> extends EventTarget {
throw new Error("spotlight is not attached");
}

this.#backward?.remove();
this.#forward?.remove();
this.#backward?.destroy();
this.#forward?.destroy();
this.#element?.classList.remove(styles.spotlightLoaded);
this.#element?.remove();
this.#scrollReader?.destroy();
Expand Down Expand Up @@ -203,7 +203,7 @@ export default class Spotlight<K, V> extends EventTarget {
const backward = this.#forward;
this.#forward = section;
this.#forward.attach(this.#element);
this.#backward.remove();
this.#backward.destroy();
this.#backward = backward;
offset = before - this.#containerHeight + this.#config.spacing;
}
Expand Down Expand Up @@ -271,7 +271,7 @@ export default class Spotlight<K, V> extends EventTarget {
this.#backward = result.section;
this.#backward.attach(this.#element);

this.#forward.remove();
this.#forward.destroy();
this.#forward = forward;
}

Expand Down
24 changes: 12 additions & 12 deletions app/packages/spotlight/src/row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class Row<K, V> {
#from: number;
#hidden: boolean;

readonly #aborter: AbortController = new AbortController();
readonly #config: SpotlightConfig<K, V>;
readonly #dangle?: boolean;
readonly #container: HTMLDivElement = create(DIV);
Expand Down Expand Up @@ -47,7 +48,7 @@ export default class Row<K, V> {
element.style.top = pixels(ZERO);

if (config.onItemClick) {
element.addEventListener("click", (event) => {
const handler = (event) => {
if (event.metaKey || event.shiftKey) {
return;
}
Expand All @@ -59,18 +60,13 @@ export default class Row<K, V> {
item,
iter,
});
};

element.addEventListener("click", handler, {
signal: this.#aborter.signal,
});
element.addEventListener("contextmenu", (event) => {
if (event.metaKey || event.shiftKey) {
return;
}
event.preventDefault();
focus(item.id);
config.onItemClick({
event,
item,
iter,
});
element.addEventListener("contextmenu", handler, {
signal: this.#aborter.signal,
});
}

Expand Down Expand Up @@ -123,6 +119,10 @@ export default class Row<K, V> {
return this.#row[this.#row.length - ONE].item.id;
}

destroy() {
this.#aborter.abort();
}

has(item: string) {
for (const i of this.#row) {
if (i.item.id.description === item) {
Expand Down
13 changes: 8 additions & 5 deletions app/packages/spotlight/src/section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ export default class Section<K, V> {
: element.appendChild(this.#section);
}

destroy() {
this.#section.remove();
for (const row of this.#rows) {
row.destroy();
}
this.#rows = [];
}

find(item: string): Row<K, V> | null {
for (const row of this.#rows) {
if (row.has(item)) {
Expand All @@ -118,11 +126,6 @@ export default class Section<K, V> {
return null;
}

remove() {
this.#section.remove();
this.#rows = [];
}

render({
config,
target,
Expand Down

0 comments on commit 2a753e6

Please sign in to comment.