Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Realize a shortcut, when there's no "cmd_***"? Example: HTML format "paragraph". #160

Open
fermiongithub opened this issue Oct 17, 2023 · 16 comments

Comments

@fermiongithub
Copy link

Hi.
Realizing shortcuts for functions, where a "cmd_***" exists works fine.

Example:
{
"meta+u": "cmd:cmd_ul",
"meta+o": "cmd:cmd_ol"
}

I like to realize shortcuts for commands, where no ""cmd_***" exists.

Example, when you analyse a function - offered via the menubar - with the developer tools:

Compose Window for HTML Mails > Menu Format > Submenu Paragraph > Paragraph

<menuitem xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="menu_p" type="radio" name="1" label="Absatz" accesskey="A" value="p" observes="cmd_renderedHTMLEnabler" checked="true"><hbox class="menu-iconic-left" align="center" pack="center" aria-hidden="true" checked="true"><image class="menu-iconic-icon"/></hbox><label class="menu-iconic-text" flex="1" crop="end" aria-hidden="true" value="Absatz" accesskey="A"/><label class="menu-iconic-highlightable-text" crop="end" aria-hidden="true" accesskey="A">Absatz</label><hbox class="menu-accel-container" aria-hidden="true"><label class="menu-iconic-accel"/></hbox></menuitem>

Is there any way to realize a shortcut for such types of commands?

There are several more than the mentioned one for "paragraph".
Example: h1-h4, pre, ...

And further:

I would love to create a shortcut for inserting the html element "blockquote", because it is appropriate for citations from external sources.

Thanks for your help.

@morat523035
Copy link

You would need to start your devtools investigation with the menupopup element.

<menupopup id="paragraphMenuPopup" oncommand="setParagraphState(event);">

Similar issue
#155

You can insert html directly.

window.GetCurrentEditor().insertHTML(`<a href='http://www.mozilla.org/'>Mozilla</a>`);

Similar issue
#123

@fermiongithub
Copy link
Author

fermiongithub commented Oct 18, 2023

Thanks for your answer :)

I read the readme.md (of course) and tried the following syntax for the compose window in tbkeys lite:
{ "meta+1": "window.editorSetParagraphState('h1');" }
But it does not work.

What do I miss please?

For inserting HTML I tried:

{"meta+8": "window.GetCurrentEditor().insertHTML(`<a href='http://www.mozilla.org/'>Mozilla</a>`);"}

No success.

@morat523035
Copy link

morat523035 commented Oct 18, 2023

Are you using tbkey or tbkeys-lite?

You cannot run arbitrary javascript with tbkeys-lite.

You can use the cmd shorthand and func shorthand with tbkeys and tbkeys-lite.

Use the cmd shorthand for calling a command using the goDoCommand method.

For example, "cmd:cmd_nextMsg" is the same as "window.goDoCommand('cmd_nextMsg');".

Use the func shorthand for calling a function on the window object without a parameter.

For example, "func:MsgNewMessage" is the same as "window.MsgNewMessage();".

There is a tbkeys.xpi link on the GitHub releases page.

Troubleshooting
https://forums.mozillazine.org/viewtopic.php?p=14872763#p14872763

@fermiongithub
Copy link
Author

fermiongithub commented Oct 18, 2023

I was using tbkeys-lite.
Now I deactivated it and installed the latest tbkeys.xpi.

I tried it again with tbkeys:

{ "meta+1": "window.editorSetParagraphState('h1');" }
No success. Is there still a syntax error I made?

The cmd shorthand works fine, als always.

Example:

{"meta+u": "cmd:cmd_ul"}

@morat523035
Copy link

morat523035 commented Oct 18, 2023

The command works for me.

Thunderbird 115.2.3
Windows 10 Pro 21H2 32-bit

Are you typing the command in the "Compose key bindings" text input field in the addon options?

Try closing and opening the compose window. Try restarting the app.

Key bindings do not fire in text input fields unless the first key combo includes a modifier other than shift.

{
  "f2": "window.alert('alpha');",
  "ctrl+2": "window.alert('beta');"
}

The f2 key binding works for me when I focus the "Cc" button, but not when I focus the "To" text input field. I press shift tab repeatedly to focus the "Cc" button.

Try the meta+1 key binding after focusing the "Cc" button.

Try running the following code snippet using the error console as a test.

(function () {
  window.Services.wm.getMostRecentWindow('msgcompose').editorSetParagraphState('h1');
})();

In Thunderbird, the Browser Console is called the Error Console.

Browser Console
https://firefox-source-docs.mozilla.org/devtools-user/browser_console/index.html

@fermiongithub
Copy link
Author

Thanks again for your test and your answer.
And I'm not saying that as an empty phrase.

Everything works fine now. That makes me happy!

My plattform, to make the documentation complete:
macOS Sonoma 14.0, M1
Thunderbird 115.3.2 (64bit)

Of course I'm wondering, why my test before failed. Because I restarted TB before each test.

Now there are so many options for shortcuts for several tasks.

Thanks for your helpful extension!

Even

"meta+2": "window.GetCurrentEditor().insertHTML(`<blockquote></blockquote>`);"

works fine.

Please allow me one questions about "inserting HTML":

When I type some text ("lorem"), mark that text and perform the shortcut, the text will be deleted.
When I look into the source code, the expected HTML has been inserted:

<blockquote><br></blockquote>

When you type some text ("lorem"), mark that text and perform the command "Menu Format > Paragraph > H1" the text will not be deleted, but marked up with the element.

<h1>lorem<br></h1>

Is it possible to get the same behaviour for blockquote as for h1?

But I do not want to bother you. Please answer only if you feel like it. Otherwise I'll ask in a javascript forum.

@morat523035
Copy link

morat523035 commented Oct 18, 2023

Try these:

window.GetCurrentEditor().document.execCommand('formatBlock', false, 'h1');
window.GetCurrentEditor().document.execCommand('formatBlock', false, 'blockquote');

execCommand formatblock
https://developer.mozilla.org/docs/Web/API/Document/execCommand#formatblock

Reference
https://searchfox.org/comm-esr115/search?q=formatBlock

Use the following addon to easily view the source of the currently composed message.

HTML Source Editor
https://addons.thunderbird.net/thunderbird/addon/988542

@fermiongithub
Copy link
Author

Works like a charm with blockquote!
Thanks a lot : )

The extension HTML Source Editor was already in use. : )

TBKeys was - before that thread - my most important extension. Now again more.

All the best for you!

@fermiongithub
Copy link
Author

fermiongithub commented Oct 20, 2023

Only one thing I forgot:
If you assign the element "blockquote" via shortcut it cannot be removed with "assigning it again".
An ordered list for example kann be removed by assigning the command for an ordered list again.

Example:

<blockquote><p>Lorem</p></blockquote>

After performing the shortcut again the result should be:

<p>Lorem</p>

If anyone knows how to realize that, please let me know : )

@Mist44230
Copy link

Hello !
I'm trying to create a shortcut to put selected text in superscript. As above, I've tested this :
window.GetCurrentEditor().document.execCommand('formatBlock', false, 'superscript');
But this doesn't work…
Do you have another lead ?
Thanks,
Mist

@morat523035
Copy link

@Mist44230

Try this:

window.doStyleUICommand('cmd_superscript');

Reference
http://searchfox.org/comm-esr115/search?q=superscript

The sup tag defines superscript text in HTML.

HTML sup tag
http://www.w3schools.com/tags/tag_sup.asp

@Mist44230
Copy link

Mist44230 commented Oct 24, 2023

Excellent, thank's a lot !
I'd searched for cmd_superscript, but not on the right page ;-)
Mist

@fermiongithub
Copy link
Author

Only one thing I forgot: If you assign the element "blockquote" via shortcut it cannot be removed with "assigning it again". An ordered list for example kann be removed by assigning the command for an ordered list again.

Example:

<blockquote><p>Lorem</p></blockquote>

After performing the shortcut again the result should be:

<p>Lorem</p>

If anyone knows how to realize that, please let me know : )

May I ask about this again?
If you can't remove blockquote with the keyboard shortcut as well (like the other assigned elements), then you can't use the function in practice.

Is anyone reading along who has an idea how to implement this via JS. I would be very happy : )

@morat523035
Copy link

morat523035 commented Oct 30, 2023

@fermiongithub

Try using the undo shortcut to remove the blockquote.

The docs page says that blockquote is the exception.

execCommand formatblock
http://developer.mozilla.org/docs/Web/API/Document/execCommand#formatblock

If I execute the following commands one at a time in a new compose window...

window.GetCurrentEditor().insertText('example');
window.GetCurrentEditor().document.execCommand('formatBlock', false, 'h1');
window.GetCurrentEditor().document.execCommand('formatBlock', false, 'h2');
window.GetCurrentEditor().document.execCommand('formatBlock', false, 'blockquote');
window.GetCurrentEditor().document.execCommand('formatBlock', false, 'h3');

Then the body element would change like so...

<body>
    <p>example<br></p>
</body>

<body>
    <h1>example<br></h1>
</body>

<body>
    <h2>example<br></h2>
</body>

<body>
    <blockquote>
        <h2>example<br></h2>
    </blockquote>
</body>

<body>
    <blockquote>
        <h3>example<br></h3>
    </blockquote>
</body>

The formatBlock stuff is done in the .cpp code.

Reference
http://searchfox.org/mozilla-esr115/search?q=FormatBlockContainerWithTransaction&path=HTMLEditor.h

@fermiongithub
Copy link
Author

Thank you for your detailed answer!

If I understand you correctly, there is no way to solve the task and turn blockquote on and off with one command.

"Undo" is unfortunately not a substitute. Because it works only immediately after assigning.

@morat523035
Copy link

I would say there isn't a simple solution to do that as far as I know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants