@@ -12,6 +12,7 @@ import {
1212} from "goals/MergeDuplicates/MergeDupsTreeTypes" ;
1313import { newMergeWords } from "goals/MergeDuplicates/MergeDupsTypes" ;
1414import { type Hash } from "types/hash" ;
15+ import { newGrammaticalInfo , newSense } from "types/word" ;
1516import { compareFlags } from "utilities/wordUtilities" ;
1617
1718// A collection of helper/utility functions only for use in the MergeDupsReducer.
@@ -112,15 +113,42 @@ export function createMergeParent(
112113export function combineIntoFirstSense ( mergeSenses : MergeTreeSense [ ] ) : void {
113114 // Set the main sense to the first sense (the top one when the sidebar was opened).
114115 const mainSense = mergeSenses [ 0 ] . sense ;
115- const sep = "; " ;
116+ const senses : Sense [ ] = [ mainSense ] ;
116117
117- // Merge the rest as duplicates.
118+ // Mark the rest as duplicates.
118119 // These were senses dropped into another sense.
119120 mergeSenses . slice ( 1 ) . forEach ( ( mergeDupSense ) => {
120- const dupSense = mergeDupSense . sense ;
121- dupSense . accessibility = Status . Duplicate ;
121+ mergeDupSense . sense . accessibility = Status . Duplicate ;
122+ senses . push ( mergeDupSense . sense ) ;
123+ } ) ;
124+
125+ // Combine the sense content and update the main sense.
126+ const combinedSense = combineSenses ( senses ) ;
127+ mainSense . definitions = combinedSense . definitions ;
128+ mainSense . glosses = combinedSense . glosses ;
129+ mainSense . grammaticalInfo = combinedSense . grammaticalInfo ;
130+ mainSense . semanticDomains = combinedSense . semanticDomains ;
131+ }
132+
133+ /** Create a copy of the first sense with the following content merged from all senses:
134+ * definitions, glosses, grammaticalInfo, semanticDomains. */
135+ export function combineSenses ( senses : Sense [ ] ) : Sense {
136+ if ( ! senses . length ) {
137+ return newSense ( ) ;
138+ }
122139
123- // Merge the duplicate's definitions into the main sense.
140+ const mainSense : Sense = {
141+ ...senses [ 0 ] ,
142+ definitions : [ ] ,
143+ glosses : [ ] ,
144+ grammaticalInfo : newGrammaticalInfo ( ) ,
145+ semanticDomains : [ ] ,
146+ } ;
147+
148+ const sep = "; " ;
149+
150+ senses . forEach ( ( dupSense ) => {
151+ // Merge in the definitions.
124152 dupSense . definitions . forEach ( ( def ) => {
125153 const newText = def . text . trim ( ) ;
126154 if ( newText ) {
@@ -143,7 +171,7 @@ export function combineIntoFirstSense(mergeSenses: MergeTreeSense[]): void {
143171 }
144172 } ) ;
145173
146- // Merge the duplicate's glosses into the main sense .
174+ // Merge in the glosses .
147175 dupSense . glosses . forEach ( ( gloss ) => {
148176 const newDef = gloss . def . trim ( ) ;
149177 if ( newDef ) {
@@ -166,7 +194,7 @@ export function combineIntoFirstSense(mergeSenses: MergeTreeSense[]): void {
166194 }
167195 } ) ;
168196
169- // Use the duplicate's part of speech if not specified in the main sense .
197+ // Use the grammatical info if not already specified .
170198 if ( mainSense . grammaticalInfo . catGroup === GramCatGroup . Unspecified ) {
171199 mainSense . grammaticalInfo = { ...dupSense . grammaticalInfo } ;
172200 } else if (
@@ -180,11 +208,13 @@ export function combineIntoFirstSense(mergeSenses: MergeTreeSense[]): void {
180208 }
181209 }
182210
183- // Put the duplicate's domains in the main sense if the id is new .
211+ // Merge in the semantic domains .
184212 dupSense . semanticDomains . forEach ( ( dom ) => {
185213 if ( mainSense . semanticDomains . every ( ( d ) => d . id !== dom . id ) ) {
186214 mainSense . semanticDomains . push ( { ...dom } ) ;
187215 }
188216 } ) ;
189217 } ) ;
218+
219+ return mainSense ;
190220}
0 commit comments