Move add content logic into handler

This commit is contained in:
Chen-I Lim 2021-03-10 11:22:52 -08:00
parent 61015d1b5e
commit c55f414b19
5 changed files with 50 additions and 36 deletions

View file

@ -4,6 +4,7 @@
import {IntlShape} from 'react-intl' import {IntlShape} from 'react-intl'
import {BlockTypes} from '../../blocks/block' import {BlockTypes} from '../../blocks/block'
import {Card} from '../../blocks/card'
import {IContentBlock, MutableContentBlock} from '../../blocks/contentBlock' import {IContentBlock, MutableContentBlock} from '../../blocks/contentBlock'
type ContentHandler = { type ContentHandler = {
@ -11,6 +12,7 @@ type ContentHandler = {
getDisplayText: (intl: IntlShape) => string, getDisplayText: (intl: IntlShape) => string,
getIcon: () => JSX.Element, getIcon: () => JSX.Element,
createBlock: () => MutableContentBlock, createBlock: () => MutableContentBlock,
addBlock(card: Card, contents: readonly IContentBlock[], index: number, intl: IntlShape): void,
createComponent: (block: IContentBlock, intl: IntlShape, readonly: boolean) => JSX.Element, createComponent: (block: IContentBlock, intl: IntlShape, readonly: boolean) => JSX.Element,
} }

View file

@ -3,6 +3,7 @@
import React from 'react' import React from 'react'
import {MutableDividerBlock} from '../../blocks/dividerBlock' import {MutableDividerBlock} from '../../blocks/dividerBlock'
import mutator from '../../mutator'
import DividerIcon from '../../widgets/icons/divider' import DividerIcon from '../../widgets/icons/divider'
import {contentRegistry} from './contentRegistry' import {contentRegistry} from './contentRegistry'
@ -21,6 +22,20 @@ contentRegistry.registerContentType({
createBlock: () => { createBlock: () => {
return new MutableDividerBlock() return new MutableDividerBlock()
}, },
addBlock: (card, contents, index, intl) => {
const newBlock = new MutableDividerBlock()
newBlock.parentId = card.id
newBlock.rootId = card.rootId
const contentOrder = contents.map((o) => o.id)
contentOrder.splice(index, 0, newBlock.id)
const typeName = intl.formatMessage({id: 'ContentBlock.divider', defaultMessage: 'divider'})
mutator.performAsUndoGroup(async () => {
const description = intl.formatMessage({id: 'ContentBlock.addElement', defaultMessage: 'add {type}'}, {type: typeName})
await mutator.insertBlock(newBlock, description)
await mutator.changeCardContentOrder(card, contentOrder, description)
})
},
createComponent: () => <DividerElement/>, createComponent: () => <DividerElement/>,
}) })

View file

@ -5,7 +5,9 @@ import {injectIntl, IntlShape} from 'react-intl'
import {IContentBlock, MutableContentBlock} from '../../blocks/contentBlock' import {IContentBlock, MutableContentBlock} from '../../blocks/contentBlock'
import {MutableImageBlock} from '../../blocks/imageBlock' import {MutableImageBlock} from '../../blocks/imageBlock'
import mutator from '../../mutator'
import octoClient from '../../octoClient' import octoClient from '../../octoClient'
import {Utils} from '../../utils'
import ImageIcon from '../../widgets/icons/image' import ImageIcon from '../../widgets/icons/image'
import {contentRegistry} from './contentRegistry' import {contentRegistry} from './contentRegistry'
@ -57,6 +59,21 @@ contentRegistry.registerContentType({
createBlock: () => { createBlock: () => {
return new MutableImageBlock() return new MutableImageBlock()
}, },
addBlock: (card, contents, index, intl) => {
Utils.selectLocalFile((file) => {
mutator.performAsUndoGroup(async () => {
const typeName = intl.formatMessage({id: 'ContentBlock.image', defaultMessage: 'image'})
const description = intl.formatMessage({id: 'ContentBlock.addElement', defaultMessage: 'add {type}'}, {type: typeName})
const newBlock = await mutator.createImageBlock(card, file, description)
if (newBlock) {
const contentOrder = contents.map((o) => o.id)
contentOrder.splice(index, 0, newBlock.id)
await mutator.changeCardContentOrder(card, contentOrder, description)
}
})
},
'.jpg,.jpeg,.png')
},
createComponent: (block, intl) => { createComponent: (block, intl) => {
return ( return (
<ImageElement <ImageElement

View file

@ -55,6 +55,20 @@ contentRegistry.registerContentType({
createBlock: () => { createBlock: () => {
return new MutableTextBlock() return new MutableTextBlock()
}, },
addBlock: (card, contents, index, intl) => {
const newBlock = new MutableTextBlock()
newBlock.parentId = card.id
newBlock.rootId = card.rootId
const contentOrder = contents.map((o) => o.id)
contentOrder.splice(index, 0, newBlock.id)
const typeName = intl.formatMessage({id: 'ContentBlock.text', defaultMessage: 'text'})
mutator.performAsUndoGroup(async () => {
const description = intl.formatMessage({id: 'ContentBlock.addElement', defaultMessage: 'add {type}'}, {type: typeName})
await mutator.insertBlock(newBlock, description)
await mutator.changeCardContentOrder(card, contentOrder, description)
})
},
createComponent: (block, intl, readonly) => { createComponent: (block, intl, readonly) => {
return ( return (
<TextElement <TextElement

View file

@ -106,51 +106,17 @@ class ContentBlock extends React.PureComponent<Props> {
return <></> return <></>
} }
switch (type) { return (
case 'image': return (
<Menu.Text <Menu.Text
ref={type} ref={type}
id={type} id={type}
name={handler.getDisplayText(intl)} name={handler.getDisplayText(intl)}
icon={handler.getIcon()} icon={handler.getIcon()}
onClick={() => { onClick={() => {
Utils.selectLocalFile((file) => { handler.addBlock(card, contents, index, intl)
mutator.performAsUndoGroup(async () => {
const description = intl.formatMessage({id: 'ContentBlock.addElement', defaultMessage: 'add {type}'}, {type: handler.getDisplayText(intl)})
const newBlock = await mutator.createImageBlock(card, file, description)
if (newBlock) {
const contentOrder = contents.map((o) => o.id)
contentOrder.splice(index, 0, newBlock.id)
await mutator.changeCardContentOrder(card, contentOrder, description)
}
})
},
'.jpg,.jpeg,.png')
}} }}
/> />
) )
default: return (
<Menu.Text
ref={type}
id={type}
name={handler.getDisplayText(intl)}
icon={handler.getIcon()}
onClick={() => {
const newBlock = handler.createBlock()!
newBlock.parentId = card.id
newBlock.rootId = card.rootId
const contentOrder = contents.map((o) => o.id)
contentOrder.splice(index, 0, newBlock.id)
mutator.performAsUndoGroup(async () => {
const description = intl.formatMessage({id: 'ContentBlock.addElement', defaultMessage: 'add {type}'}, {type: handler.getDisplayText(intl)})
await mutator.insertBlock(newBlock, description)
await mutator.changeCardContentOrder(card, contentOrder, description)
})
}}
/>
)
}
} }
} }