╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2173 }; 2174 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2175 const editing = self.data.view == .edit_comment; 2176 switch (src.repo_kind) { 2177 inline else => |repo_kind| { 2178 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2179 defer any_repo.deinit(io, allocator); 2180 switch (any_repo) { 2181 inline else => |*repo| if (editing) { 2182 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2183 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2184 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2185 } else { 2186 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2187 }, 2188 } 2189 }, 2190 } 2191 2192 body_input.clear(allocator); 2193 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2194 try self.session.navigate(route); 2195 } 2196 2197 // the terminal submit paths: the new form commits a new thread event, the 2198 // edit form re-emits the selected thread's event with the form's content. 2199 // the web posts the forms to their routes instead. 2200 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2201 if (self.data.view == .edit) { 2202 try self.submitEditedThread(allocator); 2203 } else { 2204 try self.submitNewThread(allocator); 2205 } 2206 } 2207 2208 // commit the new thread to the repo's events branch and navigate to it. 2209 // this is the terminal path; the web posts the form to the thread route, 2210 // so the wasm side never runs (or compiles) the repo access below. 2211 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2212 if (comptime wasm) return; 2213 const io = self.session.io orelse return; 2214 const src = self.data.repo_source orelse return; 2215 const author = (try self.session.eventAuthor()) orelse return; 2216 2217 const form = self.threadForm() orelse return; 2218 const title_input = &form.children.values()[title_field_index].widget.text_input; 2219 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2220 const description_input = &form.children.values()[description_field_index].widget.text_input; 2221 2222 const title = try title_input.text(allocator); 2223 defer allocator.free(title); 2224 const tags = try tags_input.text(allocator); 2225 defer allocator.free(tags); 2226 const description = try description_input.text(allocator); 2227 defer allocator.free(description); 2228 2229 if (!Event.fieldsValid(title, tags)) return; 2230 2231 if (comptime supports_drafts) { 2232 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2233 title_input.clear(allocator); 2234 tags_input.clear(allocator); 2235 description_input.clear(allocator); 2236 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2237 try self.session.navigate(route); 2238 return; 2239 } 2240 2241 var id_bytes: [evt.event_id_size]u8 = undefined; 2242 io.random(&id_bytes); 2243 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2244 2245 const event = evt.EventWithId{ 2246 .id = event_id_hex, 2247 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2248 .author = author, 2249 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2250 .title = title, 2251 .description = description, 2252 .tags = tags, 2253 }), 2254 }; 2255 2256 switch (src.repo_kind) { 2257 inline else => |repo_kind| { 2258 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2259 defer any_repo.deinit(io, allocator); 2260 switch (any_repo) { 2261 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2262 } 2263 }, 2264 } 2265 2266 // wipe the form so a return visit starts fresh 2267 title_input.clear(allocator); 2268 tags_input.clear(allocator); 2269 description_input.clear(allocator); 2270 2271 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2272 try self.session.navigate(route); 2273 } 2274 2275 // re-emit the selected thread's event with the form's content (status 2276 // preserved), then reload the thread's page. this is the terminal path; 2277 // the web posts the form to the edit route. 2278 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2279 if (comptime wasm) return; 2280 const io = self.session.io orelse return; 2281 const src = self.data.repo_source orelse return; 2282 const entry = self.data.selectedThread() orelse return; 2283 const author = (try self.session.eventAuthor()) orelse return; 2284 2285 const form = self.threadForm() orelse return; 2286 const title_input = &form.children.values()[title_field_index].widget.text_input; 2287 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2288 const description_input = &form.children.values()[description_field_index].widget.text_input; 2289 2290 const title = try title_input.text(allocator); 2291 defer allocator.free(title); 2292 const tags = try tags_input.text(allocator); 2293 defer allocator.free(tags); 2294 const description = try description_input.text(allocator); 2295 defer allocator.free(description); 2296 2297 if (!Event.fieldsValid(title, tags)) return; 2298 2299 const id_bytes = try evt.parseEventId(entry.id); 2300 2301 switch (src.repo_kind) { 2302 inline else => |repo_kind| { 2303 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2304 defer any_repo.deinit(io, allocator); 2305 switch (any_repo) { 2306 inline else => |*repo| if (has_status) 2307 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2308 .title = title, 2309 .tags = tags, 2310 .description = description, 2311 } }, author) 2312 else 2313 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2314 } 2315 }, 2316 } 2317 2318 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2319 try self.session.navigate(route); 2320 } 2321 2322 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2323 const comment = self.data.comment_id.len != 0; 2324 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2325 const id = evt.parseEventId(event_id) catch return; 2326 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2327 } 2328 2329 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2330 if (comptime wasm) return; 2331 2332 if (comptime supports_drafts) { 2333 const selected = self.data.selectedThread() orelse return; 2334 if (event_kind == kind and entryDraft(selected.*)) { 2335 try Data.removeDraft(self.session, allocator, &id); 2336 const route = draftsRoute(self.data.identity) orelse return; 2337 try self.session.navigate(route); 2338 return; 2339 } 2340 } 2341 2342 const io = self.session.io orelse return; 2343 const src = self.data.repo_source orelse return; 2344 const author = (try self.session.eventAuthor()) orelse return; 2345 2346 switch (src.repo_kind) { 2347 inline else => |repo_kind| { 2348 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2349 defer any_repo.deinit(io, allocator); 2350 switch (any_repo) { 2351 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2352 } 2353 }, 2354 } 2355 2356 const id_hex = std.fmt.bytesToHex(id, .lower); 2357 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2358 try self.session.navigate(route); 2359 } 2360 2361 // flip the shown thread's status by re-emitting its event, then reload the 2362 // page rooted at the thread so the view reflects the change. this is the 2363 // terminal path; the web posts the button's form to the status route. 2364 fn toggleStatus(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2365 if (comptime !has_status) return; 2366 if (comptime wasm) return; 2367 const io = self.session.io orelse return; 2368 const src = self.data.repo_source orelse return; 2369 const sel = self.detailed_index[index] orelse return; 2370 const entry = self.window(index).items[sel]; 2371 const author = (try self.session.eventAuthor()) orelse return; 2372 2373 const status = (statusChange(entryStatus(entry)) orelse return).status; 2374 2375 const id_bytes = try evt.parseEventId(entry.id); 2376 2377 switch (src.repo_kind) { 2378 inline else => |repo_kind| { 2379 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2380 defer any_repo.deinit(io, allocator); 2381 switch (any_repo) { 2382 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2383 } 2384 }, 2385 } 2386 2387 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 2388 try self.session.navigate(route); 2389 } 2390 2391 // arrow keys move the tag selection; at the flow's edges focus crosses to 2392 // the neighboring widgets. 2393 fn tagsInput(self: *This, index: usize, child_index: usize, tf: *TagFlow, key: Key, root_focus: *Focus) void { 2394 const cid = tf.focus.child_id orelse return; 2395 const cur = tf.indexOfFocusId(cid) orelse return; 2396 const count = tf.text_boxes.items.len; 2397 switch (key) { 2398 .arrow_left => if (cur > 0) self.focusTag(index, child_index, tf, root_focus, cur - 1) else self.focusList(index, root_focus), 2399 .arrow_right => if (cur + 1 < count) self.focusTag(index, child_index, tf, root_focus, cur + 1), 2400 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2401 self.focusTag(index, child_index, tf, root_focus, i); 2402 } else { 2403 _ = self.moveDetailVertical(index, root_focus, false); 2404 }, 2405 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2406 self.focusTag(index, child_index, tf, root_focus, i); 2407 } else { 2408 _ = self.moveDetailVertical(index, root_focus, true); 2409 }, 2410 .home => self.focusTag(index, child_index, tf, root_focus, 0), 2411 .end => self.focusTag(index, child_index, tf, root_focus, count - 1), 2412 else => {}, 2413 } 2414 } 2415 2416 // the tool row's first child after the spacer that pushes it right 2417 const first_in_row_index: usize = 1; 2418 2419 fn toolRowFocused(self: *This, index: usize, root_focus: *Focus) bool { 2420 return self.focusedDetailChild(index, root_focus) == tool_row_index; 2421 } 2422 2423 fn focusTag(self: *This, index: usize, child_index: usize, tf: *TagFlow, root_focus: *Focus, item: usize) void { 2424 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2425 // keep the tag visible on the terminal: its rect offset by the flow's 2426 // position in the pane. 2427 if (self.session.is_terminal and item < tf.rects.items.len) { 2428 if (self.detailInner(index).children.values()[child_index].rect) |flow_rect| { 2429 var rect = tf.rects.items[item]; 2430 rect.x += flow_rect.x; 2431 rect.y += flow_rect.y; 2432 self.detailScroll(index).scrollToRect(rect); 2433 } 2434 } 2435 } 2436 2437 // return to the tool row's last-focused button (the header when the 2438 // description page shows no row). 2439 fn focusToolRow(self: *This, index: usize, root_focus: *Focus) void { 2440 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2441 root_focus.setFocus(cid); 2442 if (self.detailInner(index).children.values()[tool_row_index].rect) |rect| 2443 self.detailScroll(index).scrollToRect(rect); 2444 } 2445 2446 // enter the detail pane. an empty pane can't be entered. 2447 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 2448 if (self.detailInner(index).children.count() == 0) return; 2449 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2450 } 2451 2452 // return to the list. 2453 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2454 root_focus.setFocus(self.listScroll(index).getFocus().id); 2455 } 2456 2457 // cross to the header tabs above the stack. 2458 fn focusHeader(self: *This, root_focus: *Focus) void { 2459 root_focus.setFocus(self.box.children.keys()[header_index]); 2460 } 2461 2462 pub fn clearGrid(self: *This) void { 2463 self.box.clearGrid(); 2464 } 2465 2466 pub fn getGrid(self: This) ?Grid { 2467 return self.box.getGrid(); 2468 } 2469 2470 pub fn getFocus(self: *This) *Focus { 2471 return self.box.getFocus(); 2472 } 2473 2474 // for the parent's "scroll up at the top jumps to the header" check: at the 2475 // top only while the header tabs hold focus, so up from the split 2476 // crosses into the tabs first. 2477 pub fn getSelectedIndex(self: *This) ?usize { 2478 return if (self.headerActive()) 0 else 1; 2479 } 2480 }; 2481 } 2482