feat(elasticsearch): add response content search

This commit is contained in:
onenewcode authored and GitHub committed 2026-08-06 17:09:15 +08:00
1 parent b7606ceee3
commit 939c8d493d
7 files changed
+508 -113

No files matched your search

@@ -0,0 +1,90 @@
/** Shared in-content find helpers for read-only text surfaces. */
export const TEXT_CONTENT_SEARCH_MATCH_LIMIT = 1000;
export const TEXT_CONTENT_SEARCH_FULL_HIGHLIGHT_MAX_CHARS = 256_000;
export interface TextContentMatch {
start: number;
end: number;
}
export interface TextContentSearchRenderOptions {
activeMatchIndex?: number;
matchClass?: string;
activeClass?: string;
matchAttribute?: (index: number) => string;
activeAttribute?: (index: number) => string;
}
export function findTextContentMatches(text: string, query: string, limit = TEXT_CONTENT_SEARCH_MATCH_LIMIT): TextContentMatch[] {
if (!query || limit <= 0) return [];
// Match the original UTF-16 text so Unicode case folding keeps offsets valid.
const pattern = new RegExp(escapeRegExp(query), "giu");
const matches: TextContentMatch[] = [];
for (const match of text.matchAll(pattern)) {
matches.push({ start: match.index, end: match.index + match[0].length });
if (matches.length >= limit) break;
}
return matches;
}
export function textContentSearchStatus(activeIndex: number, matchCount: number, limited = false): string {
if (matchCount <= 0) return "0/0";
return `${activeIndex + 1}/${limited ? `${matchCount}+` : matchCount}`;
}
export function nextTextContentSearchMatchIndex(current: number, delta: -1 | 1, count: number): number {
if (count <= 0) return 0;
return (current + delta + count) % count;
}
export function canFullHighlightTextContent(textLength: number): boolean {
return textLength <= TEXT_CONTENT_SEARCH_FULL_HIGHLIGHT_MAX_CHARS;
}
export function renderTextContentMatchesHtml(text: string, matches: readonly TextContentMatch[], options: TextContentSearchRenderOptions = {}): string {
if (matches.length === 0) return escapeTextContentHtml(text);
const activeMatchIndex = options.activeMatchIndex ?? -1;
const matchClass = options.matchClass ?? "document-search-match";
const activeClass = options.activeClass ?? "document-search-match-active";
const matchAttribute = options.matchAttribute ?? ((index) => `data-document-search-match="${index}"`);
const activeAttribute = options.activeAttribute ?? (() => 'data-document-search-active="true"');
let html = "";
let cursor = 0;
for (let index = 0; index < matches.length; index += 1) {
const match = matches[index];
if (match.start > cursor) html += escapeTextContentHtml(text.slice(cursor, match.start));
const active = index === activeMatchIndex;
const className = active ? `${matchClass} ${activeClass}` : matchClass;
const attributes = `${matchAttribute(index)}${active ? ` ${activeAttribute(index)}` : ""}`;
html += `<mark class="${className}" ${attributes}>${escapeTextContentHtml(text.slice(match.start, match.end))}</mark>`;
cursor = match.end;
}
if (cursor < text.length) html += escapeTextContentHtml(text.slice(cursor));
return html;
}
export function renderTextContentSearchHtml(text: string, query: string, activeMatchIndex = 0, limit = TEXT_CONTENT_SEARCH_MATCH_LIMIT): string {
if (!query || !canFullHighlightTextContent(text.length)) return escapeTextContentHtml(text);
return renderTextContentMatchesHtml(text, findTextContentMatches(text, query, limit), { activeMatchIndex });
}
/** Grip is a <button>; allow it before blocking other interactive controls. */
export function isTextContentSearchDragSource(target: EventTarget | null): boolean {
if (target == null || typeof target !== "object") return false;
const el = target as { closest?: (selector: string) => unknown };
if (typeof el.closest !== "function") return false;
if (el.closest("[data-drag-handle]")) return true;
if (el.closest("[data-search-drag-chrome]")) return true;
if (el.closest("input, textarea, select, button, a, [data-no-drag]")) return false;
return !!el.closest("[data-draggable-search-panel]");
}
export function escapeTextContentHtml(text: string): string {
return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
function escapeRegExp(text: string): string {
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
+11 -73
View File
@@ -1,74 +1,12 @@
/** Match helpers for Redis STRING value Ctrl+F (in-content find only). */
/** Compatibility exports for Redis STRING value Ctrl+F. */
import { TEXT_CONTENT_SEARCH_FULL_HIGHLIGHT_MAX_CHARS, TEXT_CONTENT_SEARCH_MATCH_LIMIT, canFullHighlightTextContent, findTextContentMatches, nextTextContentSearchMatchIndex, renderTextContentSearchHtml, textContentSearchStatus, type TextContentMatch } from "@/lib/common/textContentSearch";
export const REDIS_VALUE_SEARCH_MATCH_LIMIT = 1000;
export const REDIS_VALUE_SEARCH_FULL_HIGHLIGHT_MAX_CHARS = 256_000;
export interface RedisTextMatch {
start: number;
end: number;
}
export function findRedisTextMatches(text: string, query: string, limit = REDIS_VALUE_SEARCH_MATCH_LIMIT): RedisTextMatch[] {
if (!query || limit <= 0) return [];
// Match against the original UTF-16 text so Unicode case folding cannot
// change string length and corrupt the offsets used for highlighting.
const pattern = new RegExp(escapeRegExp(query), "giu");
const matches: RedisTextMatch[] = [];
for (const match of text.matchAll(pattern)) {
const start = match.index;
matches.push({ start, end: start + match[0].length });
if (matches.length >= limit) break;
}
return matches;
}
export function redisValueSearchStatus(activeIndex: number, matchCount: number, limited = false): string {
if (matchCount <= 0) return "0/0";
return `${activeIndex + 1}/${limited ? `${matchCount}+` : matchCount}`;
}
export function nextRedisSearchMatchIndex(current: number, delta: -1 | 1, count: number): number {
if (count <= 0) return 0;
return (current + delta + count) % count;
}
export function canFullHighlightRedisText(textLength: number): boolean {
return textLength <= REDIS_VALUE_SEARCH_FULL_HIGHLIGHT_MAX_CHARS;
}
export function renderRedisTextSearchHtml(text: string, query: string, activeMatchIndex = 0, limit = REDIS_VALUE_SEARCH_MATCH_LIMIT): string {
if (!query || !canFullHighlightRedisText(text.length)) return escapeHtml(text);
const matches = findRedisTextMatches(text, query, limit);
if (matches.length === 0) return escapeHtml(text);
let html = "";
let cursor = 0;
for (let index = 0; index < matches.length; index += 1) {
const match = matches[index];
if (match.start > cursor) html += escapeHtml(text.slice(cursor, match.start));
const activeClass = index === activeMatchIndex ? " document-search-match-active" : "";
const activeAttribute = index === activeMatchIndex ? ' data-document-search-active="true"' : "";
html += `<mark class="document-search-match${activeClass}" data-document-search-match="${index}"${activeAttribute}>${escapeHtml(text.slice(match.start, match.end))}</mark>`;
cursor = match.end;
}
if (cursor < text.length) html += escapeHtml(text.slice(cursor));
return html;
}
/** Grip is a <button>; allow it before blocking other buttons. */
export function isTextContentSearchDragSource(target: EventTarget | null): boolean {
if (target == null || typeof target !== "object") return false;
const el = target as { closest?: (selector: string) => unknown };
if (typeof el.closest !== "function") return false;
if (el.closest("[data-drag-handle]")) return true;
if (el.closest("[data-search-drag-chrome]")) return true;
if (el.closest("input, textarea, select, button, a, [data-no-drag]")) return false;
return !!el.closest("[data-draggable-search-panel]");
}
function escapeHtml(text: string): string {
return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
function escapeRegExp(text: string): string {
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
export const REDIS_VALUE_SEARCH_MATCH_LIMIT = TEXT_CONTENT_SEARCH_MATCH_LIMIT;
export const REDIS_VALUE_SEARCH_FULL_HIGHLIGHT_MAX_CHARS = TEXT_CONTENT_SEARCH_FULL_HIGHLIGHT_MAX_CHARS;
export type RedisTextMatch = TextContentMatch;
export const findRedisTextMatches = findTextContentMatches;
export const redisValueSearchStatus = textContentSearchStatus;
export const nextRedisSearchMatchIndex = nextTextContentSearchMatchIndex;
export const canFullHighlightRedisText = canFullHighlightTextContent;
export const renderRedisTextSearchHtml = renderTextContentSearchHtml;
export { isTextContentSearchDragSource } from "@/lib/common/textContentSearch";