Use an item picker for quest hasItem objectives.
Replace the raw itemId field with a searchable picker like dialogue rewards, and keep focus when editing clause values.
@@ -1,5 +1,6 @@
1
1
import { normalizeQuestDef } from "/shared/quests/normalize.js";
2
2
import { ConditionKind, emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js";
3
import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";
3
4
4
5
const SIMPLE_KINDS = [
5
6
ConditionKind.hasItem,
@@ -162,10 +163,11 @@ export function initQuestsModal ({ socket, MessageType })
162
163
}),
163
164
field("Text", textInput(obj.text, (v) => { obj.text = v; }))
164
165
);
165
objBox.append(renderConditionSet(obj.when, (next) =>
166
objBox.append(renderConditionSet(obj.when, (next, { remount = true } = {}) =>
166
167
{
167
168
obj.when = next;
168
renderEditor();
169
if (remount)
170
{ renderEditor(); }
169
171
}));
170
172
box.append(objBox);
171
173
});
@@ -300,6 +302,7 @@ function renderConditionSet (when, onChange)
300
302
{
301
303
const wrap = document.createElement("div");
302
304
const set = normalizeConditionSet(when);
305
const bump = (remount = true) => onChange(set, { remount });
303
306
const title = document.createElement("strong");
304
307
title.textContent = "When (all of)";
305
308
wrap.append(title);
@@ -315,7 +318,7 @@ function renderConditionSet (when, onChange)
315
318
notBox.addEventListener("change", () =>
316
319
{
317
320
set.all[index].not = notBox.checked;
318
onChange(set);
321
bump(false);
319
322
});
320
323
notLabel.append(notBox, document.createTextNode(" NOT"));
321
324
row.append(notLabel);
@@ -341,24 +344,23 @@ function renderConditionSet (when, onChange)
341
344
if (kind.value === "__other__")
342
345
{ return; }
343
346
set.all[index].condition = defaultCondition(kind.value);
344
onChange(set);
347
bump(true);
345
348
});
346
349
row.append(kind);
347
350
348
351
const cond = clause.condition ?? { kind: ConditionKind.hasItem };
349
352
if (cond.kind === ConditionKind.hasItem)
350
353
{
351
row.append(miniField("itemId", cond.itemId ?? "", (v) =>
354
row.append(itemConditionPicker(cond, () =>
352
355
{
353
cond.itemId = Number(v) || 0;
354
356
set.all[index].condition = cond;
355
onChange(set);
357
bump(false);
356
358
}));
357
359
row.append(miniField("qty", cond.quantity ?? 1, (v) =>
358
360
{
359
361
cond.quantity = Number(v) || 1;
360
362
set.all[index].condition = cond;
361
onChange(set);
363
bump(false);
362
364
}));
363
365
}
364
366
else if (cond.kind === ConditionKind.questState)
@@ -367,14 +369,14 @@ function renderConditionSet (when, onChange)
367
369
{
368
370
cond.questId = v;
369
371
set.all[index].condition = cond;
370
onChange(set);
372
bump(false);
371
373
}));
372
374
row.append(miniField("state", cond.state ?? "not-started", (v) =>
373
375
{
374
376
const n = Number(v);
375
377
cond.state = Number.isFinite(n) && String(n) === String(v).trim() ? n : v;
376
378
set.all[index].condition = cond;
377
onChange(set);
379
bump(false);
378
380
}));
379
381
}
380
382
else if (cond.kind === ConditionKind.dialogueHeard)
@@ -383,13 +385,13 @@ function renderConditionSet (when, onChange)
383
385
{
384
386
cond.dialogueId = v;
385
387
set.all[index].condition = cond;
386
onChange(set);
388
bump(false);
387
389
}));
388
390
row.append(miniField("stepId", cond.stepId ?? "", (v) =>
389
391
{
390
392
cond.stepId = v;
391
393
set.all[index].condition = cond;
392
onChange(set);
394
bump(false);
393
395
}));
394
396
}
395
397
else if (cond.kind === ConditionKind.killCount)
@@ -398,13 +400,13 @@ function renderConditionSet (when, onChange)
398
400
{
399
401
cond.mobTypeId = v || undefined;
400
402
set.all[index].condition = cond;
401
onChange(set);
403
bump(false);
402
404
}));
403
405
row.append(miniField("count", cond.count ?? 1, (v) =>
404
406
{
405
407
cond.count = Number(v) || 1;
406
408
set.all[index].condition = cond;
407
onChange(set);
409
bump(false);
408
410
}));
409
411
}
410
412
else if (cond.kind === ConditionKind.atTile)
@@ -413,19 +415,19 @@ function renderConditionSet (when, onChange)
413
415
{
414
416
cond.dimensionId = String(v || "overworld");
415
417
set.all[index].condition = cond;
416
onChange(set);
418
bump(false);
417
419
}));
418
420
row.append(miniField("tileX", cond.tileX ?? 0, (v) =>
419
421
{
420
422
cond.tileX = Number(v) | 0;
421
423
set.all[index].condition = cond;
422
onChange(set);
424
bump(false);
423
425
}));
424
426
row.append(miniField("tileY", cond.tileY ?? 0, (v) =>
425
427
{
426
428
cond.tileY = Number(v) | 0;
427
429
set.all[index].condition = cond;
428
onChange(set);
430
bump(false);
429
431
}));
430
432
}
431
433
else if (
@@ -437,7 +439,7 @@ function renderConditionSet (when, onChange)
437
439
{
438
440
cond.dimensionId = String(v || "overworld");
439
441
set.all[index].condition = cond;
440
onChange(set);
442
bump(false);
441
443
}));
442
444
}
443
445
else
@@ -450,7 +452,7 @@ function renderConditionSet (when, onChange)
450
452
try
451
453
{
452
454
set.all[index].condition = JSON.parse(json.value);
453
onChange(set);
455
bump(false);
454
456
}
455
457
catch
456
458
{ /* keep typing */ }
@@ -464,7 +466,7 @@ function renderConditionSet (when, onChange)
464
466
remove.addEventListener("click", () =>
465
467
{
466
468
set.all.splice(index, 1);
467
onChange(set);
469
bump(true);
468
470
});
469
471
row.append(remove);
470
472
wrap.append(row);
@@ -476,7 +478,7 @@ function renderConditionSet (when, onChange)
476
478
add.addEventListener("click", () =>
477
479
{
478
480
set.all.push({ not: false, condition: defaultCondition(ConditionKind.hasItem) });
479
onChange(set);
481
bump(true);
480
482
});
481
483
wrap.append(add);
482
484
@@ -490,7 +492,7 @@ function renderConditionSet (when, onChange)
490
492
{
491
493
try
492
494
{
493
onChange(normalizeConditionSet(JSON.parse(area.value)));
495
onChange(normalizeConditionSet(JSON.parse(area.value)), { remount: true });
494
496
}
495
497
catch
496
498
{ /* ignore */ }
@@ -500,6 +502,89 @@ function renderConditionSet (when, onChange)
500
502
return wrap;
501
503
}
502
504
505
/** Compact searchable item picker for hasItem conditions. */
506
function itemConditionPicker (cond, onPicked)
507
{
508
const wrap = document.createElement("div");
509
wrap.className = "quest-condition-item-picker";
510
const pickBtn = document.createElement("button");
511
pickBtn.type = "button";
512
const refreshLabel = () =>
513
{
514
const def = itemDefinition(cond.itemId);
515
pickBtn.textContent = def?.displayName
516
?? (cond.itemId ? `Item #${cond.itemId}` : "Pick item…");
517
pickBtn.title = cond.itemId
518
? `${def?.key ?? "item"} · #${cond.itemId}`
519
: "Choose an item";
520
};
521
refreshLabel();
522
const searchWrap = document.createElement("div");
523
searchWrap.className = "dialogue-reward-item-search";
524
searchWrap.hidden = true;
525
const searchInput = document.createElement("input");
526
searchInput.type = "search";
527
searchInput.placeholder = "Search items…";
528
searchInput.autocomplete = "off";
529
const searchResults = document.createElement("div");
530
searchResults.className = "dialogue-reward-item-results";
531
532
const renderMatches = (query) =>
533
{
534
searchResults.replaceChildren();
535
const q = String(query ?? "").trim().toLowerCase();
536
const matches = listAllItemPickerEntries().filter((item) =>
537
{
538
if (!q)
539
{ return true; }
540
return item.displayName.toLowerCase().includes(q)
541
|| item.key.toLowerCase().includes(q)
542
|| String(item.itemId).includes(q);
543
}).slice(0, 40);
544
if (!matches.length)
545
{
546
searchResults.textContent = "No matching items.";
547
return;
548
}
549
for (const item of matches)
550
{
551
const row = document.createElement("button");
552
row.type = "button";
553
row.className = "shop-item-search-row";
554
const name = document.createElement("span");
555
name.className = "shop-item-search-row-name";
556
name.textContent = item.displayName;
557
const key = document.createElement("span");
558
key.className = "shop-item-search-row-key";
559
key.textContent = `${item.key} · #${item.itemId}`;
560
row.append(name, key);
561
row.addEventListener("click", () =>
562
{
563
cond.itemId = item.itemId;
564
refreshLabel();
565
searchWrap.hidden = true;
566
onPicked();
567
});
568
searchResults.append(row);
569
}
570
};
571
572
pickBtn.addEventListener("click", () =>
573
{
574
searchWrap.hidden = !searchWrap.hidden;
575
if (!searchWrap.hidden)
576
{
577
searchInput.value = "";
578
renderMatches("");
579
searchInput.focus();
580
}
581
});
582
searchInput.addEventListener("input", () => renderMatches(searchInput.value));
583
searchWrap.append(searchInput, searchResults);
584
wrap.append(pickBtn, searchWrap);
585
return wrap;
586
}
587
503
588
function miniField (label, value, onChange)
504
589
{
505
590
const wrap = document.createElement("label");
@@ -1,5 +1,6 @@1
import { normalizeQuestDef } from "/shared/quests/normalize.js";1
import { normalizeQuestDef } from "/shared/quests/normalize.js";2
import { ConditionKind, emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js";2
import { ConditionKind, emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js";3
import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";3
4
4
const SIMPLE_KINDS = [5
const SIMPLE_KINDS = [5
ConditionKind.hasItem,6
ConditionKind.hasItem,@@ -162,10 +163,11 @@ export function initQuestsModal ({ socket, MessageType })162
}),163
}),163
field("Text", textInput(obj.text, (v) => { obj.text = v; }))164
field("Text", textInput(obj.text, (v) => { obj.text = v; }))164
);165
);165
objBox.append(renderConditionSet(obj.when, (next) =>166
objBox.append(renderConditionSet(obj.when, (next, { remount = true } = {}) =>166
{167
{167
obj.when = next;168
obj.when = next;168
renderEditor();169
if (remount)170
{ renderEditor(); }169
}));171
}));170
box.append(objBox);172
box.append(objBox);171
});173
});@@ -300,6 +302,7 @@ function renderConditionSet (when, onChange)300
{302
{301
const wrap = document.createElement("div");303
const wrap = document.createElement("div");302
const set = normalizeConditionSet(when);304
const set = normalizeConditionSet(when);305
const bump = (remount = true) => onChange(set, { remount });303
const title = document.createElement("strong");306
const title = document.createElement("strong");304
title.textContent = "When (all of)";307
title.textContent = "When (all of)";305
wrap.append(title);308
wrap.append(title);@@ -315,7 +318,7 @@ function renderConditionSet (when, onChange)315
notBox.addEventListener("change", () =>318
notBox.addEventListener("change", () =>316
{319
{317
set.all[index].not = notBox.checked;320
set.all[index].not = notBox.checked;318
onChange(set);321
bump(false);319
});322
});320
notLabel.append(notBox, document.createTextNode(" NOT"));323
notLabel.append(notBox, document.createTextNode(" NOT"));321
row.append(notLabel);324
row.append(notLabel);@@ -341,24 +344,23 @@ function renderConditionSet (when, onChange)341
if (kind.value === "__other__")344
if (kind.value === "__other__")342
{ return; }345
{ return; }343
set.all[index].condition = defaultCondition(kind.value);346
set.all[index].condition = defaultCondition(kind.value);344
onChange(set);347
bump(true);345
});348
});346
row.append(kind);349
row.append(kind);347
350
348
const cond = clause.condition ?? { kind: ConditionKind.hasItem };351
const cond = clause.condition ?? { kind: ConditionKind.hasItem };349
if (cond.kind === ConditionKind.hasItem)352
if (cond.kind === ConditionKind.hasItem)350
{353
{351
row.append(miniField("itemId", cond.itemId ?? "", (v) =>354
row.append(itemConditionPicker(cond, () =>352
{355
{353
cond.itemId = Number(v) || 0;354
set.all[index].condition = cond;356
set.all[index].condition = cond;355
onChange(set);357
bump(false);356
}));358
}));357
row.append(miniField("qty", cond.quantity ?? 1, (v) =>359
row.append(miniField("qty", cond.quantity ?? 1, (v) =>358
{360
{359
cond.quantity = Number(v) || 1;361
cond.quantity = Number(v) || 1;360
set.all[index].condition = cond;362
set.all[index].condition = cond;361
onChange(set);363
bump(false);362
}));364
}));363
}365
}364
else if (cond.kind === ConditionKind.questState)366
else if (cond.kind === ConditionKind.questState)@@ -367,14 +369,14 @@ function renderConditionSet (when, onChange)367
{369
{368
cond.questId = v;370
cond.questId = v;369
set.all[index].condition = cond;371
set.all[index].condition = cond;370
onChange(set);372
bump(false);371
}));373
}));372
row.append(miniField("state", cond.state ?? "not-started", (v) =>374
row.append(miniField("state", cond.state ?? "not-started", (v) =>373
{375
{374
const n = Number(v);376
const n = Number(v);375
cond.state = Number.isFinite(n) && String(n) === String(v).trim() ? n : v;377
cond.state = Number.isFinite(n) && String(n) === String(v).trim() ? n : v;376
set.all[index].condition = cond;378
set.all[index].condition = cond;377
onChange(set);379
bump(false);378
}));380
}));379
}381
}380
else if (cond.kind === ConditionKind.dialogueHeard)382
else if (cond.kind === ConditionKind.dialogueHeard)@@ -383,13 +385,13 @@ function renderConditionSet (when, onChange)383
{385
{384
cond.dialogueId = v;386
cond.dialogueId = v;385
set.all[index].condition = cond;387
set.all[index].condition = cond;386
onChange(set);388
bump(false);387
}));389
}));388
row.append(miniField("stepId", cond.stepId ?? "", (v) =>390
row.append(miniField("stepId", cond.stepId ?? "", (v) =>389
{391
{390
cond.stepId = v;392
cond.stepId = v;391
set.all[index].condition = cond;393
set.all[index].condition = cond;392
onChange(set);394
bump(false);393
}));395
}));394
}396
}395
else if (cond.kind === ConditionKind.killCount)397
else if (cond.kind === ConditionKind.killCount)@@ -398,13 +400,13 @@ function renderConditionSet (when, onChange)398
{400
{399
cond.mobTypeId = v || undefined;401
cond.mobTypeId = v || undefined;400
set.all[index].condition = cond;402
set.all[index].condition = cond;401
onChange(set);403
bump(false);402
}));404
}));403
row.append(miniField("count", cond.count ?? 1, (v) =>405
row.append(miniField("count", cond.count ?? 1, (v) =>404
{406
{405
cond.count = Number(v) || 1;407
cond.count = Number(v) || 1;406
set.all[index].condition = cond;408
set.all[index].condition = cond;407
onChange(set);409
bump(false);408
}));410
}));409
}411
}410
else if (cond.kind === ConditionKind.atTile)412
else if (cond.kind === ConditionKind.atTile)@@ -413,19 +415,19 @@ function renderConditionSet (when, onChange)413
{415
{414
cond.dimensionId = String(v || "overworld");416
cond.dimensionId = String(v || "overworld");415
set.all[index].condition = cond;417
set.all[index].condition = cond;416
onChange(set);418
bump(false);417
}));419
}));418
row.append(miniField("tileX", cond.tileX ?? 0, (v) =>420
row.append(miniField("tileX", cond.tileX ?? 0, (v) =>419
{421
{420
cond.tileX = Number(v) | 0;422
cond.tileX = Number(v) | 0;421
set.all[index].condition = cond;423
set.all[index].condition = cond;422
onChange(set);424
bump(false);423
}));425
}));424
row.append(miniField("tileY", cond.tileY ?? 0, (v) =>426
row.append(miniField("tileY", cond.tileY ?? 0, (v) =>425
{427
{426
cond.tileY = Number(v) | 0;428
cond.tileY = Number(v) | 0;427
set.all[index].condition = cond;429
set.all[index].condition = cond;428
onChange(set);430
bump(false);429
}));431
}));430
}432
}431
else if (433
else if (@@ -437,7 +439,7 @@ function renderConditionSet (when, onChange)437
{439
{438
cond.dimensionId = String(v || "overworld");440
cond.dimensionId = String(v || "overworld");439
set.all[index].condition = cond;441
set.all[index].condition = cond;440
onChange(set);442
bump(false);441
}));443
}));442
}444
}443
else445
else@@ -450,7 +452,7 @@ function renderConditionSet (when, onChange)450
try452
try451
{453
{452
set.all[index].condition = JSON.parse(json.value);454
set.all[index].condition = JSON.parse(json.value);453
onChange(set);455
bump(false);454
}456
}455
catch457
catch456
{ /* keep typing */ }458
{ /* keep typing */ }@@ -464,7 +466,7 @@ function renderConditionSet (when, onChange)464
remove.addEventListener("click", () =>466
remove.addEventListener("click", () =>465
{467
{466
set.all.splice(index, 1);468
set.all.splice(index, 1);467
onChange(set);469
bump(true);468
});470
});469
row.append(remove);471
row.append(remove);470
wrap.append(row);472
wrap.append(row);@@ -476,7 +478,7 @@ function renderConditionSet (when, onChange)476
add.addEventListener("click", () =>478
add.addEventListener("click", () =>477
{479
{478
set.all.push({ not: false, condition: defaultCondition(ConditionKind.hasItem) });480
set.all.push({ not: false, condition: defaultCondition(ConditionKind.hasItem) });479
onChange(set);481
bump(true);480
});482
});481
wrap.append(add);483
wrap.append(add);482
484
@@ -490,7 +492,7 @@ function renderConditionSet (when, onChange)490
{492
{491
try493
try492
{494
{493
onChange(normalizeConditionSet(JSON.parse(area.value)));495
onChange(normalizeConditionSet(JSON.parse(area.value)), { remount: true });494
}496
}495
catch497
catch496
{ /* ignore */ }498
{ /* ignore */ }@@ -500,6 +502,89 @@ function renderConditionSet (when, onChange)500
return wrap;502
return wrap;501
}503
}502
504
505
/** Compact searchable item picker for hasItem conditions. */506
function itemConditionPicker (cond, onPicked)507
{508
const wrap = document.createElement("div");509
wrap.className = "quest-condition-item-picker";510
const pickBtn = document.createElement("button");511
pickBtn.type = "button";512
const refreshLabel = () =>513
{514
const def = itemDefinition(cond.itemId);515
pickBtn.textContent = def?.displayName516
?? (cond.itemId ? `Item #${cond.itemId}` : "Pick item…");517
pickBtn.title = cond.itemId518
? `${def?.key ?? "item"} · #${cond.itemId}`519
: "Choose an item";520
};521
refreshLabel();522
const searchWrap = document.createElement("div");523
searchWrap.className = "dialogue-reward-item-search";524
searchWrap.hidden = true;525
const searchInput = document.createElement("input");526
searchInput.type = "search";527
searchInput.placeholder = "Search items…";528
searchInput.autocomplete = "off";529
const searchResults = document.createElement("div");530
searchResults.className = "dialogue-reward-item-results";531
532
const renderMatches = (query) =>533
{534
searchResults.replaceChildren();535
const q = String(query ?? "").trim().toLowerCase();536
const matches = listAllItemPickerEntries().filter((item) =>537
{538
if (!q)539
{ return true; }540
return item.displayName.toLowerCase().includes(q)541
|| item.key.toLowerCase().includes(q)542
|| String(item.itemId).includes(q);543
}).slice(0, 40);544
if (!matches.length)545
{546
searchResults.textContent = "No matching items.";547
return;548
}549
for (const item of matches)550
{551
const row = document.createElement("button");552
row.type = "button";553
row.className = "shop-item-search-row";554
const name = document.createElement("span");555
name.className = "shop-item-search-row-name";556
name.textContent = item.displayName;557
const key = document.createElement("span");558
key.className = "shop-item-search-row-key";559
key.textContent = `${item.key} · #${item.itemId}`;560
row.append(name, key);561
row.addEventListener("click", () =>562
{563
cond.itemId = item.itemId;564
refreshLabel();565
searchWrap.hidden = true;566
onPicked();567
});568
searchResults.append(row);569
}570
};571
572
pickBtn.addEventListener("click", () =>573
{574
searchWrap.hidden = !searchWrap.hidden;575
if (!searchWrap.hidden)576
{577
searchInput.value = "";578
renderMatches("");579
searchInput.focus();580
}581
});582
searchInput.addEventListener("input", () => renderMatches(searchInput.value));583
searchWrap.append(searchInput, searchResults);584
wrap.append(pickBtn, searchWrap);585
return wrap;586
}587
503
function miniField (label, value, onChange)588
function miniField (label, value, onChange)504
{589
{505
const wrap = document.createElement("label");590
const wrap = document.createElement("label");@@ -1548,6 +1548,14 @@ label {
1548
1548
font-size: 0.85rem;
1549
1549
margin: 0.25rem 0 0.5rem;
1550
1550
}
1551
1552
.quest-condition-item-picker {
1553
display: flex;
1554
flex-direction: column;
1555
gap: 0.25rem;
1556
min-width: 12rem;
1557
}
1558
1551
1559
.dialogue-reward-item-search {
1552
1560
display: flex;
1553
1561
flex-direction: column;
@@ -1548,6 +1548,14 @@ label {1548
font-size: 0.85rem;1548
font-size: 0.85rem;1549
margin: 0.25rem 0 0.5rem;1549
margin: 0.25rem 0 0.5rem;1550
}1550
}1551
1552
.quest-condition-item-picker {1553
display: flex;1554
flex-direction: column;1555
gap: 0.25rem;1556
min-width: 12rem;1557
}1558
1551
.dialogue-reward-item-search {1559
.dialogue-reward-item-search {1552
display: flex;1560
display: flex;1553
flex-direction: column;1561
flex-direction: column;