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