|
1 | | -import naturalCompare from "natural-compare-lite"; |
2 | 1 | import * as vscode from "vscode"; |
3 | 2 |
|
4 | 3 | import { AppContext } from "../context/app"; |
5 | 4 | import { ContentError } from "../schemas/error"; |
6 | 5 | import { Contents, ContentsType } from "../types"; |
| 6 | +import { EMOJI_REGEX } from "../utils/patterns"; |
7 | 7 |
|
8 | 8 | /** TreeItem の `contextValue` に設定できる値 */ |
9 | 9 | export type TreeItemType = ContentsType | "error" | "none"; |
@@ -64,6 +64,43 @@ export abstract class PreviewTreeItem extends vscode.TreeItem { |
64 | 64 | * TreeItemをソートする |
65 | 65 | */ |
66 | 66 | static sortTreeItems(items: PreviewTreeItem[]): PreviewTreeItem[] { |
67 | | - return items.sort((a, b) => naturalCompare(a.path, b.path)); |
| 67 | + // 設定からソート順を取得 |
| 68 | + const sortArticle = vscode.workspace |
| 69 | + .getConfiguration("zenn-preview") |
| 70 | + .get<string | null>("sortArticle"); |
| 71 | + |
| 72 | + return items.sort((a, b) => { |
| 73 | + // 記事タイトルでソート |
| 74 | + if (sortArticle === "title") { |
| 75 | + const getCleanLabel = (item: PreviewTreeItem): string => { |
| 76 | + const labelProp = item.label; |
| 77 | + let labelStr = ""; |
| 78 | + if (typeof labelProp === "string") { |
| 79 | + labelStr = labelProp; |
| 80 | + } else if (labelProp && typeof labelProp.label === "string") { |
| 81 | + labelStr = labelProp.label; |
| 82 | + } |
| 83 | + return labelStr.replace(EMOJI_REGEX, "").trim(); |
| 84 | + }; |
| 85 | + |
| 86 | + const aCleanLabel = getCleanLabel(a); |
| 87 | + const bCleanLabel = getCleanLabel(b); |
| 88 | + |
| 89 | + if (aCleanLabel || bCleanLabel) { |
| 90 | + // どちらかに絵文字除去後ラベルがあれば比較 |
| 91 | + return aCleanLabel.localeCompare(bCleanLabel, "ja", { |
| 92 | + sensitivity: "base", |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + // 絵文字除去後ラベルが両方空の場合はpathで比較 (元々ラベルがなかった場合など) |
| 97 | + return a.path.localeCompare(b.path, "ja", { sensitivity: "base" }); |
| 98 | + } |
| 99 | + |
| 100 | + // ファイルパスでソート |
| 101 | + else { |
| 102 | + return a.path.localeCompare(b.path, "ja", { sensitivity: "base" }); |
| 103 | + } |
| 104 | + }); |
68 | 105 | } |
69 | 106 | } |
0 commit comments