{"version":3,"file":"selection.mjs","names":[],"sources":["../../../../src/next/tree-view/selection.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 */\n\nexport type SelectionMode = 'single' | 'multiple' | 'multiple-replace';\n\nexport type SelectionEdge = 'top' | 'middle' | 'bottom';\n\n/** Modifier keys resolved from a pointer or keyboard event. */\nexport type SelectionIntent = {\n  /** Shift: extend from the anchor to the target. */\n  isRange: boolean;\n  /**\n   * Cmd on Apple platforms, Ctrl elsewhere. Ignored in `multiple`, where an\n   * unmodified click already toggles.\n   */\n  isToggle: boolean;\n};\n\n/**\n * Where a range is measured from.\n *\n * `baseSelectedIds` is the selection as it stood immediately after the anchor\n * was placed. A `multiple-replace` range unions its visible members with the\n * range so it can be grown and shrunk without losing earlier picks.\n * `isSelected` is the state the anchor row was placed into, which a `multiple`\n * range paints across its members.\n */\nexport type SelectionAnchor = {\n  baseSelectedIds: ReadonlySet<string>;\n  id: string;\n  isSelected: boolean;\n} | null;\n\ntype ComputeNextSelectionInput = {\n  anchor: SelectionAnchor;\n  intent: SelectionIntent;\n  /** Ids of every selectable, non-disabled item, visible or not. */\n  selectableIds: ReadonlySet<string>;\n  /** Currently selected ids, including items inside collapsed groups. */\n  selectedIds: ReadonlySet<string>;\n  selectionMode: SelectionMode;\n  targetId: string;\n  /** Ids of the items the user can currently see, in visual order. */\n  visibleIds: readonly string[];\n  /** Maps an item id to the id of the item that owns its group. */\n  parentIdById: ReadonlyMap<string, string>;\n};\n\ntype ComputeNextSelectionResult = {\n  anchor: SelectionAnchor;\n  selectedIds: Set<string>;\n};\n\n/**\n * Ids that are selected but cannot be acted on, because they are disabled or\n * not selectable. A replacing selection leaves them exactly as they are rather\n * than clearing them behind the user's back.\n */\nfunction getLockedIds(\n  selectedIds: ReadonlySet<string>,\n  selectableIds: ReadonlySet<string>,\n): string[] {\n  return [...selectedIds].filter((id) => !selectableIds.has(id));\n}\n\nfunction getRangeIds(\n  visibleIds: readonly string[],\n  fromId: string,\n  toId: string,\n  selectableIds: ReadonlySet<string>,\n): string[] {\n  const fromIndex = visibleIds.indexOf(fromId);\n  const toIndex = visibleIds.indexOf(toId);\n  const [start, end] =\n    fromIndex <= toIndex ? [fromIndex, toIndex] : [toIndex, fromIndex];\n  return visibleIds.slice(start, end + 1).filter((id) => selectableIds.has(id));\n}\n\n/**\n * The anchor may have been scrolled out of reach by a collapse. Walking up to\n * its nearest visible ancestor keeps a range measurable from roughly where the\n * user put it, instead of silently collapsing the range to a single item.\n */\nfunction resolveVisibleAnchorId(\n  anchor: SelectionAnchor,\n  visibleIds: readonly string[],\n  parentIdById: ReadonlyMap<string, string>,\n): string | null {\n  if (anchor === null) {\n    return null;\n  }\n  const visible = new Set(visibleIds);\n  let candidate: string | undefined = anchor.id;\n  while (candidate !== undefined) {\n    if (visible.has(candidate)) {\n      return candidate;\n    }\n    candidate = parentIdById.get(candidate);\n  }\n  return null;\n}\n\nfunction toggleId(\n  selectedIds: ReadonlySet<string>,\n  targetId: string,\n): Set<string> {\n  const next = new Set(selectedIds);\n  if (next.has(targetId)) {\n    next.delete(targetId);\n  } else {\n    next.add(targetId);\n  }\n  return next;\n}\n\nfunction anchorAt(\n  id: string,\n  selectedIds: ReadonlySet<string>,\n): SelectionAnchor {\n  return {\n    baseSelectedIds: new Set(selectedIds),\n    id,\n    isSelected: selectedIds.has(id),\n  };\n}\n\n/**\n * Resolves a selection gesture into the complete next selection. Pure: the\n * caller supplies the current state and applies the result.\n */\nexport function computeNextSelection({\n  anchor,\n  intent,\n  parentIdById,\n  selectableIds,\n  selectedIds,\n  selectionMode,\n  targetId,\n  visibleIds,\n}: ComputeNextSelectionInput): ComputeNextSelectionResult {\n  if (!selectableIds.has(targetId) || !visibleIds.includes(targetId)) {\n    return { anchor, selectedIds: new Set(selectedIds) };\n  }\n\n  if (selectionMode === 'single') {\n    const next = new Set([targetId]);\n    return { anchor: anchorAt(targetId, next), selectedIds: next };\n  }\n\n  const anchorId = intent.isRange\n    ? resolveVisibleAnchorId(anchor, visibleIds, parentIdById)\n    : null;\n\n  if (selectionMode === 'multiple') {\n    if (!intent.isRange || anchorId === null) {\n      const next = toggleId(selectedIds, targetId);\n      return { anchor: anchorAt(targetId, next), selectedIds: next };\n    }\n    // A range repeats whatever the anchor row was last put into. An anchor\n    // resolved from a collapsed group may not be selectable at all, in which\n    // case there is no prior action to repeat and selecting is the useful default.\n    const isPaintingSelected = selectableIds.has(anchorId)\n      ? (anchor?.isSelected ?? true)\n      : true;\n    const next = new Set(selectedIds);\n    getRangeIds(visibleIds, anchorId, targetId, selectableIds).forEach((id) => {\n      if (isPaintingSelected) {\n        next.add(id);\n      } else {\n        next.delete(id);\n      }\n    });\n    return { anchor, selectedIds: next };\n  }\n\n  const lockedIds = getLockedIds(selectedIds, selectableIds);\n\n  if (intent.isRange) {\n    const rangeIds = getRangeIds(\n      visibleIds,\n      anchorId ?? targetId,\n      targetId,\n      selectableIds,\n    );\n    const baseIds =\n      anchor === null\n        ? []\n        : [...anchor.baseSelectedIds].filter(\n            (id) => selectableIds.has(id) && visibleIds.includes(id),\n          );\n    const next = new Set([...lockedIds, ...baseIds, ...rangeIds]);\n    return {\n      anchor: anchorId === null ? anchorAt(targetId, next) : anchor,\n      selectedIds: next,\n    };\n  }\n\n  if (intent.isToggle) {\n    const next = toggleId(selectedIds, targetId);\n    return { anchor: anchorAt(targetId, next), selectedIds: next };\n  }\n\n  const next = new Set([...lockedIds, targetId]);\n  return { anchor: anchorAt(targetId, next), selectedIds: next };\n}\n\n/**\n * Marks each selected item's position within its run of adjacent selected\n * items, so a run can be styled as one block.\n */\nexport function computeSelectionEdges(\n  visibleIds: readonly string[],\n  selectedIds: ReadonlySet<string>,\n): Map<string, SelectionEdge> {\n  const edges = new Map<string, SelectionEdge>();\n  visibleIds.forEach((id, index) => {\n    if (!selectedIds.has(id)) {\n      return;\n    }\n    const hasPreviousSelected =\n      index > 0 && selectedIds.has(visibleIds[index - 1]);\n    const hasNextSelected =\n      index < visibleIds.length - 1 && selectedIds.has(visibleIds[index + 1]);\n    if (hasPreviousSelected && hasNextSelected) {\n      edges.set(id, 'middle');\n    } else if (hasNextSelected) {\n      edges.set(id, 'top');\n    } else if (hasPreviousSelected) {\n      edges.set(id, 'bottom');\n    }\n  });\n  return edges;\n}\n\nexport function countChangedIds(\n  previous: ReadonlySet<string>,\n  next: ReadonlySet<string>,\n): number {\n  let changed = 0;\n  previous.forEach((id) => {\n    if (!next.has(id)) {\n      changed++;\n    }\n  });\n  next.forEach((id) => {\n    if (!previous.has(id)) {\n      changed++;\n    }\n  });\n  return changed;\n}\n\nexport function formatSelectionAnnouncement(selectedCount: number): string {\n  if (selectedCount === 0) {\n    return 'No items selected';\n  }\n  if (selectedCount === 1) {\n    return '1 item selected';\n  }\n  return `${selectedCount} items selected`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA4EA,SAAS,aACP,aACA,eACU;CACV,OAAO,CAAC,GAAG,WAAW,CAAC,CAAC,QAAQ,OAAO,CAAC,cAAc,IAAI,EAAE,CAAC;AAC/D;AAEA,SAAS,YACP,YACA,QACA,MACA,eACU;CACV,MAAM,YAAY,WAAW,QAAQ,MAAM;CAC3C,MAAM,UAAU,WAAW,QAAQ,IAAI;CACvC,MAAM,CAAC,OAAO,OACZ,aAAa,UAAU,CAAC,WAAW,OAAO,IAAI,CAAC,SAAS,SAAS;CACnE,OAAO,WAAW,MAAM,OA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