@handlewithcare/react-prosemirror 3.1.0-tiptap.35 → 3.1.0-tiptap.36

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 (47) hide show
  1. package/dist/cjs/components/CustomNodeView.js +132 -0
  2. package/dist/cjs/components/DefaultNodeView.js +67 -0
  3. package/dist/cjs/components/DocNodeView.js +96 -0
  4. package/dist/cjs/components/MarkView.js +119 -0
  5. package/dist/cjs/components/NodeView.js +86 -0
  6. package/dist/cjs/components/NodeViewComponentProps.js +4 -0
  7. package/dist/cjs/components/ReactNodeView.js +174 -0
  8. package/dist/cjs/components/marks/CustomMarkView.js +110 -0
  9. package/dist/cjs/components/marks/OldMarkView.js +120 -0
  10. package/dist/cjs/components/nodes/CustomNodeView.js +135 -0
  11. package/dist/cjs/components/nodes/ReactNodeView.js +1 -1
  12. package/dist/cjs/contexts/ChildDescriptorsContext.js +19 -0
  13. package/dist/cjs/hooks/useNodeViewDescriptor.js +156 -0
  14. package/dist/cjs/tiptap/TiptapNodeView.js +26 -0
  15. package/dist/cjs/tiptap/tiptapNodeView.js +5 -6
  16. package/dist/esm/components/CustomNodeView.js +81 -0
  17. package/dist/esm/components/DefaultNodeView.js +16 -0
  18. package/dist/esm/components/DocNodeView.js +45 -0
  19. package/dist/esm/components/MarkView.js +68 -0
  20. package/dist/esm/components/NodeView.js +35 -0
  21. package/dist/esm/components/NodeViewComponentProps.js +1 -0
  22. package/dist/esm/components/ReactNodeView.js +123 -0
  23. package/dist/esm/components/marks/CustomMarkView.js +59 -0
  24. package/dist/esm/components/marks/OldMarkView.js +69 -0
  25. package/dist/esm/components/nodes/CustomNodeView.js +84 -0
  26. package/dist/esm/components/nodes/ReactNodeView.js +1 -1
  27. package/dist/esm/contexts/ChildDescriptorsContext.js +9 -0
  28. package/dist/esm/hooks/useNodeViewDescriptor.js +146 -0
  29. package/dist/esm/tiptap/TiptapNodeView.js +22 -0
  30. package/dist/esm/tiptap/tiptapNodeView.js +5 -6
  31. package/dist/tsconfig.tsbuildinfo +1 -1
  32. package/dist/types/components/CustomNodeView.d.ts +12 -0
  33. package/dist/types/components/DefaultNodeView.d.ts +3 -0
  34. package/dist/types/components/DocNodeView.d.ts +12 -0
  35. package/dist/types/components/MarkView.d.ts +9 -0
  36. package/dist/types/components/NodeView.d.ts +11 -0
  37. package/dist/types/components/NodeViewComponentProps.d.ts +12 -0
  38. package/dist/types/components/ReactNodeView.d.ts +13 -0
  39. package/dist/types/components/marks/CustomMarkView.d.ts +12 -0
  40. package/dist/types/components/marks/OldMarkView.d.ts +10 -0
  41. package/dist/types/components/nodes/CustomNodeView.d.ts +12 -0
  42. package/dist/types/constants.d.ts +1 -1
  43. package/dist/types/contexts/ChildDescriptorsContext.d.ts +6 -0
  44. package/dist/types/hooks/useNodeViewDescriptor.d.ts +20 -0
  45. package/dist/types/props.d.ts +26 -26
  46. package/dist/types/tiptap/TiptapNodeView.d.ts +15 -0
  47. package/package.json +1 -1
@@ -0,0 +1,81 @@
1
+ import { DOMSerializer } from "prosemirror-model";
2
+ import React, { cloneElement, memo, useMemo, useRef } from "react";
3
+ import { createPortal } from "react-dom";
4
+ import { ChildDescriptorsContext } from "../contexts/ChildDescriptorsContext.js";
5
+ import { useNodeViewDescriptor } from "../hooks/useNodeViewDescriptor.js";
6
+ import { ChildNodeViews, wrapInDeco } from "./ChildNodeViews.js";
7
+ export const CustomNodeView = /*#__PURE__*/ memo(function CustomNodeView(param) {
8
+ let { constructor, node, getPos, innerDeco, outerDeco } = param;
9
+ const ref = useRef(null);
10
+ const innerRef = useRef(null);
11
+ const nodeProps = useMemo(()=>({
12
+ node,
13
+ getPos,
14
+ decorations: outerDeco,
15
+ innerDecorations: innerDeco
16
+ }), [
17
+ node,
18
+ getPos,
19
+ outerDeco,
20
+ innerDeco
21
+ ]);
22
+ const createNodeView = function() {
23
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
24
+ args[_key] = arguments[_key];
25
+ }
26
+ const nodeView = constructor(...args);
27
+ if (!nodeView || !nodeView.dom) {
28
+ const spec = node.type.spec.toDOM?.(node);
29
+ if (!spec) {
30
+ throw new Error(`Node spec for ${node.type.name} is missing toDOM`);
31
+ }
32
+ return DOMSerializer.renderSpec(document, spec, null);
33
+ }
34
+ return nodeView;
35
+ };
36
+ const { childContextValue, contentDOM } = useNodeViewDescriptor(ref, function() {
37
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
38
+ args[_key] = arguments[_key];
39
+ }
40
+ const nodeView = createNodeView(...args);
41
+ const contentDOM = nodeView.contentDOM;
42
+ const nodeDOM = nodeView.dom;
43
+ const wrapperDOM = innerRef.current ?? ref.current;
44
+ wrapperDOM.appendChild(nodeDOM);
45
+ if (!contentDOM && nodeDOM instanceof HTMLElement && nodeDOM.tagName !== "BR") {
46
+ if (!nodeDOM.hasAttribute("contenteditable")) {
47
+ nodeDOM.contentEditable = "false";
48
+ }
49
+ if (node.type.spec.draggable) {
50
+ nodeDOM.draggable = true;
51
+ }
52
+ }
53
+ return {
54
+ ...nodeView,
55
+ destroy () {
56
+ if (nodeView.destroy) {
57
+ nodeView.destroy();
58
+ }
59
+ wrapperDOM.removeChild(nodeDOM);
60
+ },
61
+ selectNode: nodeView.selectNode?.bind(nodeView),
62
+ deselectNode: nodeView.deselectNode?.bind(nodeView),
63
+ stopEvent: nodeView.stopEvent?.bind(nodeView),
64
+ ignoreMutation: nodeView.ignoreMutation?.bind(nodeView)
65
+ };
66
+ }, nodeProps);
67
+ const Component = node.isInline ? "span" : "div";
68
+ const props = {
69
+ ref: innerRef
70
+ };
71
+ const children = !node.isLeaf && contentDOM ? /*#__PURE__*/ createPortal(/*#__PURE__*/ React.createElement(ChildDescriptorsContext.Provider, {
72
+ value: childContextValue
73
+ }, /*#__PURE__*/ React.createElement(ChildNodeViews, {
74
+ getPos: getPos,
75
+ node: node,
76
+ innerDecorations: innerDeco
77
+ })), contentDOM) : null;
78
+ return /*#__PURE__*/ cloneElement(outerDeco.reduce(wrapInDeco, /*#__PURE__*/ React.createElement(Component, props, children)), {
79
+ ref
80
+ });
81
+ });
@@ -0,0 +1,16 @@
1
+ import React, { forwardRef, useMemo } from "react";
2
+ import { OutputSpec } from "./OutputSpec.js";
3
+ export const DefaultNodeView = /*#__PURE__*/ forwardRef(function DefaultNodeView(param, ref) {
4
+ let { nodeProps: { node }, children, ...props } = param;
5
+ const spec = useMemo(()=>node.type.spec.toDOM?.(node), [
6
+ node
7
+ ]);
8
+ if (!spec) {
9
+ throw new Error(`Node spec for ${node.type.name} is missing toDOM`);
10
+ }
11
+ return /*#__PURE__*/ React.createElement(OutputSpec, {
12
+ ...props,
13
+ outputSpec: spec,
14
+ ref: ref
15
+ }, children);
16
+ });
@@ -0,0 +1,45 @@
1
+ import React, { cloneElement, createElement, forwardRef, memo, useImperativeHandle, useMemo, useRef } from "react";
2
+ import { ChildDescriptorsContext } from "../contexts/ChildDescriptorsContext.js";
3
+ import { useNodeViewDescriptor } from "../hooks/useNodeViewDescriptor.js";
4
+ import { ChildNodeViews, wrapInDeco } from "./ChildNodeViews.js";
5
+ export const DocNodeView = /*#__PURE__*/ memo(/*#__PURE__*/ forwardRef(function DocNodeView(param, ref) {
6
+ let { as, node, getPos, decorations, innerDecorations, setMount, ...elementProps } = param;
7
+ const innerRef = useRef(null);
8
+ useImperativeHandle(ref, ()=>innerRef.current);
9
+ useImperativeHandle(setMount, ()=>innerRef.current);
10
+ const nodeProps = useMemo(()=>({
11
+ node,
12
+ getPos,
13
+ decorations,
14
+ innerDecorations
15
+ }), [
16
+ node,
17
+ getPos,
18
+ decorations,
19
+ innerDecorations
20
+ ]);
21
+ const { childContextValue } = useNodeViewDescriptor(innerRef, ()=>{
22
+ const dom = innerRef.current;
23
+ return {
24
+ dom,
25
+ contentDOM: dom,
26
+ update () {
27
+ return true;
28
+ }
29
+ };
30
+ }, nodeProps);
31
+ const children = /*#__PURE__*/ React.createElement(ChildDescriptorsContext.Provider, {
32
+ value: childContextValue
33
+ }, /*#__PURE__*/ React.createElement(ChildNodeViews, {
34
+ getPos: getPos,
35
+ node: node,
36
+ innerDecorations: innerDecorations
37
+ }));
38
+ const props = {
39
+ ...elementProps,
40
+ suppressContentEditableWarning: true,
41
+ ref: innerRef
42
+ };
43
+ const element = as ? /*#__PURE__*/ cloneElement(as, props, children) : /*#__PURE__*/ createElement("div", props, children);
44
+ return nodeProps.decorations.reduce(wrapInDeco, element);
45
+ }));
@@ -0,0 +1,68 @@
1
+ import React, { forwardRef, memo, useContext, useImperativeHandle, useMemo, useRef } from "react";
2
+ import { ChildDescriptorsContext } from "../contexts/ChildDescriptorsContext.js";
3
+ import { useClientLayoutEffect } from "../hooks/useClientLayoutEffect.js";
4
+ import { MarkViewDesc, sortViewDescs } from "../viewdesc.js";
5
+ import { OutputSpec } from "./OutputSpec.js";
6
+ export const MarkView = /*#__PURE__*/ memo(/*#__PURE__*/ forwardRef(function MarkView(param, ref) {
7
+ let { mark, getPos, children } = param;
8
+ const { siblingsRef, parentRef } = useContext(ChildDescriptorsContext);
9
+ const viewDescRef = useRef(undefined);
10
+ const childDescriptors = useRef([]);
11
+ const domRef = useRef(null);
12
+ useImperativeHandle(ref, ()=>{
13
+ return domRef.current;
14
+ }, []);
15
+ const outputSpec = useMemo(()=>mark.type.spec.toDOM?.(mark, true), [
16
+ mark
17
+ ]);
18
+ if (!outputSpec) throw new Error(`Mark spec for ${mark.type.name} is missing toDOM`);
19
+ useClientLayoutEffect(()=>{
20
+ const siblings = siblingsRef.current;
21
+ return ()=>{
22
+ if (!viewDescRef.current) return;
23
+ if (siblings.includes(viewDescRef.current)) {
24
+ const index = siblings.indexOf(viewDescRef.current);
25
+ siblings.splice(index, 1);
26
+ }
27
+ };
28
+ }, [
29
+ siblingsRef
30
+ ]);
31
+ useClientLayoutEffect(()=>{
32
+ if (!domRef.current) return;
33
+ const firstChildDesc = childDescriptors.current[0];
34
+ if (!viewDescRef.current) {
35
+ viewDescRef.current = new MarkViewDesc(parentRef.current, childDescriptors.current, getPos, mark, domRef.current, firstChildDesc?.dom.parentElement ?? domRef.current, {
36
+ dom: domRef.current,
37
+ contentDOM: firstChildDesc?.dom.parentElement ?? domRef.current
38
+ });
39
+ } else {
40
+ viewDescRef.current.parent = parentRef.current;
41
+ viewDescRef.current.spec.dom = viewDescRef.current.dom = domRef.current;
42
+ viewDescRef.current.children = childDescriptors.current;
43
+ viewDescRef.current.spec.contentDOM = viewDescRef.current.contentDOM = firstChildDesc?.dom.parentElement ?? domRef.current;
44
+ viewDescRef.current.mark = mark;
45
+ }
46
+ if (!siblingsRef.current.includes(viewDescRef.current)) {
47
+ siblingsRef.current.push(viewDescRef.current);
48
+ }
49
+ siblingsRef.current.sort(sortViewDescs);
50
+ for (const childDesc of childDescriptors.current){
51
+ childDesc.parent = viewDescRef.current;
52
+ }
53
+ });
54
+ const childContextValue = useMemo(()=>({
55
+ parentRef: viewDescRef,
56
+ siblingsRef: childDescriptors
57
+ }), [
58
+ childDescriptors,
59
+ viewDescRef
60
+ ]);
61
+ return /*#__PURE__*/ React.createElement(OutputSpec, {
62
+ ref: domRef,
63
+ outputSpec: outputSpec,
64
+ isMark: true
65
+ }, /*#__PURE__*/ React.createElement(ChildDescriptorsContext.Provider, {
66
+ value: childContextValue
67
+ }, children));
68
+ }));
@@ -0,0 +1,35 @@
1
+ import React, { memo, useContext, useMemo } from "react";
2
+ import { NodeViewContext } from "../contexts/NodeViewContext.js";
3
+ import { CustomNodeView } from "./CustomNodeView.js";
4
+ import { DefaultNodeView } from "./DefaultNodeView.js";
5
+ import { ReactNodeView } from "./ReactNodeView.js";
6
+ export const NodeView = /*#__PURE__*/ memo(function NodeView(props) {
7
+ const { components, constructors } = useContext(NodeViewContext);
8
+ const component = components[props.node.type.name] ?? DefaultNodeView;
9
+ const constructor = constructors[props.node.type.name];
10
+ // Construct a wrapper component so that the node view remounts when either
11
+ // its component or constructor changes. A React node view would remount if
12
+ // its underlying component changed without this wrapper, but a custom node
13
+ // view otherwise uses the same React components for all custom node views.
14
+ const Component = useMemo(()=>{
15
+ if (constructor) {
16
+ return function NodeView(props) {
17
+ return /*#__PURE__*/ React.createElement(CustomNodeView, {
18
+ constructor: constructor,
19
+ ...props
20
+ });
21
+ };
22
+ } else {
23
+ return function NodeView(props) {
24
+ return /*#__PURE__*/ React.createElement(ReactNodeView, {
25
+ component: component,
26
+ ...props
27
+ });
28
+ };
29
+ }
30
+ }, [
31
+ constructor,
32
+ component
33
+ ]);
34
+ return /*#__PURE__*/ React.createElement(Component, props);
35
+ });
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,123 @@
1
+ import React, { cloneElement, memo, useCallback, useMemo, useRef, useState } from "react";
2
+ import { ChildDescriptorsContext } from "../contexts/ChildDescriptorsContext.js";
3
+ import { IgnoreMutationContext } from "../contexts/IgnoreMutationContext.js";
4
+ import { SelectNodeContext } from "../contexts/SelectNodeContext.js";
5
+ import { StopEventContext } from "../contexts/StopEventContext.js";
6
+ import { useNodeViewDescriptor } from "../hooks/useNodeViewDescriptor.js";
7
+ import { ChildNodeViews, wrapInDeco } from "./ChildNodeViews.js";
8
+ export const ReactNodeView = /*#__PURE__*/ memo(function ReactNodeView(param) {
9
+ let { component: Component, outerDeco, getPos, node, innerDeco } = param;
10
+ const [hasCustomSelectNode, setHasCustomSelectNode] = useState(false);
11
+ const [selected, setSelected] = useState(false);
12
+ const ref = useRef(null);
13
+ const innerRef = useRef(null);
14
+ const selectNodeRef = useRef(null);
15
+ const deselectNodeRef = useRef(null);
16
+ const stopEventRef = useRef(null);
17
+ const ignoreMutationRef = useRef(null);
18
+ const setSelectNode = useCallback((selectHandler, deselectHandler)=>{
19
+ selectNodeRef.current = selectHandler;
20
+ deselectNodeRef.current = deselectHandler;
21
+ setHasCustomSelectNode(true);
22
+ return ()=>{
23
+ selectNodeRef.current = null;
24
+ deselectNodeRef.current = null;
25
+ setHasCustomSelectNode(false);
26
+ };
27
+ }, []);
28
+ const setStopEvent = useCallback((handler)=>{
29
+ stopEventRef.current = handler;
30
+ return ()=>{
31
+ stopEventRef.current = null;
32
+ };
33
+ }, []);
34
+ const setIgnoreMutation = useCallback((handler)=>{
35
+ ignoreMutationRef.current = handler;
36
+ return ()=>{
37
+ ignoreMutationRef.current = null;
38
+ return ()=>{
39
+ ignoreMutationRef.current = null;
40
+ };
41
+ };
42
+ }, []);
43
+ const nodeProps = useMemo(()=>({
44
+ node: node,
45
+ getPos: getPos,
46
+ decorations: outerDeco,
47
+ innerDecorations: innerDeco
48
+ }), [
49
+ getPos,
50
+ innerDeco,
51
+ node,
52
+ outerDeco
53
+ ]);
54
+ const { childContextValue, contentDOM, nodeDOM } = useNodeViewDescriptor(ref, ()=>{
55
+ setSelected(false);
56
+ return {
57
+ dom: innerRef.current ?? ref.current,
58
+ update () {
59
+ return true;
60
+ },
61
+ multiType: true,
62
+ selectNode () {
63
+ const selectNode = selectNodeRef.current;
64
+ if (selectNode) {
65
+ selectNode.call(this);
66
+ }
67
+ setSelected(true);
68
+ },
69
+ deselectNode () {
70
+ const deselectNode = deselectNodeRef.current;
71
+ if (deselectNode) {
72
+ deselectNode.call(this);
73
+ }
74
+ setSelected(false);
75
+ },
76
+ stopEvent (event) {
77
+ const stopEvent = stopEventRef.current;
78
+ if (stopEvent) {
79
+ return stopEvent.call(this, event);
80
+ }
81
+ return false;
82
+ },
83
+ ignoreMutation (mutation) {
84
+ const ignoreMutation = ignoreMutationRef.current;
85
+ if (ignoreMutation) {
86
+ return ignoreMutation.call(this, mutation);
87
+ }
88
+ return false;
89
+ }
90
+ };
91
+ }, nodeProps);
92
+ const props = {
93
+ nodeProps,
94
+ ...!contentDOM && !nodeProps.node.isText && nodeDOM?.nodeName !== "BR" ? {
95
+ contentEditable: false,
96
+ suppressContentEditableWarning: true
97
+ } : null,
98
+ ...!hasCustomSelectNode && selected ? {
99
+ className: "ProseMirror-selectednode"
100
+ } : null,
101
+ ...!hasCustomSelectNode && selected || node.type.spec.draggable ? {
102
+ draggable: true
103
+ } : null,
104
+ ref: innerRef
105
+ };
106
+ const children = !node.isLeaf ? /*#__PURE__*/ React.createElement(ChildNodeViews, {
107
+ getPos: getPos,
108
+ node: node,
109
+ innerDecorations: innerDeco
110
+ }) : null;
111
+ const element = /*#__PURE__*/ cloneElement(outerDeco.reduce(wrapInDeco, /*#__PURE__*/ React.createElement(Component, props, children)), {
112
+ ref
113
+ });
114
+ return /*#__PURE__*/ React.createElement(SelectNodeContext.Provider, {
115
+ value: setSelectNode
116
+ }, /*#__PURE__*/ React.createElement(StopEventContext.Provider, {
117
+ value: setStopEvent
118
+ }, /*#__PURE__*/ React.createElement(IgnoreMutationContext.Provider, {
119
+ value: setIgnoreMutation
120
+ }, /*#__PURE__*/ React.createElement(ChildDescriptorsContext.Provider, {
121
+ value: childContextValue
122
+ }, element))));
123
+ });
@@ -0,0 +1,59 @@
1
+ import { DOMSerializer } from "prosemirror-model";
2
+ import React, { memo, useMemo, useRef } from "react";
3
+ import { createPortal } from "react-dom";
4
+ import { ChildDescriptionsContext } from "../../contexts/ChildDescriptionsContext.js";
5
+ import { useMarkViewDescription } from "../../hooks/useMarkViewDescription.js";
6
+ export const CustomMarkView = /*#__PURE__*/ memo(function CustomMarkView(param) {
7
+ let { constructor, mark, inline, getPos, children } = param;
8
+ const ref = useRef(null);
9
+ const innerRef = useRef(null);
10
+ const markProps = useMemo(()=>({
11
+ mark,
12
+ inline,
13
+ getPos,
14
+ contentDOMRef: {
15
+ current: null
16
+ }
17
+ }), [
18
+ mark,
19
+ inline,
20
+ getPos
21
+ ]);
22
+ const createMarkView = function() {
23
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
24
+ args[_key] = arguments[_key];
25
+ }
26
+ const markView = constructor(...args);
27
+ if (!markView || !markView.dom) {
28
+ const spec = mark.type.spec.toDOM?.(mark, inline);
29
+ if (!spec) {
30
+ throw new Error(`Mark spec for ${mark.type.name} is missing toDOM`);
31
+ }
32
+ return DOMSerializer.renderSpec(document, spec, null);
33
+ }
34
+ return markView;
35
+ };
36
+ const { childContextValue, contentDOM } = useMarkViewDescription(ref, function() {
37
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
38
+ args[_key] = arguments[_key];
39
+ }
40
+ const markView = createMarkView(...args);
41
+ const dom = markView.dom;
42
+ const wrapperDOM = innerRef.current ?? ref.current;
43
+ return {
44
+ ...markView,
45
+ destroy () {
46
+ markView.destroy?.();
47
+ wrapperDOM.removeChild(dom);
48
+ },
49
+ ignoreMutation: markView.ignoreMutation
50
+ };
51
+ }, (markView)=>markView?.contentDOM ?? null, markProps);
52
+ const Component = inline ? "span" : "div";
53
+ const props = {
54
+ ref: innerRef
55
+ };
56
+ return /*#__PURE__*/ React.createElement(Component, props, contentDOM ? /*#__PURE__*/ createPortal(/*#__PURE__*/ React.createElement(ChildDescriptionsContext.Provider, {
57
+ value: childContextValue
58
+ }, children), contentDOM) : null);
59
+ });
@@ -0,0 +1,69 @@
1
+ import React, { forwardRef, memo, useContext, useImperativeHandle, useMemo, useRef } from "react";
2
+ import { ChildDescriptorsContext } from "../../contexts/ChildDescriptorsContext.js";
3
+ import { useClientLayoutEffect } from "../../hooks/useClientLayoutEffect.js";
4
+ import { MarkViewDesc, sortViewDescs } from "../../viewdesc.js";
5
+ import { OutputSpec } from "../OutputSpec.js";
6
+ export const MarkView = /*#__PURE__*/ memo(/*#__PURE__*/ forwardRef(function MarkView(param, ref) {
7
+ let { mark, getPos, children, inline = false } = param;
8
+ const { siblingsRef, parentRef } = useContext(ChildDescriptorsContext);
9
+ const viewDescRef = useRef(undefined);
10
+ const childDescriptors = useRef([]);
11
+ const domRef = useRef(null);
12
+ useImperativeHandle(ref, ()=>{
13
+ return domRef.current;
14
+ }, []);
15
+ const outputSpec = useMemo(()=>mark.type.spec.toDOM?.(mark, inline), [
16
+ inline,
17
+ mark
18
+ ]);
19
+ if (!outputSpec) throw new Error(`Mark spec for ${mark.type.name} is missing toDOM`);
20
+ useClientLayoutEffect(()=>{
21
+ const siblings = siblingsRef.current;
22
+ return ()=>{
23
+ if (!viewDescRef.current) return;
24
+ if (siblings.includes(viewDescRef.current)) {
25
+ const index = siblings.indexOf(viewDescRef.current);
26
+ siblings.splice(index, 1);
27
+ }
28
+ };
29
+ }, [
30
+ siblingsRef
31
+ ]);
32
+ useClientLayoutEffect(()=>{
33
+ if (!domRef.current) return;
34
+ const firstChildDesc = childDescriptors.current[0];
35
+ if (!viewDescRef.current) {
36
+ viewDescRef.current = new MarkViewDesc(parentRef.current, childDescriptors.current, getPos, mark, domRef.current, firstChildDesc?.dom.parentElement ?? domRef.current, {
37
+ dom: domRef.current,
38
+ contentDOM: firstChildDesc?.dom.parentElement ?? domRef.current
39
+ });
40
+ } else {
41
+ viewDescRef.current.parent = parentRef.current;
42
+ viewDescRef.current.spec.dom = viewDescRef.current.dom = domRef.current;
43
+ viewDescRef.current.children = childDescriptors.current;
44
+ viewDescRef.current.spec.contentDOM = viewDescRef.current.contentDOM = firstChildDesc?.dom.parentElement ?? domRef.current;
45
+ viewDescRef.current.mark = mark;
46
+ }
47
+ if (!siblingsRef.current.includes(viewDescRef.current)) {
48
+ siblingsRef.current.push(viewDescRef.current);
49
+ }
50
+ siblingsRef.current.sort(sortViewDescs);
51
+ for (const childDesc of childDescriptors.current){
52
+ childDesc.parent = viewDescRef.current;
53
+ }
54
+ });
55
+ const childContextValue = useMemo(()=>({
56
+ parentRef: viewDescRef,
57
+ siblingsRef: childDescriptors
58
+ }), [
59
+ childDescriptors,
60
+ viewDescRef
61
+ ]);
62
+ return /*#__PURE__*/ React.createElement(OutputSpec, {
63
+ ref: domRef,
64
+ outputSpec: outputSpec,
65
+ isMark: true
66
+ }, /*#__PURE__*/ React.createElement(ChildDescriptorsContext.Provider, {
67
+ value: childContextValue
68
+ }, children));
69
+ }));
@@ -0,0 +1,84 @@
1
+ import { DOMSerializer } from "prosemirror-model";
2
+ import React, { cloneElement, memo, useMemo, useRef } from "react";
3
+ import { createPortal } from "react-dom";
4
+ import { ChildDescriptionsContext } from "../../contexts/ChildDescriptionsContext.js";
5
+ import { useNodeViewDescription } from "../../hooks/useNodeViewDescription.js";
6
+ import { ChildNodeViews, wrapInDeco } from "../ChildNodeViews.js";
7
+ export const CustomNodeView = /*#__PURE__*/ memo(function CustomNodeView(param) {
8
+ let { constructor, node, getPos, innerDeco, outerDeco } = param;
9
+ const ref = useRef(null);
10
+ const innerRef = useRef(null);
11
+ const nodeProps = useMemo(()=>({
12
+ node,
13
+ getPos,
14
+ decorations: outerDeco,
15
+ innerDecorations: innerDeco,
16
+ contentDOMRef: {
17
+ current: null
18
+ }
19
+ }), [
20
+ node,
21
+ getPos,
22
+ outerDeco,
23
+ innerDeco
24
+ ]);
25
+ const createNodeView = function() {
26
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
27
+ args[_key] = arguments[_key];
28
+ }
29
+ const nodeView = constructor(...args);
30
+ if (!nodeView || !nodeView.dom) {
31
+ const spec = node.type.spec.toDOM?.(node);
32
+ if (!spec) {
33
+ throw new Error(`Node spec for ${node.type.name} is missing toDOM`);
34
+ }
35
+ return DOMSerializer.renderSpec(document, spec, null);
36
+ }
37
+ return nodeView;
38
+ };
39
+ const { childContextValue, contentDOM } = useNodeViewDescription(ref, function() {
40
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
41
+ args[_key] = arguments[_key];
42
+ }
43
+ const nodeView = createNodeView(...args);
44
+ const contentDOM = nodeView.contentDOM;
45
+ const nodeDOM = nodeView.dom;
46
+ const wrapperDOM = innerRef.current ?? ref.current;
47
+ wrapperDOM.appendChild(nodeDOM);
48
+ if (!contentDOM && nodeDOM instanceof HTMLElement && nodeDOM.tagName !== "BR") {
49
+ if (!nodeDOM.hasAttribute("contenteditable")) {
50
+ nodeDOM.contentEditable = "false";
51
+ }
52
+ if (node.type.spec.draggable) {
53
+ nodeDOM.draggable = true;
54
+ }
55
+ }
56
+ return {
57
+ ...nodeView,
58
+ destroy () {
59
+ if (nodeView.destroy) {
60
+ nodeView.destroy();
61
+ }
62
+ wrapperDOM.removeChild(nodeDOM);
63
+ },
64
+ selectNode: nodeView.selectNode?.bind(nodeView),
65
+ deselectNode: nodeView.deselectNode?.bind(nodeView),
66
+ stopEvent: nodeView.stopEvent?.bind(nodeView),
67
+ ignoreMutation: nodeView.ignoreMutation?.bind(nodeView)
68
+ };
69
+ }, (source)=>source?.contentDOM ?? null, nodeProps);
70
+ const Component = node.isInline ? "span" : "div";
71
+ const props = {
72
+ ref: innerRef
73
+ };
74
+ const children = !node.isLeaf && contentDOM ? /*#__PURE__*/ createPortal(/*#__PURE__*/ React.createElement(ChildDescriptionsContext.Provider, {
75
+ value: childContextValue
76
+ }, /*#__PURE__*/ React.createElement(ChildNodeViews, {
77
+ getPos: getPos,
78
+ node: node,
79
+ innerDecorations: innerDeco
80
+ })), contentDOM) : null;
81
+ return /*#__PURE__*/ cloneElement(outerDeco.reduce(wrapInDeco, /*#__PURE__*/ React.createElement(Component, props, children)), {
82
+ ref
83
+ });
84
+ });
@@ -140,7 +140,7 @@ export const ReactNodeView = /*#__PURE__*/ memo(function ReactNodeView(param) {
140
140
  ...!hasCustomSelectNode && selected ? {
141
141
  className: "ProseMirror-selectednode"
142
142
  } : null,
143
- ...!hasCustomSelectNode && selected || node.type.spec.draggable ? {
143
+ ...!hasCustomSelectNode && selected || !contentDOMRef.current && !nodeProps.node.isText && domRef.current?.nodeName !== "BR" && node.type.spec.draggable ? {
144
144
  draggable: true
145
145
  } : null,
146
146
  ref: setNodeDOM
@@ -0,0 +1,9 @@
1
+ import { createContext } from "react";
2
+ export const ChildDescriptorsContext = createContext({
3
+ parentRef: {
4
+ current: undefined
5
+ },
6
+ siblingsRef: {
7
+ current: []
8
+ }
9
+ });