创建插件

YDoc 插件是发布到 npm 的 node 包,本篇教程假定你已经拥有了 npm 和 node 相关的经验。

基本插件

目录结构

一个基本的插件有如下的文件结构:

  1. ├── ydoc-plugin-demo/
  2. ├── index.js
  3. ├── package.json

index.js

index.js 是插件的入口文件,init、finish、page:before、page 是插件绑定的钩子。每个插件都可以绑定不同的钩子实现各种各样的功能。

  1. module.exports = {
  2. init: function() {
  3. console.log('init');
  4. },
  5. finish: function() {
  6. console.log('end...');
  7. },
  8. 'page:before': function(page) {
  9. console.log('beforePage', page);
  10. },
  11. page: function(page) {
  12. console.log('page', page);
  13. }
  14. }

私有插件

私有插件的配置直接写在 YDoc 的配置文件中,在 plugin 字段中新增数组项:

  • plugins[n].name 为插件名称,不允许重名
  • plugins[n].module 和基本插件中的 index.jsmodule.exports 内容一致
  1. module.exports = {
  2. plugins: [{
  3. name: "privatePlugin",
  4. module: {
  5. init: function() {
  6. console.log('init privatePlugin successfully!');
  7. }
  8. }
  9. }]
  10. }

我们鼓励开发者将插件发布到 npm 社区