Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | 94x 132x 132x 4279x 94x 10x 10x 10x 1x 10x 94x 18x 18x 40x 114x 18x 94x 37x 37x 852x 61143x 61143x 61143x 60495x 37x 94x 121710x 94x 125x 94x 153x 94x 28x 28x 28x 44x 62x 28x 94x 8x 8x 8x 8x 8x 8x 8x 94x 1x 1x 1x 2x 4x 4x 4x 4x 1x 94x 6x 6x 8x 13x 13x 13x 10x 6x 94x 58x 58x 58x 58x 72x 102x 58x 72x 102x 102x 102x 5x 97x 58x 94x 31x 31x 31x 31x 31x 31x 34x 34x 11x 23x 23x 31x 31x 34x 34x 34x 2x 32x 32x 32x 14x 31x | export interface GridSelection {
x: number;
y: number;
width: number;
height: number;
}
export interface GridOffset {
x: number;
y: number;
}
const createGrid = <T>(
values: readonly T[],
width: number,
height: number,
emptyValue: T,
): T[] => {
const size = width * height;
return Array.from(
{ length: size },
(_, index) => values[index] ?? emptyValue,
);
};
export const normalizeGridSize = <T>(
values: readonly T[],
size: number,
emptyValue: T,
): T[] => {
const newValues = new Array<T>(size).fill(emptyValue);
const copyLength = Math.min(values.length, size);
for (let index = 0; index < copyLength; index++) {
newValues[index] = values[index] ?? emptyValue;
}
return newValues;
};
export const resizeGrid = <T>(
values: readonly T[],
oldWidth: number,
oldHeight: number,
newWidth: number,
newHeight: number,
emptyValue: T,
): T[] => {
const result = new Array<T>(newWidth * newHeight).fill(emptyValue);
for (let y = 0; y < Math.min(oldHeight, newHeight); y++) {
for (let x = 0; x < Math.min(oldWidth, newWidth); x++) {
result[getGridIndex(x, y, newWidth)] =
values[getGridIndex(x, y, oldWidth)] ?? emptyValue;
}
}
return result;
};
export const resizeGridWithOffset = <T>(
values: readonly T[],
oldWidth: number,
oldHeight: number,
newWidth: number,
newHeight: number,
offsetX: number,
offsetY: number,
emptyValue: T,
): T[] => {
const result = new Array<T>(newWidth * newHeight).fill(emptyValue);
for (let oldY = 0; oldY < oldHeight; oldY++) {
for (let oldX = 0; oldX < oldWidth; oldX++) {
const newX = oldX + offsetX;
const newY = oldY + offsetY;
if (newX >= 0 && newX < newWidth && newY >= 0 && newY < newHeight) {
result[getGridIndex(newX, newY, newWidth)] =
values[getGridIndex(oldX, oldY, oldWidth)] ?? emptyValue;
}
}
}
return result;
};
const getGridIndex = (x: number, y: number, width: number): number =>
y * width + x;
const getSelectionBounds = (
selection: GridSelection,
width: number,
height: number,
) => ({
xStart: Math.max(0, selection.x),
yStart: Math.max(0, selection.y),
xEnd: Math.min(width, selection.x + selection.width),
yEnd: Math.min(height, selection.y + selection.height),
});
const isInsideGrid = (
x: number,
y: number,
width: number,
height: number,
): boolean => x >= 0 && y >= 0 && x < width && y < height;
export const clearGridSelection = <T>(
values: readonly T[],
width: number,
height: number,
selection: GridSelection,
emptyValue: T,
): T[] => {
const result = createGrid(values, width, height, emptyValue);
const bounds = getSelectionBounds(selection, width, height);
for (let y = bounds.yStart; y < bounds.yEnd; y++) {
for (let x = bounds.xStart; x < bounds.xEnd; x++) {
result[getGridIndex(x, y, width)] = emptyValue;
}
}
return result;
};
export const clearGridSelectionMasked = <T>(
values: readonly T[],
width: number,
height: number,
selection: GridSelection,
emptyValue: T,
shouldClear: (cellIndex: number) => boolean,
): T[] => {
const result = createGrid(values, width, height, emptyValue);
const bounds = getSelectionBounds(selection, width, height);
for (let y = bounds.yStart; y < bounds.yEnd; y++) {
for (let x = bounds.xStart; x < bounds.xEnd; x++) {
const cellIndex = getGridIndex(x, y, width);
if (shouldClear(cellIndex)) result[cellIndex] = emptyValue;
}
}
return result;
};
export const copyGridSelection = <T>(
values: readonly T[],
width: number,
height: number,
selection: GridSelection,
emptyValue: T,
): T[] => {
const source = createGrid(values, width, height, emptyValue);
const result = new Array<T>(selection.width * selection.height).fill(
emptyValue,
);
for (let y = 0; y < selection.height; y++) {
for (let x = 0; x < selection.width; x++) {
const sourceX = selection.x + x;
const sourceY = selection.y + y;
Eif (isInsideGrid(sourceX, sourceY, width, height)) {
result[getGridIndex(x, y, selection.width)] =
source[getGridIndex(sourceX, sourceY, width)];
}
}
}
return result;
};
export const pasteGridSelection = <T>(
values: readonly T[],
width: number,
height: number,
pasteX: number,
pasteY: number,
pasteWidth: number,
pasteHeight: number,
pasteValues: readonly T[],
emptyValue: T,
): T[] => {
const result = createGrid(values, width, height, emptyValue);
for (let y = 0; y < pasteHeight; y++) {
for (let x = 0; x < pasteWidth; x++) {
const targetX = pasteX + x;
const targetY = pasteY + y;
if (isInsideGrid(targetX, targetY, width, height)) {
result[getGridIndex(targetX, targetY, width)] =
pasteValues[getGridIndex(x, y, pasteWidth)] ?? emptyValue;
}
}
}
return result;
};
export const moveGridSelection = <T>(
values: readonly T[],
width: number,
height: number,
selection: GridSelection,
offset: GridOffset,
emptyValue: T,
): T[] => {
const source = createGrid(values, width, height, emptyValue);
const result = [...source];
const bounds = getSelectionBounds(selection, width, height);
for (let y = bounds.yStart; y < bounds.yEnd; y++) {
for (let x = bounds.xStart; x < bounds.xEnd; x++) {
result[getGridIndex(x, y, width)] = emptyValue;
}
}
for (let y = bounds.yStart; y < bounds.yEnd; y++) {
for (let x = bounds.xStart; x < bounds.xEnd; x++) {
const targetX = x + offset.x;
const targetY = y + offset.y;
if (!isInsideGrid(targetX, targetY, width, height)) {
continue;
}
result[getGridIndex(targetX, targetY, width)] =
source[getGridIndex(x, y, width)];
}
}
return result;
};
export const moveGridSelectionMasked = <T>(
values: readonly T[],
width: number,
height: number,
selection: GridSelection,
offset: GridOffset,
emptyValue: T,
shouldMoveSource: (sourceIndex: number) => boolean,
shouldWriteTarget: (targetIndex: number, sourceIndex: number) => boolean,
): T[] => {
const source = createGrid(values, width, height, emptyValue);
const result = [...source];
const movingSource = new Array<boolean>(width * height).fill(false);
const bounds = getSelectionBounds(selection, width, height);
for (let y = bounds.yStart; y < bounds.yEnd; y++) {
for (let x = bounds.xStart; x < bounds.xEnd; x++) {
const sourceIndex = getGridIndex(x, y, width);
if (!shouldMoveSource(sourceIndex)) {
continue;
}
movingSource[sourceIndex] = true;
result[sourceIndex] = emptyValue;
}
}
for (let y = bounds.yStart; y < bounds.yEnd; y++) {
for (let x = bounds.xStart; x < bounds.xEnd; x++) {
const targetX = x + offset.x;
const targetY = y + offset.y;
if (!isInsideGrid(targetX, targetY, width, height)) {
continue;
}
const sourceIndex = getGridIndex(x, y, width);
const targetIndex = getGridIndex(targetX, targetY, width);
if (
movingSource[sourceIndex] &&
shouldWriteTarget(targetIndex, sourceIndex)
) {
result[targetIndex] = source[sourceIndex];
}
}
}
return result;
};
|