mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
feat: read content from search params (#1607)
This commit is contained in:
@ -14,6 +14,7 @@ import ResourceIcon from "./ResourceIcon";
|
|||||||
import showResourcesSelectorDialog from "./ResourcesSelectorDialog";
|
import showResourcesSelectorDialog from "./ResourcesSelectorDialog";
|
||||||
import showCreateResourceDialog from "./CreateResourceDialog";
|
import showCreateResourceDialog from "./CreateResourceDialog";
|
||||||
import "@/less/memo-editor.less";
|
import "@/less/memo-editor.less";
|
||||||
|
import { clearContentQueryParam, getContentQueryParam } from "@/helpers/utils";
|
||||||
|
|
||||||
const listItemSymbolList = ["- [ ] ", "- [x] ", "- [X] ", "* ", "- "];
|
const listItemSymbolList = ["- [ ] ", "- [x] ", "- [X] ", "* ", "- "];
|
||||||
const emptyOlReg = /^(\d+)\. $/;
|
const emptyOlReg = /^(\d+)\. $/;
|
||||||
@ -22,6 +23,10 @@ const getEditorContentCache = (): string => {
|
|||||||
return storage.get(["editorContentCache"]).editorContentCache ?? "";
|
return storage.get(["editorContentCache"]).editorContentCache ?? "";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getInitialContent = (): string => {
|
||||||
|
return getContentQueryParam() ?? getEditorContentCache();
|
||||||
|
};
|
||||||
|
|
||||||
const setEditorContentCache = (content: string) => {
|
const setEditorContentCache = (content: string) => {
|
||||||
storage.set({
|
storage.set({
|
||||||
editorContentCache: content,
|
editorContentCache: content,
|
||||||
@ -286,6 +291,7 @@ const MemoEditor = () => {
|
|||||||
editorStore.clearResourceList();
|
editorStore.clearResourceList();
|
||||||
setEditorContentCache("");
|
setEditorContentCache("");
|
||||||
editorRef.current?.setContent("");
|
editorRef.current?.setContent("");
|
||||||
|
clearContentQueryParam();
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCancelEdit = () => {
|
const handleCancelEdit = () => {
|
||||||
@ -378,7 +384,7 @@ const MemoEditor = () => {
|
|||||||
const editorConfig = useMemo(
|
const editorConfig = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
className: `memo-editor`,
|
className: `memo-editor`,
|
||||||
initialContent: getEditorContentCache(),
|
initialContent: getInitialContent(),
|
||||||
placeholder: t("editor.placeholder"),
|
placeholder: t("editor.placeholder"),
|
||||||
fullscreen: state.fullscreen,
|
fullscreen: state.fullscreen,
|
||||||
onContentChange: handleContentChange,
|
onContentChange: handleContentChange,
|
||||||
|
@ -105,3 +105,18 @@ export const formatBytes = (bytes: number) => {
|
|||||||
i = Math.floor(Math.log(bytes) / Math.log(k));
|
i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getContentQueryParam = (): string | undefined => {
|
||||||
|
const urlParams = new URLSearchParams(window.location.search);
|
||||||
|
return urlParams.get("content") ?? undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const clearContentQueryParam = () => {
|
||||||
|
const urlParams = new URLSearchParams(window.location.search);
|
||||||
|
urlParams.delete("content");
|
||||||
|
let url = window.location.pathname;
|
||||||
|
if (urlParams.toString()) {
|
||||||
|
url += `?${urlParams.toString()}`;
|
||||||
|
}
|
||||||
|
window.history.replaceState({}, "", url);
|
||||||
|
};
|
||||||
|
Reference in New Issue
Block a user