为撇号 cms 中的常见输入字段扩展小部件

Extend widgets for common input fields in apostrophe cms

我正在用撇号 2.x 创建多个小部件,它们工作正常。现在我想正确地组织和组织它们。 我的大多数模块都有一个选项,可以将内容宽度更改为浏览器宽度。我试图在不将代码放入每个模块的情况下概括此功能。

例如在我的 layout-widget/index.js 中我有这个:

module.exports = {
  extend: 'apostrophe-widgets',
  label: 'Layout',
  addFields: [{
    name: 'size',
    label: 'Modulbreite',
    type: 'select',
    choices: [{
        label: 'Content width',
        value: 'content-width'
      },
      {
        label: 'Full width',
        value: 'full-width'
      }
    ],
    required: true
  }]
};

现在我想通过扩展这个通用模块配置来创建其他小部件,这些小部件可以获得单独的配置选项以及这个 selection。 文件:test-widget/index.js

module.exports = {
  extend: 'layout-widgets',
  label: 'Test',
  addFields: [{
      name: 'headline',
      label: 'Headline',
      type: 'string',
      required: true
    },
    {
      name: 'content',
      label: 'Add Element',
      type: 'area',
      options: {
        widgets: {
          'fact': {},
          'text': {}
        }
      }
    }
  ]
};

但目前我在我的 CMS 中没有看到测试小部件的大小选项。我只看到直接在测试模块中定义的选项。这甚至可能还是我必须将大小 select 插入每个模块本身?

此致

有可能,你只需要写'layout-widgets/index.js'就可以了。该模块不能只设置 addFields,因为它会被扩展它的模块覆盖。相反,它必须使用 beforeConstruct 函数来操作 addFields。这是来自 apostrophe-pieces 模块的示例:

beforeConstruct: function(self, options) {
  options.addFields = [
    {
      type: 'boolean',
      name: 'published',
      label: 'Published',
      def: false
    }
  ].concat(options.addFields || []);
}

beforeConstruct 应与 construct 处于同一级别,即不嵌套在其中。