Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(draw): fix remove column and row error #899

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stupid-carrots-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@plait/draw': patch
---

fix remove column and row error
5 changes: 3 additions & 2 deletions packages/draw/src/plugins/with-draw-fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export const withDrawFragment = (baseBoard: PlaitBoard) => {

const boundLineElements = [
...getBoundedLineElements(board, geometryElements),
...getBoundedLineElements(board, imageElements)
...getBoundedLineElements(board, imageElements),
...getBoundedLineElements(board, tableElements)
].filter(line => !lineElements.includes(line));
data.push(
...[
Expand Down Expand Up @@ -125,4 +126,4 @@ export const getBoundedLineElements = (board: PlaitBoard, plaitShapes: PlaitShap
return lines.filter(line =>
plaitShapes.find(shape => PlaitLine.isBoundElementOfSource(line, shape) || PlaitLine.isBoundElementOfTarget(line, shape))
);
};
};
44 changes: 22 additions & 22 deletions packages/draw/src/transforms/swimlane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ import { idCreator, PlaitBoard, Point, RectangleClient, Transforms } from '@plai
import { PlaitDrawElement, PlaitSwimlane, SwimlaneSymbols } from '../interfaces';
import { PlaitTableCell } from '../interfaces/table';
import { getCellWithPoints } from '../utils/table';
import { Alignment } from '@plait/text';

export const addSwimlaneRow = (board: PlaitBoard, swimlane: PlaitSwimlane, index: number) => {
if (PlaitDrawElement.isSwimlane(swimlane) && swimlane.shape === SwimlaneSymbols.swimlaneHorizontal) {
const newRows = [...swimlane.rows];
const newRowId = idCreator();
newRows.splice(index, 0, { id: newRowId });

let newCells = createNewRowCells(swimlane, newRowId);
newCells.shift();
newCells = [...swimlane.cells, ...newCells];

const newCells = [...swimlane.cells, ...createNewSwimlaneCells(swimlane, newRowId, 'column')];
const lastCellPoints = getCellWithPoints(board, swimlane, swimlane.cells[swimlane.cells.length - 1].id).points;
const lastRowHeight = RectangleClient.getRectangleByPoints(lastCellPoints).height;
const newPoints: Point[] = [swimlane.points[0], [swimlane.points[1][0], swimlane.points[1][1] + lastRowHeight]];
Expand All @@ -27,10 +25,7 @@ export const addSwimlaneColumn = (board: PlaitBoard, swimlane: PlaitSwimlane, in
const newColumnId = idCreator();
newColumns.splice(index, 0, { id: newColumnId });

let newCells = createNewColumnCells(swimlane, newColumnId);
newCells.shift();
newCells = [...swimlane.cells, ...newCells];

const newCells = [...swimlane.cells, ...createNewSwimlaneCells(swimlane, newColumnId, 'row')];
const lastCellPoints = getCellWithPoints(board, swimlane, swimlane.cells[swimlane.cells.length - 1].id).points;
const lastColumnWidth = RectangleClient.getRectangleByPoints(lastCellPoints).width;
const newPoints: Point[] = [swimlane.points[0], [swimlane.points[1][0] + lastColumnWidth, swimlane.points[1][1]]];
Expand All @@ -51,7 +46,8 @@ export const removeSwimlaneRow = (board: PlaitBoard, swimlane: PlaitSwimlane, in
const newCells = swimlane.cells.filter(item => item.rowId !== removeRow.id);
let removeRowHeight = removeRow.height;
if (!removeRowHeight) {
const cellPoints = getCellWithPoints(board, swimlane, swimlane.cells[index].id).points;
const rowCell = swimlane.cells.find(item => item.rowId === removeRow.id)!;
const cellPoints = getCellWithPoints(board, swimlane, rowCell.id).points;
removeRowHeight = RectangleClient.getRectangleByPoints(cellPoints).height;
}
const newPoints: Point[] = [swimlane.points[0], [swimlane.points[1][0], swimlane.points[1][1] - removeRowHeight]];
Expand All @@ -72,7 +68,8 @@ export const removeSwimlaneColumn = (board: PlaitBoard, swimlane: PlaitSwimlane,
const newCells = swimlane.cells.filter(item => item.columnId !== removeColumn.id);
let removeColumnWidth = removeColumn.width;
if (!removeColumnWidth) {
const cellPoints = getCellWithPoints(board, swimlane, swimlane.cells[index].id).points;
const columnCell = swimlane.cells.find(item => item.columnId === removeColumn.id)!;
const cellPoints = getCellWithPoints(board, swimlane, columnCell.id).points;
removeColumnWidth = RectangleClient.getRectangleByPoints(cellPoints).width;
}
const newPoints: Point[] = [swimlane.points[0], [swimlane.points[1][0] - removeColumnWidth, swimlane.points[1][1]]];
Expand All @@ -81,20 +78,23 @@ export const removeSwimlaneColumn = (board: PlaitBoard, swimlane: PlaitSwimlane,
}
};

const createNewColumnCells = (swimlane: PlaitSwimlane, newColumnId: string): PlaitTableCell[] => {
return swimlane.rows.map(row => ({
const createNewSwimlaneCells = (swimlane: PlaitSwimlane, newId: string, type: 'row' | 'column'): PlaitTableCell[] => {
const cells: PlaitTableCell[] = swimlane[`${type}s`].map(item => ({
id: idCreator(),
rowId: row.id,
columnId: newColumnId
}));
};

const createNewRowCells = (swimlane: PlaitSwimlane, newRowId: string): PlaitTableCell[] => {
return swimlane.columns.map(column => ({
id: idCreator(),
rowId: newRowId,
columnId: column.id
rowId: type === 'row' ? item.id : newId,
columnId: type === 'row' ? newId : item.id
}));
cells.shift();
cells[0] = {
...cells[0],
text: {
children: [{ text: 'Lane' }],
align: Alignment.center,
direction: type === 'row' ? undefined : 'vertical'
},
textHeight: 20
};
return cells;
};

const updateSwimlane = (
Expand Down
8 changes: 7 additions & 1 deletion packages/draw/src/utils/swimlane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ export function buildSwimlaneTable(element: PlaitSwimlane) {
};
}
if (item.text && item.textHeight && !item.text.direction) {
item.text.direction = 'vertical';
item = {
...item,
text: {
...item.text,
direction: 'vertical'
}
};
}
return item;
});
Expand Down
Loading