focalboard/webapp/src/mutator.ts

426 lines
15 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-21 03:28:55 +02:00
import {MutableBlock} from './blocks/block'
import {Board, IPropertyOption, IPropertyTemplate, MutableBoard, PropertyType} from './blocks/board'
import {BoardView, ISortOption, MutableBoardView} from './blocks/boardView'
import {Card, MutableCard} from './blocks/card'
import {MutableImageBlock} from './blocks/imageBlock'
import {IOrderedBlock, MutableOrderedBlock} from './blocks/orderedBlock'
2020-10-21 03:54:39 +02:00
import {BoardTree} from './viewModel/boardTree'
2020-10-20 21:52:56 +02:00
import {FilterGroup} from './filterGroup'
import octoClient from './octoClient'
2020-10-21 03:58:48 +02:00
import {IBlock} from './blocks/block'
2020-10-20 21:52:56 +02:00
import undoManager from './undomanager'
import {Utils} from './utils'
2020-10-08 18:21:27 +02:00
//
// The Mutator is used to make all changes to server state
// It also ensures that the Undo-manager is called for each action
//
class Mutator {
2020-10-21 03:47:02 +02:00
private async updateBlock(newBlock: IBlock, oldBlock: IBlock, description: string): Promise<void> {
await undoManager.perform(
async () => {
await octoClient.updateBlock(newBlock)
},
async () => {
await octoClient.updateBlock(oldBlock)
},
description,
)
}
private async updateBlocks(newBlocks: IBlock[], oldBlocks: IBlock[], description: string): Promise<void> {
await undoManager.perform(
async () => {
await octoClient.updateBlocks(newBlocks)
},
async () => {
await octoClient.updateBlocks(oldBlocks)
},
description,
)
}
2020-10-20 21:50:53 +02:00
async insertBlock(block: IBlock, description = 'add', afterRedo?: () => Promise<void>, beforeUndo?: () => Promise<void>) {
await undoManager.perform(
async () => {
await octoClient.insertBlock(block)
await afterRedo?.()
},
async () => {
await beforeUndo?.()
await octoClient.deleteBlock(block.id)
},
description,
)
}
async insertBlocks(blocks: IBlock[], description = 'add', afterRedo?: () => Promise<void>, beforeUndo?: () => Promise<void>) {
await undoManager.perform(
async () => {
await octoClient.insertBlocks(blocks)
await afterRedo?.()
},
async () => {
await beforeUndo?.()
for (const block of blocks) {
await octoClient.deleteBlock(block.id)
}
},
description,
)
}
async deleteBlock(block: IBlock, description?: string, beforeRedo?: () => Promise<void>, afterUndo?: () => Promise<void>) {
if (!description) {
description = `delete ${block.type}`
}
await undoManager.perform(
async () => {
await beforeRedo?.()
await octoClient.deleteBlock(block.id)
},
async () => {
await octoClient.insertBlock(block)
await afterUndo?.()
},
description,
)
}
async changeTitle(block: IBlock, title: string, description = 'change title') {
2020-10-21 03:28:55 +02:00
const newBlock = new MutableBlock(block)
newBlock.title = title
2020-10-21 03:47:02 +02:00
await this.updateBlock(newBlock, block, description)
2020-10-20 21:50:53 +02:00
}
async changeIcon(block: Card | Board, icon: string, description = 'change icon') {
let newBlock: IBlock
2020-10-21 03:28:55 +02:00
switch (block.type) {
case 'card': {
const card = new MutableCard(block)
card.icon = icon
newBlock = card
break
}
case 'board': {
const board = new MutableBoard(block)
board.icon = icon
newBlock = board
break
}
2020-10-21 03:28:55 +02:00
}
2020-10-21 03:47:02 +02:00
await this.updateBlock(newBlock, block, description)
2020-10-20 21:50:53 +02:00
}
async changeOrder(block: IOrderedBlock, order: number, description = 'change order') {
2020-10-21 03:28:55 +02:00
const newBlock = new MutableOrderedBlock(block)
newBlock.order = order
2020-10-21 03:47:02 +02:00
await this.updateBlock(newBlock, block, description)
2020-10-20 21:50:53 +02:00
}
// Property Templates
async insertPropertyTemplate(boardTree: BoardTree, index = -1, template?: IPropertyTemplate) {
const {board, activeView} = boardTree
if (index < 0) {
index = board.cardProperties.length
}
if (!template) {
template = {
id: Utils.createGuid(),
name: 'New Property',
type: 'text',
options: [],
}
}
2020-10-21 03:28:55 +02:00
const oldBlocks: IBlock[] = [board]
2020-10-20 21:50:53 +02:00
2020-10-21 03:28:55 +02:00
const newBoard = new MutableBoard(board)
newBoard.cardProperties.splice(index, 0, template)
const changedBlocks: IBlock[] = [newBoard]
2020-10-20 21:50:53 +02:00
2020-10-20 21:52:56 +02:00
let description = 'add property'
2020-10-20 21:50:53 +02:00
if (activeView.viewType === 'table') {
2020-10-21 03:28:55 +02:00
oldBlocks.push(activeView)
const newActiveView = new MutableBoardView(activeView)
newActiveView.visiblePropertyIds.push(template.id)
changedBlocks.push(newActiveView)
2020-10-20 21:50:53 +02:00
2020-10-20 21:52:56 +02:00
description = 'add column'
2020-10-20 21:50:53 +02:00
}
2020-10-21 03:47:02 +02:00
await this.updateBlocks(changedBlocks, oldBlocks, description)
2020-10-20 21:50:53 +02:00
}
2020-10-21 03:28:55 +02:00
async duplicatePropertyTemplate(boardTree: BoardTree, propertyId: string): Promise<IBlock[]> {
2020-10-20 21:50:53 +02:00
const {board, activeView} = boardTree
2020-10-21 03:28:55 +02:00
const oldBlocks: IBlock[] = [board]
2020-10-20 21:50:53 +02:00
2020-10-21 03:28:55 +02:00
const newBoard = new MutableBoard(board)
const changedBlocks: IBlock[] = [newBoard]
const index = newBoard.cardProperties.findIndex((o) => o.id === propertyId)
2020-10-20 21:50:53 +02:00
if (index === -1) {
2020-10-21 03:28:55 +02:00
Utils.assertFailure(`Cannot find template with id: ${propertyId}`)
return
2020-10-20 21:50:53 +02:00
}
2020-10-21 03:28:55 +02:00
const srcTemplate = newBoard.cardProperties[index]
2020-10-20 21:50:53 +02:00
const newTemplate: IPropertyTemplate = {
id: Utils.createGuid(),
name: `Copy of ${srcTemplate.name}`,
type: srcTemplate.type,
options: srcTemplate.options.slice(),
}
2020-10-21 03:28:55 +02:00
newBoard.cardProperties.splice(index + 1, 0, newTemplate)
2020-10-20 21:50:53 +02:00
2020-10-20 21:52:56 +02:00
let description = 'duplicate property'
2020-10-20 21:50:53 +02:00
if (activeView.viewType === 'table') {
2020-10-21 03:28:55 +02:00
oldBlocks.push(activeView)
const newActiveView = new MutableBoardView(activeView)
newActiveView.visiblePropertyIds.push(newTemplate.id)
changedBlocks.push(newActiveView)
2020-10-20 21:50:53 +02:00
2020-10-20 21:52:56 +02:00
description = 'duplicate column'
2020-10-20 21:50:53 +02:00
}
2020-10-21 03:47:02 +02:00
await this.updateBlocks(changedBlocks, oldBlocks, description)
2020-10-20 21:50:53 +02:00
return changedBlocks
}
async changePropertyTemplateOrder(board: Board, template: IPropertyTemplate, destIndex: number) {
const templates = board.cardProperties
const newValue = templates.slice()
const srcIndex = templates.indexOf(template)
Utils.log(`srcIndex: ${srcIndex}, destIndex: ${destIndex}`)
newValue.splice(destIndex, 0, newValue.splice(srcIndex, 1)[0])
2020-10-21 03:28:55 +02:00
const newBoard = new MutableBoard(board)
newBoard.cardProperties = newValue
2020-10-21 03:47:02 +02:00
await this.updateBlock(newBoard, board, 'reorder properties')
2020-10-20 21:50:53 +02:00
}
async deleteProperty(boardTree: BoardTree, propertyId: string) {
const {board, views, cards} = boardTree
2020-10-21 03:28:55 +02:00
const oldBlocks: IBlock[] = [board]
2020-10-20 21:50:53 +02:00
2020-10-21 03:28:55 +02:00
const newBoard = new MutableBoard(board)
const changedBlocks: IBlock[] = [newBoard]
newBoard.cardProperties = board.cardProperties.filter((o) => o.id !== propertyId)
2020-10-20 21:50:53 +02:00
views.forEach((view) => {
if (view.visiblePropertyIds.includes(propertyId)) {
2020-10-21 03:28:55 +02:00
oldBlocks.push(view)
const newView = new MutableBoardView(view)
newView.visiblePropertyIds = view.visiblePropertyIds.filter((o) => o !== propertyId)
changedBlocks.push(newView)
2020-10-20 21:50:53 +02:00
}
})
cards.forEach((card) => {
if (card.properties[propertyId]) {
2020-10-21 03:28:55 +02:00
oldBlocks.push(card)
const newCard = new MutableCard(card)
delete newCard.properties[propertyId]
changedBlocks.push(newCard)
2020-10-20 21:50:53 +02:00
}
})
2020-10-21 03:47:02 +02:00
await this.updateBlocks(changedBlocks, oldBlocks, 'delete property')
2020-10-20 21:50:53 +02:00
}
async renameProperty(board: Board, propertyId: string, name: string) {
2020-10-21 03:28:55 +02:00
const newBoard = new MutableBoard(board)
2020-10-20 21:50:53 +02:00
2020-10-21 03:28:55 +02:00
const template = newBoard.cardProperties.find((o) => o.id === propertyId)
2020-10-20 21:50:53 +02:00
if (!template) {
2020-10-21 03:28:55 +02:00
Utils.assertFailure(`Can't find property template with Id: ${propertyId}`)
return
2020-10-20 21:50:53 +02:00
}
Utils.log(`renameProperty from ${template.name} to ${name}`)
template.name = name
2020-10-21 03:47:02 +02:00
await this.updateBlock(newBoard, board, 'rename property')
2020-10-20 21:50:53 +02:00
}
// Properties
async insertPropertyOption(boardTree: BoardTree, template: IPropertyTemplate, option: IPropertyOption, description = 'add option') {
const {board} = boardTree
Utils.assert(board.cardProperties.includes(template))
2020-10-21 03:28:55 +02:00
const newBoard = new MutableBoard(board)
const newTemplate = newBoard.cardProperties.find((o) => o.id === template.id)
2020-10-21 03:28:55 +02:00
newTemplate.options.push(option)
2020-10-20 21:50:53 +02:00
2020-10-21 03:47:02 +02:00
await this.updateBlock(newBoard, board, description)
2020-10-20 21:50:53 +02:00
}
async deletePropertyOption(boardTree: BoardTree, template: IPropertyTemplate, option: IPropertyOption) {
const {board} = boardTree
2020-10-21 03:28:55 +02:00
const newBoard = new MutableBoard(board)
const newTemplate = newBoard.cardProperties.find((o) => o.id === template.id)
newTemplate.options = newTemplate.options.filter((o) => o.value !== option.value)
2020-10-20 21:50:53 +02:00
2020-10-21 03:47:02 +02:00
await this.updateBlock(newBoard, board, 'delete option')
2020-10-20 21:50:53 +02:00
}
async changePropertyOptionOrder(board: Board, template: IPropertyTemplate, option: IPropertyOption, destIndex: number) {
2020-10-21 03:28:55 +02:00
const srcIndex = template.options.indexOf(option)
2020-10-20 21:50:53 +02:00
Utils.log(`srcIndex: ${srcIndex}, destIndex: ${destIndex}`)
2020-10-21 03:28:55 +02:00
const newBoard = new MutableBoard(board)
const newTemplate = newBoard.cardProperties.find((o) => o.id === template.id)
2020-10-21 03:28:55 +02:00
newTemplate.options.splice(destIndex, 0, newTemplate.options.splice(srcIndex, 1)[0])
2020-10-20 21:50:53 +02:00
2020-10-21 03:47:02 +02:00
await this.updateBlock(newBoard, board, 'reorder options')
2020-10-20 21:50:53 +02:00
}
async changePropertyOptionValue(boardTree: BoardTree, propertyTemplate: IPropertyTemplate, option: IPropertyOption, value: string) {
const {board, cards} = boardTree
const oldValue = option.value
2020-10-21 03:28:55 +02:00
const oldBlocks: IBlock[] = [board]
2020-10-20 21:50:53 +02:00
2020-10-21 03:28:55 +02:00
const newBoard = new MutableBoard(board)
const newTemplate = newBoard.cardProperties.find((o) => o.id === propertyTemplate.id)
const newOption = newTemplate.options.find((o) => o.value === oldValue)
2020-10-21 03:28:55 +02:00
newOption.value = value
const changedBlocks: IBlock[] = [newBoard]
2020-10-20 21:50:53 +02:00
// Change the value on all cards that have this property too
for (const card of cards) {
const propertyValue = card.properties[propertyTemplate.id]
if (propertyValue && propertyValue === oldValue) {
2020-10-21 03:28:55 +02:00
oldBlocks.push(card)
const newCard = new MutableCard(card)
newCard.properties[propertyTemplate.id] = value
changedBlocks.push(newCard)
2020-10-20 21:50:53 +02:00
}
}
2020-10-21 03:47:02 +02:00
await this.updateBlocks(changedBlocks, oldBlocks, 'rename option')
2020-10-20 21:50:53 +02:00
return changedBlocks
}
2020-10-21 03:28:55 +02:00
async changePropertyOptionColor(board: Board, template: IPropertyTemplate, option: IPropertyOption, color: string) {
const newBoard = new MutableBoard(board)
const newTemplate = newBoard.cardProperties.find((o) => o.id === template.id)
const newOption = newTemplate.options.find((o) => o.value === option.value)
2020-10-21 03:28:55 +02:00
newOption.color = color
2020-10-21 03:47:02 +02:00
await this.updateBlock(newBoard, board, 'change option color')
2020-10-20 21:50:53 +02:00
}
async changePropertyValue(card: Card, propertyId: string, value?: string, description = 'change property') {
2020-10-21 03:28:55 +02:00
const newCard = new MutableCard(card)
newCard.properties[propertyId] = value
2020-10-21 03:47:02 +02:00
await this.updateBlock(newCard, card, description)
2020-10-20 21:50:53 +02:00
}
async changePropertyType(board: Board, propertyTemplate: IPropertyTemplate, type: PropertyType) {
2020-10-21 03:28:55 +02:00
const newBoard = new MutableBoard(board)
const newTemplate = newBoard.cardProperties.find((o) => o.id === propertyTemplate.id)
2020-10-21 03:28:55 +02:00
newTemplate.type = type
2020-10-21 03:47:02 +02:00
await this.updateBlock(newBoard, board, 'change property type')
2020-10-20 21:50:53 +02:00
}
// Views
async changeViewSortOptions(view: BoardView, sortOptions: ISortOption[]) {
2020-10-21 03:28:55 +02:00
const newView = new MutableBoardView(view)
newView.sortOptions = sortOptions
2020-10-21 03:47:02 +02:00
await this.updateBlock(newView, view, 'sort')
2020-10-20 21:50:53 +02:00
}
async changeViewFilter(view: BoardView, filter?: FilterGroup) {
2020-10-21 03:28:55 +02:00
const newView = new MutableBoardView(view)
newView.filter = filter
2020-10-21 03:47:02 +02:00
await this.updateBlock(newView, view, 'filter')
2020-10-20 21:50:53 +02:00
}
async changeViewVisibleProperties(view: BoardView, visiblePropertyIds: string[], description = 'show / hide property') {
2020-10-21 03:28:55 +02:00
const newView = new MutableBoardView(view)
newView.visiblePropertyIds = visiblePropertyIds
2020-10-21 03:47:02 +02:00
await this.updateBlock(newView, view, description)
2020-10-20 21:50:53 +02:00
}
async changeViewGroupById(view: BoardView, groupById: string) {
2020-10-21 03:28:55 +02:00
const newView = new MutableBoardView(view)
newView.groupById = groupById
2020-10-21 03:47:02 +02:00
await this.updateBlock(newView, view, 'group by')
2020-10-20 21:50:53 +02:00
}
// Not a mutator, but convenient to put here since Mutator wraps OctoClient
async exportFullArchive() {
return octoClient.exportFullArchive()
}
// Not a mutator, but convenient to put here since Mutator wraps OctoClient
async importFullArchive(blocks: IBlock[]) {
return octoClient.importFullArchive(blocks)
}
async createImageBlock(parentId: string, file: File, order = 1000): Promise<IBlock | undefined> {
const url = await octoClient.uploadFile(file)
if (!url) {
return undefined
}
2020-10-21 03:28:55 +02:00
const block = new MutableImageBlock()
block.parentId = parentId
2020-10-20 21:50:53 +02:00
block.order = order
block.url = url
await undoManager.perform(
async () => {
await octoClient.insertBlock(block)
},
async () => {
await octoClient.deleteBlock(block.id)
},
2020-10-21 03:47:02 +02:00
'add image',
2020-10-20 21:50:53 +02:00
)
return block
}
async undo() {
await undoManager.undo()
}
undoDescription(): string | undefined {
return undoManager.undoDescription
}
async redo() {
await undoManager.redo()
}
redoDescription(): string | undefined {
return undoManager.redoDescription
}
2020-10-08 18:21:27 +02:00
}
const mutator = new Mutator()
export default mutator
2020-10-20 21:50:53 +02:00
export {mutator}