mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
7f37ba737c
* Update Copybara options in preparation for open-sourcing the source editor * Move files * Update paths * Remove source-editor module and checks for its existence * Explicitly mention CM6 license in files that contain code adapted from CM6 GitOrigin-RevId: 89b7cc2b409db01ad103198ccbd1b126ab56349b
47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import { EditorSelection } from '@codemirror/state'
|
|
import { getIndentUnit, indentString, indentUnit } from '@codemirror/language'
|
|
import { EditorView } from '@codemirror/view'
|
|
|
|
export const indentMore = (view: EditorView) => {
|
|
view.dispatch(
|
|
view.state.changeByRange(range => {
|
|
const doc = view.state.doc
|
|
|
|
const changes = []
|
|
|
|
if (range.empty) {
|
|
// insert space(s) at the cursor
|
|
const line = doc.lineAt(range.from)
|
|
const unit = getIndentUnit(view.state)
|
|
const offset = range.from - line.from
|
|
const cols = unit - (offset % unit)
|
|
const insert = indentString(view.state, cols)
|
|
|
|
changes.push({ from: range.from, insert })
|
|
} else {
|
|
// indent selected lines
|
|
const insert = view.state.facet(indentUnit)
|
|
let previousLineNumber = -1
|
|
for (let pos = range.from; pos <= range.to; pos++) {
|
|
const line = doc.lineAt(pos)
|
|
if (previousLineNumber === line.number) {
|
|
continue
|
|
}
|
|
changes.push({ from: line.from, insert })
|
|
previousLineNumber = line.number
|
|
}
|
|
}
|
|
|
|
const changeSet = view.state.changes(changes)
|
|
|
|
return {
|
|
changes: changeSet,
|
|
range: EditorSelection.range(
|
|
changeSet.mapPos(range.anchor, 1),
|
|
changeSet.mapPos(range.head, 1)
|
|
),
|
|
}
|
|
})
|
|
)
|
|
return true
|
|
}
|