Skip to content
Open
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
18 changes: 18 additions & 0 deletions modules/ui-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Shared UI helpers with no messenger.* dependency.
*/

/**
* Filters a list of template elements by the current search input and category filter.
* @param {string} itemSelector - CSS selector for the template items to filter.
*/
export function filterTemplateList(itemSelector) {
const query = document.getElementById("search-input").value.toLowerCase().trim();
const selectedCategory = document.getElementById("category-filter").value.toLowerCase();
for (const item of document.querySelectorAll(itemSelector)) {
const matchesSearch =
!query || item.dataset.name.includes(query) || item.dataset.subject.includes(query);
const matchesCategory = !selectedCategory || item.dataset.category === selectedCategory;
item.hidden = !(matchesSearch && matchesCategory);
}
}
11 changes: 2 additions & 9 deletions options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ATTACHMENT_WARN_SIZE,
ATTACHMENT_TOTAL_WARN_SIZE,
} from "../modules/validation.js";
import { filterTemplateList } from "../modules/ui-helpers.js";

let editingId = null;
let pendingAttachments = [];
Expand Down Expand Up @@ -877,15 +878,7 @@ async function handleImport(file) {
}

function filterTemplates() {
const query = document.getElementById("search-input").value.toLowerCase().trim();
const selectedCategory = document.getElementById("category-filter").value.toLowerCase();
const cards = document.querySelectorAll("#template-list .template-card");
for (const card of cards) {
const matchesSearch =
!query || card.dataset.name.includes(query) || card.dataset.subject.includes(query);
const matchesCategory = !selectedCategory || card.dataset.category === selectedCategory;
card.hidden = !(matchesSearch && matchesCategory);
}
filterTemplateList("#template-list .template-card");
}

document.getElementById("search-input").addEventListener("input", filterTemplates);
Expand Down
11 changes: 2 additions & 9 deletions popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isTemplateAllowedForIdentity,
} from "../modules/template-store.js";
import { insertTemplateIntoTab } from "../modules/template-insert.js";
import { filterTemplateList } from "../modules/ui-helpers.js";

async function getCurrentIdentityId() {
try {
Expand Down Expand Up @@ -189,15 +190,7 @@ async function insertTemplate(id) {
}

function filterTemplates() {
const query = document.getElementById("search-input").value.toLowerCase().trim();
const selectedCategory = document.getElementById("category-filter").value.toLowerCase();
const items = document.querySelectorAll("#template-list .template-item");
for (const item of items) {
const matchesSearch =
!query || item.dataset.name.includes(query) || item.dataset.subject.includes(query);
const matchesCategory = !selectedCategory || item.dataset.category === selectedCategory;
item.hidden = !(matchesSearch && matchesCategory);
}
filterTemplateList("#template-list .template-item");
}

async function populateCategoryFilter() {
Expand Down
Loading