From 6f4dcb1bcb06c86c03d2100d187ab793b7c72be5 Mon Sep 17 00:00:00 2001 From: Jeff Emmett <46964190+Jeff-Emmett@users.noreply.github.com> Date: Sun, 8 Dec 2024 03:13:19 -0500 Subject: [PATCH] markdown editor first attempt --- package.json | 2 ++ src/css/style.css | 9 +++++ src/shapes/MarkdownShapeUtil.tsx | 62 +++++++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 7f895bb..83befd3 100644 --- a/package.json +++ b/package.json @@ -26,8 +26,10 @@ "gray-matter": "^4.0.3", "itty-router": "^5.0.17", "lodash.throttle": "^4.1.1", + "markdown-it": "^14.1.0", "react": "^18.2.0", "react-dom": "^18.2.0", + "react-markdown-editor-lite": "^1.3.4", "react-router-dom": "^7.0.2", "tldraw": "^3.6.0", "vercel": "^39.1.1" diff --git a/src/css/style.css b/src/css/style.css index 9902992..f87f64e 100644 --- a/src/css/style.css +++ b/src/css/style.css @@ -324,4 +324,13 @@ p:has(+ ol) { box-shadow: 0 0px 16px rgba(0, 0, 0, 0.15); overflow: hidden; background-color: white; +} + +.rc-md-editor { + border: none !important; + font-family: inherit; +} + +.rc-md-editor .editor-container { + border-radius: 4px; } \ No newline at end of file diff --git a/src/shapes/MarkdownShapeUtil.tsx b/src/shapes/MarkdownShapeUtil.tsx index 9e71fbf..aa27c19 100644 --- a/src/shapes/MarkdownShapeUtil.tsx +++ b/src/shapes/MarkdownShapeUtil.tsx @@ -1,11 +1,18 @@ /** TODO: build this */ import { BaseBoxShapeUtil, TLBaseBoxShape, TLBaseShape } from "tldraw" +import MdEditor from "react-markdown-editor-lite" +import MarkdownIt from "markdown-it" +import "react-markdown-editor-lite/lib/index.css" + +// Initialize markdown parser +const mdParser = new MarkdownIt() export type IMarkdownShape = TLBaseShape< "MarkdownTool", { content: string + html: string } > @@ -20,13 +27,58 @@ export class MarkdownShape extends BaseBoxShapeUtil< getDefaultProps(): IMarkdownShape["props"] & { w: number; h: number } { return { - content: "", - w: 100, - h: 100, + content: "# New Note", + html: "

New Note

", + w: 400, + h: 300, } } - component(shape: IMarkdownShape) { - return
{shape.props.content}
+ component(shape: IMarkdownShape & TLBaseBoxShape) { + const handleEditorChange = ({ + text, + html, + }: { + text: string + html: string + }) => { + // Update the shape's content + this.editor?.updateShape({ + id: shape.id, + type: "MarkdownTool", + props: { + ...shape.props, + content: text, + html: html, + }, + }) + } + + return ( +
+ mdParser.render(text)} + value={shape.props.content} + onChange={handleEditorChange} + view={{ menu: true, md: true, html: false }} + canView={{ + menu: true, + md: true, + html: false, + both: false, + fullScreen: false, + hideMenu: false, + }} + /> +
+ ) } }