Forming a comment after hovering the mouse over a cell

Hi. I am facing a problem related to a drop in performance when initializing and editing a table (MobX action updateCharacteristicsSpreadsheetData as a handler for afterChange hook). I analyzed sections of the code and found out that the getStyledComment function is working for a long time, which generates the width/height of the comment on the content (validation text) for each cell that has not passed validation. Is there a way to form a comment when hovering the mouse over a invalid cell? So that after validation, only the validation text is saved for the cell, and the comment is styled only when the mouse hovers over an invalid cell? As if I would like to get it in the form of a hook (kind of afterOnCellMouseOver.

export const getStyledComment = (value: string | undefined, readOnly = true) => {
  if (!value) return { value, readOnly }

  // Empirically obtained constants for calculating the width and height of a comment
  const CHAR_WIDTH = 6.6
  const CHAR_W_PADDING = 16
  const CHAR_HEIGHT = 15
  const CHAR_H_PADDING = 10

  const trimmedValue = value.trim()
  const lines = trimmedValue.split('\n')
  const maxLineLength = Math.max(...lines.map((line) => line.length))

  // Dynamically calculating the width and height of the comment based on the content
  const style = {
    width: CHAR_WIDTH * maxLineLength + CHAR_W_PADDING,
    height: CHAR_HEIGHT * lines.length + CHAR_H_PADDING,
  }

  return { value: trimmedValue, readOnly, style }
}
updateCharacteristicsSpreadsheetData: IWerSpecificConsumptionStore['updateCharacteristicsSpreadsheetData'] = (
  changes,
) => {
  if (!changes) return

  // After each change in the table, validate it
  const errorsMap = validateCharacteristicsTableData(this.specificConsumptionSpreadsheetData.data, {
    0: this.initialSettings?.pressureAllowableChange,
    1: this.initialSettings?.consumptionAllowableChange,
  })

  // States that determine whether the table is editable
  const canEdit = this.rootStore.calcModelWerStore.listOfStationsStore.characteristicsStore.editMode
  const isSelectedDateEditable = this.rootStore.calcModelWerStore.isSelectedDateEditable
  const isSelectedPlantViewOnly = this.rootStore.calcModelWerStore.isSelectedPlantViewOnly

  // Update the cell array: recalculate className for each cell,
  // and also add a comment if an error is found
  const cells = this.specificConsumptionSpreadsheetData.cell
  cells.forEach((cell) => {
    const cellKey = `${cell.row}:${cell.col}`
    const message = errorsMap.get(cellKey)
    // Optimization. If the message hasn't changed, don't update className or comment
    if (cell.message !== message) {
      cell.className = getCellStyle({
        cell,
        canEdit,
        isSelectedDateEditable,
        isSelectedPlantViewOnly,
        message,
      })
      cell.comment = message ? getStyledComment(message) : undefined
      cell.message = message // Save for comparison
    }
  })
  // Recreate the array to change the reference so that MobX detects changes
  this.specificConsumptionSpreadsheetData.cell = cells.slice()

  this.isEditRows = true
}

Hi @mr.fursy

Can you please send a code demo where the issue can be replicated? Unfortunately, the forum breaks the quotes formatting in the code and it’s not optimal to create example of of such code if it’s more complex.

I also have a question, do you have an active license for Handsontable? If so, can you please share it at support@handsontable.com?

The problem is not so much with the library itself, but rather with the need to calculate the comment style for each cell (calling getStyledComment). This happens for a very long time in the loop. And so I had an idea - why should I calculate the comment style for a cell beforehand, if this can only be done for a specific cell that I have hovered over with the mouse. That is, I want the comment to be formed when the mouse hovers over the cell immediately before the comment appears.