src/main/webapp/issue-editor.js

Tue, 06 Jan 2026 20:08:54 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 06 Jan 2026 20:08:54 +0100
changeset 409
109850e92e95
parent 408
179bda934121
permissions
-rw-r--r--

add Markdown preview - resolves #774

/*
 * Copyright 2021 Mike Becker. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * Replaces the formatted comment text with a text area.
 *
 * @param {number} id the ID of the comment
 */
function showCommentEditor(id) {
    const editor = document.getElementById(`comment-editor-${id}`)
    const view = document.getElementById(`comment-view-${id}`)
    editor.style.display='block'
    view.style.display='none'
}

function cancelCommentEditor(id) {
    const editor = document.getElementById(`comment-editor-${id}`)
    const view = document.getElementById(`comment-view-${id}`)
    editor.style.display='none'
    view.style.display='block'
}

function toggleVariantStatus() {
    const cbox = document.getElementById('use-variants')
    if (!cbox) return
    const issue_status = document.getElementById('issue-status')
    const variant_status = document.getElementById('issue-variant-status')
    if (cbox.checked) {
        issue_status.style.display = 'none'
        variant_status.style.display = 'flex'
    } else {
        issue_status.style.display = 'inline-block'
        variant_status.style.display = 'none'
    }
}

function initMarkdownEditor(id) {
    const btn_preview = document.getElementById(`${id}-btn-preview`);
    const btn_edit = document.getElementById(`${id}-btn-edit`);
    const textarea = document.getElementById(`${id}`);
    const preview = document.getElementById(`${id}-preview`);
    btn_preview.addEventListener('click', () => {
        textarea.style.display = 'none';
        preview.style.display = 'block';
        textarea.dataset.original = textarea.value;
        btn_preview.style.display = 'none';
        btn_edit.style.display = 'inline-block';
        const req = new XMLHttpRequest();
        req.addEventListener("load", (evt) => {
            if (evt.target.status === 200) {
                preview.innerHTML = evt.target.responseText;
            } else {
                // revert the button click as fallback
                setTimeout(() => btn_edit.click(), 100);
            }
        });
        let url = baseHref + 'issues/preview-markdown';
        req.open("POST", url);
        req.setRequestHeader("Content-Type", "text/plain");
        req.send(textarea.value);
    });
    btn_edit.addEventListener('click', () => {
        textarea.readOnly = false;
        textarea.value = textarea.dataset.original;
        textarea.style.display = 'block';
        preview.style.display = 'none';
        textarea.focus();
        btn_preview.style.display = 'inline-block';
        btn_edit.style.display = 'none';
    });
    btn_edit.style.display = 'none';
    preview.style.display = 'none';
}

window.addEventListener("load", () => {
    toggleVariantStatus();
    const sbox = document.getElementById('linkable-issues');
    if (!sbox) return;
    configureSearchBox('linkable-issues', sbox.dataset.project);
});

mercurial