mirror of
https://github.com/outline/outline.git
synced 2026-11-29 08:39:20 +03:00
perf: Render simple read-only comments without an editor view (#13871)
* perf: Render simple read-only comments without an editor view Read-only editor content made of paragraphs, basic marks, hard breaks, emoji and lists is rendered from the schema DOMSerializer instead of mounting a ProseMirror view per comment. Anything needing a node view or plugin falls back to the full editor. Notification comment previews now use the same read-only path. * fix: Forward className on the static editor path
This commit is contained in:
1 parent
426079d05d
commit
ad6d39ac55
6 files changed
+278
-168
No files matched your search
+13
-11
@@ -7,7 +7,7 @@ import { mergeRefs } from "react-merge-refs";
|
||||
import type { Optional } from "utility-types";
|
||||
import EditorContainer from "@shared/editor/components/Styles";
|
||||
import { AttachmentPreset } from "@shared/types";
|
||||
import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper";
|
||||
import { ProsemirrorHelper } from "~/models/helpers/ProsemirrorHelper";
|
||||
import AsyncEditor from "~/components/AsyncEditor";
|
||||
import ClickablePadding from "~/components/ClickablePadding";
|
||||
import ErrorBoundary from "~/components/ErrorBoundary";
|
||||
@@ -200,9 +200,12 @@ function Editor({ ref, ...props }: Props & { ref?: React.Ref<SharedEditor> }) {
|
||||
[updateComments]
|
||||
);
|
||||
|
||||
const paragraphs = React.useMemo(() => {
|
||||
// Read-only content that needs no node views or plugins is rendered from
|
||||
// the schema alone, avoiding an editor view per instance.
|
||||
const staticHTML = React.useMemo(() => {
|
||||
if (props.readOnly && typeof props.value === "object") {
|
||||
return ProsemirrorHelper.getPlainParagraphs(props.value);
|
||||
const node = ProsemirrorHelper.toStaticNode(props.value);
|
||||
return node ? ProsemirrorHelper.toHTML(node) : undefined;
|
||||
}
|
||||
return undefined;
|
||||
}, [props.readOnly, props.value]);
|
||||
@@ -210,22 +213,21 @@ function Editor({ ref, ...props }: Props & { ref?: React.Ref<SharedEditor> }) {
|
||||
return (
|
||||
<ErrorBoundary component="div" reloadOnChunkMissing>
|
||||
<>
|
||||
{paragraphs ? (
|
||||
{staticHTML !== undefined ? (
|
||||
<EditorContainer
|
||||
$rtl={props.dir === "rtl"}
|
||||
grow={props.grow}
|
||||
style={props.style}
|
||||
className={props.className}
|
||||
editorStyle={props.editorStyle}
|
||||
commenting={!!props.onClickCommentMark}
|
||||
lang={props.lang}
|
||||
readOnly
|
||||
>
|
||||
<div className="ProseMirror">
|
||||
{paragraphs.map((paragraph, index) => (
|
||||
<p key={index} dir="auto">
|
||||
{paragraph.content?.map((content) => content.text)}
|
||||
</p>
|
||||
))}
|
||||
</div>
|
||||
<div
|
||||
className="ProseMirror"
|
||||
dangerouslySetInnerHTML={{ __html: staticHTML }}
|
||||
/>
|
||||
</EditorContainer>
|
||||
) : (
|
||||
// The boundary must live between the lazy editor and any ancestor
|
||||
|
||||
@@ -64,6 +64,10 @@ function NotificationListItem({ notification, onNavigate }: Props) {
|
||||
[notification]
|
||||
);
|
||||
const menuAction = useMenuAction(actions);
|
||||
const commentData = React.useMemo(
|
||||
() => toJS(notification.comment?.data),
|
||||
[notification.comment?.data]
|
||||
);
|
||||
|
||||
return (
|
||||
<ContextMenu action={menuAction} ariaLabel={t("Notification options")}>
|
||||
@@ -82,10 +86,8 @@ function NotificationListItem({ notification, onNavigate }: Props) {
|
||||
<Time dateTime={notification.createdAt} addSuffix />{" "}
|
||||
{collection && <>· {collection.name}</>}
|
||||
</Text>
|
||||
{notification.comment && (
|
||||
<StyledCommentEditor
|
||||
defaultValue={toJS(notification.comment.data)}
|
||||
/>
|
||||
{commentData && (
|
||||
<StyledCommentEditor value={commentData} readOnly />
|
||||
)}
|
||||
{isAccessRequestPending && (
|
||||
<AccessRequestActions notification={notification} />
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
import type { ProsemirrorData } from "@shared/types";
|
||||
import { ProsemirrorHelper } from "./ProsemirrorHelper";
|
||||
|
||||
const paragraph = (content: ProsemirrorData[]): ProsemirrorData => ({
|
||||
type: "paragraph",
|
||||
content,
|
||||
});
|
||||
|
||||
const doc = (content: ProsemirrorData[]): ProsemirrorData => ({
|
||||
type: "doc",
|
||||
content,
|
||||
});
|
||||
|
||||
const render = (data: ProsemirrorData) => {
|
||||
const node = ProsemirrorHelper.toStaticNode(data);
|
||||
if (!node) {
|
||||
return undefined;
|
||||
}
|
||||
return ProsemirrorHelper.toHTML(node);
|
||||
};
|
||||
|
||||
describe("ProsemirrorHelper", () => {
|
||||
describe("toStaticNode", () => {
|
||||
it("should render marks, hard breaks and lists", () => {
|
||||
const html = render(
|
||||
doc([
|
||||
paragraph([
|
||||
{ type: "text", text: "bold", marks: [{ type: "strong" }] },
|
||||
{ type: "br" },
|
||||
{ type: "emoji", attrs: { "data-name": "smile" } },
|
||||
]),
|
||||
{
|
||||
type: "bullet_list",
|
||||
content: [
|
||||
{
|
||||
type: "list_item",
|
||||
content: [paragraph([{ type: "text", text: "item" }])],
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
);
|
||||
|
||||
expect(html).toContain("<strong>bold</strong>");
|
||||
expect(html).toContain("<br>");
|
||||
expect(html).toContain('class="emoji smile"');
|
||||
expect(html).toContain('<ul><li><p dir="auto">item</p></li></ul>');
|
||||
});
|
||||
|
||||
it("should render an empty paragraph", () => {
|
||||
expect(render(doc([{ type: "paragraph" }]))).toBe('<p dir="auto"></p>');
|
||||
});
|
||||
|
||||
it("should return undefined for mentions", () => {
|
||||
expect(
|
||||
render(
|
||||
doc([
|
||||
paragraph([
|
||||
{
|
||||
type: "mention",
|
||||
attrs: { type: "user", modelId: "1", label: "Tom", id: "1" },
|
||||
},
|
||||
]),
|
||||
])
|
||||
)
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return undefined for custom emoji", () => {
|
||||
expect(
|
||||
render(
|
||||
doc([
|
||||
paragraph([
|
||||
{
|
||||
type: "emoji",
|
||||
attrs: { "data-name": "7a1f7a2e-2b1e-4c3d-9f4a-1b2c3d4e5f60" },
|
||||
},
|
||||
]),
|
||||
])
|
||||
)
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return undefined for code blocks and images", () => {
|
||||
expect(
|
||||
render(doc([{ type: "code_fence", attrs: { language: "js" } }]))
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
render(doc([{ type: "image", attrs: { src: "/x.png" } }]))
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return undefined for links", () => {
|
||||
expect(
|
||||
render(
|
||||
doc([
|
||||
paragraph([
|
||||
{
|
||||
type: "text",
|
||||
text: "link",
|
||||
marks: [
|
||||
{ type: "link", attrs: { href: "https://example.com" } },
|
||||
],
|
||||
},
|
||||
]),
|
||||
])
|
||||
)
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return undefined for inline code", () => {
|
||||
expect(
|
||||
render(
|
||||
doc([
|
||||
paragraph([
|
||||
{ type: "text", text: "#fff", marks: [{ type: "code_inline" }] },
|
||||
]),
|
||||
])
|
||||
)
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return undefined for comment marks", () => {
|
||||
expect(
|
||||
render(
|
||||
doc([
|
||||
paragraph([
|
||||
{
|
||||
type: "text",
|
||||
text: "hi",
|
||||
marks: [{ type: "comment", attrs: { id: "c1", userId: "u" } }],
|
||||
},
|
||||
]),
|
||||
])
|
||||
)
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return undefined for invalid data", () => {
|
||||
expect(render(doc([paragraph([{ type: "text" }])]))).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -2,8 +2,8 @@ import ExtensionManager from "@shared/editor/lib/ExtensionManager";
|
||||
import { richExtensions, withComments } from "@shared/editor/nodes";
|
||||
import type { ProsemirrorData } from "@shared/types";
|
||||
import { ProsemirrorHelper as SharedProsemirrorHelper } from "@shared/utils/ProsemirrorHelper";
|
||||
import { Schema } from "prosemirror-model";
|
||||
import { Node } from "prosemirror-model";
|
||||
import { DOMSerializer, Node, Schema } from "prosemirror-model";
|
||||
import { isUUID } from "validator";
|
||||
|
||||
interface HasData {
|
||||
data: ProsemirrorData;
|
||||
@@ -15,8 +15,77 @@ const schema = new Schema({
|
||||
marks: extensionManager.marks,
|
||||
});
|
||||
const serializer = extensionManager.serializer();
|
||||
const domSerializer = DOMSerializer.fromSchema(schema);
|
||||
|
||||
/**
|
||||
* Node types that render identically from `toDOM` alone, without a node view,
|
||||
* plugin or decoration, so a static render matches the editor.
|
||||
*/
|
||||
const staticNodeTypes = new Set([
|
||||
"doc",
|
||||
"paragraph",
|
||||
"text",
|
||||
"br",
|
||||
"emoji",
|
||||
"bullet_list",
|
||||
"ordered_list",
|
||||
"list_item",
|
||||
]);
|
||||
|
||||
/** Mark types that render identically from `toDOM` alone. */
|
||||
const staticMarkTypes = new Set([
|
||||
"strong",
|
||||
"em",
|
||||
"underline",
|
||||
"strikethrough",
|
||||
"highlight",
|
||||
]);
|
||||
|
||||
const isStaticNode = (node: ProsemirrorData) => {
|
||||
if (!staticNodeTypes.has(node.type)) {
|
||||
return false;
|
||||
}
|
||||
// Custom emoji are rendered by a component from their uploaded image.
|
||||
if (node.type === "emoji" && isUUID(String(node.attrs?.["data-name"]))) {
|
||||
return false;
|
||||
}
|
||||
return (node.marks ?? []).every((mark) => staticMarkTypes.has(mark.type));
|
||||
};
|
||||
|
||||
export class ProsemirrorHelper {
|
||||
/**
|
||||
* Returns the ProseMirror node for the data when every node and mark in it
|
||||
* can be rendered by the schema serializer alone, otherwise undefined.
|
||||
*
|
||||
* @param data The ProsemirrorData object or ProsemirrorNode
|
||||
* @returns The node, or undefined if an editor view is needed to render it
|
||||
*/
|
||||
static toStaticNode(data: ProsemirrorData | Node): Node | undefined {
|
||||
const json =
|
||||
data instanceof Node ? (data.toJSON() as ProsemirrorData) : data;
|
||||
if (!SharedProsemirrorHelper.everyNode(json, isStaticNode)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
return Node.fromJSON(schema, json);
|
||||
} catch (_err) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes the content of a node to HTML using the schema alone.
|
||||
*
|
||||
* @param node The node to serialize
|
||||
* @returns The rendered content as an HTML string
|
||||
*/
|
||||
static toHTML(node: Node): string {
|
||||
const container = document.createElement("div");
|
||||
container.appendChild(domSerializer.serializeFragment(node.content));
|
||||
return container.innerHTML;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the markdown representation of the document derived from the ProseMirror data.
|
||||
*
|
||||
|
||||
@@ -207,148 +207,55 @@ describe("ProsemirrorHelper", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getPlainParagraphs", () => {
|
||||
it("should return an array of plain paragraphs", async () => {
|
||||
const data = {
|
||||
type: "doc",
|
||||
content: [
|
||||
{
|
||||
type: "paragraph",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "some content in a paragraph",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
type: "paragraph",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "some content in another paragraph",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
} as ProsemirrorData;
|
||||
|
||||
const paragraphs = ProsemirrorHelper.getPlainParagraphs(data);
|
||||
|
||||
expect(paragraphs).toEqual([
|
||||
describe("everyNode", () => {
|
||||
const data = {
|
||||
type: "doc",
|
||||
content: [
|
||||
{
|
||||
type: "paragraph",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "some content in a paragraph",
|
||||
},
|
||||
{ type: "text", text: "plain" },
|
||||
{ type: "text", text: "bold", marks: [{ type: "bold" }] },
|
||||
],
|
||||
},
|
||||
{
|
||||
type: "paragraph",
|
||||
type: "bullet_list",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "some content in another paragraph",
|
||||
type: "list_item",
|
||||
content: [{ type: "paragraph" }],
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
],
|
||||
} as ProsemirrorData;
|
||||
|
||||
it("should return true when every node passes the predicate", () => {
|
||||
expect(ProsemirrorHelper.everyNode(data, () => true)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return undefined when data contains inline nodes", async () => {
|
||||
const data = {
|
||||
type: "doc",
|
||||
content: [
|
||||
{
|
||||
type: "paragraph",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "some content in a paragraph",
|
||||
},
|
||||
{
|
||||
type: "emoji",
|
||||
attrs: {
|
||||
"data-name": "😆",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
} as ProsemirrorData;
|
||||
|
||||
const paragraphs = ProsemirrorHelper.getPlainParagraphs(data);
|
||||
expect(paragraphs).toBeUndefined();
|
||||
it("should visit the root node", () => {
|
||||
expect(
|
||||
ProsemirrorHelper.everyNode(data, (node) => node.type !== "doc")
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("should return undefined when data contains block nodes", async () => {
|
||||
const data = {
|
||||
type: "doc",
|
||||
content: [
|
||||
{
|
||||
type: "blockquote",
|
||||
content: [
|
||||
{
|
||||
type: "paragraph",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "some content in a paragraph",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
} as ProsemirrorData;
|
||||
|
||||
const paragraphs = ProsemirrorHelper.getPlainParagraphs(data);
|
||||
expect(paragraphs).toBeUndefined();
|
||||
it("should visit nested nodes", () => {
|
||||
expect(
|
||||
ProsemirrorHelper.everyNode(data, (node) => node.type !== "list_item")
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("should return undefined when data contains marks", async () => {
|
||||
const data = {
|
||||
type: "doc",
|
||||
content: [
|
||||
{
|
||||
type: "paragraph",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "some content in a paragraph",
|
||||
marks: [
|
||||
{
|
||||
type: "bold",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
} as ProsemirrorData;
|
||||
|
||||
const paragraphs = ProsemirrorHelper.getPlainParagraphs(data);
|
||||
expect(paragraphs).toBeUndefined();
|
||||
it("should expose marks on text nodes", () => {
|
||||
expect(
|
||||
ProsemirrorHelper.everyNode(data, (node) => !node.marks?.length)
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("should handle paragraph without content", async () => {
|
||||
const data = {
|
||||
type: "doc",
|
||||
content: [
|
||||
{
|
||||
type: "paragraph",
|
||||
},
|
||||
],
|
||||
} as ProsemirrorData;
|
||||
|
||||
const paragraphs = ProsemirrorHelper.getPlainParagraphs(data);
|
||||
expect(paragraphs).toEqual([
|
||||
{
|
||||
type: "paragraph",
|
||||
},
|
||||
]);
|
||||
it("should handle nodes without content", () => {
|
||||
expect(
|
||||
ProsemirrorHelper.everyNode({ type: "paragraph" }, () => true)
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { Schema } from "prosemirror-model";
|
||||
import { Node } from "prosemirror-model";
|
||||
import type { Node } from "prosemirror-model";
|
||||
import headingToSlug from "../editor/lib/headingToSlug";
|
||||
import textBetween from "../editor/lib/textBetween";
|
||||
import type { ProsemirrorData } from "../types";
|
||||
@@ -597,36 +597,23 @@ export class ProsemirrorHelper {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the paragraphs from the data if there are only plain paragraphs
|
||||
* without any formatting. Otherwise returns undefined.
|
||||
* Returns true when the predicate holds for every node in the data,
|
||||
* including the root and all descendants.
|
||||
*
|
||||
* @param data The ProsemirrorData object or ProsemirrorNode
|
||||
* @returns An array of paragraph nodes or undefined
|
||||
* @param data The ProsemirrorData object to walk
|
||||
* @param predicate The predicate to test each node against
|
||||
* @returns true when every node passes the predicate
|
||||
*/
|
||||
static getPlainParagraphs(data: ProsemirrorData | Node) {
|
||||
// Convert ProsemirrorNode to JSON if needed
|
||||
const jsonData =
|
||||
data instanceof Node ? (data.toJSON() as ProsemirrorData) : data;
|
||||
|
||||
const paragraphs: ProsemirrorData[] = [];
|
||||
if (!jsonData.content) {
|
||||
return paragraphs;
|
||||
static everyNode(
|
||||
data: ProsemirrorData,
|
||||
predicate: (node: ProsemirrorData) => boolean
|
||||
): boolean {
|
||||
if (!predicate(data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const node of jsonData.content) {
|
||||
if (
|
||||
node.type === "paragraph" &&
|
||||
(!node.content ||
|
||||
!node.content.some(
|
||||
(item) =>
|
||||
item.type !== "text" || (item.marks && item.marks.length > 0)
|
||||
))
|
||||
) {
|
||||
paragraphs.push(node);
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
return paragraphs;
|
||||
return (data.content ?? []).every(
|
||||
(node) => !!node && ProsemirrorHelper.everyNode(node, predicate)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user