X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/9fd5190c702c4ef755efdf91edd24420e5a8bb75..refs/pull/3908/head:/resources/js/components/markdown-editor.js diff --git a/resources/js/components/markdown-editor.js b/resources/js/components/markdown-editor.js index 162dd6771..4c3de91f6 100644 --- a/resources/js/components/markdown-editor.js +++ b/resources/js/components/markdown-editor.js @@ -14,7 +14,11 @@ export class MarkdownEditor extends Component { this.display = this.$refs.display; this.input = this.$refs.input; - this.settingContainer = this.$refs.settingContainer; + this.divider = this.$refs.divider; + this.displayWrap = this.$refs.displayWrap; + + const settingContainer = this.$refs.settingContainer; + const settingInputs = settingContainer.querySelectorAll('input[type="checkbox"]'); this.editor = null; initEditor({ @@ -23,10 +27,11 @@ export class MarkdownEditor extends Component { displayEl: this.display, inputEl: this.input, drawioUrl: this.getDrawioUrl(), + settingInputs: Array.from(settingInputs), text: { serverUploadLimit: this.serverUploadLimitText, imageUploadError: this.imageUploadErrorText, - } + }, }).then(editor => { this.editor = editor; this.setupListeners(); @@ -75,19 +80,40 @@ export class MarkdownEditor extends Component { toolbarLabel.closest('.markdown-editor-wrap').classList.add('active'); }); - // Setting changes - this.settingContainer.addEventListener('change', e => { - const actualInput = e.target.parentNode.querySelector('input[type="hidden"]'); - const name = actualInput.getAttribute('name'); - const value = actualInput.getAttribute('value'); - window.$http.patch('/preferences/update-boolean', {name, value}); - // TODO - Update state locally - }); - // Refresh CodeMirror on container resize const resizeDebounced = debounce(() => this.editor.cm.refresh(), 100, false); const observer = new ResizeObserver(resizeDebounced); observer.observe(this.elem); + + this.handleDividerDrag(); + } + + handleDividerDrag() { + this.divider.addEventListener('pointerdown', event => { + const wrapRect = this.elem.getBoundingClientRect(); + const moveListener = (event) => { + const xRel = event.pageX - wrapRect.left; + const xPct = Math.min(Math.max(20, Math.floor((xRel / wrapRect.width) * 100)), 80); + this.displayWrap.style.flexBasis = `${100-xPct}%`; + this.editor.settings.set('editorWidth', xPct); + }; + const upListener = (event) => { + window.removeEventListener('pointermove', moveListener); + window.removeEventListener('pointerup', upListener); + this.display.style.pointerEvents = null; + document.body.style.userSelect = null; + this.editor.cm.refresh(); + }; + + this.display.style.pointerEvents = 'none'; + document.body.style.userSelect = 'none'; + window.addEventListener('pointermove', moveListener); + window.addEventListener('pointerup', upListener); + }); + const widthSetting = this.editor.settings.get('editorWidth'); + if (widthSetting) { + this.displayWrap.style.flexBasis = `${100-widthSetting}%`; + } } scrollToTextIfNeeded() {