Compare commits

...
Author SHA1 Message Date
zsviczian 2d39586865 implemented toggleViewMode 2026-09-28 19:50:45 +02:00
zsviczian 3f98be4500 Merge pull request #2960 from zsviczian/kplex-support
feat: add view-scoped integration APIs for K-Plex
2026-09-28 12:13:45 +02:00
10 changed files with 142 additions and 24 deletions

No files matched your search

Binary file not shown.
+6 -1
View File
@@ -1174,7 +1174,11 @@ export default class ExcalidrawPlugin extends Plugin {
return file.path;
}
public async setMarkdownView(leaf: WorkspaceLeaf, eState?: ViewStateResult) {
public async setMarkdownView(
leaf: WorkspaceLeaf,
eState?: ViewStateResult,
active?: boolean,
) {
const state = leaf.view.getState();
//Note v2.0.19: I have absolutely no idea why I thought this is necessary. Removing this.
@@ -1189,6 +1193,7 @@ export default class ExcalidrawPlugin extends Plugin {
type: "markdown",
state,
popstate: true,
...(active === undefined ? {} : { active }),
} as ViewState,
eState ? eState : { focus: true },
);
+2 -9
View File
@@ -12,7 +12,6 @@ import {
View,
} from "obsidian";
import {
VIEW_TYPE_EXCALIDRAW,
ICON_NAME,
IMAGE_TYPES,
DEVICE,
@@ -2508,20 +2507,14 @@ export class CommandManager {
const excalidrawView =
this.app.workspace.getActiveViewOfType(ExcalidrawView);
if (excalidrawView) {
void excalidrawView.openAsMarkdown();
void this.plugin.ea.toggleViewMode(excalidrawView);
return;
}
const markdownView =
this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownView && fileIsExcalidraw) {
void (async () => {
await markdownView.save();
const activeLeaf = markdownView.leaf;
this.plugin.excalidrawFileModes[activeLeaf.id || activeFile.path] =
VIEW_TYPE_EXCALIDRAW;
await setExcalidrawView(activeLeaf);
})();
void this.plugin.ea.toggleViewMode(markdownView);
}
},
});
+6
View File
@@ -37,6 +37,12 @@ I build this plugin as a labor of love. Curious about the philosophy behind it?
- Selection rectangles in popout windows are no longer positioned incorrectly when displays use different DPI settings. [#2940](${URLs.GITHUB_COM_ZSVICZIAN_OBSIDIAN_EXCALIDRAW_PLUGIN_ISSUES}/2940)
## New/Fixed in Excalidraw Automate
- Added a view-targeted representation toggle for integrations. It switches the supplied live Excalidraw-backed view directly between Excalidraw and Markdown without relying on the workspace active leaf or command routing:
\`\`\`ts
public async toggleViewMode(view: View): Promise<View | null>;
\`\`\`
- Integrations can now register a link handler on one target view and receive Excalidraw's resolved pane action without replacing the global hook or copying modifier settings. A new zoom helper fits the complete target scene:
\`\`\`ts
+8
View File
@@ -987,6 +987,14 @@ export const EXCALIDRAW_AUTOMATE_INFO: SuggesterInfo[] = [
desc: "Returns a new instance of ExcalidrawAutomate.",
after: "",
},
{
field: "toggleViewMode",
code: "public async toggleViewMode(view: View): Promise<View | null>",
desc:
"Toggles the supplied live Excalidraw-backed view between Excalidraw and Markdown without relying on the active workspace leaf. " +
"The input may be an ExcalidrawView or a MarkdownView whose file is an Excalidraw drawing. Returns the replacement view after a successful toggle, or null when the supplied view is not eligible.",
after: "(view);",
},
{
field: "getAttachmentFilepath",
code: "async getAttachmentFilepath(filename: string): Promise<string>",
+62
View File
@@ -23,6 +23,7 @@ import {
import { ColorMap, MimeType } from "../types/embeddedFileLoaderTypes";
import {
Editor,
MarkdownView,
Notice,
OpenViewState,
RequestUrlResponse,
@@ -50,6 +51,7 @@ import {
refreshTextDimensions,
getFontFamilyString,
convertToExcalidrawElements,
VIEW_TYPE_EXCALIDRAW,
} from "src/constants/constants";
import {
blobToBase64,
@@ -79,6 +81,7 @@ import {
isObsidianThemeDark,
mergeMarkdownFiles,
openLeaf,
setExcalidrawView,
} from "src/utils/obsidianUtils";
import { getAttachmentsFolderAndFilePath } from "src/utils/pathUtils";
import {
@@ -5013,6 +5016,65 @@ export class ExcalidrawAutomate {
return view instanceof ExcalidrawView;
}
/**
* Toggles an Excalidraw-backed view between the native Excalidraw and Markdown representations.
*
* The supplied view is the authority for the transition; this method does not depend on the
* workspace's currently active leaf or the command palette. Markdown views are accepted only
* when their file is an Excalidraw file. Excalidraw compatibility-mode views are left unchanged.
*
* @param {View} view - The ExcalidrawView or MarkdownView to toggle.
* @returns {Promise<View | null>} The replacement view after a successful toggle, or null when
* the supplied view is not a live Excalidraw-backed view that can be toggled.
*/
public async toggleViewMode(view: View): Promise<View | null> {
if (view instanceof ExcalidrawView) {
const file = view.file;
const leaf = view.leaf;
if (
!file ||
!leaf ||
leaf.view !== view ||
view.compatibilityMode ||
!this.isExcalidrawFile(file)
) {
return null;
}
const preserveInactive = this.plugin.app.workspace.getMostRecentLeaf() !== leaf;
await view.openAsMarkdown(
preserveInactive ? { focus: false } : undefined,
preserveInactive ? false : undefined,
);
return leaf.view instanceof MarkdownView ? leaf.view : null;
}
if (!(view instanceof MarkdownView)) {
return null;
}
const file = view.file;
const leaf = view.leaf;
if (
!file ||
!leaf ||
leaf.view !== view ||
!this.isExcalidrawFile(file)
) {
return null;
}
await view.save();
this.plugin.excalidrawFileModes[leaf.id || file.path] =
VIEW_TYPE_EXCALIDRAW;
const preserveInactive = this.plugin.app.workspace.getMostRecentLeaf() !== leaf;
await setExcalidrawView(
leaf,
preserveInactive ? { active: false, focus: false } : undefined,
);
return leaf.view instanceof ExcalidrawView ? leaf.view : null;
}
/**
* Sets the selection in the view.
* @param {ExcalidrawElement[] | string[]} elements - Array of elements or element IDs to select.
+21 -6
View File
@@ -582,12 +582,27 @@ export const foldExcalidrawSection = (view: MarkdownView) => {
}
};
export async function setExcalidrawView(leaf: WorkspaceLeaf) {
await leaf.setViewState({
type: VIEW_TYPE_EXCALIDRAW,
state: leaf.view.getState(),
popstate: true,
} as ViewState);
export interface ViewTransitionOptions {
active?: boolean;
focus?: boolean;
}
/** Replace a leaf with an Excalidraw view while honoring explicit activation and focus policy. */
export async function setExcalidrawView(
leaf: WorkspaceLeaf,
options?: ViewTransitionOptions,
) {
await leaf.setViewState(
{
type: VIEW_TYPE_EXCALIDRAW,
state: leaf.view.getState(),
popstate: true,
...(options?.active === undefined ? {} : { active: options.active }),
} as ViewState,
options?.focus === undefined
? undefined
: { focus: options.focus },
);
}
export async function closeLeafView(leaf: WorkspaceLeaf) {
+8 -2
View File
@@ -57,16 +57,22 @@ export class ExcalidrawLoading extends FileView {
public async switchToExcalidraw() {
const prevLeaf = this.app.workspace.getLeaf();
const state = this.leaf.view.getState();
const preserveInactive = this.app.workspace.getMostRecentLeaf() !== this.leaf;
const inactiveResult = preserveInactive
? ({ focus: false } as unknown as ViewStateResult)
: undefined;
// Force a fresh view instance: switching to the same type can be a no-op.
await this.leaf.setViewState({
type: "empty",
state: {},
});
...(preserveInactive ? { active: false } : {}),
}, inactiveResult);
await this.leaf.setViewState({
type: VIEW_TYPE_EXCALIDRAW,
state,
});
...(preserveInactive ? { active: false } : {}),
}, inactiveResult);
if (DEVICE.isDesktop) {
return;
}
+8 -4
View File
@@ -4527,7 +4527,7 @@ export default class ExcalidrawView
return ICON_NAME;
}
async setMarkdownView(eState?: MarkdownViewOpenState) {
async setMarkdownView(eState?: MarkdownViewOpenState, active?: boolean) {
//save before switching to markdown view.
//this would also happen onClose, but it does not hurt to save it here
//this way isDirty() will return false in onClose, thus
@@ -4560,7 +4560,11 @@ export default class ExcalidrawView
}
plugin.excalidrawFileModes[this.id || file.path] = "markdown";
await plugin.setMarkdownView(leaf, eState as ViewStateResult | undefined);
await plugin.setMarkdownView(
leaf,
eState as ViewStateResult | undefined,
active,
);
} catch (e: unknown) {
errorlog({
where: "ExcalidrawView.setMarkdownView",
@@ -4570,7 +4574,7 @@ export default class ExcalidrawView
}
}
public async openAsMarkdown(eState?: MarkdownViewOpenState) {
public async openAsMarkdown(eState?: MarkdownViewOpenState, active?: boolean) {
if (
this.plugin.settings.compress &&
this.plugin.settings.decompressForMDView
@@ -4580,7 +4584,7 @@ export default class ExcalidrawView
} else if (this.isDirty()) {
await this.save(true, true, true);
}
void this.setMarkdownView(eState);
await this.setMarkdownView(eState, active);
}
public async convertExcalidrawToMD() {
+21 -2
View File
@@ -8,7 +8,14 @@ node.render();
container.appendChild(node.contentEl)
*/
import { Editor, TFile, View, WorkspaceLeaf, WorkspaceSplit } from "obsidian";
import {
type App,
Editor,
TFile,
View,
WorkspaceLeaf,
WorkspaceSplit,
} from "obsidian";
import type ExcalidrawView from "src/view/ExcalidrawView";
import {
getContainerForDocument,
@@ -29,6 +36,12 @@ interface ObsidianCanvas {
removeNode: (node: ObsidianCanvasNode) => void;
}
type DetachedWorkspaceSplit = WorkspaceSplit & {
insertChild(index: number, child: WorkspaceLeaf, resize?: boolean): void;
};
type WorkspaceLeafConstructor = new (app: App) => WorkspaceLeaf;
export interface ObsidianCanvasNodeChild extends View {
file?: TFile;
heading?: string;
@@ -83,7 +96,13 @@ export class CanvasNodeFactory {
rootSplit.getRoot = () =>
app.workspace[doc === mainDocument ? "rootSplit" : "floatingSplit"];
rootSplit.getContainer = () => getContainerForDocument(doc);
this.leaf = app.workspace.createLeafInParent(rootSplit, 0);
// `workspace.createLeafInParent()` activates its new leaf and schedules a workspace layout
// update. That is observable even though this split is detached: on mobile it navigates away
// from an open sidebar that hosts an embedded Excalidraw view. Construct and insert the
// internal Canvas leaf directly so initialization has no workspace activation side effect.
const LeafConstructor = WorkspaceLeaf as unknown as WorkspaceLeafConstructor;
this.leaf = new LeafConstructor(app);
(rootSplit as DetachedWorkspaceSplit).insertChild(0, this.leaf);
this.canvas = canvasPlugin.views.canvas(this.leaf).canvas as ObsidianCanvas;
this.initialized = true;
}