reactjs - 如何在 ckeditor4-react 中添加自定义插件?
问题描述
我已经按照 CKEditor 4 文档创建了一个基本插件,但它似乎没有在我的反应应用程序中注册。我添加了插件文件结构,并在节点模块中添加了 plugin.js 以及图标。如何将它传递给 ckeditor4-react 中的配置?
import logo from './logo.svg';
import './App.css';
import CKEditor from 'ckeditor4-react';
function App() {
return (
<div className="App">
<header className="App-header">
<h2>Using CKEditor 4 in React</h2>
<CKEditor
config={{
extraPlugins: "timestamp"
}}
data="<p>Hello from CKEditor 4!</p>"
/>
在 plugin.js (node_modules/ckeditor4-react/plugins/timestamp/plugin.js
CKEDITOR.plugins.add( 'timestamp', {
icons: 'timestamp',
init: function( editor ) {
editor.addCommand( 'insertTimestamp', {
exec: function( editor ) {
var now = new Date();
editor.insertHtml( 'The current date and time is: <em>' + now.toString() + '</em>' );
}
});
editor.ui.addButton( 'Timestamp', {
label: 'Insert Timestamp',
command: 'insertTimestamp',
toolbar: 'insert'
});
}
});
解决方案
您可以通过以下方式实现加载自定义插件。
import CKEditor from "ckeditor4-react";
function App() {
return (
<div className="App">
<CKEditor
data="<p>Hello from CKEditor 4!</p>"
config={{
toolbar: [["Bold"], ["Timestamp"]],
extraPlugins: "timestamp",
}}
onBeforeLoad={(CKEDITOR) => {
CKEDITOR.plugins.add("timestamp", {
init: function (editor) {
editor.addCommand("insertTimestamp", {
exec: function (editor) {
var now = new Date();
editor.insertHtml(
"The current date and time is: <em>" +
now.toString() +
"</em>"
);
},
});
editor.ui.addButton("Timestamp", {
label: "Insert Timestamp",
command: "insertTimestamp",
toolbar: "insert",
icon: "https://cdn4.iconfinder.com/data/icons/24x24-free-pixel-icons/24/Clock.png",
});
},
});
}}
/>
</div>
);
}
export default App;
您还可以将本地图标(确保图标的大小为 24x24)传递给插件,例如 -
import timestampIcon from "./timestampIcon.svg';
...
editor.ui.addButton("Timestamp", {
label: "Insert Timestamp",
command: "insertTimestamp",
toolbar: "insert",
icon: timestampIcon
});
...
推荐阅读
- solidity - 我正在做一个 Lottery 智能合约,但有时当我执行 setWinner 函数时会出错。有人能告诉我为什么吗?
- xcode - 为什么我在 Xcode 10.1 的同一个项目下执行 2 个 C 文件时出错?
- .htaccess - 如何从 URL 中删除文件夹?
- angular - 将jsPDF autoTable插件导入离子角度项目
- google-api - 如何通过电子邮件地址 gmail.com 获取名字和姓氏?
- excel - 访问 VBA(打开 excel 文件并关闭):关闭“文件现在可用”
- javascript - 如何列出我使用 Json 选择的品牌型号?
- json - 我需要从 scala 中的嵌套 json 文件创建一个 spark 数据框
- python - 将文件从 S3 Bucket 传输到另一个保存文件夹结构 - python boto
- django - 检索所有数据库对象及其相关对象 Django