Skip to content

Commit ef6d945

Browse files
committed
awful terrible js html templating stuff to get anki to work
1 parent 58c5b11 commit ef6d945

2 files changed

Lines changed: 46 additions & 29 deletions

File tree

crates/wordbase/src/records.html

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@
301301
for_el_with_class(
302302
script,
303303
class_name,
304-
(el) => (el.style.display = "hidden"),
304+
(el) => (el.style.display = "none"),
305305
);
306306
}
307307
</script>
@@ -375,6 +375,7 @@
375375
</div>
376376

377377
<div
378+
class="manage-notes"
378379
style="
379380
padding: 1.5em 0;
380381
display: flex;
@@ -408,12 +409,11 @@
408409
class="suggested-action"
409410
onclick="
410411
{
411-
const on_add_note = () => {
412-
412+
const on_add_new_note = () => {
413+
this.closest('.manage-notes').update_manage_notes();
413414
};
415+
{{ fn_add_new_note | replace(from="<js_callback>", to="on_add_new_note") | safe }}
414416
}
415-
416-
{{ fn_add_new_note | safe }}
417417
"
418418
>
419419
<svg class="icon">
@@ -428,7 +428,14 @@
428428
<div style="flex: 1; position: relative">
429429
<button
430430
title="{{ config.s_add_duplicate_note }}"
431-
onclick="{{ fn_add_duplicate_note | safe }}"
431+
onclick="
432+
{
433+
const on_add_new_note = () => {
434+
this.closest('.manage-notes').update_manage_notes();
435+
};
436+
{{ fn_add_duplicate_note | replace(from="<js_callback>", to="on_add_new_note") | safe }}
437+
}
438+
"
432439
style="flex: 1; position: relative"
433440
>
434441
<svg class="icon">
@@ -454,27 +461,30 @@
454461
<script>
455462
{
456463
const script = document.currentScript;
457-
458-
const on_num_existing_notes = (num) => {
459-
hide_with_class(script, "add-note");
460-
hide_with_class(script, "note-actions");
461-
hide_with_class(script, "multiple-notes");
462-
463-
if (num > 0) {
464-
show_with_class(script, "note-actions");
465-
if (num > 1) {
466-
for_el_with_class(script, "multiple-notes", (el) => {
467-
el.style.display = "flex";
468-
el.innerHTML = `${num}`;
469-
});
464+
function update_manage_notes() {
465+
const on_num_existing_notes = (num) => {
466+
hide_with_class(script, "add-note");
467+
hide_with_class(script, "note-actions");
468+
hide_with_class(script, "multiple-notes");
469+
470+
if (num > 0) {
471+
show_with_class(script, "note-actions");
472+
if (num > 1) {
473+
for_el_with_class(script, "multiple-notes", (el) => {
474+
el.style.display = "flex";
475+
el.innerHTML = `${num}`;
476+
});
477+
}
478+
} else {
479+
show_with_class(script, "add-note");
470480
}
471-
} else {
472-
show_with_class(script, "add-note");
473-
}
474-
};
481+
};
475482

476-
// prettier-ignore
477-
{{ fn_num_existing_notes | replace(from="<js_callback>", to="on_num_existing_notes") | safe }}
483+
// prettier-ignore
484+
{{ fn_num_existing_notes | replace(from="<js_callback>", to="on_num_existing_notes") | safe }}
485+
}
486+
script.parentElement.update_manage_notes = update_manage_notes;
487+
update_manage_notes();
478488
}
479489
</script>
480490
</div>

wordbase-android/app/src/main/java/io/github/aecsocket/wordbase/RecordsView.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ fun RawRecordsView(
351351
sViewNote = sViewNote,
352352
sAddDuplicateNote = sAddDuplicateNote,
353353
fnNumExistingNotes = jsCall("num_existing_notes", callback = true),
354-
fnAddNewNote = jsCall("add_new_note"),
355-
fnAddDuplicateNote = jsCall("add_duplicate_note"),
354+
fnAddNewNote = jsCall("add_new_note", callback = true),
355+
fnAddDuplicateNote = jsCall("add_duplicate_note", callback = true),
356356
fnViewNote = jsCall("view_note"),
357357
),
358358
)
@@ -404,7 +404,7 @@ fun RawRecordsView(
404404
}
405405

406406
@Composable
407-
fun <R> registerJsFunction(methodName: String, fn: (suspend (Term) -> R?)?) {
407+
fun <R> registerJsFunction(methodName: String, fn: (suspend (Term) -> R)?) {
408408
var jsHandler by remember { mutableStateOf<IJsMessageHandler?>(null) }
409409
LaunchedEffect(jsBridge, fn) {
410410
println("UnRegistered $methodName")
@@ -431,7 +431,14 @@ fun RawRecordsView(
431431
)
432432

433433
CoroutineScope(Dispatchers.IO).launch {
434-
fn(term)?.let { r -> callback(r.toString()) }
434+
fn(term)?.let { r ->
435+
// todo this is suck a hack 🙄
436+
if (r == Unit) {
437+
callback("{}")
438+
} else {
439+
callback(r.toString())
440+
}
441+
}
435442
}
436443
}
437444
}

0 commit comments

Comments
 (0)