Apostrophe CMS 上的预定义小部件

Pre-defined widgets on Apostrophe CMS

我一直在使用 Apostrophe CMS,它很棒。我想要做的是在页面上预定义小部件,而无需用户添加这些小部件,并且这些小部件具有占位符数据。例如,我希望 apostrophe-images 已经在某个区域的页面上并具有占位符图像。然后用户将更改内容以满足他们的需要。那可能吗?

实现此目的的一种有点脆弱的方法是挂接到 apostrophe-pages beforeInsert 方法并检测您想要应用样本数据的某些条件,然后用您的样本填充适当的属性 JSON.

  1. 在编辑器首次创建页面时按照您希望的方式填充页面。

  2. 从数据库中预先填充的属性中复制 JSON,因为 Apostrophe 会存储它(db.aposDocs.find({slug: '/sample'} 或其他)。

  3. 任何你看到从样本中复制的 _id 字段,将其替换为 self.apos.utils.generateId()(不要替换你实际引用的内容的 ID)

  4. lib/modules/apostrophe-pages/index.js


module.exports = {
  types: [
    {
      name: 'default',
      label: 'Default'
    }
    // .. other page types
  ],
  construct: function(self, options) {
    self.beforeInsert = function(req, page, options, callback) {

      ////////////
      // Should i pre populate this new page with sample data?
      if (page.type === 'default') {
        page.body = {
          "type" : "area",
          "items" : [
            {
              "by" : "id",
              "_id" : self.apos.utils.generateId(),
              "pieceIds" : [
                "ck2z5b8y0002xs6q7jcthp2ds"
              ],
              "relationships" : {
                "ck2z5b8y0002xs6q7jcthp2ds" : {
                  "left" : null,
                  "top" : null,
                  "width" : null,
                  "height" : null,
                  "x" : null,
                  "y" : null
                }
              },
              "type" : "apostrophe-images"
            },
            {
              "_id" : self.apos.utils.generateId(),
              "type" : "apostrophe-rich-text",
              "content" : "<p><strong><em>Authors often misinterpret the mom as a surest quilt, when in actuality it feels more like a rimless jeep. Far from the truth, an iffy russia without porcupines is truly a sycamore of jessant farmers.</em></strong></p>\n\n<p> </p>\n"
            }
          ]
        }
      }
      return setImmediate(callback);
    };
  }
};

这将使用包含 apostrophe-images 小部件和 apostrophe-rich-text 小部件的区域预填充页面的 body 属性。

您可以通过在您感兴趣的页面类型上添加一个字段来进一步抑制这种行为,让编辑者选择退出这种行为并根据您的 beforeInsert 条件进行检查。

同样,这个示例有点死板,因为您将图像片段 ID 硬编码到 beforeInsert 中。您可以通过 运行 查询具有特定标签的图像来进一步获得幻想( 'sample'),在填充 属性.

之前,使用外部模块生成 lorem ipsum 文本等