mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
feat: Markdown Editor Keyboard Shortcuts (#2763)
* Add bold and italic keyboard shortcut * Add hyperlink keyboard shortcut support
This commit is contained in:
@ -6,6 +6,7 @@ import { useTranslation } from "react-i18next";
|
|||||||
import useLocalStorage from "react-use/lib/useLocalStorage";
|
import useLocalStorage from "react-use/lib/useLocalStorage";
|
||||||
import { memoServiceClient } from "@/grpcweb";
|
import { memoServiceClient } from "@/grpcweb";
|
||||||
import { TAB_SPACE_WIDTH, UNKNOWN_ID } from "@/helpers/consts";
|
import { TAB_SPACE_WIDTH, UNKNOWN_ID } from "@/helpers/consts";
|
||||||
|
import { isValidUrl } from "@/helpers/utils";
|
||||||
import { useGlobalStore, useResourceStore } from "@/store/module";
|
import { useGlobalStore, useResourceStore } from "@/store/module";
|
||||||
import { useMemoStore, useUserStore } from "@/store/v1";
|
import { useMemoStore, useUserStore } from "@/store/v1";
|
||||||
import { MemoRelation, MemoRelation_Type } from "@/types/proto/api/v2/memo_relation_service";
|
import { MemoRelation, MemoRelation_Type } from "@/types/proto/api/v2/memo_relation_service";
|
||||||
@ -115,6 +116,20 @@ const MemoEditor = (props: Props) => {
|
|||||||
handleSaveBtnClick();
|
handleSaveBtnClick();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (event.key === "b") {
|
||||||
|
const boldDelimiter = "**";
|
||||||
|
event.preventDefault();
|
||||||
|
styleHighlightedText(editorRef.current, boldDelimiter);
|
||||||
|
}
|
||||||
|
if (event.key === "i") {
|
||||||
|
const italicsDelimiter = "*";
|
||||||
|
event.preventDefault();
|
||||||
|
styleHighlightedText(editorRef.current, italicsDelimiter);
|
||||||
|
}
|
||||||
|
if (event.key === "k") {
|
||||||
|
event.preventDefault();
|
||||||
|
hyperlinkHighlightedTest(editorRef.current);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (event.key === "Tab") {
|
if (event.key === "Tab") {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@ -239,6 +254,9 @@ const MemoEditor = (props: Props) => {
|
|||||||
if (event.clipboardData && event.clipboardData.files.length > 0) {
|
if (event.clipboardData && event.clipboardData.files.length > 0) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
await uploadMultiFiles(event.clipboardData.files);
|
await uploadMultiFiles(event.clipboardData.files);
|
||||||
|
} else if (editorRef.current != null && isValidUrl(event.clipboardData.getData("Text"))) {
|
||||||
|
event.preventDefault();
|
||||||
|
hyperlinkHighlightedTest(editorRef.current, event.clipboardData.getData("Text"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -339,6 +357,33 @@ const MemoEditor = (props: Props) => {
|
|||||||
editorRef.current?.focus();
|
editorRef.current?.focus();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const styleHighlightedText = (editor: EditorRefActions, delimiter: string) => {
|
||||||
|
const cursorPosition = editor.getCursorPosition();
|
||||||
|
const selectedContent = editor.getSelectedContent();
|
||||||
|
if (selectedContent.startsWith(delimiter) && selectedContent.endsWith(delimiter)) {
|
||||||
|
editor.insertText(selectedContent.slice(delimiter.length, -delimiter.length));
|
||||||
|
const newContentLength = selectedContent.length - delimiter.length * 2;
|
||||||
|
editor.setCursorPosition(cursorPosition, cursorPosition + newContentLength);
|
||||||
|
} else {
|
||||||
|
editor.insertText(`${delimiter}${selectedContent}${delimiter}`);
|
||||||
|
editor.setCursorPosition(cursorPosition + delimiter.length, cursorPosition + delimiter.length + selectedContent.length);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const hyperlinkHighlightedTest = (editor: EditorRefActions, url?: string) => {
|
||||||
|
const cursorPosition = editor.getCursorPosition();
|
||||||
|
const selectedContent = editor.getSelectedContent();
|
||||||
|
const blankURL = "url";
|
||||||
|
url = url ?? blankURL;
|
||||||
|
|
||||||
|
editor.insertText(`[${selectedContent}](${url})`);
|
||||||
|
|
||||||
|
if (url === blankURL) {
|
||||||
|
const newCursorStart = cursorPosition + selectedContent.length + 3;
|
||||||
|
editor.setCursorPosition(newCursorStart, newCursorStart + url.length);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const editorConfig = useMemo(
|
const editorConfig = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
className: editorClassName ?? "",
|
className: editorClassName ?? "",
|
||||||
|
@ -83,3 +83,12 @@ 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 isValidUrl = (url: string): boolean => {
|
||||||
|
try {
|
||||||
|
new URL(url);
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Reference in New Issue
Block a user