{"version":3,"file":"AutoCell.mjs","sources":["../../../../../../src/components/Table/TableNG/Cells/AutoCell.tsx"],"sourcesContent":["import { css } from '@emotion/css';\nimport { clsx } from 'clsx';\nimport memoize from 'micro-memoize';\n\nimport { formattedValueToString } from '@grafana/data';\n\nimport { TABLE } from '../constants';\nimport { getActiveCellSelector, isTableCellStylesKeyEqual } from '../styles';\nimport { type AutoCellProps, type TableCellStyleOptions, type TableCellStyles } from '../types';\n\nimport { TextCellContents, textCellClassName } from './TextCellContents';\n\nexport function AutoCell({ value, field, rowIdx, tableRefreshEnabled }: AutoCellProps) {\n  const displayValue = field.display!(value);\n  const formattedValue = formattedValueToString(displayValue);\n  return (\n    <TextCellContents field={field} rowIdx={rowIdx} tableRefreshEnabled={tableRefreshEnabled}>\n      {formattedValue}\n    </TextCellContents>\n  );\n}\n\n/**\n * `pre-line` collapses runs of whitespace, which suits prose but flattens the indentation a JSON\n * cell depends on; `pre-wrap` keeps it and still soft-wraps lines too long for the column.\n */\ntype CellWhiteSpace = 'pre-line' | 'pre-wrap';\n\n/**\n * Shared style body for every cell rendered by AutoCell. The whitespace mode is a parameter rather\n * than something a caller layers on afterwards: two classes each declaring `white-space` have equal\n * specificity, so the winner would come down to the order emotion happened to insert them.\n */\nfunction buildAutoCellStyles(\n  { textWrap, shouldOverflow, hoverOverflow, maxHeight, tableRefreshEnabled }: TableCellStyleOptions,\n  whiteSpace: CellWhiteSpace,\n  // Bounds the hover expansion. Worth it where a single value can be arbitrarily large, since an\n  // unbounded cell grows past the panel and puts its own content out of reach. Applies in both the\n  // no-maxHeight and maxHeight branches below — a row-height clamp still clears itself on hover\n  // (WebkitLineClamp: 'none', height: 'fit-content'), so without a cap here too a JSON cell in a\n  // fixed-height row would grow unbounded on hover, the exact overflow this option exists to stop.\n  boundExpansion = false\n) {\n  // never shrink below the column's own width — only grow past it, up to the cap. Without this, a\n  // cell in a column wider than the cap would visually shrink to the cap on hover: the cursor ends up\n  // past the now-narrower box, hover ends, the box snaps back under the cursor, hover re-fires, and\n  // it flickers between the two sizes.\n  //\n  // the cell root inherits `align-items: center` from the default cell styles. Centering an\n  // overflow-clipped box is \"unsafe\" by default: content taller than the cap gets centered on the\n  // box's midpoint, pushing the top of the content into the negative-scroll region above scrollTop 0\n  // — unreachable, since scrolling up further isn't possible past 0. That reads as the cell opening\n  // already scrolled a few lines down with no way back to the top.\n  const expansionBounds = {\n    minWidth: '100%',\n    maxWidth: TABLE.JSON_OVERFLOW_MAX_WIDTH,\n    maxHeight: TABLE.JSON_OVERFLOW_MAX_HEIGHT,\n    overflowY: 'auto' as const,\n    alignItems: 'flex-start' as const,\n  };\n\n  const lineClamp = {\n    display: '-webkit-box',\n    WebkitBoxOrient: 'vertical' as const,\n    WebkitLineClamp: Math.floor((maxHeight ?? 0) / TABLE.LINE_HEIGHT),\n  };\n  const clearLineClamp = {\n    WebkitLineClamp: 'none',\n    WebkitBoxOrient: 'unset' as const,\n  };\n\n  return css({\n    ...(textWrap && { whiteSpace }),\n    ...(shouldOverflow && {\n      [getActiveCellSelector(Boolean(maxHeight), hoverOverflow)]: {\n        whiteSpace,\n        ...(boundExpansion && maxHeight == null && expansionBounds),\n      },\n    }),\n    ...(maxHeight != null &&\n      textWrap && {\n        height: 'auto',\n        overflowY: 'hidden',\n        ...(tableRefreshEnabled ? { [`> .${textCellClassName}`]: lineClamp } : lineClamp),\n        [getActiveCellSelector(true, hoverOverflow)]: {\n          display: 'flex',\n          ...(tableRefreshEnabled\n            ? { [`> .${textCellClassName}`]: { display: 'block', ...clearLineClamp } }\n            : clearLineClamp),\n          overflowY: 'auto',\n          height: 'fit-content',\n          ...(boundExpansion && expansionBounds),\n        },\n      }),\n  });\n}\n\nexport const getStyles: TableCellStyles = memoize((_theme, options) => buildAutoCellStyles(options, 'pre-line'), {\n  isMatchingKey: isTableCellStylesKeyEqual,\n});\n\n// Nothing here overlaps the shared body, so composing the two classes is order-independent.\nconst jsonCellFont = css({ fontFamily: 'monospace' });\n\nexport const getJsonCellStyles: TableCellStyles = memoize(\n  (_theme, options) => clsx(jsonCellFont, buildAutoCellStyles(options, 'pre-wrap', true)),\n  { isMatchingKey: isTableCellStylesKeyEqual }\n);\n"],"names":[],"mappings":";;;;;;;;;;AAYO,SAAS,SAAS,EAAE,KAAA,EAAO,KAAA,EAAO,MAAA,EAAQ,qBAAoB,EAAkB;AACrF,EAAA,MAAM,YAAA,GAAe,KAAA,CAAM,OAAA,CAAS,KAAK,CAAA;AACzC,EAAA,MAAM,cAAA,GAAiB,uBAAuB,YAAY,CAAA;AAC1D,EAAA,uBACE,GAAA,CAAC,gBAAA,EAAA,EAAiB,KAAA,EAAc,MAAA,EAAgB,qBAC7C,QAAA,EAAA,cAAA,EACH,CAAA;AAEJ;AAaA,SAAS,mBAAA,CACP,EAAE,QAAA,EAAU,cAAA,EAAgB,aAAA,EAAe,WAAW,mBAAA,EAAoB,EAC1E,UAAA,EAMA,cAAA,GAAiB,KAAA,EACjB;AAWA,EAAA,MAAM,eAAA,GAAkB;AAAA,IACtB,QAAA,EAAU,MAAA;AAAA,IACV,UAAU,KAAA,CAAM,uBAAA;AAAA,IAChB,WAAW,KAAA,CAAM,wBAAA;AAAA,IACjB,SAAA,EAAW,MAAA;AAAA,IACX,UAAA,EAAY;AAAA,GACd;AAEA,EAAA,MAAM,SAAA,GAAY;AAAA,IAChB,OAAA,EAAS,aAAA;AAAA,IACT,eAAA,EAAiB,UAAA;AAAA,IACjB,iBAAiB,IAAA,CAAK,KAAA,CAAA,CAAO,SAAA,IAAA,IAAA,GAAA,SAAA,GAAa,CAAA,IAAK,MAAM,WAAW;AAAA,GAClE;AACA,EAAA,MAAM,cAAA,GAAiB;AAAA,IACrB,eAAA,EAAiB,MAAA;AAAA,IACjB,eAAA,EAAiB;AAAA,GACnB;AAEA,EAAA,OAAO,GAAA,CAAI;AAAA,IACT,GAAI,QAAA,IAAY,EAAE,UAAA,EAAW;AAAA,IAC7B,GAAI,cAAA,IAAkB;AAAA,MACpB,CAAC,qBA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