mirror of
https://github.com/docmost/docmost.git
synced 2026-09-01 03:15:32 +08:00
support multiple text matching
This commit is contained in:
+2
-2
@@ -66,7 +66,7 @@ function SearchAndReplaceDialog({ editor, editable = true }: PageFindDialogDialo
|
||||
}
|
||||
// Clear search term in editor
|
||||
if (isEditorReady(editor)) {
|
||||
editor.commands.setSearchTerm("");
|
||||
editor.commands.setSearchTerms([""]);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -117,7 +117,7 @@ function SearchAndReplaceDialog({ editor, editable = true }: PageFindDialogDialo
|
||||
|
||||
useEffect(() => {
|
||||
if (!isEditorReady(editor)) return;
|
||||
editor.commands.setSearchTerm(searchText);
|
||||
editor.commands.setSearchTerms([searchText]);
|
||||
editor.commands.resetIndex();
|
||||
editor.commands.selectCurrentItem();
|
||||
}, [searchText]);
|
||||
|
||||
@@ -195,7 +195,7 @@ function CollabPageEditor({
|
||||
const documentState = useDocumentVisibility();
|
||||
const { pageSlug } = useParams();
|
||||
const [searchParams] = useSearchParams();
|
||||
const q = searchParams.get("q");
|
||||
const searchQueries = searchParams.getAll("q")
|
||||
const slugId = extractPageSlugId(pageSlug);
|
||||
const currentPageEditMode = useAtomValue(currentPageEditModeAtom);
|
||||
const canScroll = useCallback(
|
||||
@@ -422,12 +422,13 @@ function CollabPageEditor({
|
||||
useEffect(() => {
|
||||
if (!editor || editor.isDestroyed) return;
|
||||
if (showStatic || !isSynced) return;
|
||||
if (!q?.trim() || appliedSearchRef.current) return;
|
||||
if (!searchQueries.length || appliedSearchRef.current) return;
|
||||
|
||||
const frame = requestAnimationFrame(() => {
|
||||
if (!editor || editor.isDestroyed || !editor.view.dom.isConnected) return;
|
||||
|
||||
editor.commands.setSearchTerm(q);
|
||||
editor.commands.setSearchTerms(searchQueries);
|
||||
editor.commands.setWholeWord(true);
|
||||
editor.commands.resetIndex();
|
||||
|
||||
const { results, resultIndex } = editor.storage.searchAndReplace;
|
||||
@@ -437,8 +438,6 @@ function CollabPageEditor({
|
||||
|
||||
appliedSearchRef.current = true;
|
||||
|
||||
editor.commands.setSearchTerm(q)
|
||||
editor.commands.setWholeWord(true);
|
||||
const element = document.querySelector(".search-result-current");
|
||||
element?.scrollIntoView({ behavior: "smooth", block: "center" });
|
||||
editor.commands.setTextSelection(0)
|
||||
@@ -446,7 +445,7 @@ function CollabPageEditor({
|
||||
|
||||
|
||||
return () => cancelAnimationFrame(frame);
|
||||
}, [editor, isSynced, showStatic, q]);
|
||||
}, [editor, isSynced, showStatic, searchQueries]);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
|
||||
@@ -30,7 +30,7 @@ export const buildPageUrl = (
|
||||
pageSlugId: string,
|
||||
pageTitle?: string,
|
||||
anchorId?: string,
|
||||
searchString?: string
|
||||
search?: string[],
|
||||
): string => {
|
||||
let url: string;
|
||||
if (spaceName === undefined) {
|
||||
@@ -39,8 +39,19 @@ export const buildPageUrl = (
|
||||
url = `/s/${spaceName}/p/${buildPageSlug(pageSlugId, pageTitle)}`;
|
||||
}
|
||||
|
||||
if (searchString) {
|
||||
url += `?q=${encodeURIComponent(searchString)}`;
|
||||
if (search?.length > 0) {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
search
|
||||
.map((term) => term.trim())
|
||||
.filter(Boolean)
|
||||
.forEach((term) => params.append("q", term));
|
||||
|
||||
const queryString = params.toString();
|
||||
|
||||
if (queryString) {
|
||||
url += `?${queryString}`;
|
||||
}
|
||||
}
|
||||
|
||||
return anchorId ? `${url}#${anchorId}` : url;
|
||||
@@ -51,9 +62,9 @@ export const buildSharedPageUrl = (opts: {
|
||||
pageSlugId: string;
|
||||
pageTitle?: string;
|
||||
anchorId?: string;
|
||||
searchString?: string
|
||||
search?: string[]
|
||||
}): string => {
|
||||
const { shareId, pageSlugId, pageTitle, anchorId, searchString } = opts;
|
||||
const { shareId, pageSlugId, pageTitle, anchorId, search } = opts;
|
||||
let url: string;
|
||||
if (!shareId) {
|
||||
url = `/share/p/${buildPageSlug(pageSlugId, pageTitle)}`;
|
||||
@@ -61,8 +72,19 @@ export const buildSharedPageUrl = (opts: {
|
||||
url = `/share/${shareId}/p/${buildPageSlug(pageSlugId, pageTitle)}`;
|
||||
}
|
||||
|
||||
if (searchString) {
|
||||
url += `?q=${encodeURIComponent(searchString)}`;
|
||||
if (search?.length > 0) {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
search
|
||||
.map((term) => term.trim())
|
||||
.filter(Boolean)
|
||||
.forEach((term) => params.append("q", term));
|
||||
|
||||
const queryString = params.toString();
|
||||
|
||||
if (queryString) {
|
||||
url += `?${queryString}`;
|
||||
}
|
||||
}
|
||||
|
||||
return anchorId ? `${url}#${anchorId}` : url;
|
||||
|
||||
@@ -14,7 +14,7 @@ export interface IPageSearch {
|
||||
updatedAt: Date;
|
||||
rank: string;
|
||||
highlight: string;
|
||||
matchedText: string;
|
||||
matchedText: string[];
|
||||
space: Partial<ISpace>;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ export class SearchResponseDto {
|
||||
creatorId: string;
|
||||
rank: number;
|
||||
highlight: string;
|
||||
matchedText?: string;
|
||||
matchedText?: string[];
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
space: Partial<Space>;
|
||||
|
||||
@@ -140,16 +140,20 @@ export class SearchService {
|
||||
|
||||
//@ts-ignore
|
||||
const searchResults = results.map((result: SearchResponseDto) => {
|
||||
if (result.highlight) {
|
||||
result.highlight = result.highlight
|
||||
.replace(/\r\n|\r|\n/g, ' ')
|
||||
.replace(/\s+/g, ' ');
|
||||
|
||||
const matchedText = result.highlight.match(/<b>([^<]*)<\/b>/i);
|
||||
result.matchedText = matchedText?.[1] ?? null;
|
||||
} else {
|
||||
result.matchedText = null;
|
||||
if (!result.highlight) {
|
||||
result.matchedText = [];
|
||||
return result;
|
||||
}
|
||||
|
||||
result.highlight = result.highlight
|
||||
.replace(/\r\n|\r|\n/g, ' ')
|
||||
.replace(/\s+/g, ' ');
|
||||
|
||||
result.matchedText = Array.from(
|
||||
result.highlight.matchAll(/<b>([^<]*)<\/b>/gi),
|
||||
(match) => match[1],
|
||||
);
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user