feat: Highlight term upon setting it SentenceListeningNote

This commit is contained in:
Dendy 2025-04-12 08:30:42 +02:00
parent e3064a3ace
commit 1ff04811cb
2 changed files with 18 additions and 2 deletions

View File

@ -94,4 +94,18 @@ class Note
{
return \DateTimeImmutable::createFromFormat('U', $this->mod);
}
// -------------------------------------------------- Utility functions ---
protected static function stringHighlight(string $haystack, string $needle)
{
$replace = sprintf(
'<span %s>%s</span>',
self::HIGHLIGHT_ATTR_KANJI,
$needle,
);
return str_replace($needle, $replace, strip_tags($haystack));
}
}

View File

@ -20,6 +20,10 @@ class SentenceListeningNote extends Note
$this->fields['VocabKanji'] = $term->getKanji();
$this->fields['VocabFurigana'] = $term->getReading();
$this->fields['VocabDef'] = $term->toAnkiVocabDef();
$this->fields['SentKanji'] = $this->stringHighlight(
$this->fields['SentKanji'],
$term->getKanji(),
);
$this->term = $term;
return $this;
}
@ -55,8 +59,6 @@ class SentenceListeningNote extends Note
// Related fields are updated using the setter
$slNote->setTerm($term);
// Remove highlighting
$slNote->fields['SentKanji'] = strip_tags($slNote->fields['SentKanji']);
// Reset relations and basic data
$slNote->id = null;
$slNote->model = self::MODEL_NAME;