Jira Server: toggleable checkboxes in issue descriptions

Renders Markdown style toggleable checkboxes in Jira Server issue descriptions

目前為 2024-09-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Jira Server: toggleable checkboxes in issue descriptions
// @description  Renders Markdown style toggleable checkboxes in Jira Server issue descriptions
// @author       Antti Kaihola
// @namespace    https://github.com/akaihola
// @version      0.1
// @license      MIT
// @match        https://*/*/RapidBoard.jspa*
// @match        https://jira.*/browse/*-*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const slugify = text => text.toLowerCase().replace(/\W+/g, '-').replace(/^-+|-+$/g, '');
    let lastSeenDescription = '';

    function handleCheckboxes() {
        const description = jQuery('#description-val > div');
        description.off('click').on('click', 'input[type="checkbox"]', function() {
            const checkbox = jQuery(this);
            const li = checkbox.closest('li');
            const checked = checkbox.prop('checked');
            const content = li.contents().filter(function () {return this.nodeType === 3;}).first().text();

            jQuery('#description-val > span[role="button"]').click();

            const waitForEditor = setInterval(() => {
                const textarea = jQuery('textarea#description');
                if (textarea.length) {
                    clearInterval(waitForEditor);
                    const lines = textarea.val().split('\n');
                    const updatedLines = lines.map(line => {
                        const match = line.match(/^(\s*)-\s*\[([ \u2002xX])\]\s*(.+)/);
                        return match && slugify(match[3]) === slugify(content)
                            ? `${match[1]}- [${checked ? 'x' : '\u2002'}] ${match[3]}`
                            : line;
                    });
                    textarea.val(updatedLines.join('\n'));
                    jQuery('#description-form > div.save-options button.submit').click();
                }
            }, 50);
        });

        description.find('li').each(function() {
            const li = jQuery(this);
            const textNode = li.contents().filter(function () {return this.nodeType === 3;}).first();
            if (!textNode.length) return;
            const firstChild = li.children().first();
            let content = textNode.text().trim();
            let checked;
            if (firstChild.is("span") && firstChild.text().trim().match(/^\[[xX]]/)) {
                firstChild.remove();
                checked = " checked";
            } else {
                const match = content.match(/^\[([ \u2002xX])\]\s*(.+)/);
                if (!match) return;
                content = match[2];
                textNode[0].nodeValue = match[2];
                checked = match[1].toLowerCase() === 'x' ? " checked" : "";
            }
            li.attr('id', slugify(content)).css('list-style-type', 'none');
            const checkbox = jQuery(`<input type="checkbox" style="margin-left: -1.5em"${checked}>`);
            li.prepend(checkbox);
        });
    }

    setInterval(() => {
        const description = document.querySelector('#description-val > div');
        if (description) {
            if (description.innerHTML !== lastSeenDescription) {
                handleCheckboxes();
                lastSeenDescription = description.innerHTML;
            }
        }
    }, 200);
})();