Skip to content

Commit c3ac70f

Browse files
committed
🐛 修复VEditor的markdown编辑器输入时失去焦点的问题
1 parent 7daff1c commit c3ac70f

File tree

2 files changed

+70
-30
lines changed

2 files changed

+70
-30
lines changed

app/src/pages/_vditor_panel.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default function MarkdownEditor({
1515
className?: string;
1616
options?: Omit<IOptions, "after" | "input">;
1717
}) {
18+
// console.log("veditor render");
1819
const [vd, setVd] = useState<Vditor>();
1920
const el = useRef<HTMLDivElement>(null);
2021

@@ -24,7 +25,8 @@ export default function MarkdownEditor({
2425
vditor.setValue(defaultValue);
2526
setVd(vditor);
2627
},
27-
input: (value: string) => {
28+
// input 有问题,只要一输入内容停下来的时候就被迫失去焦点了。
29+
blur: (value: string) => {
2830
onChange(value);
2931
},
3032
theme: "dark",
@@ -42,8 +44,18 @@ export default function MarkdownEditor({
4244

4345
return () => {
4446
vd?.destroy();
47+
setVd(undefined);
48+
// console.log("veditor unmount");
4549
};
4650
}, [defaultValue]);
4751

48-
return <div ref={el} id={id} className={className} onKeyDown={(e) => e.stopPropagation()} />;
52+
return (
53+
<div
54+
ref={el}
55+
id={id}
56+
className={className}
57+
onKeyDown={(e) => e.stopPropagation()}
58+
onKeyUp={(e) => e.stopPropagation()}
59+
/>
60+
);
4961
}

app/src/pages/test.tsx

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,86 @@
11
import { useEffect, useState } from "react";
22
import Button from "../components/Button";
3-
import { Vector } from "../core/dataStruct/Vector";
4-
import { Renderer } from "../core/render/canvas2d/renderer";
5-
import { InputElement } from "../core/render/domElement/inputElement";
6-
import { StageStyleManager } from "../core/service/feedbackService/stageStyle/StageStyleManager";
7-
import { Camera } from "../core/stage/Camera";
83
import { Settings } from "../core/service/Settings";
94
import tipsJson from "../assets/projectGraphTips.json";
5+
// import DetailsEditSidePanel from "./_details_edit_side_panel";
6+
// import MarkdownEditor from "./_vditor_panel";
7+
// import Vditor from "vditor";
8+
import "vditor/dist/index.css";
9+
import MarkdownEditor from "./_vditor_panel";
1010

1111
export default function TestPage() {
1212
useEffect(() => {
1313
// InputElement.textarea(new Vector(300, 300), "hello world");
1414
});
1515

16-
function testInput() {
17-
InputElement.input(new Vector(300, 300), "hello world", () => {}, {
18-
fontSize: Renderer.FONT_SIZE * Camera.currentScale + "px",
19-
backgroundColor: "transparent",
20-
color: StageStyleManager.currentStyle.StageObjectBorderColor.toString(),
21-
outline: "solid 1px white",
22-
marginTop: -8 * Camera.currentScale + "px",
23-
width: "100vw",
24-
});
25-
}
26-
27-
function testTextarea() {
28-
InputElement.textarea(new Vector(300, 300), "hello world", () => {}, {
29-
fontSize: Renderer.FONT_SIZE * Camera.currentScale + "px",
30-
backgroundColor: "transparent",
31-
color: StageStyleManager.currentStyle.StageObjectBorderColor.toString(),
32-
outline: "solid 1px white",
33-
marginTop: -8 * Camera.currentScale + "px",
34-
});
35-
}
36-
3716
const [theme, setTheme] = useState("light");
17+
// const [vd, setVd] = useState<Vditor>();
3818
useEffect(() => {
3919
Settings.watch("uiTheme", (value) => {
4020
setTheme(value);
4121
document.documentElement.setAttribute("data-theme", value);
4222
});
23+
return () => {
24+
// vd?.destroy();
25+
// setVd(undefined);
26+
};
4327
}, []);
4428

29+
const [inputCurrentDetails, setInputCurrentDetails] = useState("");
30+
const setInputCurrentDetailsHandler = (value?: string | undefined) => {
31+
if (value !== undefined) {
32+
setInputCurrentDetails(value);
33+
}
34+
};
4535
return (
4636
<>
4737
<div className="p-4">
4838
<h1> Test Page </h1>
49-
<Button onClick={testInput}>TestInput</Button>
50-
<Button onClick={testTextarea}>TestTextarea</Button>
5139
<Button onClick={() => console.log(tipsJson)}>test json</Button>
5240
<div className="h-16 bg-gray-800" />
5341
<p>当前主题: {theme}</p>
5442
<div className="bg-test-bg h-64 w-64 outline-2"></div>
5543
<div className="bg-test-bg h-64 w-64 outline-2 hover:cursor-pointer"></div>
44+
<MarkdownEditor
45+
id="details-editor"
46+
className="fixed bottom-4 right-4 top-20 z-50 overflow-hidden rounded-xl"
47+
onChange={setInputCurrentDetailsHandler}
48+
defaultValue={inputCurrentDetails}
49+
options={{
50+
width: "50%",
51+
toolbar: [
52+
// {
53+
// name: "confirm",
54+
// tip: "确认",
55+
// tipPosition: "n",
56+
// icon: '<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 1024 1024"><path fill="currentColor" d="M912 190h-69.9c-9.8 0-19.1 4.5-25.1 12.2L404.7 724.5L207 474a32 32 0 0 0-25.1-12.2H112c-6.7 0-10.4 7.7-6.3 12.9l273.9 347c12.8 16.2 37.4 16.2 50.3 0l488.4-618.9c4.1-5.1.4-12.8-6.3-12.8"/></svg>',
57+
// click: handleConfirmDetailsEdit,
58+
// },
59+
"fullscreen",
60+
"|",
61+
"headings",
62+
"bold",
63+
"italic",
64+
"strike",
65+
"link",
66+
"|",
67+
"list",
68+
"ordered-list",
69+
"check",
70+
"outdent",
71+
"indent",
72+
"|",
73+
"quote",
74+
"line",
75+
"code",
76+
"inline-code",
77+
"table",
78+
"|",
79+
"undo",
80+
"redo",
81+
],
82+
}}
83+
/>
5684
</div>
5785
</>
5886
);

0 commit comments

Comments
 (0)