pluginStoreOrdering.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { ZCODE_OFFICIAL_PLUGIN_MARKETPLACE_ID } from "./plugin-marketplaces.js";
  2. import type { PluginStoreModeOrder } from "./pluginStoreOrder.js";
  3. export const FALLBACK_PLUGIN_STORE_CATEGORY = "other";
  4. export const PLUGIN_STORE_CATEGORY_ORDER: readonly string[] = [
  5. "productivity",
  6. "developer-tools",
  7. "utilities",
  8. "finance",
  9. "legal",
  10. "template",
  11. ];
  12. // 完整 ID 避免个人市场的同名插件被误置顶;所有展示入口复用同一默认顺序。
  13. const DOCUMENT_PLUGIN_RANKS = new Map(
  14. ["pdf", "presentations", "spreadsheets", "documents"].map((name, index) => [
  15. `${name}@${ZCODE_OFFICIAL_PLUGIN_MARKETPLACE_ID}`,
  16. index,
  17. ]),
  18. );
  19. export function compareDocumentPluginPriority(leftId: string, rightId: string): number {
  20. return compareRanks(DOCUMENT_PLUGIN_RANKS, leftId, rightId);
  21. }
  22. /** 分类归并只影响展示,市场与引用 Picker 必须使用同一个排序键。 */
  23. export function resolvePluginStoreCategory(category: string | undefined): string | undefined {
  24. const normalized = category?.trim();
  25. return normalized === "guides" ? "utilities" : normalized || undefined;
  26. }
  27. interface PluginStoreSortEntry {
  28. id: string;
  29. category?: string;
  30. displayName: string;
  31. }
  32. /** 纯展示排序:配置优先,剩余分类按产品默认顺序,类内文档插件优先,再按本地化名称稳定兜底。 */
  33. export function sortPluginStoreEntries<T>(
  34. items: readonly T[],
  35. project: (item: T) => PluginStoreSortEntry,
  36. locale: string,
  37. order?: PluginStoreModeOrder,
  38. ): T[] {
  39. const categoryRanks = ranks(order?.categoryOrder);
  40. const pluginRanks = new Map(
  41. Object.entries(order?.pluginOrder ?? {}).map(([category, ids]) => [category, ranks(ids)]),
  42. );
  43. return items
  44. .map((item, index) => {
  45. const entry = project(item);
  46. return {
  47. item,
  48. index,
  49. ...entry,
  50. category: resolvePluginStoreCategory(entry.category) ?? FALLBACK_PLUGIN_STORE_CATEGORY,
  51. };
  52. })
  53. .sort(
  54. (left, right) =>
  55. compareRanks(categoryRanks, left.category, right.category) ||
  56. compareCategories(left.category, right.category) ||
  57. compareRanks(pluginRanks.get(left.category), left.id, right.id) ||
  58. compareDocumentPluginPriority(left.id, right.id) ||
  59. left.displayName.localeCompare(right.displayName, locale) ||
  60. left.index - right.index,
  61. )
  62. .map(({ item }) => item);
  63. }
  64. function ranks(order: readonly string[] = []): Map<string, number> {
  65. const result = new Map<string, number>();
  66. for (const key of order) if (!result.has(key)) result.set(key, result.size);
  67. return result;
  68. }
  69. function compareRanks(order: Map<string, number> | undefined, left: string, right: string): number {
  70. if (!order) return 0;
  71. return (order.get(left) ?? order.size) - (order.get(right) ?? order.size);
  72. }
  73. function compareCategories(left: string, right: string): number {
  74. if (left === right) return 0;
  75. if (left === FALLBACK_PLUGIN_STORE_CATEGORY) return 1;
  76. if (right === FALLBACK_PLUGIN_STORE_CATEGORY) return -1;
  77. const a = PLUGIN_STORE_CATEGORY_ORDER.indexOf(left);
  78. const b = PLUGIN_STORE_CATEGORY_ORDER.indexOf(right);
  79. if (a !== -1 && b !== -1) return a - b;
  80. if (a !== -1) return -1;
  81. if (b !== -1) return 1;
  82. return left < right ? -1 : 1;
  83. }