@@ -10,6 +10,7 @@ import {
1010 type IIndexQueryOptions ,
1111 type IMiniLcmFeatures ,
1212 type IMiniLcmJsInvokable ,
13+ type IMorphType ,
1314 type IPartOfSpeech ,
1415 type IProjectModel ,
1516 type IPublication ,
@@ -22,8 +23,9 @@ import {
2223 type WritingSystemType ,
2324 type ICustomView ,
2425 ViewBase ,
26+ MorphTypeKind ,
2527} from '$lib/dotnet-types' ;
26- import { entries , partsOfSpeech , projectName , writingSystems } from './demo-entry-data' ;
28+ import { entries , morphTypes , partsOfSpeech , projectName , writingSystems } from './demo-entry-data' ;
2729
2830import { WritingSystemService } from '../data/writing-system-service.svelte' ;
2931import { FwLitePlatform } from '$lib/dotnet-types/generated-types/FwLiteShared/FwLitePlatform' ;
@@ -41,6 +43,7 @@ import {type IAvailableUpdate, UpdateResult} from '$lib/dotnet-types/generated-t
4143import { type EventBus , useEventBus , ProjectEventBus } from '$lib/services/event-bus' ;
4244import type { IJsEventListener } from '$lib/dotnet-types/generated-types/FwLiteShared/Events/IJsEventListener' ;
4345import { initProjectStorage } from '$lib/storage' ;
46+ import { MorphTypesService } from '$project/data/morph-types.svelte' ;
4447
4548function pickWs ( ws : string , defaultWs : string ) : string {
4649 return ws === 'default' ? defaultWs : ws ;
@@ -50,17 +53,6 @@ const complexFormTypes = entries
5053 . flatMap ( entry => entry . complexFormTypes )
5154 . filter ( ( value , index , all ) => all . findIndex ( v2 => v2 . id === value . id ) === index ) ;
5255
53- function filterEntries ( entries : IEntry [ ] , query : string ) : IEntry [ ] {
54- return entries . filter ( entry =>
55- [
56- ...Object . values ( entry . lexemeForm ?? { } ) ,
57- ...Object . values ( entry . citationForm ?? { } ) ,
58- ...entry . senses . flatMap ( sense => [
59- ...Object . values ( sense . gloss ?? { } ) ,
60- ] ) ,
61- ] . some ( value => value ?. toLowerCase ( ) . includes ( query . toLowerCase ( ) ) ) ) ;
62- }
63-
6456export const mockFwLiteConfig : IFwLiteConfig = {
6557 appVersion : 'dev' ,
6658 feedbackUrl : '' ,
@@ -94,10 +86,12 @@ const mockJsEventListener: IJsEventListener = {
9486} ;
9587
9688export class InMemoryDemoApi implements IMiniLcmJsInvokable {
89+ #morphTypesService: MorphTypesService ;
9790 #writingSystemService: WritingSystemService ;
9891 #projectEventBus: ProjectEventBus ;
9992 constructor ( projectContext : ProjectContext , eventBus : EventBus ) {
100- this . #writingSystemService = new WritingSystemService ( projectContext ) ;
93+ this . #morphTypesService = new MorphTypesService ( projectContext ) ;
94+ this . #writingSystemService = new WritingSystemService ( projectContext , this . #morphTypesService) ;
10195 this . #projectEventBus = new ProjectEventBus ( projectContext , eventBus ) ;
10296 }
10397
@@ -159,6 +153,17 @@ export class InMemoryDemoApi implements IMiniLcmJsInvokable {
159153 ) ;
160154 }
161155
156+ getMorphTypes ( ) : Promise < IMorphType [ ] > {
157+ return Promise . resolve (
158+ morphTypes
159+ // [
160+ // {id: 'd7f713e8-e8cf-11d3-9764-00c04f186933', kind: MorphTypeKind.Stem},
161+ // {id: 'd7f713db-e8cf-11d3-9764-00c04f186933', kind: MorphTypeKind.Prefix, postfix='-'},
162+ // {id: 'd7f713dd-e8cf-11d3-9764-00c04f186933', kind: MorphTypeKind.Suffix, prefix='-'},
163+ // ]
164+ ) ;
165+ }
166+
162167 getPartsOfSpeech ( ) : Promise < IPartOfSpeech [ ] > {
163168 return Promise . resolve (
164169 partsOfSpeech
@@ -250,27 +255,44 @@ export class InMemoryDemoApi implements IMiniLcmJsInvokable {
250255 return entries . slice ( options . offset , options . offset + options . count ) ;
251256 }
252257
258+ private filterEntries ( entries : IEntry [ ] , query : string ) : IEntry [ ] {
259+ return entries . filter ( entry =>
260+ [
261+ ...this . #writingSystemService. vernacular . map ( ws => this . #writingSystemService. headword ( entry , ws . wsId ) ) ,
262+ ...Object . values ( entry . lexemeForm ?? { } ) ,
263+ ...entry . senses . flatMap ( sense => [
264+ ...Object . values ( sense . gloss ?? { } ) ,
265+ ] ) ,
266+ ] . some ( value => value ?. toLowerCase ( ) . includes ( query . toLowerCase ( ) ) ) ) ;
267+ }
268+
253269 private getFilteredSortedEntries ( query ?: string , options ?: Omit < IQueryOptions , 'count' | 'offset' > ) : IEntry [ ] {
254270 const entries = this . getFilteredEntries ( query , options ) ;
255-
256- if ( ! options ) return entries ;
257271 const defaultWs = writingSystems . vernacular [ 0 ] . wsId ;
258- const sortWs = pickWs ( options . order . writingSystem , defaultWs ) ;
272+ const sortWs = pickWs ( options ?. order ?. writingSystem ?? defaultWs , defaultWs ) ;
273+ const ascending = options ?. order ?. ascending ?? true ;
274+ const stem = morphTypes . find ( m => m . kind === MorphTypeKind . Stem ) ! ;
259275 return entries
260276 . sort ( ( e1 , e2 ) => {
261- const v1 = this . #writingSystemService. headword ( e1 , sortWs ) ;
262- const v2 = this . #writingSystemService. headword ( e2 , sortWs ) ;
277+ // morph-tokens should not be included when sorting
278+ const v1 = e1 . citationForm [ sortWs ] || e1 . lexemeForm [ sortWs ] ;
279+ const v2 = e2 . citationForm [ sortWs ] || e2 . lexemeForm [ sortWs ] ;
263280 if ( ! v2 ) return - 1 ;
264281 if ( ! v1 ) return 1 ;
265282 let compare = v1 . localeCompare ( v2 , sortWs ) ;
266- if ( compare == 0 ) compare = e1 . id . localeCompare ( e2 . id ) ;
267- return options . order . ascending ? compare : - compare ;
283+ if ( compare === 0 ) {
284+ const m1 = ( morphTypes . find ( m => m . kind === e1 . morphType ) ?? stem ) ;
285+ const m2 = ( morphTypes . find ( m => m . kind === e2 . morphType ) ?? stem ) ;
286+ compare = m1 . secondaryOrder - m2 . secondaryOrder ;
287+ }
288+ if ( compare === 0 ) compare = e1 . id . localeCompare ( e2 . id ) ;
289+ return ascending ? compare : - compare ;
268290 } ) ;
269291 }
270292
271293 private getFilteredEntries ( query ?: string , options ?: IFilterQueryOptions ) : IEntry [ ] {
272294 let entries = this . _Entries ( ) ;
273- if ( query ) entries = filterEntries ( entries , query ) ;
295+ if ( query ) entries = this . filterEntries ( entries , query ) ;
274296 if ( ! options ) return entries ;
275297
276298 const defaultWs = writingSystems . vernacular [ 0 ] . wsId ;
0 commit comments