A Pen by Oliver Jimenez-Servin on CodePen.
Created
April 15, 2021 00:11
-
-
Save oliverservin/861d0f8a4e1e94917607997f6875bc21 to your computer and use it in GitHub Desktop.
YzNLdVg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form x-data="initialData()" x-init="init()" x-on:trix-action-invoke="if ($event.actionName == 'x-horizontal-rule') insertHorizontalRule($event)"> | |
<input id="x" type="hidden" name="content"> | |
<trix-editor x-ref="trix" input="x"></trix-editor> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function initialData() { | |
return { | |
element: '', | |
init() { | |
Trix.config.blockAttributes.heading1.tagName = 'h2'; | |
this.element = this.$refs.trix; | |
this.insertDividerElements(); | |
}, | |
insertDividerElements() { | |
this.quoteButton.insertAdjacentHTML('afterend', this.horizontalButtonTemplate); | |
}, | |
insertHorizontalRule($event) { | |
$event.target.editor.insertAttachment(this.buildHorizontalRule()); | |
}, | |
buildHorizontalRule() { | |
return new Trix.Attachment({ content: '<hr>', contentType: 'application/vnd.alpinejs.horizontal-rule.html' }); | |
}, | |
get quoteButton() { | |
return this.toolbarElement.querySelector('[data-trix-attribute=quote]'); | |
}, | |
get toolbarElement() { | |
return this.element.toolbarElement; | |
}, | |
get horizontalButtonTemplate() { | |
return '<button type="button" class="trix-button" data-trix-action="x-horizontal-rule" tabindex="-1" title="Divider">HR</button>'; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/trix/1.3.1/trix.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/alpinejs/2.8.2/alpine.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="https://cdnjs.cloudflare.com/ajax/libs/trix/1.3.1/trix.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment