Customize IDE Editor

  • Install Web Extension Tampermonkey

  • Active Development Mode (in extensions)

  • Add this Script…

    // ==UserScript==
    // @name Snippets PHP para Scriptcase
    // @namespace http://tampermonkey.net/
    // @version 1.1
    // @description Inserta snippets PHP al presionar Ctrl+J en Scriptcase
    // @match :///scriptcase/*
    // @include scriptcase
    // @run-at document-idle
    // @grant none
    // ==/UserScript==

(function () {
‘use strict’;

const phpSnippets = [
    { text: 'foreach ($array as $key => $value) {\n    \n}', displayText: 'foreach' },
    { text: 'function nombre($params) {\n    \n}', displayText: 'function' },
    { text: 'try {\n    \n} catch (Exception $e) {\n    \n}', displayText: 'try/catch' },
    { text: 'if ($condicion) {\n    \n}', displayText: 'if' },
    { text: 'switch ($var) {\n    case 1:\n        break;\n    default:\n        break;\n}', displayText: 'switch' },
    { text: 'echo "texto";', displayText: 'echo' }
];

function showSnippetHints(cm) {
    const cursor = cm.getCursor();
    const from = { line: cursor.line, ch: cursor.ch };
    const to = { line: cursor.line, ch: cursor.ch };

    cm.showHint({
        hint: function () {
            return {
                from: from,
                to: to,
                list: phpSnippets
            };
        },
        completeSingle: false
    });
}

function setup(cm) {
    cm.addKeyMap({
        'Ctrl-J': function (cm) {
            console.log("[Snippets] Ctrl+J activado.");
            showSnippetHints(cm);
        }
    });
}

function init() {
    const editorDiv = document.querySelector('.CodeMirror');
    const cm = editorDiv?.CodeMirror;
    if (!cm || typeof cm.showHint !== "function") {
        console.warn("CodeMirror o showHint no disponible.");
        return;
    }

    console.log("[Snippets] CodeMirror detectado. Activando keymap...");
    setup(cm);
}

// Esperamos un poco a que cargue
setTimeout(init, 1500);

})();

And then, with Ctrl+J … auto snippets…
sample… foreach Ctrl+J…

Try, and optimize!
Thanks

1 Like

Excellent idea.
My contribution. I’m sharing a script to restore the cursor position when saving code in the editor. (Code assisted by chatgpt)
Works with scriptcase 9.12.011 (10)

Same steps:

  • Install the Tampermonkey web extension
  • Activate Development Mode (in extensions)
  • Add the script

restoreposition.zip (1.1 KB)

// ==UserScript==
// @name Restaurar posición del cursor y scroll en CodeMirror Scriptcase
// @namespace http://tampermonkey.net/
// @version 1.2
// @description Guarda y restaura posición del cursor y scroll en CodeMirror tras recargar, asegurando foco y timing
// @match :///scriptcase/*
// @grant none
// ==/UserScript==

(function () {
‘use strict’;

const content = document.querySelector('.CodeMirror')?.CodeMirror.getValue() || '';
const hash = hashString(content);
const STORAGE_KEY = 'codemirror_cursor_' + hash;
const SCROLL_KEY = 'codemirror_scroll_' + hash;

// Guarda posición de cursor y scroll al salir
window.addEventListener('beforeunload', () => {
    const cm = document.querySelector('.CodeMirror')?.CodeMirror;
    if (cm) {
        const cursor = cm.getCursor();
        const scroll = cm.getScrollInfo();
        localStorage.setItem(STORAGE_KEY, JSON.stringify(cursor));
        localStorage.setItem(SCROLL_KEY, JSON.stringify(scroll));
    }
});

function hashString(str) {
    let hash = 0;
    for (let i = 0; i < str.length; i++) {
        hash = (hash << 5) - hash + str.charCodeAt(i);
        hash |= 0;
    }
    return hash;
}

function waitForEditorAndRestoreCursorAndScroll() {
    const interval = setInterval(() => {
        const cmElement = document.querySelector('.CodeMirror');
        if (cmElement && cmElement.CodeMirror) {
            const cm = cmElement.CodeMirror;
            const savedCursor = localStorage.getItem(STORAGE_KEY);
            const savedScroll = localStorage.getItem(SCROLL_KEY);

            // Restaurar cursor
            if (savedCursor) {
                try {
                    const cursor = JSON.parse(savedCursor);

                    requestIdleCallback(() => {
                        cm.focus();
                        setTimeout(() => {
                            cm.setCursor(cursor);
                        }, 100);
                    });

                    let retries = 5;
                    const retrySetCursor = () => {
                        if (retries-- > 0) {
                            setTimeout(() => {
                                cm.focus();
                                cm.setCursor(cursor);
                                retrySetCursor();
                            }, 200);
                        }
                    };
                    retrySetCursor();

                } catch (e) {
                    console.warn('Error al restaurar cursor:', e);
                }
            }

            // Restaurar scroll
            if (savedScroll) {
                try {
                    const scroll = JSON.parse(savedScroll);
                    setTimeout(() => {
                        cm.scrollTo(scroll.left, scroll.top);
                    }, 200);
                } catch (e) {
                    console.warn('Error al restaurar scroll:', e);
                }
            }

            clearInterval(interval);
        }
    }, 300);
}

waitForEditorAndRestoreCursorAndScroll();

})();

@derazo
nice script. thanks!