zadam / trilium

Build your personal knowledge base with Trilium Notes
GNU Affero General Public License v3.0
27.45k stars 1.93k forks source link

(Feature request) I think a simple "smart equation" shortcut would help a lot. #3768

Closed metorm closed 6 months ago

metorm commented 1 year ago

Describe feature

Feature description: If I select some text and press a shortcut key, the editor gets the selected text, cleans the front/tail $ or \[ or $$ text, and puts the remaining text in the equation editing box.

I think this feature is simple enough but would make equation editing much easier. It also helps a lot to tidy the imported mark-down file having equations.

Additional Information

No response

SiriusXT commented 1 year ago

I have a script that turns selected text into a formula, I changed it slightly to remove the $ and $$, hope it does what you need. setTimeout(() => { api.bindGlobalShortcut("alt+=",function (){ if (window.getSelection().toString()!=""){ var clip=window.getSelection().toString(); setTimeout(() => { window.getSelection().deleteFromDocument(); }, 50); setTimeout(() => {document.querySelectorAll("button.ck.ck-button.ck-off.ck-block-toolbar-button:not(.ck-hidden)")[0].click();}, 100); setTimeout(() => { document.querySelectorAll("div.ck.ck-balloon-panel.ck-balloon-panel_visible.ck-balloon-panel_with-arrow.ck-toolbar-container button.ck:nth-child(12)")[0].click();}, 110); setTimeout(() => { var input=document.querySelectorAll(' div.ck-balloon-rotator__content form.ck.ck-math-form div.ck-math-view div.ck.ck-labeled-input input.ck.ck-input.ck-input-text')[0]; clip = clip.replace(/^[$]+|[$]+$/g, ""); clip = clip.replace(/^\${2,}|\${2,}$/g, ""); input.value=clip; input.dispatchEvent(new Event('input')); }, 200); setTimeout(() => { document.querySelectorAll("button.ck.ck-button.ck-off.ck-button-save")[0].click();//Automatically click the save button }, 250); } else { setTimeout(() => {document.querySelectorAll("button.ck.ck-button.ck-off.ck-block-toolbar-button:not(.ck-hidden)")[0].click();}, 10); setTimeout(() => {document.querySelectorAll("div.ck.ck-balloon-panel.ck-balloon-panel_visible.ck-balloon-panel_with-arrow.ck-toolbar-container button.ck:nth-child(12)")[0].click();}, 20); } } ); }, 1000);

metorm commented 1 year ago

@SiriusXT Wow, this one looks amazing. However, I'm a noob, could you please show me how to run this code?

SiriusXT commented 1 year ago

Use the content of the above script to create a JS Frontend type code note, then add the #widget label, and finally restart the note software. The shortcut key is alt+=.

metorm commented 1 year ago

Thank you @SiriusXT , I think I am almost there.

However, I get an unexpected token ) error in the first setTimeout(() = >{ statement. I tried to move other setTimeout(() = >{ section to the first few lines and the error repeats. Did I miss something?

image

metorm commented 1 year ago

OK, it is now working. I think it's my formating tool that caused this.

The following picture shows my working sample.

image

image

Again, thanks a lot, you saved my day @SiriusXT

XXXJetfireXXX commented 1 year ago

@SiriusXT, a little advise for the future: when you want to post a script in a comment, try this:

```\<programming language here ('js' in your case)> \ ```

For example:

setTimeout(() => {
    api.bindGlobalShortcut("alt+=", function() {
        if (window.getSelection().toString() != "") {
            var clip = window.getSelection().toString();
            setTimeout(() => {
                window.getSelection().deleteFromDocument();
            }, 50);
            setTimeout(() => {
                document.querySelectorAll("button.ck.ck-button.ck-off.ck-block-toolbar-button:not(.ck-hidden)")[0].click();
            }, 100);
            setTimeout(() => {
                document.querySelectorAll("div.ck.ck-balloon-panel.ck-balloon-panel_visible.ck-balloon-panel_with-arrow.ck-toolbar-container button.ck:nth-child(12)")[0].click();
            }, 110);
            setTimeout(() => {
                var input = document.querySelectorAll(' div.ck-balloon-rotator__content form.ck.ck-math-form div.ck-math-view div.ck.ck-labeled-input input.ck.ck-input.ck-input-text')[0];
                clip = clip.replace(/^[$]+|[$]+$/g, "");
                clip = clip.replace(/^\${2,}|\${2,}$/g, "");
                input.value = clip;
                input.dispatchEvent(new Event('input'));
            }, 200);
            setTimeout(() => {
                document.querySelectorAll("button.ck.ck-button.ck-off.ck-button-save")[0].click(); //Automatically click the save button    
            }, 250);
        } else {
            setTimeout(() => {
                document.querySelectorAll("button.ck.ck-button.ck-off.ck-block-toolbar-button:not(.ck-hidden)")[0].click();
            }, 10);
            setTimeout(() => {
                document.querySelectorAll("div.ck.ck-balloon-panel.ck-balloon-panel_visible.ck-balloon-panel_with-arrow.ck-toolbar-container button.ck:nth-child(12)")[0].click();
            }, 20);
        }
    });
}, 1000);

Hope this helps! :)

mm21 commented 1 year ago

@SiriusXT, can you please help me understand why setTimeout() is used this way? Why wait 1s before binding the shortcut, and why not just call those functions sequentially rather than scheduling them for execution at staggered intervals?

zadam commented 1 year ago

This sounds like a good feature for https://github.com/isaul32/ckeditor5-math

SiriusXT commented 1 year ago

This implementation is relatively rough. Execution at time intervals is just to wait for the webpage to load, and sometimes it can be executed directly

metorm commented 1 year ago

@SiriusXT BTW, if i want to write other front-end scripts, where can I find the element names like ck-button, ck-balloon-panel_with-arrow and 'ck-input' to operate?

SiriusXT commented 1 year ago

I found it by right-clicking on the browser and inspecting the element

@SiriusXT BTW, if i want to write other front-end scripts, where can I find the element names like ck-button, ck-balloon-panel_with-arrow and 'ck-input' to operate?

sottey commented 1 year ago

The ck-* elements are (I am guessing) part of the CKEditor. You could also find the element names by searching the source on GitHub.

metorm commented 1 year ago

Thanks @SiriusXT, it's a smart idea. I had spent some time searching for the documents and any "debugging" option in my client.

meichthys commented 6 months ago

Trilium has entered maintenance mode. Future enhancements will be addressed in TrilumNext: https://github.com/TriliumNext/Notes/issues/73