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

Master no shared borders rar #5452

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
216 changes: 111 additions & 105 deletions src/plugins/core/borders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
AddColumnsRowsCommand,
Border,
BorderDescr,
BorderDescription,
BorderPosition,
CellPosition,
Color,
Expand All @@ -30,7 +29,7 @@ import {
import { CorePlugin } from "../core_plugin";

interface BordersPluginState {
readonly borders: Record<UID, (BorderDescription[] | undefined)[] | undefined>;
readonly borders: Record<UID, ((Border | undefined)[] | undefined)[] | undefined>;
}
/**
* Formatting plugin.
Expand Down Expand Up @@ -105,53 +104,15 @@ export class BordersPlugin extends CorePlugin<BordersPluginState> implements Bor
const elements = [...cmd.elements].sort((a, b) => b - a);
for (const group of groupConsecutive(elements)) {
if (cmd.dimension === "COL") {
if (group[0] >= this.getters.getNumberCols(cmd.sheetId)) {
for (let row: HeaderIndex = 0; row < this.getters.getNumberRows(cmd.sheetId); row++) {
this.history.update(
"borders",
cmd.sheetId,
group[0] + 1,
row,
"vertical",
undefined
);
}
}
if (group[group.length - 1] === 0) {
for (let row: HeaderIndex = 0; row < this.getters.getNumberRows(cmd.sheetId); row++) {
this.history.update("borders", cmd.sheetId, 0, row, "vertical", undefined);
}
}
const zone = this.getters.getColsZone(
cmd.sheetId,
group[group.length - 1] + 1,
group[0]
);
const start = Math.min(group[0], group[group.length - 1]);
const end = Math.max(group[0], group[group.length - 1]);
const zone = this.getters.getColsZone(cmd.sheetId, start, end);
this.clearInsideBorders(cmd.sheetId, [zone]);
this.shiftBordersHorizontally(cmd.sheetId, group[0] + 1, -group.length);
} else {
if (group[0] >= this.getters.getNumberRows(cmd.sheetId)) {
for (let col = 0; col < this.getters.getNumberCols(cmd.sheetId); col++) {
this.history.update(
"borders",
cmd.sheetId,
col,
group[0] + 1,
"horizontal",
undefined
);
}
}
if (group[group.length - 1] === 0) {
for (let col = 0; col < this.getters.getNumberCols(cmd.sheetId); col++) {
this.history.update("borders", cmd.sheetId, col, 0, "horizontal", undefined);
}
}
const zone = this.getters.getRowsZone(
cmd.sheetId,
group[group.length - 1] + 1,
group[0]
);
const start = Math.min(group[0], group[group.length - 1]);
const end = Math.max(group[0], group[group.length - 1]);
const zone = this.getters.getRowsZone(cmd.sheetId, start, end);
this.clearInsideBorders(cmd.sheetId, [zone]);
this.shiftBordersVertically(cmd.sheetId, group[0] + 1, -group.length);
}
Expand Down Expand Up @@ -182,7 +143,7 @@ export class BordersPlugin extends CorePlugin<BordersPluginState> implements Bor
colLeftOfInsertion = cmd.base - 1;
colRightOfInsertion = cmd.base + cmd.quantity;
} else {
this.shiftBordersHorizontally(cmd.sheetId, cmd.base + 1, cmd.quantity, {
this.shiftBordersHorizontally(cmd.sheetId, cmd.base, cmd.quantity, {
moveFirstLeftBorder: false,
});
colLeftOfInsertion = cmd.base;
Expand All @@ -206,7 +167,7 @@ export class BordersPlugin extends CorePlugin<BordersPluginState> implements Bor
rowAboveInsertion = cmd.base - 1;
rowBelowInsertion = cmd.base + cmd.quantity;
} else {
this.shiftBordersVertically(cmd.sheetId, cmd.base + 1, cmd.quantity, {
this.shiftBordersVertically(cmd.sheetId, cmd.base, cmd.quantity, {
moveFirstTopBorder: false,
});
rowAboveInsertion = cmd.base;
Expand All @@ -220,16 +181,8 @@ export class BordersPlugin extends CorePlugin<BordersPluginState> implements Bor
// ---------------------------------------------------------------------------

getCellBorder({ sheetId, col, row }: CellPosition): Border | null {
const border = {
top: this.borders[sheetId]?.[col]?.[row]?.horizontal,
bottom: this.borders[sheetId]?.[col]?.[row + 1]?.horizontal,
left: this.borders[sheetId]?.[col]?.[row]?.vertical,
right: this.borders[sheetId]?.[col + 1]?.[row]?.vertical,
};
if (!border.bottom && !border.left && !border.right && !border.top) {
return null;
}
return border;
const border = this.borders[sheetId]?.[col]?.[row];
return border && Object.keys(border).length > 0 ? border : null;
}

getBordersColors(sheetId: UID): Color[] {
Expand All @@ -238,11 +191,8 @@ export class BordersPlugin extends CorePlugin<BordersPluginState> implements Bor
if (sheetBorders) {
for (const borders of sheetBorders.filter(isDefined)) {
for (const cellBorder of borders) {
if (cellBorder?.horizontal) {
colors.push(cellBorder.horizontal.color);
}
if (cellBorder?.vertical) {
colors.push(cellBorder.vertical.color);
if (cellBorder) {
colors.push(...Object.values(cellBorder).map((b) => b?.color));
}
}
}
Expand Down Expand Up @@ -358,7 +308,7 @@ export class BordersPlugin extends CorePlugin<BordersPluginState> implements Bor
const borders = this.borders[sheetId];
if (!borders) return;
if (delta < 0) {
this.moveBordersOfColumn(sheetId, start, delta, "vertical", {
this.moveBordersOfColumn(sheetId, start, delta, {
destructive: false,
});
}
Expand All @@ -367,9 +317,8 @@ export class BordersPlugin extends CorePlugin<BordersPluginState> implements Bor
.sort((a, b) => (delta < 0 ? a - b : b - a)) // start by the end when moving up
.forEach((col) => {
if ((col === start && moveFirstLeftBorder) || col !== start) {
this.moveBordersOfColumn(sheetId, col, delta, "vertical");
this.moveBordersOfColumn(sheetId, col, delta);
}
this.moveBordersOfColumn(sheetId, col, delta, "horizontal");
});
}

Expand All @@ -388,7 +337,7 @@ export class BordersPlugin extends CorePlugin<BordersPluginState> implements Bor
const borders = this.borders[sheetId];
if (!borders) return;
if (delta < 0) {
this.moveBordersOfRow(sheetId, start, delta, "horizontal", {
this.moveBordersOfRow(sheetId, start, delta, {
destructive: false,
});
}
Expand All @@ -397,9 +346,8 @@ export class BordersPlugin extends CorePlugin<BordersPluginState> implements Bor
.sort((a, b) => (delta < 0 ? a - b : b - a)) // start by the end when moving up
.forEach((row) => {
if ((row === start && moveFirstTopBorder) || row !== start) {
this.moveBordersOfRow(sheetId, row, delta, "horizontal");
this.moveBordersOfRow(sheetId, row, delta);
}
this.moveBordersOfRow(sheetId, row, delta, "vertical");
});
}

Expand All @@ -418,23 +366,21 @@ export class BordersPlugin extends CorePlugin<BordersPluginState> implements Bor
sheetId: UID,
row: HeaderIndex,
delta: number,
borderDirection: "vertical" | "horizontal",
{ destructive }: { destructive: boolean } = { destructive: true }
) {
const borders = this.borders[sheetId];
if (!borders) return;
this.getColumnsWithBorders(sheetId).forEach((col) => {
const targetBorder = borders[col]?.[row + delta]?.[borderDirection];
const movedBorder = borders[col]?.[row]?.[borderDirection];
const targetBorder = borders[col]?.[row + delta];
const movedBorder = borders[col]?.[row];
this.history.update(
"borders",
sheetId,
col,
row + delta,
borderDirection,
destructive ? movedBorder : movedBorder || targetBorder
);
this.history.update("borders", sheetId, col, row, borderDirection, undefined);
this.history.update("borders", sheetId, col, row, undefined);
});
}

Expand All @@ -453,23 +399,23 @@ export class BordersPlugin extends CorePlugin<BordersPluginState> implements Bor
sheetId: UID,
col: HeaderIndex,
delta: number,
borderDirection: "vertical" | "horizontal",
{ destructive }: { destructive: boolean } = { destructive: true }
) {
const borders = this.borders[sheetId];
if (!borders) return;
this.getRowsRange(sheetId).forEach((row) => {
const targetBorder = borders[col + delta]?.[row]?.[borderDirection];
const movedBorder = borders[col]?.[row]?.[borderDirection];
const targetBorder = borders[col + delta]?.[row];
const movedBorder = borders[col]?.[row];
this.history.update(
"borders",
sheetId,
col + delta,
row,
borderDirection,
destructive ? movedBorder : movedBorder || targetBorder
);
this.history.update("borders", sheetId, col, row, borderDirection, undefined);
if (destructive) {
this.history.update("borders", sheetId, col, row, undefined);
}
});
}

Expand All @@ -484,34 +430,78 @@ export class BordersPlugin extends CorePlugin<BordersPluginState> implements Bor
border?: Border,
override = true
) {
if (override || !this.borders?.[sheetId]?.[col]?.[row]?.vertical) {
this.history.update("borders", sheetId, col, row, "vertical", border?.left);
const maxCol = this.getters.getNumberCols(sheetId) - 1;
const maxRow = this.getters.getNumberRows(sheetId) - 1;
if (override || !this.borders[sheetId]?.[col]?.[row]?.left) {
this.history.update("borders", sheetId, col, row, "left", border?.left);
if (
border?.left &&
col > 0 &&
!deepEquals(this.getCellBorder({ sheetId, col: col - 1, row })?.right, border?.left)
) {
this.history.update("borders", sheetId, col - 1, row, "right", undefined);
}
}
if (override || !this.borders?.[sheetId]?.[col]?.[row]?.horizontal) {
this.history.update("borders", sheetId, col, row, "horizontal", border?.top);
if (override || !this.borders[sheetId]?.[col]?.[row]?.top) {
this.history.update("borders", sheetId, col, row, "top", border?.top);
if (
border?.top &&
row > 0 &&
!deepEquals(this.getCellBorder({ sheetId, col, row: row - 1 })?.bottom, border?.top)
) {
this.history.update("borders", sheetId, col, row - 1, "bottom", undefined);
}
}
if (override || !this.borders?.[sheetId]?.[col + 1]?.[row]?.vertical) {
this.history.update("borders", sheetId, col + 1, row, "vertical", border?.right);
if (override || !this.borders[sheetId]?.[col]?.[row]?.right) {
this.history.update("borders", sheetId, col, row, "right", border?.right);
if (
border?.right &&
col < maxCol &&
!deepEquals(this.getCellBorder({ sheetId, col: col + 1, row })?.left, border?.right)
) {
this.history.update("borders", sheetId, col + 1, row, "left", undefined);
}
}
if (override || !this.borders?.[sheetId]?.[col]?.[row + 1]?.horizontal) {
this.history.update("borders", sheetId, col, row + 1, "horizontal", border?.bottom);
if (override || !this.borders[sheetId]?.[col]?.[row]?.bottom) {
this.history.update("borders", sheetId, col, row, "bottom", border?.bottom);
if (
border?.bottom &&
row < maxRow &&
!deepEquals(this.getCellBorder({ sheetId, col, row: row + 1 })?.top, border?.bottom)
) {
this.history.update("borders", sheetId, col, row + 1, "top", undefined);
}
}
}

/**
* Remove the borders of a zone
*/
private clearBorders(sheetId: UID, zones: Zone[]) {
private clearBorders(sheetId: UID, zones: Zone[], eraseBoundaries = false) {
const maxCol = this.getters.getNumberCols(sheetId) - 1;
const maxRow = this.getters.getNumberRows(sheetId) - 1;
for (let zone of recomputeZones(zones)) {
for (let row = zone.top; row <= zone.bottom; row++) {
this.history.update("borders", sheetId, zone.right + 1, row, "vertical", undefined);
if (eraseBoundaries) {
if (zone.left > 0) {
this.history.update("borders", sheetId, zone.left - 1, row, "right", undefined);
}
if (zone.right < maxCol) {
this.history.update("borders", sheetId, zone.right + 1, row, "left", undefined);
}
}
for (let col = zone.left; col <= zone.right; col++) {
this.history.update("borders", sheetId, col, row, undefined);
if (eraseBoundaries) {
if (zone.top > 0) {
this.history.update("borders", sheetId, col, zone.top - 1, "bottom", undefined);
}
if (zone.bottom < maxRow) {
this.history.update("borders", sheetId, col, zone.bottom + 1, "top", undefined);
}
}
}
}
for (let col = zone.left; col <= zone.right; col++) {
this.history.update("borders", sheetId, col, zone.bottom + 1, "horizontal", undefined);
}
}
}

Expand Down Expand Up @@ -549,41 +539,57 @@ export class BordersPlugin extends CorePlugin<BordersPluginState> implements Bor
border: BorderDescr | undefined
) {
if (position === "clear") {
return this.clearBorders(sheetId, zones);
return this.clearBorders(sheetId, zones, true);
}
for (let zone of recomputeZones(zones)) {
if (position === "h" || position === "hv" || position === "all") {
for (let row = zone.top + 1; row <= zone.bottom; row++) {
if (position === "all") {
for (let row = zone.top; row <= zone.bottom; row++) {
for (let col = zone.left; col <= zone.right; col++) {
this.addBorder(sheetId, col, row, { top: border });
this.addBorder(sheetId, col, row, {
top: border,
right: border,
bottom: border,
left: border,
});
}
}
}
if (position === "h" || position === "hv") {
for (let col = zone.left; col <= zone.right; col++) {
this.addBorder(sheetId, col, zone.top, { bottom: border });
for (let row = zone.top + 1; row < zone.bottom; row++) {
this.addBorder(sheetId, col, row, { top: border, bottom: border });
}
this.addBorder(sheetId, col, zone.bottom, { top: border });
}
}
if (position === "v" || position === "hv" || position === "all") {
if (position === "v" || position === "hv") {
for (let row = zone.top; row <= zone.bottom; row++) {
for (let col = zone.left + 1; col <= zone.right; col++) {
this.addBorder(sheetId, col, row, { left: border });
this.addBorder(sheetId, zone.left, row, { right: border });
for (let col = zone.left + 1; col < zone.right; col++) {
this.addBorder(sheetId, col, row, { left: border, right: border });
}
this.addBorder(sheetId, zone.right, row, { left: border });
}
}
if (position === "left" || position === "all" || position === "external") {
if (position === "left" || position === "external") {
for (let row = zone.top; row <= zone.bottom; row++) {
this.addBorder(sheetId, zone.left, row, { left: border });
}
}
if (position === "right" || position === "all" || position === "external") {
if (position === "right" || position === "external") {
for (let row = zone.top; row <= zone.bottom; row++) {
this.addBorder(sheetId, zone.right + 1, row, { left: border });
this.addBorder(sheetId, zone.right, row, { right: border });
}
}
if (position === "top" || position === "all" || position === "external") {
if (position === "top" || position === "external") {
for (let col = zone.left; col <= zone.right; col++) {
this.addBorder(sheetId, col, zone.top, { top: border });
}
}
if (position === "bottom" || position === "all" || position === "external") {
if (position === "bottom" || position === "external") {
for (let col = zone.left; col <= zone.right; col++) {
this.addBorder(sheetId, col, zone.bottom + 1, { top: border });
this.addBorder(sheetId, col, zone.bottom, { bottom: border });
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/stores/grid_renderer_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,8 @@ export class GridRenderer {
nextColIndex = this.getters.getMerge(position)!.right;
previousColIndex = col;
} else {
nextColIndex = this.findNextEmptyCol(col, right, row);
previousColIndex = this.findPreviousEmptyCol(col, left, row);
nextColIndex = box.border?.right ? zone.right : this.findNextEmptyCol(col, right, row);
previousColIndex = box.border?.left ? zone.left : this.findPreviousEmptyCol(col, left, row);
box.isOverflow = true;
}

Expand Down
Loading