Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example on how to wrap and customize the block editor #209

Merged
merged 4 commits into from
Sep 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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