(block: HTMLElement, folded: boolean)
| 150 | } |
| 151 | |
| 152 | function persistCodeBlockFoldState(block: HTMLElement, folded: boolean): void { |
| 153 | const storageKey = block.getAttribute(CODE_BLOCK_STORAGE_KEY_ATTR) |
| 154 | const rawIndex = block.getAttribute(CODE_BLOCK_INDEX_ATTR) |
| 155 | const index = rawIndex == null ? Number.NaN : Number.parseInt(rawIndex, 10) |
| 156 | if (!storageKey || !Number.isFinite(index) || index < 0) return |
| 157 | |
| 158 | const next = readFoldMap(storageKey) |
| 159 | if (folded) next[String(index)] = true |
| 160 | else delete next[String(index)] |
| 161 | |
| 162 | try { |
| 163 | if (Object.keys(next).length > 0) { |
| 164 | window.localStorage.setItem(storageKey, JSON.stringify(next)) |
| 165 | } else { |
| 166 | window.localStorage.removeItem(storageKey) |
| 167 | } |
| 168 | } catch { |
| 169 | // Persistence is best-effort; folding should still work without storage. |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | function readPersistedFoldState(storageKey: string, index: number): boolean | null { |
| 174 | const value = readFoldMap(storageKey)[String(index)] |
no test coverage detected