Skip to content

Commit

Permalink
obsidianUtils cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zsviczian committed Jul 1, 2024
1 parent 2f60398 commit 4053873
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 42 deletions.
24 changes: 13 additions & 11 deletions packages/excalidraw/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ import { AnimatedTrail } from "../animated-trail";
import { LaserTrails } from "../laser-trails";
import { withBatchedUpdates, withBatchedUpdatesThrottled } from "../reactUtils";
import { getRenderOpacity } from "../renderer/renderElement";
import { getExcalidrawContentEl, hideFreedrawPenmodeCursor } from "../obsidianUtils";
import { destroyObsidianUtils, getExcalidrawContentEl, hideFreedrawPenmodeCursor, hostPlugin, initializeObsidianUtils } from "../obsidianUtils";
import {
hitElementBoundText,
hitElementBoundingBoxOnly,
Expand Down Expand Up @@ -525,8 +525,6 @@ let currentScrollBars: ScrollBars = { horizontal: null, vertical: null };
let touchTimeout = 0;
let invalidateContextMenu = false;

export let hostPlugin: any; //zsviczian

/**
* Map of youtube embed video states
*/
Expand Down Expand Up @@ -683,7 +681,7 @@ class App extends React.Component<AppProps, AppState> {
};

this.id = nanoid();
hostPlugin = obsidianHostPlugin; //zsviczian
initializeObsidianUtils(obsidianHostPlugin.deref()); //zsviczian
this.library = new Library(this);
this.actionManager = new ActionManager(
this.syncActionResult,
Expand Down Expand Up @@ -2042,7 +2040,7 @@ class App extends React.Component<AppProps, AppState> {
}

//zsviczian - ugly hack
private get OPENAI_KEY():string | null { return hostPlugin?.settings?.openAIAPIToken };
private get OPENAI_KEY():string | null { return hostPlugin.settings.openAIAPIToken };
private set OPENAI_KEY(value:string | null) { return; };

private OPENAI_KEY_IS_PERSISTED: boolean =
Expand Down Expand Up @@ -2582,7 +2580,7 @@ class App extends React.Component<AppProps, AppState> {
this.resizeObserver?.disconnect();
this.unmounted = true;
this.removeEventListeners();
this.scene.destroy();
this.scene.destroy(true); //zsviczian
this.library.destroy();
this.laserTrails.stop();
this.eraserTrail.stop();
Expand Down Expand Up @@ -2614,6 +2612,8 @@ class App extends React.Component<AppProps, AppState> {
this.lastPointerDownEvent = null; //zsviczian
this.lastPointerUpEvent = null; //zsviczian
this.lastPointerMoveEvent = null; //zsviczian
//@ts-ignore
this.actionManager.app = null; //zsviczian
this.actionManager.actions = {} as Record<ActionName, Action>; //zsviczian
this.history.clear(); //zsviczian
//@ts-ignore
Expand All @@ -2633,14 +2633,14 @@ class App extends React.Component<AppProps, AppState> {
this.excalidrawContainerValue = { container: null, id:"unknown" }; //zsviczian
//@ts-ignore
this.props = null; //zsviczian
hostPlugin = null; //zsviczian
this.laserTrails.terminate(); //zsviczian
this.eraserTrail.terminate(); //zsviczian

destroyObsidianUtils(); //zsviczian
/*
Object.keys(this).forEach((key) => {
//@ts-ignore
delete this[key];
});
});*/
}

private onResize = withBatchedUpdates(() => {
Expand Down Expand Up @@ -2697,16 +2697,18 @@ class App extends React.Component<AppProps, AppState> {
addEventListener(document, EVENT.POINTER_UP, this.removePointer), // #3553
addEventListener(document, EVENT.COPY, this.onCopy),
addEventListener(document, EVENT.KEYUP, this.onKeyUp, { passive: true }),
addEventListener(window,"focus", ()=>this.triggerRender(true), { passive: true }), //zsviczian
addEventListener(
document,
EVENT.POINTER_MOVE,
this.updateCurrentCursorPosition,
),
// rerender text elements on font load to fix #637 && #1553
addEventListener(document.fonts, "loadingdone", (event) => {
// zsviczian In Obsidian this is not needed as I manage font load separately
/*addEventListener(document.fonts, "loadingdone", (event) => {
const loadedFontFaces = (event as FontFaceSetLoadEvent).fontfaces;
this.fonts.onFontsLoaded(loadedFontFaces);
}),
}),*/
// Safari-only desktop pinch zoom
addEventListener(
document,
Expand Down
43 changes: 15 additions & 28 deletions packages/excalidraw/obsidianUtils.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,37 @@
import { DOMElement } from "react";

//zsviczian, my dirty little secrets. These are hacks I am not proud of...
let OBSIDIAN_PLUGIN:any;
let EXCALIDRAW_CONFIG:any;
const setObsidianPlugin = () => {
//@ts-ignore
OBSIDIAN_PLUGIN = app?.plugins?.plugins?.["obsidian-excalidraw-plugin"];
EXCALIDRAW_CONFIG = OBSIDIAN_PLUGIN?.excalidrawConfig;
export let hostPlugin: any = null;

export const initializeObsidianUtils = (obsidianPlugin: any) => {
hostPlugin = obsidianPlugin;
}

export const destroyObsidianUtils = () => {
hostPlugin = null;
}

const getExcalidrawConfig = () => hostPlugin.excalidrawConfig;

export const getAreaLimit = () => {
if(!OBSIDIAN_PLUGIN) {
setObsidianPlugin();
}
return EXCALIDRAW_CONFIG?.areaLimit ?? 16777216;
return getExcalidrawConfig().areaLimit ?? 16777216;
}

export const getWidthHeightLimit = () => {
if(!OBSIDIAN_PLUGIN) {
setObsidianPlugin();
}
return EXCALIDRAW_CONFIG?.widthHeightLimit ?? 32767;
return getExcalidrawConfig().widthHeightLimit ?? 32767;
}

export const isExcaliBrainView = () => {
if(!OBSIDIAN_PLUGIN) {
setObsidianPlugin();
}
const excalidrawView = OBSIDIAN_PLUGIN?.activeExcalidrawView;
const excalidrawView = hostPlugin.activeExcalidrawView;
if(!excalidrawView) return false;
return excalidrawView.linksAlwaysOpenInANewPane && excalidrawView.allowFrameButtonsInViewMode;
}

export const getExcalidrawContentEl = ():HTMLElement => {
if(!OBSIDIAN_PLUGIN) {
setObsidianPlugin();
}
const excalidrawView = OBSIDIAN_PLUGIN?.activeExcalidrawView;
const excalidrawView = hostPlugin.activeExcalidrawView;
if(!excalidrawView) return document.body;
return excalidrawView.contentEl as HTMLElement;
}

export const hideFreedrawPenmodeCursor = () => {
if(!OBSIDIAN_PLUGIN) {
setObsidianPlugin();
}
if(!OBSIDIAN_PLUGIN) return true;
return !OBSIDIAN_PLUGIN.settings.penModeCrosshairVisible;
return !hostPlugin.settings.penModeCrosshairVisible;
}
4 changes: 2 additions & 2 deletions packages/excalidraw/scene/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class Scene {
};
}

destroy() {
destroy(force: boolean = false) { //zsviczian
this.elements = [];
this.nonDeletedElements = [];
this.nonDeletedFramesLikes = [];
Expand All @@ -332,7 +332,7 @@ class Scene {
this.selectedElementsCache.cache.clear();

Scene.sceneMapById.forEach((scene, elementKey) => {
if (scene === this) {
if (force || (scene === this)) { //zsviczian
Scene.sceneMapById.delete(elementKey);
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/excalidraw/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ export interface ExcalidrawProps {
onClose: (callback?: () => void) => void,
) => JSX.Element | null;
aiEnabled?: boolean;
obsidianHostPlugin?: any; //zsviczian
obsidianHostPlugin: WeakRef<any>; //zsviczian
}

export type SceneData = {
Expand Down

0 comments on commit 4053873

Please sign in to comment.