gpt4 book ai didi

ruby-on-rails - 将按钮添加到 Rails ActionText 工具栏

转载 作者:行者123 更新时间:2023-12-04 10:59:25 25 4
gpt4 key购买 nike

配置 ActionText 以向工具栏添加按钮的最佳方法是什么。例如,一个 h2 和 h3 按钮?

最佳答案

我找不到任何有关执行此操作的“正确”方法的文档,但以下方法有效:

我已经对我的/app/javascript/application.js 文件进行了这些修改,ActiveText 在其中放置了 trix 和 actiontext 的 require 语句。

首先,我通过将 require 返回到变量中来获得 Trix 实例。

var Trix  = require("trix")
console.log("Config", Trix.config);

然后我指定我希望 Trix 识别的额外项目:
Trix.config.blockAttributes.heading2 = {
tagName: 'h2'
}
Trix.config.blockAttributes.heading3 = {
tagName: 'h3'
}
Trix.config.textAttributes.underline = {
tagName: 'u'
}

然后我通过粘贴 Trix 的代码并修改 html 来重建工具栏,这是冗长但易于理解的:
const {lang} = Trix.config;

Trix.config.toolbar = {
getDefaultHTML() { return `\
<div class="trix-button-row">
<span class="trix-button-group trix-button-group--text-tools" data-trix-button-group="text-tools">
<button type="button" class="trix-button trix-button--icon trix-button--icon-bold" data-trix-attribute="bold" data-trix-key="b" title="${lang.bold}" tabindex="-1">${lang.bold}</button>
<button type="button" class="trix-button trix-button--icon trix-button--icon-italic" data-trix-attribute="italic" data-trix-key="i" title="${lang.italic}" tabindex="-1">${lang.italic}</button>
<button type="button" class="trix-button trix-button--icon trix-button--icon-strike" data-trix-attribute="strike" title="${lang.strike}" tabindex="-1">${lang.strike}</button>
<button type="button" class="trix-button " data-trix-attribute="underline" title="h3" tabindex="-1">u</button>
<button type="button" class="trix-button trix-button--icon trix-button--icon-link" data-trix-attribute="href" data-trix-action="link" data-trix-key="k" title="${lang.link}" tabindex="-1">${lang.link}</button>
</span>
<span class="trix-button-group trix-button-group--block-tools" data-trix-button-group="block-tools">
<button type="button" class="trix-button " data-trix-attribute="heading1" title="h1" tabindex="-1">h1</button>
<button type="button" class="trix-button " data-trix-attribute="heading2" title="h2" tabindex="-1">h2</button>
<button type="button" class="trix-button " data-trix-attribute="heading3" title="h3" tabindex="-1">h3</button>
<button type="button" class="trix-button trix-button--icon trix-button--icon-quote" data-trix-attribute="quote" title="${lang.quote}" tabindex="-1">${lang.quote}</button>
<button type="button" class="trix-button trix-button--icon trix-button--icon-code" data-trix-attribute="code" title="${lang.code}" tabindex="-1">${lang.code}</button>
<button type="button" class="trix-button trix-button--icon trix-button--icon-bullet-list" data-trix-attribute="bullet" title="${lang.bullets}" tabindex="-1">${lang.bullets}</button>
<button type="button" class="trix-button trix-button--icon trix-button--icon-number-list" data-trix-attribute="number" title="${lang.numbers}" tabindex="-1">${lang.numbers}</button>
<button type="button" class="trix-button trix-button--icon trix-button--icon-decrease-nesting-level" data-trix-action="decreaseNestingLevel" title="${lang.outdent}" tabindex="-1">${lang.outdent}</button>
<button type="button" class="trix-button trix-button--icon trix-button--icon-increase-nesting-level" data-trix-action="increaseNestingLevel" title="${lang.indent}" tabindex="-1">${lang.indent}</button>
</span>
<span class="trix-button-group trix-button-group--file-tools" data-trix-button-group="file-tools">
<button type="button" class="trix-button trix-button--icon trix-button--icon-attach" data-trix-action="attachFiles" title="${lang.attachFiles}" tabindex="-1">${lang.attachFiles}</button>
</span>
<span class="trix-button-group-spacer"></span>
<span class="trix-button-group trix-button-group--history-tools" data-trix-button-group="history-tools">
<button type="button" class="trix-button trix-button--icon trix-button--icon-undo" data-trix-action="undo" data-trix-key="z" title="${lang.undo}" tabindex="-1">${lang.undo}</button>
<button type="button" class="trix-button trix-button--icon trix-button--icon-redo" data-trix-action="redo" data-trix-key="shift+z" title="${lang.redo}" tabindex="-1">${lang.redo}</button>
</span>
</div>
<div class="trix-dialogs" data-trix-dialogs>
<div class="trix-dialog trix-dialog--link" data-trix-dialog="href" data-trix-dialog-attribute="href">
<div class="trix-dialog__link-fields">
<input type="url" name="href" class="trix-input trix-input--dialog" placeholder="${lang.urlPlaceholder}" aria-label="${lang.url}" required data-trix-input>
<div class="trix-button-group">
<input type="button" class="trix-button trix-button--dialog" value="${lang.link}" data-trix-method="setAttribute">
<input type="button" class="trix-button trix-button--dialog" value="${lang.unlink}" data-trix-method="removeAttribute">
</div>
</div>
</div>
</div>\
`; }
};

这是从 https://github.com/basecamp/trix/blob/master/src/trix/config/toolbar.coffee 中获取(并脱除咖啡因)的

关于ruby-on-rails - 将按钮添加到 Rails ActionText 工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58805984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com