{"version":3,"file":"tree-view-keyboard-coordinates.mjs","names":[],"sources":["../../../src/tree-view/tree-view-keyboard-coordinates.ts"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport {\n  closestCorners,\n  type DroppableContainer,\n  getFirstCollision,\n  KeyboardCode,\n  type KeyboardCoordinateGetter,\n} from '@dnd-kit/core';\n\nimport { getProjection, type SensorContext } from './tree-view-utils';\n\nconst directions: string[] = [\n  KeyboardCode.Down,\n  KeyboardCode.Right,\n  KeyboardCode.Up,\n  KeyboardCode.Left,\n];\n\nconst horizontal: string[] = [KeyboardCode.Left, KeyboardCode.Right];\n\nexport const sortableTreeKeyboardCoordinates: <T>(\n  context: SensorContext<T>,\n  indicator: boolean,\n  indentationWidth: number,\n) => KeyboardCoordinateGetter =\n  (context, indicator, indentationWidth) =>\n  (\n    event,\n    {\n      currentCoordinates,\n      context: {\n        active,\n        over,\n        collisionRect,\n        droppableRects,\n        droppableContainers,\n      },\n    },\n  ) => {\n    if (directions.includes(event.code)) {\n      if (!active || !collisionRect) {\n        return;\n      }\n      event.preventDefault();\n\n      const {\n        current: { items, offset },\n      } = context;\n      if (horizontal.includes(event.code) && over?.id) {\n        const { depth, maxDepth, minDepth } = getProjection(\n          items,\n          active.id,\n          over.id,\n          offset,\n          indentationWidth,\n        );\n\n        switch (event.code) {\n          case KeyboardCode.Left:\n            if (depth > minDepth) {\n              return {\n                ...currentCoordinates,\n                x: currentCoordinates.x - indentationWidth, // oxlint-disable-line id-length\n              };\n            }\n            break;\n          case KeyboardCode.Right:\n            if (depth < maxDepth) {\n              return {\n                ...currentCoordinates,\n                x: currentCoordinates.x + indentationWidth, // oxlint-disable-line id-length\n              };\n            }\n            break;\n        }\n\n        return undefined;\n      }\n\n      const containers: DroppableContainer[] = [];\n\n      droppableContainers.forEach((container) => {\n        if (\n          container?.disabled ||\n          container.id === over?.id ||\n          container?.id === 'ndl-active-tree-item'\n        ) {\n          return;\n        }\n        const rect = droppableRects.get(container.id);\n\n        if (!rect) {\n          return;\n        }\n\n        switch (event.code) {\n          case KeyboardCode.Down:\n            if (collisionRect.top < rect.top) {\n              containers.push(container);\n            }\n            break;\n          case KeyboardCode.Up:\n            if (collisionRect.top > rect.top) {\n              containers.push(container);\n            }\n            break;\n        }\n      });\n\n      const collisions = closestCorners({\n        active,\n        collisionRect,\n        droppableContainers: containers,\n        droppableRects,\n        pointerCoordinates: null,\n      });\n      let closestId = getFirstCollision(collisions, 'id');\n      if (closestId === over?.id && collisions.length > 1) {\n        closestId = collisions[1].id;\n      }\n\n      if (closestId && over?.id) {\n        const activeRect = droppableRects.get(active.id);\n        const newRect = droppableRects.get(closestId);\n        const newDroppable = droppableContainers.get(closestId);\n\n        if (activeRect && newRect && newDroppable) {\n          const newIndex = items.findIndex(({ id }) => id === closestId);\n          const newItem = items[newIndex];\n          const activeIndex = items.findIndex(({ id }) => id === active.id);\n          const activeItem = items[activeIndex];\n\n          if (newItem && activeItem) {\n            const { depth } = getProjection(\n              items,\n              active.id,\n              closestId,\n              (newItem.depth - activeItem.depth) * indentationWidth,\n              indentationWidth,\n            );\n            const isBelow = newIndex > activeIndex;\n            const modifier = isBelow ? 1 : -1;\n            const offset = indicator\n              ? (collisionRect.height - activeRect.height) / 2\n              : 0;\n\n            const newCoordinates = {\n              // oxlint-disable-next-line id-length\n              x: newRect.left + depth * indentationWidth,\n              // oxlint-disable-next-line id-length\n              y: newRect.top + modifier * offset,\n            };\n\n            return newCoordinates;\n          }\n        }\n      }\n    }\n\n    return undefined;\n  };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA8BA,MAAM,aAAuB;CAC3B,aAAa;CACb,aAAa;CACb,aAAa;CACb,aAAa;AACf;AAEA,MAAM,aAAuB,CAAC,aAAa,MAAM,aAAa,KAAK;AAEnE,MAAa,mCAKV,SAAS,WAAW,sBAEnB,OACA,EACE,oBACA,SAAS,EACP,QACA,MACA,eACA,gBACA,4BAGD;CACH,IAAI,WAAW,SAAS,MAAM,IAAI,GAAG;EACnC,IAAI,CAAC,UAAU,CAAC,eACd;EAEF,MAAM,eAAe;EAErB,MAAM,EACJ,SAAS,EAAE,OAAO,aAChB;EACJ,IAAI,WAAW,SAAS,MAAM,IAAI,MAAA,SAAA,QAAA,SAAA,KAAA,IAAA,KAAA,IAAK,KAAM,KAAI;GAC/C,MAAM,EAAE,OAAO,UAAU,aAAa,cACpC,OACA,OAAO,IACP,KAAK,IACL,QACA,gBACF;GAEA,QAAQ,MAAM,MAAd;IACE,KAAK,aAAa;KAChB,IAAI,QAAQ,UACV,OAAA,eAAA,eAAA,CAAA,GACK,kBAAA,GAAA,CAAA,GAAA,EACH,GAAG,mBAAmB,IAAI,iBAAA,CAC5B;KAEF;IACF,KAAK,aAAa,OAChB,IAAI,QAAQ,UACV,OAAA,eAAA,eAAA,CAAA,GACK,kBAAA,GAAA,CAAA,GAAA,EACH,GAAG,mBAAmB,IAAI,iBAAA,CAC5B;GAGN;GAEA;EACF;EAEA,MAAM,aAAmC,CAAC;EAE1C,oBAAoB,SAAS,cAAc;GACzC,KAAA,cAAA,QAAA,cAAA,KAAA,IAAA,KAAA,IACE,UAAW,aACX,UAAU,QAAA,SAAA,QAAA,SAAA,KAAA,IAAA,KAAA,IAAO,KAAM,QAAA,cAAA,QAAA,cAAA,KAAA,IAAA,KAAA,IACvB,UAAW,QAAO,wBAElB;GAEF,MAAM,OAAO,eAAe,IAAI,UAAU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