All files / src/libs/BlockRenderComponents HeaderBlockRenderComponent.ts

0% Statements 0/157
0% Branches 0/115
0% Functions 0/48
0% Lines 0/150

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import { App, FrontMatterCache, MarkdownRenderer, TFile } from 'obsidian';
import API from 'src/classes/API';
import Lng from 'src/classes/Lng';
import type { ILogger, ILogger_ } from 'src/interfaces/ILogger';
import type IMetadataCache from 'src/interfaces/IMetadataCache';
import { IProcessorSettings } from 'src/interfaces/IProcessorSettings';
import { IPrjTaskManagementData } from 'src/models/Data/interfaces/IPrjTaskManagementData';
import PrjBaseData from 'src/models/Data/PrjBaseData';
import { PrjTaskManagementModel } from 'src/models/PrjTaskManagementModel';
import RedrawableBlockRenderComponent from './RedrawableBlockRenderComponent';
import CustomizableRenderChild from '../CustomizableRenderChild/CustomizableRenderChild';
import { Inject } from '../DependencyInjection/decorators/Inject';
import EditableDataView from '../EditableDataView/EditableDataView';
import type { IHelperObsidian } from '../Helper/interfaces/IHelperObsidian';
import { StatusTypes } from '../StatusType/interfaces/IStatusType';
import { ITags } from '../Tags/interfaces/ITags';
import { Tag } from '../Tags/Tag';
import { Tags } from '../Tags/Tags';
import { TagTree } from '../Tags/types/TagTree';
 
/**
 * Header Block Render Component class.
 * Renders the header of the Prj File with
 * - the title,
 * - the status,
 * - the tags as a tag tree
 * - and the description.
 * @remarks This header watches the `prj-task-management-file-changed` event
 * and redraws the header when the event is fired and the file is the file in which the block is located.
 */
export default class HeaderBlockRenderComponent
    implements RedrawableBlockRenderComponent
{
    @Inject('IApp')
    // eslint-disable-next-line @typescript-eslint/naming-convention
    private readonly _IApp!: App;
    @Inject('IHelperObsidian')
    private readonly _IHelperObsidian!: IHelperObsidian;
    @Inject(
        'ILogger_',
        (x: ILogger_) => x.getLogger('HeaderBlockRenderComponent'),
        false,
    )
    private readonly _logger?: ILogger;
    @Inject('IMetadataCache')
    private readonly _IMetadataCache!: IMetadataCache;
    private _model:
        | PrjTaskManagementModel<IPrjTaskManagementData & PrjBaseData<unknown>>
        | undefined;
 
    private readonly _processorSettings: IProcessorSettings;
    private readonly _childComponent: CustomizableRenderChild;
    private _activeFileDebounceTimer: NodeJS.Timeout;
 
    private _headerContainer: HTMLElement | undefined;
 
    /**
     * The path of the file in which the block is located.
     */
    private get path(): string {
        return this._processorSettings.source;
    }
 
    /**
     * Sets the path value.
     * @param value - The new path value.
     */
    private set path(value: string) {
        this._processorSettings.source = value;
        this._model = undefined;
    }
 
    /**
     * The container of the block.
     */
    private get container(): HTMLElement {
        return this._processorSettings.container;
    }
 
    /**
     * The header container of this HeaderBlockRenderComponent.
     * @remarks - This container is used to append the title, status and tags.
     * - If the container is not set, the function creates a new container.
     */
    private get headerContainer(): HTMLElement {
        Iif (!this._headerContainer) {
            this._headerContainer = document.createElement('div');
            this._headerContainer.addClass('header-block-component');
        }
 
        return this._headerContainer;
    }
 
    /**
     * The component of this HeaderBlockRenderComponent.
     */
    private get component(): CustomizableRenderChild {
        return this._childComponent;
    }
 
    /**
     * The title of the Prj File.
     */
    private get title(): string | undefined {
        return this.model?.data.title ?? undefined;
    }
 
    /**
     * Sets the title of the Prj File.
     */
    private set title(value: string | null | undefined) {
        Iif (this.model) this.model.data.title = value;
    }
 
    /**
     * The status of the Prj File.
     */
    private get status(): StatusTypes | undefined {
        return this.model?.data.status?.value ?? undefined;
    }
 
    /**
     * Sets the status of the Prj File.
     */
    private set status(value: StatusTypes | undefined) {
        Iif (this.model) this.model.changeStatus(value);
    }
 
    /**
     * The description of the Prj File.
     */
    private get description(): string | undefined {
        return this.model?.data.description ?? undefined;
    }
 
    /**
     * Sets the description of the Prj File.
     */
    private set description(value: string | null | undefined) {
        Iif (this.model) this.model.data.description = value;
    }
 
    /**
     * The model of the Prj File.
     */
    private get model():
        | PrjTaskManagementModel<IPrjTaskManagementData & PrjBaseData<unknown>>
        | undefined {
        Iif (this._model) return this._model;
 
        Iif (!this.file) return undefined;
 
        this._model = API.prjTaskManagementModel.getCorospondingModel(
            this.file,
        );
 
        return this._model;
    }
 
    /**
     * Sets the model of the Prj File.
     * @remarks This function is used to set the model to undefined.
     */
    private set model(
        value:
            | PrjTaskManagementModel<
                  IPrjTaskManagementData & PrjBaseData<unknown>
              >
            | undefined,
    ) {
        this._model = value;
    }
 
    /**
     * The file in which the block is located.
     */
    private get file(): TFile | undefined {
        return this._IMetadataCache.getEntryByPath(this.path)?.file;
    }
 
    /**
     * The frontmatter of the Prj File.
     */
    private get frontmatter(): FrontMatterCache | undefined {
        return (
            this._IMetadataCache.getEntryByPath(this.path)?.metadata
                ?.frontmatter ?? undefined
        );
    }
 
    /**
     * The tags of the Prj File.
     */
    private get tags(): ITags {
        return new Tags(this.frontmatter?.tags);
    }
 
    /**
     * Initializes a new instance of the HeaderBlockRenderComponent.
     * @param settings The settings of the processor.
     */
    constructor(settings: IProcessorSettings) {
        this._processorSettings = settings;
        this.onUnload = this.onUnload.bind(this);
        this.redraw = this.redraw.bind(this);
 
        this.onDocumentChangedMetadata =
            this.onDocumentChangedMetadata.bind(this);
 
        this.onPathChanged = this.onPathChanged.bind(this);
 
        this._childComponent = new CustomizableRenderChild(
            this.container,
            () => this.onLoad(),
            () => this.onUnload(),
            this._logger,
        );
        this._childComponent.load();
        this._processorSettings.ctx.addChild(this._childComponent);
        this.parseSettings();
    }
 
    /**
     * The `onLoad` function which is linked to the `onload` event in the `CustomizableRenderChild` class.
     * @remarks This function is called when the block is loaded and register the `prj-task-management-file-changed` event.
     */
    private onLoad(): void {
        this._IMetadataCache.on(
            'prj-task-management-file-changed-event',
            this.onDocumentChangedMetadata,
        );
 
        this._IMetadataCache.on('file-rename-event', this.onPathChanged);
    }
 
    /**
     * The `onUnload` function which is linked to the `onunload` event in the `CustomizableRenderChild` class.
     * @remarks This function is called when the block is unloaded and unregister the `prj-task-management-file-changed` event.
     */
    private onUnload(): void {
        this._IMetadataCache.off(
            'prj-task-management-file-changed-event',
            this.onDocumentChangedMetadata,
        );
 
        this._IMetadataCache.off('file-rename-event', this.onPathChanged);
    }
 
    /**
     * Redraws the HeaderBlockRenderComponent.
     * @remarks - This function is called when the `prj-task-management-file-changed` event is fired.
     * - The function emptys the `headerContainer`, clear the stored `model` and calls the `build` function.
     */
    public async redraw(): Promise<void> {
        try {
            this.headerContainer?.empty();
            this.model = undefined;
            await this.build();
        } catch (error) {
            this._logger?.error(
                `Error while redrawing HeaderBlockRenderComponent: ${error}`,
            );
        }
    }
 
    /**
     * The `prj-task-management-file-changed` event handler.
     * @param file The file which has changed.
     * @remarks - This function is called when the `prj-task-management-file-changed` event is fired.
     * - The function checks if the file is the file in which the block is located and calls the `redraw` function.
     */
    private onDocumentChangedMetadata(file: TFile): void {
        Iif (file.path === this.path) {
            this.redraw();
        }
    }
 
    /**
     * The `file-rename-event` event handler.
     * @param file Contains `{ oldPath: string; newPath: string }` of the file which has changed.
     * @param file.oldPath The old path of the file.
     * @param file.newPath The new path of the file.
     */
    private onPathChanged(file: { oldPath: string; newPath: string }): void {
        Iif (file.oldPath === this.path) {
            this.path = file.newPath;
            this.redraw();
        }
    }
 
    /**
     * Builds the HeaderBlockRenderComponent.
     */
    public async build(): Promise<void> {
        try {
            Iif (this.title) this.headerContainer.append(this.createTitle());
 
            Iif (this.status) this.headerContainer.append(this.createStatus());
 
            Iif (this.tags.length > 0)
                this.headerContainer.append(this.createTags());
 
            Iif (this.description)
                this.headerContainer.append(this.createDescription());
 
            Iif (this.headerContainer.childElementCount !== 0)
                this.headerContainer.append(this.createSeparatorLine());
 
            this.container.append(this.headerContainer);
        } catch (error) {
            this._logger?.error(
                `Error while building HeaderBlockRenderComponent: ${error}`,
            );
        }
    }
 
    /**
     * Creates a separator line as a DocumentFragment.
     * @returns The created separator line as a DocumentFragment.
     */
    private createSeparatorLine(): DocumentFragment {
        const separatorLineDiv = document.createElement('div');
 
        MarkdownRenderer.render(
            this._IApp,
            `---`,
            separatorLineDiv,
            this.path,
            this.component,
        );
 
        return this.createDocumentFragment(separatorLineDiv);
    }
 
    /**
     * Creates the title of the Prj File.
     * @returns The title of the Prj File.
     */
    private createTitle(): DocumentFragment {
        const titleDiv = document.createElement('div');
        titleDiv.classList.add('title');
 
        new EditableDataView(titleDiv, this._childComponent).addText((text) => {
            text.setValue(this.title ?? '')
                .setTitle(Lng.gt('Title'))
                .setPlaceholder(Lng.gt('Title'))
                .enableEditability()
                .setRenderMarkdown()
                .onSave((value: string) => {
                    this.title = value;
 
                    return Promise.resolve();
                });
        });
 
        return this.createDocumentFragment(titleDiv);
    }
 
    /**
     * Creates the status of the Prj File.
     * @returns The status of the Prj File.
     */
    private createStatus(): DocumentFragment {
        const statusDiv = document.createElement('div');
        statusDiv.classList.add('status');
 
        const statusLabel = document.createElement('p');
        statusLabel.classList.add('status-label');
        statusLabel.innerText = `${Lng.gt('Status')}: `;
 
        new EditableDataView(statusDiv, this._childComponent).addDropdown(
            (dropdown) => {
                dropdown
                    .setOptions([
                        { value: 'Active', text: Lng.gt('StatusActive') },
                        { value: 'Waiting', text: Lng.gt('StatusWaiting') },
                        { value: 'Later', text: Lng.gt('StatusLater') },
                        { value: 'Someday', text: Lng.gt('StatusSomeday') },
                        { value: 'Done', text: Lng.gt('StatusDone') },
                    ])
                    .setTitle(Lng.gt('Status'))
                    .setValue(this.status ?? '')
                    .onSave(async (value) => {
                        this.status = value as StatusTypes;
                    })
                    .enableEditability()
                    .setFormator((value: string) => {
                        const status = Lng.gt(`Status${value}`);
 
                        return { text: `${status}`, html: undefined };
                    })
                    .then((value) => {
                        // Add status label
                        value.firstChild
                            ? value.insertBefore(statusLabel, value.firstChild)
                            : value.appendChild(statusLabel);
 
                        // Find presentation span (.textarea-presentation)
                        // and add class cm-strong (bold)
                        const presentationSpan =
                            value.querySelector('.text-presentation');
                        presentationSpan?.addClass('cm-strong');
                    });
            },
        );
 
        return this.createDocumentFragment(statusDiv);
    }
 
    /**
     * Creates a document fragment for the description.
     * @returns The created document fragment.
     */
    private createDescription(): DocumentFragment {
        const descriptionDiv = document.createElement('div');
        descriptionDiv.classList.add('description');
 
        new EditableDataView(descriptionDiv, this._childComponent).addTextarea(
            (textarea) => {
                textarea
                    .setValue(this.description ?? '')
                    .setTitle(Lng.gt('Description'))
                    .setPlaceholder(Lng.gt('Description'))
                    .enableEditability()
                    .setRenderMarkdown()
                    .onSave((value: string) => {
                        this.description = value;
 
                        return Promise.resolve();
                    });
            },
        );
 
        return this.createDocumentFragment(descriptionDiv);
    }
 
    /**
     * Creates the tags of the Prj File as a tag tree.
     * @param tagTree The tag tree to create the tags from.
     * @param path The path of the tag. On first call, this parameter is not needed.
     * @returns The tags of the Prj File as a tag tree.
     */
    private createDomList(tagTree: TagTree, path = ''): HTMLElement {
        const ul = document.createElement('ul');
 
        for (const tag in tagTree) {
            const fullPath = path ? `${path}/${tag}` : tag;
            const li = document.createElement('li');
 
            const tagObject = new Tag(fullPath);
 
            const tagLink = tagObject.getObsidianLink(
                path ? tagObject.getElements().last() : tagObject.tagWithHash,
            );
 
            li.appendChild(tagLink);
            const subTags = tagTree[tag];
            const hasSubTags = Object.keys(subTags).length > 0;
 
            Iif (hasSubTags) {
                li.appendChild(this.createDomList(subTags, fullPath));
            }
 
            ul.appendChild(li);
        }
 
        return ul;
    }
 
    /**
     * Creates the tags of the Prj File.
     * @returns The tags of the Prj File.
     */
    private createTags(): DocumentFragment {
        const tagsDiv = document.createElement('div');
        tagsDiv.classList.add('tag-tree');
 
        const labelDiv = document.createElement('div');
        tagsDiv.appendChild(labelDiv);
        labelDiv.classList.add('tag-label');
        labelDiv.textContent = `${Lng.gt('Tags')}:`;
 
        const tagTree = this.tags.getTagTree();
        const tagsList = this.createDomList(tagTree);
        tagsDiv.appendChild(tagsList);
 
        return this.createDocumentFragment(tagsDiv);
    }
 
    /**
     * Creates a document fragment with the given element as a child.
     * @param element The element to append to the document fragment.
     * @returns The document fragment with the given element as a child.
     */
    private createDocumentFragment(element: HTMLElement): DocumentFragment {
        const documentFragment = new DocumentFragment();
        documentFragment.append(element);
 
        return documentFragment;
    }
 
    /**
     * Parses the settings of the block.
     * @remarks This function is called in the constructor.
     */
    private parseSettings(): void {
        Iif (
            !this._processorSettings.options ||
            this._processorSettings.options.length === 0
        )
            return;
 
        this._processorSettings.options.forEach((option) => {
            switch (option.label) {
                case 'watchActiveFile':
                    Iif (option.value === 'true') {
                        // Register event to update the header when the active file changes
                        this.component.registerEvent(
                            this._IApp.workspace.on('active-leaf-change', () =>
                                this.onActiveFileChange.bind(this)(),
                            ),
                        );
                    }
                    break;
                default:
                    break;
            }
        });
    }
 
    /**
     * Handles the event when the active file changes.
     * If the active file is not in the "Ressourcen/Panels/" path,
     * it updates the `path` in the instance and calls the `onActiveFileDebounce` function.
     * @private
     */
    private onActiveFileChange(): void {
        const activeFile = this._IHelperObsidian.getActiveFile();
 
        Iif (activeFile && !activeFile.path.contains('Ressourcen/Panels/')) {
            this._logger?.trace('Active file changed: ', activeFile.path);
 
            Iif (this.path !== activeFile.path) {
                this.path = activeFile.path;
                this.onActiveFileDebounce();
            }
        }
    }
 
    /**
     * Debounces the active file change event and triggers a redraw after a delay.
     */
    private onActiveFileDebounce(): void {
        this._logger?.trace('Active file changed: Debouncing');
        clearTimeout(this._activeFileDebounceTimer);
 
        this._activeFileDebounceTimer = setTimeout(() => {
            this.redraw();
        }, 750);
    }
}
 
Zur TypeDoc-Dokumentation