focalboard/webapp/src/components/cardDetail.tsx

259 lines
10 KiB
TypeScript
Raw Normal View History

2020-10-20 21:50:53 +02:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2020-10-20 21:52:56 +02:00
import React from 'react'
import {FormattedMessage, IntlShape, injectIntl} from 'react-intl'
2020-10-21 03:28:55 +02:00
import {BlockIcons} from '../blockIcons'
import {MutableTextBlock} from '../blocks/textBlock'
import {BoardTree} from '../viewModel/boardTree'
2020-11-03 19:35:24 +01:00
import {PropertyType} from '../blocks/board'
2020-11-11 18:21:16 +01:00
import {CardTree} from '../viewModel/cardTree'
import mutator from '../mutator'
import {Utils} from '../utils'
2020-10-21 03:28:55 +02:00
import MenuWrapper from '../widgets/menuWrapper'
import Menu from '../widgets/menu'
2020-11-03 20:15:16 +01:00
import PropertyMenu from '../widgets/propertyMenu'
import Editable from '../widgets/editable'
2020-10-27 22:04:38 +01:00
import Button from '../widgets/buttons/button'
import EmojiIcon from '../widgets/icons/emoji'
import {MarkdownEditor} from './markdownEditor'
2020-10-25 15:36:49 +01:00
import ContentBlock from './contentBlock'
import CommentsList from './commentsList'
import BlockIconSelector from './blockIconSelector'
2020-10-26 12:43:16 +01:00
import PropertyValueElement from './propertyValueElement'
import './cardDetail.scss'
type Props = {
2020-10-20 21:50:53 +02:00
boardTree: BoardTree
2020-11-11 18:21:16 +01:00
cardTree: CardTree
intl: IntlShape
2020-12-17 21:02:12 +01:00
readonly: boolean
}
type State = {
title: string
}
class CardDetail extends React.Component<Props, State> {
2020-10-20 21:50:53 +02:00
private titleRef = React.createRef<Editable>()
2020-11-12 23:06:19 +01:00
shouldComponentUpdate(): boolean {
2020-10-25 14:40:47 +01:00
return true
}
2020-11-12 23:06:19 +01:00
componentDidMount(): void {
if (!this.state.title) {
this.titleRef.current?.focus()
}
2020-11-12 23:06:19 +01:00
}
2020-10-20 21:50:53 +02:00
constructor(props: Props) {
super(props)
this.state = {
2020-11-11 18:21:16 +01:00
title: props.cardTree.card.title,
}
2020-10-20 21:50:53 +02:00
}
render() {
2020-11-11 18:21:16 +01:00
const {boardTree, cardTree, intl} = this.props
2020-10-20 21:50:53 +02:00
const {board} = boardTree
if (!cardTree) {
return null
2020-10-20 21:50:53 +02:00
}
2020-10-21 03:28:55 +02:00
const {card, comments} = cardTree
2020-10-20 21:50:53 +02:00
let contentElements
if (cardTree.contents.length > 0) {
contentElements =
(<div className='octo-content'>
2020-10-25 15:36:49 +01:00
{cardTree.contents.map((block) => (
<ContentBlock
key={block.id}
block={block}
card={card}
contents={cardTree.contents}
2020-12-17 21:02:12 +01:00
readonly={this.props.readonly}
2020-10-25 15:36:49 +01:00
/>
))}
2020-10-20 21:50:53 +02:00
</div>)
} else {
2020-10-20 21:50:53 +02:00
contentElements = (<div className='octo-content'>
<div className='octo-block'>
2020-10-20 21:50:53 +02:00
<div className='octo-block-margin'/>
2020-12-17 21:02:12 +01:00
{!this.props.readonly &&
<MarkdownEditor
text=''
placeholderText='Add a description...'
onBlur={(text) => {
if (text) {
const block = new MutableTextBlock()
block.parentId = card.id
block.rootId = card.rootId
block.title = text
block.order = (this.props.cardTree.contents.length + 1) * 1000
mutator.insertBlock(block, 'add card text')
}
}}
/>
}
2020-10-20 21:50:53 +02:00
</div>
</div>)
}
2020-10-20 21:50:53 +02:00
const icon = card.icon
return (
2020-10-20 21:50:53 +02:00
<>
<div className='CardDetail content'>
<BlockIconSelector
block={card}
size='l'
2020-12-17 21:02:12 +01:00
readonly={this.props.readonly}
/>
2020-12-17 21:02:12 +01:00
{!this.props.readonly && !icon &&
<div className='add-buttons'>
<Button
onClick={() => {
const newIcon = BlockIcons.shared.randomIcon()
mutator.changeIcon(card, newIcon)
2020-10-27 22:00:15 +01:00
}}
icon={<EmojiIcon/>}
2020-10-27 22:00:15 +01:00
>
<FormattedMessage
id='CardDetail.add-icon'
2020-12-11 20:10:25 +01:00
defaultMessage='Add icon'
/>
</Button>
</div>}
2020-10-20 21:50:53 +02:00
<Editable
ref={this.titleRef}
className='title'
value={this.state.title}
placeholderText='Untitled'
onChange={(title: string) => this.setState({title})}
2020-11-10 22:19:46 +01:00
saveOnEsc={true}
2020-11-09 21:05:55 +01:00
onSave={() => {
2020-11-11 18:21:16 +01:00
if (this.state.title !== this.props.cardTree.card.title) {
2020-11-09 21:05:55 +01:00
mutator.changeTitle(card, this.state.title)
}
}}
2020-11-11 18:21:16 +01:00
onCancel={() => this.setState({title: this.props.cardTree.card.title})}
2020-12-17 21:02:12 +01:00
readonly={this.props.readonly}
/>
2020-10-20 21:50:53 +02:00
{/* Property list */}
<div className='octo-propertylist'>
{board.cardProperties.map((propertyTemplate) => {
2020-10-20 21:50:53 +02:00
return (
<div
key={propertyTemplate.id}
className='octo-propertyrow'
>
2020-12-17 21:02:12 +01:00
{this.props.readonly && <div className='octo-propertyname'>{propertyTemplate.name}</div>}
{!this.props.readonly &&
<MenuWrapper>
<div className='octo-propertyname'><Button>{propertyTemplate.name}</Button></div>
<PropertyMenu
propertyId={propertyTemplate.id}
propertyName={propertyTemplate.name}
propertyType={propertyTemplate.type}
onNameChanged={(newName: string) => mutator.renameProperty(board, propertyTemplate.id, newName)}
onTypeChanged={(newType: PropertyType) => mutator.changePropertyType(boardTree, propertyTemplate, newType)}
onDelete={(id: string) => mutator.deleteProperty(boardTree, id)}
/>
</MenuWrapper>
}
2020-10-26 12:43:16 +01:00
<PropertyValueElement
2020-12-17 21:02:12 +01:00
readOnly={this.props.readonly}
2020-10-26 12:43:16 +01:00
card={card}
2020-10-30 15:22:11 +01:00
boardTree={boardTree}
2020-10-26 12:43:16 +01:00
propertyTemplate={propertyTemplate}
emptyDisplayValue='Empty'
/>
2020-10-20 21:50:53 +02:00
</div>
)
2020-10-20 21:50:53 +02:00
})}
2020-12-17 21:02:12 +01:00
{!this.props.readonly &&
<div className='octo-propertyname add-property'>
<Button
onClick={async () => {
// TODO: Show UI
await mutator.insertPropertyTemplate(boardTree)
}}
>
<FormattedMessage
id='CardDetail.add-property'
defaultMessage='+ Add a property'
/>
</Button>
</div>
}
</div>
2020-10-20 21:50:53 +02:00
{/* Comments */}
2020-12-17 21:02:12 +01:00
{!this.props.readonly &&
<>
<hr/>
<CommentsList
comments={comments}
cardId={card.id}
/>
<hr/>
</>
}
2020-10-20 21:50:53 +02:00
</div>
{/* Content blocks */}
2020-10-20 21:50:53 +02:00
<div className='CardDetail content fullwidth'>
2020-10-20 21:50:53 +02:00
{contentElements}
</div>
2020-12-17 21:02:12 +01:00
{!this.props.readonly &&
<div className='CardDetail content add-content'>
<MenuWrapper>
<Button>
<FormattedMessage
id='CardDetail.add-content'
defaultMessage='Add content'
/>
</Button>
<Menu position='top'>
<Menu.Text
id='text'
name={intl.formatMessage({id: 'CardDetail.text', defaultMessage: 'Text'})}
onClick={() => {
const block = new MutableTextBlock()
block.parentId = card.id
block.rootId = card.rootId
block.order = (this.props.cardTree.contents.length + 1) * 1000
mutator.insertBlock(block, 'add text')
}}
/>
<Menu.Text
id='image'
name={intl.formatMessage({id: 'CardDetail.image', defaultMessage: 'Image'})}
onClick={() => Utils.selectLocalFile(
(file) => mutator.createImageBlock(card, file, (this.props.cardTree.contents.length + 1) * 1000),
'.jpg,.jpeg,.png',
)}
/>
</Menu>
</MenuWrapper>
</div>
}
</>
2020-10-20 21:50:53 +02:00
)
}
}
export default injectIntl(CardDetail)