Repository: ueberdosis/tiptap
File: packages/extension-code-block/src/code-block.ts (TypeScript)

Code before:
        }

        if (empty) {
          return editor.commands.command(({ tr }) => {
            const { pos } = $from
            const codeBlockStart = $from.start()
            const codeBlockEnd = $from.end()

            const allText = state.doc.textBetween(codeBlockStart, codeBlockEnd, '\n', '\n')
            const lines = allText.split('\n')

            let currentLineIndex = 0
            let charCount = 0
            const relativeCursorPos = pos - codeBlockStart

            for (let i = 0; i < lines.length; i += 1) {
              if (charCount + lines[i].length >= relativeCursorPos) {
                currentLineIndex = i
                break
              }
              charCount += lines[i].length + 1
            }

            const currentLine = lines[currentLineIndex]
            const leadingSpaces = currentLine.match(/^ */)?.[0] || ''
            const spacesToRemove = Math.min(leadingSpaces.length, this.options.tabSize || 4)

            if (spacesToRemove === 0) {
              return true
            }

            let lineStartPos = codeBlockStart
            for (let i = 0; i < currentLineIndex; i += 1) {
              lineStartPos += lines[i].length + 1
            }

            tr.delete(lineStartPos, lineStartPos + spacesToRemove)

            const cursorPosInLine = pos - lineStartPos
            if (cursorPosInLine <= spacesToRemove) {
              tr.setSelection(TextSelection.create(tr.doc, lineStartPos))
            }

            return true
          })
        }

        return editor.commands.command(({ tr }) => {
          const { from, to } = selection
          const text = state.doc.textBetween(from, to, '\n', '\n')
          const lines = text.split('\n')

Review comment (refactor):
Just a nitpick - I don't like how we re-use the `4` hardcoded here.

Maybe it makes sense to move this out into a constant `const DEFAULT_TAB_SIZE = 4` and use it in `addOptions` and all other occurences.

You could also do something like this:

```
const { tabSize = DEFAULT_TAB_SIZE } = this.options
```

a few lines before you use the value and then just use `tabSize`

Code after:
        const { $from, empty } = selection

        if ($from.parent.type !== this.type) {
          return false
        }

        if (empty) {
          return editor.commands.command(({ tr }) => {
            const { pos } = $from
            const codeBlockStart = $from.start()
            const codeBlockEnd = $from.end()

            const allText = state.doc.textBetween(codeBlockStart, codeBlockEnd, '\n', '\n')
            const lines = allText.split('\n')

            let currentLineIndex = 0
            let charCount = 0
            const relativeCursorPos = pos - codeBlockStart

            for (let i = 0; i < lines.length; i += 1) {
              if (charCount + lines[i].length >= relativeCursorPos) {
                currentLineIndex = i
                break
              }
              charCount += lines[i].length + 1
            }

            const currentLine = lines[currentLineIndex]
            const leadingSpaces = currentLine.match(/^ */)?.[0] || ''
            const spacesToRemove = Math.min(leadingSpaces.length, tabSize)

            if (spacesToRemove === 0) {
              return true
            }

            let lineStartPos = codeBlockStart
            for (let i = 0; i < currentLineIndex; i += 1) {
              lineStartPos += lines[i].length + 1
            }

            tr.delete(lineStartPos, lineStartPos + spacesToRemove)

            const cursorPosInLine = pos - lineStartPos
            if (cursorPosInLine <= spacesToRemove) {
              tr.setSelection(TextSelection.create(tr.doc, lineStartPos))
            }

            return true
          })
        }