From 66310e8bfbfb5a5bd5cabd97bea2ea3711a8fc38 Mon Sep 17 00:00:00 2001 From: Red J Adaya Date: Sat, 15 Jun 2024 01:00:53 +0800 Subject: [PATCH] fix pre tag bg color in markdown (#678) --- src/app/common/elements/index.tsx | 1 - src/app/common/elements/markdown.less | 2 +- src/app/common/elements/markdown.tsx | 135 ++++++++++++-------------- src/app/common/elements/markdown2.tsx | 112 --------------------- src/app/sidebar/aichat.tsx | 6 +- src/plugins/markdown/markdown.tsx | 1 - 6 files changed, 65 insertions(+), 192 deletions(-) delete mode 100644 src/app/common/elements/markdown2.tsx diff --git a/src/app/common/elements/index.tsx b/src/app/common/elements/index.tsx index d33d23b7..33ae9525 100644 --- a/src/app/common/elements/index.tsx +++ b/src/app/common/elements/index.tsx @@ -7,7 +7,6 @@ export { InlineSettingsTextEdit } from "./inlinesettingstextedit"; export { InputDecoration } from "./inputdecoration"; export { LinkButton } from "./linkbutton"; export { Markdown } from "./markdown"; -export { Markdown2 } from "./markdown2"; export { Modal } from "./modal"; export { PasswordField } from "./passwordfield"; export { ResizableSidebar } from "./resizablesidebar"; diff --git a/src/app/common/elements/markdown.less b/src/app/common/elements/markdown.less index af5f3f2c..6a934235 100644 --- a/src/app/common/elements/markdown.less +++ b/src/app/common/elements/markdown.less @@ -46,7 +46,7 @@ padding: 2px 4px 2px 6px; } - pre.codeblock { + pre { background-color: var(--markdown-bg-color); margin: 4px 10px; padding: 0.4em 0.7em; diff --git a/src/app/common/elements/markdown.tsx b/src/app/common/elements/markdown.tsx index f225d11f..cdadf380 100644 --- a/src/app/common/elements/markdown.tsx +++ b/src/app/common/elements/markdown.tsx @@ -5,14 +5,14 @@ import * as React from "react"; import * as mobxReact from "mobx-react"; import ReactMarkdown from "react-markdown"; import remarkGfm from "remark-gfm"; +import { CopyButton } from "@/elements"; import { clsx } from "clsx"; -import { GlobalModel } from "@/models"; -import { v4 as uuidv4 } from "uuid"; +import * as mobx from "mobx"; +import { If } from "tsx-control-statements/components"; import "./markdown.less"; -import { boundMethod } from "autobind-decorator"; -function LinkRenderer(props: any): any { +function Link(props: any): JSX.Element { let newUrl = "https://extern?" + encodeURIComponent(props.href); return ( @@ -21,99 +21,86 @@ function LinkRenderer(props: any): any { ); } -function HeaderRenderer(props: any, hnum: number): any { +function Header(props: any, hnum: number): JSX.Element { return
{props.children}
; } -function CodeRenderer(props: any): any { +function Code(props: any): JSX.Element { return {props.children}; } -@mobxReact.observer -class CodeBlockMarkdown extends React.Component< - { children: React.ReactNode; codeSelectSelectedIndex?: number; uuid: string }, - {} -> { - blockIndex: number; - blockRef: React.RefObject; +const CodeBlock = mobxReact.observer( + (props: { children: React.ReactNode; onClickExecute?: (cmd: string) => void }): JSX.Element => { + const copied: OV = mobx.observable.box(false, { name: "copied" }); - constructor(props) { - super(props); - this.blockRef = React.createRef(); - this.blockIndex = GlobalModel.inputModel.addCodeBlockToCodeSelect(this.blockRef, this.props.uuid); - } + const getTextContent = (children: any) => { + if (typeof children === "string") { + return children; + } else if (Array.isArray(children)) { + return children.map(getTextContent).join(""); + } else if (children.props && children.props.children) { + return getTextContent(children.props.children); + } + return ""; + }; - render() { - let clickHandler: (e: React.MouseEvent, blockIndex: number) => void; - let inputModel = GlobalModel.inputModel; - clickHandler = (e: React.MouseEvent, blockIndex: number) => { - const sel = window.getSelection(); - if (sel?.toString().length == 0) { - inputModel.setCodeSelectSelectedCodeBlock(blockIndex); + const handleCopy = async (e: React.MouseEvent) => { + let textToCopy = getTextContent(props.children); + textToCopy = textToCopy.replace(/\n$/, ""); // remove trailing newline + await navigator.clipboard.writeText(textToCopy); + copied.set(true); + setTimeout(() => copied.set(false), 2000); // Reset copied state after 2 seconds + }; + + const handleExecute = (e: React.MouseEvent) => { + let textToCopy = getTextContent(props.children); + textToCopy = textToCopy.replace(/\n$/, ""); // remove trailing newline + if (props.onClickExecute) { + props.onClickExecute(textToCopy); + return; } }; - let selected = this.blockIndex == this.props.codeSelectSelectedIndex; + return ( -
 clickHandler(event, this.blockIndex)}
-            >
-                {this.props.children}
+            
+                {props.children}
+                
+ + + + +
); } -} +); @mobxReact.observer class Markdown extends React.Component< - { text: string; style?: any; extraClassName?: string; codeSelect?: boolean }, + { + text: string; + style?: any; + className?: string; + onClickExecute?: (cmd: string) => void; + }, {} > { - curUuid: string; - - constructor(props) { - super(props); - this.curUuid = uuidv4(); - } - - @boundMethod - CodeBlockRenderer(props: any, codeSelect: boolean, codeSelectIndex: number, curUuid: string): any { - if (codeSelect) { - return ( - - {props.children} - - ); - } else { - const clickHandler = (e: React.MouseEvent) => { - let blockText = (e.target as HTMLElement).innerText; - if (blockText) { - blockText = blockText.replace(/\n$/, ""); // remove trailing newline - navigator.clipboard.writeText(blockText); - } - }; - return
 clickHandler(event)}>{props.children}
; - } - } - render() { - let text = this.props.text; - let codeSelect = this.props.codeSelect; - let curCodeSelectIndex = GlobalModel.inputModel.getCodeSelectSelectedIndex(); + let { text, className, onClickExecute } = this.props; let markdownComponents = { - a: LinkRenderer, - h1: (props) => HeaderRenderer(props, 1), - h2: (props) => HeaderRenderer(props, 2), - h3: (props) => HeaderRenderer(props, 3), - h4: (props) => HeaderRenderer(props, 4), - h5: (props) => HeaderRenderer(props, 5), - h6: (props) => HeaderRenderer(props, 6), - code: (props) => CodeRenderer(props), - pre: (props) => this.CodeBlockRenderer(props, codeSelect, curCodeSelectIndex, this.curUuid), + a: Link, + h1: (props) =>
, + h2: (props) =>
, + h3: (props) =>
, + h4: (props) =>
, + h5: (props) =>
, + h6: (props) =>
, + code: Code, + pre: (props) => , }; + return ( -
+
{text} diff --git a/src/app/common/elements/markdown2.tsx b/src/app/common/elements/markdown2.tsx deleted file mode 100644 index fafa9f1f..00000000 --- a/src/app/common/elements/markdown2.tsx +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright 2023, Command Line Inc. -// SPDX-License-Identifier: Apache-2.0 - -import * as React from "react"; -import * as mobxReact from "mobx-react"; -import ReactMarkdown from "react-markdown"; -import remarkGfm from "remark-gfm"; -import { CopyButton } from "@/elements"; -import { clsx } from "clsx"; -import * as mobx from "mobx"; -import { If } from "tsx-control-statements/components"; - -import "./markdown.less"; - -function Link(props: any): JSX.Element { - let newUrl = "https://extern?" + encodeURIComponent(props.href); - return ( - - {props.children} - - ); -} - -function Header(props: any, hnum: number): JSX.Element { - return
{props.children}
; -} - -function Code(props: any): JSX.Element { - return {props.children}; -} - -const CodeBlock = mobxReact.observer( - (props: { children: React.ReactNode; onClickExecute?: (cmd: string) => void }): JSX.Element => { - const copied: OV = mobx.observable.box(false, { name: "copied" }); - - const getTextContent = (children: any) => { - if (typeof children === "string") { - return children; - } else if (Array.isArray(children)) { - return children.map(getTextContent).join(""); - } else if (children.props && children.props.children) { - return getTextContent(children.props.children); - } - return ""; - }; - - const handleCopy = async (e: React.MouseEvent) => { - let textToCopy = getTextContent(props.children); - textToCopy = textToCopy.replace(/\n$/, ""); // remove trailing newline - await navigator.clipboard.writeText(textToCopy); - copied.set(true); - setTimeout(() => copied.set(false), 2000); // Reset copied state after 2 seconds - }; - - const handleExecute = (e: React.MouseEvent) => { - let textToCopy = getTextContent(props.children); - textToCopy = textToCopy.replace(/\n$/, ""); // remove trailing newline - if (props.onClickExecute) { - props.onClickExecute(textToCopy); - return; - } - }; - - return ( -
-                {props.children}
-                
- - - - -
-
- ); - } -); - -@mobxReact.observer -class Markdown2 extends React.Component< - { - text: string; - style?: any; - className?: string; - onClickExecute?: (cmd: string) => void; - }, - {} -> { - render() { - let { text, className, onClickExecute } = this.props; - let markdownComponents = { - a: Link, - h1: (props) =>
, - h2: (props) =>
, - h3: (props) =>
, - h4: (props) =>
, - h5: (props) =>
, - h6: (props) =>
, - code: Code, - pre: (props) => , - }; - - return ( -
- - {text} - -
- ); - } -} - -export { Markdown2 }; diff --git a/src/app/sidebar/aichat.tsx b/src/app/sidebar/aichat.tsx index 7494512f..41e31c15 100644 --- a/src/app/sidebar/aichat.tsx +++ b/src/app/sidebar/aichat.tsx @@ -7,7 +7,7 @@ import * as mobx from "mobx"; import { GlobalModel } from "@/models"; import { boundMethod } from "autobind-decorator"; import { For, If } from "tsx-control-statements/components"; -import { Markdown2, TypingIndicator } from "@/elements"; +import { Markdown, TypingIndicator } from "@/elements"; import type { OverlayScrollbars } from "overlayscrollbars"; import { OverlayScrollbarsComponent, OverlayScrollbarsComponentRef } from "overlayscrollbars-react"; import tinycolor from "tinycolor2"; @@ -72,7 +72,7 @@ class ChatItem extends React.Component<
- + ); if (isassistantresponse) { @@ -94,7 +94,7 @@ class ChatItem extends React.Component<
- + ); } diff --git a/src/plugins/markdown/markdown.tsx b/src/plugins/markdown/markdown.tsx index a7ff7f2f..93994921 100644 --- a/src/plugins/markdown/markdown.tsx +++ b/src/plugins/markdown/markdown.tsx @@ -6,7 +6,6 @@ import * as mobx from "mobx"; import * as mobxReact from "mobx-react"; import { sprintf } from "sprintf-js"; import { Markdown } from "@/elements"; -import { GlobalModel } from "@/models/global"; import "./markdown.less";