Skip to content
Merged
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
Empty file added .yarn/versions/d7f962a1.yml
Empty file.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nytimes/react-prosemirror",
"version": "2.0.0",
"version": "2.0.1",
"license": "Apache-2.0",
"type": "module",
"main": "dist/cjs/index.js",
Expand Down
43 changes: 43 additions & 0 deletions src/nodeViews/__tests__/createReactNodeViewConstructor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Schema } from "prosemirror-model";
import { EditorState } from "prosemirror-state";
import type { EditorView } from "prosemirror-view";

import { ROOT_NODE_KEY, react } from "../../plugins/react.js";
import { findNodeKeyUp } from "../createReactNodeViewConstructor.js";

const schema = new Schema({
nodes: {
doc: { content: "block+" },
paragraph: { group: "block", content: "inline*" },
text: { group: "inline" },
},
});

function createState(): EditorState {
return EditorState.create({
doc: schema.nodes.doc.create(null, [schema.nodes.paragraph.create()]),
plugins: [react()],
});
}

/**
* findNodeKeyUp only reads editorView.state and editorView.props.nodeViews,
* so a minimal stub suffices and avoids needing toDOM specs for the schema.
*/
function stubView(state: EditorState): EditorView {
return { state, props: { nodeViews: {} } } as unknown as EditorView;
}

describe("findNodeKeyUp", () => {
it("should return ROOT_NODE_KEY for a negative position", () => {
const view = stubView(createState());

expect(findNodeKeyUp(view, -1)).toBe(ROOT_NODE_KEY);
});

it("should return ROOT_NODE_KEY for a valid position with no ancestor node views", () => {
const view = stubView(createState());

expect(findNodeKeyUp(view, 0)).toBe(ROOT_NODE_KEY);
});
});
4 changes: 4 additions & 0 deletions src/nodeViews/createReactNodeViewConstructor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ export function findNodeKeyUp(editorView: EditorView, pos: number): NodeKey {
const pluginState = reactPluginKey.getState(editorView.state);
if (!pluginState) return ROOT_NODE_KEY;

// -1 is the standard sentinel for nodes that are no longer part of the document
// since the node has no valid position, there are no ancestors to find
if (pos === -1) return ROOT_NODE_KEY;

const $pos = editorView.state.doc.resolve(pos);

for (let d = $pos.depth; d > 0; d--) {
Expand Down
Loading