【问题标题】:Any way to add a key value pair object in Prismic.io Custom type?有什么方法可以在 Prismic.io 自定义类型中添加键值对对象?
【发布时间】:2021-09-11 04:42:40
【问题描述】:

Prismic 的文档有点模糊,而且他们的社区形式并没有真正反应灵敏,所以我想我也想在这里问一下。在我拥有的自定义类型(产品)中,我希望创建一个(变体)的组转发器,我可以使用它来指定产品的“版本”(例如,视频包将具有分辨率和编解码器作为不同的选项)。我希望在前端显示前端的“漂亮”版本,并且我希望实际后端的 url 安全的小写值。

例子:

variants: {
  'H.264': 'h264',
  'ProRes': 'prores',
  'MP4': 'mp4'
}

另一个例子:

variants: {
  '44.1kHz': '44khz',
  '48kHz': '48khz',
  '96kHz': '96khz
}

解决方案有点复杂,与上面的示例一样,我可以使用.replace(/\s/g, '_').replace(/./g, '').toLowerCase() 将其“破解”为后端可用代码(它会匹配)。但是,对于分辨率,它变得有点复杂:

variants: {
  '1920 x 1080': '1920x1080',
  '1080 x 1920': '1080x1920',
  '1080 (Square)': '1080x1080',
  '4K': '4k'
}

我想如果我能找到一种方法在 Prismic 中制作可重复的键/值映射,我可以创建我需要的任何变体,并使其抽象到足以在任何未来的变体中工作。

这是我自定义类型的 json:

{
  "Main" : {
    "title" : {
      "type" : "StructuredText",
      "config" : {
        "single" : "heading1",
        "label" : "Title",
        "placeholder" : "Product Title"
      }
    },
    "uid" : {
      "type" : "UID",
      "config" : {
        "label" : "UID",
        "placeholder" : "The unique identifier"
      }
    },
    "category" : {
      "type" : "Link",
      "config" : {
        "select" : "document",
        "customtypes" : [ "category" ],
        "label" : "Category",
        "placeholder" : "Product Category"
      }
    },
    "featured_image" : {
      "type" : "Image",
      "config" : {
        "constraint" : { },
        "thumbnails" : [ {
          "name" : "thumb",
          "width" : 150,
          "height" : null
        }, {
          "name" : "small",
          "width" : 300,
          "height" : null
        }, {
          "name" : "medium",
          "width" : 600,
          "height" : null
        }, {
          "name" : "large",
          "width" : 900,
          "height" : null
        }, {
          "name" : "xl",
          "width" : 1200,
          "height" : null
        } ],
        "label" : "featured image"
      }
    },
    "product_primary_color" : {
      "type" : "Color",
      "config" : {
        "label" : "Product Primary Color"
      }
    },
    "short_description" : {
      "type" : "StructuredText",
      "config" : {
        "single" : "paragraph",
        "label" : "Short Description",
        "placeholder" : "The one-liner description"
      }
    },
    "price" : {
      "type" : "Number",
      "config" : {
        "label" : "Price",
        "placeholder" : "The regular price"
      }
    },
    "sale_price" : {
      "type" : "Number",
      "config" : {
        "label" : "Sale Price",
        "placeholder" : "Sale price (if any)"
      }
    },
    "on_sale" : {
      "type" : "Boolean",
      "config" : {
        "default_value" : false,
        "label" : "On sale?"
      }
    },
    "base_download_link" : {
      "type" : "Link",
      "config" : {
        "label" : "Base Download Link",
        "placeholder" : "This is the bucket-level link for product",
        "select" : null
      }
    },
    "upsell_items" : {
      "type" : "Group",
      "config" : {
        "fields" : {
          "upsell_item" : {
            "type" : "Link",
            "config" : {
              "select" : "document",
              "customtypes" : [ "product" ],
              "label" : "Upsell Item",
              "placeholder" : "This are the items that can be upsold/cross-sold"
            }
          }
        },
        "label" : "upsell_items"
      }
    },
// Variants are here ============================================
    "variants" : {
      "type" : "Group",
      "config" : {
        "fields" : {
          "variant" : {
            "type" : "Text",
            "config" : {
              "label" : "variant",
              "placeholder" : "A variant (resolution/sample rate/etc) for the download path on S3"
            }
          }
        },
        "label" : "variants"
      }
    },
    "body" : {
      "type" : "Slices",
      "fieldset" : "Slice zone",
      "config" : {
        "labels" : { },
        "choices" : {
          "embed_slice" : {
            "type" : "SharedSlice"
          },
          "image_text_slice" : {
            "type" : "SharedSlice"
          },
          "wide_text_slice" : {
            "type" : "SharedSlice"
          }
        }
      }
    }
  }
}

【问题讨论】:

    标签: json prismic.io


    【解决方案1】:

    目前这是不可能的,但我们正在将此作为开放功能请求进行跟踪,并在此处提供解决方法: https://community.prismic.io/t/iterating-select-values-in-template-from-other-document/818/2

    谢谢。

    【讨论】:

    • 我发现的解决方法是使用预先格式化的文本作为 json 对象,然后在访问之前只使用 JSON.parse() 字段。这是文档指定的吗?
    • 是的,这与使用正则表达式仅获取字符串括号内的数字类似。
    猜你喜欢
    • 2019-06-18
    • 1970-01-01
    • 2013-09-28
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多