Skip to content

Commit

Permalink
Merge pull request #401 from ThibG/glitch-soc/features/unfold-thread
Browse files Browse the repository at this point in the history
Port the “unfold thread” feature from Mastodon's UI to glitch-soc flavour
  • Loading branch information
beatrix-bitrot authored Apr 26, 2018
2 parents 8f12afb + b383c06 commit f4ed382
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 85 deletions.
34 changes: 19 additions & 15 deletions app/javascript/flavours/glitch/components/column_header.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export default class ColumnHeader extends React.PureComponent {

static propTypes = {
intl: PropTypes.object.isRequired,
title: PropTypes.node.isRequired,
icon: PropTypes.string.isRequired,
title: PropTypes.node,
icon: PropTypes.string,
active: PropTypes.bool,
localSettings : ImmutablePropTypes.map,
multiColumn: PropTypes.bool,
focusable: PropTypes.bool,
extraButton: PropTypes.node,
showBackButton: PropTypes.bool,
notifCleaning: PropTypes.bool, // true only for the notification column
notifCleaningActive: PropTypes.bool,
Expand All @@ -41,10 +41,6 @@ export default class ColumnHeader extends React.PureComponent {
intl: PropTypes.object.isRequired,
};

static defaultProps = {
focusable: true,
}

state = {
collapsed: true,
animating: false,
Expand Down Expand Up @@ -91,7 +87,7 @@ export default class ColumnHeader extends React.PureComponent {
}

render () {
const { intl, icon, active, children, pinned, onPin, multiColumn, focusable, showBackButton, intl: { formatMessage }, notifCleaning, notifCleaningActive } = this.props;
const { intl, icon, active, children, pinned, onPin, multiColumn, extraButton, showBackButton, intl: { formatMessage }, notifCleaning, notifCleaningActive } = this.props;
const { collapsed, animating, animatingNCD } = this.state;

let title = this.props.title;
Expand Down Expand Up @@ -167,18 +163,26 @@ export default class ColumnHeader extends React.PureComponent {
}

if (children || multiColumn) {
collapseButton = <button className={collapsibleButtonClassName} aria-label={formatMessage(collapsed ? messages.show : messages.hide)} aria-pressed={collapsed ? 'false' : 'true'} onClick={this.handleToggleClick}><i className='fa fa-sliders' /></button>;
collapseButton = <button className={collapsibleButtonClassName} title={formatMessage(collapsed ? messages.show : messages.hide)} aria-label={formatMessage(collapsed ? messages.show : messages.hide)} aria-pressed={collapsed ? 'false' : 'true'} onClick={this.handleToggleClick}><i className='fa fa-sliders' /></button>;
}

const hasTitle = icon && title;

return (
<div className={wrapperClassName}>
<h1 tabIndex={focusable ? 0 : null} role='button' className={buttonClassName} aria-label={title} onClick={this.handleTitleClick}>
<i className={`fa fa-fw fa-${icon} column-header__icon`} />
<span className='column-header__title'>
{title}
</span>
<h1 className={buttonClassName}>
{hasTitle && (
<button onClick={this.handleTitleClick}>
<i className={`fa fa-fw fa-${icon} column-header__icon`} />
{title}
</button>
)}

{!hasTitle && backButton}

<div className='column-header__buttons'>
{backButton}
{hasTitle && backButton}
{extraButton}
{ notifCleaning ? (
<button
aria-label={msgEnterNotifCleaning}
Expand Down
78 changes: 44 additions & 34 deletions app/javascript/flavours/glitch/components/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ export default class Status extends ImmutablePureComponent {
onMoveDown: PropTypes.func,
getScrollPosition: PropTypes.func,
updateScrollBottom: PropTypes.func,
expanded: PropTypes.bool,
};

state = {
isExpanded: null,
isExpanded: this.props.expanded,
isCollapsed: false,
autoCollapsed: false,
}

Expand All @@ -71,6 +73,7 @@ export default class Status extends ImmutablePureComponent {

updateOnStates = [
'isExpanded',
'isCollapsed',
]

// If our settings have changed to disable collapsed statuses, then we
Expand All @@ -83,18 +86,21 @@ export default class Status extends ImmutablePureComponent {
// uncollapse our status accordingly.
componentWillReceiveProps (nextProps) {
if (!nextProps.settings.getIn(['collapsed', 'enabled'])) {
if (this.state.isExpanded === false) {
this.setExpansion(null);
if (this.state.isCollapsed) {
this.setCollapsed(false);
}
} else if (
nextProps.collapse !== this.props.collapse &&
nextProps.collapse !== undefined
) this.setExpansion(nextProps.collapse ? false : null);
) this.setCollapsed(nextProps.collapse);
if (nextProps.expanded !== this.props.expanded &&
nextProps.expanded !== undefined
) this.setExpansion(nextProps.expanded);
}

// When mounting, we just check to see if our status should be collapsed,
// and collapse it if so. We don't need to worry about whether collapsing
// is enabled here, because `setExpansion()` already takes that into
// is enabled here, because `setCollapsed()` already takes that into
// account.

// The cases where a status should be collapsed are:
Expand Down Expand Up @@ -138,7 +144,7 @@ export default class Status extends ImmutablePureComponent {
return false;
}
}()) {
this.setExpansion(false);
this.setCollapsed(true);
// Hack to fix timeline jumps on second rendering when auto-collapsing
this.setState({ autoCollapsed: true });
}
Expand All @@ -164,23 +170,26 @@ export default class Status extends ImmutablePureComponent {
}
}

// `setExpansion()` sets the value of `isExpanded` in our state. It takes
// one argument, `value`, which gives the desired value for `isExpanded`.
// The default for this argument is `null`.
// `setCollapsed()` sets the value of `isCollapsed` in our state, that is,
// whether the toot is collapsed or not.

// `setExpansion()` automatically checks for us whether toot collapsing
// `setCollapsed()` automatically checks for us whether toot collapsing
// is enabled, so we don't have to.
setCollapsed = (value) => {
if (this.props.settings.getIn(['collapsed', 'enabled'])) {
this.setState({ isCollapsed: value });
if (value) {
this.setExpansion(false);
}
} else {
this.setState({ isCollapsed: false });
}
}

setExpansion = (value) => {
switch (true) {
case value === undefined || value === null:
this.setState({ isExpanded: null });
break;
case !value && this.props.settings.getIn(['collapsed', 'enabled']):
this.setState({ isExpanded: false });
break;
case !!value:
this.setState({ isExpanded: true });
break;
this.setState({ isExpanded: value });
if (value) {
this.setCollapsed(false);
}
}

Expand All @@ -192,17 +201,17 @@ export default class Status extends ImmutablePureComponent {
parseClick = (e, destination) => {
const { router } = this.context;
const { status } = this.props;
const { isExpanded } = this.state;
const { isCollapsed } = this.state;
if (!router) return;
if (destination === undefined) {
destination = `/statuses/${
status.getIn(['reblog', 'id'], status.get('id'))
}`;
}
if (e.button === 0) {
if (isExpanded === false) this.setExpansion(null);
if (isCollapsed) this.setCollapsed(false);
else if (e.shiftKey) {
this.setExpansion(false);
this.setCollapsed(true);
document.getSelection().removeAllRanges();
} else router.history.push(destination);
e.preventDefault();
Expand All @@ -219,7 +228,7 @@ export default class Status extends ImmutablePureComponent {

handleExpandedToggle = () => {
if (this.props.status.get('spoiler_text')) {
this.setExpansion(this.state.isExpanded ? null : true);
this.setExpansion(!this.state.isExpanded);
}
};

Expand Down Expand Up @@ -278,6 +287,7 @@ export default class Status extends ImmutablePureComponent {
handleRef,
parseClick,
setExpansion,
setCollapsed,
} = this;
const { router } = this.context;
const {
Expand All @@ -295,7 +305,7 @@ export default class Status extends ImmutablePureComponent {
featured,
...other
} = this.props;
const { isExpanded } = this.state;
const { isExpanded, isCollapsed } = this.state;
let background = null;
let attachments = null;
let media = null;
Expand Down Expand Up @@ -414,16 +424,16 @@ export default class Status extends ImmutablePureComponent {
};

const computedClass = classNames('status', `status-${status.get('visibility')}`, {
collapsed: isExpanded === false,
'has-background': isExpanded === false && background,
collapsed: isCollapsed,
'has-background': isCollapsed && background,
muted,
}, 'focusable');

return (
<HotKeys handlers={handlers}>
<div
className={computedClass}
style={isExpanded === false && background ? { backgroundImage: `url(${background})` } : null}
style={isCollapsed && background ? { backgroundImage: `url(${background})` } : null}
{...selectorAttribs}
ref={handleRef}
tabIndex='0'
Expand All @@ -439,11 +449,11 @@ export default class Status extends ImmutablePureComponent {
notificationId={this.props.notificationId}
/>
) : null}
{!muted || isExpanded !== false ? (
{!muted || !isCollapsed ? (
<StatusHeader
status={status}
friend={account}
collapsed={isExpanded === false}
collapsed={isCollapsed}
parseClick={parseClick}
/>
) : null}
Expand All @@ -452,20 +462,20 @@ export default class Status extends ImmutablePureComponent {
status={status}
mediaIcon={mediaIcon}
collapsible={settings.getIn(['collapsed', 'enabled'])}
collapsed={isExpanded === false}
setExpansion={setExpansion}
collapsed={isCollapsed}
setCollapsed={setCollapsed}
/>
</header>
<StatusContent
status={status}
media={media}
mediaIcon={mediaIcon}
expanded={isExpanded}
setExpansion={setExpansion}
onExpandedToggle={this.handleExpandedToggle}
parseClick={parseClick}
disabled={!router}
/>
{isExpanded !== false || !muted ? (
{!isCollapsed || !muted ? (
<StatusActionBar
{...other}
status={status}
Expand Down
11 changes: 6 additions & 5 deletions app/javascript/flavours/glitch/components/status_content.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export default class StatusContent extends React.PureComponent {
static propTypes = {
status: ImmutablePropTypes.map.isRequired,
expanded: PropTypes.bool,
setExpansion: PropTypes.func,
collapsed: PropTypes.bool,
onExpandedToggle: PropTypes.func,
media: PropTypes.element,
mediaIcon: PropTypes.string,
parseClick: PropTypes.func,
Expand Down Expand Up @@ -64,7 +65,7 @@ export default class StatusContent extends React.PureComponent {
}

onLinkClick = (e) => {
if (this.props.expanded === false) {
if (this.props.collapsed) {
if (this.props.parseClick) this.props.parseClick(e);
}
}
Expand Down Expand Up @@ -111,8 +112,8 @@ export default class StatusContent extends React.PureComponent {
handleSpoilerClick = (e) => {
e.preventDefault();

if (this.props.setExpansion) {
this.props.setExpansion(this.props.expanded ? null : true);
if (this.props.onExpandedToggle) {
this.props.onExpandedToggle();
} else {
this.setState({ hidden: !this.state.hidden });
}
Expand All @@ -131,7 +132,7 @@ export default class StatusContent extends React.PureComponent {
disabled,
} = this.props;

const hidden = this.props.setExpansion ? !this.props.expanded : this.state.hidden;
const hidden = this.props.onExpandedToggle ? !this.props.expanded : this.state.hidden;

const content = { __html: status.get('contentHtml') };
const spoilerContent = { __html: status.get('spoilerHtml') };
Expand Down
6 changes: 3 additions & 3 deletions app/javascript/flavours/glitch/components/status_icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export default class StatusIcons extends React.PureComponent {
mediaIcon: PropTypes.string,
collapsible: PropTypes.bool,
collapsed: PropTypes.bool,
setExpansion: PropTypes.func.isRequired,
setCollapsed: PropTypes.func.isRequired,
intl: PropTypes.object.isRequired,
};

// Handles clicks on collapsed button
handleCollapsedClick = (e) => {
const { collapsed, setExpansion } = this.props;
const { collapsed, setCollapsed } = this.props;
if (e.button === 0) {
setExpansion(collapsed ? null : false);
setCollapsed(!collapsed);
e.preventDefault();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default class DetailedStatus extends ImmutablePureComponent {
settings: ImmutablePropTypes.map.isRequired,
onOpenMedia: PropTypes.func.isRequired,
onOpenVideo: PropTypes.func.isRequired,
onToggleHidden: PropTypes.func.isRequired,
expanded: PropTypes.bool,
};

handleAccountClick = (e) => {
Expand All @@ -41,7 +43,7 @@ export default class DetailedStatus extends ImmutablePureComponent {

render () {
const status = this.props.status.get('reblog') ? this.props.status.get('reblog') : this.props.status;
const { expanded, setExpansion, settings } = this.props;
const { expanded, onToggleHidden, settings } = this.props;

let media = '';
let mediaIcon = null;
Expand Down Expand Up @@ -114,7 +116,8 @@ export default class DetailedStatus extends ImmutablePureComponent {
media={media}
mediaIcon={mediaIcon}
expanded={expanded}
setExpansion={setExpansion}
collapsed={false}
onExpandedToggle={onToggleHidden}
/>

<div className='detailed-status__meta'>
Expand Down
Loading

0 comments on commit f4ed382

Please sign in to comment.