9dfa06ffee
Docker image / Build (linux/amd64) (push) Has been cancelled
Docker image / Build (linux/arm64) (push) Has been cancelled
Docker image / Merge release multi-arch manifest (push) Has been cancelled
Docker image / Merge debug multi-arch manifest (push) Has been cancelled
Docker image / Build public push gateway (linux/amd64) (push) Has been cancelled
Docker image / Build public push gateway (linux/arm64) (push) Has been cancelled
Docker image / Publish public push gateway image (push) Has been cancelled
Sprig image / Build (linux/amd64) (push) Has been cancelled
Sprig image / Build (linux/arm64) (push) Has been cancelled
Sprig image / Merge multi-arch manifest (push) Has been cancelled
Harbor Buzz Orchestra / Python tests and lint (push) Has been cancelled
CI / Detect Changed Paths (push) Has been cancelled
CI / Rust Lint (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Desktop Core (push) Has been cancelled
CI / Desktop Smoke E2E (1) (push) Has been cancelled
CI / Desktop Smoke E2E (2) (push) Has been cancelled
CI / Desktop Smoke E2E (3) (push) Has been cancelled
CI / Desktop Smoke E2E (4) (push) Has been cancelled
CI / Desktop (push) Has been cancelled
CI / Desktop E2E Relay (push) Has been cancelled
CI / Desktop E2E Integration (1/2) (push) Has been cancelled
CI / Desktop E2E Integration (2/2) (push) Has been cancelled
CI / Desktop E2E Integration (push) Has been cancelled
CI / Backend Integration (relay e2e) (push) Has been cancelled
CI / Relay E2E (push) Has been cancelled
CI / Web (push) Has been cancelled
CI / Mobile (push) Has been cancelled
CI / Security (push) Has been cancelled
CI / Dead Token Reference Guard (push) Has been cancelled
CI / Server Cross-Compile (aarch64-unknown-linux-musl) (push) Has been cancelled
CI / Server Cross-Compile (x86_64-unknown-linux-musl) (push) Has been cancelled
CI / Windows Rust (x86_64-pc-windows-msvc) (push) Has been cancelled
CI / Desktop Build (macOS) (push) Has been cancelled
helm chart / lint + unittest + render matrix (push) Has been cancelled
helm chart / install on kind (gated) (push) Has been cancelled
helm chart / publish chart to GHCR (push) Has been cancelled
Mesh Lifecycle / Relay-Driven Mesh Lifecycle Smoke (push) Has been cancelled
Sprig / Build (aarch64-unknown-linux-musl) (push) Has been cancelled
Sprig / Build (x86_64-unknown-linux-musl) (push) Has been cancelled
Sprig / Publish rolling release (push) Has been cancelled
Sprig / Publish tagged release (push) Has been cancelled
Signed-off-by: cls_宁波本机 <908705107@qq.com>
215 lines
6.9 KiB
Dart
215 lines
6.9 KiB
Dart
/// Emoji search for the mobile picker and reaction tray.
|
|
///
|
|
/// Ported from `desktop/src/shared/lib/emojiSearch.ts`, which exists because
|
|
/// emoji-mart's own search is token-prefix based and so can't cross the `_` in
|
|
/// a shortcode — `pointup` never finds `point_up`. The tiers keep ordinary
|
|
/// queries ranked the way you'd expect while still surfacing looser matches
|
|
/// last: exact > prefix > substring > subsequence over the shortcode.
|
|
///
|
|
/// Mobile extends the desktop tiers with name/keyword matching, because the
|
|
/// tray replaces emoji-mart's picker (which searched names and keywords) rather
|
|
/// than just its `:shortcode` autocomplete. Shortcode hits still outrank
|
|
/// keyword hits, so typing a shortcode you know never buries it.
|
|
library;
|
|
|
|
import 'emoji_data.dart';
|
|
|
|
// Lower tier = stronger match.
|
|
const _tierExact = 0;
|
|
const _tierShortcodePrefix = 1;
|
|
const _tierKeywordPrefix = 2;
|
|
const _tierShortcodeSubstring = 3;
|
|
const _tierKeywordSubstring = 4;
|
|
const _tierSubsequence = 5;
|
|
|
|
/// Sentinel ranking worse than every real tier.
|
|
const _noMatch = 1 << 20;
|
|
|
|
final _separators = RegExp(r'[:_\s-]');
|
|
final _wordSplit = RegExp(r'[\s_-]+');
|
|
|
|
/// Lowercase and drop separators (`:`, `_`, `-`, whitespace) so matching is
|
|
/// insensitive to shortcode punctuation. `point_up` and `pointup` collapse to
|
|
/// the same normalized form.
|
|
///
|
|
/// Named for what it does rather than desktop's `normalizeShortcode`, because
|
|
/// `custom_emoji.dart` already exports a validating `normalizeShortcode` with
|
|
/// different semantics (it returns null for malformed input).
|
|
String collapseSeparators(String value) {
|
|
return value.toLowerCase().replaceAll(_separators, '');
|
|
}
|
|
|
|
/// A ranked match. [score] is a within-tier tiebreak — lower is better.
|
|
class EmojiMatch {
|
|
final int tier;
|
|
final int score;
|
|
|
|
const EmojiMatch({required this.tier, required this.score});
|
|
}
|
|
|
|
/// If [query] is a subsequence of [target] (all chars in order, gaps allowed),
|
|
/// return the span of the match as a tightness score — smaller is tighter.
|
|
/// Returns null when it isn't a subsequence.
|
|
int? _subsequenceSpan(String query, String target) {
|
|
var first = -1;
|
|
var last = -1;
|
|
var qi = 0;
|
|
for (var ti = 0; ti < target.length && qi < query.length; ti++) {
|
|
if (target[ti] == query[qi]) {
|
|
if (first == -1) first = ti;
|
|
last = ti;
|
|
qi++;
|
|
}
|
|
}
|
|
if (qi < query.length) return null;
|
|
return last - first;
|
|
}
|
|
|
|
/// Score [query] against a single [shortcode]. Returns null when there is no
|
|
/// match at any tier. Both sides are normalized first, so separators are
|
|
/// ignored. This is the desktop-identical half of the ranking.
|
|
EmojiMatch? scoreShortcodeMatch(String query, String shortcode) {
|
|
final q = collapseSeparators(query);
|
|
if (q.isEmpty) return null;
|
|
final target = collapseSeparators(shortcode);
|
|
if (target.isEmpty) return null;
|
|
|
|
if (q == target) return const EmojiMatch(tier: _tierExact, score: 0);
|
|
if (target.startsWith(q)) {
|
|
return const EmojiMatch(tier: _tierShortcodePrefix, score: 0);
|
|
}
|
|
final index = target.indexOf(q);
|
|
if (index != -1) {
|
|
return EmojiMatch(tier: _tierShortcodeSubstring, score: index);
|
|
}
|
|
final span = _subsequenceSpan(q, target);
|
|
if (span != null) return EmojiMatch(tier: _tierSubsequence, score: span);
|
|
return null;
|
|
}
|
|
|
|
/// Score [query] against an entry's name and keywords, word by word. Used to
|
|
/// top up shortcode matching so a query like `smiling` finds emoji whose
|
|
/// shortcode doesn't contain it.
|
|
EmojiMatch? _scoreKeywordMatch(
|
|
String query,
|
|
String name,
|
|
List<String> keywords,
|
|
) {
|
|
final q = query.toLowerCase().trim();
|
|
if (q.isEmpty) return null;
|
|
|
|
var bestTier = _noMatch;
|
|
var bestScore = 0;
|
|
|
|
// Earlier words rank better: the name comes before the keywords, and within
|
|
// each the dataset's own order is meaningful.
|
|
void consider(String candidate, int wordIndex) {
|
|
if (bestTier == _tierKeywordPrefix) return;
|
|
final word = candidate.toLowerCase();
|
|
if (word.isEmpty) return;
|
|
final tier = word.startsWith(q)
|
|
? _tierKeywordPrefix
|
|
: word.contains(q)
|
|
? _tierKeywordSubstring
|
|
: _noMatch;
|
|
if (tier < bestTier) {
|
|
bestTier = tier;
|
|
bestScore = wordIndex;
|
|
}
|
|
}
|
|
|
|
var wordIndex = 0;
|
|
for (final word in name.split(_wordSplit)) {
|
|
consider(word, wordIndex++);
|
|
}
|
|
for (final keyword in keywords) {
|
|
for (final word in keyword.split(_wordSplit)) {
|
|
consider(word, wordIndex++);
|
|
}
|
|
}
|
|
|
|
if (bestTier == _noMatch) return null;
|
|
return EmojiMatch(tier: bestTier, score: bestScore);
|
|
}
|
|
|
|
class _Scored<T> {
|
|
final T item;
|
|
final int tier;
|
|
final int score;
|
|
final String code;
|
|
|
|
const _Scored({
|
|
required this.item,
|
|
required this.tier,
|
|
required this.score,
|
|
required this.code,
|
|
});
|
|
}
|
|
|
|
int _compare<T>(_Scored<T> a, _Scored<T> b) {
|
|
if (a.tier != b.tier) return a.tier - b.tier;
|
|
if (a.score != b.score) return a.score - b.score;
|
|
// Shorter shortcode is the more specific match, then alphabetical for a
|
|
// stable result.
|
|
if (a.code.length != b.code.length) return a.code.length - b.code.length;
|
|
return a.code.compareTo(b.code);
|
|
}
|
|
|
|
/// Rank [items] by how well their shortcode matches [query], best first, capped
|
|
/// at [limit]. Mirrors desktop's `rankByShortcode` — used for the custom-emoji
|
|
/// palette, which has no names or keywords.
|
|
List<T> rankByShortcode<T>(
|
|
String query,
|
|
List<T> items,
|
|
String Function(T item) shortcodeOf, {
|
|
int? limit,
|
|
}) {
|
|
if (limit != null && limit <= 0) return const [];
|
|
final scored = <_Scored<T>>[];
|
|
for (final item in items) {
|
|
final code = shortcodeOf(item);
|
|
final match = scoreShortcodeMatch(query, code);
|
|
if (match == null) continue;
|
|
scored.add(
|
|
_Scored(item: item, tier: match.tier, score: match.score, code: code),
|
|
);
|
|
}
|
|
scored.sort(_compare);
|
|
final ranked = scored.map((entry) => entry.item).toList();
|
|
if (limit == null || ranked.length <= limit) return ranked;
|
|
return ranked.sublist(0, limit);
|
|
}
|
|
|
|
/// Rank standard emoji by shortcode, name, and keywords, best first.
|
|
List<EmojiEntry> searchEmoji(
|
|
String query,
|
|
List<EmojiEntry> entries, {
|
|
int? limit,
|
|
}) {
|
|
if (limit != null && limit <= 0) return const [];
|
|
final scored = <_Scored<EmojiEntry>>[];
|
|
for (final entry in entries) {
|
|
final shortcode = scoreShortcodeMatch(query, entry.id);
|
|
final keyword = _scoreKeywordMatch(query, entry.name, entry.keywords);
|
|
final match = switch ((shortcode, keyword)) {
|
|
(null, null) => null,
|
|
(final EmojiMatch only, null) => only,
|
|
(null, final EmojiMatch only) => only,
|
|
(final EmojiMatch a, final EmojiMatch b) => a.tier <= b.tier ? a : b,
|
|
};
|
|
if (match == null) continue;
|
|
scored.add(
|
|
_Scored(
|
|
item: entry,
|
|
tier: match.tier,
|
|
score: match.score,
|
|
code: entry.id,
|
|
),
|
|
);
|
|
}
|
|
scored.sort(_compare);
|
|
final ranked = scored.map((entry) => entry.item).toList();
|
|
if (limit == null || ranked.length <= limit) return ranked;
|
|
return ranked.sublist(0, limit);
|
|
}
|