Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect code block rendering in live preview #2417

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 4 additions & 8 deletions src/ui/views/inline-field-live-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ViewUpdate,
WidgetType,
} from "@codemirror/view";
import { InlineField, extractInlineFields, parseInlineValue } from "data-import/inline-field";
import { extractInlineFields, InlineField, parseInlineValue } from "data-import/inline-field";
import { canonicalizeVarName } from "util/normalize";
import { renderCompactMarkdown, renderValue } from "ui/render";
import { DataviewSettings } from "settings";
Expand Down Expand Up @@ -58,7 +58,7 @@ function buildInlineFields(state: EditorState): RangeSet<InlineFieldValue> {
export const inlineFieldsField = StateField.define<RangeSet<InlineFieldValue>>({
create: buildInlineFields,
update(oldFields, tr) {
return tr.docChanged ? buildInlineFields(tr.state) : oldFields;
return buildInlineFields(tr.state);
},
});

Expand Down Expand Up @@ -128,9 +128,7 @@ export const replaceInlineFieldsInLivePreview = (app: App, settings: DataviewSet
if (update.docChanged) {
this.decorations = this.decorations.map(update.changes);
this.updateDecorations(update.view);
} else if (update.selectionSet) {
this.updateDecorations(update.view);
} else if (update.viewportChanged || layoutChanged) {
} else if (update.selectionSet || update.viewportChanged || layoutChanged) {
this.decorations = this.buildDecorations(update.view);
}
}
Expand Down Expand Up @@ -235,12 +233,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 +257,6 @@ class InlineFieldWidget extends WidgetType {
const value = renderContainer.createSpan({
cls: ["dataview", "inline-field-standalone-value"],
});

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