Skip to content

Commit

Permalink
Improve block comment toggling
Browse files Browse the repository at this point in the history
  • Loading branch information
FIameCaster committed Jan 5, 2024
1 parent bdae922 commit edcc167
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions package/src/extensions/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "../utils"

const clipboard = navigator.clipboard
const mod = isMac ? 4 : 2

/**
* Extension that will add automatic indentation, closing of brackets,
Expand Down Expand Up @@ -69,26 +70,29 @@ export const defaultCommands = (
) => {
let newLines = newL.join("\n")
if (newLines != old.join("\n")) {
let last = old.length - 1
let lastLine = newL[last]
let oldLastLine = old[last]
let lastDiff = oldLastLine.length - lastLine.length
let firstDiff = newL[0].length - old[0].length
let firstInsersion = start + (firstDiff < 0 ? newL : old)[0].search(/\S|$/)
let lastInsersion = end - oldLastLine.length + (lastDiff > 0 ? lastLine : oldLastLine).search(/\S|$/)
let offset = start - end + newLines.length + lastDiff
let newCursorStart =
const last = old.length - 1
const lastLine = newL[last]
const oldLastLine = old[last]
const lastDiff = oldLastLine.length - lastLine.length
const firstDiff = newL[0].length - old[0].length
const firstInsersion = start + (firstDiff < 0 ? newL : old)[0].search(/\S|$/)
const lastInsersion =
end - oldLastLine.length + (lastDiff > 0 ? lastLine : oldLastLine).search(/\S|$/)
const offset = start - end + newLines.length + lastDiff
const newCursorStart =
firstInsersion > selectionStart
? selectionStart
: Math.max(firstInsersion, selectionStart + firstDiff)
let newCursorEnd = selectionEnd + start - end + newLines.length
const newCursorEnd = selectionEnd + start - end + newLines.length
insertText(
editor,
newLines,
start,
end,
newCursorStart,
selectionEnd < lastInsersion ? newCursorEnd + lastDiff : Math.max(lastInsersion + offset, newCursorEnd)
selectionEnd < lastInsersion
? newCursorEnd + lastDiff
: Math.max(lastInsersion + offset, newCursorEnd),
)
}
}
Expand Down Expand Up @@ -222,8 +226,7 @@ export const defaultCommands = (

textarea.addEventListener("keydown", e => {
const code = getModifierCode(e),
keyCode = e.keyCode,
mod = isMac ? 4 : 2
keyCode = e.keyCode

if (code == mod && (keyCode == 221 || keyCode == 219)) {
const [start, end] = getSelection()
Expand Down Expand Up @@ -305,16 +308,13 @@ export const defaultCommands = (
: newLines[last] + " " + close

let newText = newLines.join("\n")
insertText(
editor,
newText,
start1,
end1,
insertionPoint > start - start1
? start
: Math.max(start + diff, insertionPoint + start1),
Math.min(end + diff, start1 + newText.length),
)
let firstInsersion = insertionPoint + start1
let newStart = firstInsersion > start ? start : Math.max(start + diff, firstInsersion)
let newEnd =
firstInsersion > end - <any>(start != end)
? end
: Math.min(Math.max(firstInsersion, end + diff), start1 + newText.length)
insertText(editor, newText, start1, end1, newStart, Math.max(newStart, newEnd))
scroll()
}
}
Expand Down

0 comments on commit edcc167

Please sign in to comment.