Skip to content

Commit 5a399bb

Browse files
authored
refactor(thumbnail): sort blocks by their names (#110)
1 parent 378925d commit 5a399bb

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/utils/thumbnail.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,30 @@ const readFile = async (path: string): Promise<string | undefined> => {
229229
catch { }
230230
};
231231

232+
let numericCollator: Intl.Collator | null = null;
233+
234+
try {
235+
numericCollator = new Intl.Collator(undefined, { numeric: true });
236+
}
237+
catch {
238+
// Ignore.
239+
}
240+
241+
function compareFileName(name1: string, name2: string, order?: string[]): number {
242+
if (order) {
243+
if (order.includes(name1) && order.includes(name2)) {
244+
return order.indexOf(name1) - order.indexOf(name2);
245+
}
246+
if (order.includes(name1)) {
247+
return -1;
248+
}
249+
if (order.includes(name2)) {
250+
return 1;
251+
}
252+
}
253+
return numericCollator?.compare(name1, name2) || name1.localeCompare(name2);
254+
}
255+
232256
const manifestFileCache = new Map<string, Record<string, any> | null>();
233257

234258
export class Thumbnail implements ThumbnailProvider {
@@ -294,6 +318,7 @@ export class Thumbnail implements ThumbnailProvider {
294318
});
295319
}
296320
}
321+
result.blocks?.sort((a, b) => compareFileName(a.name, b.name, this.wsPkgData.blocksOrder));
297322
return result;
298323
}
299324

@@ -328,6 +353,7 @@ export class Thumbnail implements ThumbnailProvider {
328353
}
329354
}
330355
}
356+
result.sort((a, b) => compareFileName(a.name, b.name));
331357
return result;
332358
}
333359

@@ -540,6 +566,7 @@ class PkgData {
540566
public readonly title: string | undefined;
541567
public readonly description: string | undefined;
542568
public readonly icon: string | undefined;
569+
public readonly blocksOrder: string[] | undefined;
543570

544571
private readonly dependencies: Record<string, string>;
545572
private readonly sharedBlockCache: Map<string, SharedBlockData | null | Promise<SharedBlockData | undefined>> = new Map();
@@ -557,6 +584,7 @@ class PkgData {
557584
this.icon = this.resolveResourceURI(data.icon, packageDir);
558585
this.title = data.title || data.name;
559586
this.description = data.description;
587+
this.blocksOrder = data.ui?.blocks;
560588
this.dependencies = isPlainObject(data.dependencies) ? data.dependencies : {};
561589
}
562590

0 commit comments

Comments
 (0)