All files / src/libs/BlockRenderComponents/InnerComponents ProjectComponents.ts

0% Statements 0/84
0% Branches 0/30
0% Functions 0/21
0% Lines 0/84

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import { Component, MarkdownRenderer, setIcon } from 'obsidian';
import Lng from 'src/classes/Lng';
import { IApp } from 'src/interfaces/IApp';
import { Resolve } from 'src/libs/DependencyInjection/functions/Resolve';
import EditableDataView from 'src/libs/EditableDataView/EditableDataView';
import { FileTypes } from 'src/libs/FileType/interfaces/IFileType';
import { HelperGeneral } from 'src/libs/Helper/General';
import { StatusTypes } from 'src/libs/StatusType/interfaces/IStatusType';
import { PrjTaskManagementModel } from 'src/models/PrjTaskManagementModel';
import { UrgencySymbols } from 'src/types/PrjTypes';
 
/**
 * Represents the project components.
 */
export default class ProjectComponents {
    /**
     * Creates a title component.
     * @param container The container to append the title to.
     * @param component The component to register the events to.
     * @param path The path to the file.
     * @param onRead On read callback. Returns the title.
     * @param onWrite On write callback. Passes the new title.
     */
    public static createTitle(
        container: DocumentFragment,
        component: Component,
        path: string,
        onRead: () => string,
        onWrite: (value: string) => void,
    ): void {
        new EditableDataView(container, component).addLink((link) => {
            link.setValue(onRead())
                .setTitle(Lng.gt('Title'))
                .setPlaceholder(Lng.gt('Title'))
                .setLinkType('file')
                .setFormator((value: string) => {
                    let title: DocumentFragment | undefined =
                        document.createDocumentFragment();
 
                    if (value === '') {
                        setIcon(
                            title as unknown as HTMLDivElement,
                            'paperclip',
                        );
                    } else if (HelperGeneral.containsMarkdown(value)) {
                        const div = document.createElement('div');
 
                        MarkdownRenderer.render(
                            Resolve<IApp>('IApp'),
                            value ?? '',
                            div,
                            '',
                            component,
                        );
                        title.appendChild(div);
                    } else {
                        title = undefined;
                    }
 
                    return { href: `${path}`, text: `${value}`, html: title };
                })
                .enableEditability()
                .onSave((value: string) => {
                    onWrite(value);
 
                    return Promise.resolve();
                });
        });
    }
 
    /**
     * Creates a summary component.
     * @param container The container to append the summary to.
     * @param component The component to register the events to.
     * @param description The description to display.
     * @param onWrite On write callback. Passes the new description.
     */
    public static createSummary(
        container: DocumentFragment,
        component: Component,
        description: string,
        onWrite: (value: string) => void,
    ): void {
        new EditableDataView(container, component).addTextarea((text) => {
            text.setValue(description)
                .setTitle(Lng.gt('Description'))
                .setPlaceholder(Lng.gt('Description'))
                .enableEditability()
                .setRenderMarkdown()
                .onSave((value: string) => {
                    onWrite(value);
 
                    return Promise.resolve();
                });
        });
    }
 
    /**
     * Creates a status component.
     * @param container The container to append the status to.
     * @param component The component to register the events to.
     * @param onRead On read callback. Should return the status.
     * @param onWrite On write callback. Should write the new status to the file.
     */
    public static createStatus(
        container: DocumentFragment,
        component: Component,
        onRead: () => string,
        onWrite: (value: string) => void,
    ): void {
        new EditableDataView(container, component).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(onRead())
                .onSave(async (value) => {
                    onWrite(value);
                })
                .enableEditability()
                .setFormator((value: string) => {
                    const status = value as StatusTypes;
                    let iconString: string;
 
                    switch (status) {
                        case 'Active':
                            iconString = '⚡';
                            break;
                        case 'Waiting':
                            iconString = '⏳';
                            break;
                        case 'Later':
                            iconString = '🔜';
                            break;
                        case 'Someday':
                            iconString = '📆';
                            break;
                        case 'Done':
                            iconString = '✔️';
                            break;
                        default:
                            iconString = '⚡';
                            break;
                    }
 
                    return { text: `${iconString}`, html: undefined };
                });
        });
    }
 
    /**
     * Creates a priority component.
     * @param container The container to append the priority to.
     * @param component The component to register the events to.
     * @param onRead On read callback. Returns the priority.
     * @param onWrite On write callback. Passes the new priority.
     */
    public static createPriority(
        container: DocumentFragment,
        component: Component,
        onRead: () => string,
        onWrite: (value: string) => void,
    ): void {
        new EditableDataView(container, component).addDropdown((dropdown) => {
            dropdown
                .setOptions([
                    { value: '3', text: Lng.gt('HighPriority') },
                    { value: '2', text: Lng.gt('MediumPriority') },
                    { value: '1', text: Lng.gt('LowPriority') },
                    { value: '0', text: Lng.gt('NoPriority') },
                ])
                .setTitle(Lng.gt('PriorityText'))
                .setValue(onRead())
                .onSave(async (value) => {
                    onWrite(value);
                })
                .enableEditability()
                .setFormator((value: string) => {
                    const icon = document.createDocumentFragment();
                    let iconString: string;
 
                    switch (value) {
                        case '3':
                            iconString = 'signal';
                            break;
                        case '2':
                            iconString = 'signal-medium';
                            break;
                        case '1':
                            iconString = 'signal-low';
                            break;
                        case '0':
                            iconString = 'signal-zero';
                            break;
                        default:
                            iconString = 'signal-zero';
                            break;
                    }
                    setIcon(icon as unknown as HTMLDivElement, iconString);
 
                    return { text: `${value}`, html: icon };
                });
        });
    }
 
    /**
     * Creates a link to the file at `path` with the `corospondingSymbol` as icon.
     * @param container The container to append the link to.
     * @param component The component to register the events to.
     * @param path The path to the file.
     * @param type The type of the file.
     * @param corospondingSymbol The corosponding symbol for the file type.
     */
    public static createMetadataLink(
        container: DocumentFragment,
        component: Component,
        path: string,
        type: FileTypes | undefined | null,
        corospondingSymbol: string,
    ): void {
        new EditableDataView(container, component).addLink((link) => {
            link.setValue(path)
                .setTitle(Lng.gt(type ?? 'File'))
                .setLinkType('file')
                .setFormator((value: string) => {
                    const icon = document.createDocumentFragment();
                    const iconString = corospondingSymbol;
                    setIcon(icon as unknown as HTMLDivElement, iconString);
 
                    return { href: `${value}`, text: `${value}`, html: icon };
                });
        });
    }
 
    /**
     * Creates a span with the to `urgency` corresponding urgency symbol.
     * @param container The container to append the span to.
     * @param urgency The urgency to get the symbol for. (3 to -2)
     * @see {@link PrjTaskManagementModel.calculateUrgency}
     * @see {@link UrgencySymbols}
     */
    public static createTraficLight(
        container: DocumentFragment,
        urgency: number,
    ): void {
        const traficLightSpan = document.createElement('span');
        container.appendChild(traficLightSpan);
        let iconString: UrgencySymbols;
 
        switch (urgency) {
            case 3:
                iconString = '🔴';
                break;
            case 2:
                iconString = '🟠';
                break;
            case 1:
                iconString = '🟡';
                break;
            case 0:
                iconString = '🟢';
                break;
            case -1:
                iconString = '🟢';
                break;
            case -2:
                iconString = '🔵';
                break;
            default:
                iconString = '🔴';
                break;
        }
        traficLightSpan.textContent = iconString;
    }
}
 
Zur TypeDoc-Dokumentation