* feat(saved-views): keep a collection in a saved view Images saved views gain a collection key (#331 D140): empty or a collection id of 1 to 9 digits. Writes refuse favorites together with a collection; older rows read an empty collection, and a stored row with both opens the collection, as the listing does for a URL with both. Part of #331 * feat(collections): store collections in the library database * feat(collections): add, remove and rename collections and keep members through duplicate cleanup * feat(collections): add collection routes and the Images collection filter * feat(images): store images' own categories in the library database (#332) One new table, image_categories, and its category index, created empty by a forward-only migration that reads and writes only its own objects. Startup checks the structure; restore also checks the values. The store sets, clears and reads own categories by file key (the deepest output source holding the file and the path below it), and copies a removed duplicate's category to a kept copy that has none. * feat(images): duplicate cleanup keeps images' own categories (#332) The kept copy's own category wins; a kept copy without one receives the removed copy's, keyed by the deepest output source holding each file. The removed copy's row is never deleted, so a give-back or a failed delete loses nothing. The job log reports how many were kept. * feat(images): set, remove and list images' own categories (#332) PUT /prompt_manager/images/category sets the own category of up to 500 listed files, DELETE removes it (listed or not), and GET /prompt_manager/images/categories lists the values whose file is listed. Targets are the Images listing identity of an output source, checked by media_target (shared with Collections). The routes never touch prompts, recipes or links; POST /prompt_manager/bulk/category is unchanged. * fix(images): key symlinked output folders and keep key work off the event loop (#332) Review fixes: a source matches by its configured spelling too, so a detected output folder that is a symlink keys its files (routes and duplicate cleanup). File keys are computed in the executor. A path no source holds is a plain 403, a lone surrogate a 400. The install repairs a table that lacks its category index instead of refusing to open. * fix(collections): address review findings * feat(images): set or remove a category on all N matching images (#332) The category routes also accept the Images listing filter of "Select all N matching" (#392) instead of explicit items, resolved now and capped at 500 images like an explicit selection. * feat(collections): browse a collection from the sidebar The sidebar gains the concept's COLLECTIONS group (D136): one row per collection with its folder icon, name and listed-member count, without the PROPOSED badge; the label shows once a collection exists. A row opens #/collection/<id>, the Images page filtered to that collection (D137): the collection's name as h1, Collection as breadcrumb and title, Edit to rename or delete it through the saved views' Edit dialog with collection nouns, and a note counting members the sources do not list now. An unknown or deleted collection shows a page that says so with a link to Images. A saved view on a deleted collection lists nothing and says so (MC6, D140). The list is read again after a restore and the counts 300 ms after a change. Part of #331 * test(collections): compare the COLLECTIONS group with the concept The concept hides COLLECTIONS at 950 px and below, which would leave a phone no way to open a collection; production shows the rows there in the wrapped layout of every other sidebar nav (D136). The shell comparison now compares the group instead of filtering it out: the concept fixture seeds Landscapes (12) and Experiments (8), the rows compare by name order, and at phone width the nav compares against the concept's own sidebar-nav layout. The collection rows are checked at 1600x1100 and 390x844 in both themes. Part of #331 * test(vendor): count the #332 host case in the execution matrix image-attribution.spec.mjs gains the image category route case, so the execution config lists 92 tests (3 attribution cases per project). * test(browser): install the slideshow clock before wrapping setTimeout The slideshow fixture wrapped window.setTimeout before page.clock.install(), so the viewer's timer ran on real time and clock.runFor() did not drive it; a late WebKit timer failed 'slideshow exposes exact state labels' on CI. * feat(collections): add and remove from the selection bar Add to collection follows Set category in the Images selection bar and opens the concept's simpleInput ("Add selected images to collection", "Collection name", Add) with the existing names as a datalist. Remove from collection follows it while a collection filter is active and reloads the listing in place, keeping the checked files still listed. Every outcome uses the design's strings (section 7.4). The routes take at most 500 explicit files, so an "all N matching" selection is sent only when every match is loaded, checked and at most 500; otherwise the action toasts "Select at most 500 images." (D138, #331). * fix(collections): address review findings - A list read superseded by a newer one (the 300 ms count read, a restore) no longer shows "could not be loaded": the collection and saved-view pages wait for the list to settle. - A listing that finds the open collection gone shows the deleted page once the list no longer holds it; when the list read fails or still holds it, the page keeps the listing's error and Retry instead of staying blank, and each failed read is reported again. - The missing-members note follows every list read and goes away at zero. - A rename that settles after its Edit dialog closed leaves any dialog opened since alone. - The deleted-collection, list-failed and saved-view failure pages focus their title after a route change, as every page does. Part of #331 * feat(collections): add and remove from the inspector and the viewer The shared inspector's footer ends with Collections… (folder icon) after Prompt history for a listed file, in the Images side panel and the viewer. Its dialog reads the file's membership, lists every collection as the concept's checkbox rows, takes an optional new name, and Save sends one request per change (new name, added, removed). A failure keeps the dialog with the membership read again. On a collection page a change to the open collection reloads the listing at once from the side panel and only when the viewer closes from the viewer. The inspector concept comparison draws the button into the concept after Prompt history (D139, #331). * test(collections): compare the collection page and dialog with the concept images-concept compares Add to collection in place in the selection bar (only Remove links… stays omitted) and the collection page's heading, toolbar and filter row against the concept's #collection-landscapes, with D137's Edit drawn into the concept. collections-concept compares the Add to collection dialog with the concept's simpleInput (D138: "Add", an empty field, the datalist of names), in both themes at 1600 and 390 px, with a mutation check. TESTING.md gains the Collections contract (database, keys, missing members, duplicate move, restore, routes, rules, limits, the PR 1 rulings and the all-matching ruling), D136-D140, and amends D28 and D8; docs/application-redesign.md gets the delivery-status line. The coverage baseline adds the two new modules; optional collection callbacks are null rather than no-op defaults so they do not count as uncovered functions. * test(coverage): keep main's floors for files this PR does not touch coverage:update raised nine floors of untouched files from one run; metadata-autocomplete.js branches then measured 84.84 against 85. * fix(collections): address review findings - M1: after a failed Save the Collections dialog shows the membership loading and keeps Save disabled until the re-read lands, so a stale plan cannot be sent. - M2: the changes a Save made reach the page only when the dialog closes, so a collection page's reload cannot remove the dialog before its failure is read. - L4: a New collection name equal to a listed collection ignoring case is that collection's checkbox, checked; never an add then a remove. - L5: a name that cannot be a collection name is refused on the field; nothing is sent, the checkbox edits stay, no re-read. - L6: focus starts on the field and moves to the first checkbox once listed; closing after a re-render replaced the trigger focuses the inspector's current Collections… button or its heading (openContentDialog takes an optional focusTarget). - L3, L7: TESTING.md D138/D139 describe the stale-source and deleted collection outcomes and the above; the viewer spec asserts no listing request while it is open, and a browser test covers M2. * test(legacy): record the image category routes in the legacy contract snapshot (#332) #332 adds PUT/DELETE /prompt_manager/images/category and GET /prompt_manager/images/categories; the legacy pages' routes are unchanged. * test(duplicates): a removed copy with a collection and a category moves both (#331, #332) * test(collections): map the Add to collection suggestions to the shared dialog's datalist (#331) After the merge with #332 PR 3 the input dialog places its datalist after the field with the id workspace-input-suggestions; the concept mapping follows.
190 lines
9.2 KiB
JavaScript
190 lines
9.2 KiB
JavaScript
/**
|
|
* The shared inspector's "Collections…" dialog (#331 D139, design §8), beyond the concept: one listed file's
|
|
* collections as a checklist in the sidebar's order, a New collection field, then Cancel and Save.
|
|
*
|
|
* - The file's membership is read when the dialog opens ("Loading collections…"); a failed read says "Collections
|
|
* could not be loaded." with Retry, and Save waits for a read that worked.
|
|
* - Each row is the concept's checkbox row (`label.tag-suggestion`, app.js a1128). With no collection the list says
|
|
* "No collections yet. Name one below."
|
|
* - Save sends one request per change, in order: the new name, each newly checked collection, each unchecked one.
|
|
* Nothing changed sends nothing and closes. All sent: the dialog closes with "Collections updated.". A failure keeps
|
|
* it open with "The collections could not be updated. Try again." and the membership read again; the changes that
|
|
* went through still reach `onChanged({added, removed})` (collection ids), as a full success does, but only once the
|
|
* dialog closes: a page that lists the file again (a collection page) would otherwise remove the dialog before its
|
|
* failure is read. Save waits for the membership read again.
|
|
* - A New collection name equal to a listed collection ignoring case is that collection's checkbox, checked. A name
|
|
* that cannot be a collection name is refused on the field; nothing is sent and the checkbox edits stay.
|
|
* - Focus starts on the field and moves to the first checkbox once they are listed; closing returns it to the
|
|
* trigger, or to `focusFallback()` when an inspector re-render removed the trigger.
|
|
*/
|
|
import { createButton } from '../shared/toolkit/core.js';
|
|
import { closeWorkspaceDialog, field, openContentDialog } from './workspace-dialogs.mjs';
|
|
import { showWorkspaceToast } from './toast.mjs';
|
|
import { COLLECTIONS_UPDATED_TOAST, MAX_COLLECTION_NAME_LENGTH, MEMBERSHIP_FAILED_TEXT, MEMBERSHIP_LOADING_TEXT,
|
|
NO_COLLECTIONS_TEXT, UPDATE_FAILED_TEXT, collectionNameKey, normalizeCollectionName } from './collections.mjs';
|
|
|
|
export const COLLECTIONS_DIALOG_TITLE = 'Collections';
|
|
export const NEW_COLLECTION_LABEL = 'New collection';
|
|
|
|
function el(tag, text, className) {
|
|
const node = document.createElement(tag);
|
|
if (text !== undefined) node.textContent = text;
|
|
if (className) node.className = className;
|
|
return node;
|
|
}
|
|
|
|
/**
|
|
* Open the dialog for `target` (`{root_index, root_scope, path}`) with the client store `collections`. `openDialog`
|
|
* and `closeDialog` are the dialog seams; `notify` the toast.
|
|
*/
|
|
export function openCollectionsDialog({ scope, collections, target, trigger = null, notify = showWorkspaceToast, onChanged = null,
|
|
focusFallback = null, openDialog = openContentDialog, closeDialog = closeWorkspaceDialog }) {
|
|
const local = new AbortController();
|
|
const active = () => !scope.signal.aborted && !local.signal.aborted;
|
|
let member = null, saving = false, listed = false;
|
|
// The confirmed changes, reported once when the dialog closes (review M2).
|
|
const changes = { added: [], removed: [] };
|
|
const status = el('p', '', 'small');
|
|
const retry = createButton('Retry');
|
|
const list = el('div');
|
|
list.setAttribute('role', 'group');
|
|
list.setAttribute('aria-label', COLLECTIONS_DIALOG_TITLE);
|
|
const input = el('input');
|
|
input.value = '';
|
|
input.maxLength = MAX_COLLECTION_NAME_LENGTH;
|
|
input.autocomplete = 'off';
|
|
input.addEventListener('input', () => input.setCustomValidity?.(''));
|
|
const problem = el('p', '', 'small');
|
|
problem.setAttribute('role', 'alert');
|
|
problem.hidden = true;
|
|
const actions = el('div', undefined, 'dialog-actions');
|
|
const cancel = createButton('Cancel');
|
|
const save = createButton('Save', 'ca-primary');
|
|
actions.append(cancel, save);
|
|
const blocked = () => member === null || saving;
|
|
const paintSave = () => {
|
|
if (blocked()) save.setAttribute('aria-disabled', 'true');
|
|
else save.removeAttribute('aria-disabled');
|
|
};
|
|
retry.addEventListener('click', () => load());
|
|
cancel.addEventListener('click', () => { if (!saving) closeDialog(); });
|
|
save.addEventListener('click', () => { if (!blocked()) submit(); });
|
|
|
|
/** One checkbox row per collection, checked where the file is a member. */
|
|
function renderList() {
|
|
list.replaceChildren();
|
|
if (!collections.collections.length) {
|
|
list.append(el('p', NO_COLLECTIONS_TEXT, 'small'));
|
|
return;
|
|
}
|
|
for (const item of collections.collections) {
|
|
const row = el('label', undefined, 'tag-suggestion');
|
|
const box = el('input');
|
|
Object.assign(box, { type: 'checkbox', id: `collection-choice-${item.id}`, checked: member.has(item.id) });
|
|
box.dataset.collectionId = String(item.id);
|
|
row.htmlFor = box.id;
|
|
row.append(box, el('span', item.name));
|
|
list.append(row);
|
|
}
|
|
}
|
|
function showStatus(text, failed) {
|
|
status.textContent = text;
|
|
status.setAttribute('role', failed ? 'alert' : 'status');
|
|
status.hidden = !text;
|
|
retry.hidden = !failed;
|
|
}
|
|
/** Save waits while the membership is read (again). */
|
|
function loading() {
|
|
member = null;
|
|
paintSave();
|
|
list.replaceChildren();
|
|
showStatus(MEMBERSHIP_LOADING_TEXT, false);
|
|
}
|
|
async function load() {
|
|
loading();
|
|
try {
|
|
const ids = await collections.membership(target);
|
|
if (!active()) return;
|
|
member = new Set(ids);
|
|
showStatus('', false);
|
|
renderList();
|
|
// The first listing takes focus from the untouched field to the first checkbox (review L6).
|
|
if (!listed && !input.value) firstBox().focus();
|
|
listed = true;
|
|
} catch {
|
|
if (!active()) return;
|
|
showStatus(MEMBERSHIP_FAILED_TEXT, true);
|
|
}
|
|
paintSave();
|
|
}
|
|
const boxesOf = () => [...list.children].map(row => row.children?.[0]).filter(box => box?.dataset?.collectionId);
|
|
const firstBox = () => boxesOf()[0] ?? input;
|
|
/**
|
|
* The requests Save sends, in order; null when the new name cannot be a collection name. A name equal to a listed
|
|
* collection's ignoring case is that collection's checkbox, checked (review L4).
|
|
*/
|
|
function plan() {
|
|
const typed = input.value.trim();
|
|
const name = typed ? normalizeCollectionName(typed) : '';
|
|
if (name === null) return null;
|
|
const named = name ? collections.collections.find(item => collectionNameKey(item.name) === collectionNameKey(name)) : null;
|
|
const steps = name && !named ? [{ add: { name } }] : [];
|
|
const boxes = boxesOf().map(box => ({ id: Number(box.dataset.collectionId), checked: box.checked }))
|
|
.map(box => (box.id === named?.id ? { ...box, checked: true } : box));
|
|
for (const box of boxes) if (box.checked && !member.has(box.id)) steps.push({ add: { collectionId: box.id } });
|
|
for (const box of boxes) if (!box.checked && member.has(box.id)) steps.push({ remove: box.id });
|
|
return steps;
|
|
}
|
|
async function submit() {
|
|
problem.textContent = '';
|
|
problem.hidden = true;
|
|
const steps = plan();
|
|
if (steps === null) {
|
|
// Review L5: refused on the field; nothing is sent and the checkbox edits stay.
|
|
input.setCustomValidity?.(UPDATE_FAILED_TEXT);
|
|
input.reportValidity?.();
|
|
input.focus();
|
|
return;
|
|
}
|
|
if (steps.length === 0) { closeDialog(); return; }
|
|
saving = true;
|
|
paintSave();
|
|
let failed = false;
|
|
for (const step of steps) {
|
|
try {
|
|
if (step.add) changes.added.push((await collections.add(step.add, [target])).collection.id);
|
|
else changes.removed.push((await collections.remove(step.remove, [target])).collection.id);
|
|
} catch {
|
|
failed = true;
|
|
break;
|
|
}
|
|
if (!active()) return;
|
|
if (step.add?.name) input.value = '';
|
|
}
|
|
if (!active()) return;
|
|
saving = false;
|
|
if (!failed) {
|
|
closeDialog();
|
|
notify(COLLECTIONS_UPDATED_TOAST);
|
|
return;
|
|
}
|
|
problem.textContent = UPDATE_FAILED_TEXT;
|
|
problem.hidden = false;
|
|
// Review M1: Save waits from here until the membership is read again. A collection deleted meanwhile is
|
|
// re-read by the store; the list then shows what the server holds.
|
|
loading();
|
|
await collections.settled();
|
|
if (active()) await load();
|
|
}
|
|
function closed() {
|
|
local.abort();
|
|
if (changes.added.length || changes.removed.length) onChanged?.({ added: [...changes.added], removed: [...changes.removed] });
|
|
// The shell focuses a trigger still on the page; one an inspector re-render replaced falls back (review L6).
|
|
if (trigger && !trigger.isConnected) focusFallback?.()?.focus();
|
|
}
|
|
showStatus('', false);
|
|
openDialog({ scope, title: COLLECTIONS_DIALOG_TITLE, trigger, canClose: () => !saving, onClose: closed, focusTarget: input,
|
|
nodes: [status, retry, list, field(NEW_COLLECTION_LABEL, input, 'collection-new'), problem, actions] });
|
|
load();
|
|
}
|