refactor(canvas): remove use of any

This commit is contained in:
Elian Doran 2025-06-11 14:01:24 +03:00
parent 9d296dd692
commit bb762cfab6
No known key found for this signature in database
2 changed files with 18 additions and 19 deletions

View File

@ -2,9 +2,10 @@ import TypeWidget from "./type_widget.js";
import server from "../../services/server.js"; import server from "../../services/server.js";
import type FNote from "../../entities/fnote.js"; import type FNote from "../../entities/fnote.js";
import options from "../../services/options.js"; import options from "../../services/options.js";
import type { AppState, BinaryFileData, LibraryItem } from "@excalidraw/excalidraw/types"; import type { LibraryItem } from "@excalidraw/excalidraw/types";
import type { ExcalidrawElement, Theme } from "@excalidraw/excalidraw/element/types"; import type { Theme } from "@excalidraw/excalidraw/element/types";
import type Canvas from "./canvas_el.js"; import type Canvas from "./canvas_el.js";
import { CanvasContent } from "./canvas_el.js";
const TPL = /*html*/` const TPL = /*html*/`
<div class="canvas-widget note-detail-canvas note-detail-printable note-detail"> <div class="canvas-widget note-detail-canvas note-detail-printable note-detail">
@ -46,11 +47,7 @@ const TPL = /*html*/`
</div> </div>
`; `;
interface CanvasContent {
elements: ExcalidrawElement[];
files: BinaryFileData[];
appState: Partial<AppState>;
}
interface AttachmentMetadata { interface AttachmentMetadata {
title: string; title: string;

View File

@ -1,9 +1,15 @@
import "@excalidraw/excalidraw/index.css"; import "@excalidraw/excalidraw/index.css";
import { Excalidraw, getSceneVersion, exportToSvg } from "@excalidraw/excalidraw"; import { Excalidraw, getSceneVersion, exportToSvg } from "@excalidraw/excalidraw";
import { createElement, render } from "preact/compat"; import { createElement, render } from "preact/compat";
import { AppState, BinaryFileData, ExcalidrawImperativeAPI, ExcalidrawProps, LibraryItem, SceneData } from "@excalidraw/excalidraw/types"; import { AppState, BinaryFileData, ExcalidrawImperativeAPI, ExcalidrawProps, LibraryItem } from "@excalidraw/excalidraw/types";
import type { ComponentType } from "preact"; import type { ComponentType } from "preact";
import { Theme } from "@excalidraw/excalidraw/element/types"; import { ExcalidrawElement, NonDeletedExcalidrawElement, Theme } from "@excalidraw/excalidraw/element/types";
export interface CanvasContent {
elements: ExcalidrawElement[];
files: BinaryFileData[];
appState: Partial<AppState>;
}
/** Indicates that it is fresh. excalidraw scene version is always >0 */ /** Indicates that it is fresh. excalidraw scene version is always >0 */
const SCENE_VERSION_INITIAL = -1; const SCENE_VERSION_INITIAL = -1;
@ -85,17 +91,11 @@ export default class Canvas {
}); });
} }
loadData(content: any, theme: any) { loadData(content: CanvasContent, theme: Theme) {
const { elements, files } = content; const { elements, files } = content;
const appState: Partial<AppState> = content.appState ?? {}; const appState: Partial<AppState> = content.appState ?? {};
appState.theme = theme; appState.theme = theme;
const sceneData: SceneData = {
elements,
appState
};
// files are expected in an array when loading. they are stored as a key-index object // files are expected in an array when loading. they are stored as a key-index object
// see example for loading here: // see example for loading here:
// https://github.com/excalidraw/excalidraw/blob/c5a7723185f6ca05e0ceb0b0d45c4e3fbcb81b2a/src/packages/excalidraw/example/App.js#L68 // https://github.com/excalidraw/excalidraw/blob/c5a7723185f6ca05e0ceb0b0d45c4e3fbcb81b2a/src/packages/excalidraw/example/App.js#L68
@ -111,7 +111,10 @@ export default class Canvas {
// Update the scene // Update the scene
// TODO: Fix type of sceneData // TODO: Fix type of sceneData
this.excalidrawApi.updateScene(sceneData as any); this.excalidrawApi.updateScene({
elements,
appState: appState as AppState
});
this.excalidrawApi.addFiles(fileArray); this.excalidrawApi.addFiles(fileArray);
this.excalidrawApi.history.clear(); this.excalidrawApi.history.clear();
} }
@ -135,8 +138,7 @@ export default class Canvas {
const svgString = svg.outerHTML; const svgString = svg.outerHTML;
const activeFiles: Record<string, BinaryFileData> = {}; const activeFiles: Record<string, BinaryFileData> = {};
// TODO: Used any where upstream typings appear to be broken. elements.forEach((element: NonDeletedExcalidrawElement) => {
elements.forEach((element: any) => {
if ("fileId" in element && element.fileId) { if ("fileId" in element && element.fileId) {
activeFiles[element.fileId] = files[element.fileId]; activeFiles[element.fileId] = files[element.fileId];
} }