From dbb544dc9243e82ac87360865e6c7c269848ab3c Mon Sep 17 00:00:00 2001 From: Max Malm Date: Thu, 27 Apr 2023 01:15:40 +0200 Subject: [PATCH] feat: read content from search params (#1607) --- web/src/components/MemoEditor.tsx | 8 +++++++- web/src/helpers/utils.ts | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/web/src/components/MemoEditor.tsx b/web/src/components/MemoEditor.tsx index c06dc626..65b03ad0 100644 --- a/web/src/components/MemoEditor.tsx +++ b/web/src/components/MemoEditor.tsx @@ -14,6 +14,7 @@ import ResourceIcon from "./ResourceIcon"; import showResourcesSelectorDialog from "./ResourcesSelectorDialog"; import showCreateResourceDialog from "./CreateResourceDialog"; import "@/less/memo-editor.less"; +import { clearContentQueryParam, getContentQueryParam } from "@/helpers/utils"; const listItemSymbolList = ["- [ ] ", "- [x] ", "- [X] ", "* ", "- "]; const emptyOlReg = /^(\d+)\. $/; @@ -22,6 +23,10 @@ const getEditorContentCache = (): string => { return storage.get(["editorContentCache"]).editorContentCache ?? ""; }; +const getInitialContent = (): string => { + return getContentQueryParam() ?? getEditorContentCache(); +}; + const setEditorContentCache = (content: string) => { storage.set({ editorContentCache: content, @@ -286,6 +291,7 @@ const MemoEditor = () => { editorStore.clearResourceList(); setEditorContentCache(""); editorRef.current?.setContent(""); + clearContentQueryParam(); }; const handleCancelEdit = () => { @@ -378,7 +384,7 @@ const MemoEditor = () => { const editorConfig = useMemo( () => ({ className: `memo-editor`, - initialContent: getEditorContentCache(), + initialContent: getInitialContent(), placeholder: t("editor.placeholder"), fullscreen: state.fullscreen, onContentChange: handleContentChange, diff --git a/web/src/helpers/utils.ts b/web/src/helpers/utils.ts index cbae5964..ff2b3d31 100644 --- a/web/src/helpers/utils.ts +++ b/web/src/helpers/utils.ts @@ -105,3 +105,18 @@ export const formatBytes = (bytes: number) => { i = Math.floor(Math.log(bytes) / Math.log(k)); 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); +};