diff --git a/web/src/components/Memo.tsx b/web/src/components/Memo.tsx index 12b9aedc..6f72e334 100644 --- a/web/src/components/Memo.tsx +++ b/web/src/components/Memo.tsx @@ -1,7 +1,7 @@ import { memo, useEffect, useRef, useState } from "react"; -import { escape } from "lodash-es"; +import { escape, indexOf } from "lodash-es"; import { IMAGE_URL_REG, LINK_REG, MEMO_LINK_REG, TAG_REG, UNKNOWN_ID } from "../helpers/consts"; -import { parseMarkedToHtml } from "../helpers/marked"; +import { DONE_BLOCK_REG, parseMarkedToHtml, TODO_BLOCK_REG } from "../helpers/marked"; import * as utils from "../helpers/utils"; import useToggle from "../hooks/useToggle"; import { editorStateService, locationService, memoService } from "../services"; @@ -116,7 +116,7 @@ const Memo: React.FC = (props: Props) => { toastHelper.error("MEMO Not Found"); targetEl.classList.remove("memo-link-text"); } - } else if (targetEl.tagName === "SPAN" && targetEl.className === "tag-span") { + } else if (targetEl.className === "tag-span") { const tagName = targetEl.innerText.slice(1); const currTagQuery = locationService.getState().query?.tag; if (currTagQuery === tagName) { @@ -125,7 +125,32 @@ const Memo: React.FC = (props: Props) => { locationService.setTagQuery(tagName); } } else if (targetEl.className === "todo-block") { - // ...do nth + const status = targetEl.dataset?.value; + const todoElementList = [...(memoContainerRef.current?.querySelectorAll(`span.todo-block[data-value=${status}]`) ?? [])]; + for (const element of todoElementList) { + if (element === targetEl) { + const index = indexOf(todoElementList, element); + const tempList = memo.content.split(status === "DONE" ? DONE_BLOCK_REG : TODO_BLOCK_REG); + let finalContent = ""; + + for (let i = 0; i < tempList.length; i++) { + if (i === 0) { + finalContent += `${tempList[i]}`; + } else { + if (i === index + 1) { + finalContent += status === "DONE" ? "- [ ] " : "- [x] "; + } else { + finalContent += status === "DONE" ? "- [x] " : "- [ ] "; + } + finalContent += `${tempList[i]}`; + } + } + await memoService.patchMemo({ + id: memo.id, + content: finalContent, + }); + } + } } }; diff --git a/web/src/components/MemoEditor.tsx b/web/src/components/MemoEditor.tsx index 06e423e3..9d8560bb 100644 --- a/web/src/components/MemoEditor.tsx +++ b/web/src/components/MemoEditor.tsx @@ -214,7 +214,7 @@ const MemoEditor: React.FC = () => { const prevString = currentValue.slice(0, selectionStart); const nextString = currentValue.slice(selectionStart); - let nextValue = prevString + "# " + nextString; + let nextValue = prevString + "#" + nextString; let cursorIndex = prevString.length + 1; if (prevString.endsWith("#") && nextString.startsWith(" ")) { @@ -265,7 +265,7 @@ const MemoEditor: React.FC = () => { const handleTagSeletorClick = useCallback((event: React.MouseEvent) => { if (tagSeletorRef.current !== event.target && tagSeletorRef.current?.contains(event.target as Node)) { - editorRef.current?.insertText((event.target as HTMLElement).textContent ?? ""); + editorRef.current?.insertText((event.target as HTMLElement).textContent + " " ?? ""); toggleTagSeletor(false); } }, []); diff --git a/web/src/components/MenuBtnsPopup.tsx b/web/src/components/MenuBtnsPopup.tsx index 15412d32..80bf84ee 100644 --- a/web/src/components/MenuBtnsPopup.tsx +++ b/web/src/components/MenuBtnsPopup.tsx @@ -57,12 +57,12 @@ const MenuBtnsPopup: React.FC = (props: Props) => { return (
- + diff --git a/web/src/helpers/marked.ts b/web/src/helpers/marked.ts index 821de7b1..5b9e95a7 100644 --- a/web/src/helpers/marked.ts +++ b/web/src/helpers/marked.ts @@ -1,23 +1,22 @@ const CODE_BLOCK_REG = /```([\s\S]*?)```/g; const BOLD_TEXT_REG = /\*\*(.+?)\*\*/g; const EM_TEXT_REG = /\*(.+?)\*/g; -const TODO_BLOCK_REG = /- \[ \] /g; -const DONE_BLOCK_REG = /- \[x\] /g; +export const TODO_BLOCK_REG = /- \[ \] /g; +export const DONE_BLOCK_REG = /- \[x\] /g; const DOT_LI_REG = /[*-] /g; const NUM_LI_REG = /(\d+)\. /g; const parseMarkedToHtml = (markedStr: string): string => { const htmlText = markedStr + .replace(/([\u4e00-\u9fa5])([A-Za-z0-9?.,;[\]]+)/g, "$1 $2") + .replace(/([A-Za-z0-9?.,;[\]]+)([\u4e00-\u9fa5])/g, "$1 $2") .replace(CODE_BLOCK_REG, "
$1
") - .replace(TODO_BLOCK_REG, "⬜") - .replace(DONE_BLOCK_REG, "✅") + .replace(TODO_BLOCK_REG, "⬜") + .replace(DONE_BLOCK_REG, "✅") .replace(DOT_LI_REG, "•") .replace(NUM_LI_REG, "$1.") .replace(BOLD_TEXT_REG, "$1") - .replace(EM_TEXT_REG, "$1") - .replace(/([\u4e00-\u9fa5])([A-Za-z0-9?.,;[\]]+)/g, "$1 $2") - .replace(/([A-Za-z0-9?.,;[\]]+)([\u4e00-\u9fa5])/g, "$1 $2"); - + .replace(EM_TEXT_REG, "$1"); return htmlText; }; diff --git a/web/src/less/memo-content.less b/web/src/less/memo-content.less index fae3ebab..c017a531 100644 --- a/web/src/less/memo-content.less +++ b/web/src/less/memo-content.less @@ -21,7 +21,7 @@ .counter-block, .todo-block { - @apply inline-block text-center w-6 font-mono; + @apply inline-block text-center w-6 font-mono select-none cursor-pointer hover:shadow-inner; } pre {