mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
feat: support adding multiple library items on canvas (#5116)
This commit is contained in:
parent
cad6097d60
commit
d2cc76e52e
11 changed files with 275 additions and 34 deletions
|
@ -191,12 +191,11 @@ export const loadFromBlob = async (
|
|||
return ret.data;
|
||||
};
|
||||
|
||||
export const loadLibraryFromBlob = async (
|
||||
blob: Blob,
|
||||
export const parseLibraryJSON = (
|
||||
json: string,
|
||||
defaultStatus: LibraryItem["status"] = "unpublished",
|
||||
) => {
|
||||
const contents = await parseFileContents(blob);
|
||||
const data: ImportedLibraryData | undefined = JSON.parse(contents);
|
||||
const data: ImportedLibraryData | undefined = JSON.parse(json);
|
||||
if (!isValidLibrary(data)) {
|
||||
throw new Error("Invalid library");
|
||||
}
|
||||
|
@ -204,6 +203,13 @@ export const loadLibraryFromBlob = async (
|
|||
return restoreLibraryItems(libraryItems, defaultStatus);
|
||||
};
|
||||
|
||||
export const loadLibraryFromBlob = async (
|
||||
blob: Blob,
|
||||
defaultStatus: LibraryItem["status"] = "unpublished",
|
||||
) => {
|
||||
return parseLibraryJSON(await parseFileContents(blob), defaultStatus);
|
||||
};
|
||||
|
||||
export const canvasToBlob = async (
|
||||
canvas: HTMLCanvasElement,
|
||||
): Promise<Blob> => {
|
||||
|
|
|
@ -9,6 +9,8 @@ import { restoreLibraryItems } from "./restore";
|
|||
import type App from "../components/App";
|
||||
import { atom } from "jotai";
|
||||
import { jotaiStore } from "../jotai";
|
||||
import { ExcalidrawElement } from "../element/types";
|
||||
import { getCommonBoundingBox } from "../element/bounds";
|
||||
import { AbortError } from "../errors";
|
||||
import { t } from "../i18n";
|
||||
import { useEffect, useRef } from "react";
|
||||
|
@ -242,6 +244,98 @@ class Library {
|
|||
|
||||
export default Library;
|
||||
|
||||
export const distributeLibraryItemsOnSquareGrid = (
|
||||
libraryItems: LibraryItems,
|
||||
) => {
|
||||
const PADDING = 50;
|
||||
const ITEMS_PER_ROW = Math.ceil(Math.sqrt(libraryItems.length));
|
||||
|
||||
const resElements: ExcalidrawElement[] = [];
|
||||
|
||||
const getMaxHeightPerRow = (row: number) => {
|
||||
const maxHeight = libraryItems
|
||||
.slice(row * ITEMS_PER_ROW, row * ITEMS_PER_ROW + ITEMS_PER_ROW)
|
||||
.reduce((acc, item) => {
|
||||
const { height } = getCommonBoundingBox(item.elements);
|
||||
return Math.max(acc, height);
|
||||
}, 0);
|
||||
return maxHeight;
|
||||
};
|
||||
|
||||
const getMaxWidthPerCol = (targetCol: number) => {
|
||||
let index = 0;
|
||||
let currCol = 0;
|
||||
let maxWidth = 0;
|
||||
for (const item of libraryItems) {
|
||||
if (index % ITEMS_PER_ROW === 0) {
|
||||
currCol = 0;
|
||||
}
|
||||
if (currCol === targetCol) {
|
||||
const { width } = getCommonBoundingBox(item.elements);
|
||||
maxWidth = Math.max(maxWidth, width);
|
||||
}
|
||||
index++;
|
||||
currCol++;
|
||||
}
|
||||
return maxWidth;
|
||||
};
|
||||
|
||||
let colOffsetX = 0;
|
||||
let rowOffsetY = 0;
|
||||
|
||||
let maxHeightCurrRow = 0;
|
||||
let maxWidthCurrCol = 0;
|
||||
|
||||
let index = 0;
|
||||
let col = 0;
|
||||
let row = 0;
|
||||
|
||||
for (const item of libraryItems) {
|
||||
if (index && index % ITEMS_PER_ROW === 0) {
|
||||
rowOffsetY += maxHeightCurrRow + PADDING;
|
||||
colOffsetX = 0;
|
||||
col = 0;
|
||||
row++;
|
||||
}
|
||||
|
||||
if (col === 0) {
|
||||
maxHeightCurrRow = getMaxHeightPerRow(row);
|
||||
}
|
||||
maxWidthCurrCol = getMaxWidthPerCol(col);
|
||||
|
||||
const { minX, minY, width, height } = getCommonBoundingBox(item.elements);
|
||||
const offsetCenterX = (maxWidthCurrCol - width) / 2;
|
||||
const offsetCenterY = (maxHeightCurrRow - height) / 2;
|
||||
resElements.push(
|
||||
// eslint-disable-next-line no-loop-func
|
||||
...item.elements.map((element) => ({
|
||||
...element,
|
||||
x:
|
||||
element.x +
|
||||
// offset for column
|
||||
colOffsetX +
|
||||
// offset to center in given square grid
|
||||
offsetCenterX -
|
||||
// subtract minX so that given item starts at 0 coord
|
||||
minX,
|
||||
y:
|
||||
element.y +
|
||||
// offset for row
|
||||
rowOffsetY +
|
||||
// offset to center in given square grid
|
||||
offsetCenterY -
|
||||
// subtract minY so that given item starts at 0 coord
|
||||
minY,
|
||||
})),
|
||||
);
|
||||
colOffsetX += maxWidthCurrCol + PADDING;
|
||||
index++;
|
||||
col++;
|
||||
}
|
||||
|
||||
return resElements;
|
||||
};
|
||||
|
||||
export const parseLibraryTokensFromUrl = () => {
|
||||
const libraryUrl =
|
||||
// current
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue