Skip to content

Commit

Permalink
Merge pull request #2434 from ilandikov/simplify-TaskLineRender
Browse files Browse the repository at this point in the history
refactor: simplify TaskLineRenderer
  • Loading branch information
claremacrae authored Nov 21, 2023
2 parents 7632d98 + 25d8f2b commit 6e3f545
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
16 changes: 8 additions & 8 deletions src/InlineRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,26 @@ export class InlineRenderer {
}
}

const taskLineRenderer = new TaskLineRenderer({
textRenderer: TaskLineRenderer.obsidianMarkdownRenderer,
obsidianComponent: childComponent,
parentUlElement: element,
layoutOptions: new LayoutOptions(),
});

// The section index is the nth task within this section.
for (let sectionIndex = 0; sectionIndex < renderedElements.length; sectionIndex++) {
const task = fileTasks[sectionIndex];
const renderedElement = renderedElements[sectionIndex];

const renderedElement = renderedElements[sectionIndex];
if (task === undefined || renderedElement === undefined) {
// Assuming match of tasks in file and render preview.
// If there is a mis-match in the numbers, we still process
// what we can.
continue;
}

const dataLine: string = renderedElement.getAttr('data-line') ?? '0';
const taskIndex: number = Number.parseInt(dataLine, 10);
const taskLineRenderer = new TaskLineRenderer({
textRenderer: TaskLineRenderer.obsidianMarkdownRenderer,
obsidianComponent: childComponent,
parentUlElement: element,
layoutOptions: new LayoutOptions(),
});
const taskElement = await taskLineRenderer.renderTaskLine(task, taskIndex);

// If the rendered element contains a sub-list or sub-div (e.g. the
Expand Down
18 changes: 9 additions & 9 deletions src/QueryRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,17 @@ class QueryRenderChild extends MarkdownRenderChild {
taskList.addClasses(layout.taskListHiddenClasses);
const groupingAttribute = this.getGroupingAttribute();
if (groupingAttribute && groupingAttribute.length > 0) taskList.dataset.taskGroupBy = groupingAttribute;

const taskLineRenderer = new TaskLineRenderer({
textRenderer: TaskLineRenderer.obsidianMarkdownRenderer,
obsidianComponent: this,
parentUlElement: taskList,
layoutOptions: this.query.layoutOptions,
});

for (const [taskIndex, task] of tasks.entries()) {
const isFilenameUnique = this.isFilenameUnique({ task });

const taskLineRenderer = new TaskLineRenderer({
textRenderer: TaskLineRenderer.obsidianMarkdownRenderer,
obsidianComponent: this,
parentUlElement: taskList,
layoutOptions: this.query.layoutOptions,
isFilenameUnique,
});
const listItem = await taskLineRenderer.renderTaskLine(task, taskIndex);
const listItem = await taskLineRenderer.renderTaskLine(task, taskIndex, isFilenameUnique);

// Remove all footnotes. They don't re-appear in another document.
const footnotes = listItem.querySelectorAll('[data-footnote-id]');
Expand Down
19 changes: 7 additions & 12 deletions src/TaskLineRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ export class TaskLineRenderer {
obsidianComponent: Component | null;
parentUlElement: HTMLElement;
layoutOptions: LayoutOptions;
isFilenameUnique?: boolean;

static async obsidianMarkdownRenderer(
text: string,
Expand All @@ -198,29 +197,22 @@ export class TaskLineRenderer {
* @param parentUlElement HTML element where the task shall be rendered.
*
* @param layoutOptions See {@link LayoutOptions}.
*
* @param isFilenameUnique Whether the name of the file that contains the task is unique in the vault.
* If it is undefined, the outcome will be the same as with a unique file name: the file name only.
* If set to `true`, the full path will be returned.
*/
constructor({
textRenderer,
obsidianComponent,
parentUlElement,
layoutOptions,
isFilenameUnique,
}: {
textRenderer: TextRenderer;
obsidianComponent: Component | null;
parentUlElement: HTMLElement;
layoutOptions: LayoutOptions;
isFilenameUnique?: boolean;
}) {
this.textRenderer = textRenderer;
this.obsidianComponent = obsidianComponent;
this.parentUlElement = parentUlElement;
this.layoutOptions = layoutOptions;
this.isFilenameUnique = isFilenameUnique;
}

/**
Expand All @@ -235,8 +227,11 @@ export class TaskLineRenderer {
* @note Output is based on the {@link DefaultTaskSerializer}'s format, with default (emoji) symbols
* @param task The task to be rendered.
* @param taskIndex Task's index in the list. This affects `data-line` data attributes of the list item.
* @param isFilenameUnique Whether the name of the file that contains the task is unique in the vault.
* If it is undefined, the outcome will be the same as with a unique file name:
* the file name only. If set to `true`, the full path will be returned.
*/
public async renderTaskLine(task: Task, taskIndex: number): Promise<HTMLLIElement> {
public async renderTaskLine(task: Task, taskIndex: number, isFilenameUnique?: boolean): Promise<HTMLLIElement> {
const li: HTMLLIElement = document.createElement('li');
this.parentUlElement.appendChild(li);

Expand Down Expand Up @@ -289,7 +284,7 @@ export class TaskLineRenderer {
checkbox.setAttribute('data-line', taskIndex.toString());

if (this.layoutOptions.shortMode) {
this.addTooltip(task, textSpan);
this.addTooltip(task, textSpan, isFilenameUnique);
}

return li;
Expand Down Expand Up @@ -435,7 +430,7 @@ export class TaskLineRenderer {
}
}

private addTooltip(task: Task, element: HTMLSpanElement) {
private addTooltip(task: Task, element: HTMLSpanElement, isFilenameUnique: boolean | undefined) {
const {
recurrenceSymbol,
startDateSymbol,
Expand Down Expand Up @@ -478,7 +473,7 @@ export class TaskLineRenderer {
addDateToTooltip(tooltip, task.dueDate, dueDateSymbol);
addDateToTooltip(tooltip, task.doneDate, doneDateSymbol);

const linkText = task.getLinkText({ isFilenameUnique: this.isFilenameUnique });
const linkText = task.getLinkText({ isFilenameUnique });
if (linkText) {
const backlinkDiv = tooltip.createDiv();
backlinkDiv.setText(`🔗 ${linkText}`);
Expand Down

0 comments on commit 6e3f545

Please sign in to comment.