Skip to content

Commit

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

This reverts commit 6a6e025.

* Fix inline rendering for lists in live preview

* fix formatting
  • Loading branch information
reply2za authored Feb 22, 2025
1 parent 5ac0424 commit 41c8f3a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
54 changes: 43 additions & 11 deletions src/ui/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,45 @@ export async function renderCompactMarkdown(
markdown: string,
container: HTMLElement,
sourcePath: string,
component: Component
component: Component,
isInlineFieldLivePreview: boolean = false
) {
let subcontainer = container.createSpan();
await MarkdownRenderer.render(app, markdown, subcontainer, sourcePath, component);
// 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);
let paragraph = subcontainer.querySelector(":scope > p");
if (subcontainer.children.length == 1 && paragraph) {
while (paragraph.firstChild) {
subcontainer.appendChild(paragraph.firstChild);
}
subcontainer.removeChild(paragraph);
}
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 paragraph = tmpContainer.querySelector(":scope > p");
if (tmpContainer.childNodes.length == 1 && paragraph) {
container.appendChild(paragraph.childNodes.item(paragraph.childNodes.length - 1));
} else {
container.replaceChildren(...tmpContainer.childNodes);
}

tmpContainer.remove();
}

/** Render a pre block with an error in it; returns the element to allow for dynamic updating. */
export function renderErrorPre(container: HTMLElement, error: string): HTMLElement {
let pre = container.createEl("pre", { cls: ["dataview", "dataview-error"] });
Expand Down Expand Up @@ -62,15 +87,22 @@ export async function renderValue(
}

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

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

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

renderValue(
this.app,
parseInlineValue(this.field.value),
Expand All @@ -260,7 +259,6 @@ 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 41c8f3a

Please sign in to comment.