Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Add placeholder text for empty image captions using css
Browse files Browse the repository at this point in the history
  • Loading branch information
brijeshb42 committed Jun 5, 2019
1 parent 4b7369f commit ee6c341
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
2 changes: 0 additions & 2 deletions rendered.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
}

window.addEventListener('message', function(e) {
console.log(e);
console.log();
if (typeof e.data === 'object') {
return;
}
Expand Down
13 changes: 11 additions & 2 deletions src/components/blocks/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,25 @@ class ImageBlock extends React.Component {
};

render() {
const { block } = this.props;
const { blockProps, block } = this.props;
const data = block.getData();
const src = data.get('src');
const showPlaceholder = block.getLength() === 0 && blockProps.placeholder;

if (src !== null) {
const extraProps = {};

if (showPlaceholder) {
extraProps['data-placeholder'] = blockProps.placeholder;
extraProps.className = 'md-block-image-caption--empty';
}

return (
<div>
<div className="md-block-image-inner-container" onClick={this.focusBlock}>
<img role="presentation" src={src} />
</div>
<figcaption>
<figcaption {...extraProps}>
<EditorBlock {...this.props} />
</figcaption>
</div>
Expand Down
11 changes: 11 additions & 0 deletions src/components/blocks/image.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ figure.md-block-image {
.public-DraftStyleDefault-block {
text-align: center;
}

&.md-block-image-caption--empty {
position: relative;

&::before {
position: absolute;
content: attr(data-placeholder);
left: 0;
opacity: 0.5;
}
}
}

.md-block-image-inner-container {
Expand Down
3 changes: 2 additions & 1 deletion src/components/customrenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import BreakBlock from './blocks/break';

import { Block } from '../util/constants';

export default (setEditorState, getEditorState) => (contentBlock) => {
export default (setEditorState, getEditorState, extraProps) => (contentBlock) => {
// console.log(editorState, onChange);
const type = contentBlock.getType();
switch (type) {
Expand Down Expand Up @@ -36,6 +36,7 @@ export default (setEditorState, getEditorState) => (contentBlock) => {
props: {
setEditorState,
getEditorState,
placeholder: extraProps ? extraProps.imageCaptionPlaceholder : '',
},
};
case Block.BREAK: return {
Expand Down
4 changes: 3 additions & 1 deletion src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class MediumDraftEditor extends React.Component {
description: PropTypes.string,
})),
placeholder: PropTypes.string,
imageCaptionPlaceholder: PropTypes.string,
continuousBlocks: PropTypes.arrayOf(PropTypes.string),
sideButtons: PropTypes.arrayOf(PropTypes.shape({
title: PropTypes.string.isRequired,
Expand Down Expand Up @@ -104,6 +105,7 @@ class MediumDraftEditor extends React.Component {
blockButtons: BLOCK_BUTTONS,
inlineButtons: INLINE_BUTTONS,
placeholder: 'Write your story...',
imageCaptionPlaceholder: 'Add image caption...',
continuousBlocks: [
Block.UNSTYLED,
Block.BLOCKQUOTE,
Expand Down Expand Up @@ -140,7 +142,7 @@ class MediumDraftEditor extends React.Component {
this.toggleBlockType = this._toggleBlockType.bind(this);
this.toggleInlineStyle = this._toggleInlineStyle.bind(this);
this.setLink = this.setLink.bind(this);
this.blockRendererFn = this.props.rendererFn(this.onChange, this.getEditorState);
this.blockRendererFn = this.props.rendererFn(this.onChange, this.getEditorState, this.props);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,12 @@ class App extends React.Component {
setTimeout(this.fetchData, 1000);
}

rendererFn(setEditorState, getEditorState) {
rendererFn(setEditorState, getEditorState, ...args) {
const atomicRenderers = {
embed: AtomicEmbedComponent,
separator: AtomicSeparatorComponent,
};
const rFnOld = rendererFn(setEditorState, getEditorState);
const rFnOld = rendererFn(setEditorState, getEditorState, ...args);
const rFnNew = (contentBlock) => {
const type = contentBlock.getType();
switch(type) {
Expand Down

0 comments on commit ee6c341

Please sign in to comment.