4747 * carries the echo itself, so compared with itself nothing differs, and
4848 * compared with the platform's `queue` its label is `队列` for `Queue`. That
4949 * is what lets the next save of such a row — and the stored-row migration,
50- * which re-saves through this door — repair it.
50+ * which re-saves through this door — repair it;
51+ * - last, for what a CODE extender brings and the stored row does not
52+ * declare at all, the packaged base: a field the packaged owner does not
53+ * declare, and a `relatedLists` entry whose `list` the owner does not name
54+ * (an extender's `place`). The read serves both from the fold over a stored
55+ * row of any age, and translates them against the packaged base; a row
56+ * saved before the extending package was installed carries no copy, so
57+ * without this source the translation would be stored as the text.
5158 *
5259 * The locales tried are every one the read could have answered in: the
5360 * bundle's, and the built-in system-field table's
6067 * the source in some locale gets the source back — which the reader in that
6168 * locale is served anyway, since the catalog translates it. Nothing else is
6269 * touched: a label that is not a translation of its source is the author's
63- * edit and is kept, and a field the source does not declare has nothing to
64- * be compared with.
70+ * edit and is kept, and a field no source declares (neither the stored row nor,
71+ * for what a code extender brings, the packaged base) has nothing to be
72+ * compared with. The packaged base is never a source for a field or entry the
73+ * stored row declares, nor for one the packaged owner declares: a stored slot
74+ * is the stored row's to explain (a diverged one is served as written), and an
75+ * owner's field or entry the stored row dropped is not served by the fold, so
76+ * a body carrying it again carries the author's text.
6577 *
6678 * The slots are the ones the read translates: the object's scalars, each
6779 * field's text slots and option labels, and the `label` of each object-form
7284 * from the packaged text) is no translation's source.
7385 *
7486 * Pure: no store, no service. The caller hands in the source row, the
75- * bundle and the packaged base .
87+ * bundle, the packaged base and the packaged owner .
7688 */
7789
7890import type { RuntimeAuthoringIssue } from '@objectstack/spec/api' ;
@@ -175,6 +187,17 @@ function relatedListEntry(entries: unknown, list: string): Rec | undefined {
175187 return Array . isArray ( entries ) ? ( entries . find ( ( e ) => isRec ( e ) && e . list === list ) as Rec | undefined ) : undefined ;
176188}
177189
190+ /** Every `list` the entries name, as a bare string or an object entry's `list`. */
191+ function relatedListNames ( entries : unknown ) : Set < string > {
192+ const out = new Set < string > ( ) ;
193+ if ( ! Array . isArray ( entries ) ) return out ;
194+ for ( const e of entries ) {
195+ if ( typeof e === 'string' ) out . add ( e ) ;
196+ else if ( isRec ( e ) && typeof e . list === 'string' ) out . add ( e . list ) ;
197+ }
198+ return out ;
199+ }
200+
178201/** That entry's `label`; {@link NOT_DECLARED} when no entry names `list` with a string label. */
179202function relatedListLabel ( entries : unknown , list : string ) : unknown {
180203 const label = relatedListEntry ( entries , list ) ?. label ;
@@ -194,8 +217,8 @@ export function translatedEchoLocales(bundle: TranslationBundle | undefined): st
194217 * only the names the incoming body declares.
195218 */
196219interface TranslatedSource {
197- /** Which source: the stored row, one patch entry's base, or the platform's columns. */
198- kind : 'stored' | 'patch' | 'injected' ;
220+ /** Which source: the stored row, one patch entry's base, the platform's columns, or what a code extender brings . */
221+ kind : 'stored' | 'patch' | 'injected' | 'extender' ;
199222 doc : Rec ;
200223 fields : Map < string , Rec > ;
201224 reads : Array < { doc : Rec ; fields : Map < string , Rec > } > ;
@@ -236,12 +259,14 @@ function translatedSource(
236259 * Put every translated slot of `item` back to its source text. Returns the
237260 * SAME reference when nothing was restored. See the module header.
238261 *
239- * Two sources, asked in order, the first that explains the value wins: the
240- * stored row's own declaration, then — for an injected column — the
241- * platform's definition. The second is what heals a row a zh-CN session
242- * already stored: its `queue` declaration IS the translation, so compared
243- * with itself nothing differs, while compared with the platform's `queue` the
244- * label is `队列` for `Queue`.
262+ * The sources, asked in order, the first that explains the value wins: the
263+ * stored row's own declaration, each patch entry's base, then — for an
264+ * injected column — the platform's definition, and last — given
265+ * `packagedOwner` — what a code extender brings that the stored row does not
266+ * declare. The injected one is what heals a row a zh-CN session already
267+ * stored: its `queue` declaration IS the translation, so compared with itself
268+ * nothing differs, while compared with the platform's `queue` the label is
269+ * `队列` for `Queue`.
245270 */
246271export function restoreTranslatedEchoes < T > (
247272 item : T ,
@@ -279,6 +304,19 @@ export function restoreTranslatedEchoes<T>(
279304 * package's translation.
280305 */
281306 patchBundles ?: PatchBundles ;
307+ /**
308+ * The packaged OWNER's own declaration (the registry's
309+ * `getPackagedObjectOwner`), the base before any code extender widened
310+ * it. With it and `packagedBase`, what the base carries beyond the
311+ * owner is the last source, for an incoming field or `relatedLists`
312+ * entry the stored row does not declare at all: a field a code
313+ * extender adds (a name the owner does not declare) and an entry a
314+ * code extender's `place` splices in (a `list` the owner does not
315+ * name). Those the read serves from the fold over a stored row that
316+ * predates the extending package, translated against the base, and
317+ * nothing else explains them. Absent, there is no such source.
318+ */
319+ packagedOwner ?: unknown ;
282320 } ,
283321) : { item : T ; restored : RestoredTranslatedSlot [ ] } {
284322 const restored : RestoredTranslatedSlot [ ] = [ ] ;
@@ -322,7 +360,27 @@ export function restoreTranslatedEchoes<T>(
322360 }
323361 return doc ;
324362 } ) ;
325- const sources = ( [ [ 'stored' , storedDoc ] , ...patchDocs . map ( ( doc ) => [ 'patch' , doc ] as const ) , [ 'injected' , injectedDoc ] ] as const )
363+ // What a code extender brings, cut to what the stored row does not
364+ // declare: the packaged base's fields the owner does not declare, and its
365+ // `relatedLists` entries whose `list` the owner does not name. A slot the
366+ // stored row declares is the stored row's to explain, so this source can
367+ // never out-explain a tenant's own text; with no stored row the source IS
368+ // the packaged base, and this cut is empty.
369+ const extenderDoc : Rec = { name : item . name , fields : { } } ;
370+ if ( isRec ( args . packagedOwner ) && isRec ( args . packagedBase ) && args . packagedBase !== args . packagedOwner ) {
371+ const ownerFields = fieldMap ( args . packagedOwner ) ;
372+ for ( const name of incoming . keys ( ) ) {
373+ const def = packagedFields . get ( name ) ;
374+ if ( def && ! ownerFields . has ( name ) && ! storedFields . has ( name ) ) ( extenderDoc . fields as Rec ) [ name ] = { ...def } ;
375+ }
376+ const ownerLists = relatedListNames ( args . packagedOwner . relatedLists ) ;
377+ const storedLists = relatedListNames ( isRec ( args . source ) ? args . source . relatedLists : undefined ) ;
378+ const placed = Array . isArray ( args . packagedBase . relatedLists )
379+ ? args . packagedBase . relatedLists . filter ( ( e ) => isRec ( e ) && typeof e . list === 'string' && ! ownerLists . has ( e . list ) && ! storedLists . has ( e . list ) )
380+ : [ ] ;
381+ if ( placed . length > 0 ) extenderDoc . relatedLists = placed ;
382+ }
383+ const sources = ( [ [ 'stored' , storedDoc ] , ...patchDocs . map ( ( doc ) => [ 'patch' , doc ] as const ) , [ 'injected' , injectedDoc ] , [ 'extender' , extenderDoc ] ] as const )
326384 . filter ( ( [ , doc ] ) => Object . keys ( doc . fields as Rec ) . length > 0 || Object . keys ( doc ) . length > 2 )
327385 . map ( ( [ kind , doc ] ) => translatedSource ( kind , doc , locales , args . bundle , args . packagedBase , patchBundles ) ) ;
328386 if ( sources . length === 0 ) return { item, restored } ;
0 commit comments