Skip to content

Commit

Permalink
Ensure that nodes re-render when their decorations change
Browse files Browse the repository at this point in the history
  • Loading branch information
smoores-dev committed Oct 6, 2024
1 parent 0813243 commit 0f227c7
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 59 deletions.
58 changes: 29 additions & 29 deletions demo/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,35 +108,35 @@ const schema = new Schema({
const editorState = EditorState.create({
schema,
doc: schema.nodes.doc.create({}, [
// schema.nodes.paragraph.create({}, [
// schema.text("This ", [schema.marks.em.create()]),
// schema.text("is", [
// schema.marks.em.create(),
// schema.marks.strong.create(),
// ]),
// schema.text(" the first paragraph"),
// ]),
// schema.nodes.paragraph.create({}, [
// schema.text("This is the second paragraph"),
// schema.nodes.footnote.create({ number: 1 }, schema.text("Footnote")),
// ]),
schema.nodes.paragraph.create({}, [
schema.text("This ", [schema.marks.em.create()]),
schema.text("is", [
schema.marks.em.create(),
schema.marks.strong.create(),
]),
schema.text(" the first paragraph"),
]),
schema.nodes.paragraph.create({}, [
schema.text("This is the second paragraph"),
schema.nodes.footnote.create({ number: 1 }, schema.text("Footnote")),
]),
schema.nodes.paragraph.create(),
// schema.nodes.image.create(),
// schema.nodes.image.create(),
// schema.nodes.paragraph.create(
// {},
// schema.text("This is the third paragraph 🫵")
// ),
// schema.nodes.table.create({}, [
// schema.nodes.table_row.create({}, [
// schema.nodes.table_header.create({}, schema.text("h1")),
// schema.nodes.table_header.create({}, schema.text("h2")),
// ]),
// schema.nodes.table_row.create({}, [
// schema.nodes.table_cell.create({}, schema.text("c1")),
// schema.nodes.table_cell.create({}, schema.text("c2")),
// ]),
// ]),
schema.nodes.image.create(),
schema.nodes.image.create(),
schema.nodes.paragraph.create(
{},
schema.text("This is the third paragraph 🫵")
),
schema.nodes.table.create({}, [
schema.nodes.table_row.create({}, [
schema.nodes.table_header.create({}, schema.text("h1")),
schema.nodes.table_header.create({}, schema.text("h2")),
]),
schema.nodes.table_row.create({}, [
schema.nodes.table_cell.create({}, schema.text("c1")),
schema.nodes.table_cell.create({}, schema.text("c2")),
]),
]),
]),
plugins: [
reactKeys(),
Expand Down Expand Up @@ -309,7 +309,7 @@ function DemoEditor() {
() =>
showReactNodeViews
? {
// paragraph: Paragraph,
paragraph: Paragraph,
list: List,
list_item: ListItem,
footnote: Footnote,
Expand Down
28 changes: 14 additions & 14 deletions docs/assets/index-DNM_ioi2.js → docs/assets/index-BGqMXhJy.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>React-ProseMirror Demo</title>
<script type="module" crossorigin src="/react-prosemirror/assets/index-DNM_ioi2.js"></script>
<script type="module" crossorigin src="/react-prosemirror/assets/index-BGqMXhJy.js"></script>
<link rel="stylesheet" crossorigin href="/react-prosemirror/assets/index-DAGU9WLy.css">
</head>
<body>
Expand Down
25 changes: 21 additions & 4 deletions src/components/ChildNodeViews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import React, {
import { ChildDescriptorsContext } from "../contexts/ChildDescriptorsContext.js";
import { EditorContext } from "../contexts/EditorContext.js";
import { ReactWidgetDecoration } from "../decorations/ReactWidgetType.js";
import { InternalDecorationSource } from "../decorations/internalTypes.js";
import { iterDeco } from "../decorations/iterDeco.js";
// import { useEditorState } from "../hooks/useEditorState.js";
import { useReactKeys } from "../hooks/useReactKeys.js";
Expand Down Expand Up @@ -374,10 +375,26 @@ const ChildElement = memo(
mark.isInSet(prevProps.child.marks)
) &&
prevProps.child.offset === nextProps.child.offset &&
// @ts-expect-error It's fine if these are undefined
prevProps.child.node === nextProps.child.node &&
// @ts-expect-error It's fine if these are undefined
prevProps.child.widget === nextProps.child.widget
(prevProps.child.type === "node"
? prevProps.child.outerDeco?.length ===
(nextProps.child as ChildNode).outerDeco?.length &&
prevProps.child.outerDeco?.every((prevDeco) =>
(nextProps.child as ChildNode).outerDeco?.some(
(nextDeco) =>
prevDeco.from === nextDeco.from &&
prevDeco.to &&
nextDeco.to &&
(prevDeco as any).type.eq((nextDeco as any).type)

Check warning on line 387 in src/components/ChildNodeViews.tsx

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 387 in src/components/ChildNodeViews.tsx

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
)
) &&
(prevProps.child.innerDeco as InternalDecorationSource).eq(
(nextProps.child as ChildNode).innerDeco
)
: true) &&
(prevProps.child as ChildNode).node ===
(nextProps.child as ChildNode).node &&
(prevProps.child as ChildWidget).widget ===
(nextProps.child as ChildWidget).widget
);

function createChildElements(
Expand Down
22 changes: 11 additions & 11 deletions src/components/__tests__/ProseMirror.draw-decoration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe("Decoration drawing", () => {
widget(
4,
forwardRef<HTMLSpanElement, WidgetViewComponentProps>(function B(
props,
{ widget, getPos, ...props },
ref
) {
return (
Expand All @@ -199,7 +199,7 @@ describe("Decoration drawing", () => {
widget(
4,
forwardRef<HTMLSpanElement, WidgetViewComponentProps>(function A(
props,
{ widget, getPos, ...props },
ref
) {
return (
Expand All @@ -213,7 +213,7 @@ describe("Decoration drawing", () => {
widget(
4,
forwardRef<HTMLSpanElement, WidgetViewComponentProps>(function C(
props,
{ widget, getPos, ...props },
ref
) {
return (
Expand Down Expand Up @@ -256,8 +256,8 @@ describe("Decoration drawing", () => {

it("calls widget destroy methods", async () => {
let destroyed = false;
const DestroyableWidget = forwardRef<HTMLElement>(
function DestroyableWidget(props, ref) {
const DestroyableWidget = forwardRef<HTMLElement, WidgetViewComponentProps>(
function DestroyableWidget({ widget, getPos, ...props }, ref) {
useEffect(() => {
destroyed = true;
});
Expand Down Expand Up @@ -589,7 +589,7 @@ describe("Decoration drawing", () => {
widget(
4,
forwardRef<HTMLImageElement, WidgetViewComponentProps>(function Img(
props,
{ widget, getPos, ...props },
ref
) {
return (
Expand All @@ -615,7 +615,7 @@ describe("Decoration drawing", () => {
widget(
4,
forwardRef<HTMLImageElement, WidgetViewComponentProps>(function Img(
props,
{ widget, getPos, ...props },
ref
) {
return (
Expand All @@ -629,7 +629,7 @@ describe("Decoration drawing", () => {
widget(
4,
forwardRef<HTMLImageElement, WidgetViewComponentProps>(function BR(
props,
{ widget, getPos, ...props },
ref
) {
return <br {...props} ref={ref as LegacyRef<HTMLBRElement>} />;
Expand Down Expand Up @@ -676,7 +676,7 @@ describe("Decoration drawing", () => {
widget(
2,
forwardRef<HTMLImageElement, WidgetViewComponentProps>(function Img(
props,
{ widget, getPos, ...props },
ref
) {
return (
Expand All @@ -699,7 +699,7 @@ describe("Decoration drawing", () => {
widget(
3,
forwardRef<HTMLSpanElement, WidgetViewComponentProps>(function Span(
props,
{ widget, getPos, ...props },
ref
) {
useEditorEffect((view) => {
Expand Down Expand Up @@ -728,7 +728,7 @@ describe("Decoration drawing", () => {
3,
forwardRef<HTMLButtonElement, WidgetViewComponentProps>(
function Widget(
{ getPos, ...props }: WidgetViewComponentProps,
{ widget, getPos, ...props }: WidgetViewComponentProps,
ref
) {
expect(getPos()).toBe(3);
Expand Down

0 comments on commit 0f227c7

Please sign in to comment.