Skip to content

Commit 61d9926

Browse files
committed
add 'add card' button
1 parent b8a1341 commit 61d9926

3 files changed

Lines changed: 50 additions & 7 deletions

File tree

crates/wordbase-cli/src/lookup.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use {
22
anyhow::{Context, Result},
33
std::{fmt::Write as _, path::Path, time::Instant},
44
tokio::fs,
5-
wordbase::{Engine, Profile, RecordKind},
5+
wordbase::{Engine, Profile, RecordKind, render::RenderConfig},
66
};
77

88
pub fn deinflect(engine: &Engine, text: &str) {
@@ -46,7 +46,13 @@ pub async fn render(engine: &Engine, profile: &Profile, text: &str, output: &Pat
4646

4747
let start = Instant::now();
4848
let mut html = engine
49-
.render_to_html(&records)
49+
.render_to_html(
50+
&records,
51+
&RenderConfig {
52+
add_card_text: "Add Card".into(),
53+
add_card_js_fn: "unimplemented".into(),
54+
},
55+
)
5056
.context("failed to render HTML")?;
5157
let end = Instant::now();
5258
println!("Rendered HTML in {:?}", end.duration_since(start));

crates/wordbase/src/render.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,30 @@ use wordbase_api::{DictionaryId, Record, RecordKind, RecordLookup, Term, dict};
88
use crate::{Engine, IndexMap};
99

1010
impl Engine {
11-
pub fn render_to_html(&self, records: &[RecordLookup]) -> Result<String> {
11+
pub fn render_to_html(
12+
&self,
13+
records: &[RecordLookup],
14+
config: &RenderConfig,
15+
) -> Result<String> {
1216
let terms = group_terms(records);
1317

1418
let mut context = tera::Context::new();
1519
context.insert("dictionaries", &self.dictionaries().0);
1620
context.insert("terms", &terms);
21+
context.insert("config", config);
1722

1823
let html = self.renderer.render("records.html", &context)?;
1924
Ok(html)
2025
}
2126
}
2227

28+
#[derive(Debug, Clone, Serialize)]
29+
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
30+
pub struct RenderConfig {
31+
pub add_card_text: String,
32+
pub add_card_js_fn: String,
33+
}
34+
2335
fn group_terms(records: &[RecordLookup]) -> Vec<RecordTerm> {
2436
// note on ordering:
2537
// by default, tera will not preserve the order of IndexMap entries,
@@ -197,8 +209,12 @@ const _: () = {
197209

198210
#[uniffi::export]
199211
impl Wordbase {
200-
pub fn render_to_html(&self, records: &[RecordLookup]) -> FfiResult<String> {
201-
Ok(self.0.render_to_html(records)?)
212+
pub fn render_to_html(
213+
&self,
214+
records: &[RecordLookup],
215+
translations: &RenderConfig,
216+
) -> FfiResult<String> {
217+
Ok(self.0.render_to_html(records, translations)?)
202218
}
203219
}
204220
};

record-templates/records.html

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@
115115
background-color: var(--button-active-color);
116116
}
117117

118+
button.suggested-action {
119+
background: var(--accent-color);
120+
}
121+
118122
table {
119123
border-collapse: collapse;
120124
margin: 8px 0;
@@ -241,14 +245,31 @@
241245
{% for group in terms %}
242246
<div style="display: flex; flex-direction: column; gap: 8px">
243247
<div class="header">
244-
<div class="content">
245-
<div style="padding: 16px 0; font-size: 2em">
248+
<div
249+
class="content"
250+
style="width: 100%; display: flex; flex-direction: row; gap: 16px"
251+
>
252+
<div style="padding: 16px 0; font-size: 2em; flex: 1">
246253
<ruby>
247254
{%- for part in group.furigana_parts -%}
248255
<span>{{- part[0] -}}</span><rt>{{- part[1] -}}</rt>
249256
{%- endfor -%}
250257
</ruby>
251258
</div>
259+
260+
<div
261+
style="display: flex; flex-direction: column; justify-content: center"
262+
>
263+
<button
264+
class="suggested-action"
265+
onclick='{{ config.add_card_js_fn }}({
266+
headword: {% if group.term.headword %}"{{ group.term.headword | addslashes }}"{% else %}null{% endif %},
267+
reading: {% if group.term.reading %}"{{ group.term.reading | addslashes }}"{% else %}null{% endif %},
268+
})'
269+
>
270+
{{ config.add_card_text }}
271+
</button>
272+
</div>
252273
</div>
253274
</div>
254275

0 commit comments

Comments
 (0)