繁体   English   中英

如何将自定义插件添加到 CKEditor 5 版本?

[英]How to add a custom plugin to a CKEditor 5 build?

使用的工具:

介绍

我正在学习如何使用 CKEditor 5 以及如何添加和创建插件。 按照本教程,我已经成功地添加了一个现有的官方插件:

https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/installing-plugins.html#adding-a-plugin-to-a-build

据说在 CKEditor 5 中有两种添加插件的方法。基本上,您可以使用现成的构建并为其添加插件,也可以更改编辑器创建设置。

修改构建的优势在于,无论何时创建 CKEditor 实例,您都不再需要配置任何内容,因为所有内容都已使用所需的设置进行构建。 此外,客户端应用程序不需要使用例如 Webpack 导入代码进行新分发。

我是怎么做的

我在开发过程中不使用 Git/GitHub,因此我从Git 存储库下载了 ckeditor5-build-classic zip 文件,并将内部内容移动到所需的文件夹。 使用 Visual Studio 代码,我将文件夹作为项目打开,并按照上述教程中描述的相同步骤开始操作它:

npm install

然后:

npm install --save-dev @ckeditor/ckeditor5-alignment

我对src/ckeditor.js源代码进行了相同的修改,最后使用以下命令创建了构建:

npm run build

创建构建后,我将所有 3 个结果文件(ckeditor.js、ckeditor.js.map 和翻译文件夹)与index.html页面放在一起:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="ckeditor.js"></script>
    <script type="text/javascript" src="jquery-3.4.1.min.js"></script>
    <script type="text/javascript" src="app.js"></script>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div id="editor-container">
        <div id="editor">CKEditor content.</div>
    </div>
</body>
</html>

我的目录结构:

 Test App+
 |---index.html
 |---app.js
 |---jquery-3.4.1.min.js
 |---ckeditor.js
 |---ckeditor.js.map
 |---translations (folder)
 |---styles.css

这是我的 app.js:

$( document ).ready(function() {
    ClassicEditor
        .create(document.querySelector('#editor'))
        .then(editor => {
            console.log('CKEditor is ready.');
        })
        .catch(error => {
           console.error('Error: ' + error); 
        });
});

当我打开 index.html 时,CKEditor 5 工作得非常好,并且包括添加的插件。

创建我自己的插件(问题

以这种方式安装插件非常简单实用,因此我尝试创建自己的插件并执行相同的过程来安装它。 为此,我一直遵循以下指示:

https://ckeditor.com/docs/ckeditor5/latest/framework/guides/tutorials/implementing-a-block-widget.html https://ckeditor.com/docs/ckeditor5/latest/framework/guides/creating-simple -plugin.html https://github.com/ckeditor/ckeditor5-alignment

我的项目符合这个结构:

 MyPlugin+
 |---package.json
 |---package-lock.json
 |---src+
 |---|---myplugin.js
 |---|---myplugin_ui.js
 |---|---myplugin_editing.js
 |---node_modules+
 |---|---lodash-es (folder)
 |---|---ckeditor5 (folder)
 |---|---@ckeditor (folder)

package.json:

{
  "name": "myplugin",
  "version": "1.0.0",
  "description": "My first CKEditor 5 plugin project.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "RDS",
  "license": "ISC",
  "dependencies": {
    "@ckeditor/ckeditor5-core": "^15.0.0",
    "@ckeditor/ckeditor5-ui": "^15.0.0"
  }
}

myplugin.js:

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import MyPlugin_ui from './myplugin_ui';
import MyPlugin_editing from './myplugin_editing';

export default class MyPlugin extends Plugin
{
    static get requires()
    {
        return [MyPlugin_editing , MyPlugin_ui];
    }
}

myplugin_ui.js:

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';

export default class MyPlugin_ui extends Plugin
{
    init()
    {
        console.log('MyPlugin_ui#init() has been called.');
    }
}

myplugin_editing.js:

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';

export default class MyPlugin_editing extends Plugin
{
    init()
    {
        console.log('MyPlugin_editing#init() has been called.');
    }
}

当我在 CKEditor 5 项目中安装插件时,构建成功创建。 但是,在测试编辑器时,浏览器会显示以下问题:

ckeditor-duplicated-modules

https://ckeditor.com/docs/ckeditor5/latest/framework/guides/support/error-codes.html#error-ckeditor-duplicated-modules

在显示的链接中说:

因此,除非您的插件没有依赖项,否则您绝不能将插件添加到现有构建中。

但与此同时,教程页面上似乎教导了相反的内容:

https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/installing-plugins.html#adding-a-plugin-to-a-build

我没有太多使用 Node JS 或 npm 的经验。 我确定我的项目中有一些错误配置,但我不知道在哪里。 我相信它可能在我的 package.json 文件中。

我试过的

  1. 从插件项目中删除node_modulespackage-lock.json文件。

结果:

在使用安装的插件构建 CKEditor 构建时,Webpack 说:

ERROR in ../MyPlugin/src/myplugin.js
Module not found: Error: Can't resolve '@ckeditor/ckeditor5-core/src/plugin' in 'C:\Users\RDS\Desktop\CKEditor\MyPlugin\src'
 @ ../MyPlugin/src/myplugin.js 1:0-57 5:38-44
 @ ./src/ckeditor.js

ERROR in ../MyPlugin/src/myplugin_editing.js
Module not found: Error: Can't resolve '@ckeditor/ckeditor5-core/src/plugin' in 'C:\Users\RDS\Desktop\CKEditor\MyPlugin\src'
 @ ../MyPlugin/src/myplugin_editing.js 1:0-57 3:46-52
 @ ../MyPlugin/src/myplugin.js
 @ ./src/ckeditor.js

ERROR in ../MyPlugin/src/myplugin_ui.js
Module not found: Error: Can't resolve '@ckeditor/ckeditor5-core/src/plugin' in 'C:\Users\RDS\Desktop\CKEditor\MyPlugin\src'
 @ ../MyPlugin/src/myplugin_ui.js 1:0-57 3:41-47
 @ ../MyPlugin/src/myplugin.js
 @ ./src/ckeditor.js

ERROR in chunk main [entry]
ckeditor.js
C:\Users\RDS\Desktop\CKEditor\ckeditor5-build-classic-master\src\ckeditor.js 15684830ec81692702e020bf47ce4f65
Unexpected token (4:26)
| 
| 
| class MyPlugin_ui extends !(function webpackMissingModule() { var e = new Error("Cannot find module '@ckeditor/ckeditor5-core/src/plugin'"); e.code = 'MODULE_NOT_FOUND'; throw e; }())
| {
|     init()
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! @ckeditor/ckeditor5-build-classic@15.0.0 build: `webpack --mode production`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the @ckeditor/ckeditor5-build-classic@15.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\RDS\AppData\Roaming\npm-cache\_logs\2019-11-01T12_40_26_177Z-debug.log
  1. 更改 package.json 中设置的dependenciesdevDependencies属性之间的依赖package.json

后果:同样的事情发生。

所有 CKEditor 5 项目依赖项都设置在devDependencies中的 devDependencies 上。 我的插件在CKEditor 5项目中的安装已经通过现有的两种方式完成:

并且再次显示了相同的问题。 啊,还有。 奇怪的事情发生了。 我已经说过我正确执行了 CKeditor 5 alignment 插件的安装。 我什至已经展示了这是如何完成的,毕竟这个问题。 我尝试在本地安装 alignment 插件,我从存储库下载了它。 我通过链接和文件进行了相同的安装,使用这种方法,插件神秘地给了我ckeditor-duplicated-modules问题,如前所述。

我不知道如何正确地从 CKEditor 5 本身在本地安装此插件,而无需从 Internet 下载(使用npm install <module name> )。 我很清楚我做错了什么,但我无法确定它是什么。 如果有人能给我提示、替代方案,当然还有解决方案,我将不胜感激。 我已经尝试了几天了,我不知道我能做什么,不能做什么。 如果有人可以帮助我,我将不胜感激。

在这里找到解决方案没有成功,我了解到我可以在 ckeditor git 存储库中寻求帮助。 请按照以下地址获取解决方案:

https://github.com/ckeditor/ckeditor5/issues/5699

也许这只是措辞,但您不必“安装”您的自定义插件。 只需将您的插件信息添加到 \src\ckeditor.js 文件(导入、builtinPlugins[]、工具栏 [] 等)。 然后执行 npm 运行构建,它将包含在 \build\ckeditor.js

除了引用本地插件外,您将按照与更改 alignment 插件相同的步骤更改 \src\ckeditor.js。 只需确保自定义插件的名称匹配即可。

由于您没有 UI 工具栏代码,我认为您需要的只是 ClassicEditor.builtInPlugins = [..., MyPlugin] 中的导入和条目

这应该足以测试 init

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM