Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply incremental dom update #397

Merged
merged 3 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"@tanstack/react-query": "^5.17.15",
"@uiw/codemirror-extensions-basic-setup": "^4.23.2",
"@uiw/codemirror-theme-xcode": "^4.21.21",
"@uiw/codemirror-themes": "^4.21.21",
"@uiw/react-markdown-preview": "^5.0.7",
"@vscode/markdown-it-katex": "^1.1.0",
"axios": "^1.6.5",
"browser-image-resizer": "^2.4.1",
"clipboardy": "^4.0.0",
Expand All @@ -49,9 +49,15 @@
"codemirror-toolbar": "^0.0.4",
"color": "^4.2.3",
"form-data": "^4.0.0",
"hast-util-to-html": "^9.0.3",
"incremental-dom": "^0.7.0",
"katex": "^0.16.9",
"lib0": "^0.2.88",
"lodash": "^4.17.21",
"markdown-it": "^14.1.0",
"markdown-it-incremental-dom": "^2.1.0",
"markdown-it-prism": "^2.3.0",
"markdown-it-sanitizer": "^0.4.3",
"match-sorter": "^6.3.3",
"moment": "^2.30.1",
"notistack": "^3.0.1",
Expand All @@ -70,11 +76,7 @@
"react-social-login-buttons": "^3.9.1",
"react-use": "^17.5.0",
"redux-persist": "^6.0.0",
"rehype-external-links": "^3.0.0",
"rehype-katex": "^7.0.1",
"rehype-rewrite": "^4.0.2",
"rehype-sanitize": "^6.0.0",
"remark-math": "^6.0.0",
"refractor": "^4.8.1",
"validator": "^13.12.0",
"vite-plugin-package-version": "^1.1.0",
"yorkie-js-sdk": "0.5.4"
Expand All @@ -84,6 +86,7 @@
"@types/color": "^3.0.6",
"@types/katex": "^0.16.7",
"@types/lodash": "^4.14.202",
"@types/markdown-it": "^14.1.2",
"@types/node": "^20.14.9",
"@types/randomcolor": "^0.5.9",
"@types/react": "^18.2.43",
Expand Down
126 changes: 50 additions & 76 deletions frontend/src/components/editor/Preview.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
import { CircularProgress, Stack } from "@mui/material";
import MarkdownPreview from "@uiw/react-markdown-preview";
import katex from "katex";
import "katex/dist/katex.min.css";
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { useSelector } from "react-redux";
import rehypeExternalLinks from "rehype-external-links";
import rehypeKatex from "rehype-katex";
import { getCodeString } from "rehype-rewrite";
import rehypeSanitize, { defaultSchema } from "rehype-sanitize";
import remarkMath from "remark-math";
import { useCurrentTheme } from "../../hooks/useCurrentTheme";
import { selectEditor } from "../../store/editorSlice";
import { addSoftLineBreak } from "../../utils/document";
import MarkdownIt from "markdown-it";
import { toHtml } from "hast-util-to-html";
import markdownItKatex from "@vscode/markdown-it-katex";
import { refractor } from "refractor";
import { Root } from "hast";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import markdownItIncrementalDOM from "markdown-it-incremental-dom";
blurfx marked this conversation as resolved.
Show resolved Hide resolved
import * as IncrementalDOM from "incremental-dom";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import markdownItSanitizer from "markdown-it-sanitizer";
blurfx marked this conversation as resolved.
Show resolved Hide resolved
import "./editor.css";
import "./preview.css";

function Preview() {
const md = new MarkdownIt({
html: true,
linkify: true,
breaks: true,
highlight(code: string, lang: string) {
return `<pre class="language-${lang}"><code>${toHtml(
refractor.highlight(code, lang) as Root
)}</code></pre>`;
},
})
.use(markdownItIncrementalDOM, IncrementalDOM)
.use(markdownItKatex)
.use(markdownItSanitizer);
blurfx marked this conversation as resolved.
Show resolved Hide resolved

const Preview = () => {
const currentTheme = useCurrentTheme();
const editorStore = useSelector(selectEditor);
const [content, setContent] = useState("");
const containerRef = useRef<HTMLDivElement>(null);

useEffect(() => {
if (!editorStore.doc) return;
Expand All @@ -30,89 +51,42 @@

updatePreviewContent();

const unsubsribe = editorStore.doc.subscribe("$.content", () => {
const unsubscribe = editorStore.doc.subscribe("$.content", () => {
updatePreviewContent();
});

return () => {
unsubsribe();
unsubscribe();
setContent("");
};
}, [editorStore.doc]);

if (!editorStore?.doc)
useEffect(() => {
if (containerRef.current == null) {
return;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
IncrementalDOM.patch(containerRef.current, md.renderToIncrementalDOM(content));
blurfx marked this conversation as resolved.
Show resolved Hide resolved
}, [content, md]);

Check warning on line 72 in frontend/src/components/editor/Preview.tsx

View workflow job for this annotation

GitHub Actions / Check the source code (18.x)

React Hook useEffect has an unnecessary dependency: 'md'. Either exclude it or remove the dependency array. Outer scope values like 'md' aren't valid dependencies because mutating them doesn't re-render the component
blurfx marked this conversation as resolved.
Show resolved Hide resolved

if (!editorStore?.doc) {
return (
<Stack direction="row" justifyContent="center">
<CircularProgress sx={{ mt: 2 }} />
</Stack>
);
}

return (
<MarkdownPreview
style={{
paddingBottom: "2rem",
}}
source={addSoftLineBreak(content)}
wrapperElement={{
"data-color-mode": currentTheme,
style: {
whiteSpace: "wrap !important",
WebkitUserModify: "read-only",
},
}}
remarkPlugins={[remarkMath]}
rehypePlugins={[
[
rehypeSanitize,
{
...defaultSchema,
attributes: {
...defaultSchema.attributes,
code: [["className", /^language-./, "math-inline", "math-display"]],
},
},
],
rehypeKatex,
[rehypeExternalLinks, { target: "_blank" }],
]}
components={{
code: ({ children = [], className, ...props }) => {
// https://www.npmjs.com/package/@uiw/react-markdown-preview#support-custom-katex-preview
if (typeof children === "string" && /^\$\$(.*)\$\$/.test(children)) {
const html = katex.renderToString(children.replace(/^\$\$(.*)\$\$/, "$1"), {
throwOnError: false,
});
return (
<code
dangerouslySetInnerHTML={{ __html: html }}
style={{ background: "transparent" }}
/>
);
}
const code =
props.node && props.node.children
? getCodeString(props.node.children)
: children;
if (
typeof code === "string" &&
typeof className === "string" &&
/^language-katex/.test(className.toLocaleLowerCase())
) {
const html = katex.renderToString(code, {
throwOnError: false,
});
return (
<code
style={{ fontSize: "150%" }}
dangerouslySetInnerHTML={{ __html: html }}
/>
);
}
return <code className={String(className)}>{children}</code>;
},
}}
<div
ref={containerRef}
data-color-mode={currentTheme}
style={{ paddingBottom: "2rem" }}
className="markdown-preview"
/>
);
}
};

export default Preview;
Loading
Loading