Action

Load Action Group

Posted by @jsamlarose, Last update 2 months ago

Offers prompt to search for and load action group.

Steps

  • script

    // FUNCTIONS
    
    // escape characters for regex 
    function escapeRegex(string) {
        return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    }
    
    Date.prototype.getWeek = function() {
        var date = new Date(this.getTime());
        date.setHours(0, 0, 0, 0);
        // Thursday in current week decides the year.
        date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
        // January 4 is always in week 1.
        var week1 = new Date(date.getFullYear(), 0, 4);
        // Adjust to Thursday in week 1 and count number of weeks from date to week1.
        return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
            - 3 + (week1.getDay() + 6) % 7) / 7);
    }
    
    var weekNumber = Date.today().getISOWeek();
    var weekTag = new Date().toString("yyyy-") + weekNumber;
    
    // Sort the array
    function compare(a, b) {
        const varA = a.forSort;
        const varB = b.forSort;
    
        let comparison = 0;
        if (varA > varB) {
            comparison = -1;
        } else if (varA < varB) {
            comparison = 1;
        }
        return comparison;
    }
    
    var currentLineText = editor.getTextInRange(editor.getSelectedLineRange()[0], editor.getSelectedLineRange()[1]);
    
    // Get all action groups
    var actionGroups = ActionGroup.getAll();
    var groupNames = actionGroups.map(group => group.name);
    
    // Convert groupNames array to JSON string for embedding in HTML
    let groupNamesArray = JSON.stringify(groupNames);
    let currentLineTextJSON = JSON.stringify(currentLineText);
    
    // HTML and JavaScript for the web preview
    let html = `
    <!DOCTYPE html>
    <html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
            body { 
                font-family: 'Avenir Next', -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
                padding: 1em; 
                background-color: #1e1e1e; 
                color: #e0e0e0;
            }
            #searchBar { 
                width: 100%; 
                padding: 0.5em; 
                margin-bottom: 1em; 
                background-color: #2d2d2d; 
                color: #e0e0e0; 
                border: 1px solid #3d3d3d;
                border-radius: 4px;
            }
            ul { 
                list-style: none; 
                padding-left: 0; 
            }
            li { 
                padding: 0.5em 0; 
                border-bottom: 1px solid #3d3d3d; 
                cursor: pointer;
            }
            li:hover { 
                background-color: #2d2d2d; 
            }
            li.selected {
                background-color: #444;
            }
            li.highlight {
                background-color: #555;
            }
            .h1 { color: #ff6600; }
            .h2 { color: #ff9900; }
            .h3 { color: #ffcc00; }
            .h4 { color: #99cc33; }
            .h5 { color: #66cc99; }
            .h6 { color: #9999cc; }
        </style>
    </head>
    <body>
        <input type="text" id="searchBar" placeholder="Search action groups...">
        <ul id="itemList"></ul>
        <script>
        let itemList = document.getElementById('itemList');
        let searchBar = document.getElementById('searchBar');
        let groupNames = ${groupNamesArray};
        let selectedGroup = null;
        let currentIndex = 0;
    
        function createListItems() {
            itemList.innerHTML = '';
            groupNames.forEach((group, index) => {
                let li = document.createElement('li');
                li.textContent = group;
                li.dataset.index = index;
                li.classList.add('h' + ((index % 6) + 1));
    
                li.onclick = function() {
                    Array.from(itemList.children).forEach(item => item.classList.remove('selected'));
                    this.classList.add('selected');
                    selectedGroup = this.textContent;
                    submitSelection();
                };
    
                itemList.appendChild(li);
            });
    
            if (itemList.children.length > 0) {
                itemList.children[0].classList.add('selected');
                selectedGroup = itemList.children[0].textContent;
            }
        }
    
        function filterItems() {
            let searchText = searchBar.value.toLowerCase();
            let visibleItems = 0;
            Array.from(itemList.children).forEach((item, index) => {
                let shouldShow = item.textContent.toLowerCase().includes(searchText);
                item.style.display = shouldShow ? '' : 'none';
                if (shouldShow) {
                    visibleItems++;
                }
            });
            currentIndex = 0;
    
            if (visibleItems > 0) {
                Array.from(itemList.children).forEach((item) => item.classList.remove('selected'));
                let firstVisibleItem = Array.from(itemList.children).find(item => item.style.display !== 'none');
                if (firstVisibleItem) {
                    firstVisibleItem.classList.add('selected');
                    selectedGroup = firstVisibleItem.textContent;
                }
            }
            return visibleItems;
        }
    
        function selectItem(index) {
            Array.from(itemList.children).forEach(item => {
                item.classList.remove('highlight');
                item.classList.remove('selected');
            });
            
            let visibleItems = Array.from(itemList.children).filter(item => item.style.display !== 'none');
            if (visibleItems.length > 0) {
                let item = visibleItems[index];
                item.classList.add('highlight');
                item.classList.add('selected');
                item.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
                selectedGroup = item.textContent;
                currentIndex = index;
            }
        }
    
        function submitSelection() {
            if (selectedGroup) {
                Drafts.send("action_group_to_load", JSON.stringify([selectedGroup]));
                Drafts.continue();
            } else {
                alert("Please select an action group.");
            }
        }
    
        document.addEventListener('keydown', function(event) {
            let visibleItems = Array.from(itemList.children).filter(item => item.style.display !== 'none');
    
            if (event.key === 'ArrowDown') {
                currentIndex = (currentIndex + 1) % visibleItems.length;
                selectItem(currentIndex);
                event.preventDefault();
            } else if (event.key === 'ArrowUp') {
                currentIndex = (currentIndex - 1 + visibleItems.length) % visibleItems.length;
                selectItem(currentIndex);
                event.preventDefault();
            } else if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
                searchBar.focus();
                event.preventDefault();
            } else if (event.key === 'Enter') {
                submitSelection();
                event.preventDefault();
            }
        });
    
        createListItems();
        searchBar.addEventListener('input', filterItems);
        window.onload = function() { 
            searchBar.focus();
        };
        </script>
    </body>
    </html>
    `;
    
    let preview = HTMLPreview.create();
    
    if (preview.show(html)) {
    } else {
        context.cancel();
    }
  • script

    // Get the selected line number from the draft
    
    if (context.previewValues["action_group_to_load"] !== undefined) {
    
    let aG = JSON.parse(context.previewValues["action_group_to_load"]);
    
    
    if (aG) {
        // Load the selected action group
        let actionGroup = ActionGroup.find(aG);
        
        if (actionGroup) {
            app.loadActionGroup(actionGroup);
            app.currentWindow.showActionList();
            // app.displayInfoMessage("Loaded action group: " + selectedGroup);
        } else {
            app.displayErrorMessage("Action group not found.");
        }
    } else {
        context.fail("No heading was selected or passed correctly.");
    }
    }

Options

  • After Success Default
    Notification Error
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.