mirror of
https://github.com/docmost/docmost.git
synced 2026-09-01 03:15:32 +08:00
Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0e5f2972a2 | ||
|
|
1a741ee153 | ||
|
|
a5bba9863d | ||
|
|
78ef95f8a9 | ||
|
|
15eb67b3b0 | ||
|
|
1b73d7668c | ||
|
|
435c4d3129 | ||
|
|
f4cdd72146 | ||
|
|
9797a3671f | ||
|
|
70c8c6cfda |
+5
-3
@@ -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]);
|
||||
@@ -181,7 +181,9 @@ function SearchAndReplaceDialog({ editor, editable = true }: PageFindDialogDialo
|
||||
|
||||
const location = useLocation();
|
||||
useEffect(() => {
|
||||
closeDialog();
|
||||
if(pageFindState.isOpen){
|
||||
closeDialog();
|
||||
}
|
||||
}, [location]);
|
||||
|
||||
return (
|
||||
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
import { ActionIcon, Dialog, Flex, Text, Tooltip } from "@mantine/core";
|
||||
import { IconArrowNarrowDown, IconArrowNarrowUp, IconX } from "@tabler/icons-react";
|
||||
import { useEditor } from "@tiptap/react";
|
||||
import { isEditorReady } from "@docmost/editor-ext";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import classes from "./search-replace.module.css";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
interface SearchNavigationDialogProps {
|
||||
editor: ReturnType<typeof useEditor>;
|
||||
}
|
||||
|
||||
interface SearchNavigationEvent extends CustomEvent {
|
||||
detail: {
|
||||
searchTerms: string[];
|
||||
wholeWord?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
function SearchNavigationDialog({ editor }: SearchNavigationDialogProps) {
|
||||
const {t} = useTranslation()
|
||||
const [open, setOpen] = useState(false);
|
||||
const [resultState, setResultState] = useState({
|
||||
resultIndex: 0,
|
||||
resultsLength: 0,
|
||||
});
|
||||
|
||||
const goToSelection = () => {
|
||||
if (!isEditorReady(editor)) return;
|
||||
|
||||
const { results, resultIndex } = editor.storage.searchAndReplace;
|
||||
const position = results[resultIndex];
|
||||
|
||||
setResultState({
|
||||
resultsLength: results.length,
|
||||
resultIndex,
|
||||
});
|
||||
|
||||
if (!position) return;
|
||||
requestAnimationFrame(() => {
|
||||
document
|
||||
.querySelector(".search-result-current")
|
||||
?.scrollIntoView({ behavior: "smooth", block: "center" });
|
||||
});
|
||||
};
|
||||
|
||||
const next = () => {
|
||||
if (!isEditorReady(editor)) return;
|
||||
editor.commands.nextSearchResult();
|
||||
goToSelection();
|
||||
};
|
||||
|
||||
const previous = () => {
|
||||
if (!isEditorReady(editor)) return;
|
||||
editor.commands.previousSearchResult();
|
||||
goToSelection();
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const handleOpen = (event: Event) => {
|
||||
const { searchTerms: terms, wholeWord = true } = (
|
||||
event as SearchNavigationEvent
|
||||
).detail;
|
||||
|
||||
if (!terms?.length || !isEditorReady(editor)) return;
|
||||
|
||||
setOpen(true);
|
||||
editor.commands.setSearchTerms(terms);
|
||||
editor.commands.setWholeWord(wholeWord);
|
||||
editor.commands.resetIndex();
|
||||
|
||||
const { results, resultIndex } = editor.storage.searchAndReplace;
|
||||
setResultState({
|
||||
resultIndex,
|
||||
resultsLength: results.length,
|
||||
});
|
||||
|
||||
goToSelection();
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
document.addEventListener("openSearchNavigationDialog", handleOpen);
|
||||
document.addEventListener("openFindDialogFromEditor", handleClose);
|
||||
|
||||
return () => {
|
||||
document.removeEventListener("openSearchNavigationDialog", handleOpen);
|
||||
document.removeEventListener("openFindDialogFromEditor", handleClose);
|
||||
};
|
||||
}, [editor]);
|
||||
|
||||
useEffect(() => {
|
||||
const handleTransaction = () => {
|
||||
if (!open || editor.isDestroyed) return;
|
||||
|
||||
const { results } = editor.storage.searchAndReplace;
|
||||
if (results.length === 0) {
|
||||
setOpen(false);
|
||||
}
|
||||
};
|
||||
|
||||
editor.on("transaction", handleTransaction);
|
||||
return () => {
|
||||
editor.off("transaction", handleTransaction);
|
||||
};
|
||||
}, [editor, open]);
|
||||
|
||||
const close = () => {
|
||||
editor.commands.setSearchTerms([""]);
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
className={classes.findDialog}
|
||||
opened={open}
|
||||
size="xs"
|
||||
radius="md"
|
||||
w="auto"
|
||||
position={{ top: 90, right: 50 }}
|
||||
withBorder
|
||||
aria-label="Search navigation"
|
||||
>
|
||||
<Flex align="center" gap="xs">
|
||||
<Text size="xs" style={{ flex: 1 }}>
|
||||
{resultState.resultsLength > 0
|
||||
? `${resultState.resultIndex + 1}/${resultState.resultsLength}`
|
||||
: t("Not found")}
|
||||
</Text>
|
||||
<Tooltip label="Previous match">
|
||||
<ActionIcon
|
||||
variant="subtle"
|
||||
color="gray"
|
||||
onClick={previous}
|
||||
aria-label="Previous match"
|
||||
disabled={resultState.resultsLength === 0}
|
||||
>
|
||||
<IconArrowNarrowUp size={16} />
|
||||
</ActionIcon>
|
||||
</Tooltip>
|
||||
<Tooltip label="Next match">
|
||||
<ActionIcon
|
||||
variant="subtle"
|
||||
color="gray"
|
||||
onClick={next}
|
||||
aria-label="Next match"
|
||||
disabled={resultState.resultsLength === 0}
|
||||
>
|
||||
<IconArrowNarrowDown size={16} />
|
||||
</ActionIcon>
|
||||
</Tooltip>
|
||||
<Tooltip label="Close">
|
||||
<ActionIcon
|
||||
variant="subtle"
|
||||
color="gray"
|
||||
onClick={close}
|
||||
aria-label="Close"
|
||||
>
|
||||
<IconX size={16} />
|
||||
</ActionIcon>
|
||||
</Tooltip>
|
||||
</Flex>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
export default SearchNavigationDialog;
|
||||
@@ -61,11 +61,12 @@ import ExcalidrawMenu from "./components/excalidraw/excalidraw-menu-lazy";
|
||||
import DrawioMenu from "./components/drawio/drawio-menu";
|
||||
import { useCollabToken } from "@/features/auth/queries/auth-query.tsx";
|
||||
import SearchAndReplaceDialog from "@/features/editor/components/search-and-replace/search-and-replace-dialog.tsx";
|
||||
import SearchNavigationDialog from "@/features/editor/components/search-and-replace/search-navigation-dialog.tsx";
|
||||
import { useDebouncedCallback, useDocumentVisibility } from "@mantine/hooks";
|
||||
import { useIdle } from "@/hooks/use-idle.ts";
|
||||
import { queryClient } from "@/main.tsx";
|
||||
import { IPage } from "@/features/page/types/page.types.ts";
|
||||
import { useParams } from "react-router-dom";
|
||||
import { useParams, useSearchParams } from "react-router-dom";
|
||||
import { extractPageSlugId, platformModifierKey } from "@/lib";
|
||||
import { FIVE_MINUTES } from "@/lib/constants.ts";
|
||||
import { PageEditMode } from "@/features/user/types/user.types.ts";
|
||||
@@ -194,6 +195,7 @@ function CollabPageEditor({
|
||||
const { isIdle, resetIdle } = useIdle(FIVE_MINUTES, { initialState: false });
|
||||
const documentState = useDocumentVisibility();
|
||||
const { pageSlug } = useParams();
|
||||
const [searchParams] = useSearchParams();
|
||||
const slugId = extractPageSlugId(pageSlug);
|
||||
const currentPageEditMode = useAtomValue(currentPageEditModeAtom);
|
||||
const canScroll = useCallback(
|
||||
@@ -415,6 +417,32 @@ function CollabPageEditor({
|
||||
const hasConnectedOnceRef = useRef(false);
|
||||
const [showStatic, setShowStatic] = useState(true);
|
||||
|
||||
const appliedSearchKeyRef = useRef<string | null>(null);
|
||||
const searchKey = `${pageId}:${searchParams.toString()}`;
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
!editor ||
|
||||
editor.isDestroyed ||
|
||||
!editor.view.dom.isConnected ||
|
||||
appliedSearchKeyRef.current === searchKey
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const searchQueries = searchParams.getAll("q");
|
||||
const match = searchParams.get("m");
|
||||
if (!searchQueries.length) return;
|
||||
|
||||
appliedSearchKeyRef.current = searchKey;
|
||||
|
||||
document.dispatchEvent(
|
||||
new CustomEvent("openSearchNavigationDialog", {
|
||||
detail: { searchTerms: searchQueries, wholeWord: match === "whole" },
|
||||
})
|
||||
);
|
||||
}, [editor, isSynced, showStatic, searchKey, searchParams]);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
!hasConnectedOnceRef.current &&
|
||||
@@ -438,6 +466,7 @@ function CollabPageEditor({
|
||||
{editor && (
|
||||
<SearchAndReplaceDialog editor={editor} editable={editable} />
|
||||
)}
|
||||
{editor && <SearchNavigationDialog editor={editor} />}
|
||||
|
||||
{editor && editorIsEditable && (
|
||||
<div>
|
||||
|
||||
@@ -25,11 +25,37 @@ const buildPageSlug = (pageSlugId: string, pageTitle?: string): string => {
|
||||
return `${titleSlug}-${pageSlugId}`;
|
||||
};
|
||||
|
||||
function appendSearchParams(
|
||||
url: string,
|
||||
search?: string[],
|
||||
wholeWord?: boolean,
|
||||
): string {
|
||||
if(search?.length === 0){
|
||||
return url;
|
||||
}
|
||||
|
||||
const params = new URLSearchParams();
|
||||
search
|
||||
?.map((term) => term.trim())
|
||||
.filter(Boolean)
|
||||
.forEach((term) => params.append("q", term));
|
||||
|
||||
if (wholeWord) {
|
||||
params.set("m", "whole");
|
||||
}
|
||||
|
||||
const queryString = params.toString();
|
||||
|
||||
return queryString ? `${url}?${queryString}` : url;
|
||||
}
|
||||
|
||||
export const buildPageUrl = (
|
||||
spaceName: string,
|
||||
pageSlugId: string,
|
||||
pageTitle?: string,
|
||||
anchorId?: string,
|
||||
search?: string[],
|
||||
wholeWord?: boolean,
|
||||
): string => {
|
||||
let url: string;
|
||||
if (spaceName === undefined) {
|
||||
@@ -37,6 +63,9 @@ export const buildPageUrl = (
|
||||
} else {
|
||||
url = `/s/${spaceName}/p/${buildPageSlug(pageSlugId, pageTitle)}`;
|
||||
}
|
||||
|
||||
url = appendSearchParams(url, search, wholeWord);
|
||||
|
||||
return anchorId ? `${url}#${anchorId}` : url;
|
||||
};
|
||||
|
||||
@@ -45,13 +74,18 @@ export const buildSharedPageUrl = (opts: {
|
||||
pageSlugId: string;
|
||||
pageTitle?: string;
|
||||
anchorId?: string;
|
||||
search?: string[];
|
||||
wholeWord?: boolean;
|
||||
}): string => {
|
||||
const { shareId, pageSlugId, pageTitle, anchorId } = opts;
|
||||
const { shareId, pageSlugId, pageTitle, anchorId, search, wholeWord } = opts;
|
||||
let url: string;
|
||||
if (!shareId) {
|
||||
url = `/share/p/${buildPageSlug(pageSlugId, pageTitle)}`;
|
||||
} else {
|
||||
url = `/share/${shareId}/p/${buildPageSlug(pageSlugId, pageTitle)}`;
|
||||
}
|
||||
|
||||
url = appendSearchParams(url, search, wholeWord);
|
||||
|
||||
return anchorId ? `${url}#${anchorId}` : url;
|
||||
};
|
||||
|
||||
@@ -101,6 +101,9 @@ export function SearchResultItem({
|
||||
pageResult.space.slug,
|
||||
pageResult.slugId,
|
||||
pageResult.title,
|
||||
undefined,
|
||||
pageResult.matchedText,
|
||||
pageResult.wholeWord
|
||||
)}
|
||||
style={{ userSelect: "none" }}
|
||||
>
|
||||
|
||||
@@ -14,6 +14,8 @@ export interface IPageSearch {
|
||||
updatedAt: Date;
|
||||
rank: string;
|
||||
highlight: string;
|
||||
matchedText: string[];
|
||||
wholeWord: boolean;
|
||||
space: Partial<ISpace>;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ export class SearchResponseDto {
|
||||
creatorId: string;
|
||||
rank: number;
|
||||
highlight: string;
|
||||
matchedText: string[];
|
||||
wholeWord: boolean;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
space: Partial<Space>;
|
||||
|
||||
@@ -140,11 +140,25 @@ 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, ' ');
|
||||
result.wholeWord = true
|
||||
if (!result.highlight) {
|
||||
result.matchedText = [];
|
||||
return result;
|
||||
}
|
||||
|
||||
result.highlight = result.highlight
|
||||
.replace(/\r\n|\r|\n/g, ' ')
|
||||
.replace(/\s+/g, ' ');
|
||||
|
||||
result.matchedText = [
|
||||
...new Set(
|
||||
Array.from(
|
||||
result.highlight.matchAll(/<b>([^<]*)<\/b>/gi),
|
||||
(match) => match[1],
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
|
||||
+1
-1
Submodule apps/server/src/ee updated: 6dfbcb9241...13ca295ed9
@@ -39,7 +39,11 @@ declare module "@tiptap/core" {
|
||||
/**
|
||||
* @description Set search term in extension.
|
||||
*/
|
||||
setSearchTerm: (searchTerm: string) => ReturnType;
|
||||
setSearchTerms: (searchTerms: string[]) => ReturnType;
|
||||
/**
|
||||
* @description Set whole word search in extension.
|
||||
*/
|
||||
setWholeWord: (wholeWord: boolean) => ReturnType;
|
||||
/**
|
||||
* @description Set replace term in extension.
|
||||
*/
|
||||
@@ -82,13 +86,26 @@ interface TextNodesWithPosition {
|
||||
}
|
||||
|
||||
const getRegex = (
|
||||
s: string,
|
||||
searchTerms: string[],
|
||||
disableRegex: boolean,
|
||||
caseSensitive: boolean,
|
||||
wholeWord: boolean,
|
||||
): RegExp => {
|
||||
return RegExp(
|
||||
disableRegex ? s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") : s,
|
||||
caseSensitive ? "gu" : "gui",
|
||||
const terms = searchTerms.filter(Boolean).sort((a, b) => b.length - a.length);
|
||||
|
||||
const pattern = terms
|
||||
.map((term) =>
|
||||
disableRegex ? term.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") : term,
|
||||
)
|
||||
.join("|");
|
||||
|
||||
const finalPattern = wholeWord
|
||||
? `(?<![\\p{L}\\p{N}_])(?:${pattern})(?![\\p{L}\\p{N}_])`
|
||||
: pattern;
|
||||
|
||||
return new RegExp(
|
||||
finalPattern,
|
||||
caseSensitive ? "gu" : "giu",
|
||||
);
|
||||
};
|
||||
|
||||
@@ -255,12 +272,14 @@ export interface SearchAndReplaceOptions {
|
||||
}
|
||||
|
||||
export interface SearchAndReplaceStorage {
|
||||
searchTerm: string;
|
||||
searchTerms: string[];
|
||||
replaceTerm: string;
|
||||
results: Range[];
|
||||
lastSearchTerm: string;
|
||||
lastSearchTerms: string[];
|
||||
caseSensitive: boolean;
|
||||
lastCaseSensitive: boolean;
|
||||
wholeWord: boolean;
|
||||
lastWholeWord: boolean;
|
||||
resultIndex: number;
|
||||
lastResultIndex: number;
|
||||
}
|
||||
@@ -280,11 +299,13 @@ export const SearchAndReplace = Extension.create<
|
||||
|
||||
addStorage() {
|
||||
return {
|
||||
searchTerm: "",
|
||||
searchTerms: [],
|
||||
replaceTerm: "",
|
||||
results: [],
|
||||
lastSearchTerm: "",
|
||||
lastSearchTerms: [],
|
||||
caseSensitive: false,
|
||||
wholeWord: false,
|
||||
lastWholeWord: false,
|
||||
lastCaseSensitive: false,
|
||||
resultIndex: 0,
|
||||
lastResultIndex: 0,
|
||||
@@ -293,10 +314,21 @@ export const SearchAndReplace = Extension.create<
|
||||
|
||||
addCommands() {
|
||||
return {
|
||||
setSearchTerm:
|
||||
(searchTerm: string) =>
|
||||
setSearchTerms:
|
||||
(searchTerms: string[]) =>
|
||||
({ editor }) => {
|
||||
editor.storage.searchAndReplace.searchTerm = searchTerm;
|
||||
editor.storage.searchAndReplace.searchTerms = searchTerms.filter(Boolean);
|
||||
|
||||
// clear whole word by default
|
||||
// should remove if whole word toggle is added to search and replace dialog
|
||||
editor.storage.searchAndReplace.wholeWord = false;
|
||||
|
||||
return false;
|
||||
},
|
||||
setWholeWord:
|
||||
(wholeWord: boolean) =>
|
||||
({ editor }) => {
|
||||
editor.storage.searchAndReplace.wholeWord = wholeWord;
|
||||
|
||||
return false;
|
||||
},
|
||||
@@ -409,8 +441,10 @@ export const SearchAndReplace = Extension.create<
|
||||
const editor = this.editor;
|
||||
const { searchResultClass, disableRegex } = this.options;
|
||||
|
||||
const setLastSearchTerm = (t: string) =>
|
||||
(editor.storage.searchAndReplace.lastSearchTerm = t);
|
||||
const setLastSearchTerms = (terms: string[]) =>
|
||||
(editor.storage.searchAndReplace.lastSearchTerms = [...terms]);
|
||||
const setLastWholeWord = (t: boolean) =>
|
||||
(editor.storage.searchAndReplace.lastWholeWord = t);
|
||||
const setLastCaseSensitive = (t: boolean) =>
|
||||
(editor.storage.searchAndReplace.lastCaseSensitive = t);
|
||||
const setLastResultIndex = (t: number) =>
|
||||
@@ -425,37 +459,47 @@ export const SearchAndReplace = Extension.create<
|
||||
const storage = editor.storage.searchAndReplace;
|
||||
if (!storage) return oldState;
|
||||
const {
|
||||
searchTerm,
|
||||
lastSearchTerm,
|
||||
caseSensitive,
|
||||
searchTerms,
|
||||
lastCaseSensitive,
|
||||
lastSearchTerms,
|
||||
caseSensitive,
|
||||
wholeWord,
|
||||
lastWholeWord,
|
||||
resultIndex,
|
||||
lastResultIndex,
|
||||
} = storage;
|
||||
|
||||
if (
|
||||
!docChanged &&
|
||||
lastSearchTerm === searchTerm &&
|
||||
searchTerms.length === lastSearchTerms.length &&
|
||||
searchTerms.every((term, index) => term === lastSearchTerms[index]) &&
|
||||
lastCaseSensitive === caseSensitive &&
|
||||
lastWholeWord === wholeWord &&
|
||||
lastResultIndex === resultIndex
|
||||
)
|
||||
return oldState;
|
||||
|
||||
setLastSearchTerm(searchTerm);
|
||||
setLastSearchTerms(searchTerms);
|
||||
setLastCaseSensitive(caseSensitive);
|
||||
setLastWholeWord(wholeWord);
|
||||
setLastResultIndex(resultIndex);
|
||||
|
||||
if (!searchTerm) {
|
||||
if (searchTerms.length === 0) {
|
||||
editor.storage.searchAndReplace.results = [];
|
||||
return DecorationSet.empty;
|
||||
}
|
||||
|
||||
const { decorationsToReturn, results } = processSearches(
|
||||
doc,
|
||||
getRegex(searchTerm, disableRegex, caseSensitive),
|
||||
searchResultClass,
|
||||
resultIndex,
|
||||
);
|
||||
doc,
|
||||
getRegex(
|
||||
searchTerms,
|
||||
disableRegex,
|
||||
caseSensitive,
|
||||
wholeWord,
|
||||
),
|
||||
searchResultClass,
|
||||
resultIndex,
|
||||
);
|
||||
|
||||
editor.storage.searchAndReplace.results = results;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user