mark elements params as readonly & remove unnecessary copying

This commit is contained in:
dwelle 2020-01-09 11:59:59 +01:00
parent 88006ab426
commit 332fc518b7
9 changed files with 41 additions and 34 deletions

View file

@ -2,7 +2,7 @@ import { ExcalidrawElement } from "../element/types";
import { hitTest } from "../element/collision";
import { getElementAbsoluteCoords } from "../element";
export const hasBackground = (elements: ExcalidrawElement[]) =>
export const hasBackground = (elements: readonly ExcalidrawElement[]) =>
elements.some(
element =>
element.isSelected &&
@ -11,7 +11,7 @@ export const hasBackground = (elements: ExcalidrawElement[]) =>
element.type === "diamond")
);
export const hasStroke = (elements: ExcalidrawElement[]) =>
export const hasStroke = (elements: readonly ExcalidrawElement[]) =>
elements.some(
element =>
element.isSelected &&
@ -21,11 +21,11 @@ export const hasStroke = (elements: ExcalidrawElement[]) =>
element.type === "arrow")
);
export const hasText = (elements: ExcalidrawElement[]) =>
export const hasText = (elements: readonly ExcalidrawElement[]) =>
elements.some(element => element.isSelected && element.type === "text");
export function getElementAtPosition(
elements: ExcalidrawElement[],
elements: readonly ExcalidrawElement[],
x: number,
y: number
) {
@ -42,7 +42,7 @@ export function getElementAtPosition(
}
export function getElementContainingPosition(
elements: ExcalidrawElement[],
elements: readonly ExcalidrawElement[],
x: number,
y: number
) {

View file

@ -1,6 +1,6 @@
import { ExcalidrawElement } from "../element/types";
export const createScene = () => {
const elements = Array.of<ExcalidrawElement>();
const elements: readonly ExcalidrawElement[] = [];
return { elements };
};

View file

@ -23,11 +23,14 @@ function saveFile(name: string, data: string) {
}
interface DataState {
elements: ExcalidrawElement[];
elements: readonly ExcalidrawElement[];
appState: any;
}
export function saveAsJSON(elements: ExcalidrawElement[], name: string) {
export function saveAsJSON(
elements: readonly ExcalidrawElement[],
name: string
) {
const serialized = JSON.stringify({
version: 1,
source: window.location.origin,
@ -74,7 +77,7 @@ export function loadFromJSON() {
}
export function exportAsPNG(
elements: ExcalidrawElement[],
elements: readonly ExcalidrawElement[],
canvas: HTMLCanvasElement,
{
exportBackground,
@ -140,7 +143,7 @@ export function exportAsPNG(
}
function restore(
savedElements: ExcalidrawElement[],
savedElements: readonly ExcalidrawElement[],
savedState: any
): DataState {
return {
@ -185,7 +188,7 @@ export function restoreFromLocalStorage() {
}
export function saveToLocalStorage(
elements: ExcalidrawElement[],
elements: readonly ExcalidrawElement[],
state: AppState
) {
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(elements));

View file

@ -7,7 +7,7 @@ export const SCROLLBAR_WIDTH = 6;
export const SCROLLBAR_COLOR = "rgba(0,0,0,0.3)";
export function getScrollBars(
elements: ExcalidrawElement[],
elements: readonly ExcalidrawElement[],
canvasWidth: number,
canvasHeight: number,
scrollX: number,
@ -76,7 +76,7 @@ export function getScrollBars(
}
export function isOverScrollBars(
elements: ExcalidrawElement[],
elements: readonly ExcalidrawElement[],
x: number,
y: number,
canvasWidth: number,

View file

@ -2,7 +2,7 @@ import { ExcalidrawElement } from "../element/types";
import { getElementAbsoluteCoords } from "../element";
export function setSelection(
elements: ExcalidrawElement[],
elements: readonly ExcalidrawElement[],
selection: ExcalidrawElement
) {
const [
@ -29,7 +29,7 @@ export function setSelection(
return elements;
}
export function clearSelection(elements: ExcalidrawElement[]) {
export function clearSelection(elements: readonly ExcalidrawElement[]) {
elements.forEach(element => {
element.isSelected = false;
});
@ -37,11 +37,11 @@ export function clearSelection(elements: ExcalidrawElement[]) {
return elements;
}
export function deleteSelectedElements(elements: ExcalidrawElement[]) {
export function deleteSelectedElements(elements: readonly ExcalidrawElement[]) {
return elements.filter(el => !el.isSelected);
}
export function getSelectedIndices(elements: ExcalidrawElement[]) {
export function getSelectedIndices(elements: readonly ExcalidrawElement[]) {
const selectedIndices: number[] = [];
elements.forEach((element, index) => {
if (element.isSelected) {
@ -51,11 +51,11 @@ export function getSelectedIndices(elements: ExcalidrawElement[]) {
return selectedIndices;
}
export const someElementIsSelected = (elements: ExcalidrawElement[]) =>
export const someElementIsSelected = (elements: readonly ExcalidrawElement[]) =>
elements.some(element => element.isSelected);
export function getSelectedAttribute<T>(
elements: ExcalidrawElement[],
elements: readonly ExcalidrawElement[],
getAttribute: (element: ExcalidrawElement) => T
): T | null {
const attributes = Array.from(