support for the hover editor

This commit is contained in:
Zsolt Viczian
2022-04-11 22:52:20 +02:00
parent e0266c6731
commit 646b63e3dd
2 changed files with 33 additions and 15 deletions

View File

@@ -67,12 +67,11 @@ import {
getSVGPadding,
getWithBackground,
hasExportTheme,
rotatedDimensions,
scaleLoadedImage,
svgToBase64,
viewportCoordsToSceneCoords,
} from "./utils/Utils";
import { getNewOrAdjacentLeaf } from "./utils/ObsidianUtils";
import { getNewOrAdjacentLeaf, getParentOfClass } from "./utils/ObsidianUtils";
import { splitFolderAndFilename } from "./utils/FileUtils";
import { NewFileActions, Prompt } from "./dialogs/Prompt";
import { ClipboardData } from "@zsviczian/excalidraw/types/clipboard";
@@ -874,18 +873,7 @@ export default class ExcalidrawView extends TextFileView {
private offsetLeft: number = 0;
private offsetTop: number = 0;
private addParentMoveObserver() {
const getParentOfClass = (element: HTMLElement, cssClass: string) => {
let parent = element.parentElement;
while (
parent &&
!(parent instanceof window.HTMLBodyElement) &&
!parent.classList.contains(cssClass)
) {
parent = parent.parentElement;
}
return parent.classList.contains(cssClass) ? parent : null;
};
const parent =
getParentOfClass(this.containerEl, "popover") ??
getParentOfClass(this.containerEl, "workspace-leaf");
@@ -893,6 +881,8 @@ export default class ExcalidrawView extends TextFileView {
return;
}
const inHoverEditorLeaf = parent.classList.contains("popover");
this.offsetLeft = parent.offsetLeft;
this.offsetTop = parent.offsetTop;
const self = this;
@@ -915,7 +905,7 @@ export default class ExcalidrawView extends TextFileView {
this.parentMoveObserver.observe(parent, {
attributeOldValue: true,
attributeFilter: parent.classList.contains("popover")
attributeFilter: inHoverEditorLeaf
? ["data-x", "data-y"]
: ["class", "style"],
});

View File

@@ -5,11 +5,39 @@ import {
import ExcalidrawPlugin from "../main";
import { checkAndCreateFolder, splitFolderAndFilename } from "./FileUtils";
export const getParentOfClass = (element: HTMLElement, cssClass: string):HTMLElement | null => {
let parent = element.parentElement;
while (
parent &&
!(parent instanceof window.HTMLBodyElement) &&
!parent.classList.contains(cssClass)
) {
parent = parent.parentElement;
}
return parent.classList.contains(cssClass) ? parent : null;
};
export const getNewOrAdjacentLeaf = (
plugin: ExcalidrawPlugin,
leaf: WorkspaceLeaf
): WorkspaceLeaf => {
const inHoverEditorLeaf = leaf.view?.containerEl
? getParentOfClass(leaf.view.containerEl, "popover") !== null
: false;
if (inHoverEditorLeaf) {
const mainLeaves = app.workspace.getLayout().main.children.filter((c:any) => c.type === "leaf");
if(mainLeaves.length === 0) {
//@ts-ignore
return leafToUse = app.workspace.createLeafInParent(app.workspace.rootSplit);
}
const targetLeaf = app.workspace.getLeafById(mainLeaves[0].id);
if (plugin.settings.openInAdjacentPane) {
return targetLeaf;
}
return plugin.app.workspace.createLeafBySplit(targetLeaf);
}
if (plugin.settings.openInAdjacentPane) {
let leafToUse = plugin.app.workspace.getAdjacentLeafInDirection(
leaf,