Skip to content

Commit

Permalink
[example-studio] Add example on how to wrap and customize the block e…
Browse files Browse the repository at this point in the history
…ditor (#209)

* [example-studio] Add example on how to wrap and customize the block editor

* [example-studio] Fix errors in FunkyEditor

* [example-studio] Fix localizedSlug schema bug

* [example-studio] Make a cleaner example of FunkyEditor
  • Loading branch information
skogsmaskin authored and bjoerge committed Sep 27, 2017
1 parent 0a7488a commit b8c7301
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
42 changes: 42 additions & 0 deletions packages/example-studio/components/FunkyEditor/FunkyEditor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import PropTypes from 'prop-types'
import React from 'react'
import {BlockEditor} from 'part:@sanity/form-builder'

function extractTextFromBlocks(blocks) {
return blocks
.filter(val => val._type === 'block')
.map(block => {
return block.children
.filter(child => child._type === 'span')
.map(span => span.text)
.join('')
}).join('')
}

export default class FunkyEditor extends React.Component {
static propTypes = {
type: PropTypes.shape({
title: PropTypes.string
}).isRequired,
level: PropTypes.number,
value: PropTypes.array,
onChange: PropTypes.func.isRequired
};

render() {
const {type, value = [], level, onChange} = this.props
return (
<div>
<BlockEditor
type={type}
level={level}
value={value}
onChange={onChange}
/>
<p>
Your funkyness is <strong>{extractTextFromBlocks(value).length}</strong> characters long
</p>
</div>
)
}
}
1 change: 1 addition & 0 deletions packages/example-studio/components/FunkyEditor/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default} from './FunkyEditor'
5 changes: 5 additions & 0 deletions packages/example-studio/parts/inputResolver.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import FunkyEditor from '../components/FunkyEditor/FunkyEditor'
import Slider from '../components/Slider/Slider'
import {get} from 'lodash'

Expand All @@ -6,5 +7,9 @@ export default function resolveInput(type) {
return Slider
}

if (type.name === 'array' && type.of.find(ofType => ofType.name === 'block')) {
return FunkyEditor
}

return false
}
4 changes: 4 additions & 0 deletions packages/example-studio/sanity.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
{
"implements": "part:@sanity/base/brand-logo",
"path": "components/BrandLogo.js"
},
{
"implements": "part:@sanity/form-builder/input-resolver",
"path": "./parts/inputResolver.js"
}
],
"env": {
Expand Down
2 changes: 1 addition & 1 deletion packages/example-studio/schemas/localeSlug.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default {
title: lang.title,
fieldset: lang.default ? null : 'translations',
options: {
source: document => document.title[lang.id],
source: document => ((document && document.title) ? document.title[lang.id] : ''),
maxLength: 96,
auto: true,
},
Expand Down

0 comments on commit b8c7301

Please sign in to comment.