mirror of
https://github.com/dnlbauer/obsidian-dataview-autocompletion.git
synced 2026-09-10 22:25:29 +00:00
feat: only add suggester if dataview is enabled
This commit is contained in:
@@ -15,7 +15,6 @@ import { getAPI, DataviewApi } from "obsidian-dataview";
|
|||||||
export class DataviewSuggester extends EditorSuggest<String> {
|
export class DataviewSuggester extends EditorSuggest<String> {
|
||||||
maxSuggestions: number;
|
maxSuggestions: number;
|
||||||
searcher: uFuzzy;
|
searcher: uFuzzy;
|
||||||
dataviewApi: DataviewApi;
|
|
||||||
|
|
||||||
initialized: boolean = false;
|
initialized: boolean = false;
|
||||||
suggestionsList: string[] = [];
|
suggestionsList: string[] = [];
|
||||||
@@ -34,7 +33,6 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
intraMode: singleErrorMode ? 1 : 0,
|
intraMode: singleErrorMode ? 1 : 0,
|
||||||
intraIns: allowExtraChars ? 1 : 0,
|
intraIns: allowExtraChars ? 1 : 0,
|
||||||
});
|
});
|
||||||
this.dataviewApi = getAPI(plugin.app);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onTrigger(cursor: EditorPosition, editor: Editor, file: TFile): EditorSuggestTriggerInfo | null {
|
onTrigger(cursor: EditorPosition, editor: Editor, file: TFile): EditorSuggestTriggerInfo | null {
|
||||||
@@ -93,7 +91,7 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
// possible types: update, rename, delete. rename has oldPath
|
// possible types: update, rename, delete. rename has oldPath
|
||||||
public onDataviewMetadataChange(type: string, file: TFile, oldPath?: string) {
|
public onDataviewMetadataChange(type: string, file: TFile, oldPath?: string) {
|
||||||
if (!this.initialized) {
|
if (!this.initialized) {
|
||||||
console.log("Dataview Autocompletion index not ready yet. Skipping index update");
|
// console.warn("Dataview Autocompletion index not ready yet. Skipping index update");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.updateIndex(type, file, oldPath);
|
this.updateIndex(type, file, oldPath);
|
||||||
@@ -104,21 +102,19 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
* returns the composite value in format "key:: value"
|
* returns the composite value in format "key:: value"
|
||||||
*/
|
*/
|
||||||
formatCompositeValue(key: string, value: any): string {
|
formatCompositeValue(key: string, value: any): string {
|
||||||
|
const dataviewAPI = getAPI(this.app);
|
||||||
|
|
||||||
let stringValue: string;
|
let stringValue: string;
|
||||||
|
|
||||||
// If the value is a string, number or boolean, we can simply convert it to a string
|
// If the value is a string, number or boolean, we can simply convert it to a string
|
||||||
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
||||||
stringValue = value.toString();
|
stringValue = value.toString();
|
||||||
} else if (
|
} else if (dataviewAPI.value.typeOf(value) === "link" && value.type === "file" && value.display !== undefined) {
|
||||||
this.dataviewApi.value.typeOf(value) === "link" &&
|
|
||||||
value.type === "file" &&
|
|
||||||
value.display !== undefined
|
|
||||||
) {
|
|
||||||
// value.toString always adds a display value to wiki-style links.
|
// value.toString always adds a display value to wiki-style links.
|
||||||
// parse wiki-style links without display value manually here to prevent this from happening for [[filename]]
|
// parse wiki-style links without display value manually here to prevent this from happening for [[filename]]
|
||||||
stringValue = `[[${value.path.split("/").pop().replace(".md", "")}]]`;
|
stringValue = `[[${value.path.split("/").pop().replace(".md", "")}]]`;
|
||||||
} else {
|
} else {
|
||||||
stringValue = this.dataviewApi.value.toString(value);
|
stringValue = dataviewAPI.value.toString(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${key}:: ${stringValue}`;
|
return `${key}:: ${stringValue}`;
|
||||||
@@ -127,15 +123,22 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
buildNewIndex() {
|
buildNewIndex() {
|
||||||
console.log("Begin Rebuilding dataview suggestion index");
|
console.log("Begin Rebuilding dataview suggestion index");
|
||||||
const startTime = performance.now();
|
const startTime = performance.now();
|
||||||
|
const dataviewApi = getAPI(this.app);
|
||||||
|
|
||||||
const newSuggestions: string[] = [];
|
const newSuggestions: string[] = [];
|
||||||
const newSuggestionsRefs: { [key: string]: string[] } = {};
|
const newSuggestionsRefs: { [key: string]: string[] } = {};
|
||||||
const newSuggestionsRefCount: { [key: string]: number } = {};
|
const newSuggestionsRefCount: { [key: string]: number } = {};
|
||||||
|
|
||||||
for (const page of this.dataviewApi.index.pages) {
|
const files = this.app.vault.getFiles();
|
||||||
|
for (const file of files) {
|
||||||
|
const page = dataviewApi.page(file.path);
|
||||||
|
const fields = Object.keys(page)
|
||||||
|
.filter((k) => k !== "file")
|
||||||
|
.map((k) => [k, page[k]]);
|
||||||
|
|
||||||
const pageRefs = [];
|
const pageRefs = [];
|
||||||
|
|
||||||
for (let [key, val] of page[1].fields) {
|
for (let [key, val] of fields) {
|
||||||
// fields can be a single value or a dict, so we need to handle both
|
// fields can be a single value or a dict, so we need to handle both
|
||||||
let arrayVal;
|
let arrayVal;
|
||||||
if (!Array.isArray(val)) {
|
if (!Array.isArray(val)) {
|
||||||
@@ -162,7 +165,7 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
newSuggestionsRefs[page[0]] = pageRefs;
|
newSuggestionsRefs[file.path] = pageRefs;
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace old index
|
// replace old index
|
||||||
@@ -181,7 +184,7 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
if (type === "update") {
|
if (type === "update") {
|
||||||
const updateCompositeValues = [];
|
const updateCompositeValues = [];
|
||||||
|
|
||||||
const page = this.dataviewApi.page(file.path);
|
const page = getAPI(this.app).page(file.path);
|
||||||
const fields = Object.keys(page)
|
const fields = Object.keys(page)
|
||||||
.filter((k) => k !== "file")
|
.filter((k) => k !== "file")
|
||||||
.map((k) => [k, page[k]]);
|
.map((k) => [k, page[k]]);
|
||||||
@@ -202,7 +205,6 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
updateCompositeValues.push(compositeValue);
|
updateCompositeValues.push(compositeValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const compositeValue of this.suggestionsRefs[file.path]) {
|
for (const compositeValue of this.suggestionsRefs[file.path]) {
|
||||||
if (updateCompositeValues.indexOf(compositeValue) === -1) {
|
if (updateCompositeValues.indexOf(compositeValue) === -1) {
|
||||||
// delete value
|
// delete value
|
||||||
|
|||||||
17
src/main.ts
17
src/main.ts
@@ -1,5 +1,6 @@
|
|||||||
import { App, Plugin, PluginSettingTab, Setting, TFile } from "obsidian";
|
import { App, Plugin, PluginSettingTab, Setting, TFile } from "obsidian";
|
||||||
import { DataviewSuggester } from "./DataviewSuggester";
|
import { DataviewSuggester } from "./DataviewSuggester";
|
||||||
|
import { getAPI } from "obsidian-dataview";
|
||||||
|
|
||||||
interface DataviewAutocompleteSettings {
|
interface DataviewAutocompleteSettings {
|
||||||
// mySetting: string;
|
// mySetting: string;
|
||||||
@@ -16,10 +17,6 @@ export default class DataviewAutocompletePlugin extends Plugin {
|
|||||||
async onload() {
|
async onload() {
|
||||||
await this.loadSettings();
|
await this.loadSettings();
|
||||||
|
|
||||||
// register suggester
|
|
||||||
this.suggester = new DataviewSuggester(this, 10, true, true);
|
|
||||||
this.registerEditorSuggest(this.suggester);
|
|
||||||
|
|
||||||
this.registerEvent(
|
this.registerEvent(
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
this.app.metadataCache.on("dataview:index-ready", () => {
|
this.app.metadataCache.on("dataview:index-ready", () => {
|
||||||
@@ -36,6 +33,18 @@ export default class DataviewAutocompletePlugin extends Plugin {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.app.workspace.onLayoutReady(() => {
|
||||||
|
// @ts-ignore
|
||||||
|
if (!Object.keys(this.app.plugins.plugins).includes("dataview")) {
|
||||||
|
console.warn("Dataview plugin not installed. Dataview Autocompletion plugin will not work.");
|
||||||
|
} else {
|
||||||
|
// register suggester, requires dataview to be loaded first.
|
||||||
|
console.log("Registering dataview autocompletion suggester");
|
||||||
|
this.suggester = new DataviewSuggester(this, 10, true, true);
|
||||||
|
this.registerEditorSuggest(this.suggester);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onunload() {}
|
onunload() {}
|
||||||
|
|||||||
@@ -53,7 +53,6 @@ export function getTriggerText(line: string, cursorPos: number): [string, number
|
|||||||
function getTriggerTextFromRegex(line: string, cursorPos: number, regex: RegExp): [string, number, number] | null {
|
function getTriggerTextFromRegex(line: string, cursorPos: number, regex: RegExp): [string, number, number] | null {
|
||||||
let matches = Array.from(line.matchAll(regex));
|
let matches = Array.from(line.matchAll(regex));
|
||||||
for (const match of matches) {
|
for (const match of matches) {
|
||||||
console.warn(match);
|
|
||||||
if (match.index === undefined) {
|
if (match.index === undefined) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user