import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static targets = [
'editor',
'form',
'undo',
'play',
'type',
'cancelEdit',
];
connect() {
if (this.hasEditorTarget && this.hasFormTarget && !this.codeMirror) {
this.initCodeMirrorOnTarget(this.editorTarget)
}
}
initCodeMirrorOnTarget(target) {
this.codeMirror = CodeMirror.fromTextArea(target, {
lineWrapping: true,
matchBrackets: true,
mode: 'sql',
scrollbarStyle: 'null',
})
this.codeMirror.setSize('100%', 250)
const keyMap = {
'Ctrl-Enter': () => this.formTarget.requestSubmit(),
'Cmd-Enter': () => this.formTarget.requestSubmit(),
'Ctrl-/': () => this.codeMirror.execCommand('toggleComment'),
'Cmd-/': () => this.codeMirror.execCommand('toggleComment'),
};
this.codeMirror.addKeyMap(keyMap)
this.selectCellType()
}
selectCellType(event) {
const value = this.typeTarget.options[this.typeTarget.selectedIndex].value
if (value == 3) {
this.codeMirror.setOption('mode', 'sql')
} else {
this.codeMirror.setOption('mode', 'gfm')
}
}
freezeScrollOnNextRender(event) {
document.addEventListener('turbo:render', scrollToBottom);
}
play(event) {
this.playTarget.querySelector('span').innerHTML = 'pending'
this.playTarget.disabled = true
if (this.codeMirror) {
const disableKeyMap = {
'Ctrl-Enter': () => null,
'Cmd-Enter': () => null,
'Ctrl-/': () => null,
'Cmd-/': () => null,
};
this.codeMirror.setOption('readOnly', true)
this.codeMirror.addKeyMap(disableKeyMap)
}
}
cancelEdit(event) {
event.preventDefault()
this.cancelEditTarget.requestSubmit()
}
}
const scrollToBottom = () => {
window.Turbo.navigator.currentVisit.scrolled = true;
window.scrollTo(0, document.body.scrollHeight)
document.removeEventListener('turbo:render', scrollToBottom);
};