From e744b4853695bade3354aaad6322dccd4047dd2b Mon Sep 17 00:00:00 2001 From: daniel Date: Mon, 23 Dec 2024 00:22:51 +0100 Subject: [PATCH] add highlighting and suggestion note --- src/DataviewSuggester.ts | 39 ++++++++++++++++++++++++++++++++------- styles.css | 27 ++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/DataviewSuggester.ts b/src/DataviewSuggester.ts index a55fb41..1727dad 100644 --- a/src/DataviewSuggester.ts +++ b/src/DataviewSuggester.ts @@ -5,12 +5,16 @@ import { EditorSuggest, EditorSuggestContext, EditorSuggestTriggerInfo, + htmlToMarkdown, + MarkdownRenderer, + MarkdownView, TFile, } from "obsidian"; import { getTriggerText } from "./trigger"; import uFuzzy from "@leeoniya/ufuzzy"; import { getAPI, DataviewApi } from "obsidian-dataview"; import DataviewAutocompletePlugin from "./main"; +import { parentPort } from "worker_threads"; export class DataviewSuggester extends EditorSuggest { plugin: DataviewAutocompletePlugin; @@ -71,16 +75,37 @@ export class DataviewSuggester extends EditorSuggest { // Split the value into parts based on the tags and their content const parts = value.split(/(.*?<\/mark>)/g); - // We cannot use inner HTML; Create a span for each part - parts.forEach((part) => { - if (part.startsWith("") && part.endsWith("")) { - const text = part.slice(6, -7); // Remove and - el.createEl("span", { text, cls: "suggestion-highlight" }); + const container = el.createDiv("dataview-suggestion-content"); + const titleDiv = container.createDiv("dataview-suggestion-title"); + + parts.forEach((textPart) => { + if (textPart.startsWith("") && textPart.endsWith("")) { + const text = textPart.slice(6, -7); // Remove and + titleDiv.createEl("span", { text, cls: "dataview-suggestion-highlight" }); } else { - // For normal text, create a text node or - el.createEl("span", { text: part }); + titleDiv.appendText(textPart); } }); + + if (value.indexOf("[[") !== -1 || value.indexOf("]]") !== -1 || value.indexOf("](") !== -1) { + const noteDiv = container.createDiv("dataview-suggestion-note"); + let suggestionText = ""; + parts.forEach((textPart) => { + if (textPart.startsWith("") && textPart.endsWith("")) { + const text = textPart.slice(6, -7); // Remove and + suggestionText += text; + } 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 { diff --git a/styles.css b/styles.css index 125cb57..dd74563 100644 --- a/styles.css +++ b/styles.css @@ -1,3 +1,28 @@ -span.suggestion-highlight { +div.dataview-suggestion-content { + display: flex; + flex-direction: column; + overflow: hidden; + text-overflow: ellipsis; + margin-inline-end: auto; +} + +div.dataview-suggestion-title { + overflow-wrap: break-word; +} + +div.dataview-suggestion-note p { + margin-block: 0px; +} + +div.dataview-suggestion-note{ + font-size: 0.8em; + color: var(--text-muted); + width: 100%; + flex-basis: 100%; + overflow-wrap: break-word; +} + +span.dataview-suggestion-highlight { font-weight: bold; } +