mirror of
https://github.com/dnlbauer/obsidian-dataview-autocompletion.git
synced 2026-09-10 22:25:29 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b10656c2c3 | ||
|
|
cc078fae6a | ||
|
|
b6cdf6cb47 | ||
|
|
5bfb3116bc | ||
|
|
dac07242cd |
@@ -5,7 +5,7 @@ This is a plugin for Obsidian (https://obsidian.md).
|
|||||||
Obsidian Dataview Autocomplete enhances the functionality of the popular [Dataview plugin](https://github.com/blacksmithgu/obsidian-dataview)
|
Obsidian Dataview Autocomplete enhances the functionality of the popular [Dataview plugin](https://github.com/blacksmithgu/obsidian-dataview)
|
||||||
by providing autocomplete suggestions for metadata fields in the editor. This makes it easy to reuse existing fields and reduces typing errors.
|
by providing autocomplete suggestions for metadata fields in the editor. This makes it easy to reuse existing fields and reduces typing errors.
|
||||||
|
|
||||||

|
<img src="./assets/demo.gif" alt="Demo" width="400" >
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
|
|||||||
BIN
assets/demo.gif
BIN
assets/demo.gif
Binary file not shown.
|
Before Width: | Height: | Size: 853 KiB After Width: | Height: | Size: 1.5 MiB |
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"id": "dataview-autocompletion",
|
"id": "dataview-autocompletion",
|
||||||
"name": "Dataview Autocompletion",
|
"name": "Dataview Autocompletion",
|
||||||
"version": "0.9.0",
|
"version": "0.9.1",
|
||||||
"minAppVersion": "0.15.0",
|
"minAppVersion": "0.15.0",
|
||||||
"description": "Adds autocompletion to Dataview metadata fields",
|
"description": "Adds autocompletion to Dataview metadata fields",
|
||||||
"author": "Daniel Bauer",
|
"author": "Daniel Bauer",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "obsidian-dataview-autocompletion",
|
"name": "obsidian-dataview-autocompletion",
|
||||||
"version": "0.9.0",
|
"version": "0.9.1",
|
||||||
"description": "This is a plugin for Obsidian that provides autocompletion for Dataview metadata fields",
|
"description": "This is a plugin for Obsidian that provides autocompletion for Dataview metadata fields",
|
||||||
"main": "main.js",
|
"main": "main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import { getTriggerText } from "./trigger";
|
|||||||
import uFuzzy from "@leeoniya/ufuzzy";
|
import uFuzzy from "@leeoniya/ufuzzy";
|
||||||
import { getAPI, DataviewApi } from "obsidian-dataview";
|
import { getAPI, DataviewApi } from "obsidian-dataview";
|
||||||
import DataviewAutocompletePlugin from "./main";
|
import DataviewAutocompletePlugin from "./main";
|
||||||
import { parentPort } from "worker_threads";
|
|
||||||
|
|
||||||
export class DataviewSuggester extends EditorSuggest<String> {
|
export class DataviewSuggester extends EditorSuggest<String> {
|
||||||
plugin: DataviewAutocompletePlugin;
|
plugin: DataviewAutocompletePlugin;
|
||||||
@@ -71,52 +70,57 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
renderSuggestion(value: string, el: HTMLElement): void {
|
renderMarkdownSuggestion(value: string, el: HTMLElement): void {
|
||||||
// Split the value into parts based on the <mark> tags and their content
|
// render markdown preview of inline dataview metadata field
|
||||||
|
let suggestionText = this.context!.editor.getLine(this.context!.start.line).slice(
|
||||||
|
this.context!.start.ch - 1,
|
||||||
|
this.context!.start.ch,
|
||||||
|
)!;
|
||||||
|
suggestionText += value.replace(/<mark>(.*?)<\/mark>/g, "$1");
|
||||||
|
suggestionText += suggestionText.startsWith("(") ? ")" : "]";
|
||||||
|
|
||||||
|
MarkdownRenderer.render(
|
||||||
|
this.app,
|
||||||
|
htmlToMarkdown(suggestionText),
|
||||||
|
el,
|
||||||
|
this.context!.file.path,
|
||||||
|
this.app.workspace.getActiveViewOfType(MarkdownView)!,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
renderSourceSuggestion(value: string, el: HTMLElement): void {
|
||||||
|
// render source text with highlights
|
||||||
|
// replaces <mark>...</mark> with <span>...</span>
|
||||||
const parts = value.split(/(<mark>.*?<\/mark>)/g);
|
const parts = value.split(/(<mark>.*?<\/mark>)/g);
|
||||||
|
|
||||||
const container = el.createDiv("dataview-suggestion-content");
|
|
||||||
const titleDiv = container.createDiv("dataview-suggestion-title");
|
|
||||||
|
|
||||||
parts.forEach((textPart) => {
|
parts.forEach((textPart) => {
|
||||||
if (textPart.startsWith("<mark>") && textPart.endsWith("</mark>")) {
|
if (textPart.startsWith("<mark>") && textPart.endsWith("</mark>")) {
|
||||||
const text = textPart.slice(6, -7); // Remove <mark> and </mark>
|
const text = textPart.slice(6, -7); // Remove <mark> and </mark>
|
||||||
titleDiv.createEl("span", { text, cls: "dataview-suggestion-highlight" });
|
el.createEl("span", { text, cls: "dataview-suggestion-highlight" });
|
||||||
} else {
|
} else {
|
||||||
titleDiv.appendText(textPart);
|
el.appendText(textPart);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (value.indexOf("[[") !== -1 || value.indexOf("]]") !== -1 || value.indexOf("](") !== -1) {
|
renderSuggestion(value: string, el: HTMLElement): void {
|
||||||
const noteDiv = container.createDiv("dataview-suggestion-note");
|
const container = el.createDiv("dataview-suggestion-content");
|
||||||
let suggestionText = "";
|
const titleDiv = container.createDiv("dataview-suggestion-title");
|
||||||
parts.forEach((textPart) => {
|
const noteDiv = container.createDiv("dataview-suggestion-note");
|
||||||
if (textPart.startsWith("<mark>") && textPart.endsWith("</mark>")) {
|
|
||||||
const text = textPart.slice(6, -7); // Remove <mark> and </mark>
|
this.renderMarkdownSuggestion(value, titleDiv);
|
||||||
suggestionText += text;
|
this.renderSourceSuggestion(value, noteDiv);
|
||||||
} else {
|
|
||||||
suggestionText += textPart;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
MarkdownRenderer.render(
|
|
||||||
this.app,
|
|
||||||
htmlToMarkdown(suggestionText),
|
|
||||||
noteDiv,
|
|
||||||
this.context!.file.path,
|
|
||||||
this.app.workspace.getActiveViewOfType(MarkdownView)!,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
selectSuggestion(value: string, evt: MouseEvent | KeyboardEvent): void {
|
selectSuggestion(value: string, evt: MouseEvent | KeyboardEvent): void {
|
||||||
const { editor, start, end } = this.context!;
|
|
||||||
// remove marks from selection
|
// remove marks from selection
|
||||||
value = value.replace(/<mark>(.*?)<\/mark>/g, "$1");
|
value = value.replace(/<mark>(.*?)<\/mark>/g, "$1");
|
||||||
|
|
||||||
|
const { editor, start, end } = this.context!;
|
||||||
editor.replaceRange(value, start, end);
|
editor.replaceRange(value, start, end);
|
||||||
|
|
||||||
const newCursorPos = {
|
const newCursorPos = {
|
||||||
line: end.line + 1,
|
line: end.line,
|
||||||
ch: 0,
|
ch: start.ch + value.length + 1,
|
||||||
};
|
};
|
||||||
editor.setCursor(newCursorPos);
|
editor.setCursor(newCursorPos);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ div.dataview-suggestion-title {
|
|||||||
overflow-wrap: break-word;
|
overflow-wrap: break-word;
|
||||||
}
|
}
|
||||||
|
|
||||||
div.dataview-suggestion-note p {
|
div.dataview-suggestion-title p {
|
||||||
margin-block: 0px;
|
margin-block: 0px;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,6 +20,7 @@ div.dataview-suggestion-note{
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
flex-basis: 100%;
|
flex-basis: 100%;
|
||||||
overflow-wrap: break-word;
|
overflow-wrap: break-word;
|
||||||
|
padding-left: 8px;
|
||||||
}
|
}
|
||||||
|
|
||||||
span.dataview-suggestion-highlight {
|
span.dataview-suggestion-highlight {
|
||||||
|
|||||||
Reference in New Issue
Block a user