gem-panel
Version:
A custom element <gem-panel>, let you easily create layout similar to Adobe After Effects.
100 lines • 2.99 kB
JavaScript
export default function add(a, b) {
return a + b;
}
export function getNewFocusElementIndex(arr, currentFocusIndex, deleteIndex) {
if (arr.length === 1)
return -1;
if (deleteIndex >= currentFocusIndex) {
if (currentFocusIndex > arr.length - 2)
return arr.length - 2;
return currentFocusIndex;
}
return currentFocusIndex - 1;
}
export function isEqualArray(a, b) {
if (a.length !== b.length)
return false;
return a.every((e, index) => e === b[index]);
}
export function distance(x, y) {
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
export function removeItem(arr, item) {
const index = arr.findIndex((e) => e === item);
arr.splice(index, 1);
}
export function swapPosition(arr, item2, item1) {
const index1 = arr.findIndex((e) => e === item1);
const index2 = arr.findIndex((e) => e === item2);
[arr[index1], arr[index2]] = [item2, item1];
}
export function detectPosition([ox, oy, w, h], [px, py], border) {
const [x, y] = [px - ox, py - oy];
if (x >= y && y >= 0 && y <= border && x <= w - y) {
return 'top';
}
else if (x >= w - border && x <= w && y >= w - x && y <= h - (w - x)) {
return 'right';
}
else if (y >= h - border && y <= h && x >= h - y && x <= w - (h - y)) {
return 'bottom';
}
else if (x >= 0 && x <= border && y >= x && y <= h - x) {
return 'left';
}
return 'center';
}
export function findLimintPosition(arr, limit) {
let total = 0;
for (let index = 0; index < arr.length; index++) {
total += arr[index];
if (total > limit) {
return { index, margin: total - limit };
}
}
return { index: arr.length, margin: total - limit };
}
export function isOutside(rect, target) {
if (target.x > rect.x + rect.width ||
target.y > rect.y + rect.height ||
rect.x > target.x + target.width ||
rect.y > target.y + target.height) {
return true;
}
return false;
}
export function keyBy(arr, key) {
const obj = {};
for (const ele of arr) {
obj[ele[key]] = ele;
}
return obj;
}
export function exclude(obj, key, arr) {
for (const ele of arr) {
delete obj[ele[key]];
}
return obj;
}
// only read and modify element
export function getFlipMatrix(matrix) {
return new Proxy(matrix, {
get(_, p) {
return new Proxy({}, {
get(_, c) {
if (c in Array.prototype) {
const arr = matrix.map((row) => row[p]);
const v = arr[c];
return typeof v === 'function' ? v.bind(arr) : arr[c];
}
return matrix[c][p];
},
set(_, c, v) {
matrix[c][p] = v;
return true;
},
});
},
});
}
//# sourceMappingURL=utils.js.map