Files
obsidian-excalidraw-plugin/src/types/types.d.ts
T
Michael Naumov 9cf33817b7 Save pasted and dropped images through app.saveAttachment
When the drawing is the active file and no onImageFilePathHook is set, an
imported image is saved the way Obsidian saves one pasted or dropped into a
markdown note, so attachment-management plugins apply their naming and
location rules to it. A generated paste name follows Obsidian's own
"Pasted image YYYYMMDDHHmmss" in that case. Every other caller keeps the
existing path.
2026-09-29 14:31:31 -06:00

430 lines
10 KiB
TypeScript

import {
TFile,
View,
WorkspaceLeaf,
PluginManifest,
Hotkey,
TAbstractFile,
MenuItem,
Component,
EventRef,
WorkspaceContainer,
PluginSettingTab,
} from "obsidian";
import { ExcalidrawAutomate } from "../shared/ExcalidrawAutomate";
import { ExcalidrawLib } from "./excalidrawLib";
import type { PdfJsLibrary } from "./pdfJsTypes";
type PolyBoolLibrary = {
epsilon(value: number): void;
};
type ObsidianInternalPluginInstance = {
openGlobalSearch?(query: string): void;
apiList?(): Promise<{
files?: Array<{
path: string;
}>;
}>;
};
type ObsidianInternalPlugin = {
_loaded?: boolean;
load?(): Promise<void>;
views?: {
canvas(leaf: WorkspaceLeaf): {
canvas: unknown;
};
};
instance?: ObsidianInternalPluginInstance;
};
type ObsidianInternalPluginsManager = {
plugins: Record<string, ObsidianInternalPlugin | undefined>;
getPluginById(id: string): ObsidianInternalPlugin | undefined;
};
type ObsidianSettingsManager = {
open(): void;
findTabById?(tabId: string): PluginSettingTab | null;
openTabById(tabId: string): void;
/**
* Resolves a settings-tab identifier and opens its declarative page path.
* This is an unpublished Obsidian 1.13+ API and may be absent or change
* without notice; callers must guard it at runtime.
*/
openPagePath?(
tabId: string,
pagePath: string[],
): PluginSettingTab | null;
/**
* Opens a declarative settings page for a resolved tab object and localized
* page-name path. This is an unpublished Obsidian 1.13+ API and may be
* absent or change without notice; callers must guard it at runtime.
*/
navigateToPage?(tab: PluginSettingTab, pagePath: string[]): void;
};
type ObsidianViewRegistry = {
getViewTypeForFile?(file: TFile): string | null | undefined;
getViewTypeByFile?(file: TFile): string | null | undefined;
getTypeByFile?(file: TFile): string | null | undefined;
getViewTypeByExtension?(ext: string): string | null | undefined;
getTypeByExtension?(ext: string): string | null | undefined;
};
type MarkdownBlockNode = {
type: string;
id?: string;
depth?: number;
level?: number;
value?: string;
title?: string;
data?: {
hProperties?: {
dataHeading?: string;
};
};
children?: MarkdownBlockNode[];
position: {
start: {
offset: number;
line: number;
};
end: {
offset: number;
};
};
};
type ObsidianDragManager = {
draggable?: {
file?: TFile;
files?: TFile[];
title?: string;
type?: string;
};
};
type MarkdownBlockCacheEntry = {
display: string;
node: MarkdownBlockNode;
};
type MarkdownBlockCacheResult = {
blocks: MarkdownBlockCacheEntry[];
};
type BacklinksForFileResult = {
data: Record<string, import("obsidian").LinkCache[]>;
};
export type ObsidianCommand = {
id: string;
name: string;
icon?: string;
mobileOnly?: boolean;
repeatable?: boolean;
};
export type ObsidianCommandManager = {
listCommands(): ObsidianCommand[];
executeCommandById(commandId: string): boolean;
commands: Record<string, ObsidianCommand>;
};
export type ObsidianDraggable = {
type?: "file" | "files" | "link" | "text" | "unknown";
file?: TFile;
files?: TFile[];
title?: string;
[key: string]: unknown;
};
export type ConnectionPoint = "top" | "bottom" | "left" | "right" | null;
export type Packages = {
react: typeof import("react");
reactDOM: typeof import("react-dom") & typeof import("react-dom/client");
excalidrawLib: typeof ExcalidrawLib | null;
};
/**
* An idempotent handle for the shared runtime acquired by one view.
*
* @remarks
* The captured window remains the view's acquisition window even if Obsidian
* reparents its DOM before teardown completes. All leases share the one package
* evaluated in the main application realm, so lifecycle code must use `window`
* for migration ownership and never infer package evaluation ownership from it.
*/
export type PackageLease = {
readonly window: Window;
readonly packages: Packages;
release(): void;
};
export type ValueOf<T> = T[keyof T];
export type DynamicStyle = "none" | "gray" | "colorful";
export type GridSettings = {
DYNAMIC_COLOR: boolean; // Whether the grid color is dynamic
COLOR: string; // The grid color (in hex format)
OPACITY: number; // The grid opacity (hex value between "00" and "FF")
GRID_DIRECTION: { horizontal: boolean; vertical: boolean }; // Whether the grid is horizontal or vertical
};
export type DeviceType = {
isDesktop: boolean;
isPhone: boolean;
isTablet: boolean;
isMobile: boolean;
isLinux: boolean;
isMacOS: boolean;
isWindows: boolean;
isIOS: boolean;
isAndroid: boolean;
};
export type Point = [number, number];
export type LinkSuggestion = {
file: TFile;
path: string;
alias?: string;
};
export type LocalGraphView = View & {
file?: TFile;
loadFile?: (file: TFile) => void;
};
declare global {
interface Window {
MouseEvent?: typeof MouseEvent;
ExcalidrawAutomate: ExcalidrawAutomate;
ExcalidrawLib: typeof ExcalidrawLib;
pdfjsLib: PdfJsLibrary;
unpackBase64Deflate?: (value: string) => string;
PolyBool?: PolyBoolLibrary;
electronWindow?: {
isAlwaysOnTop(): boolean;
setAlwaysOnTop(flag: boolean): void;
};
eval: (x: string) => unknown;
MathJax?: {
typesetPromise(elements: HTMLElement[]): Promise<void>;
};
}
interface File {
path?: string;
}
}
declare module "obsidian" {
interface App {
appId: string;
dragManager: ObsidianDragManager;
viewRegistry?: ObsidianViewRegistry;
internalPlugins: ObsidianInternalPluginsManager;
setting: ObsidianSettingsManager;
commands: ObsidianCommandManager;
isMobile(): boolean;
getAccentColor(): string;
getObsidianUrl(file: TFile): string;
metadataTypeManager: {
setType(name: string, type: string): void;
};
plugins: {
manifests: Record<string, PluginManifest>;
plugins: Record<string, (Plugin & { api?: unknown }) | undefined>;
enablePlugin(id: string): Promise<void>;
disablePlugin(id: string): Promise<void>;
};
/**
* Saves an attachment where Obsidian saves an image pasted or dropped into the active note, making the
* path unique. Internal, present with this signature since Obsidian 0.8.5; attachment-management plugins
* patch it to apply their naming and location rules.
*/
saveAttachment(
name: string,
extension: string,
data: ArrayBuffer,
): Promise<TFile>;
hotkeyManager: {
addDefaultHotkeys(commandId: string, hotkeys: Hotkey[]): void;
};
}
interface FileManager {
promptForFileRename(file: TFile): Promise<void>;
}
interface Vault {
getAbstractFileByPathInsensitive(path: string): TAbstractFile | null;
getConfig(name: string): unknown;
}
interface FileView {
_loaded: boolean;
headerEl: HTMLElement;
}
interface View {
file?: TFile;
canvas?: {
setReadonly(readonly: boolean): void;
};
modes?: Record<string, unknown>;
setMode?(mode: unknown): void;
viewer?: {
child?: {
pdfViewer?: {
page?: number;
setBackground?(color: string | null, isInverted: boolean): void;
};
};
};
}
interface TextFileView {
lastSavedData: string;
}
interface Menu {
items: MenuItem[];
}
interface Setting {
setVisibility(visible: boolean): Setting;
}
interface Modal {
containerEl: HTMLElement;
modalEl: HTMLElement;
bgEl: HTMLElement;
titleEl: HTMLElement;
headerEl: HTMLElement;
/**
* Whether to *dim* the background behind the modal. If {@link dimmed} is `true`, the
* opacity-value from [setBackgroundOpacity]{@link Modal#setBackgroundOpacity} or
* the default of `0.85` is used.
* @note The hidden backdrop will still catch focus.
*/
setDimBackground(dimmed: boolean): Modal;
/** Sets the opacity of the Modal backdrop. */
setBackgroundOpacity(opacity: number): Modal;
}
interface Keymap {
getRootScope(): Scope;
}
interface Scope {
keys: Array<{
key?: string;
modifiers?: Array<"Mod" | "Ctrl" | "Meta" | "Shift" | "Alt">;
}>;
}
interface WorkspaceLeaf {
id: string;
containerEl: HTMLDivElement;
tabHeaderInnerTitleEl: HTMLDivElement;
tabHeaderInnerIconEl: HTMLDivElement;
isVisible?(): boolean;
}
interface WorkspaceWindowInitData {
x?: number;
y?: number;
}
interface WorkspaceTabs {
type?: string;
children?: unknown[];
}
interface WorkspaceMobileDrawer {
type?: string;
children?: unknown[];
}
interface Workspace {
floatingSplit?: WorkspaceSplit;
getAdjacentLeafInDirection(
leaf: WorkspaceLeaf,
direction: string,
): WorkspaceLeaf;
on(
name: "hover-link",
callback: (e: MouseEvent) => boolean | void,
ctx?: Component,
): EventRef;
}
interface WorkspaceSplit {
containerEl: HTMLDivElement;
children?: WorkspaceContainer[];
}
interface DataAdapter {
url: {
pathToFileURL(path: string): URL;
};
basePath: string;
fs: {
readFile(
path: string,
encoding: BufferEncoding,
callback: (err: NodeJS.ErrnoException | null, data: string) => void,
): void;
readFile(
path: string,
callback: (err: NodeJS.ErrnoException | null, data: Buffer) => void,
): void;
};
}
interface FoldPosition {
from: number;
to: number;
}
interface FoldInfo {
folds: FoldPosition[];
lines: number;
}
interface MarkdownSubView {
applyFoldInfo(foldInfo: FoldInfo): void;
getFoldInfo(): FoldInfo | null;
}
interface MarkdownPostProcessorContext {
remainingNestLevel: number;
containerEl: HTMLElement;
}
/*interface Editor {
insertText(data: string): void;
}*/
interface MetadataCache {
getLinkSuggestions?(): Array<{
alias?: string;
path: string;
file: TFile;
}>;
getBacklinksForFile(file: TFile): BacklinksForFileResult | null;
getLinks(): Record<string, import("obsidian").LinkCache[]>;
getCachedFiles(): string[];
blockCache: {
getForFile(
x: { isCancelled(): boolean },
f: TAbstractFile,
): Promise<MarkdownBlockCacheResult>;
};
}
interface FuzzySuggestModal<T> {
chooser: {
values: Array<{ item: T }>;
selectedItem: number;
};
}
interface HoverPopover {
containerEl: HTMLElement;
embed?: {
editor?: unknown;
};
hide(): void;
}
interface Plugin {
_loaded: boolean;
}
}