@handlewithcare/react-prosemirror 2.4.12 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/dist/cjs/AbstractEditorView.js +4 -0
  2. package/dist/cjs/ReactEditorView.js +156 -0
  3. package/dist/cjs/StaticEditorView.js +86 -0
  4. package/dist/cjs/components/ChildNodeViews.js +59 -30
  5. package/dist/cjs/components/CustomNodeView.js +9 -25
  6. package/dist/cjs/components/DocNodeView.js +6 -15
  7. package/dist/cjs/components/MarkView.js +1 -2
  8. package/dist/cjs/components/NativeWidgetView.js +2 -3
  9. package/dist/cjs/components/NodeView.js +1 -1
  10. package/dist/cjs/components/ProseMirror.js +11 -14
  11. package/dist/cjs/components/ReactNodeView.js +3 -4
  12. package/dist/cjs/components/SeparatorHackView.js +1 -2
  13. package/dist/cjs/components/TextNodeView.js +4 -5
  14. package/dist/cjs/components/TrailingHackView.js +1 -2
  15. package/dist/cjs/components/WidgetView.js +2 -4
  16. package/dist/cjs/constants.js +33 -0
  17. package/dist/cjs/hooks/useEditor.js +32 -228
  18. package/dist/cjs/hooks/useEditorEffect.js +2 -2
  19. package/dist/cjs/hooks/useEditorEventCallback.js +8 -5
  20. package/dist/cjs/hooks/useNodeViewDescriptor.js +10 -10
  21. package/dist/cjs/hooks/useReactKeys.js +1 -1
  22. package/dist/cjs/testing/editorViewTestHelpers.js +0 -2
  23. package/dist/cjs/viewdesc.js +10 -9
  24. package/dist/esm/AbstractEditorView.js +1 -0
  25. package/dist/esm/ReactEditorView.js +156 -0
  26. package/dist/esm/StaticEditorView.js +76 -0
  27. package/dist/esm/components/ChildNodeViews.js +60 -32
  28. package/dist/esm/components/CustomNodeView.js +9 -25
  29. package/dist/esm/components/DocNodeView.js +6 -15
  30. package/dist/esm/components/MarkView.js +1 -2
  31. package/dist/esm/components/NativeWidgetView.js +2 -3
  32. package/dist/esm/components/NodeView.js +1 -1
  33. package/dist/esm/components/ProseMirror.js +11 -14
  34. package/dist/esm/components/ReactNodeView.js +3 -4
  35. package/dist/esm/components/SeparatorHackView.js +1 -2
  36. package/dist/esm/components/TextNodeView.js +4 -5
  37. package/dist/esm/components/TrailingHackView.js +1 -2
  38. package/dist/esm/components/WidgetView.js +2 -4
  39. package/dist/esm/constants.js +15 -0
  40. package/dist/esm/hooks/useEditor.js +28 -217
  41. package/dist/esm/hooks/useEditorEffect.js +2 -2
  42. package/dist/esm/hooks/useEditorEventCallback.js +8 -5
  43. package/dist/esm/hooks/useNodeViewDescriptor.js +10 -10
  44. package/dist/esm/hooks/useReactKeys.js +1 -1
  45. package/dist/esm/testing/editorViewTestHelpers.js +0 -2
  46. package/dist/esm/viewdesc.js +3 -2
  47. package/dist/tsconfig.tsbuildinfo +1 -1
  48. package/dist/types/AbstractEditorView.d.ts +27 -0
  49. package/dist/types/ReactEditorView.d.ts +79 -0
  50. package/dist/types/StaticEditorView.d.ts +24 -0
  51. package/dist/types/components/ChildNodeViews.d.ts +2 -2
  52. package/dist/types/components/CustomNodeView.d.ts +2 -2
  53. package/dist/types/components/DocNodeView.d.ts +2 -5
  54. package/dist/types/components/MarkView.d.ts +2 -2
  55. package/dist/types/components/NativeWidgetView.d.ts +2 -2
  56. package/dist/types/components/NodeView.d.ts +2 -2
  57. package/dist/types/components/ReactNodeView.d.ts +2 -2
  58. package/dist/types/components/SeparatorHackView.d.ts +2 -2
  59. package/dist/types/components/TextNodeView.d.ts +4 -3
  60. package/dist/types/components/TrailingHackView.d.ts +2 -2
  61. package/dist/types/components/WidgetView.d.ts +2 -2
  62. package/dist/types/constants.d.ts +4 -0
  63. package/dist/types/contexts/EditorContext.d.ts +6 -4
  64. package/dist/types/decorations/computeDocDeco.d.ts +3 -2
  65. package/dist/types/decorations/viewDecorations.d.ts +3 -2
  66. package/dist/types/hooks/useEditor.d.ts +5 -46
  67. package/dist/types/hooks/useNodeViewDescriptor.d.ts +1 -1
  68. package/dist/types/hooks/useReactKeys.d.ts +1 -1
  69. package/dist/types/props.d.ts +3 -3
  70. package/dist/types/viewdesc.d.ts +6 -5
  71. package/package.json +6 -2
  72. package/dist/cjs/components/Editor.js +0 -28
  73. package/dist/cjs/components/NodeViews.js +0 -73
  74. package/dist/cjs/components/__tests__/LayoutGroup.test.js +0 -141
  75. package/dist/cjs/components/__tests__/ProseMirror.test.js +0 -255
  76. package/dist/cjs/contexts/NodeViewsContext.js +0 -10
  77. package/dist/cjs/hooks/__tests__/useEditorViewLayoutEffect.test.js +0 -107
  78. package/dist/cjs/hooks/__tests__/useNodeViews.test.js +0 -159
  79. package/dist/cjs/hooks/useEditorView.js +0 -100
  80. package/dist/cjs/hooks/useNodePos.js +0 -69
  81. package/dist/cjs/hooks/useNodeViews.js +0 -100
  82. package/dist/cjs/nodeViews/createReactNodeViewConstructor.js +0 -244
  83. package/dist/cjs/nodeViews/phrasingContentTags.js +0 -57
  84. package/dist/cjs/plugins/__tests__/react.test.js +0 -139
  85. package/dist/cjs/plugins/react.js +0 -71
  86. package/dist/cjs/selection/SelectionDOMObserver.js +0 -171
  87. package/dist/cjs/selection/hasFocusAndSelection.js +0 -35
  88. package/dist/cjs/selection/selectionFromDOM.js +0 -77
  89. package/dist/cjs/selection/selectionToDOM.js +0 -226
  90. package/dist/cjs/ssr.js +0 -85
  91. package/dist/esm/components/Editor.js +0 -15
  92. package/dist/esm/components/NodeViews.js +0 -26
  93. package/dist/esm/components/__tests__/LayoutGroup.test.js +0 -98
  94. package/dist/esm/components/__tests__/ProseMirror.test.js +0 -207
  95. package/dist/esm/contexts/NodeViewsContext.js +0 -9
  96. package/dist/esm/hooks/__tests__/useEditorViewLayoutEffect.test.js +0 -98
  97. package/dist/esm/hooks/__tests__/useNodeViews.test.js +0 -116
  98. package/dist/esm/hooks/useEditorView.js +0 -99
  99. package/dist/esm/hooks/useNodePos.js +0 -16
  100. package/dist/esm/hooks/useNodeViews.js +0 -53
  101. package/dist/esm/nodeViews/createReactNodeViewConstructor.js +0 -214
  102. package/dist/esm/nodeViews/phrasingContentTags.js +0 -49
  103. package/dist/esm/plugins/__tests__/react.test.js +0 -135
  104. package/dist/esm/plugins/react.js +0 -64
  105. package/dist/esm/selection/SelectionDOMObserver.js +0 -161
  106. package/dist/esm/selection/hasFocusAndSelection.js +0 -17
  107. package/dist/esm/selection/selectionFromDOM.js +0 -59
  108. package/dist/esm/selection/selectionToDOM.js +0 -196
  109. package/dist/esm/ssr.js +0 -82
  110. package/dist/types/components/Editor.d.ts +0 -7
  111. package/dist/types/components/NodeViews.d.ts +0 -6
  112. package/dist/types/components/__tests__/LayoutGroup.test.d.ts +0 -1
  113. package/dist/types/contexts/NodeViewsContext.d.ts +0 -19
  114. package/dist/types/hooks/__tests__/useEditorViewLayoutEffect.test.d.ts +0 -1
  115. package/dist/types/hooks/__tests__/useNodeViews.test.d.ts +0 -1
  116. package/dist/types/hooks/useEditorView.d.ts +0 -23
  117. package/dist/types/hooks/useNodePos.d.ts +0 -9
  118. package/dist/types/hooks/useNodeViews.d.ts +0 -5
  119. package/dist/types/nodeViews/createReactNodeViewConstructor.d.ts +0 -48
  120. package/dist/types/nodeViews/phrasingContentTags.d.ts +0 -1
  121. package/dist/types/plugins/__tests__/react.test.d.ts +0 -1
  122. package/dist/types/plugins/react.d.ts +0 -21
  123. package/dist/types/selection/SelectionDOMObserver.d.ts +0 -33
  124. package/dist/types/selection/hasFocusAndSelection.d.ts +0 -3
  125. package/dist/types/selection/selectionFromDOM.d.ts +0 -4
  126. package/dist/types/selection/selectionToDOM.d.ts +0 -9
  127. package/dist/types/ssr.d.ts +0 -19
package/dist/esm/ssr.js DELETED
@@ -1,82 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-empty-function */ /**
2
- * @fileoverview
3
- *
4
- * Stubs for ProseMirror View during SSR. These are extremely
5
- * barebones, because they _do not need to actually work_. They
6
- * just need to prevent errors from being thrown when ProseMirror
7
- * View attemps to access these APIs while constructing the
8
- * initial EditorView. None of these APIs are necessary for SSR to
9
- * work properly, so it's fine that they're all no-ops.
10
- */ let ClassList = class ClassList {
11
- add() {}
12
- remove() {}
13
- };
14
- let ElementStub = class ElementStub {
15
- get parent() {
16
- return new ElementStub();
17
- }
18
- get parentNode() {
19
- return new ElementStub();
20
- }
21
- nodeName = "div";
22
- appendChild() {
23
- return new ElementStub();
24
- }
25
- setAttribute() {}
26
- hasAttribute() {
27
- return false;
28
- }
29
- insertBefore() {}
30
- get classList() {
31
- return new ClassList();
32
- }
33
- get ownerDocument() {
34
- return new DocumentStub();
35
- }
36
- style = {};
37
- addEventListener() {}
38
- removeEventListener() {}
39
- replaceChildren() {}
40
- };
41
- let DocumentStub = class DocumentStub {
42
- createElement() {
43
- return new ElementStub();
44
- }
45
- addEventListener() {}
46
- removeEventListener() {}
47
- get documentElement() {
48
- return new ElementStub();
49
- }
50
- };
51
- /**
52
- * Sets up tiny no-op stubs for the global window and document.
53
- * These are used to prevent errors from being thrown when ProseMirror's
54
- * EditorView attempts to access the DOM in its constructor during SSR.
55
- *
56
- * Returns a cleanup function that resets the window and document back
57
- * to their original values (undefined).
58
- */ export function setSsrStubs() {
59
- const prevWindow = globalThis.window;
60
- // @ts-expect-error HACK - EditorView checks for window.MutationObserver
61
- // in its constructor, which breaks SSR. We temporarily set window
62
- // to an empty object to prevent an error from being thrown, and then
63
- // clean it up so that other isomorphic code doesn't get confused about
64
- // whether there's a functioning global window object
65
- globalThis.window ??= {
66
- visualViewport: null
67
- };
68
- const prevDocument = globalThis.document;
69
- // @ts-expect-error HACK: This is only used during SSR, and only
70
- // to prevent outright errors when ProseMirror View attempts to
71
- // access document properties either on import or when constructing
72
- // the EditorView.
73
- globalThis.document ??= new DocumentStub();
74
- return function cleanupSsrStubs() {
75
- if (globalThis.window !== prevWindow) {
76
- globalThis.window = prevWindow;
77
- }
78
- if (globalThis.document !== prevDocument) {
79
- globalThis.document = prevDocument;
80
- }
81
- };
82
- }
@@ -1,7 +0,0 @@
1
- import type { ReactNode } from "react";
2
- import type { UseEditorViewOptions } from "../hooks/useEditorView.js";
3
- export interface EditorProps extends UseEditorViewOptions {
4
- mount: HTMLElement | null;
5
- children?: ReactNode | null;
6
- }
7
- export declare function Editor({ mount, children, ...options }: EditorProps): JSX.Element;
@@ -1,6 +0,0 @@
1
- import type { NodeViewsContextValue } from "../contexts/NodeViewsContext.js";
2
- type NodeViewsProps = {
3
- portals: NodeViewsContextValue;
4
- };
5
- export declare function NodeViews({ portals }: NodeViewsProps): JSX.Element;
6
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,19 +0,0 @@
1
- import type { ReactPortal } from "react";
2
- import type { NodeKey } from "../plugins/react";
3
- type NodeViewRegistration = {
4
- getPos: () => number;
5
- portal: ReactPortal;
6
- };
7
- export interface NodeViewsContextValue {
8
- [key: NodeKey]: NodeViewRegistration[];
9
- }
10
- /**
11
- * A context containing a map of node view keys to portals.
12
- *
13
- * Each node view registers a portal under its parent's
14
- * key. Each can then retrieve the list of portals under their
15
- * key, allowing portals to be rendered with the appropriate
16
- * hierarchy.
17
- */
18
- export declare const NodeViewsContext: import("react").Context<NodeViewsContextValue>;
19
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,23 +0,0 @@
1
- import { EditorState } from "prosemirror-state";
2
- import type { Plugin, Transaction } from "prosemirror-state";
3
- import { EditorView } from "prosemirror-view";
4
- import type { EditorProps } from "prosemirror-view";
5
- import type { EditorContextValue } from "../contexts/EditorContext.js";
6
- import type { ReactNodeViewConstructor } from "../nodeViews/createReactNodeViewConstructor.js";
7
- export interface UseEditorViewOptions extends EditorProps {
8
- nodeViews?: Record<string, ReactNodeViewConstructor>;
9
- defaultState?: EditorState;
10
- state?: EditorState;
11
- plugins?: Plugin[];
12
- view?: EditorView | null;
13
- dispatchTransaction?(this: EditorView, tr: Transaction): void;
14
- }
15
- /**
16
- * Creates, mounts, and manages a ProseMirror `EditorView`.
17
- *
18
- * All state and props updates are executed in a layout effect.
19
- * To ensure that the EditorState and EditorView are never out of
20
- * sync, it's important that the EditorView produced by this hook
21
- * is only accessed through the provided hooks.
22
- */
23
- export declare function useEditorView<T extends HTMLElement = HTMLElement>(mount: T | null, options: UseEditorViewOptions): EditorContextValue;
@@ -1,9 +0,0 @@
1
- import type { ReactNode } from "react";
2
- import type { NodeKey } from "../plugins/react.js";
3
- type Props = {
4
- nodeKey: NodeKey;
5
- children: ReactNode;
6
- };
7
- export declare function NodePosProvider({ nodeKey, children }: Props): JSX.Element;
8
- export declare function useNodePos(): number;
9
- export {};
@@ -1,5 +0,0 @@
1
- import type { ReactNodeViewConstructor } from "../nodeViews/createReactNodeViewConstructor.js";
2
- export declare function useNodeViews(nodeViews?: Record<string, ReactNodeViewConstructor>): {
3
- nodeViews: any;
4
- nodeViewsComponent: JSX.Element;
5
- };
@@ -1,48 +0,0 @@
1
- import type { Node } from "prosemirror-model";
2
- import type { Decoration, DecorationSource, EditorView, NodeView, NodeViewConstructor } from "prosemirror-view";
3
- import type { ComponentType, ReactNode, ReactPortal } from "react";
4
- import type { NodeKey } from "../plugins/react.js";
5
- export interface NodeViewComponentProps {
6
- decorations: readonly Decoration[];
7
- node: Node;
8
- children: ReactNode;
9
- isSelected: boolean;
10
- }
11
- export type UnregisterElement = () => void;
12
- export type RegisterPortal = (view: EditorView, getPos: () => number, portal: ReactPortal) => UnregisterElement;
13
- export type ReactNodeView = NodeView & {
14
- component?: ComponentType<NodeViewComponentProps>;
15
- };
16
- export type ReactNodeViewConstructor = (...args: Parameters<NodeViewConstructor>) => ReactNodeView;
17
- /**
18
- * Identifies a node view constructor as having been created
19
- * by @nytimes/react-prosemirror
20
- */
21
- export declare const REACT_NODE_VIEW: unique symbol;
22
- /**
23
- * Searches upward for the nearest node with a node key,
24
- * returning the first node key it finds associated with
25
- * a React node view.
26
- *
27
- * Returns the root key if no ancestor nodes have node keys.
28
- */
29
- export declare function findNodeKeyUp(editorView: EditorView, pos: number): NodeKey;
30
- /**
31
- * Factory function for creating nodeViewConstructors that
32
- * render as React components.
33
- *
34
- * `NodeView` can be any React component that takes
35
- * `NodeViewComponentProps`. It will be passed all of the
36
- * arguments to the `nodeViewConstructor` except for
37
- * `editorView`. NodeView components that need access
38
- * directly to the EditorView should use the
39
- * `useEditorViewEvent` and `useEditorViewLayoutEffect`
40
- * hooks to ensure safe access.
41
- *
42
- * For contentful Nodes, the NodeView component will also
43
- * be passed a `children` prop containing an empty element.
44
- * ProseMirror will render content nodes into this element.
45
- */
46
- export declare function createReactNodeViewConstructor(nodeViewConstructor: ReactNodeViewConstructor, registerPortal: RegisterPortal): ((node: Node, editorView: EditorView, getPos: () => number, decorations: readonly Decoration[], innerDecorations: DecorationSource) => NodeView) & {
47
- [REACT_NODE_VIEW]: boolean;
48
- };
@@ -1 +0,0 @@
1
- export declare const phrasingContentTags: string[];
@@ -1 +0,0 @@
1
- export {};
@@ -1,21 +0,0 @@
1
- import { Plugin, PluginKey } from "prosemirror-state";
2
- /**
3
- * This is a stand-in for the doc node itself, which doesn't have a
4
- * unique position to map to.
5
- */
6
- export declare const ROOT_NODE_KEY: unique symbol;
7
- export type NodeKey = string | symbol;
8
- export declare function createNodeKey(): string;
9
- export type ReactPluginState = {
10
- posToKey: Map<number, string>;
11
- keyToPos: Map<NodeKey, number>;
12
- };
13
- export declare const reactPluginKey: PluginKey<ReactPluginState>;
14
- /**
15
- * Tracks a unique key for each (non-text) node in the
16
- * document, identified by its current position. Keys are
17
- * (mostly) stable across transaction applications. The
18
- * key for a given node can be accessed by that node's
19
- * current position in the document, and vice versa.
20
- */
21
- export declare function react(): Plugin<ReactPluginState>;
@@ -1,33 +0,0 @@
1
- import { EditorView } from "prosemirror-view";
2
- import { DOMSelectionRange } from "../dom.js";
3
- declare class SelectionState {
4
- anchorNode: Node | null;
5
- anchorOffset: number;
6
- focusNode: Node | null;
7
- focusOffset: number;
8
- set(sel: DOMSelectionRange): void;
9
- clear(): void;
10
- eq(sel: DOMSelectionRange): boolean;
11
- }
12
- export declare class SelectionDOMObserver {
13
- readonly view: EditorView;
14
- flushingSoon: number;
15
- currentSelection: SelectionState;
16
- suppressingSelectionUpdates: boolean;
17
- constructor(view: EditorView);
18
- connectSelection(): void;
19
- disconnectSelection(): void;
20
- stop(): void;
21
- start(): void;
22
- suppressSelectionUpdates(): void;
23
- setCurSelection(): void;
24
- ignoreSelectionChange(sel: DOMSelectionRange): true | undefined;
25
- registerMutation(): void;
26
- flushSoon(): void;
27
- updateSelection(): void;
28
- selectionToDOM(): void;
29
- flush(): void;
30
- forceFlush(): void;
31
- onSelectionChange(): void;
32
- }
33
- export {};
@@ -1,3 +0,0 @@
1
- import { EditorView } from "prosemirror-view";
2
- export declare function hasFocusAndSelection(view: EditorView): boolean;
3
- export declare function hasSelection(view: EditorView): boolean;
@@ -1,4 +0,0 @@
1
- import { ResolvedPos } from "prosemirror-model";
2
- import { EditorView } from "prosemirror-view";
3
- export declare function selectionBetween(view: EditorView, $anchor: ResolvedPos, $head: ResolvedPos, bias?: number): import("prosemirror-state").Selection;
4
- export declare function selectionFromDOM(view: EditorView, origin?: string | null): import("prosemirror-state").Selection | null;
@@ -1,9 +0,0 @@
1
- import { Selection } from "prosemirror-state";
2
- import { EditorView } from "prosemirror-view";
3
- export declare const isEquivalentPosition: (node: Node, off: number, targetNode: Node, targetOff: number) => boolean;
4
- export declare function hasBlockDesc(dom: Node): boolean | null | undefined;
5
- export declare const domIndex: (node: Node) => number;
6
- export declare function nodeSize(node: Node): number;
7
- export declare function syncNodeSelection(view: EditorView, sel: Selection): void;
8
- export declare function hasSelection(view: EditorView): boolean;
9
- export declare function selectionToDOM(view: EditorView, force?: boolean): void;
@@ -1,19 +0,0 @@
1
- /**
2
- * @fileoverview
3
- *
4
- * Stubs for ProseMirror View during SSR. These are extremely
5
- * barebones, because they _do not need to actually work_. They
6
- * just need to prevent errors from being thrown when ProseMirror
7
- * View attemps to access these APIs while constructing the
8
- * initial EditorView. None of these APIs are necessary for SSR to
9
- * work properly, so it's fine that they're all no-ops.
10
- */
11
- /**
12
- * Sets up tiny no-op stubs for the global window and document.
13
- * These are used to prevent errors from being thrown when ProseMirror's
14
- * EditorView attempts to access the DOM in its constructor during SSR.
15
- *
16
- * Returns a cleanup function that resets the window and document back
17
- * to their original values (undefined).
18
- */
19
- export declare function setSsrStubs(): () => void;