mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 16:28:31 +00:00
54 lines
1.2 KiB
JavaScript
54 lines
1.2 KiB
JavaScript
import { Extension } from '@tiptap/core'
|
|
|
|
export function getCodeAutoIndentText(textBeforeCursor, indent) {
|
|
const currentLine = textBeforeCursor.split('\n').pop() || ''
|
|
const currentIndent = currentLine.match(/^[\t ]*/)?.[0] || ''
|
|
const shouldIndentNextLine = /[{[(]\s*$/.test(currentLine)
|
|
|
|
return `\n${currentIndent}${shouldIndentNextLine ? indent : ''}`
|
|
}
|
|
|
|
export const CodeAutoIndentExtension = Extension.create({
|
|
name: 'codeAutoIndent',
|
|
priority: 1000,
|
|
|
|
addOptions() {
|
|
return {
|
|
indent: ' ',
|
|
}
|
|
},
|
|
|
|
addKeyboardShortcuts() {
|
|
return {
|
|
Enter: () => {
|
|
const { state } = this.editor
|
|
const { selection } = state
|
|
const { $from } = selection
|
|
|
|
if ($from.parent.type.name !== 'codeBlock') {
|
|
return false
|
|
}
|
|
|
|
const textBeforeCursor = $from.parent.textBetween(
|
|
0,
|
|
$from.parentOffset,
|
|
'\n',
|
|
'\n'
|
|
)
|
|
const indentation = getCodeAutoIndentText(
|
|
textBeforeCursor,
|
|
this.options.indent
|
|
)
|
|
|
|
return this.editor.commands.insertContent(indentation, {
|
|
parseOptions: {
|
|
preserveWhitespace: 'full',
|
|
},
|
|
})
|
|
},
|
|
}
|
|
},
|
|
})
|
|
|
|
export default CodeAutoIndentExtension
|