Skip to content

Commit c98ca53

Browse files
add ability to create views for queries that are treated like any other file view (e.g., they can be dragged into the sidebar)
1 parent 95fefe0 commit c98ca53

File tree

4 files changed

+628
-0
lines changed

4 files changed

+628
-0
lines changed

src/main.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DateTime } from "luxon";
44
import { App, Plugin, PluginSettingTab, Setting } from "obsidian";
55
import { createElement, render } from "preact";
66
import { DEFAULT_SETTINGS, Settings } from "settings";
7+
import { DatacoreQueryView as DatacoreJSView, VIEW_TYPE_DATACOREJS } from "ui/view-page";
78
import { IndexStatusBar } from "ui/index-status";
89

910
/** Reactive data engine for your Obsidian.md vault. */
@@ -52,6 +53,21 @@ export default class DatacorePlugin extends Plugin {
5253
-100
5354
);
5455

56+
// Views: DatacoreJS view.
57+
// @ts-ignore be quiet
58+
this.registerView(VIEW_TYPE_DATACOREJS, (leaf) => new DatacoreJSView(leaf, this.api));
59+
60+
// Add a command for creating a new view page.
61+
this.addCommand({
62+
id: "datacore-add-view-page",
63+
name: "Create View Page",
64+
callback: () => {
65+
const newLeaf = this.app.workspace.getLeaf("tab");
66+
newLeaf.setViewState({ type: VIEW_TYPE_DATACOREJS, active: true });
67+
this.app.workspace.setActiveLeaf(newLeaf, { focus: true });
68+
},
69+
});
70+
5571
// Register JS highlighting for codeblocks.
5672
this.register(this.registerCodeblockHighlighting());
5773

src/typings/obsidian-ex.d.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,48 @@
1+
import { Extension } from "@codemirror/state";
12
import type { DatacoreApi } from "api/api";
23
import { CanvasMetadataIndex } from "index/types/json/canvas";
34
import "obsidian";
5+
import { App } from "obsidian";
6+
import * as hooks from "preact/hooks";
47

58
/** @hidden */
69
declare module "obsidian" {
10+
interface WorkspaceLeaf {
11+
serialize(): {
12+
id: string;
13+
type: "leaf";
14+
state: {
15+
type: string;
16+
state: any;
17+
};
18+
};
19+
tabHeaderEl: HTMLElement;
20+
tabHeaderInnerTitleEl: HTMLElement;
21+
}
22+
interface View {
23+
getState(): any;
24+
}
25+
interface ItemView {
26+
titleEl: HTMLElement;
27+
getState(): any;
28+
}
29+
30+
interface InternalPlugin<T> {
31+
id: string;
32+
name: string;
33+
description: string;
34+
instance: T;
35+
}
36+
export interface PagePreviewPlugin {
37+
onLinkHover: (
38+
view: View,
39+
hovered: HTMLElement,
40+
hoveredPath: string,
41+
sourcePath: string,
42+
_unknown: unknown
43+
) => void;
44+
}
45+
746
interface FileManager {
847
linkUpdaters: {
948
canvas: {
@@ -15,6 +54,9 @@ declare module "obsidian" {
1554
};
1655
};
1756
}
57+
interface Vault {
58+
getConfig: (conf: string) => any;
59+
}
1860
interface App {
1961
appId?: string;
2062
plugins: {
@@ -23,8 +65,15 @@ declare module "obsidian" {
2365
datacore?: {
2466
api: DatacoreApi;
2567
};
68+
"datacore-addon-autocomplete"?: {
69+
readonly extensions: Extension[];
70+
};
2671
};
2772
};
73+
internalPlugins: {
74+
getPluginById: <T>(id: string) => InternalPlugin<T>;
75+
};
76+
2877
embedRegistry: {
2978
embedByExtension: {
3079
[key: string]: unknown;
@@ -50,5 +99,10 @@ declare module "obsidian" {
5099
declare global {
51100
interface Window {
52101
datacore?: DatacoreApi;
102+
app: App;
103+
CodeMirror: {
104+
defineMode: (mode: string, conf: (config: any) => any) => unknown;
105+
[key: string]: any;
106+
};
53107
}
54108
}

src/ui/view-page.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.dc-cm-editor .cm-gutters {
2+
flex: 0 0 auto;
3+
background-color: transparent;
4+
color: var(--text-faint) !important;
5+
border-right: none !important;
6+
margin-inline-end: var(--file-folding-offset);
7+
font-size: var(--font-ui-smaller);
8+
z-index: 1;
9+
font-variant: tabular-nums;
10+
}
11+
.cm-typeName {
12+
color: var(--headers);
13+
}
14+
.dc-cm-editor {
15+
padding: 1em;
16+
border-radius: 0.5em;
17+
border: 1px solid var(--h5-color);
18+
}
19+
.cm-tooltip.cm-tooltip-autocomplete {
20+
background: var(--embed-bg);
21+
}
22+
.dc-cm-editor img.cm-widgetBuffer[aria-hidden="true"] {
23+
display: none;
24+
}

0 commit comments

Comments
 (0)