Skip to content

Commit

Permalink
Fix inline list rendering in live preview (#2387)
Browse files Browse the repository at this point in the history
* fix list rendering in live preview

* style: format code

* remove .DS_Store
  • Loading branch information
reply2za authored Jul 16, 2024
1 parent 15b8d52 commit 6a6e025
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 45 deletions.
55 changes: 11 additions & 44 deletions src/ui/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,22 @@ import { Literal, Values, Widgets } from "data-model/value";

/** Render simple fields compactly, removing wrapping content like paragraph and span. */
export async function renderCompactMarkdown(
app: App,
markdown: string,
container: HTMLElement,
sourcePath: string,
component: Component,
isInlineFieldLivePreview: boolean = false
) {
// check if the call is from the CM6 view plugin defined in src/ui/views/inline-field-live-preview.ts
if (isInlineFieldLivePreview) {
await renderCompactMarkdownForInlineFieldLivePreview(app, markdown, container, sourcePath, component);
} else {
let subcontainer = container.createSpan();
await MarkdownRenderer.render(app, markdown, subcontainer, sourcePath, component);

let paragraph = subcontainer.querySelector(":scope > p");
if (subcontainer.children.length == 1 && paragraph) {
while (paragraph.firstChild) {
subcontainer.appendChild(paragraph.firstChild);
}
subcontainer.removeChild(paragraph);
}
}
}

async function renderCompactMarkdownForInlineFieldLivePreview(
app: App,
markdown: string,
container: HTMLElement,
sourcePath: string,
component: Component
) {
const tmpContainer = createSpan();
await MarkdownRenderer.render(app, markdown, tmpContainer, sourcePath, component);
let subcontainer = container.createSpan();
await MarkdownRenderer.render(app, markdown, subcontainer, sourcePath, component);

let paragraph = tmpContainer.querySelector(":scope > p");
if (tmpContainer.childNodes.length == 1 && paragraph) {
container.replaceChildren(...paragraph.childNodes);
} else {
container.replaceChildren(...tmpContainer.childNodes);
let paragraph = subcontainer.querySelector(":scope > p");
if (subcontainer.children.length == 1 && paragraph) {
while (paragraph.firstChild) {
subcontainer.appendChild(paragraph.firstChild);
}
subcontainer.removeChild(paragraph);
}

tmpContainer.remove();
}

/** Render a pre block with an error in it; returns the element to allow for dynamic updating. */
Expand Down Expand Up @@ -88,22 +62,15 @@ export async function renderValue(
}

if (Values.isNull(field)) {
await renderCompactMarkdown(
app,
settings.renderNullAs,
container,
originFile,
component,
isInlineFieldLivePreview
);
await renderCompactMarkdown(app, settings.renderNullAs, container, originFile, component);
} 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(app, "" + field, container, originFile, component, isInlineFieldLivePreview);
await renderCompactMarkdown(app, "" + field, container, originFile, component);
} else if (Values.isLink(field)) {
await renderCompactMarkdown(app, field.markdown(), container, originFile, component, isInlineFieldLivePreview);
await renderCompactMarkdown(app, field.markdown(), container, originFile, component);
} else if (Values.isHtml(field)) {
container.appendChild(field);
} else if (Values.isWidget(field)) {
Expand Down
4 changes: 3 additions & 1 deletion src/ui/views/inline-field-live-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,12 @@ class InlineFieldWidget extends WidgetType {
},
});

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

const value = renderContainer.createSpan({
cls: ["dataview", "inline-field-value"],
});

renderValue(
this.app,
parseInlineValue(this.field.value),
Expand All @@ -259,6 +260,7 @@ class InlineFieldWidget extends WidgetType {
const value = renderContainer.createSpan({
cls: ["dataview", "inline-field-standalone-value"],
});

renderValue(
this.app,
parseInlineValue(this.field.value),
Expand Down

0 comments on commit 6a6e025

Please sign in to comment.