All files / src/libs/EditableDataView/Components TextareaComponent.ts

0% Statements 0/70
0% Branches 0/25
0% Functions 0/17
0% Lines 0/69

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import { Component, MarkdownRenderer } from 'obsidian';
import { IApp } from 'src/interfaces/IApp';
import { Resolve } from 'src/libs/DependencyInjection/functions/Resolve';
import { HelperGeneral } from 'src/libs/Helper/General';
import BaseComponent from './BaseComponent';
 
/**
 * Represents a textarea component for editable data view.
 */
export default class TextareaComponent extends BaseComponent {
    //#region base properties
    protected _isEditable = false;
    onEnableEditCallback: () => void;
    onDisableEditCallback: () => void;
    onSaveCallback: () => Promise<void>;
    onFirstEdit: () => void;
    onFinalize: () => void;
    //#endregion
    //#region extended properties
    private _onPresentation: ((value: string) => Promise<void>) | undefined;
    private _onMarkdownPresentation:
        | ((value: string) => Promise<void>)
        | undefined;
    private _onSave: ((value: string) => Promise<void>) | undefined;
    private _value: string;
    private _placeholder: string;
    private _title: string;
    //#endregion
    //#region HTML Elements
    private _presentationSpan: HTMLElement;
    //#endregion
 
    /**
     * Creates a new instance of TextareaComponent.
     * @param component The component to attach the textarea to.
     */
    constructor(component: Component) {
        super(component);
        this.onFinalize = this.build;
        this.onFirstEdit = this.buildInput;
        this.onEnableEditCallback = this.enableEdit;
        this.onSaveCallback = this.save;
        this.onDisableEditCallback = this.disableEdit;
    }
 
    //#region Configuration methods
    /**
     * Enables the editability of the component.
     * @returns The component itself.
     */
    public enableEditability(): this {
        this._isEditable = true;
 
        return this;
    }
 
    /**
     * Sets the value of the component.
     * @param value The value to set.
     * @returns The component itself.
     */
    public setValue(value: string): this {
        this._value = value;
 
        return this;
    }
 
    /**
     * Sets the placeholder of the input element.
     * @param placeholder The placeholder to set.
     * @returns The component itself.
     */
    public setPlaceholder(placeholder: string): this {
        this._placeholder = placeholder;
 
        return this;
    }
 
    /**
     * Sets the title of the component.
     * @param title The title to set.
     * @returns The component itself.
     */
    public setTitle(title: string): this {
        this._title = title;
 
        return this;
    }
 
    /**
     * Sets the formator of the component.
     * @param formator The formator to set.
     * @returns The component itself.
     * @remarks The formator is called when the component changes in `not-edit` mode.
     */
    public setFormator(formator: (value: string) => Promise<string>): this {
        /**
         * Sets the presentation of the component using the specified formator.
         * @param value The value to format and set as the presentation.
         */
        this._onPresentation = async (value: string): Promise<void> => {
            this._presentationSpan.textContent = await formator(this._value);
        };
 
        return this;
    }
 
    /**
     * Sets the markdown formator of the component.
     * @param path The path of the file to resolve internal links.
     * @returns The component itself.
     * @remarks The formator is called when the component changes in `not-edit` mode.
     * - The custom formator is ignored if this method is called!
     */
    public setRenderMarkdown(path = ''): this {
        /**
         * Sets the presentation of the component using the specified markdown formator.
         * @param value The value to format and set as the presentation.
         * @returns A promise that resolves when the presentation is set.
         */
        this._onMarkdownPresentation = (value: string): Promise<void> => {
            if (HelperGeneral.containsMarkdown(value)) {
                return MarkdownRenderer.render(
                    Resolve<IApp>('IApp'),
                    value,
                    this._presentationSpan,
                    path,
                    this._component,
                );
            } else {
                this._presentationSpan.innerHTML = '';
                this._presentationSpan.textContent = value;
 
                return Promise.resolve();
            }
        };
 
        return this;
    }
 
    /**
     * Sets the saver of the component.
     * @param callback The saver to set.
     * @returns The component itself.
     * @remarks The saver is called when the component save button is clicked.
     */
    public onSave(callback: (value: string) => Promise<void>): this {
        this._onSave = callback;
 
        return this;
    }
    //#endregion
 
    //#region Base Callbacks
    /**
     * Builds the presentation of the component.
     */
    private build(): void {
        this._presentationSpan = document.createElement('span');
        this._presentationContainer.appendChild(this._presentationSpan);
 
        this._presentationSpan.contentEditable = 'false';
        this._presentationSpan.title = this._title;
        this._presentationSpan.classList.add('editable-data-view');
        this._presentationSpan.classList.add('textarea-presentation');
 
        if (this._onMarkdownPresentation) {
            this._presentationSpan.textContent = null;
            this._onMarkdownPresentation(this._value);
        } else if (this._onPresentation) {
            this._onPresentation(this._value);
        } else {
            this._presentationSpan.textContent = this._value;
        }
    }
 
    /**
     * Builds the input element for editing.
     */
    private buildInput(): void {
        this._component.registerDomEvent(
            this._presentationSpan,
            'keydown',
            (event: KeyboardEvent) => {
                if (event.key === 'Escape') {
                    this.disableEditMode();
                } else Iif (event.key === 'Enter') {
                    event.preventDefault();
                    const selection = window.getSelection();
 
                    Iif (selection && selection.rangeCount > 0) {
                        const range = selection.getRangeAt(0);
                        range.deleteContents();
                        range.insertNode(document.createTextNode('\n'));
                        range.collapse(false);
                    }
                }
            },
        );
    }
 
    /**
     * Enables the edit mode of the component.
     */
    private enableEdit(): void {
        this._presentationContainer.classList.remove('hidden');
        this._presentationSpan.textContent = this._value;
        this._presentationSpan.contentEditable = 'true';
        this._presentationSpan.focus();
        this.setInputCursorAbsolutePosition('end', this._presentationSpan);
    }
 
    /**
     * Disables the edit mode of the component.
     */
    private disableEdit(): void {
        if (this._onMarkdownPresentation) {
            this._presentationSpan.textContent = null;
            this._onMarkdownPresentation(this._value);
        } else if (this._onPresentation) {
            this._onPresentation(this._value);
        } else {
            this._presentationSpan.textContent = this._value;
        }
        this._presentationSpan.contentEditable = 'false';
    }
 
    /**
     * Saves the value of the component.
     */
    private async save(): Promise<void> {
        this._value = this._presentationSpan.textContent ?? '';
        await this._onSave?.(this._value);
    }
    //#endregion
}
 
Zur TypeDoc-Dokumentation