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

Fix block marks #155

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .yarn/versions/60d041bd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
"@nytimes/react-prosemirror": prerelease
1 change: 1 addition & 0 deletions docs/assets/index-DAGU9WLy.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions docs/assets/index-DG8eHnxT.js

Large diffs are not rendered by default.

52 changes: 0 additions & 52 deletions docs/assets/index-jZ5wdX2i.js

This file was deleted.

3 changes: 2 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<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-jZ5wdX2i.js"></script>
<script type="module" crossorigin src="/react-prosemirror/assets/index-DG8eHnxT.js"></script>
<link rel="stylesheet" crossorigin href="/react-prosemirror/assets/index-DAGU9WLy.css">
</head>
<body>
<div id="root"></div>
Expand Down
7 changes: 6 additions & 1 deletion src/components/ChildNodeViews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
prevDeco.from === nextDeco.from &&
prevDeco.to &&
nextDeco.to &&
(prevDeco as any).type.eq((nextDeco as any).type)

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

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

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

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
)
) &&
(a.innerDeco as InternalDecorationSource).eq((b as ChildNode).innerDeco)
Expand Down Expand Up @@ -341,7 +341,12 @@
getNodePos.current = () => getInnerPos.current() + child.offset;

if (child.type === "node") {
return (
return child.marks.reduce(
(element, mark) => (
<MarkView getPos={getNodePos} mark={mark}>
{element}
</MarkView>
),
<NodeView
key={child.key}
outerDeco={child.outerDeco}
Expand Down
1 change: 1 addition & 0 deletions src/components/MarkView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const MarkView = memo(
} else {
viewDescRef.current.parent = parentRef.current;
viewDescRef.current.dom = domRef.current;
viewDescRef.current.children = childDescriptors.current;
viewDescRef.current.contentDOM =
firstChildDesc?.dom.parentElement ?? domRef.current;
viewDescRef.current.mark = mark;
Expand Down
26 changes: 1 addition & 25 deletions src/components/NodeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { StopEventContext } from "../contexts/StopEventContext.js";
import { useNodeViewDescriptor } from "../hooks/useNodeViewDescriptor.js";

import { ChildNodeViews, wrapInDeco } from "./ChildNodeViews.js";
import { MarkView } from "./MarkView.js";
import { NodeViewComponentProps } from "./NodeViewComponentProps.js";
import { OutputSpec } from "./OutputSpec.js";

Expand Down Expand Up @@ -228,19 +227,6 @@ export const NodeView = memo(function NodeView({
undefined
);

// Inline nodes will already be wrapped in marks
// via the ChildNodeViews component
const markedElement = node.isInline
? decoratedElement
: node.marks.reduce(
(element, mark) => (
<MarkView getPos={getPos} mark={mark}>
{element}
</MarkView>
),
decoratedElement
);

const childContextValue = useMemo(
() => ({
parentRef: nodeViewDescRef,
Expand All @@ -253,17 +239,7 @@ export const NodeView = memo(function NodeView({
<SelectNodeContext.Provider value={setSelectNode}>
<StopEventContext.Provider value={setStopEvent}>
<ChildDescriptorsContext.Provider value={childContextValue}>
{cloneElement(
markedElement,
(node.marks.length && !node.isInline) ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
outerDeco.some((d) => (d as any).type.attrs.nodeName)
? { ref: domRef }
: // If all of the node decorations were attr-only, then
// we've already passed the domRef to the NodeView component
// as a prop
undefined
)}
{decoratedElement}
</ChildDescriptorsContext.Provider>
</StopEventContext.Provider>
</SelectNodeContext.Provider>
Expand Down
31 changes: 31 additions & 0 deletions src/components/__tests__/ProseMirror.draw.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Schema } from "prosemirror-model";
import { Plugin } from "prosemirror-state";
import {
builders,
doc,
h1,
hr,
Expand Down Expand Up @@ -258,4 +259,34 @@ describe("EditorView draw", () => {
expect(strongDom?.firstElementChild?.tagName).toBe("IMG");
expect(strongDom?.childNodes.item(1).textContent).toBe(" two");
});

it("correctly wraps blokc nodes with marks", async () => {
const testSchema = new Schema<"doc" | "image", "difficulty">({
nodes: schema.spec.nodes.update("image", {
...schema.spec.nodes.get("image")!,
inline: false,
group: "block",
}),
marks: schema.spec.marks.addToEnd("difficulty", {
attrs: { level: { default: "beginner" } },
toDOM(mark) {
return ["div", { "data-difficulty": mark.attrs["level"] }, 0];
},
}),
});

const { doc, image, difficulty } = builders(testSchema);

const { view } = tempEditor({
doc: doc(difficulty(image({ src: "" }))),
});

const docDom = view.dom;
const difficultyDom = docDom.firstElementChild as HTMLElement;
const imageDom = difficultyDom.firstElementChild!;

expect(difficultyDom.tagName).toBe("DIV");
expect(difficultyDom.dataset["difficulty"]).toBe("beginner");
expect(imageDom.tagName).toBe("IMG");
});
});
Loading