| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
use makeover_layout::{FieldKind, Fit}; |
| 69 |
use quasi_router::screen::Act; |
| 70 |
use quasi_router::{Action, Choice, Consult, Field, Node, Picture, RegionKind, Slot}; |
| 71 |
|
| 72 |
|
| 73 |
const REGION: &str = "media-picker"; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
#[must_use] |
| 86 |
pub fn region_id(into: &str) -> String { |
| 87 |
format!("{REGION}-{into}") |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
#[must_use] |
| 98 |
pub fn panel_id(into: &str) -> String { |
| 99 |
format!("{REGION}-{into}-panel") |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
#[must_use] |
| 107 |
pub fn grid_id(into: &str) -> String { |
| 108 |
format!("{REGION}-{into}-grid") |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
#[must_use] |
| 123 |
pub fn addressable(into: &str) -> bool { |
| 124 |
!into.is_empty() |
| 125 |
&& into.len() <= 64 |
| 126 |
&& into |
| 127 |
.bytes() |
| 128 |
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'_') |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
pub const PATH: &str = "/media/picker"; |
| 133 |
|
| 134 |
|
| 135 |
pub const GRID_PATH: &str = "/media/picker/grid"; |
| 136 |
|
| 137 |
|
| 138 |
pub const CLOSE_PATH: &str = "/media/picker/close"; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
pub const INTO: &str = "into"; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
pub const NAME: &str = "media-name"; |
| 155 |
|
| 156 |
|
| 157 |
pub const FOLDER: &str = "media-folder"; |
| 158 |
|
| 159 |
|
| 160 |
const PANEL: &str = "media-picker"; |
| 161 |
|
| 162 |
|
| 163 |
const GRID_WIDGET: &str = "media-picker-grid"; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
fn widget(name: &str) -> RegionKind { |
| 175 |
RegionKind::Widget { |
| 176 |
name: name.to_owned(), |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
const WAIT: std::time::Duration = std::time::Duration::from_millis(150); |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
#[derive(Debug, Clone)] |
| 193 |
pub struct Entry { |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
pub id: String, |
| 200 |
|
| 201 |
pub filename: String, |
| 202 |
|
| 203 |
pub folder: String, |
| 204 |
|
| 205 |
|
| 206 |
pub reference: String, |
| 207 |
|
| 208 |
pub url: String, |
| 209 |
|
| 210 |
pub image: bool, |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
#[must_use] |
| 219 |
pub fn trigger(into: &str) -> String { |
| 220 |
use quasi_axum::Serves as _; |
| 221 |
|
| 222 |
let act = Act::new( |
| 223 |
"Insert Image", |
| 224 |
Action::get(PATH) |
| 225 |
.carrying(INTO, into) |
| 226 |
.replacing(region_id(into)), |
| 227 |
); |
| 228 |
|
| 229 |
format!( |
| 230 |
"{}{}", |
| 231 |
quasi_webview::Webview::new().fragment(&Node::Act(act)), |
| 232 |
dismissed(into) |
| 233 |
) |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
#[must_use] |
| 244 |
pub fn picker( |
| 245 |
entries: &[Entry], |
| 246 |
folders: &[String], |
| 247 |
into: &str, |
| 248 |
name: &str, |
| 249 |
folder: &str, |
| 250 |
) -> String { |
| 251 |
use quasi_axum::Serves as _; |
| 252 |
|
| 253 |
let mut panel = Slot::new(panel_id(into), widget(PANEL)) |
| 254 |
.with(Node::section("Insert from Media Library")) |
| 255 |
.with(Node::Field(Box::new(name_filter(into, name, folder)))) |
| 256 |
.with(Node::Field(Box::new(folder_filter( |
| 257 |
folders, into, name, folder, |
| 258 |
)))) |
| 259 |
.with(Node::Region(grid_slot(entries, into))); |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
panel = panel.with(Node::Act(Act::new( |
| 267 |
"Close", |
| 268 |
Action::get(CLOSE_PATH) |
| 269 |
.carrying(INTO, into) |
| 270 |
.replacing(region_id(into)), |
| 271 |
))); |
| 272 |
|
| 273 |
let region = Slot::new(region_id(into), RegionKind::Modal).with(Node::Region(panel)); |
| 274 |
|
| 275 |
quasi_webview::Webview::new().fragment(&Node::Region(region)) |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
#[must_use] |
| 280 |
pub fn grid(entries: &[Entry], into: &str) -> String { |
| 281 |
use quasi_axum::Serves as _; |
| 282 |
|
| 283 |
quasi_webview::Webview::new().fragment(&Node::Region(grid_slot(entries, into))) |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
#[must_use] |
| 291 |
pub fn dismissed(into: &str) -> String { |
| 292 |
use quasi_axum::Serves as _; |
| 293 |
|
| 294 |
quasi_webview::Webview::new() |
| 295 |
.fragment(&Node::Region(Slot::new(region_id(into), RegionKind::Group))) |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
fn grid_slot(entries: &[Entry], into: &str) -> Slot { |
| 300 |
let mut slot = Slot::new(grid_id(into), widget(GRID_WIDGET)); |
| 301 |
if entries.is_empty() { |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
return slot.with(Node::text( |
| 306 |
"No media files match. Upload images in your Media Library tab first.", |
| 307 |
)); |
| 308 |
} |
| 309 |
for entry in entries { |
| 310 |
slot = slot.with(card(entry, into)); |
| 311 |
} |
| 312 |
slot |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
fn card(entry: &Entry, into: &str) -> Node { |
| 336 |
let mut act = Act::new(&entry.filename, Action::local()).filling(into, &entry.reference); |
| 337 |
if entry.image { |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
let mut picture = Picture::new(&entry.url, &entry.filename).lazy(); |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
picture.fit = Fit::Contain; |
| 346 |
act = act.showing(picture); |
| 347 |
} |
| 348 |
Node::Act(act) |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
fn name_filter(into: &str, name: &str, folder: &str) -> Field { |
| 369 |
let mut field = Field::new(FieldKind::Text, NAME, "Filter by name").consulting( |
| 370 |
Consult::new( |
| 371 |
Action::get(GRID_PATH) |
| 372 |
.carrying(INTO, into) |
| 373 |
.carrying(FOLDER, folder) |
| 374 |
.replacing(grid_id(into)), |
| 375 |
) |
| 376 |
.after(WAIT) |
| 377 |
.sending([FOLDER]), |
| 378 |
); |
| 379 |
field.placeholder = Some("Filter by name...".to_owned()); |
| 380 |
field.value = Some(name.to_owned()); |
| 381 |
field |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
fn folder_filter(folders: &[String], into: &str, name: &str, folder: &str) -> Field { |
| 393 |
let mut options = vec![Choice::new("", "All folders")]; |
| 394 |
options.extend( |
| 395 |
folders |
| 396 |
.iter() |
| 397 |
.map(|folder| Choice::new(folder, if folder.is_empty() { "(root)" } else { folder })), |
| 398 |
); |
| 399 |
|
| 400 |
let mut field = Field::select(FOLDER, "Folder", options).consulting(Consult::at_once( |
| 401 |
Action::get(GRID_PATH) |
| 402 |
.carrying(INTO, into) |
| 403 |
.carrying(NAME, name) |
| 404 |
.replacing(grid_id(into)), |
| 405 |
)); |
| 406 |
field.value = Some(folder.to_owned()); |
| 407 |
field |
| 408 |
} |
| 409 |
|
| 410 |
#[cfg(test)] |
| 411 |
mod tests { |
| 412 |
use super::*; |
| 413 |
|
| 414 |
fn entry(filename: &str, folder: &str) -> Entry { |
| 415 |
let reference = if folder.is_empty() { |
| 416 |
format!("") |
| 417 |
} else { |
| 418 |
format!("") |
| 419 |
}; |
| 420 |
Entry { |
| 421 |
id: "11111111-1111-1111-1111-111111111111".to_owned(), |
| 422 |
filename: filename.to_owned(), |
| 423 |
folder: folder.to_owned(), |
| 424 |
reference, |
| 425 |
url: format!("https://cdn.test/{filename}"), |
| 426 |
image: true, |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
#[test] |
| 433 |
fn a_card_names_the_editor_it_writes_into_and_the_reference_it_deposits() { |
| 434 |
let html = grid(&[entry("kick.png", "drums")], "body"); |
| 435 |
|
| 436 |
assert!(html.contains(r#"data-fills="body""#), "{html}"); |
| 437 |
assert!( |
| 438 |
html.contains(r#"data-fill="""#), |
| 439 |
"{html}" |
| 440 |
); |
| 441 |
|
| 442 |
|
| 443 |
assert!(html.contains("data-local"), "{html}"); |
| 444 |
assert!(!html.contains("hx-get"), "{html}"); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
#[test] |
| 451 |
fn a_tile_shows_the_thumbnail_beside_the_control() { |
| 452 |
let html = grid(&[entry("kick.png", "drums")], "body"); |
| 453 |
|
| 454 |
assert!( |
| 455 |
html.contains(r#"src="https://cdn.test/kick.png""#), |
| 456 |
"{html}" |
| 457 |
); |
| 458 |
|
| 459 |
|
| 460 |
assert!(html.contains(r#"alt="kick.png""#), "{html}"); |
| 461 |
assert!(html.contains(r#"loading="lazy""#), "{html}"); |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
assert!(html.contains(r#"data-fit="contain""#), "{html}"); |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
assert!( |
| 472 |
!html.contains("media-card-"), |
| 473 |
"the per-tile region is gone: {html}" |
| 474 |
); |
| 475 |
let control = html |
| 476 |
.split_once("<button") |
| 477 |
.and_then(|(_, rest)| rest.split_once("</button>")) |
| 478 |
.map_or("", |(inner, _)| inner); |
| 479 |
assert!(control.contains("<img"), "{html}"); |
| 480 |
assert!(control.contains("kick.png"), "{html}"); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
#[test] |
| 486 |
fn a_file_with_nothing_to_show_is_the_control_alone() { |
| 487 |
let mut clip = entry("intro.mp4", ""); |
| 488 |
clip.image = false; |
| 489 |
let html = grid(&[clip], "body"); |
| 490 |
|
| 491 |
assert!(!html.contains("<img"), "{html}"); |
| 492 |
assert!(html.contains("intro.mp4"), "{html}"); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
#[test] |
| 499 |
fn a_card_reads_as_its_file_name() { |
| 500 |
let html = grid(&[entry("kick.png", "drums")], "body"); |
| 501 |
assert!(html.contains("kick.png"), "{html}"); |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
#[test] |
| 507 |
fn a_reference_is_deposited_exactly_once() { |
| 508 |
let html = grid(&[entry("kick.png", "drums")], "body"); |
| 509 |
assert!(!html.contains(", "{html}"); |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
#[test] |
| 517 |
fn a_crafted_file_name_stays_a_value() { |
| 518 |
let html = grid(&[entry(r#"" onclick="alert(1)"#, "")], "body"); |
| 519 |
|
| 520 |
assert!(!html.contains(r#"" onclick=""#), "{html}"); |
| 521 |
assert!(html.contains("""), "{html}"); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
#[test] |
| 527 |
fn a_filter_replaces_the_grid_and_leaves_the_box_it_was_typed_into() { |
| 528 |
let html = picker( |
| 529 |
&[entry("kick.png", "")], |
| 530 |
&["drums".to_owned()], |
| 531 |
"body", |
| 532 |
"", |
| 533 |
"", |
| 534 |
); |
| 535 |
|
| 536 |
assert!( |
| 537 |
html.contains(r##"hx-target="#media-picker-body-grid""##), |
| 538 |
"{html}" |
| 539 |
); |
| 540 |
assert!(html.contains(r#"hx-get="/media/picker/grid?"#), "{html}"); |
| 541 |
|
| 542 |
|
| 543 |
assert!(html.contains("delay:150ms"), "{html}"); |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
#[test] |
| 549 |
fn every_action_carries_the_editor_the_picker_was_opened_for() { |
| 550 |
let html = picker(&[entry("kick.png", "")], &[], "post-body", "", ""); |
| 551 |
assert!(html.contains("into"), "{html}"); |
| 552 |
assert!(html.contains("post-body"), "{html}"); |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
#[test] |
| 558 |
fn the_picker_is_a_modal_and_says_so() { |
| 559 |
let html = picker(&[], &[], "body", "", ""); |
| 560 |
assert!(html.contains(r#"role="dialog""#), "{html}"); |
| 561 |
assert!(html.contains(r#"aria-modal="true""#), "{html}"); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
#[test] |
| 567 |
fn a_dismissal_leaves_the_region_the_page_started_with() { |
| 568 |
let opened = trigger("body"); |
| 569 |
assert!(opened.contains(&dismissed("body")), "{opened}"); |
| 570 |
assert!( |
| 571 |
dismissed("body").contains(r#"id="media-picker-body""#), |
| 572 |
"{}", |
| 573 |
dismissed("body") |
| 574 |
); |
| 575 |
assert!(!dismissed("body").contains("kick.png")); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
#[test] |
| 581 |
fn the_trigger_aims_at_the_region_it_ships_with() { |
| 582 |
let html = trigger("body"); |
| 583 |
assert!( |
| 584 |
html.contains(r#"hx-get="/media/picker?into=body""#), |
| 585 |
"{html}" |
| 586 |
); |
| 587 |
assert!( |
| 588 |
html.contains(r##"hx-target="#media-picker-body""##), |
| 589 |
"{html}" |
| 590 |
); |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
#[test] |
| 596 |
fn nothing_to_show_says_so() { |
| 597 |
let html = grid(&[], "body"); |
| 598 |
assert!(html.contains("No media files match"), "{html}"); |
| 599 |
} |
| 600 |
} |
| 601 |
|