Skip to content

Commit

Permalink
Track contentDOM in descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
smoores-dev committed Jul 28, 2023
1 parent 0bc48d4 commit 3177534
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
29 changes: 23 additions & 6 deletions src/components/NodeWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,42 @@ import {
NodeViewDescriptorsContext,
} from "../contexts/NodeViewPositionsContext.js";

export function findChildDesc(
pos: number,
posToDesc: Map<number, NodeViewDescriptor>
) {
const positions = Array.from(posToDesc.keys()).sort((a, b) => b - a);

let parentPos = null;
for (const nodePos of positions) {
if (nodePos < pos) break;

parentPos = nodePos;
}

return parentPos === null ? null : posToDesc.get(parentPos);
}

type NodeWrapperProps = {
children: ReactNode;
pos: number;
};
export function NodeWrapper({ children, pos }: NodeWrapperProps) {
const { posToDesc: posToDOM, domToDesc: domToPos } = useContext(
NodeViewDescriptorsContext
);
const { posToDesc, domToDesc } = useContext(NodeViewDescriptorsContext);
const ref = useRef<Element | null>(null);

useLayoutEffect(() => {
if (!ref.current) return;

const childDesc = findChildDesc(pos, posToDesc);

const desc: NodeViewDescriptor = {
pos,
dom: ref.current,
contentDOM: null,
contentDOM: childDesc?.dom.parentNode ?? null,
};
posToDOM.set(pos, desc);
domToPos.set(ref.current, desc);
posToDesc.set(pos, desc);
domToDesc.set(ref.current, desc);
});

const child = Children.only(children);
Expand Down
12 changes: 6 additions & 6 deletions src/components/TextNodeWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ export class TextNodeWrapper extends Component<TextNodeWrapperProps> {
const textNode = findDOMNode(this);
if (!textNode) return;

const { posToDesc: posToDOM, domToDesc: domToPos } = this
const { posToDesc, domToDesc } = this
.context as NodeViewDescriptorsContextValue;

const desc: NodeViewDescriptor = {
pos: this.props.pos,
dom: textNode,
contentDOM: null,
};
posToDOM.set(this.props.pos, desc);
domToPos.set(textNode, desc);
posToDesc.set(this.props.pos, desc);
domToDesc.set(textNode, desc);
}

componentDidUpdate(): void {
Expand All @@ -36,16 +36,16 @@ export class TextNodeWrapper extends Component<TextNodeWrapperProps> {
const textNode = findDOMNode(this);
if (!textNode) return;

const { posToDesc: posToDOM, domToDesc: domToPos } = this
const { posToDesc, domToDesc } = this
.context as NodeViewDescriptorsContextValue;

const desc: NodeViewDescriptor = {
pos: this.props.pos,
dom: textNode,
contentDOM: null,
};
posToDOM.set(this.props.pos, desc);
domToPos.set(textNode, desc);
posToDesc.set(this.props.pos, desc);
domToDesc.set(textNode, desc);
}

render() {
Expand Down

0 comments on commit 3177534

Please sign in to comment.