diff --git a/.yarn/versions/d7f962a1.yml b/.yarn/versions/d7f962a1.yml new file mode 100644 index 00000000..e69de29b diff --git a/package.json b/package.json index a599f5a4..3d903e4c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/nodeViews/__tests__/createReactNodeViewConstructor.test.ts b/src/nodeViews/__tests__/createReactNodeViewConstructor.test.ts new file mode 100644 index 00000000..8490eec8 --- /dev/null +++ b/src/nodeViews/__tests__/createReactNodeViewConstructor.test.ts @@ -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); + }); +}); diff --git a/src/nodeViews/createReactNodeViewConstructor.tsx b/src/nodeViews/createReactNodeViewConstructor.tsx index b9300e7f..f3127f8a 100644 --- a/src/nodeViews/createReactNodeViewConstructor.tsx +++ b/src/nodeViews/createReactNodeViewConstructor.tsx @@ -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--) {