WorkTile交互优化

优化愚蠢的交互方式和样式,让操作更便捷更人性!

目前為 2021-12-01 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         WorkTile交互优化
// @namespace    http://fulicat.com
// @version      1.1.1
// @description  优化愚蠢的交互方式和样式,让操作更便捷更人性!
// @author       Jack.Chan
// @homepage     https://greasyfork.org/zh-CN/scripts/436038
// @license MIT
// @url          https://greasyfork.org/zh-CN/scripts/436038-worktile%E4%BA%A4%E4%BA%92%E4%BC%98%E5%8C%96
// @match        https://*.worktile.com/*
// @icon         https://cdn.pingcode.com/static/portal/favicon.ico
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var $style = document.createElement('style');
    $style.type = 'text/css';
    $style.innerHTML = `
.app-nav-new-area-x,
.thy-header-self-adaption-x,
.shortcut-tray-content{
    z-index: 3000 !important;
}
.dialog-max-lg{
    width: 90% !important;
}
.task-table-next{
    padding: 16px 16px 46px 16px !important;
}

.shortcut-main{
    position: relative;
}
.shortcut-main shortcut-tray-list{
    box-shadow: 5px 0 10px 0 rgb(0 0 0 / 20%);
    background: #fafafa;
    position: fixed;
    right: 0;
    left: 108px;
    bottom: 0;
}
.shortcut-main .shortcut-list-content{
    justify-content: center;
}

.toc-section.thy-menu{
    padding-bottom: 80px;
}
mission-work-addon-group.thy-layout{
    padding-bottom: 50px;
}
mission-work-addon-group .addon-task-table-body.addon-task-table-scroll{
    bottom: 50px;
}

/* 右侧抽屉 */
.view-filter .view-filter-body{
    height: auto !important;
}
.view-filter .view-filter-footer{
    position: static !important;
}

/* 需求、任务描述 */
.textarea-show-wrapper .textarea-show-footer{
    text-align: center;
    background: #eee;
    padding: 10px;
}
.textarea-show-wrapper .textarea-show-footer a{
    padding: 10px 20px;
}
.textarea-show-body{
    position: relative;
}
.markdown-body{
    visibility: hidden;
}
.markdown-body-new{
    visibility: visible;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1;
    background: rgba(255 ,255, 255, 1);
}
.x-link{
    width: auto !important;
}

.app-nav-new-area{
    padding-top: 35px;
}
.link-open-new-window{
    position: absolute;
    top: 0;
    color: #fff;
    z-index: 99996;
    font-weight: bold;
    padding: 13px 0;
    display: block;
    width: 60px;
    text-align: center;
    background: #358fe4;
}
.link-open-new-window:hover{
    color: #fff;
    background: #ff9f73;
    background: #4caf50;
    background: #ff894e;
}
`;

    function init() {
        document.createElement('shortcut-tray-list');
        document.head.appendChild($style);
    }

    function getTaskInfo(identifier, callback) {
        if (identifier) {
            var url = `https://shebaochina.worktile.com/api/mission-vnext/tasks/no/${identifier}?t=${Date.now()}`;
            var xhr = new XMLHttpRequest();
            xhr.open('GET', url, false);
            xhr.onload = function() {
                var response = xhr.responseText || xhr.response;
                if (response) {
                    try{
                        response = JSON.parse(response);
                        if (response.code === 200 && response.data && response.data.value) {
                            var data = response.data.value;
                            if (data.project_id && data._id) {
                                if (typeof(callback) === 'function') {
                                    callback(data, response);
                                }
                            }
                        }
                    } catch (ex) {
                        console.warn('--WARNING-', ex)
                    }
                }
            }
            xhr.send(null);
        }
    }

    function addLink(delay) {
        delay = delay || 0;
        setTimeout(function() {

            // 增加新开窗口
            var $linkOpenNewWindow = document.querySelector('.link-open-new-window');
            if (!$linkOpenNewWindow) {
                $linkOpenNewWindow = document.createElement('a');
                $linkOpenNewWindow.className = 'link-open-new-window';
                $linkOpenNewWindow.title = '打开新窗口';
                $linkOpenNewWindow.innerText = '新窗口';
                $linkOpenNewWindow.target='_blank';
                $linkOpenNewWindow.rel='opener';
                $linkOpenNewWindow.href = window.location.href;
                $linkOpenNewWindow.addEventListener('click', function(event) {
                    $linkOpenNewWindow.href = window.location.href;
                }, false);
                document.body.appendChild($linkOpenNewWindow);
            }

            // 弹框
            var $dialogs = document.querySelectorAll('.thy-dialog-container');
            if ($dialogs && $dialogs.length) {
                $dialogs.forEach(function($dialog) {
                    var $identifier = $dialog.querySelector('.task-identifier');
                    var identifier = $identifier ? ($identifier.innerText || '').trim() : '';
                    var $dialogNavSecondary = $dialog.querySelector('.thy-icon-nav-secondary');
                    if ($dialogNavSecondary) {
                        // 弹框: 在新窗口打开、复制链接
                        var $linkOpen = $dialogNavSecondary.querySelector('.x-link-open-new');
                        var $linkCopy = $dialogNavSecondary.querySelector('.x-link-copy');
                        if (!$linkOpen) {
                            $linkOpen = document.createElement('a');
                            $linkOpen.className = 'thy-icon-nav-link x-link x-link-open-new';
                            $linkOpen.target = '_blank';
                            $linkOpen.innerText = '新窗口打开';
                            $linkOpen.title = '在新窗口中打开';
                            $linkOpen.href = window.location.href;
                            $dialogNavSecondary.appendChild($linkOpen);

                            $linkCopy = document.createElement('a');
                            $linkCopy.className = 'thy-icon-nav-link x-link x-link-copy';
                            $linkCopy.innerText = '复制链接';
                            $linkCopy.title = '复制链接';
                            $linkCopy.href = window.location.href;
                            $linkCopy.addEventListener('click', function(event) {
                                event.preventDefault();
                                event.stopPropagation();
                                var $textarea = document.createElement('textarea');
                                $textarea.style.cssText = 'position:fixed;top:0;left:-500px;z-index:9999;';
                                document.body.appendChild($textarea);
                                var $projectName = $dialog.querySelector('.project-name');
                                var projectName = $projectName ? $projectName.innerText : ''
                                var $taskTitle = $dialog.querySelector('.task-title');
                                var taskTitle = $taskTitle ? $taskTitle.innerText : '';
                                var value = `【${projectName}】 ${identifier ? '#'+ identifier : ''} - ${taskTitle}\r\n${this.href}`;
                                $textarea.value = value;
                                $textarea.select();
                                document.execCommand('Copy');
                                $linkCopy.innerText = '复制成功';
                                setTimeout(function() {
                                    $linkCopy.innerText = '复制链接';
                                    document.body.removeChild($textarea);
                                }, 1500);
                                return false;
                            }, false);
                            $dialogNavSecondary.appendChild($linkCopy);

                            // 当前页面是非需求/任务页面时 查询任务信息
                            var pattern = /(.*)\/projects\/(\w+)\/tasks\/(\w+)([^\w].*)?/;
                            if (identifier && !pattern.test(window.location.href)) {
                                getTaskInfo(identifier, function(data) {
                                    var task_url = `https://shebaochina.worktile.com/mission/projects/${data.project_id}/tasks/${data._id}`;
                                    $linkCopy.href = $linkOpen.href = task_url;
                                });
                            }
                        }


                        // 弹框:需求/任务 描述 编辑、复制
                        if (!$dialog.querySelector('.markdown-body-new')) {
                            var $markdownBody = $dialog.querySelector('.markdown-body');
                            if ($markdownBody && $markdownBody.parentNode) {
                                var $markdownBodyParent = $markdownBody.parentNode;
                                var $newMarkdownBody = $markdownBody.cloneNode(true);
                                $newMarkdownBody.classList.add('markdown-body-new');
                                $markdownBodyParent.appendChild($newMarkdownBody, $markdownBody);

                                var syncDescHandler = function(event) {
                                    setTimeout(function() {
                                        $newMarkdownBody.innerHTML = $markdownBody.innerHTML;
                                    }, 300);
                                }

                                $dialog.addEventListener('mousedown', function(event) {
                                    var $btns = $dialog.querySelectorAll('field-item-textarea-show-edit .thy-btn')
                                    $btns.forEach(function($btn) {
                                        $btn.removeEventListener('mouseup', syncDescHandler, false);
                                        $btn.addEventListener('mouseup', syncDescHandler, false);
                                    });
                                }, false);

                                $newMarkdownBody.addEventListener('dblclick', function(event) {
                                    $markdownBodyParent.classList.toggle('textarea-show-body-collapse');
                                    return false
                                }, false);

                             }
                        }

                    }
                })
            }
        }, delay);
    }

    // if (!window.location.pathname.startsWith('/api/')) {
    if (document.contentType.startsWith('text/html')) {
        init();

        document.body.addEventListener('mousedown', function(e) {
            addLink(500);
        }, false);

        addLink(1500);
    }
})();