Skip to content

Commit

Permalink
folder contents block
Browse files Browse the repository at this point in the history
  • Loading branch information
mihai-macaneata committed Apr 8, 2020
1 parent 629df95 commit ef65f06
Show file tree
Hide file tree
Showing 4 changed files with 397 additions and 0 deletions.
201 changes: 201 additions & 0 deletions src/components/manage/Blocks/FolderContentsBlock/AddLinkForm.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/**
* Add link form.
* @module components/manage/AnchorPlugin/components/LinkButton/AddLinkForm
*/

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { compose } from 'redux';
import unionClassNames from 'union-class-names';
import { connect } from 'react-redux';
import { map } from 'lodash';
import { doesNodeContainClick } from 'semantic-ui-react/dist/commonjs/lib';
import { defineMessages, injectIntl } from 'react-intl';

import { resetSearchContent, searchContent } from '@plone/volto/actions';
import URLUtils from '@plone/volto/components/manage/AnchorPlugin/utils/URLUtils';

const messages = defineMessages({
placeholder: {
id: 'Enter URL or title',
defaultMessage: 'Enter URL or title',
},
});

class AddLinkForm extends Component {
static propTypes = {
onOverrideContent: PropTypes.func.isRequired,
resetSearchContent: PropTypes.func.isRequired,
searchContent: PropTypes.func.isRequired,
search: PropTypes.arrayOf(
PropTypes.shape({
'@id': PropTypes.string,
'@type': PropTypes.string,
title: PropTypes.string,
description: PropTypes.string,
}),
),
};

static defaultProps = {
placeholder: 'Enter URL or search for content',
search: [],
};

constructor(props) {
super(props);
this.state = {
value: '',
isInvalid: false,
};
this.onRef = this.onRef.bind(this);
this.onChange = this.onChange.bind(this);
this.onKeyDown = this.onKeyDown.bind(this);
this.onSubmit = this.onSubmit.bind(this);
}

componentDidMount() {
this.input.focus();
this.props.resetSearchContent();
document.addEventListener('mousedown', this.handleClickOutside, false);
}

componentWillUnmount() {
document.removeEventListener('mousedown', this.handleClickOutside, false);
}

handleClickOutside = e => {
if (
this.linkFormContainer.current &&
doesNodeContainClick(this.linkFormContainer.current, e)
)
return;
this.onClose();
};

onClose() {
return true;
}

onRef(node) {
this.input = node;
}

linkFormContainer = React.createRef();

onChange({ target: { value } }) {
const nextState = { value };
if (this.state.isInvalid && URLUtils.isUrl(URLUtils.normalizeUrl(value))) {
nextState.isInvalid = false;
}
this.setState(nextState);
if (value && value !== '') {
this.props.searchContent('', {
Title: `*${value}*`,
});
} else {
this.props.resetSearchContent();
}
}

onSelectItem = (e, item) => {
e.preventDefault();
const itemToSave = {
key: item['@id'],
text: item.title,
value: item['@id'],
description: item.description,
external: true,
};
this.setState({
value: itemToSave,
isInvalid: false,
});
this.props.onAddLink(itemToSave)
this.props.resetSearchContent();
this.input.blur();
this.onClose();
};

onKeyDown(e) {
if (e.key === 'Enter') {
e.preventDefault();
this.onSubmit();
} else if (e.key === 'Escape') {
e.preventDefault();
this.onClose();
}
}

onSubmit() {
let { value: url } = this.state;
if (!URLUtils.isMail(URLUtils.normaliseMail(url))) {
url = URLUtils.normalizeUrl(url);
if (!URLUtils.isUrl(url)) {
this.setState({ isInvalid: true });
return;
}
} else {
url = URLUtils.normaliseMail(url);
}
this.input.blur();
this.onClose();
}

render() {
const { value, isInvalid } = this.state;
console.log('link value', this.props);
return (
<div className="link-form-container" ref={this.linkFormContainer}>
<div
style={{ marginLeft: '5px', display: 'flex', alignItems: 'center' }}
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 36 36"
fill="#B8B2C8"
>
<g fillRule="evenodd">
<path d="M27.1318,7.333 C24.4028,4.604 19.9618,4.604 17.2328,7.333 L12.9898,11.576 C11.8428,12.723 11.1288,14.248 10.9778,15.871 C10.8228,17.541 11.2708,19.211 12.2378,20.576 C12.4818,20.919 12.7278,21.213 12.9888,21.475 C13.7848,22.271 14.7778,22.868 15.8608,23.202 C16.5498,23.415 17.2548,23.519 17.9518,23.518 C19.7808,23.518 21.5598,22.804 22.8888,21.475 L23.9498,20.414 L22.5358,19 L21.4748,20.061 C20.1648,21.371 18.2388,21.842 16.4498,21.291 C15.6668,21.049 14.9778,20.635 14.4038,20.061 C14.2218,19.879 14.0478,19.668 13.8698,19.418 C13.1778,18.443 12.8588,17.249 12.9688,16.056 C13.0768,14.896 13.5868,13.808 14.4038,12.99 L18.6468,8.747 C20.5958,6.798 23.7688,6.798 25.7178,8.747 C26.6568,9.687 27.1748,10.942 27.1748,12.283 C27.1748,13.623 26.6568,14.878 25.7178,15.818 L27.1318,17.232 C28.4488,15.915 29.1748,14.157 29.1748,12.283 C29.1748,10.408 28.4488,8.65 27.1318,7.333" />
<path d="M25.0107,16.5254 C24.2147,15.7294 23.2217,15.1324 22.1387,14.7984 C19.6417,14.0284 16.9477,14.6894 15.1107,16.5254 L14.0507,17.5864 L15.4647,19.0004 L16.5247,17.9394 C17.8357,16.6294 19.7587,16.1554 21.5497,16.7094 C22.3337,16.9514 23.0217,17.3644 23.5957,17.9394 C23.7777,18.1214 23.9527,18.3314 24.1307,18.5824 C24.8217,19.5564 25.1417,20.7514 25.0317,21.9444 C24.9237,23.1034 24.4137,24.1924 23.5957,25.0104 L19.3537,29.2534 C17.4047,31.2024 14.2317,31.2024 12.2817,29.2534 C11.3427,28.3134 10.8247,27.0574 10.8247,25.7174 C10.8247,24.3774 11.3427,23.1214 12.2817,22.1824 L10.8677,20.7684 C9.5507,22.0854 8.8247,23.8424 8.8247,25.7174 C8.8247,27.5924 9.5507,29.3504 10.8677,30.6674 C12.2327,32.0314 14.0257,32.7134 15.8177,32.7134 C17.6107,32.7134 19.4027,32.0314 20.7677,30.6674 L25.0107,26.4244 C26.1567,25.2774 26.8717,23.7524 27.0227,22.1294 C27.1777,20.4594 26.7297,18.7894 25.7617,17.4244 C25.5177,17.0814 25.2717,16.7874 25.0107,16.5254" />
</g>
</svg>
<input
onChange={this.onChange}
onKeyDown={this.onKeyDown}
ref={this.onRef}
type="text"
value={value.title}
placeholder={this.props.intl.formatMessage(messages.placeholder)}
/>
</div>
<ul style={{ margin: 0, paddingLeft: '35px' }}>
{map(this.props.search, item => (
<li style={{ padding: '5px' }}>
<button
style={{ cursor: 'pointer' }}
onClick={e => this.onSelectItem(e, item)}
title={item['@id']}
role="link"
>
{item.title}
</button>
</li>
))}
</ul>
</div>
);
}
}

export default compose(
injectIntl,
connect(
state => ({
search: state.search.items,
}),
{ resetSearchContent, searchContent },
),
)(AddLinkForm);
117 changes: 117 additions & 0 deletions src/components/manage/Blocks/FolderContentsBlock/Edit.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* Edit map block.
* @module components/manage/Blocks/Maps/Edit
*/

import React, { Component } from 'react';
import PropTypes, { array } from 'prop-types';
import { Link } from 'react-router-dom';
import { Icon, SidebarPortal, TextWidget } from '@plone/volto/components';
import { Dropdown, Segment, Checkbox, Input, Button } from 'semantic-ui-react';
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
import { Card } from 'semantic-ui-react';
import { settings } from '~/config';
import AddLinkForm from './AddLinkForm';

function removeDuplicates(myArr, prop) {
return myArr.filter((obj, pos, arr) => {
return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos;
});
}

class Edit extends Component {
/**
* Property types.
* @property {Object} propTypes Property types.
* @static
*/
static propTypes = {
selected: PropTypes.bool.isRequired,
block: PropTypes.string.isRequired,
index: PropTypes.number.isRequired,
data: PropTypes.objectOf(PropTypes.any).isRequired,
pathname: PropTypes.string.isRequired,
onChangeBlock: PropTypes.func.isRequired,
onSelectBlock: PropTypes.func.isRequired,
onDeleteBlock: PropTypes.func.isRequired,
onFocusPreviousBlock: PropTypes.func.isRequired,
onFocusNextBlock: PropTypes.func.isRequired,
};

constructor(props) {
super(props);
this.state = {
link: this.props.data.link
};
}
getPath(url) {
if (!url) return '';
return url
.replace(settings.apiPath, '')
.replace(settings.internalApiPath, '');
}


componentDidUpdate(prevProps, prevState) {
if (JSON.stringify(prevState) !== JSON.stringify(this.state)) {
this.onChangeData();
}
}

onChangeData() {
this.props.onChangeBlock(this.props.block, {
...this.props.data,
link: this.state.link,
});
}


onAddLink = link => {
this.setState({
link
});
};

render() {
console.log('link instate', this.state.link)
return (
<div>
{this.state.link && (
<Link className="detailed-link-block" onClick={(e) => e.preventDefault} to={this.getPath(this.state.link.value)}>
<div className="detailed-link-block-item-title">
{this.state.link.text}
</div>
<p>
{this.state.link.description && <p>{this.state.link.description}</p>}
</p>
<Button basic>Read more</Button>
</Link>
) || 'Select a page from sidebar'}
<SidebarPortal selected={true}>
<Segment.Group raised>
<header className="header pulled">
<h2> Detailed Link </h2>
</header>
<Segment className="form sidebar-image-data">
<div className="segment-row">
<AddLinkForm onAddLink={this.onAddLink} />
</div>
</Segment>
<Segment>
{/* <input
onChange={this.onChange}
onKeyDown={this.onKeyDown}
ref={this.onRef}
type="text"
value={value.title}
placeholder={this.props.intl.formatMessage(messages.placeholder)}
/> */}
</Segment>
</Segment.Group>
</SidebarPortal>
</div>
);
}
}

export default injectIntl(Edit);
67 changes: 67 additions & 0 deletions src/components/manage/Blocks/FolderContentsBlock/View.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Edit map block.
* @module components/manage/Blocks/Maps/Edit
*/

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import { injectIntl } from 'react-intl';
import { settings } from '~/config';
import { BodyClass } from '@plone/volto/helpers';

import { Button } from 'semantic-ui-react';
// import AddLinkForm from './AddLinkForm';

function removeDuplicates(myArr, prop) {
return myArr.filter((obj, pos, arr) => {
return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos;
});
}

class View extends Component {
/**
* Property types.
* @property {Object} propTypes Property types.
* @static
*/
static propTypes = {
data: PropTypes.objectOf(PropTypes.any).isRequired,
// pathname: PropTypes.string.isRequired,
};

constructor(props) {
super(props);
this.state = {
link: this.props.data.link
};
}
getPath(url) {
if (!url) return '';
return url
.replace(settings.apiPath, '')
.replace(settings.internalApiPath, '');
}


render() {
return (
<div>
{this.state.link && (
<Link className="detailed-link-block" onClick={(e) => e.preventDefault} to={this.getPath(this.state.link.value)}>
<div className="detailed-link-block-item-title">
{this.state.link.text}
</div>
<p>

{this.state.link.description && <p>{this.state.link.description}</p>}
</p>
<Button basic>Read more</Button>
</Link>
) || 'Select a page from sidebar'}
</div>
);
}
}

export default injectIntl(View);
Loading

0 comments on commit ef65f06

Please sign in to comment.