@vectoriox/iox-ui 5.90.2 → 5.91.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -118,6 +118,8 @@ export interface StockItemSlot extends StockNeed {
118
118
  index: number;
119
119
  /** The field playing the type's `image` role. */
120
120
  field: string;
121
+ /** The item's own name, as words (its `title`): an owner's photo stands under it only when it shows that. */
122
+ subject?: string[];
121
123
  }
122
124
 
123
125
  export type StockTarget = StockSlot | StockItemSlot;
@@ -125,40 +127,102 @@ export type StockTarget = StockSlot | StockItemSlot;
125
127
  /** Where the owner's photo goes: a slot of a composed section or the image field of a sample item. */
126
128
  export type OwnPhotoPlacement = (Pick<StockSlot, 'nodeId' | 'owner'> | { item: Pick<StockItemSlot, 'contentType' | 'index' | 'field'> }) & { url: string };
127
129
 
128
- /** The owner's photos, in order, come before any stock or sample picture: sections never repeat one, and sample items go round again. */
129
- export function planOwnPhotos(slots: ReadonlyArray<StockTarget>, urls: ReadonlyArray<string>): { placements: OwnPhotoPlacement[]; rest: StockTarget[] } {
130
- if (!urls.length) return { placements: [], rest: [...slots] };
130
+ /** One of the owner's photos: its address, and what it shows in words (the owner's description or the picture review's). */
131
+ export interface OwnPhoto {
132
+ url: string;
133
+ words?: string;
134
+ }
135
+
136
+ const ownPhotoOf = (photo: string | OwnPhoto): OwnPhoto => (typeof photo === 'string' ? { url: photo } : photo);
137
+
138
+ /** A title word counts this much more than any other word the item and the photo share. */
139
+ const OWN_NAMED_POINTS = 10;
140
+ /** Without its name, this many of the item's words must be in the photo's. */
141
+ const OWN_SHARED_MIN = 2;
142
+
143
+ /**
144
+ * How well an owner's photo stands under a sample item (M8 demo: the bar's room photos stood under "Martini" and
145
+ * "Negroni"): its words name the item, or share at least two of the item's words. 0 — it does not; a photo with no words
146
+ * never does.
147
+ */
148
+ export function ownPhotoFit(photo: OwnPhoto, subject: ReadonlyArray<string>, words: ReadonlyArray<string>): number {
149
+ const has = new Set(stockWords(photo.words));
150
+ if (!has.size) return 0;
151
+ const named = subject.filter(w => has.has(w)).length;
152
+ const shared = words.filter(w => has.has(w)).length;
153
+ return named ? OWN_NAMED_POINTS * named + shared : shared >= OWN_SHARED_MIN ? shared : 0;
154
+ }
155
+
156
+ /** The owner's photos that fit an item, best first (their order breaks a tie). */
157
+ function ownPhotosFor(photos: ReadonlyArray<OwnPhoto>, subject: ReadonlyArray<string>, words: ReadonlyArray<string>): OwnPhoto[] {
158
+ return photos.map((photo, i) => ({ photo, i, fit: ownPhotoFit(photo, subject, words) }))
159
+ .filter(f => f.fit > 0)
160
+ .sort((a, b) => b.fit - a.fit || a.i - b.i)
161
+ .map(f => f.photo);
162
+ }
163
+
164
+ /**
165
+ * The owner's photos come before any stock or sample picture. A composed section's empty picture takes them in order and
166
+ * never repeats one. A sample item takes only one that shows it (`ownPhotoFit`), never twice in one type's samples; an
167
+ * item none fits keeps its sample picture or goes on to the library (`rest`) — never an unrelated photo under its name.
168
+ */
169
+ export function planOwnPhotos(slots: ReadonlyArray<StockTarget>, photos: ReadonlyArray<string | OwnPhoto>): { placements: OwnPhotoPlacement[]; rest: StockTarget[] } {
170
+ const own = photos.map(ownPhotoOf).filter(p => !!p.url);
171
+ if (!own.length) return { placements: [], rest: [...slots] };
131
172
  const placements: OwnPhotoPlacement[] = [];
132
173
  const rest: StockTarget[] = [];
174
+ const shown = new Map<string, Set<string>>();
133
175
  let next = 0;
134
176
  for (const slot of slots) {
135
177
  if (slot.kind === StockSlotKind.Section) {
136
- if (next < urls.length) placements.push({ nodeId: slot.nodeId, owner: slot.owner, url: urls[next++] });
178
+ if (next < own.length) placements.push({ nodeId: slot.nodeId, owner: slot.owner, url: own[next++].url });
137
179
  else rest.push(slot);
138
- } else {
139
- placements.push({ item: { contentType: slot.contentType, index: slot.index, field: slot.field }, url: urls[next++ % urls.length] });
180
+ continue;
181
+ }
182
+ const group = shown.get(slot.contentType) ?? new Set<string>();
183
+ const best = ownPhotosFor(own.filter(p => !group.has(p.url)), slot.subject ?? [], slot.words)[0];
184
+ if (!best) {
185
+ rest.push(slot);
186
+ continue;
140
187
  }
188
+ group.add(best.url);
189
+ shown.set(slot.contentType, group);
190
+ placements.push({ item: { contentType: slot.contentType, index: slot.index, field: slot.field }, url: best.url });
141
191
  }
142
192
  return { placements, rest };
143
193
  }
144
194
 
145
- /** Each marked sample's gallery shows the owner's photos instead of bundled samples, each list starting where the last one stopped. */
146
- export function applyOwnPhotoGalleries(plan: Pick<SitePlan, 'contentTypes'>, items: Readonly<Record<string, ReadonlyArray<Record<string, unknown>>>>, urls: ReadonlyArray<string>): number {
147
- if (!urls.length) return 0;
195
+ /**
196
+ * Each marked sample's gallery shows the owner's photos that fit the item (`ownPhotoFit`) instead of bundled samples; an
197
+ * item none fits keeps its samples.
198
+ */
199
+ export function applyOwnPhotoGalleries(plan: Pick<SitePlan, 'contentTypes'>, items: Readonly<Record<string, ReadonlyArray<Record<string, unknown>>>>, photos: ReadonlyArray<string | OwnPhoto>): number {
200
+ const own = photos.map(ownPhotoOf).filter(p => !!p.url);
201
+ if (!own.length) return 0;
148
202
  let changed = 0;
149
203
  for (const type of plan.contentTypes) {
150
204
  const field = roleField(type, ContentRole.Gallery);
151
205
  if (!field) continue;
152
- (items[type.name] ?? []).forEach((item, index) => {
206
+ (items[type.name] ?? []).forEach(item => {
153
207
  const list = item[field];
154
208
  if (!item['isSample'] || !Array.isArray(list)) return;
155
- item[field] = list.map((value, i) => (parseSampleImageRef(value) === null ? value : urls[(index + i) % urls.length]));
209
+ const { subject, words } = itemWords(type, item);
210
+ const fitting = ownPhotosFor(own, subject, words);
211
+ if (!fitting.length) return;
212
+ let next = 0;
213
+ item[field] = list.map(value => (parseSampleImageRef(value) === null ? value : fitting[next++ % fitting.length].url));
156
214
  changed++;
157
215
  });
158
216
  }
159
217
  return changed;
160
218
  }
161
219
 
220
+ /** A sample item's words: its name (`title`) and everything that says what it is (the type's label and name, its summary). */
221
+ function itemWords(type: SitePlan['contentTypes'][number], item: Record<string, unknown>): { subject: string[]; words: string[] } {
222
+ const title = item[roleField(type, ContentRole.Title) ?? ''];
223
+ return { subject: stockWords(title), words: stockWords(type.label, type.name, title, item[roleField(type, ContentRole.Summary) ?? '']) };
224
+ }
225
+
162
226
  /** Where a photo may not appear twice: one page; one content type's samples (a list shows them together). */
163
227
  export function stockGroupOf(target: StockTarget): string {
164
228
  return target.kind === StockSlotKind.Section ? `page:${target.owner}` : `items:${target.contentType}`;
@@ -216,10 +280,10 @@ export function stockItemSlots(plan: Pick<SitePlan, 'contentTypes'>, composed: P
216
280
  for (const type of plan.contentTypes) {
217
281
  const field = roleField(type, ContentRole.Image);
218
282
  if (!field) continue;
219
- const words = (item: Record<string, unknown>) => [type.label, type.name, ...[ContentRole.Title, ContentRole.Summary].map(r => item[roleField(type, r) ?? ''])];
220
283
  (composed.items[type.name] ?? []).forEach((item, index) => {
221
284
  if (!item.isSample || parseSampleImageRef(item[field]) === null) return;
222
- out.push({ kind: StockSlotKind.Item, contentType: type.name, index, field, intent: StockIntent.Product, aspect: STOCK_ITEM_ASPECT, words: stockWords(...words(item)) });
285
+ const { subject, words } = itemWords(type, item);
286
+ out.push({ kind: StockSlotKind.Item, contentType: type.name, index, field, intent: StockIntent.Product, aspect: STOCK_ITEM_ASPECT, words, subject });
223
287
  });
224
288
  }
225
289
  return out;