From b0dbccd283a8e17f23bd627209b9a0de5a167295 Mon Sep 17 00:00:00 2001 From: mikiher Date: Fri, 10 Jan 2025 08:03:41 +0200 Subject: [PATCH] Fix Trix to use paragraphs and break on return --- client/components/ui/VueTrix.vue | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/client/components/ui/VueTrix.vue b/client/components/ui/VueTrix.vue index ace1edd3..5d351c72 100644 --- a/client/components/ui/VueTrix.vue +++ b/client/components/ui/VueTrix.vue @@ -249,11 +249,33 @@ export default { } } return target + }, + enableBreakParagraphOnReturn() { + // Trix works with divs by default, we want paragraphs instead + Trix.config.blockAttributes.default.tagName = 'p' + // Enable break paragraph on Enter (Shift + Enter will still create a line break) + Trix.config.blockAttributes.default.breakOnReturn = true + + // Hack to fix buggy paragraph breaks + // Copied from https://github.com/basecamp/trix/issues/680#issuecomment-735742942 + Trix.Block.prototype.breaksOnReturn = function () { + const attr = this.getLastAttribute() + const config = Trix.getBlockConfig(attr ? attr : 'default') + return config ? config.breakOnReturn : false + } + Trix.LineBreakInsertion.prototype.shouldInsertBlockBreak = function () { + if (this.block.hasAttributes() && this.block.isListItem() && !this.block.isEmpty()) { + return this.startLocation.offset > 0 + } else { + return !this.shouldBreakFormattedBlock() ? this.breaksOnReturn : false + } + } } }, mounted() { /** Override editor configuration */ this.overrideConfig(this.config) + this.enableBreakParagraphOnReturn() /** Check if editor read-only mode is required */ this.decorateDisabledEditor(this.disabledEditor) this.$nextTick(() => { @@ -283,4 +305,4 @@ export default { .trix_container .trix-content { background-color: white; } - \ No newline at end of file +