Skip to content

Commit

Permalink
feat: add the url widget
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed Nov 25, 2023
1 parent 2b61287 commit 3785058
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
48 changes: 48 additions & 0 deletions widgets/url/assets/decap-cms/widgets/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export const name = 'url'

export const control = createClass({
handleChange: function (e) {
this.props.onChange(e.target.value);
},

render: function () {
var value = this.props.value;
return h('input', {
id: this.props.forID,
className: this.props.classNameWrapper,
type: 'url',
value: value ?? '',
onChange: this.handleChange,
});
},

isValid: function () {
try {
const url = new URL(this.props.value);

const protocols = this.props.field.get('protocols');
if (protocols && !protocols.includes(url.protocol)) {
return {
error: {
message: "invalid protocol.",
}
};
}

return true;
}
catch (e) {
return {
error: {
message: "invalid URL.",
}
};
}
},
});

export const schema = {
properties: {
protocols: { type: 'array' },
},
}
5 changes: 5 additions & 0 deletions widgets/url/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/decap-cms/widgets/url

go 1.21.3

require github.com/hugomods/decap-cms v0.4.0 // indirect
2 changes: 2 additions & 0 deletions widgets/url/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/hugomods/decap-cms v0.4.0 h1:DGUWeog+IlQaNXsd5Vm3esoqzy/K66lpQwEVP3lT28c=
github.com/hugomods/decap-cms v0.4.0/go.mod h1:P9gUtk59PZYBCNVIqiovRPqI13WiKkutMtJl2YQLkMA=
2 changes: 2 additions & 0 deletions widgets/url/hugo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[[module.imports]]
path = "github.com/hugomods/decap-cms"

0 comments on commit 3785058

Please sign in to comment.