Repository: marktext/marktext
File: src/editor/contentState/selectionCtrl.js (JavaScript)

Code before:

  ContentState.prototype.tableCellHandler = function (event) {
    const { start, end } = this.cursor
    const startBlock = this.getBlock(start.key)
    const endBlock = this.getBlock(end.key)
    const { type: startType } = startBlock
    const { type: endType } = endBlock
    if (/th|td/.test(startType) && /th|td/.test(endType)) {
      if (start.key === end.key) {
        const { text, key } = startBlock
        this.cursor = {
          start: { key, offset: 0 },
          end: { key, offset: text.length }
        }
      } else {
        const startTable = this.getTableFromTableCell(startBlock)
        const endTable = this.getTableFromTableCell(endBlock)
        // Check whether both blocks are in the same table.
        if (!startTable || !endTable) {
          console.error('No table found or invalid type.')
          return
        } else if (startTable.key !== endTable.key) {
          // Select entire document
          return
        }
        const firstTableBlock = this.firstInDescendant(startTable)
        const lastTableBlock = this.lastInDescendant(startTable)
        if (!firstTableBlock || !/th|td/.test(firstTableBlock.type) ||
          !lastTableBlock || !/th|td/.test(lastTableBlock.type)) {
          console.error('No table cell found or invalid type.')
          return
        }
        const { key: startKey } = firstTableBlock
        const { key: endKey, text } = lastTableBlock
        this.cursor = {
          start: { key: startKey, offset: 0 },
          end: { key: endKey, offset: text.length }
        }
      }

      event.preventDefault()
      event.stopPropagation()
      this.partialRender()
    }
  }
}

export default selectionCtrl


Review comment (suggestion):
@fxha Little optimization:

I think `firstTableCell` would be a better variable name, i think `firstTableBlock` is a table block.

`lastTableBlock` is also need to be renamed.

Code after:

  ContentState.prototype.tableCellHandler = function (event) {
    const { start, end } = this.cursor
    const startBlock = this.getBlock(start.key)
    const endBlock = this.getBlock(end.key)
    const { type: startType } = startBlock
    const { type: endType } = endBlock
    if (/th|td/.test(startType) && /th|td/.test(endType)) {
      if (start.key === end.key) {
        const { text, key } = startBlock
        this.cursor = {
          start: { key, offset: 0 },
          end: { key, offset: text.length }
        }
      } else {
        const startTable = this.getTableFromTableCell(startBlock)
        const endTable = this.getTableFromTableCell(endBlock)
        // Check whether both blocks are in the same table.
        if (!startTable || !endTable) {
          console.error('No table found or invalid type.')
          return
        } else if (startTable.key !== endTable.key) {
          // Select entire document
          return
        }
        const firstTableCell = this.firstInDescendant(startTable)
        const lastTableCell = this.lastInDescendant(startTable)
        if (!firstTableCell || !/th|td/.test(firstTableCell.type) ||
          !lastTableCell || !/th|td/.test(lastTableCell.type)) {
          console.error('No table cell found or invalid type.')
          return
        }
        const { key: startKey } = firstTableCell
        const { key: endKey, text } = lastTableCell
        this.cursor = {
          start: { key: startKey, offset: 0 },
          end: { key: endKey, offset: text.length }
        }
      }

      event.preventDefault()
      event.stopPropagation()
      this.partialRender()
    }
  }
}

export default selectionCtrl