codeblocks - 如何将代码标签放在 quill pre 标签内
问题描述
我正在尝试格式化pre
带有块的羽毛笔标签以code
进行格式化。
所以,例如,我想要这个:
<pre>
<code>
...my code
</code>
</pre>
默认情况下,Quill 会删除您直接添加到 DOM 的任何内容,因此您必须在 Quill 中执行此操作。
我用这个潜在的代码找到了这个,但它没有添加代码块。它并没有真正做任何事情。
var BlockEmbed = Quill.import('blots/block/embed');
class CustomCode extends BlockEmbed {
static create(value) {
let { lang, content } = value;
let node = super.create(value);
const code = document.createElement('code');
code.setAttribute('class', lang);
code.textContent = content;
node.appendChild(code);
return node;
}
static value(node) {
return {
lang: node.firstChild.getAttribute('class'),
content: node.firstChild.innerText
};
}
}
CustomCode.blotName = 'code-custom';
CustomCode.tagName = 'pre';
Quill.register('modules/CustomCode', CustomCode);
var editor = new Quill("#editor", {
modules: {
syntax: true
},
theme: "bubble"
});
有什么想法,还是我错过了什么?
解决方案
你几乎做对了。
为了让它工作,只需要:
- 注册它有点不同
Quill.register(CustomCode);
- 调整
CustomCode
类以反映该create
方法将字符串作为输入并且该方法应返回标签value
内的文本内容这一事实code
- 调整新创建的编辑器的工具栏,以确保我们有一个按钮为我们提供新功能
- 稍微设计一下那个按钮
我在 StackBlitz 上准备了一个演示来说明这些要点。
为了尝试“自定义代码”功能,请单击编辑器工具栏中的“代码”按钮并粘贴/键入您的代码段,然后点击确定。
这是一个示例片段:function something() {console.log('It works');}
推荐阅读
- ansible - 使用 hash_vault Ansible 插件无法访问 Hashicorp Vault KV 存储版本 2
- r - 添加水平分位数线到散点图ggplot2 R
- yii2 - yii2 将线程动作函数路由到控制器
- ruby-on-rails - Rails 表单未提交
- mysql - 无法在mysql上创建外键
- python - “名称错误:名称‘键’未定义无法显示”错误使用瓶
- java - 无法使用 Spring Webflux 和 Thymeleaf 对静态资源进行版本控制
- java - 如何使用外部 java 应用程序配置 OBIEE 12c 身份验证并绕过用户名和密码?在 BI 套件中?
- android - 约束到底部但允许键盘覆盖视图
- asp.net-mvc-4 - ASP.NET MVC 多重身份验证