Skip to content

Commit

Permalink
feat(proto): add rudimentary support for custom widgets
Browse files Browse the repository at this point in the history
Enables the use of arbitrary custom widgets from proto annotations. No
assumptions made on the name of type of the widget.

Custom widget options can be provided either as YAML key/values or as
raw YAML strings.
  • Loading branch information
odsod committed Feb 2, 2023
1 parent efa225b commit 3668fa0
Show file tree
Hide file tree
Showing 7 changed files with 548 additions and 392 deletions.
5 changes: 5 additions & 0 deletions cmd/protoc-gen-netlify-cms/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ func genField(g *generatedYAMLFile, field *cmsv1.Field) {
g.Down()
}
g.Y("multiple: ", strconv.FormatBool(widget.RelationWidget.Multiple))
case *cmsv1.Widget_CustomWidget:
g.Y("widget: ", strconv.Quote(widget.CustomWidget.GetWidget()))
for _, option := range widget.CustomWidget.GetOptions() {
g.Y(option)
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions example/admin/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,14 @@ collections:
hint: "An int64 value."
widget: "number"
value_type: "int"

- name: "custom_value"
label: "CUSTOM VALUE"
comment: "A value with a custom widget."
required: false
hint: "A value with a custom widget."
widget: "test"
bar: 'baz'
foo: 5
outer:
inner: 42
11 changes: 11 additions & 0 deletions proto/einride/netlify/cms/example/v1/kitchen_sink.proto
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ message KitchenSink {
// An int64 value.
int64 int64_value = 9;

// A value with a custom widget.
string custom_value = 10 [(einride.netlify.cms.v1.field) = {
widget: {
custom_widget: {
widget: "test"
options: "outer:"
options: " inner: 42"
}
}
}];

// Example enum.
enum ExampleEnum {
// Default value. This value is unused.
Expand Down
10 changes: 10 additions & 0 deletions proto/einride/netlify/cms/v1/annotations.proto
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ message Widget {
StringWidget string_widget = 19;
// Text widget.
TextWidget text_widget = 20;
// Custom widget.
CustomWidget custom_widget = 21;
}
// Add field validation by specifying a list with a regex pattern and an error message.
// More extensive validation can be achieved with custom widgets.
Expand All @@ -242,6 +244,14 @@ message Widget {
}
}

// The boolean widget translates a toggle switch input to a true/false value.
message CustomWidget {
// Type of the custom widget.
string widget = 1;
// Widget options, as raw YAML strings appended to the custom widget.
repeated string options = 2;
}

// The boolean widget translates a toggle switch input to a true/false value.
message BooleanWidget {
// Default value.
Expand Down
9 changes: 9 additions & 0 deletions proto/gen/cms/einride/netlify/cms/example/v1/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,12 @@ collections:
hint: "An int64 value."
widget: "number"
value_type: "int"

- name: "custom_value"
label: "CUSTOM VALUE"
comment: "A value with a custom widget."
required: false
hint: "A value with a custom widget."
widget: "test"
outer:
inner: 42
93 changes: 53 additions & 40 deletions proto/gen/go/einride/netlify/cms/example/v1/kitchen_sink.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3668fa0

Please sign in to comment.