@aiquants/spreadsheet 0.20.1 → 0.20.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +149 -0
- package/README.md +153 -10
- package/dist/index.cjs +8 -8
- package/dist/index.d.cts +301 -25
- package/dist/index.d.ts +301 -25
- package/dist/index.js +6320 -5670
- package/dist/styles/spreadsheet.css +1 -1
- package/dist/styles/spreadsheet.standalone.css +1 -1
- package/package.json +2 -2
- package/src/Spreadsheet.tsx +379 -39
- package/src/commands/useCommands.ts +27 -0
- package/src/editor/useCellEditor.ts +38 -6
- package/src/grid/GridBody.tsx +61 -3
- package/src/index.ts +23 -1
- package/src/keyboard/useGridKeyboard.ts +101 -3
- package/src/model/labels.ts +100 -10
- package/src/popover/PopoverLayer.tsx +66 -4
- package/src/popover/usePopovers.ts +87 -9
- package/src/selection/selectionState.ts +70 -0
- package/src/selection/useSelectionEngine.ts +141 -2
- package/src/sortFilter/ColumnMenu.tsx +380 -0
- package/src/sortFilter/HeaderControls.tsx +128 -0
- package/src/sortFilter/sortFilterModel.ts +648 -0
- package/src/styles/spreadsheet.css +148 -2
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview The model of header sorting and per-column filtering (`sortFilter`): the seven public types, the
|
|
3
|
+
* render-time validation `resolveSortFilter` and the pure state transitions the header press and the column menu use.
|
|
4
|
+
* The package never orders or drops rows itself — the host owns the state (a controlled prop), matches the rows and
|
|
5
|
+
* reorders its adapter; this module only decides which columns carry the controls, which state is well formed and
|
|
6
|
+
* what the next state is. Every function is pure and React-free.
|
|
7
|
+
*
|
|
8
|
+
* Contract:
|
|
9
|
+
* - `resolveSortFilter(sortFilter, colCount, headerRows)` reads every column key and option once and throws a
|
|
10
|
+
* `RangeError` (message prefix `[spreadsheet] sortFilter`) on a malformed prop, options or state: no header row to
|
|
11
|
+
* hold the controls, an unknown or mistyped member, empty / unknown / repeated match modes, a default mode the
|
|
12
|
+
* column cannot use, an empty or repeated column key, exact matching without `getFilterValues`, and a state naming
|
|
13
|
+
* an unknown column, sorting a column that cannot sort, filtering a column that cannot filter, or using a mode the
|
|
14
|
+
* column does not offer.
|
|
15
|
+
* - The transitions (`cycleSort`, `withSort`, `withExactFilter`, `withFuzzyFilter`, `withoutFilter`,
|
|
16
|
+
* `withoutFilters`) return a new state and never mutate their input; `withExactFilter` lifts the filter when every
|
|
17
|
+
* listed value is checked (it then keeps the same rows) and `withFuzzyFilter` lifts it for a blank query.
|
|
18
|
+
* - `sameSortFilterState` compares two states by shape, so an action that would hand the host the state it already
|
|
19
|
+
* has is dropped.
|
|
20
|
+
* - `validateFilterValues` checks the host's value list (strings, a non-negative integer count, no repeated value)
|
|
21
|
+
* and `filterValueMatches` is the value search of the column menu (NFKC, lower case, substring).
|
|
22
|
+
*
|
|
23
|
+
* 見出しの並べ替えと列ごとの絞り込み (`sortFilter`) のモデル。公開の型 7 つ、描く時の検め `resolveSortFilter`、見出しの押下と列の
|
|
24
|
+
* 品書きが使う状態の純関数。部品は行を並べ替えも落としもしない — 状態は利用側が持ち (制御された props)、行の照合とアダプタの並べ直しも
|
|
25
|
+
* 利用側。このモジュールは、操作を持つ列・状態の形の正しさ・次の状態だけを決める。全て純関数で React に依らない。
|
|
26
|
+
* 契約: `resolveSortFilter` は全ての列の鍵と指定を 1 回ずつ読み、誤った props・指定・状態を `RangeError` (文の頭は
|
|
27
|
+
* `[spreadsheet] sortFilter`) で止める (操作を置く見出しの行が無い・知らない項目や型の誤り・空や知らない名や重なる検索方法・使えない
|
|
28
|
+
* 初めの方法・空や重なる列の鍵・`getFilterValues` 無しの完全一致・知らない列や並べ替えできない列や絞り込めない列や使えない方法を名指す
|
|
29
|
+
* 状態)。状態の関数は新しい状態を返し、入力を変えない。`withExactFilter` は一覧の全ての値に印があれば絞り込みを外し (残る行が同じ)、
|
|
30
|
+
* `withFuzzyFilter` は空白だけの語で外す。`sameSortFilterState` は形で比べ、利用側が既に持つ状態を知らせない。
|
|
31
|
+
* `validateFilterValues` は利用側の値の一覧 (文字列・0 以上の整数の件数・値の重なり無し) を検め、`filterValueMatches` は品書きの値の
|
|
32
|
+
* 探す欄の照合 (NFKC・小文字・部分一致)。
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/** A match mode of a column filter: exact values or a fuzzy query (the host does the matching). / 列の絞り込みの検索方法。完全一致か、あいまい検索 (照合は利用側)。 */
|
|
36
|
+
export type SpreadsheetMatchMode = "exact" | "fuzzy"
|
|
37
|
+
|
|
38
|
+
/** A sort direction. / 並べ替えの向き。 */
|
|
39
|
+
export type SpreadsheetSortDirection = "asc" | "desc"
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* One column's filter: the checked values of an exact match (the rows whose value is one of them stay), or the query of a
|
|
43
|
+
* fuzzy search.
|
|
44
|
+
* 1 つの列の絞り込み。完全一致で印を付けた値 (どれかと同じ値の行が残る)、またはあいまい検索の語。
|
|
45
|
+
*/
|
|
46
|
+
export type SpreadsheetColumnFilter = { readonly mode: "exact"; readonly values: readonly string[] } | { readonly mode: "fuzzy"; readonly query: string }
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The sort and filter state the host owns: at most one sorted column and one filter per column key (every filter must hold
|
|
50
|
+
* for a row to stay).
|
|
51
|
+
* 利用側が持つ並べ替えと絞り込みの状態。並べ替える列は 1 つまで、絞り込みは列の鍵ごとに 1 つ (行が残るには全ての絞り込みに当たる)。
|
|
52
|
+
*/
|
|
53
|
+
export type SpreadsheetSortFilterState = {
|
|
54
|
+
readonly sort: { readonly columnKey: string; readonly direction: SpreadsheetSortDirection } | null
|
|
55
|
+
readonly filters: Readonly<Record<string, SpreadsheetColumnFilter>>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* What a column offers: whether it sorts, whether it filters, the match modes it offers (at least one) and the mode its menu
|
|
60
|
+
* starts with (one of them).
|
|
61
|
+
* 列が持つ操作。並べ替えできるか・絞り込めるか・使える検索方法 (1 つ以上)・品書きの初めの方法 (使える方法のどれか)。
|
|
62
|
+
*/
|
|
63
|
+
export type SpreadsheetColumnSortFilterOptions = {
|
|
64
|
+
readonly sortable: boolean
|
|
65
|
+
readonly filterable: boolean
|
|
66
|
+
readonly matchModes: readonly SpreadsheetMatchMode[]
|
|
67
|
+
readonly defaultMatchMode: SpreadsheetMatchMode
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* One row of an exact-match value list: the value the filter stores, the text the menu shows (an empty label shows the
|
|
72
|
+
* `filterBlank` label) and the number of rows holding it.
|
|
73
|
+
* 完全一致の値の一覧の 1 行。絞り込みが持つ値、品書きが見せる字 (空の字は言葉 `filterBlank` で出す)、その値を持つ行の数。
|
|
74
|
+
*/
|
|
75
|
+
export type SpreadsheetFilterValue = { readonly value: string; readonly label: string; readonly count: number }
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The `sortFilter` prop. The package reads every member at render and validates the whole object again whenever its
|
|
79
|
+
* identity changes, so a host passes a memoized object.
|
|
80
|
+
* - `state` / `onStateChange`: the controlled state; `onStateChange` is called once per accepted action, after the
|
|
81
|
+
* in-place edit has been committed, and never with a state equal in shape to `state`.
|
|
82
|
+
* - `getColumnKey(col)`: the host's key of a column, or `null` for a column without controls; keys are unique.
|
|
83
|
+
* - `defaults` / `getColumnOptions(key)`: the options of every column, and the members one column overrides.
|
|
84
|
+
* - `getFilterValues(key, state, signal)`: the value list of an exact match, counted over the rows the OTHER columns'
|
|
85
|
+
* filters keep; required when a column offers the exact mode; `signal` aborts when the menu closes.
|
|
86
|
+
* props `sortFilter`。部品は描く時に全ての項目を読み、物が替わるたびに全体を検め直すので、利用側は memo した物を渡す。
|
|
87
|
+
* `state` / `onStateChange` は制御された状態 (`onStateChange` は受けた操作ごとに 1 回、入力を確定した後に呼び、`state` と同じ形の状態では
|
|
88
|
+
* 呼ばない)。`getColumnKey(col)` は列の利用側の鍵 (操作を持たない列は `null`。鍵は重ならない)。`defaults` / `getColumnOptions(key)` は
|
|
89
|
+
* 全ての列の指定と、列ごとに差し替える項目。`getFilterValues(key, state, signal)` は完全一致の値の一覧 (ほかの列の絞り込みが残す行から
|
|
90
|
+
* 数える。完全一致を使える列があれば必須。品書きを閉じると `signal` が止まる)。
|
|
91
|
+
*/
|
|
92
|
+
export type SpreadsheetSortFilter = {
|
|
93
|
+
readonly state: SpreadsheetSortFilterState
|
|
94
|
+
readonly onStateChange: (next: SpreadsheetSortFilterState) => void
|
|
95
|
+
readonly getColumnKey: (col: number) => string | null
|
|
96
|
+
readonly defaults: SpreadsheetColumnSortFilterOptions
|
|
97
|
+
readonly getColumnOptions?: (columnKey: string) => Partial<SpreadsheetColumnSortFilterOptions>
|
|
98
|
+
readonly getFilterValues?: (columnKey: string, state: SpreadsheetSortFilterState, signal: AbortSignal) => Promise<readonly SpreadsheetFilterValue[]>
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The resolved options of one keyed column (the defaults with the column's overrides laid on top).
|
|
103
|
+
* 鍵を持つ列 1 つの解いた指定 (全体の指定に列の差し替えを重ねた物)。
|
|
104
|
+
*/
|
|
105
|
+
export type ResolvedSortFilterColumn = {
|
|
106
|
+
readonly key: string
|
|
107
|
+
readonly sortable: boolean
|
|
108
|
+
readonly filterable: boolean
|
|
109
|
+
readonly matchModes: readonly SpreadsheetMatchMode[]
|
|
110
|
+
readonly defaultMatchMode: SpreadsheetMatchMode
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The validated `sortFilter`: one entry per column (`null` = no key), the column of each key, the header row holding the
|
|
115
|
+
* controls, the sorted column and the filtered columns of the state.
|
|
116
|
+
* 検めた `sortFilter`。列ごとの項目 (`null` = 鍵なし)、鍵から列への表、操作を置く見出しの行、状態の並べ替えている列と絞り込んでいる列。
|
|
117
|
+
*/
|
|
118
|
+
export type ResolvedSortFilter = {
|
|
119
|
+
readonly columns: readonly (ResolvedSortFilterColumn | null)[]
|
|
120
|
+
readonly columnOfKey: ReadonlyMap<string, number>
|
|
121
|
+
readonly headerRow: number
|
|
122
|
+
readonly sort: { readonly col: number; readonly direction: SpreadsheetSortDirection } | null
|
|
123
|
+
readonly filteredColumns: ReadonlySet<number>
|
|
124
|
+
/** True when at least one column sorts or filters (the popover layer is then mounted). / 並べ替えできるか絞り込める列が 1 つでもあれば true (層を描く)。 */
|
|
125
|
+
readonly hasControls: boolean
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The members of the `sortFilter` prop, each once (typed as a record of every member, so a new member must be listed). / props `sortFilter` の項目 (1 つずつ。全ての項目の記録として型付けるので、足した項目はここへ並べるまで型検査が落ちる)。 */
|
|
129
|
+
const SORT_FILTER_MEMBERS: Readonly<Record<keyof SpreadsheetSortFilter, true>> = Object.freeze({ state: true, onStateChange: true, getColumnKey: true, defaults: true, getColumnOptions: true, getFilterValues: true })
|
|
130
|
+
|
|
131
|
+
/** The members of the column options, each once. / 列の指定の項目 (1 つずつ)。 */
|
|
132
|
+
const OPTION_MEMBERS: Readonly<Record<keyof SpreadsheetColumnSortFilterOptions, true>> = Object.freeze({ sortable: true, filterable: true, matchModes: true, defaultMatchMode: true })
|
|
133
|
+
|
|
134
|
+
/** The members of the state, each once. / 状態の項目 (1 つずつ)。 */
|
|
135
|
+
const STATE_MEMBERS: Readonly<Record<keyof SpreadsheetSortFilterState, true>> = Object.freeze({ sort: true, filters: true })
|
|
136
|
+
|
|
137
|
+
/** The members of the state's sort, each once. / 状態の並べ替えの項目 (1 つずつ)。 */
|
|
138
|
+
const SORT_MEMBERS: Readonly<Record<"columnKey" | "direction", true>> = Object.freeze({ columnKey: true, direction: true })
|
|
139
|
+
|
|
140
|
+
/** The members of a value-list row, each once. / 値の一覧の 1 行の項目 (1 つずつ)。 */
|
|
141
|
+
const FILTER_VALUE_MEMBERS: Readonly<Record<keyof SpreadsheetFilterValue, true>> = Object.freeze({ value: true, label: true, count: true })
|
|
142
|
+
|
|
143
|
+
/** Every match mode, in menu order. / 全ての検索方法 (品書きの順)。 */
|
|
144
|
+
export const MATCH_MODES: readonly SpreadsheetMatchMode[] = Object.freeze(["exact", "fuzzy"])
|
|
145
|
+
|
|
146
|
+
/** Every sort direction. / 全ての並べ替えの向き。 */
|
|
147
|
+
const SORT_DIRECTIONS: readonly SpreadsheetSortDirection[] = Object.freeze(["asc", "desc"])
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Describes a rejected value for a RangeError message without throwing: strings as JSON, other primitives as written, and
|
|
151
|
+
* the kind of anything else.
|
|
152
|
+
* RangeError の文のための拒んだ値の説明 (投げない)。文字列は JSON、ほかの素の値はそのまま、それ以外は種類。
|
|
153
|
+
*
|
|
154
|
+
* @param value - The rejected value. 拒んだ値。
|
|
155
|
+
* @returns The description. 説明。
|
|
156
|
+
*/
|
|
157
|
+
const describeSortFilterValue = (value: unknown): string => {
|
|
158
|
+
if (typeof value === "string") {
|
|
159
|
+
return JSON.stringify(value)
|
|
160
|
+
}
|
|
161
|
+
if (value === null || value === undefined || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
|
|
162
|
+
return String(value)
|
|
163
|
+
}
|
|
164
|
+
if (Array.isArray(value)) {
|
|
165
|
+
return "an array"
|
|
166
|
+
}
|
|
167
|
+
return typeof value === "function" ? "a function" : typeof value === "symbol" ? "a symbol" : "an object"
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Whether a value is a plain object (prototype `Object.prototype` or `null`), so that only its own members are read.
|
|
172
|
+
* 値が素の物 (プロトタイプが `Object.prototype` か `null`) かどうか。自身の項目だけを読むための判定。
|
|
173
|
+
*
|
|
174
|
+
* @param value - The value. 値。
|
|
175
|
+
* @returns True for a plain object. 素の物なら true。
|
|
176
|
+
*/
|
|
177
|
+
const isPlainObject = (value: unknown): value is Record<string, unknown> => {
|
|
178
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
179
|
+
return false
|
|
180
|
+
}
|
|
181
|
+
const prototype: unknown = Object.getPrototypeOf(value)
|
|
182
|
+
return prototype === Object.prototype || prototype === null
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Throws when a plain object holds a key outside `members` (a misspelt member would otherwise be ignored without a word).
|
|
187
|
+
* 素の物が `members` の外の鍵を持てば投げる処理 (綴りを誤った項目が黙って無視されるのを防ぐ)。
|
|
188
|
+
*
|
|
189
|
+
* @param value - The object. 物。
|
|
190
|
+
* @param members - The known members. 知っている項目。
|
|
191
|
+
* @param path - The item's name in the message. 文の中の項目の名。
|
|
192
|
+
* @returns Nothing. なし。
|
|
193
|
+
* @throws {RangeError} When an unknown key is present. 知らない鍵がある場合。
|
|
194
|
+
*/
|
|
195
|
+
const assertKnownMembers = (value: Record<string, unknown>, members: Readonly<Record<string, true>>, path: string): void => {
|
|
196
|
+
for (const key of Object.keys(value)) {
|
|
197
|
+
if (!Object.hasOwn(members, key)) {
|
|
198
|
+
throw new RangeError(`[spreadsheet] ${path} has an unknown member ${JSON.stringify(key)}; expected ${Object.keys(members).join(", ")}`)
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Validates one complete options object (the defaults, or the defaults with a column's overrides laid on top).
|
|
205
|
+
* 指定の物 1 つ (全体の指定、または全体に列の差し替えを重ねた物) を検める処理。
|
|
206
|
+
*
|
|
207
|
+
* @param options - The options. 指定。
|
|
208
|
+
* @param path - The item's name in the message. 文の中の項目の名。
|
|
209
|
+
* @returns The options, typed. 型を付けた指定。
|
|
210
|
+
* @throws {RangeError} When a member is mistyped, the modes are empty, unknown or repeated, or the default mode is not offered. 項目の型の誤り・空や知らない名や重なる検索方法・使えない初めの方法の場合。
|
|
211
|
+
*/
|
|
212
|
+
const validateOptions = (options: Record<string, unknown>, path: string): SpreadsheetColumnSortFilterOptions => {
|
|
213
|
+
const { sortable, filterable, matchModes, defaultMatchMode } = options
|
|
214
|
+
if (typeof sortable !== "boolean") {
|
|
215
|
+
throw new RangeError(`[spreadsheet] ${path}.sortable must be true or false (got ${describeSortFilterValue(sortable)})`)
|
|
216
|
+
}
|
|
217
|
+
if (typeof filterable !== "boolean") {
|
|
218
|
+
throw new RangeError(`[spreadsheet] ${path}.filterable must be true or false (got ${describeSortFilterValue(filterable)})`)
|
|
219
|
+
}
|
|
220
|
+
if (!Array.isArray(matchModes)) {
|
|
221
|
+
throw new RangeError(`[spreadsheet] ${path}.matchModes must be an array of "exact" / "fuzzy" (got ${describeSortFilterValue(matchModes)})`)
|
|
222
|
+
}
|
|
223
|
+
if (matchModes.length === 0) {
|
|
224
|
+
throw new RangeError(`[spreadsheet] ${path}.matchModes must not be empty`)
|
|
225
|
+
}
|
|
226
|
+
const seen = new Set<SpreadsheetMatchMode>()
|
|
227
|
+
for (const mode of matchModes as unknown[]) {
|
|
228
|
+
if (!MATCH_MODES.includes(mode as SpreadsheetMatchMode)) {
|
|
229
|
+
throw new RangeError(`[spreadsheet] ${path}.matchModes holds an unknown match mode ${describeSortFilterValue(mode)}; expected "exact" or "fuzzy"`)
|
|
230
|
+
}
|
|
231
|
+
if (seen.has(mode as SpreadsheetMatchMode)) {
|
|
232
|
+
throw new RangeError(`[spreadsheet] ${path}.matchModes holds ${JSON.stringify(mode)} twice`)
|
|
233
|
+
}
|
|
234
|
+
seen.add(mode as SpreadsheetMatchMode)
|
|
235
|
+
}
|
|
236
|
+
if (!seen.has(defaultMatchMode as SpreadsheetMatchMode)) {
|
|
237
|
+
throw new RangeError(`[spreadsheet] ${path}.defaultMatchMode ${describeSortFilterValue(defaultMatchMode)} is not one of ${path}.matchModes (${[...seen].map((mode) => JSON.stringify(mode)).join(", ")})`)
|
|
238
|
+
}
|
|
239
|
+
return { sortable, filterable, matchModes: matchModes as readonly SpreadsheetMatchMode[], defaultMatchMode: defaultMatchMode as SpreadsheetMatchMode }
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** A keyed column with its index (the key table the state is checked against). / 列の番号を添えた鍵を持つ列 (状態を照らす鍵の表)。 */
|
|
243
|
+
type KeyedColumn = { readonly col: number; readonly column: ResolvedSortFilterColumn }
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Validates the state against the resolved columns and returns its sorted column and filtered columns.
|
|
247
|
+
* 状態を解いた列と照らして検め、並べ替えている列と絞り込んでいる列を返す処理。
|
|
248
|
+
*
|
|
249
|
+
* @param state - The state. 状態。
|
|
250
|
+
* @param keyed - The resolved column of each key. 鍵から解いた列への表。
|
|
251
|
+
* @returns The sorted column (or null) and the filtered columns. 並べ替えている列 (無ければ null) と絞り込んでいる列。
|
|
252
|
+
* @throws {RangeError} When the state is malformed or names a column that cannot take it. 状態の形の誤り、または受けられない列を名指す場合。
|
|
253
|
+
*/
|
|
254
|
+
const validateState = (state: unknown, keyed: ReadonlyMap<string, KeyedColumn>): { sort: { col: number; direction: SpreadsheetSortDirection } | null; filteredColumns: ReadonlySet<number> } => {
|
|
255
|
+
if (!isPlainObject(state)) {
|
|
256
|
+
throw new RangeError(`[spreadsheet] sortFilter.state must be an object with sort and filters (got ${describeSortFilterValue(state)})`)
|
|
257
|
+
}
|
|
258
|
+
assertKnownMembers(state, STATE_MEMBERS, "sortFilter.state")
|
|
259
|
+
// 知らない鍵は名指して止める (黙って捨てると、URL から作った状態の誤りが利用側に見えない)
|
|
260
|
+
const columnOf = (key: string, path: string): KeyedColumn => {
|
|
261
|
+
const entry = keyed.get(key)
|
|
262
|
+
if (entry === undefined) {
|
|
263
|
+
throw new RangeError(`[spreadsheet] ${path} names ${JSON.stringify(key)}, which is not the key of any column`)
|
|
264
|
+
}
|
|
265
|
+
return entry
|
|
266
|
+
}
|
|
267
|
+
const rawSort = state.sort
|
|
268
|
+
let sort: { col: number; direction: SpreadsheetSortDirection } | null = null
|
|
269
|
+
if (rawSort !== null) {
|
|
270
|
+
if (!isPlainObject(rawSort)) {
|
|
271
|
+
throw new RangeError(`[spreadsheet] sortFilter.state.sort must be null or { columnKey, direction } (got ${describeSortFilterValue(rawSort)})`)
|
|
272
|
+
}
|
|
273
|
+
assertKnownMembers(rawSort, SORT_MEMBERS, "sortFilter.state.sort")
|
|
274
|
+
const { columnKey, direction } = rawSort
|
|
275
|
+
if (typeof columnKey !== "string") {
|
|
276
|
+
throw new RangeError(`[spreadsheet] sortFilter.state.sort.columnKey must be a string (got ${describeSortFilterValue(columnKey)})`)
|
|
277
|
+
}
|
|
278
|
+
if (!SORT_DIRECTIONS.includes(direction as SpreadsheetSortDirection)) {
|
|
279
|
+
throw new RangeError(`[spreadsheet] sortFilter.state.sort.direction must be "asc" or "desc" (got ${describeSortFilterValue(direction)})`)
|
|
280
|
+
}
|
|
281
|
+
const { col, column } = columnOf(columnKey, "sortFilter.state.sort.columnKey")
|
|
282
|
+
if (!column.sortable) {
|
|
283
|
+
throw new RangeError(`[spreadsheet] sortFilter.state.sort.columnKey ${JSON.stringify(columnKey)} names column ${col}, which cannot sort`)
|
|
284
|
+
}
|
|
285
|
+
sort = { col, direction: direction as SpreadsheetSortDirection }
|
|
286
|
+
}
|
|
287
|
+
const rawFilters = state.filters
|
|
288
|
+
if (!isPlainObject(rawFilters)) {
|
|
289
|
+
throw new RangeError(`[spreadsheet] sortFilter.state.filters must be an object keyed by column key (got ${describeSortFilterValue(rawFilters)})`)
|
|
290
|
+
}
|
|
291
|
+
const filteredColumns = new Set<number>()
|
|
292
|
+
for (const key of Object.keys(rawFilters)) {
|
|
293
|
+
const path = `sortFilter.state.filters[${JSON.stringify(key)}]`
|
|
294
|
+
const { col, column } = columnOf(key, "sortFilter.state.filters")
|
|
295
|
+
if (!column.filterable) {
|
|
296
|
+
throw new RangeError(`[spreadsheet] ${path} names column ${col}, which cannot filter`)
|
|
297
|
+
}
|
|
298
|
+
const filter = rawFilters[key]
|
|
299
|
+
if (!isPlainObject(filter)) {
|
|
300
|
+
throw new RangeError(`[spreadsheet] ${path} must be { mode: "exact", values } or { mode: "fuzzy", query } (got ${describeSortFilterValue(filter)})`)
|
|
301
|
+
}
|
|
302
|
+
const mode = filter.mode
|
|
303
|
+
if (mode === "exact") {
|
|
304
|
+
assertKnownMembers(filter, { mode: true, values: true }, path)
|
|
305
|
+
const values = filter.values
|
|
306
|
+
if (!(Array.isArray(values) && values.every((value) => typeof value === "string"))) {
|
|
307
|
+
throw new RangeError(`[spreadsheet] ${path}.values must be an array of strings (got ${describeSortFilterValue(values)})`)
|
|
308
|
+
}
|
|
309
|
+
} else if (mode === "fuzzy") {
|
|
310
|
+
assertKnownMembers(filter, { mode: true, query: true }, path)
|
|
311
|
+
if (typeof filter.query !== "string") {
|
|
312
|
+
throw new RangeError(`[spreadsheet] ${path}.query must be a string (got ${describeSortFilterValue(filter.query)})`)
|
|
313
|
+
}
|
|
314
|
+
} else {
|
|
315
|
+
throw new RangeError(`[spreadsheet] ${path}.mode must be "exact" or "fuzzy" (got ${describeSortFilterValue(mode)})`)
|
|
316
|
+
}
|
|
317
|
+
if (!column.matchModes.includes(mode)) {
|
|
318
|
+
throw new RangeError(`[spreadsheet] ${path}.mode ${JSON.stringify(mode)} is not offered by column ${col} (match modes ${column.matchModes.map((offered) => JSON.stringify(offered)).join(", ")})`)
|
|
319
|
+
}
|
|
320
|
+
filteredColumns.add(col)
|
|
321
|
+
}
|
|
322
|
+
return { sort, filteredColumns }
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Validates the `sortFilter` prop at render and resolves it against the grid: the header row holding the controls is the
|
|
327
|
+
* last header row (`headerRows - 1`), every column in `[0, colCount)` is read once through `getColumnKey` and, for a keyed
|
|
328
|
+
* column, `getColumnOptions`, and the state is checked against the result.
|
|
329
|
+
* 描く時に props `sortFilter` を検め、グリッドに照らして解く処理。操作を置くのは見出しの最後の行 (`headerRows - 1`)。`[0, colCount)` の
|
|
330
|
+
* 全ての列を `getColumnKey` で 1 回ずつ読み、鍵を持つ列は `getColumnOptions` も読み、その結果に照らして状態を検める。
|
|
331
|
+
*
|
|
332
|
+
* @param sortFilter - The prop. props。
|
|
333
|
+
* @param colCount - The grid's column count. グリッドの列の数。
|
|
334
|
+
* @param headerRows - The `headerRows` prop (already validated against `frozenRows`). props `headerRows` (`frozenRows` と照らし済み)。
|
|
335
|
+
* @returns The resolved columns, key table, header row, sorted column and filtered columns. 解いた列・鍵の表・見出しの行・並べ替えている列・絞り込んでいる列。
|
|
336
|
+
* @throws {RangeError} When the prop, an option or the state is malformed (see the module contract). props・指定・状態に誤りがある場合 (モジュールの契約)。
|
|
337
|
+
*/
|
|
338
|
+
export const resolveSortFilter = (sortFilter: SpreadsheetSortFilter, colCount: number, headerRows: number): ResolvedSortFilter => {
|
|
339
|
+
const raw: unknown = sortFilter
|
|
340
|
+
if (!isPlainObject(raw)) {
|
|
341
|
+
throw new RangeError(`[spreadsheet] sortFilter must be an object or absent (got ${describeSortFilterValue(raw)})`)
|
|
342
|
+
}
|
|
343
|
+
if (headerRows < 1) {
|
|
344
|
+
throw new RangeError(`[spreadsheet] sortFilter needs headerRows of 1 or more: the sort and filter controls sit on the last header row (got headerRows ${headerRows})`)
|
|
345
|
+
}
|
|
346
|
+
assertKnownMembers(raw, SORT_FILTER_MEMBERS, "sortFilter")
|
|
347
|
+
for (const member of ["onStateChange", "getColumnKey"] as const) {
|
|
348
|
+
if (typeof raw[member] !== "function") {
|
|
349
|
+
throw new RangeError(`[spreadsheet] sortFilter.${member} must be a function (got ${describeSortFilterValue(raw[member])})`)
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
for (const member of ["getColumnOptions", "getFilterValues"] as const) {
|
|
353
|
+
if (raw[member] !== undefined && typeof raw[member] !== "function") {
|
|
354
|
+
throw new RangeError(`[spreadsheet] sortFilter.${member} must be a function or absent (got ${describeSortFilterValue(raw[member])})`)
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
const rawDefaults = raw.defaults
|
|
358
|
+
if (!isPlainObject(rawDefaults)) {
|
|
359
|
+
throw new RangeError(`[spreadsheet] sortFilter.defaults must be an object (got ${describeSortFilterValue(rawDefaults)})`)
|
|
360
|
+
}
|
|
361
|
+
assertKnownMembers(rawDefaults, OPTION_MEMBERS, "sortFilter.defaults")
|
|
362
|
+
const defaults = validateOptions(rawDefaults, "sortFilter.defaults")
|
|
363
|
+
const columns: (ResolvedSortFilterColumn | null)[] = []
|
|
364
|
+
const keyed = new Map<string, KeyedColumn>()
|
|
365
|
+
let exactColumn: KeyedColumn | null = null
|
|
366
|
+
let hasControls = false
|
|
367
|
+
for (let col = 0; col < colCount; col++) {
|
|
368
|
+
const key: unknown = sortFilter.getColumnKey(col)
|
|
369
|
+
if (key === null) {
|
|
370
|
+
columns.push(null)
|
|
371
|
+
continue
|
|
372
|
+
}
|
|
373
|
+
if (typeof key !== "string" || key === "") {
|
|
374
|
+
throw new RangeError(`[spreadsheet] sortFilter.getColumnKey(${col}) must return a non-empty string or null (got ${describeSortFilterValue(key)})`)
|
|
375
|
+
}
|
|
376
|
+
const earlier = keyed.get(key)
|
|
377
|
+
if (earlier !== undefined) {
|
|
378
|
+
throw new RangeError(`[spreadsheet] sortFilter.getColumnKey returned ${JSON.stringify(key)} for columns ${earlier.col} and ${col}; column keys must be unique`)
|
|
379
|
+
}
|
|
380
|
+
let options = defaults
|
|
381
|
+
if (sortFilter.getColumnOptions !== undefined) {
|
|
382
|
+
const path = `sortFilter.getColumnOptions(${JSON.stringify(key)})`
|
|
383
|
+
const override: unknown = sortFilter.getColumnOptions(key)
|
|
384
|
+
if (!isPlainObject(override)) {
|
|
385
|
+
throw new RangeError(`[spreadsheet] ${path} must return an object (got ${describeSortFilterValue(override)})`)
|
|
386
|
+
}
|
|
387
|
+
assertKnownMembers(override, OPTION_MEMBERS, path)
|
|
388
|
+
// 差し替えた項目だけが全体に代わる (無い項目と undefined は全体のまま — 部分型の省略と同じ読み)
|
|
389
|
+
const merged: Record<string, unknown> = { ...defaults }
|
|
390
|
+
for (const member of Object.keys(override)) {
|
|
391
|
+
if (override[member] !== undefined) {
|
|
392
|
+
merged[member] = override[member]
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
options = validateOptions(merged, path)
|
|
396
|
+
}
|
|
397
|
+
const column: ResolvedSortFilterColumn = { key, ...options }
|
|
398
|
+
columns.push(column)
|
|
399
|
+
keyed.set(key, { col, column })
|
|
400
|
+
if (options.sortable || options.filterable) {
|
|
401
|
+
hasControls = true
|
|
402
|
+
}
|
|
403
|
+
if (exactColumn === null && options.filterable && options.matchModes.includes("exact")) {
|
|
404
|
+
exactColumn = { col, column }
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
if (exactColumn !== null && sortFilter.getFilterValues === undefined) {
|
|
408
|
+
throw new RangeError(`[spreadsheet] sortFilter.getFilterValues is required: column ${exactColumn.col} (${JSON.stringify(exactColumn.column.key)}) can filter by exact match`)
|
|
409
|
+
}
|
|
410
|
+
const { sort, filteredColumns } = validateState(raw.state, keyed)
|
|
411
|
+
const columnOfKey = new Map<string, number>()
|
|
412
|
+
for (const [key, entry] of keyed) {
|
|
413
|
+
columnOfKey.set(key, entry.col)
|
|
414
|
+
}
|
|
415
|
+
return { columns, columnOfKey, headerRow: headerRows - 1, sort, filteredColumns, hasControls }
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* The resolved column at `col` when it carries controls (it sorts or filters), else null.
|
|
420
|
+
* `col` の解いた列。操作 (並べ替えか絞り込み) を持つ時だけ返し、それ以外は null。
|
|
421
|
+
*
|
|
422
|
+
* @param resolved - The resolved prop. 解いた props。
|
|
423
|
+
* @param col - The column. 列。
|
|
424
|
+
* @returns The column, or null. 列か null。
|
|
425
|
+
*/
|
|
426
|
+
export const controlsColumnOf = (resolved: ResolvedSortFilter, col: number): ResolvedSortFilterColumn | null => {
|
|
427
|
+
const column = resolved.columns[col]
|
|
428
|
+
return column !== null && column !== undefined && (column.sortable || column.filterable) ? column : null
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Whether two resolved columns are the same column with the same options (the root closes an open column menu when the
|
|
433
|
+
* live resolution of its header cell stops being the one it was opened for).
|
|
434
|
+
* 2 つの解いた列が同じ列で同じ指定か (開いている列の品書きの見出しのマスの今の解き方が、開いた時の物でなくなれば root が閉じる)。
|
|
435
|
+
*
|
|
436
|
+
* @param a - One column. 列 1。
|
|
437
|
+
* @param b - The other column. 列 2。
|
|
438
|
+
* @returns True when the key and every option are equal. 鍵と全ての指定が等しければ true。
|
|
439
|
+
*/
|
|
440
|
+
export const sameResolvedColumn = (a: ResolvedSortFilterColumn, b: ResolvedSortFilterColumn): boolean =>
|
|
441
|
+
a.key === b.key && a.sortable === b.sortable && a.filterable === b.filterable && a.defaultMatchMode === b.defaultMatchMode && a.matchModes.length === b.matchModes.length && a.matchModes.every((mode, index) => mode === b.matchModes[index])
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* The state with one column's sort cycled the way a header press cycles it: unsorted or another column sorted → ascending,
|
|
445
|
+
* ascending → descending, descending → unsorted.
|
|
446
|
+
* 見出しの押下が巡るとおりに 1 つの列の並べ替えを巡らせた状態。並べ替え無しか別の列 → 昇順、昇順 → 降順、降順 → 無し。
|
|
447
|
+
*
|
|
448
|
+
* @param state - The current state. 今の状態。
|
|
449
|
+
* @param key - The column key. 列の鍵。
|
|
450
|
+
* @returns The next state. 次の状態。
|
|
451
|
+
*/
|
|
452
|
+
export const cycleSort = (state: SpreadsheetSortFilterState, key: string): SpreadsheetSortFilterState => {
|
|
453
|
+
if (state.sort === null || state.sort.columnKey !== key) {
|
|
454
|
+
return { sort: { columnKey: key, direction: "asc" }, filters: state.filters }
|
|
455
|
+
}
|
|
456
|
+
return state.sort.direction === "asc" ? { sort: { columnKey: key, direction: "desc" }, filters: state.filters } : { sort: null, filters: state.filters }
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* The state with one column sorted in `direction`, or with that column's sort lifted (`null`). Lifting a column that is not
|
|
461
|
+
* the sorted one returns the state unchanged.
|
|
462
|
+
* 1 つの列を `direction` で並べ替えた状態、またはその列の並べ替えを外した状態 (`null`)。並べ替えていない列を外すなら状態はそのまま。
|
|
463
|
+
*
|
|
464
|
+
* @param state - The current state. 今の状態。
|
|
465
|
+
* @param key - The column key. 列の鍵。
|
|
466
|
+
* @param direction - The direction, or null to lift the sort. 向き (外すなら null)。
|
|
467
|
+
* @returns The next state. 次の状態。
|
|
468
|
+
*/
|
|
469
|
+
export const withSort = (state: SpreadsheetSortFilterState, key: string, direction: SpreadsheetSortDirection | null): SpreadsheetSortFilterState => {
|
|
470
|
+
if (direction !== null) {
|
|
471
|
+
return { sort: { columnKey: key, direction }, filters: state.filters }
|
|
472
|
+
}
|
|
473
|
+
return state.sort !== null && state.sort.columnKey === key ? { sort: null, filters: state.filters } : state
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* The state with one column's filter removed (the state itself when the column is not filtered).
|
|
478
|
+
* 1 つの列の絞り込みを外した状態 (絞り込んでいなければ状態そのもの)。
|
|
479
|
+
*
|
|
480
|
+
* @param state - The current state. 今の状態。
|
|
481
|
+
* @param key - The column key. 列の鍵。
|
|
482
|
+
* @returns The next state. 次の状態。
|
|
483
|
+
*/
|
|
484
|
+
export const withoutFilter = (state: SpreadsheetSortFilterState, key: string): SpreadsheetSortFilterState => {
|
|
485
|
+
if (!Object.hasOwn(state.filters, key)) {
|
|
486
|
+
return state
|
|
487
|
+
}
|
|
488
|
+
const filters: Record<string, SpreadsheetColumnFilter> = {}
|
|
489
|
+
for (const other of Object.keys(state.filters)) {
|
|
490
|
+
if (other !== key) {
|
|
491
|
+
filters[other] = state.filters[other] as SpreadsheetColumnFilter
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
return { sort: state.sort, filters }
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* The state with every filter removed (the state itself when nothing is filtered).
|
|
499
|
+
* 全ての絞り込みを外した状態 (何も絞り込んでいなければ状態そのもの)。
|
|
500
|
+
*
|
|
501
|
+
* @param state - The current state. 今の状態。
|
|
502
|
+
* @returns The next state. 次の状態。
|
|
503
|
+
*/
|
|
504
|
+
export const withoutFilters = (state: SpreadsheetSortFilterState): SpreadsheetSortFilterState => (Object.keys(state.filters).length === 0 ? state : { sort: state.sort, filters: {} })
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* The state with an exact filter applied from the column menu: when every listed value is checked the filter is lifted
|
|
508
|
+
* (it would keep the same rows), otherwise the checked values are stored in list order, and a checked value missing from
|
|
509
|
+
* the list (another column's filter removed its rows) is dropped.
|
|
510
|
+
* 品書きから完全一致の絞り込みを当てた状態。一覧の全ての値に印があれば絞り込みを外し (残る行が同じ)、それ以外は印のある値を一覧の順で
|
|
511
|
+
* 持つ。一覧に無い印の値 (ほかの列の絞り込みで行が消えた値) は落ちる。
|
|
512
|
+
*
|
|
513
|
+
* @param state - The current state. 今の状態。
|
|
514
|
+
* @param key - The column key. 列の鍵。
|
|
515
|
+
* @param checked - The checked values. 印のある値。
|
|
516
|
+
* @param listed - Every value of the list, in list order. 一覧の全ての値 (一覧の順)。
|
|
517
|
+
* @returns The next state. 次の状態。
|
|
518
|
+
*/
|
|
519
|
+
export const withExactFilter = (state: SpreadsheetSortFilterState, key: string, checked: ReadonlySet<string>, listed: readonly string[]): SpreadsheetSortFilterState => {
|
|
520
|
+
const values = listed.filter((value) => checked.has(value))
|
|
521
|
+
if (values.length === listed.length) {
|
|
522
|
+
return withoutFilter(state, key)
|
|
523
|
+
}
|
|
524
|
+
return { sort: state.sort, filters: { ...state.filters, [key]: { mode: "exact", values } } }
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* The state with a fuzzy filter applied from the column menu: a blank query (whitespace only) lifts the filter, any other
|
|
529
|
+
* query is stored as typed (the host owns the normalization).
|
|
530
|
+
* 品書きからあいまい検索の絞り込みを当てた状態。空白だけの語は絞り込みを外し、それ以外の語は打ったまま持つ (揃え方は利用側)。
|
|
531
|
+
*
|
|
532
|
+
* @param state - The current state. 今の状態。
|
|
533
|
+
* @param key - The column key. 列の鍵。
|
|
534
|
+
* @param query - The query. 語。
|
|
535
|
+
* @returns The next state. 次の状態。
|
|
536
|
+
*/
|
|
537
|
+
export const withFuzzyFilter = (state: SpreadsheetSortFilterState, key: string, query: string): SpreadsheetSortFilterState => {
|
|
538
|
+
if (query.trim() === "") {
|
|
539
|
+
return withoutFilter(state, key)
|
|
540
|
+
}
|
|
541
|
+
return { sort: state.sort, filters: { ...state.filters, [key]: { mode: "fuzzy", query } } }
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Whether two column filters have the same shape (the exact values compare in order).
|
|
546
|
+
* 2 つの列の絞り込みが同じ形か (完全一致の値は順も比べる)。
|
|
547
|
+
*
|
|
548
|
+
* @param a - One filter. 絞り込み 1。
|
|
549
|
+
* @param b - The other filter. 絞り込み 2。
|
|
550
|
+
* @returns True for the same shape. 同じ形なら true。
|
|
551
|
+
*/
|
|
552
|
+
const sameFilter = (a: SpreadsheetColumnFilter, b: SpreadsheetColumnFilter): boolean => {
|
|
553
|
+
if (a.mode === "fuzzy" || b.mode === "fuzzy") {
|
|
554
|
+
return a.mode === "fuzzy" && b.mode === "fuzzy" && a.query === b.query
|
|
555
|
+
}
|
|
556
|
+
return a.values.length === b.values.length && a.values.every((value, index) => value === b.values[index])
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Whether two states have the same shape: the same sort (or both none) and the same filter for every key (key order does
|
|
561
|
+
* not matter).
|
|
562
|
+
* 2 つの状態が同じ形か。並べ替えが同じ (どちらも無しを含む) で、全ての鍵の絞り込みが同じ (鍵の順は問わない)。
|
|
563
|
+
*
|
|
564
|
+
* @param a - One state. 状態 1。
|
|
565
|
+
* @param b - The other state. 状態 2。
|
|
566
|
+
* @returns True for the same shape. 同じ形なら true。
|
|
567
|
+
*/
|
|
568
|
+
export const sameSortFilterState = (a: SpreadsheetSortFilterState, b: SpreadsheetSortFilterState): boolean => {
|
|
569
|
+
if (a === b) {
|
|
570
|
+
return true
|
|
571
|
+
}
|
|
572
|
+
if (a.sort === null || b.sort === null ? a.sort !== b.sort : a.sort.columnKey !== b.sort.columnKey || a.sort.direction !== b.sort.direction) {
|
|
573
|
+
return false
|
|
574
|
+
}
|
|
575
|
+
const keys = Object.keys(a.filters)
|
|
576
|
+
if (keys.length !== Object.keys(b.filters).length) {
|
|
577
|
+
return false
|
|
578
|
+
}
|
|
579
|
+
return keys.every((key) => Object.hasOwn(b.filters, key) && sameFilter(a.filters[key] as SpreadsheetColumnFilter, b.filters[key] as SpreadsheetColumnFilter))
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Validates the value list `getFilterValues` resolved with: an array of `{ value, label, count }` rows with string value
|
|
584
|
+
* and label, a non-negative integer count, no other member and no repeated value. The column menu shows a malformed list as
|
|
585
|
+
* a load failure.
|
|
586
|
+
* `getFilterValues` が返した値の一覧を検める処理。`{ value, label, count }` の行の配列で、値と字は文字列、件数は 0 以上の整数、ほかの
|
|
587
|
+
* 項目は無く、値は重ならない。品書きは誤った一覧を読み込みの失敗として示す。
|
|
588
|
+
*
|
|
589
|
+
* @param values - The resolved value. 返った値。
|
|
590
|
+
* @returns The list, typed. 型を付けた一覧。
|
|
591
|
+
* @throws {RangeError} When the list is malformed. 一覧の形が誤っている場合。
|
|
592
|
+
*/
|
|
593
|
+
export const validateFilterValues = (values: unknown): readonly SpreadsheetFilterValue[] => {
|
|
594
|
+
if (!Array.isArray(values)) {
|
|
595
|
+
throw new RangeError(`[spreadsheet] sortFilter.getFilterValues must resolve with an array (got ${describeSortFilterValue(values)})`)
|
|
596
|
+
}
|
|
597
|
+
const seen = new Set<string>()
|
|
598
|
+
for (const [index, row] of (values as unknown[]).entries()) {
|
|
599
|
+
const path = `sortFilter.getFilterValues()[${index}]`
|
|
600
|
+
if (!isPlainObject(row)) {
|
|
601
|
+
throw new RangeError(`[spreadsheet] ${path} must be { value, label, count } (got ${describeSortFilterValue(row)})`)
|
|
602
|
+
}
|
|
603
|
+
assertKnownMembers(row, FILTER_VALUE_MEMBERS, path)
|
|
604
|
+
if (typeof row.value !== "string") {
|
|
605
|
+
throw new RangeError(`[spreadsheet] ${path}.value must be a string (got ${describeSortFilterValue(row.value)})`)
|
|
606
|
+
}
|
|
607
|
+
if (typeof row.label !== "string") {
|
|
608
|
+
throw new RangeError(`[spreadsheet] ${path}.label must be a string (got ${describeSortFilterValue(row.label)})`)
|
|
609
|
+
}
|
|
610
|
+
if (!(Number.isInteger(row.count) && (row.count as number) >= 0)) {
|
|
611
|
+
throw new RangeError(`[spreadsheet] ${path}.count must be a non-negative integer (got ${describeSortFilterValue(row.count)})`)
|
|
612
|
+
}
|
|
613
|
+
if (seen.has(row.value)) {
|
|
614
|
+
throw new RangeError(`[spreadsheet] ${path}.value ${JSON.stringify(row.value)} repeats an earlier value`)
|
|
615
|
+
}
|
|
616
|
+
seen.add(row.value)
|
|
617
|
+
}
|
|
618
|
+
return values as readonly SpreadsheetFilterValue[]
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Folds a text for the value search: NFKC (full-width and half-width forms meet) and lower case.
|
|
623
|
+
* 値の探す欄の照合のために字を揃える処理。NFKC (全角と半角を揃える) と小文字。
|
|
624
|
+
*
|
|
625
|
+
* @param text - The text. 字。
|
|
626
|
+
* @returns The folded text. 揃えた字。
|
|
627
|
+
*/
|
|
628
|
+
const foldForSearch = (text: string): string => text.normalize("NFKC").toLowerCase()
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* Whether a value's shown text matches the column menu's value search: the folded query is a substring of the folded text
|
|
632
|
+
* (an empty query matches everything).
|
|
633
|
+
* 値の見せる字が品書きの値の探す欄に当たるか。揃えた語が揃えた字の部分であること (空の語は全てに当たる)。
|
|
634
|
+
*
|
|
635
|
+
* @param shown - The shown text of the value. 値の見せる字。
|
|
636
|
+
* @param query - The query typed in the search box. 探す欄の語。
|
|
637
|
+
* @returns True on a match. 当たれば true。
|
|
638
|
+
*/
|
|
639
|
+
export const filterValueMatches = (shown: string, query: string): boolean => foldForSearch(shown).includes(foldForSearch(query))
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* The accessible column name of a header cell: its shown text with every line break turned into a space.
|
|
643
|
+
* 見出しのマスの読み上げる列の名。見せる字の改行を全て空白にした物。
|
|
644
|
+
*
|
|
645
|
+
* @param display - The header cell's shown text. 見出しのマスの見せる字。
|
|
646
|
+
* @returns The column name. 列の名。
|
|
647
|
+
*/
|
|
648
|
+
export const headerColumnName = (display: string): string => display.replace(/\r\n|\r|\n/g, " ")
|