Skip to content

Commit

Permalink
Fixed: toggleImageAnchoring
Browse files Browse the repository at this point in the history
  • Loading branch information
zsviczian committed Dec 23, 2024
1 parent fd1604c commit d9534fc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"license": "MIT",
"dependencies": {
"@popperjs/core": "^2.11.8",
"@zsviczian/excalidraw": "0.17.6-22",
"@zsviczian/excalidraw": "0.17.6-24",
"chroma-js": "^2.4.2",
"clsx": "^2.0.0",
"@zsviczian/colormaster": "^1.2.2",
Expand Down
35 changes: 34 additions & 1 deletion src/utils/excalidrawViewUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import { App, Modal, Notice, TFile, WorkspaceLeaf } from "obsidian";
import { ExcalidrawAutomate } from "src/shared/ExcalidrawAutomate";
import { REGEX_LINK, REG_LINKINDEX_HYPERLINK, getExcalidrawMarkdownHeaderSection, REGEX_TAGS } from "../shared/ExcalidrawData";
import ExcalidrawView from "src/view/ExcalidrawView";
import { ExcalidrawElement, ExcalidrawFrameElement } from "@zsviczian/excalidraw/types/excalidraw/element/types";
import { ExcalidrawElement, ExcalidrawFrameElement, ExcalidrawImageElement } from "@zsviczian/excalidraw/types/excalidraw/element/types";
import { getEmbeddedFilenameParts, getLinkParts, isImagePartRef } from "./utils";
import { cleanSectionHeading } from "./obsidianUtils";
import { getEA } from "src/core";
import { ExcalidrawImperativeAPI } from "@zsviczian/excalidraw/types/excalidraw/types";
import { EmbeddableMDCustomProps } from "src/shared/Dialogs/EmbeddableSettings";
import { nanoid } from "nanoid";
import { t } from "src/lang/helpers";
import { Mutable } from "@zsviczian/excalidraw/types/excalidraw/utility-types";
import { EmbeddedFile } from "src/shared/EmbeddedFileLoader";

export async function insertImageToView(
ea: ExcalidrawAutomate,
Expand Down Expand Up @@ -431,4 +433,35 @@ export function displayFontMessage(app: App) {
}

modal.open();
}

export async function toggleImageAnchoring(
el: ExcalidrawImageElement,
view: ExcalidrawView,
shouldAnchor: boolean,
ef: EmbeddedFile,
) {
const ea = getEA(view) as ExcalidrawAutomate;
let imgEl = view.getViewElements().find((x:ExcalidrawElement)=>x.id === el.id) as Mutable<ExcalidrawImageElement>;
if(!imgEl) {
ea.destroy();
return;
}
ea.copyViewElementsToEAforEditing([imgEl]);
imgEl = ea.getElements()[0] as Mutable<ExcalidrawImageElement>;
if(!imgEl.customData) {
imgEl.customData = {};
}
imgEl.customData.isAnchored = shouldAnchor;
if(shouldAnchor) {
const {height, width} = ef.size;
const dX = width - imgEl.width;
const dY = height - imgEl.height;
imgEl.height = height;
imgEl.width = width;
imgEl.x -= dX/2;
imgEl.y -= dY/2;
}
await ea.addElementsToView(false, false);
ea.destroy();
}
12 changes: 10 additions & 2 deletions src/view/ExcalidrawView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ import { getEA } from "src/core"
import { anyModifierKeysPressed, emulateKeysForLinkClick, isWinALTorMacOPT, isWinCTRLorMacCMD, isWinMETAorMacCTRL, isSHIFT, linkClickModifierType, localFileDragModifierType, ModifierKeys, modifierKeyTooltipMessages } from "../utils/modifierkeyHelper";
import { setDynamicStyle } from "../utils/dynamicStyling";
import { CustomEmbeddable, renderWebView } from "./components/CustomEmbeddable";
import { addBackOfTheNoteCard, getExcalidrawFileForwardLinks, getFrameBasedOnFrameNameOrId, getLinkTextFromLink, insertEmbeddableToView, insertImageToView, isTextImageTransclusion, openExternalLink, parseObsidianLink, renderContextMenuAction, tmpBruteForceCleanup } from "../utils/excalidrawViewUtils";
import { addBackOfTheNoteCard, getExcalidrawFileForwardLinks, getFrameBasedOnFrameNameOrId, getLinkTextFromLink, insertEmbeddableToView, insertImageToView, isTextImageTransclusion, openExternalLink, parseObsidianLink, renderContextMenuAction, tmpBruteForceCleanup, toggleImageAnchoring } from "../utils/excalidrawViewUtils";
import { imageCache } from "../shared/ImageCache";
import { CanvasNodeFactory, ObsidianCanvasNode } from "./managers/CanvasNodeFactory";
import { EmbeddableMenu } from "./components/menu/EmbeddableActionsMenu";
Expand Down Expand Up @@ -973,9 +973,14 @@ export default class ExcalidrawView extends TextFileView implements HoverParent{
if (!link || ef.linkParts.original === link) {
return;
}
const originalAnchor = Boolean(ef.linkParts.original.endsWith("|100%"));
const nextAnchor = Boolean(link.endsWith("|100%"));
ef.resetImage(this.file.path, link);
this.excalidrawData.setFile(fileId, ef);
this.setDirty(2);
if(originalAnchor !== nextAnchor) {
await toggleImageAnchoring(el, this, nextAnchor, ef)
}
await this.save(false);
await sleep(100);
if(!this.plugin.isExcalidrawFile(ef.file) && !link.endsWith("|100%")) {
Expand Down Expand Up @@ -2433,7 +2438,10 @@ export default class ExcalidrawView extends TextFileView implements HoverParent{
(files: FileData[], isDark: boolean, final:boolean = true) => {
if (!files) {
return;
}
}
if(!this.file || !this.excalidrawAPI) {
return; //The view was closed in the mean time
}
addFiles(files, this, isDark);
if(!final) return;
this.activeLoader = null;
Expand Down

0 comments on commit d9534fc

Please sign in to comment.