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

TRY: Footnotes #2818

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion blocks/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import {
*/
import { getBlockType } from './registration';

const FixedBlockUids = {
'core/footnotes': '[[footnotes]]',
};

/**
* Returns a block object given its type and attributes.
*
Expand Down Expand Up @@ -46,7 +50,7 @@ export function createBlock( name, blockAttributes = {} ) {
// Blocks are stored with a unique ID, the assigned type name,
// and the block attributes.
return {
uid: uuid(),
uid: FixedBlockUids[ name ] || uuid(),
name,
isValid: true,
attributes,
Expand Down
47 changes: 44 additions & 3 deletions blocks/editable/format-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class FormatToolbar extends Component {
settingsVisible: false,
opensInNewWindow: false,
newLinkValue: '',
showFootnoteEntry: false,
footnote: '',
};

this.addLink = this.addLink.bind( this );
Expand All @@ -56,6 +58,10 @@ class FormatToolbar extends Component {
this.onChangeLinkValue = this.onChangeLinkValue.bind( this );
this.toggleLinkSettingsVisibility = this.toggleLinkSettingsVisibility.bind( this );
this.setLinkTarget = this.setLinkTarget.bind( this );

this.addFootnote = this.addFootnote.bind( this );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related: #2474

This is begging to be extensible behavior, not engrained into the toolbar itself.

this.onFootnoteChange = this.onFootnoteChange.bind( this );
this.submitFootnote = this.submitFootnote.bind( this );
}

componentDidMount() {
Expand Down Expand Up @@ -83,6 +89,8 @@ class FormatToolbar extends Component {
settingsVisible: false,
opensInNewWindow: !! nextProps.formats.link && !! nextProps.formats.link.target,
newLinkValue: '',
showFootnoteEntry: false,
foonote: '',
} );
}
}
Expand Down Expand Up @@ -131,13 +139,29 @@ class FormatToolbar extends Component {
}
}

onFootnoteChange( event ) {
this.setState( { footnote: event.target.value } );
}

addFootnote() {
this.setState( { showFootnoteEntry: true } );
}

submitFootnote( event ) {
event.preventDefault();

this.props.onChange( { footnote: { text: this.state.footnote } } );
}

render() {
const { formats, focusPosition, enabledControls = DEFAULT_CONTROLS } = this.props;
const { isAddingLink, isEditingLink, newLinkValue, settingsVisible, opensInNewWindow } = this.state;
const { isAddingLink, isEditingLink, newLinkValue, settingsVisible, opensInNewWindow, showFootnoteEntry, footnote } = this.state;
const linkStyle = focusPosition
? { position: 'absolute', ...focusPosition }
: null;

const isNodeFootnote = formats.link && formats.link.node.getAttribute( 'data-footnote-id' );

const toolbarControls = FORMATTING_CONTROLS
.filter( control => enabledControls.indexOf( control.format ) !== -1 )
.map( ( control ) => ( {
Expand All @@ -160,10 +184,17 @@ class FormatToolbar extends Component {
icon: 'admin-links',
title: __( 'Link' ),
onClick: this.addLink,
isActive: isAddingLink || !! formats.link,
isActive: ( isAddingLink || !! formats.link ) && ! isNodeFootnote,
} );
}

toolbarControls.push( {
icon: 'format-audio',
title: __( 'Footnote' ),
onClick: this.addFootnote,
isActive: isNodeFootnote,
} );

return (
<div className="blocks-format-toolbar">
<Toolbar controls={ toolbarControls } />
Expand All @@ -181,7 +212,7 @@ class FormatToolbar extends Component {
</form>
}

{ !! formats.link && ! isAddingLink && ! isEditingLink &&
{ ! isNodeFootnote && !! formats.link && ! isAddingLink && ! isEditingLink &&
<div className="blocks-format-toolbar__link-modal" style={ linkStyle }>
<a
className="blocks-format-toolbar__link-value"
Expand All @@ -196,6 +227,16 @@ class FormatToolbar extends Component {
{ linkSettings }
</div>
}

{ ( isNodeFootnote || showFootnoteEntry ) &&
<form
style={ linkStyle }
className="blocks-format-toolbar__footnote-modal"
onSubmit={ this.submitFootnote }>
<textarea value={ footnote } onChange={ this.onFootnoteChange } placeholder={ __( 'Footnote' ) } />
<IconButton icon="yes" type="submit" label={ __( 'Apply' ) } />
</form>
}
</div>
);
}
Expand Down
20 changes: 20 additions & 0 deletions blocks/editable/format-toolbar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,23 @@
margin: 0;
}
}


.blocks-format-toolbar__footnote-modal {
position: absolute;
box-shadow: 0px 3px 20px rgba( 18, 24, 30, .1 ), 0px 1px 3px rgba( 18, 24, 30, .1 );
border: 1px solid #e0e5e9;
background: #fff;
width: 305px;
display: inline-flex;
flex-wrap: wrap;
align-items: center;
font-family: $default-font;
font-size: $default-font-size;
line-height: $default-line-height;
z-index: z-index( '.blocks-format-toolbar__link-modal' );

textarea {
flex-grow: 1
}
}
37 changes: 36 additions & 1 deletion blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import {
find,
defer,
noop,
pick,
} from 'lodash';
import { nodeListToReact } from 'dom-react';
import { Fill } from 'react-slot-fill';
import 'element-closest';
import uuid from 'uuid/v4';

/**
* WordPress dependencies
Expand Down Expand Up @@ -93,6 +95,8 @@ export default class Editable extends Component {
empty: ! value || ! value.length,
selectedNodeId: 0,
};

this.footnotes = props.footnotes || {};
}

getSettings( settings ) {
Expand Down Expand Up @@ -233,7 +237,34 @@ export default class Editable extends Component {

this.savedContent = this.getContent();
this.editor.save();
this.props.onChange( this.savedContent );
this.footnotes = pick( this.footnotes, this.getFootnoteIds( this.savedContent ) );
this.props.onChange( { content: this.savedContent, footnotes: this.footnotes } );
}

getFootnoteIds( content ) {
const footnoteIds = [];
const nodesToVisit = [ ...content ];
let node = nodesToVisit.pop();

while ( node !== undefined ) {
if ( typeof node !== 'string' ) {
if ( node.type === 'a' && node.props[ 'data-footnote-id' ] ) {
footnoteIds.push( node.props[ 'data-footnote-id' ] );
}

if ( node.props.children ) {
if ( Array.isArray( node.props.children ) ) {
node.props.children.forEach( n => nodesToVisit.push( n ) );
} else {
nodesToVisit.push( node.props.children );
}
}
}

node = nodesToVisit.pop();
}

return footnoteIds;
}

getEditorSelectionRect() {
Expand Down Expand Up @@ -564,6 +595,10 @@ export default class Editable extends Component {
} else {
this.editor.execCommand( 'Unlink' );
}
} else if ( format === 'footnote' ) {
const footnoteId = uuid();
this.footnotes[ footnoteId ] = formatValue.text;
this.editor.selection.setContent( `<a href="#footnote-${ footnoteId }" data-footnote-id="${ footnoteId }" contenteditable="false"><sup>[*]</sup></a>` );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is being persisted into the saved content of the post, which is problematic because we don't want/care for this editor-specific attribute to be present on the front-end of the site. Also, it's causing React to log a warning into the developer console.

} else {
const isActive = this.isFormatActive( format );
if ( isActive && ! formatValue ) {
Expand Down
35 changes: 35 additions & 0 deletions blocks/library/footnotes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { mapKeys } from 'lodash';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/**
 * External dependencies
 */


/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

import { registerBlockType } from '../../api';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/**
 * Internal dependencies
 */


registerBlockType( 'core/footnotes', {
title: __( 'Footnotes' ),

icon: 'button',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guessing this block could use the once: true flag (see also: "More" block)


category: 'layout',

attributes: {
footnotes: {
type: 'object',
},
},

save( { attributes } ) {
const { footnotes = {} } = attributes;

const footnoteElements = Object.keys( footnotes ).map( footnoteId => <p key={ footnoteId } id={ `footnote-${ footnoteId }` }>{footnotes[ footnoteId ]}</p> );

return (
<div>
<h2>{ __( 'Footnotes' ) }</h2>
{ footnoteElements }
</div>
);
},
} );
2 changes: 2 additions & 0 deletions blocks/library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ import './text-columns';
import './verse';
import './video';
import './audio';
import './footnotes';

7 changes: 6 additions & 1 deletion blocks/library/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ registerBlockType( 'core/paragraph', {
fontSize: {
type: 'number',
},
footnotes: {
type: 'object',
},
},

transforms: {
Expand Down Expand Up @@ -158,8 +161,10 @@ registerBlockType( 'core/paragraph', {
} }
value={ content }
onChange={ ( nextContent ) => {

setAttributes( {
content: nextContent,
content: nextContent.content,
footnotes: nextContent.footnotes,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is causing paragraph blocks to include an empty footnotes object in comment serialized block attributes whenever the text is changed (regardless of adding footnotes). We should avoid this being included for paragraphs which aren't using footnotes.

} );
} }
focus={ focus }
Expand Down
8 changes: 8 additions & 0 deletions editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ class VisualEditorBlock extends Component {
return result;
}, {} );

if ( attributes.footnotes ) {
this.props.changeFootnotes( { footnotes: attributes.footnotes } );
}

if ( size( metaAttributes ) ) {
this.props.onMetaChange( {
...this.props.meta,
Expand Down Expand Up @@ -454,6 +458,10 @@ export default connect(
dispatch( updateBlockAttributes( uid, attributes ) );
},

changeFootnotes( footnotes ) {
dispatch( updateBlockAttributes( '[[footnotes]]', footnotes ) );
},

onSelect() {
dispatch( selectBlock( ownProps.uid ) );
},
Expand Down