Skip to content

Commit

Permalink
feat: add the todo editor component
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed Nov 25, 2023
1 parent 3785058 commit b51eb35
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
57 changes: 57 additions & 0 deletions editor-components/todo/assets/decap-cms/editor-components/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export default {
// Internal id of the component
id: "todo",
// Visible label
label: "Todo",
// Fields the user need to fill out when adding an instance of the component
fields: [
{
name: 'done',
label: 'Done',
widget: 'boolean'
},
{
name: 'title',
label: 'Title',
widget: 'string'
}
],
// Regex pattern used to search for instances of this block in the markdown document.
// Patterns are run in a multline environment (against the entire markdown document),
// and so generally should make use of the multiline flag (`m`). If you need to capture
// newlines in your capturing groups, you can either use something like
// `([\S\s]*)`, or you can additionally enable the "dot all" flag (`s`),
// which will cause `(.*)` to match newlines as well.
//
// Additionally, it's recommended that you use non-greedy capturing groups (e.g.
// `(.*?)` vs `(.*)`), especially if matching against newline characters.
pattern: /^\-\s*\[([\sx])\]\s*(.+?)$/ms,
// Given a RegExp Match object
// (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match#return_value),
// return an object with one property for each field defined in `fields`.
//
// This is used to populate the custom widget in the markdown editor in the CMS.
fromBlock: function (match) {
console.log(match,{
done: match[1] === 'x',
title: match[2]
})
return {
done: match[1] === 'x',
title: match[2]
};
},
// Given an object with one property for each field defined in `fields`,
// return the string you wish to be inserted into your markdown.
//
// This is used to serialize the data from the custom widget to the
// markdown document
toBlock: function (data) {
return `- [${data.done?'x':' '}] ${data.title}`;
},
// Preview output for this component. Can either be a string or a React component
// (component gives better render performance)
toPreview: function (data) {
return `<p><input ${data.done?' checked':''} disabled type="checkbox">${data.title}</p>`;
}
}
5 changes: 5 additions & 0 deletions editor-components/todo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/decap-cms/editor-components/todo

go 1.21.3

require github.com/hugomods/decap-cms v0.4.0 // indirect
2 changes: 2 additions & 0 deletions editor-components/todo/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 editor-components/todo/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 b51eb35

Please sign in to comment.