Prefer arrow functions and callbacks (#1210)

This commit is contained in:
Lipis 2020-05-20 16:21:37 +03:00 committed by GitHub
parent 33fe223b5d
commit c427aa3cce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
64 changed files with 784 additions and 847 deletions

View file

@ -31,10 +31,10 @@ export interface ExcalidrawElementWithCanvas {
canvasOffsetY: number;
}
function generateElementCanvas(
const generateElementCanvas = (
element: NonDeletedExcalidrawElement,
zoom: number,
): ExcalidrawElementWithCanvas {
): ExcalidrawElementWithCanvas => {
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d")!;
@ -75,13 +75,13 @@ function generateElementCanvas(
1 / (window.devicePixelRatio * zoom),
);
return { element, canvas, canvasZoom: zoom, canvasOffsetX, canvasOffsetY };
}
};
function drawElementOnCanvas(
const drawElementOnCanvas = (
element: NonDeletedExcalidrawElement,
rc: RoughCanvas,
context: CanvasRenderingContext2D,
) {
) => {
context.globalAlpha = element.opacity / 100;
switch (element.type) {
case "rectangle":
@ -132,7 +132,7 @@ function drawElementOnCanvas(
}
}
context.globalAlpha = 1;
}
};
const elementWithCanvasCache = new WeakMap<
ExcalidrawElement,
@ -144,15 +144,13 @@ const shapeCache = new WeakMap<
Drawable | Drawable[] | null
>();
export function getShapeForElement(element: ExcalidrawElement) {
return shapeCache.get(element);
}
export const getShapeForElement = (element: ExcalidrawElement) =>
shapeCache.get(element);
export function invalidateShapeForElement(element: ExcalidrawElement) {
export const invalidateShapeForElement = (element: ExcalidrawElement) =>
shapeCache.delete(element);
}
export function generateRoughOptions(element: ExcalidrawElement): Options {
export const generateRoughOptions = (element: ExcalidrawElement): Options => {
const options: Options = {
seed: element.seed,
strokeLineDash:
@ -214,13 +212,13 @@ export function generateRoughOptions(element: ExcalidrawElement): Options {
throw new Error(`Unimplemented type ${element.type}`);
}
}
}
};
function generateElement(
const generateElement = (
element: NonDeletedExcalidrawElement,
generator: RoughGenerator,
sceneState?: SceneState,
) {
) => {
let shape = shapeCache.get(element) || null;
if (!shape) {
elementWithCanvasCache.delete(element);
@ -319,14 +317,14 @@ function generateElement(
return elementWithCanvas;
}
return prevElementWithCanvas;
}
};
function drawElementFromCanvas(
const drawElementFromCanvas = (
elementWithCanvas: ExcalidrawElementWithCanvas,
rc: RoughCanvas,
context: CanvasRenderingContext2D,
sceneState: SceneState,
) {
) => {
const element = elementWithCanvas.element;
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
const cx = ((x1 + x2) / 2 + sceneState.scrollX) * window.devicePixelRatio;
@ -346,15 +344,15 @@ function drawElementFromCanvas(
context.rotate(-element.angle);
context.translate(-cx, -cy);
context.scale(window.devicePixelRatio, window.devicePixelRatio);
}
};
export function renderElement(
export const renderElement = (
element: NonDeletedExcalidrawElement,
rc: RoughCanvas,
context: CanvasRenderingContext2D,
renderOptimizations: boolean,
sceneState: SceneState,
) {
) => {
const generator = rc.generator;
switch (element.type) {
case "selection": {
@ -404,15 +402,15 @@ export function renderElement(
throw new Error(`Unimplemented type ${element.type}`);
}
}
}
};
export function renderElementToSvg(
export const renderElementToSvg = (
element: NonDeletedExcalidrawElement,
rsvg: RoughSVG,
svgRoot: SVGElement,
offsetX?: number,
offsetY?: number,
) {
) => {
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
const cx = (x2 - x1) / 2 - (element.x - x1);
const cy = (y2 - y1) / 2 - (element.y - y1);
@ -528,4 +526,4 @@ export function renderElementToSvg(
}
}
}
}
};