All files / src/UI/Canvas NodeRefProvider.tsx

96.77% Statements 60/62
84.29% Branches 59/70
88.89% Functions 8/9
96.43% Lines 54/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185                              7x                 7x                                                                             7x   7x 2x 61x 61x 61x 61x 139x 139x 117x           61x   2x       61x 61x 139x 139x 117x         2x                         7x 110x 110x 110x 122x 61x 61x   49x       7x 51x 51x 51x   177x     177x 177x 177x 177x 174x 174x 63x   111x 111x 37x     37x     37x 37x 37x             37x 37x               37x   37x                         74x   3x       51x    
/*
    Copyright (C) 2017 Red Hat, Inc.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
 
            http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
import React, {
  FunctionComponent,
  MutableRefObject,
  createContext,
  useCallback,
  useContext,
  useEffect,
  useRef,
} from 'react';
import { useBoundingCanvasRect } from './useBoundingCanvasRect';
 
export interface NodeRect extends DOMRect {
  clipped?: boolean;
}
 
export interface NodeRefProps {
  ref: MutableRefObject<HTMLElement | SVGElement | null>;
  parentId?: string;
  boundaryId?: string;
  overrideWidth?: string;
  overrideHeight?: string;
}
 
export interface NodeRefPropsWithOptionalId extends NodeRefProps {
  id: string | undefined | Array<string | undefined>;
}
 
export interface NodeRefPropsWithId extends NodeRefProps {
  id: string;
}
 
export interface NodeRefPropsWithKey extends NodeRefProps {
  key: string;
}
 
export interface NodeRefMap {
  [id: string]: NodeRefPropsWithKey;
}
 
export interface INodeRefContext {
  nodes: MutableRefObject<NodeRefMap>;
  setRef: (props: NodeRefPropsWithId) => string;
  unsetRef: (
    id: string | undefined | Array<string | undefined>,
    key: string,
  ) => void;
}
 
export const NodeRefContext = createContext<INodeRefContext | null>(null);
 
export const NodeRefProvider: FunctionComponent = ({ children }) => {
  const nodes = useRef<NodeRefMap>({});
  const setRef = ({ id, ...props }: NodeRefPropsWithId) => {
    const key = `${id}-${Date.now()}`;
    const ids = Array.isArray(id) ? id : [id];
    for (let index = 0; index < ids.length; index++) {
      const curId = ids[index];
      if (curId) {
        nodes.current[ids[index]] = {
          ...props,
          key,
        };
      }
    }
    return key;
  };
  const unsetRef = (
    id: string | undefined | Array<string | undefined>,
    key: string,
  ) => {
    const ids = Array.isArray(id) ? id : [id];
    for (let index = 0; index < ids.length; index++) {
      const curId = ids[index];
      if (curId && nodes.current[curId]?.key === key) {
        delete nodes.current[curId];
      }
    }
  };
 
  return (
    <NodeRefContext.Provider
      value={{
        nodes,
        setRef,
        unsetRef,
      }}
    >
      {children}
    </NodeRefContext.Provider>
  );
};
 
export function useNodeRef(props: NodeRefPropsWithOptionalId) {
  const context = useContext(NodeRefContext);
  useEffect(() => {
    if (context && props.id) {
      const { setRef, unsetRef } = context;
      const key = setRef(props as NodeRefPropsWithId);
      return () => unsetRef(props.id!, key);
    }
    return;
  }, [context, props]);
}
 
export function useNodeRect() {
  const context = useContext(NodeRefContext);
  const { getBoundingCanvasRect } = useBoundingCanvasRect();
  const getRect = useCallback(
    (id: string): NodeRect | null => {
      Iif (!context) {
        return null;
      }
      const { nodes } = context;
      const node = nodes.current[id];
      const element = node ? node.ref.current : undefined;
      if (element) {
        let dimensions = getBoundingCanvasRect(element);
        if (dimensions.height === 0 && node.parentId) {
          return getRect(node.parentId);
        }
        let boundaryRect;
        if (node.boundaryId && (boundaryRect = getRect(node.boundaryId))) {
          const overrideWidthRect = node.overrideWidth
            ? getRect(node.overrideWidth)
            : undefined;
          const overrideHeightRect = node.overrideHeight
            ? getRect(node.overrideHeight)
            : undefined;
          const width = overrideWidthRect?.width || dimensions.width;
          const height = overrideHeightRect?.height || dimensions.height;
          const x = Math.min(
            Math.max(
              overrideWidthRect?.left || dimensions.left,
              boundaryRect.left,
            ),
            boundaryRect.width + boundaryRect.left - dimensions.width,
          );
          const isXClipped = x !== (overrideWidthRect?.x || dimensions.x);
          const y = Math.min(
            Math.max(
              overrideHeightRect?.top || dimensions.top + dimensions.height / 2,
              boundaryRect.top,
            ),
            boundaryRect.height + boundaryRect.top,
          );
          const isYClipped =
            y === boundaryRect.top ||
            y === boundaryRect.height + boundaryRect.top;
          return {
            width,
            height,
            x,
            y,
            left: x,
            top: y,
            right: x + width,
            bottom: y + height,
            toJSON: () => '',
            clipped: isXClipped || isYClipped,
          };
        }
        return dimensions;
      }
      return null;
    },
    [context, getBoundingCanvasRect],
  );
  return getRect;
}