{"version":3,"file":"create-rects-from-dom-range.cjs","sources":["../src/create-rects-from-dom-range.ts"],"sourcesContent":["/**\n * MIT License\n * Copyright (c) Meta Platforms, Inc. and affiliates.\n\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * \n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n * \n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport type { LexicalEditor } from \"lexical\";\n\n/**\n * Creates DOMRects, generally used to help the editor find a specific location on the screen.\n * @param editor - The lexical editor\n * @param range - A fragment of a document that can contain nodes and parts of text nodes.\n * @returns The selectionRects as an array.\n */\nexport function createRectsFromDOMRange(\n  editor: LexicalEditor,\n  range: Range\n): Array<ClientRect> {\n  const rootElement = editor.getRootElement();\n\n  if (rootElement === null) {\n    return [];\n  }\n  const rootRect = rootElement.getBoundingClientRect();\n  const computedStyle = getComputedStyle(rootElement);\n  const rootPadding =\n    parseFloat(computedStyle.paddingLeft) +\n    parseFloat(computedStyle.paddingRight);\n  const selectionRects = Array.from(range.getClientRects());\n  let selectionRectsLength = selectionRects.length;\n  //sort rects from top left to bottom right.\n  selectionRects.sort((a, b) => {\n    const top = a.top - b.top;\n    // Some rects match position closely, but not perfectly,\n    // so we give a 3px tolerance.\n    if (Math.abs(top) <= 3) {\n      return a.left - b.left;\n    }\n    return top;\n  });\n  let prevRect;\n  for (let i = 0; i < selectionRectsLength; i++) {\n    const selectionRect = selectionRects[i];\n    // Exclude rects that overlap preceding Rects in the sorted list.\n    const isOverlappingRect =\n      prevRect &&\n      prevRect.top <= selectionRect.top &&\n      prevRect.top + prevRect.height > selectionRect.top &&\n      prevRect.left + prevRect.width > selectionRect.left;\n    // Exclude selections that span the entire element\n    const selectionSpansElement =\n      selectionRect.width + rootPadding === rootRect.width;\n    if (isOverlappingRect || selectionSpansElement) {\n      selectionRects.splice(i--, 1);\n      selectionRectsLength--;\n      continue;\n    }\n    prevRect = selectionRect;\n  }\n  return selectionRects;\n}\n"],"names":[],"mappings":";;AA+BgB,SAAA,uBAAA,CACd,QACA,KACmB,EAAA;AACnB,EAAM,MAAA,WAAA,GAAc,OAAO,cAAe,EAAA,CAAA;AAE1C,EAAA,IAAI,gBAAgB,IAAM,EAAA;AACxB,IAAA,OAAO,EAAC,CAAA;AAAA,GACV;AACA,EAAM,MAAA,QAAA,GAAW,YAAY,qBAAsB,EAAA,CAAA;AACnD,EAAM,MAAA,aAAA,GAAgB,iBAAiB,WAAW,CAAA,CAAA;AAClD,EAAA,MAAM,cACJ,UAAW,CAAA,aAAA,CAAc,WAAW,CACpC,GAAA,UAAA,CAAW,cAAc,YAAY,CAAA,CAAA;AACvC,EAAA,MAAM,cAAiB,GAAA,KAAA,CAAM,IAAK,CAAA,KAAA,CAAM,gBAAgB,CAAA,CAAA;AACxD,EAAA,IAAI,uBAAuB,cAAe,CAAA,MAAA,CAAA;AAE1C,EAAe,cAAA,CAAA,IAAA,CAAK,CAAC,CAAA,EAAG,CAAM,KAAA;AAC5B,IAAM,MAAA,GAAA,GAAM,CAAE,CAAA,GAAA,GAAM,CAAE,CAAA,GAAA,CAAA;AAGtB,IAAA,IAAI,IAAK,CAAA,GAAA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