╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2043 // both minimums, so when it's that narrow we lift the cap and let 2044 // the list fill the whole width. 2045 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2046 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2047 2048 // stretch the detail pane across the rest of the width so it fills 2049 // the area rather than shrinking to its content; its scroll fills 2050 // the pane. 2051 if (constraint.max_size.width) |w| { 2052 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2053 } else { 2054 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2055 } 2056 } 2057 2058 // refresh the form inputs' entries in the session's focus-id -> input 2059 // map with this frame's addresses, so the web/wasm form handling can 2060 // find them by focus id 2061 if (self.threadForm()) |form| { 2062 const inputs_arena = self.session.arena.allocator(); 2063 for (form.children.values()) |*child| switch (child.widget) { 2064 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2065 else => {}, 2066 }; 2067 } 2068 2069 try self.box.build(allocator, constraint, root_focus); 2070 } 2071 2072 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2073 // wheel input moves focus like the arrows 2074 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2075 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2076 else 2077 raw_key; 2078 2079 if (self.headerActive()) { 2080 // down from the tabs re-enters the stack if the selected view has 2081 // something to focus; other keys move the tabs. 2082 if (inp.vertDirection(key) == .down) { 2083 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2084 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2085 // nothing focusable in the unauthorized view 2086 .unauthorized => false, 2087 else => true, 2088 } else false; 2089 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2090 } else { 2091 try self.header().input(allocator, key, root_focus); 2092 } 2093 return; 2094 } 2095 if (self.tagsViewActive()) { 2096 self.tagsViewInput(key, root_focus); 2097 return; 2098 } 2099 if (self.formViewActive()) { 2100 if (confirmation(self.data.view) != null) { 2101 try self.confirmationFormInput(allocator, key, root_focus); 2102 } else if (supports_conflicts and self.data.view == .resolve) { 2103 try self.resolveInput(allocator, key, root_focus); 2104 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 2105 try self.commentFormInput(allocator, key, root_focus); 2106 } else { 2107 try self.formInput(allocator, key, root_focus); 2108 } 2109 return; 2110 } 2111 const i = self.selectedSplitIndex() orelse return; 2112 if (self.detailActive(i)) { 2113 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2114 .none => {}, 2115 .list => self.focusList(i, root_focus), 2116 .header => self.focusHeader(root_focus), 2117 } 2118 } else { 2119 self.listInput(i, key, root_focus); 2120 } 2121 } 2122 2123 // arrow keys move the tag selection; up from the top row crosses to the 2124 // header tabs. 2125 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2126 const tf = self.tagsView(); 2127 const cid = tf.focus.child_id orelse return; 2128 const cur = tf.indexOfFocusId(cid) orelse return; 2129 const count = tf.text_boxes.items.len; 2130 switch (key) { 2131 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2132 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2133 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2134 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2135 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2136 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2137 else => {}, 2138 } 2139 } 2140 2141 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2142 // up/down (and the scroll wheel) move the selection a row; page up/down 2143 // jump a fixed amount. right/Enter cross into the detail pane. up from 2144 // the top row crosses into the header tabs. 2145 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2146 const lb = self.listBox(index); 2147 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2148 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2149 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2150 return; 2151 } 2152 switch (key) { 2153 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2154 else => {}, 2155 } 2156 } 2157 2158 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2159 const form = self.threadForm() orelse return; 2160 const cid = form.getFocus().child_id orelse return; 2161 const cur = form.children.getIndex(cid) orelse return; 2162 2163 if (cur == description_field_index) { 2164 const child = &form.children.values()[cur]; 2165 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2166 return child.widget.input(allocator, key, root_focus); 2167 } 2168 2169 switch (key) { 2170 .arrow_up, .back_tab => if (cur > 0) 2171 root_focus.setFocus(form.children.keys()[cur - 1]) 2172 else 2173 self.focusHeader(root_focus), 2174 .arrow_down, .tab => if (cur < submit_field_index) { 2175 root_focus.setFocus(form.children.keys()[cur + 1]); 2176 }, 2177 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2178 .mouse => |mouse| if (cur == submit_field_index) { 2179 const submit = &form.children.values()[cur].widget.submit_button; 2180 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2181 }, 2182 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2183 } 2184 } 2185 2186 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2187 const form = self.threadForm() orelse return; 2188 const cid = form.getFocus().child_id orelse return; 2189 const cur = form.children.getIndex(cid) orelse return; 2190 const child = &form.children.values()[cur]; 2191 2192 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2193 return child.widget.input(allocator, key, root_focus); 2194 2195 switch (key) { 2196 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2197 root_focus.setFocus(form.children.keys()[cur - 1]) 2198 else 2199 self.focusHeader(root_focus), 2200 .arrow_down, .tab => if (cur < comment_submit_field_index) 2201 root_focus.setFocus(form.children.keys()[cur + 1]), 2202 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2203 .mouse => |mouse| if (cur == comment_submit_field_index) { 2204 const submit = &child.widget.submit_button; 2205 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2206 }, 2207 else => try child.widget.input(allocator, key, root_focus), 2208 } 2209 } 2210 2211 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2212 const form = self.confirmationForm() orelse return; 2213 const child_id = form.getFocus().child_id orelse return; 2214 const index = form.children.getIndex(child_id) orelse return; 2215 switch (key) { 2216 .arrow_up, .back_tab => if (index > 1) 2217 root_focus.setFocus(form.children.keys()[index - 1]) 2218 else 2219 self.focusHeader(root_focus), 2220 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2221 .enter => try self.submitConfirmation(allocator, index), 2222 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2223 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2224 try self.submitConfirmation(allocator, button_index); 2225 break; 2226 } 2227 }, 2228 else => {}, 2229 } 2230 } 2231 2232 // the multiline hunk inputs keep enter and any up/down that has a row to 2233 // move to; enter on a "use this" link is a navigation the host follows 2234 // before this runs. 2235 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2236 const form = self.threadForm() orelse return; 2237 const cid = form.getFocus().child_id orelse return; 2238 const cur = form.children.getIndex(cid) orelse return; 2239 const child = &form.children.values()[cur]; 2240 2241 if (child.widget == .text_input) { 2242 const ti = &child.widget.text_input; 2243 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2244 return child.widget.input(allocator, key, root_focus); 2245 } 2246 2247 const on_submit = child.widget == .submit_button; 2248 2249 switch (key) { 2250 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2251 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2252 .arrow_left, .arrow_right => switch (child.widget) { 2253 .box => |*row| { 2254 const row_cid = row.getFocus().child_id orelse return; 2255 const row_index = row.children.getIndex(row_cid) orelse return; 2256 if (key == .arrow_left) { 2257 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2258 } else if (row_index + 1 < row.children.count()) { 2259 root_focus.setFocus(row.children.keys()[row_index + 1]); 2260 } 2261 }, 2262 else => try child.widget.input(allocator, key, root_focus), 2263 }, 2264 .enter => if (on_submit) try self.submitResolution(allocator), 2265 .mouse => |mouse| if (on_submit) { 2266 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2267 }, 2268 else => try child.widget.input(allocator, key, root_focus), 2269 } 2270 } 2271 2272 // a multiline input keeps enter and any up/down that has a row to move to 2273 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2274 return switch (key) { 2275 .enter => true, 2276 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2277 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2278 else => false, 2279 }; 2280 } 2281 2282 // the neighboring focusable resolve-form child, skipping the spacer and 2283 // the blank gap rows 2284 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2285 var i = cur; 2286 while (true) { 2287 if (down) { 2288 i += 1; 2289 if (i >= form.children.count()) return null; 2290 } else { 2291 if (i == 0) return null; 2292 i -= 1; 2293 } 2294 switch (form.children.values()[i].widget) { 2295 .spacer, .text => continue, 2296 else => return i, 2297 } 2298 } 2299 } 2300 2301 // focus the resolve-form child at `i` (a version row focuses its selected 2302 // child) and keep it visible on the terminal 2303 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2304 const child = &form.children.values()[i]; 2305 switch (child.widget) { 2306 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2307 else => root_focus.setFocus(form.children.keys()[i]), 2308 } 2309 if (self.session.is_terminal) { 2310 if (self.resolveScroll()) |sc| { 2311 if (child.rect) |rect| sc.scrollToRect(rect); 2312 } 2313 } 2314 } 2315 2316 // re-emit the conflicted thread's event with the resolved content; any 2317 // event on the thread settles its conflict entry. this is the terminal 2318 // path; the web posts the form to the resolve route. 2319 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2320 if (comptime wasm) return; 2321 const io = self.session.io orelse return; 2322 const src = self.data.repo_source orelse return; 2323 const entry = self.data.selectedThread() orelse return; 2324 const author = (try self.session.eventAuthor()) orelse return; 2325 const form = self.threadForm() orelse return; 2326 2327 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2328 var title: ?[]u8 = null; 2329 var tags: ?[]u8 = null; 2330 var hunks: std.ArrayList([]const u8) = .empty; 2331 defer { 2332 if (title) |t| allocator.free(t); 2333 if (tags) |t| allocator.free(t); 2334 for (hunks.items) |h| allocator.free(h); 2335 hunks.deinit(allocator); 2336 } 2337 for (form.children.values()) |*child| switch (child.widget) { 2338 .text_input => |*ti| { 2339 const text = try ti.text(allocator); 2340 errdefer allocator.free(text); 2341 if (std.mem.eql(u8, ti.options.name, "title")) { 2342 title = text; 2343 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2344 tags = text; 2345 } else { 2346 try hunks.append(allocator, text); 2347 } 2348 }, 2349 else => {}, 2350 }; 2351 2352 const id_bytes = try evt.parseEventId(entry.id); 2353 2354 switch (src.repo_kind) { 2355 inline else => |repo_kind| { 2356 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2357 defer any_repo.deinit(io, allocator); 2358 switch (any_repo) { 2359 inline else => |*repo| { 2360 var resolution: Event.Resolve = .{ 2361 .title = title, 2362 .tags = tags, 2363 .hunks = hunks.items, 2364 }; 2365 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2366 const change = Event.Update{ .resolve = resolution }; 2367 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2368 // leave the form up for correction 2369 error.InvalidFields => return, 2370 else => |e| return e, 2371 }; 2372 }, 2373 } 2374 }, 2375 } 2376 2377 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2378 try self.session.navigate(route); 2379 } 2380 2381 // commit a new or edited comment and navigate to its permalink. this is the 2382 // terminal path; the web posts the form to its comment route. 2383 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2384 if (comptime wasm) return; 2385 const io = self.session.io orelse return; 2386 const src = self.data.repo_source orelse return; 2387 const author = (try self.session.eventAuthor()) orelse return; 2388 const form = self.threadForm() orelse return; 2389 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2390 const body = try body_input.text(allocator); 2391 defer allocator.free(body); 2392 if (!evt.Comment.fieldsValid(body)) return; 2393 2394 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2395 @memcpy(&thread_id, self.data.selected_id); 2396 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2397 var parent: [evt.event_id_size * 2]u8 = undefined; 2398 @memcpy(&parent, self.data.comment_id); 2399 break :blk parent; 2400 }; 2401 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2402 const editing = self.data.view == .edit_comment; 2403 switch (src.repo_kind) { 2404 inline else => |repo_kind| { 2405 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2406 defer any_repo.deinit(io, allocator); 2407 switch (any_repo) { 2408 inline else => |*repo| if (editing) { 2409 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2410 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2411 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2412 } else { 2413 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2414 }, 2415 } 2416 }, 2417 } 2418 2419 body_input.clear(allocator); 2420 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2421 try self.session.navigate(route); 2422 } 2423 2424 // the terminal submit paths: the new form commits a new thread event, the 2425 // edit form re-emits the selected thread's event with the form's content. 2426 // the web posts the forms to their routes instead. 2427 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2428 if (self.data.view == .edit) { 2429 try self.submitEditedThread(allocator); 2430 } else { 2431 try self.submitNewThread(allocator); 2432 } 2433 } 2434 2435 // commit the new thread to the repo's events branch and navigate to it. 2436 // this is the terminal path; the web posts the form to the thread route, 2437 // so the wasm side never runs (or compiles) the repo access below. 2438 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2439 if (comptime wasm) return; 2440 const io = self.session.io orelse return; 2441 const src = self.data.repo_source orelse return; 2442 const author = (try self.session.eventAuthor()) orelse return; 2443 2444 const form = self.threadForm() orelse return; 2445 const title_input = &form.children.values()[title_field_index].widget.text_input; 2446 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2447 const description_input = &form.children.values()[description_field_index].widget.text_input; 2448 2449 const title = try title_input.text(allocator); 2450 defer allocator.free(title); 2451 const tags = try tags_input.text(allocator); 2452 defer allocator.free(tags); 2453 const description = try description_input.text(allocator); 2454 defer allocator.free(description); 2455 2456 if (!Event.fieldsValid(title, tags)) return; 2457 2458 if (comptime supports_drafts) { 2459 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2460 title_input.clear(allocator); 2461 tags_input.clear(allocator); 2462 description_input.clear(allocator); 2463 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2464 try self.session.navigate(route); 2465 return; 2466 } 2467 2468 var id_bytes: [evt.event_id_size]u8 = undefined; 2469 io.random(&id_bytes); 2470 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2471 2472 const event = evt.EventWithId{ 2473 .id = event_id_hex, 2474 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2475 .author = author, 2476 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2477 .title = title, 2478 .description = description, 2479 .tags = tags, 2480 }), 2481 }; 2482 2483 switch (src.repo_kind) { 2484 inline else => |repo_kind| { 2485 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2486 defer any_repo.deinit(io, allocator); 2487 switch (any_repo) { 2488 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2489 } 2490 }, 2491 } 2492 2493 // wipe the form so a return visit starts fresh 2494 title_input.clear(allocator); 2495 tags_input.clear(allocator); 2496 description_input.clear(allocator); 2497 2498 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2499 try self.session.navigate(route); 2500 } 2501 2502 // re-emit the selected thread's event with the form's content (status 2503 // preserved), then reload the thread's page. this is the terminal path; 2504 // the web posts the form to the edit route. 2505 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2506 if (comptime wasm) return; 2507 const io = self.session.io orelse return; 2508 const entry = self.data.selectedThread() orelse return; 2509 2510 const form = self.threadForm() orelse return; 2511 const title_input = &form.children.values()[title_field_index].widget.text_input; 2512 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2513 const description_input = &form.children.values()[description_field_index].widget.text_input; 2514 2515 const title = try title_input.text(allocator); 2516 defer allocator.free(title); 2517 const tags = try tags_input.text(allocator); 2518 defer allocator.free(tags); 2519 const description = try description_input.text(allocator); 2520 defer allocator.free(description); 2521 2522 if (!Event.fieldsValid(title, tags)) return; 2523 2524 if (comptime supports_drafts) { 2525 if (entryDraft(entry.*)) { 2526 try self.data.editDraft(self.session, allocator, entry.id, title, tags, description); 2527 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2528 try self.session.navigate(route); 2529 return; 2530 } 2531 } 2532 2533 const id_bytes = try evt.parseEventId(entry.id); 2534 const src = self.data.repo_source orelse return; 2535 const author = (try self.session.eventAuthor()) orelse return; 2536 2537 switch (src.repo_kind) { 2538 inline else => |repo_kind| { 2539 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2540 defer any_repo.deinit(io, allocator); 2541 switch (any_repo) { 2542 inline else => |*repo| if (has_status) 2543 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2544 .title = title, 2545 .tags = tags, 2546 .description = description, 2547 } }, author) 2548 else 2549 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2550 } 2551 }, 2552 } 2553 2554 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2555 try self.session.navigate(route); 2556 } 2557 2558 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2559 switch (confirmation(self.data.view) orelse return) { 2560 .publish => if (comptime supports_drafts) { 2561 if (comptime wasm) return; 2562 const entry = self.data.selectedThread() orelse return; 2563 if (!entryDraft(entry.*)) return; 2564 try self.data.publishDraft(self.session, allocator, entry.id); 2565 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2566 try self.session.navigate(route); 2567 } else unreachable, 2568 .merge => if (comptime supports_merge) { 2569 if (comptime wasm) return; 2570 const entry = self.data.selectedThread() orelse return; 2571 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2572 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2573 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2574 try self.session.navigate(route); 2575 } else unreachable, 2576 .remove => try self.submitRemove(allocator), 2577 } 2578 } 2579 2580 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2581 const comment = self.data.comment_id.len != 0; 2582 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2583 const id = evt.parseEventId(event_id) catch return; 2584 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2585 } 2586 2587 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2588 if (comptime wasm) return; 2589 2590 if (comptime supports_drafts) { 2591 const selected = self.data.selectedThread() orelse return; 2592 if (event_kind == kind and entryDraft(selected.*)) { 2593 try Data.removeDraft(self.session, allocator, &id); 2594 const route = draftsRoute(self.data.identity) orelse return; 2595 try self.session.navigate(route); 2596 return; 2597 } 2598 } 2599 2600 const io = self.session.io orelse return; 2601 const src = self.data.repo_source orelse return; 2602 const author = (try self.session.eventAuthor()) orelse return; 2603 2604 switch (src.repo_kind) { 2605 inline else => |repo_kind| { 2606 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2607 defer any_repo.deinit(io, allocator); 2608 switch (any_repo) { 2609 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2610 } 2611 }, 2612 } 2613 2614 const id_hex = std.fmt.bytesToHex(id, .lower); 2615 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2616 try self.session.navigate(route); 2617 } 2618 2619 // return to the list. 2620 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2621 root_focus.setFocus(self.listScroll(index).getFocus().id); 2622 } 2623 2624 // cross to the header tabs above the stack. 2625 fn focusHeader(self: *This, root_focus: *Focus) void { 2626 root_focus.setFocus(self.box.children.keys()[header_index]); 2627 } 2628 2629 pub fn clearGrid(self: *This) void { 2630 self.box.clearGrid(); 2631 } 2632 2633 pub fn getGrid(self: This) ?Grid { 2634 return self.box.getGrid(); 2635 } 2636 2637 pub fn getFocus(self: *This) *Focus { 2638 return self.box.getFocus(); 2639 } 2640 2641 // for the parent's "scroll up at the top jumps to the header" check: at the 2642 // top only while the header tabs hold focus, so up from the split 2643 // crosses into the tabs first. 2644 pub fn getSelectedIndex(self: *This) ?usize { 2645 return if (self.headerActive()) 0 else 1; 2646 } 2647 }; 2648 } 2649