Skip to content

Commit

Permalink
switch implementation of `renderCompactMarkdown based on a new boolea…
Browse files Browse the repository at this point in the history
…n parameter, not `component instanceof` test
  • Loading branch information
RyotaUshio committed Oct 31, 2023
1 parent a953f30 commit cc4e057
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 24 deletions.
90 changes: 76 additions & 14 deletions src/ui/render.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { Component, MarkdownRenderer } from "obsidian";
import { Component, MarkdownRenderer, Notice } from "obsidian";
import { DataArray } from "api/data-array";
import { QuerySettings } from "settings";
import { currentLocale } from "util/locale";
import { renderMinimalDate, renderMinimalDuration } from "util/normalize";
import { Literal, Values, Widgets } from "data-model/value";
import { InlineFieldLivePreviewComponent } from "./views/inline-field-live-preview";

/** Render simple fields compactly, removing wrapping content like paragraph and span. */
export async function renderCompactMarkdown(
markdown: string,
container: HTMLElement,
sourcePath: string,
component: Component
component: Component,
isInlineFieldLivePreview: boolean = false
) {
new Notice(`${markdown}: ${isInlineFieldLivePreview}`);
// check if the call is from the CM6 view plugin defined in src/ui/views/inline-field-live-preview.ts
if (component instanceof InlineFieldLivePreviewComponent) {
if (isInlineFieldLivePreview) {
const tmpContainer = createSpan();
await MarkdownRenderer.renderMarkdown(markdown, tmpContainer, sourcePath, component);

Expand Down Expand Up @@ -91,7 +92,8 @@ export async function renderValue(
settings: QuerySettings,
expandList: boolean = false,
context: ValueRenderContext = "root",
depth: number = 0
depth: number = 0,
isInlineFieldLivePreview: boolean = false
) {
// Prevent infinite recursion.
if (depth > settings.maxRecursiveRenderDepth) {
Expand All @@ -100,22 +102,42 @@ export async function renderValue(
}

if (Values.isNull(field)) {
await renderCompactMarkdown(settings.renderNullAs, container, originFile, component);
await renderCompactMarkdown(settings.renderNullAs, container, originFile, component, isInlineFieldLivePreview);
} else if (Values.isDate(field)) {
container.appendText(renderMinimalDate(field, settings, currentLocale()));
} else if (Values.isDuration(field)) {
container.appendText(renderMinimalDuration(field));
} else if (Values.isString(field) || Values.isBoolean(field) || Values.isNumber(field)) {
await renderCompactMarkdown("" + field, container, originFile, component);
await renderCompactMarkdown("" + field, container, originFile, component, isInlineFieldLivePreview);
} else if (Values.isLink(field)) {
await renderCompactMarkdown(field.markdown(), container, originFile, component);
await renderCompactMarkdown(field.markdown(), container, originFile, component, isInlineFieldLivePreview);
} else if (Values.isHtml(field)) {
container.appendChild(field);
} else if (Values.isWidget(field)) {
if (Widgets.isListPair(field)) {
await renderValue(field.key, container, originFile, component, settings, expandList, context, depth);
await renderValue(
field.key,
container,
originFile,
component,
settings,
expandList,
context,
depth,
isInlineFieldLivePreview
);
container.appendText(": ");
await renderValue(field.value, container, originFile, component, settings, expandList, context, depth);
await renderValue(
field.value,
container,
originFile,
component,
settings,
expandList,
context,
depth,
isInlineFieldLivePreview
);
} else if (Widgets.isExternalLink(field)) {
let elem = document.createElement("a");
elem.textContent = field.display ?? field.url;
Expand All @@ -140,7 +162,17 @@ export async function renderValue(
});
for (let child of field) {
let li = list.createEl("li", { cls: "dataview-result-list-li" });
await renderValue(child, li, originFile, component, settings, expandList, "list", depth + 1);
await renderValue(
child,
li,
originFile,
component,
settings,
expandList,
"list",
depth + 1,
isInlineFieldLivePreview
);
}
} else {
if (field.length == 0) {
Expand All @@ -154,7 +186,17 @@ export async function renderValue(
if (first) first = false;
else span.appendText(", ");

await renderValue(val, span, originFile, component, settings, expandList, "list", depth + 1);
await renderValue(
val,
span,
originFile,
component,
settings,
expandList,
"list",
depth + 1,
isInlineFieldLivePreview
);
}
}
} else if (Values.isObject(field)) {
Expand All @@ -169,7 +211,17 @@ export async function renderValue(
for (let [key, value] of Object.entries(field)) {
let li = list.createEl("li", { cls: ["dataview", "dataview-li", "dataview-result-object-li"] });
li.appendText(key + ": ");
await renderValue(value, li, originFile, component, settings, expandList, "list", depth + 1);
await renderValue(
value,
li,
originFile,
component,
settings,
expandList,
"list",
depth + 1,
isInlineFieldLivePreview
);
}
} else {
if (Object.keys(field).length == 0) {
Expand All @@ -184,7 +236,17 @@ export async function renderValue(
else span.appendText(", ");

span.appendText(key + ": ");
await renderValue(value, span, originFile, component, settings, expandList, "list", depth + 1);
await renderValue(
value,
span,
originFile,
component,
settings,
expandList,
"list",
depth + 1,
isInlineFieldLivePreview
);
}
}
} else {
Expand Down
24 changes: 14 additions & 10 deletions src/ui/views/inline-field-live-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import { DataviewSettings } from "settings";
import { selectionAndRangeOverlap } from "ui/lp-render";
import { syntaxTree } from "@codemirror/language";

export class InlineFieldLivePreviewComponent extends Component {}

class InlineFieldValue extends RangeValue {
constructor(public field: InlineField) {
super();
Expand Down Expand Up @@ -69,10 +67,10 @@ export const replaceInlineFieldsInLivePreview = (app: App, settings: DataviewSet
ViewPlugin.fromClass(
class implements PluginValue {
decorations: DecorationSet;
component: InlineFieldLivePreviewComponent;
component: Component;

constructor(view: EditorView) {
this.component = new InlineFieldLivePreviewComponent();
this.component = new Component();
this.component.load();
this.decorations = this.buildDecorations(view);
}
Expand Down Expand Up @@ -208,7 +206,7 @@ class InlineFieldWidget extends WidgetType {
public app: App,
public field: InlineField,
public sourcePath: string,
public parentComponent: Component,
public component: Component,
public settings: DataviewSettings,
public view: EditorView
) {
Expand Down Expand Up @@ -237,7 +235,7 @@ class InlineFieldWidget extends WidgetType {
},
});

renderCompactMarkdown(this.field.key, key, this.sourcePath, this.parentComponent);
renderCompactMarkdown(this.field.key, key, this.sourcePath, this.component, true);

const value = renderContainer.createSpan({
cls: ["dataview", "inline-field-value"],
Expand All @@ -246,9 +244,12 @@ class InlineFieldWidget extends WidgetType {
parseInlineValue(this.field.value),
value,
this.sourcePath,
this.parentComponent,
this.component,
this.settings,
false
false,
undefined,
undefined,
true
);

this.addKeyClickHandler(key, renderContainer);
Expand All @@ -261,9 +262,12 @@ class InlineFieldWidget extends WidgetType {
parseInlineValue(this.field.value),
value,
this.sourcePath,
this.parentComponent,
this.component,
this.settings,
false
false,
undefined,
undefined,
true
);
this.addValueClickHandler(value, renderContainer);
}
Expand Down

0 comments on commit cc4e057

Please sign in to comment.