Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/text editor/extended code blocks: add a copy button in toolbar
Adorian Doran5957df5caf2e
diff --git a/apps/server/src/assets/help/help_content.json b/apps/server/src/assets/help/help_content.jsonindex 49c56f3358..65a861601c 100644--- a/apps/server/src/assets/help/help_content.json+++ b/apps/server/src/assets/help/help_content.json@@ -124,7 +124,7 @@"_help_EK2ckIZ74tbT": "<p>Content tabs group several titled panels of content into one block that shows a single panel at a time. They suit content that comes in variants (such as installation steps for each operating system or the same snippet in several programming languages).</p><p>A tabs block consists of:</p><ul><li><strong>A single row of tab titles</strong>, above the panel or, when the tabs are aligned at the bottom, below it. The title of the panel that is showing is highlighted.</li><li><strong>The panel of the active tab.</strong> A panel can hold any kind of content (including nested tabs blocks).</li></ul><h2>Inserting a tabs block</h2><ul><li>In the formatting bar, go to <em>Insert</em> → <em>Tabs</em>.</li><li>Alternatively, type <code>/tabs</code> and press <kbd>Enter</kbd>, as described in <a class=\"reference-link\" href=\"#root/_help_ZlN4nump6EbW\">Slash Commands</a>.</li></ul><p>A new block contains two tabs, named “Tab 1” and “Tab 2”. Any content that was selected prior to inserting the tabs will be moved into the first tab. The title of the first tab is selected, so typing replaces it.</p><h2>Scrolling through the titles</h2><p>When the titles of a block do not fit on their row, a <span class=\"tn-icon bx bx-chevron-left\"></span> button and a <span class=\"tn-icon bx bx-chevron-right\"></span> button appear at its ends:</p><ul><li>To scroll the titles, click one of the buttons. Holding a button down scrolls the titles continuously until it is released.</li><li>On a touch screen, the titles can also be swiped sideways.</li></ul><p>Whenever another tab is shown (for example by clicking its title, by moving the cursor into it or by following a link to it), the row scrolls so that its whole title is visible. This works the same while editing, in read-only notes and on shared pages.</p><h2>Editing tabs</h2><ul><li>To show a tab, click its title or move the cursor into it.</li><li>To move from a title into its panel, press <kbd>Enter</kbd>.</li><li>The label of a tab can be formatted just like normal text, including support for <a class=\"reference-link\" href=\"#root/_help_hD8GawVuazff\">Icons</a>.</li></ul><p>While the cursor is inside a tabs block, a toolbar appears above it with the following buttons:</p><ul><li><span class=\"tn-icon cke cke-plus\"></span> <em>(Add tab)</em> inserts a tab after the current one and selects its title.</li><li><span class=\"tn-icon cke cke-remove\"></span> <em>(Remove tab)</em> removes the current tab. Removing the last tab removes the whole block.</li><li><span class=\"tn-icon cke cke-chevron-right bx-flip-horizontal\"></span> <em>(Move tab left)</em> and <span class=\"tn-icon cke cke-chevron-right\"></span> (<em>Move tab right)</em> change the order of the tabs.</li><li><span class=\"tn-icon cke cke-link\"></span> <em>(Copy link to tab)</em> copies a link to the current tab, as described below.</li><li><span class=\"tn-icon cke cke-trilium-tabs\"></span> <em>(Tab alignment)</em> places the tab titles above the panel (<em>Align tabs at top</em>, the default) or below it (<em>Align tabs at bottom</em>).</li></ul><p>The tab that is showing is not saved with the note: every tabs block opens on its first tab.</p><h3>Adding tabs</h3><p>While the cursor is in a tab, either in its title or in its panel, you can use these key combinations:</p><ul><li><kbd>Ctrl</kbd> + <kbd>Enter</kbd>: Add a tab after the current one.</li><li><kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Enter</kbd>: Add a tab before the current one.</li></ul><p>The new tab is shown and its title is selected, so typing replaces it.</p><p><kbd>Ctrl</kbd> + <kbd>Enter</kbd> keeps its usual function on a link, in a to-do item and in the title of a collapsible block, and <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Enter</kbd> in a to-do item.</p><h3>Reordering tabs</h3><p>While the cursor is in a tab, either in its title or in its panel, you can use these key combinations:</p><ul><li><kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>Right</kbd>: Move the tab to the right.</li><li><kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>Left</kbd>: Move the tab to the left.</li><li><kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>Home</kbd>: Make it the first tab.</li><li><kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>End</kbd>: Make it the last tab.</li></ul><h2>Moving content between tabs</h2><p>To move content to another tab, select it in the panel, and then use these key combinations:</p><ul><li><kbd>Ctrl</kbd> + <kbd>Right</kbd>: Move to the tab on the right.</li><li><kbd>Ctrl</kbd> + <kbd>Left</kbd>: Move to the tab on the left.</li><li><kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>End</kbd>: Move to the last tab.</li><li><kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Home</kbd>: Move to the first tab.</li></ul><p>The tab that receives the content is shown, with the content still selected, so pressing the key again moves it on to the next tab. Paragraphs, list items, tables and other blocks move whole, even when only part of them is selected. Content moved to a tab on the left goes to the end of that tab, and content moved to a tab on the right goes to its start, so it keeps its place in the reading order.</p><p>Without a selection, or with a selection in a tab title, these keys keep their usual function: moving the cursor by word, or selecting up to the start or end of the tab.</p><p>When a tabs block and a <a class=\"reference-link\" href=\"#root/_help_wYDzQ1fBFgfT\">Multicolumn layout</a> are nested in one another, the key combinations act on the one closest to the cursor.</p><h2>Reading tabs</h2><p>In <a class=\"reference-link\" href=\"#root/_help_CoFPLs3dRlXc\">Read-Only Notes</a> and on <a class=\"reference-link\" href=\"#root/_help_R9pX4DGra2Vt\">Sharing</a>, tabs blocks work as tabs as well:</p><ul><li>To show a tab, click its title, or focus it and press <kbd>Enter</kbd> or <kbd>Space</kbd>.</li><li>To move between the titles of a block, use the arrow keys, <kbd>Home</kbd> and <kbd>End</kbd>.</li></ul><p>A tab without a title shows “Tab title” in place of one.</p><p>In read-only notes, a title too long for its tab fades out where it is cut off. To read the rest of it, swipe it sideways or drag it with the mouse.</p><h2>Linking to a tab</h2><p>A link can point to a single tab, much like a link to a block (see <a class=\"reference-link\" href=\"#root/_help_klC3ilXa9kUE\">Block references</a>):</p><ol><li>Place the cursor in the tab, either in its title or in its panel.</li><li>Press <em>Copy link to tab</em> (<span class=\"tn-icon cke cke-link\"></span>) in the toolbar of the tabs block. Alternatively, right-click the text and select <em>Copy</em> → <em>Copy link to this tab</em>. The tab flashes briefly.</li><li>Go to the note where to insert the link and press <kbd>Ctrl</kbd>+<kbd>V</kbd>.</li></ol><p>The link shows the title of the note, followed by the title of the tab. Clicking it opens the note, shows the tab and flashes it. As with any block reference, the link can be turned into an <a class=\"reference-link\" href=\"#root/_help_nBAXQFj20hS1\">Include Note</a> that shows only that tab.</p><p>Links to a tab can be copied only from a note being edited. On a <a href=\"#root/_help_R9pX4DGra2Vt\">shared page</a>, such a link opens the whole note.</p><h2>Content in hidden tabs</h2><p>Content inside a tab that is not showing is still reachable (both while editing and while reading). The tab that holds it is shown when:</p><ul><li>searching the note with <kbd>Ctrl</kbd>+<kbd>F</kbd> highlights a match inside it;</li><li>a link points to an anchor or a block inside it (see <a class=\"reference-link\" href=\"#root/_help_oSuaNgyyKnhu\">Anchors</a> and <a class=\"reference-link\" href=\"#root/_help_klC3ilXa9kUE\">Block references</a>);</li><li>an entry of the <a class=\"reference-link\" href=\"#root/_help_BFvAtE74rbP6\">Table of contents</a> or the <a class=\"reference-link\" href=\"#root/_help_AxshuNRegLAv\">Highlights list</a> points inside it;</li><li>on a shared page, the address of the page points to a heading or an anchor inside it.</li></ul><p>Tab titles are not headings, so they do not appear in the table of contents; headings inside a panel do.</p><h2>Printing and exporting</h2><ul><li>When printing or <a href=\"#root/_help_NRnIZmSMc5sj\">exporting to PDF</a>, every tab is printed, one after another, each with its title above its panel, whatever the tab alignment.</li><li>Markdown export and import write tabs blocks in the content tabs syntax of Material for MkDocs, described in <a class=\"reference-link\" href=\"#root/_help_rJ9grSgoExl9\">Supported syntax</a>. <a class=\"reference-link\" href=\"#root/_help_6RM1Q7ppFVoj\">Markdown</a> notes use the same syntax, show the blocks as tabs in their preview and insert them with <code>/tabs</code>. That syntax has no tab alignment, so tabs aligned at the bottom are exported as regular tabs.</li><li>Anywhere the tabs cannot work as tabs (such as an HTML export opened elsewhere), a tabs block reads as a sequence of titled sections.</li></ul>","_help_2x0ZAX9ePtzV": "<figure class=\"image image_resized image-style-align-right\" style=\"width:64.63%;\"><img style=\"aspect-ratio:812/585;\" src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/Cut%20to%20subnote_cut-to-subnote.gif\" width=\"812\" height=\"585\"></figure><p>When editing a document that becomes too large, you can split it into sub-notes:</p><ol><li>Select the desired text and cut it to the clipboard.</li><li>Create a new sub-note and name it.</li><li>Paste the content from the clipboard into the sub-note.</li></ol><p>Trilium can automate this process. Select some text within the note, and in the selection toolbar, click the scissors icon for the \"cut & pasted selection to sub-note\" action. The heading is automatically detected and the new sub-note is named accordingly. You can also assign a keyboard shortcut for this action. This functionality is available through the block toolbar icon. On a phone, the scissors are at the end of the text formatting row, which shows as you select text.</p>","_help_UYuUB1ZekNQU": "<h3>Inline code</h3><p>Inline code formats text using a monospace font to indicate technical content in a sentence such as code, paths, etc.</p><p><img src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/Developer-specific%20formatting_image.png\"></p><p>Example of inline code being used to illustrate file system paths as well as shell commands (<code>git</code> in this case).</p><p>To insert an inline code:</p><ul><li>Via the <a href=\"#root/_help_nRhnJkTT8cPs\">Formatting toolbar</a>, look for the <span class=\"tn-icon cke cke-code\"></span> button.</li><li>Type `code` where <code>code</code> is the desired text to be automatically formatted as inline code.</li><li>Paste a single line copied from Visual Studio Code. See <a href=\"#root/_help_QxEyIjRBizuC\">Code blocks</a> for pasting several lines.</li></ul><h3>Code blocks</h3><p>Code blocks display a snippet of code as a dedicated block:</p><p><img src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/1_Developer-specific%20formatting_image.jpg\"></p><p>A code block with JavaScript syntax highlight enabled.</p><p>Note that these are not meant for very large portions of code. Use the dedicated <a href=\"#root/_help_6f9hih2hXXZk\">Code</a> note type instead.</p><p>See the dedicated documentation for more information: <a href=\"#root/_help_QxEyIjRBizuC\">Code blocks</a></p><h3>Keyboard shortcuts</h3><p>This allows marking a portion of text as a shortcut key.</p><p><img src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/Developer-specific%20formatting_image.jpg\"></p><p>To apply this style, press the <span class=\"tn-icon cke cke-trilium-kbd\"></span> button in <span class=\"tn-icon bx bx-text\"></span> group from the <a href=\"#root/_help_nRhnJkTT8cPs\">Formatting toolbar</a>. On the floating toolbar, the buttons appear when selecting a text.</p><p>Alternatively, press the <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>K</kbd> combination to apply keyboard shortcut formatting to the current text selection.</p><p>The formatting can be removed by pressing again the button (from the UI or from the keyboard), or by using the <span class=\"tn-icon cke cke-remove-format\"></span> <em>Remove formatting</em> button.</p>",- "_help_QxEyIjRBizuC": "<p><img src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/Developer-specific%20formatting/Code%20blocks_image.jpg\"></p><p>The code blocks feature allows entering pieces of code in text notes.</p><p>Note that this feature is meant for generally small snippets of code. For larger files such as an entire log, see the <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> note type instead.</p><h2>Inserting a code block</h2><ul><li>Via the <a class=\"reference-link\" href=\"#root/_help_nRhnJkTT8cPs\">Formatting toolbar</a>, look for the <span class=\"tn-icon cke cke-code-block\"></span> button.<ul><li>Pressing directly on the icon will insert a code block with the language that was selected most recently. If this is the first time a code block is inserted, the language will be “Auto-detected” by default.</li><li>Pressing the arrow next to the icon, which will show a popup with the available languages.</li></ul></li><li>Type <code>```</code> (as in Markdown).<ul><li>Note that it's not possible to specify the language, as it will default to the last selected language.</li></ul></li><li>Paste several lines of code copied from Visual Studio Code.<ul><li>The code block uses the language of the file in Visual Studio Code.</li><li>If that language is not in the list of languages, the code block uses <em>Auto-detected</em>.</li><li>The colors of the Visual Studio Code theme are removed.</li><li>A single line becomes inline code instead of a code block.</li><li>In Firefox, the code block always uses <em>Auto-detected</em>. Firefox can't read the language from Visual Studio Code.</li></ul></li></ul><h2>Exiting out of the code block</h2><ul><li>To exit out of a code block and enter a normal paragraph, move the cursor at the end of the code block and press Enter twice.</li><li>Similarly, to insert a paragraph above the note block, move the cursor at the beginning of the code block and press Enter twice.</li></ul><aside class=\"admonition note\"><p>If you've pasted a code block with a more complex HTML structure, exiting out of the code block by pressing Enter multiple times might not work. In that case the best approach is to delete the code block entirely and use Ctrl+Shift+V (paste as plain text).</p></aside><h2>Syntax highlighting & color schemes</h2><p>Since TriliumNext v0.90.12, Trilium will try to offer syntax highlighting to the code block. Note that the syntax highlighting mechanism is slightly different than the one in <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> notes as different technologies are involved.</p><p>Interaction:</p><ul><li>When the language is set to <em>Auto-detected</em> (by default), Trilium will try to identify the programming language (or similar) that corresponds to the given snippet of text and highlight it. Only the languages enabled in <a class=\"reference-link\" href=\"#root/_help_4TIF1oA4VQRO\">Options</a> → <em>Code Notes</em> → <em>Available MIME types in the dropdown</em> are considered. If this is problematic, consider changing the language of the code block manually.</li><li>When the language is set to <em>Plain text</em>, there will be no syntax highlighting.</li></ul><p>Note that when editing a text note, syntax highlighting is automatically disabled if the code block is too big (somewhere around 500 lines). In <a class=\"reference-link\" href=\"#root/_help_CoFPLs3dRlXc\">Read-Only Notes</a>, in print and on a <a href=\"#root/_help_R9pX4DGra2Vt\">shared page</a>, a code block of more than 500 lines or 50,000 characters is shown without syntax highlighting. These limits are not configurable.</p><p>In order to configure this new feature, a section has been added in <a class=\"reference-link\" href=\"#root/_help_4TIF1oA4VQRO\">Options</a> → <em>Appearance</em> to control the syntax highlighting. There the color scheme can be chosen, from a builtin selection of themes from Highlight.js. <em>VS Code (Light)</em> and <em>VS Code (Dark)</em> use the same colors as the default themes of <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> notes, so a code block and a code note color the same code alike.</p><ul><li>It is possible to disable the syntax highlighting for all the notes by selecting “No syntax highlighting” in the “Color scheme” option.</li><li>Word wrapping is disabled by default, but can be configured from the same section.</li><li>The tab width can also be adjusted from Options.</li></ul><aside class=\"admonition note\"><p><strong>Context regarding syntax highlighting</strong></p><p>In order to achieve the syntax highlight, the Highlight.js library is being used. Do note that support for syntax highlighting in code blocks is not a supported feature of the text editor we are using CKEditor), but rather a hack which makes use of the highlights API (used for highlighting search results for example). Nevertheless, we haven't noticed any major issues during the development of the feature, but feel free to report any issues you might have.</p><p>Most of the work to achieve the syntax highlight itself was already done by <a href=\"https://github.com/antoniotejada\">antoniotejada</a> in <a href=\"https://github.com/antoniotejada/Trilium-SyntaxHighlightWidget\">https://github.com/antoniotejada/Trilium-SyntaxHighlightWidget</a>. On our side we added customization but also additional functionality.</p></aside><h3>Migrating from existing syntax highlight plugins</h3><p>If you are already using a syntax highlighting plugin such as the <a href=\"https://github.com/antoniotejada/Trilium-SyntaxHighlightWidget\">Trilium-SyntaxHighlightWidget</a> we are basing off of, it is important to disable that plugin before upgrading in order for it not to conflict with our implementation.</p><p>Should you encounter any issues after the migration, try running Trilium in safe mode.</p><h2>Changing the language of a code block</h2><p>Simply click anywhere inside the code block and press again the code block button in the <a class=\"reference-link\" href=\"#root/_help_nRhnJkTT8cPs\">Formatting toolbar</a>:<br><img src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/Developer-specific%20formatting/Code%20blocks_image.png\"></p><h2>Adjusting the list of languages</h2><p>The code blocks feature shares the list of languages with the <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> note type.</p><p>The supported languages, and the order in which they are listed, can be adjusted by going to <a class=\"reference-link\" href=\"#root/_help_4TIF1oA4VQRO\">Options</a> → <em>Code Notes</em> → <em>Available MIME types in the dropdown</em>. See <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> for how to add, remove and reorder them.</p><p>Note that the list of languages is not immediately refreshed, you'd have to manually <a href=\"#root/_help_s8alTXmpFR61\">refresh the application</a>.</p><h2>Text replacements and ligatures</h2><p><a class=\"reference-link\" href=\"#root/_help_oI7GoyilXXAr\">Automatic replacements</a> are a feature of text notes which converts some character sequences into another and some of them interfere with programming text, such as the conversion of quotes. While in a code block, the automatic replacements <strong>are not effect</strong>. The only way the automatic replacements can interfere is if the text is typed/pasted before being put in a code block.</p><p>A different feature is <em>programming ligatures</em>, which is simply a display affordance which turns <code>!=</code> into <code>≠</code> or <code>-></code> as <code>→</code> . These come from the <a href=\"#root/_help_w4rMEhJsALlA\">font</a>. The underlying text is not actually changed, and copying it out gives you back <code>!=</code> and <code>-></code>.</p><p>Ligatures can be disabled in <a class=\"reference-link\" href=\"#root/_help_4TIF1oA4VQRO\">Options</a><em>→ Appearance → Fonts → Programming ligatures.</em></p><h2>Extended code blocks</h2><aside class=\"admonition important\"><p>Extended code blocks are experimental, and they change as they mature. To use them, turn on <em>Extended code blocks</em> in <a class=\"reference-link\" href=\"#root/_help_4TIF1oA4VQRO\">Options</a> → <em>Advanced</em> → <em>Experimental Options</em>.</p></aside><p>An extended code block is a code block that is edited with the code editor of a <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> note, inside the text. It has the same syntax highlighting and code folding as a code note, and it suits longer pieces of code.</p><ul><li>To insert one, open the <span class=\"tn-icon cke cke-plus\"></span> <em>Insert</em> menu of the <a class=\"reference-link\" href=\"#root/_help_nRhnJkTT8cPs\">Formatting toolbar</a> and choose <span class=\"tn-icon bx bx-code-block\"></span> <em>Extended code block</em>, after <em>Columns</em>. It is also available as <em>Extended code block</em> in the <a class=\"reference-link\" href=\"#root/_help_ZlN4nump6EbW\">Slash Commands</a>.</li><li>A new block is empty and editable, in a <em>Full</em> box, and takes the focus. Its language is the one set last on an extended code block, or <em>Plain text</em>.</li><li>A click places the cursor in the code, as in a code block. Over a <em>Small</em> or <em>Medium</em> box, the mouse wheel scrolls the code.</li><li>The block shows in a box like the one of an included code note, with the corners and the shadow of a code block. While the block can be edited, its language shows in its top corner, as for a code block.</li><li>Its toolbar has the controls of an embedded code file, described in <em>Editing an include in place</em> in <a class=\"reference-link\" href=\"#root/_help_nBAXQFj20hS1\">Include Note</a>: the <span class=\"tn-icon bx bx-edit-alt\"></span> <em>Editable</em> button, the <em>Change language mode</em> and <em>Change tab width</em> menus, and the <span class=\"tn-icon bx bx-move-vertical\"></span> <em>Box size</em> menu, with <em>Small</em>, <em>Medium</em> and <em>Full</em>. Its <span class=\"tn-icon cke cke-three-vertical-dots\"></span> <em>More actions</em> menu has the <em>Word wrap</em> and <em>Line numbers</em> submenus.</li><li>The changes made in the block are part of the undo history of the text note.</li></ul><p>The code stays in the note as a code block of its language. The Markdown export, <a href=\"#root/_help_R9pX4DGra2Vt\">shared pages</a> and printing show it as a code block, and copying it into another application pastes a code block. Pasted into a text note, it becomes an extended code block again, with its settings.</p><p>Turning <em>Extended code blocks</em> off again removes them from the <em>Insert</em> menu and the slash commands only: the extended code blocks already in notes keep working. A version of Trilium without this feature shows them as code blocks, and saves them back as code blocks, without their box size and other settings.</p>",+ "_help_QxEyIjRBizuC": "<p><img src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/Developer-specific%20formatting/Code%20blocks_image.jpg\"></p><p>The code blocks feature allows entering pieces of code in text notes.</p><p>Note that this feature is meant for generally small snippets of code. For larger files such as an entire log, see the <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> note type instead.</p><h2>Inserting a code block</h2><ul><li>Via the <a class=\"reference-link\" href=\"#root/_help_nRhnJkTT8cPs\">Formatting toolbar</a>, look for the <span class=\"tn-icon cke cke-code-block\"></span> button.<ul><li>Pressing directly on the icon will insert a code block with the language that was selected most recently. If this is the first time a code block is inserted, the language will be “Auto-detected” by default.</li><li>Pressing the arrow next to the icon, which will show a popup with the available languages.</li></ul></li><li>Type <code>```</code> (as in Markdown).<ul><li>Note that it's not possible to specify the language, as it will default to the last selected language.</li></ul></li><li>Paste several lines of code copied from Visual Studio Code.<ul><li>The code block uses the language of the file in Visual Studio Code.</li><li>If that language is not in the list of languages, the code block uses <em>Auto-detected</em>.</li><li>The colors of the Visual Studio Code theme are removed.</li><li>A single line becomes inline code instead of a code block.</li><li>In Firefox, the code block always uses <em>Auto-detected</em>. Firefox can't read the language from Visual Studio Code.</li></ul></li></ul><h2>Exiting out of the code block</h2><ul><li>To exit out of a code block and enter a normal paragraph, move the cursor at the end of the code block and press Enter twice.</li><li>Similarly, to insert a paragraph above the note block, move the cursor at the beginning of the code block and press Enter twice.</li></ul><aside class=\"admonition note\"><p>If you've pasted a code block with a more complex HTML structure, exiting out of the code block by pressing Enter multiple times might not work. In that case the best approach is to delete the code block entirely and use Ctrl+Shift+V (paste as plain text).</p></aside><h2>Syntax highlighting & color schemes</h2><p>Since TriliumNext v0.90.12, Trilium will try to offer syntax highlighting to the code block. Note that the syntax highlighting mechanism is slightly different than the one in <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> notes as different technologies are involved.</p><p>Interaction:</p><ul><li>When the language is set to <em>Auto-detected</em> (by default), Trilium will try to identify the programming language (or similar) that corresponds to the given snippet of text and highlight it. Only the languages enabled in <a class=\"reference-link\" href=\"#root/_help_4TIF1oA4VQRO\">Options</a> → <em>Code Notes</em> → <em>Available MIME types in the dropdown</em> are considered. If this is problematic, consider changing the language of the code block manually.</li><li>When the language is set to <em>Plain text</em>, there will be no syntax highlighting.</li></ul><p>Note that when editing a text note, syntax highlighting is automatically disabled if the code block is too big (somewhere around 500 lines). In <a class=\"reference-link\" href=\"#root/_help_CoFPLs3dRlXc\">Read-Only Notes</a>, in print and on a <a href=\"#root/_help_R9pX4DGra2Vt\">shared page</a>, a code block of more than 500 lines or 50,000 characters is shown without syntax highlighting. These limits are not configurable.</p><p>In order to configure this new feature, a section has been added in <a class=\"reference-link\" href=\"#root/_help_4TIF1oA4VQRO\">Options</a> → <em>Appearance</em> to control the syntax highlighting. There the color scheme can be chosen, from a builtin selection of themes from Highlight.js. <em>VS Code (Light)</em> and <em>VS Code (Dark)</em> use the same colors as the default themes of <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> notes, so a code block and a code note color the same code alike.</p><ul><li>It is possible to disable the syntax highlighting for all the notes by selecting “No syntax highlighting” in the “Color scheme” option.</li><li>Word wrapping is disabled by default, but can be configured from the same section.</li><li>The tab width can also be adjusted from Options.</li></ul><aside class=\"admonition note\"><p><strong>Context regarding syntax highlighting</strong></p><p>In order to achieve the syntax highlight, the Highlight.js library is being used. Do note that support for syntax highlighting in code blocks is not a supported feature of the text editor we are using CKEditor), but rather a hack which makes use of the highlights API (used for highlighting search results for example). Nevertheless, we haven't noticed any major issues during the development of the feature, but feel free to report any issues you might have.</p><p>Most of the work to achieve the syntax highlight itself was already done by <a href=\"https://github.com/antoniotejada\">antoniotejada</a> in <a href=\"https://github.com/antoniotejada/Trilium-SyntaxHighlightWidget\">https://github.com/antoniotejada/Trilium-SyntaxHighlightWidget</a>. On our side we added customization but also additional functionality.</p></aside><h3>Migrating from existing syntax highlight plugins</h3><p>If you are already using a syntax highlighting plugin such as the <a href=\"https://github.com/antoniotejada/Trilium-SyntaxHighlightWidget\">Trilium-SyntaxHighlightWidget</a> we are basing off of, it is important to disable that plugin before upgrading in order for it not to conflict with our implementation.</p><p>Should you encounter any issues after the migration, try running Trilium in safe mode.</p><h2>Changing the language of a code block</h2><p>Simply click anywhere inside the code block and press again the code block button in the <a class=\"reference-link\" href=\"#root/_help_nRhnJkTT8cPs\">Formatting toolbar</a>:<br><img src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/Developer-specific%20formatting/Code%20blocks_image.png\"></p><h2>Adjusting the list of languages</h2><p>The code blocks feature shares the list of languages with the <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> note type.</p><p>The supported languages, and the order in which they are listed, can be adjusted by going to <a class=\"reference-link\" href=\"#root/_help_4TIF1oA4VQRO\">Options</a> → <em>Code Notes</em> → <em>Available MIME types in the dropdown</em>. See <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> for how to add, remove and reorder them.</p><p>Note that the list of languages is not immediately refreshed, you'd have to manually <a href=\"#root/_help_s8alTXmpFR61\">refresh the application</a>.</p><h2>Text replacements and ligatures</h2><p><a class=\"reference-link\" href=\"#root/_help_oI7GoyilXXAr\">Automatic replacements</a> are a feature of text notes which converts some character sequences into another and some of them interfere with programming text, such as the conversion of quotes. While in a code block, the automatic replacements <strong>are not effect</strong>. The only way the automatic replacements can interfere is if the text is typed/pasted before being put in a code block.</p><p>A different feature is <em>programming ligatures</em>, which is simply a display affordance which turns <code>!=</code> into <code>≠</code> or <code>-></code> as <code>→</code> . These come from the <a href=\"#root/_help_w4rMEhJsALlA\">font</a>. The underlying text is not actually changed, and copying it out gives you back <code>!=</code> and <code>-></code>.</p><p>Ligatures can be disabled in <a class=\"reference-link\" href=\"#root/_help_4TIF1oA4VQRO\">Options</a><em>→ Appearance → Fonts → Programming ligatures.</em></p><h2>Extended code blocks</h2><aside class=\"admonition important\"><p>Extended code blocks are experimental, and they change as they mature. To use them, turn on <em>Extended code blocks</em> in <a class=\"reference-link\" href=\"#root/_help_4TIF1oA4VQRO\">Options</a> → <em>Advanced</em> → <em>Experimental Options</em>.</p></aside><p>An extended code block is a code block that is edited with the code editor of a <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> note, inside the text. It has the same syntax highlighting and code folding as a code note, and it suits longer pieces of code.</p><ul><li>To insert one, open the <span class=\"tn-icon cke cke-plus\"></span> <em>Insert</em> menu of the <a class=\"reference-link\" href=\"#root/_help_nRhnJkTT8cPs\">Formatting toolbar</a> and choose <span class=\"tn-icon bx bx-code-block\"></span> <em>Extended code block</em>, after <em>Columns</em>. It is also available as <em>Extended code block</em> in the <a class=\"reference-link\" href=\"#root/_help_ZlN4nump6EbW\">Slash Commands</a>.</li><li>A new block is empty and editable, in a <em>Full</em> box, and takes the focus. Its language is the one set last on an extended code block, or <em>Plain text</em>.</li><li>A click places the cursor in the code, as in a code block. Over a <em>Small</em> or <em>Medium</em> box, the mouse wheel scrolls the code.</li><li>The block shows in a box like the one of an included code note, with the corners and the shadow of a code block. While the block can be edited, its language shows in its top corner, as for a code block.</li><li>Its toolbar has the controls of an embedded code file, described in <em>Editing an include in place</em> in <a class=\"reference-link\" href=\"#root/_help_nBAXQFj20hS1\">Include Note</a>: the <span class=\"tn-icon bx bx-edit-alt\"></span> <em>Editable</em> button, the <em>Change language mode</em> and <em>Change tab width</em> menus, and the <span class=\"tn-icon bx bx-move-vertical\"></span> <em>Box size</em> menu, with <em>Small</em>, <em>Medium</em> and <em>Full</em>. Before the <em>Box size</em> menu, the <span class=\"tn-icon cke cke-trilium-copy\"></span> <em>Copy to clipboard</em> button copies the code, as the one of a code block does. Its <span class=\"tn-icon cke cke-three-vertical-dots\"></span> <em>More actions</em> menu has the <em>Word wrap</em> and <em>Line numbers</em> submenus.</li><li>The changes made in the block are part of the undo history of the text note.</li></ul><p>The code stays in the note as a code block of its language. The Markdown export, <a href=\"#root/_help_R9pX4DGra2Vt\">shared pages</a> and printing show it as a code block, and copying it into another application pastes a code block. Pasted into a text note, it becomes an extended code block again, with its settings.</p><p>Turning <em>Extended code blocks</em> off again removes them from the <em>Insert</em> menu and the slash commands only: the extended code blocks already in notes keep working. A version of Trilium without this feature shows them as code blocks, and saves them back as code blocks, without their box size and other settings.</p>","_help_AgjCISero73a": "<figure class=\"image image-style-align-right\"><img style=\"aspect-ratio:199/187;\" src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/Footnotes_image.png\" width=\"199\" height=\"187\"></figure><p>Footnotes are a good place to insert references to a paragraph or details that are displayed at the bottom of the note.</p><h2>Interaction</h2><ul><li>To insert a new footnote, press <span class=\"tn-icon cke cke-trilium-insert-footnote\"></span> (or press the right arrow and select <em>New footnote</em>).</li><li>To insert a reference to an existing footnote, press the right arrow and select <em>Insert footnote</em>, followed by the number of the footnote to insert.</li><li>Hovering over a reference (e.g. <code>[1]</code>) will display the content of the footnote in a tooltip for easy reference.</li><li>Clicking on a reference in the text (e.g. <code>[1]</code>) will navigate to the corresponding footnote.</li><li>Similarly, clicking on the <code>^</code> button of a footnote will navigate to the corresponding reference in the text.</li></ul>","_help_5wZallV2Qo1t": "<figure class=\"image image-style-align-right\"><img style=\"aspect-ratio:220/76;\" src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/Format%20Painter_image.png\" width=\"220\" height=\"76\"></figure><p>The Format Painter is a feature in text notes that allows users to copy the formatting of text (such as <strong>bold</strong>, <em>italic</em>, <del>Strikethrough</del>, etc.) and apply it to other parts of the document. It helps maintain consistent formatting and accelerates the creation of rich content.</p><h2>Usage Instructions</h2><p>Click the text that you want to copy the formatting from and use the <em>Copy formatting</em> toolbar button (<span class=\"tn-icon cke cke-paint-roller\"></span>) to copy the style. Then select the target text with your mouse, or click a word, to apply the formatting.</p><ul><li><strong>To copy the formatting</strong>: Place the cursor inside text with some formatting and click the <em>Copy formatting</em> toolbar button. The mouse cursor changes to indicate the painter is armed.</li><li><strong>To paint with the copied formatting</strong>: Select the target text with your mouse. The formatting is applied when you release the button, and the painter disarms — the cursor returns to normal. Clicking or tapping a word applies it to the whole word.</li><li><strong>To paint a selection made otherwise</strong>, such as with the keyboard or the selection handles of a touch screen: select the target text, then use the toolbar button again, which reads <em>Apply formatting</em> once the selection has moved. On a phone, the formatting row stays open while the painter is armed, so the button stays in reach.</li><li><strong>To cancel without painting</strong>: Click the toolbar button again before selecting other text, or press <kbd>Escape</kbd>.</li></ul><h2>Limitations</h2><ol><li>Painting with block-level formatting (like headings or image styles) is not supported yet. This is because, in <a class=\"reference-link\" href=\"#root/_help_MI26XDLSAlCD\">CKEditor</a>, they are considered a part of the content rather than text formatting.</li><li>A click outside a word, such as between two spaces or at the end of a line, only primes the caret, so text you type there takes the copied formatting.</li><li>The painter is one-shot; each paint requires copying the formatting again.</li></ol>","_help_nRhnJkTT8cPs": "<p>Trilium allows two different editing experiences for text notes, based on your preference.</p><p>The type of formatting toolbar can be changed by going to <a class=\"reference-link\" href=\"#root/_help_4TIF1oA4VQRO\">Options</a> and then looking for the <a class=\"reference-link\" href=\"#root/_hidden/_options/_optionsTextNotes\">Text Notes</a> section. In it, look for the <em>Formatting toolbar</em> category.</p><h2>Floating</h2><p>The floating bar is a more minimalist option, in which the formatting is hidden behind two different popups.</p><h3>Block-level popup</h3><p>The first popup is the block-level popup. This will appear to the left of the current paragraph you are currently editing, as a sequence of dots:<br><img src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/Formatting%20toolbar_image.jpg\"></p><p>Clicking this popup will reveal the formatting options that are specific to the entire paragraph, such as setting a heading, lists, tables, etc.</p><p>The dots are also a drag handle: drag them to move the paragraph, all the selected paragraphs, or a selected element such as an image, a table or an included note to another place in the note. Right-clicking them copies a reference to the paragraph or to the selected paragraphs, see <a class=\"reference-link\" href=\"#root/_help_klC3ilXa9kUE\">Block references</a>.</p><figure class=\"image\"><img style=\"aspect-ratio:1404/156;\" src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/2_Formatting%20toolbar_image.png\" width=\"1404\" height=\"156\"></figure><h3>Inline popup</h3><p>The inline popup appears automatically when selecting a text:</p><figure class=\"image\"><img style=\"aspect-ratio:1008/154;\" src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/1_Formatting%20toolbar_image.png\" width=\"1008\" height=\"154\"></figure><p>All the options here apply only to the selected portion of text, such as changing the font size, or making the text bold.</p><h2>Fixed</h2><p>The fixed formatting offers a more traditional editing paradigm, usually useful if a lot of formatting is needed.</p><p>All the options are combined into a single toolbar that will appear at the above the note. When using <a class=\"reference-link\" href=\"#root/_help_luNhaphA37EO\">Split View</a>, a single toolbar will be available to save space and it acts on the active note.</p><p>The sequence of dots to the left of the current paragraph is still shown, but only as a drag handle: drag it to move the paragraph, all the selected paragraphs, or a selected element such as an image, a table or an included note to another place in the note. Releasing it above the first paragraph or below the last one moves it to the start or the end of the note. Clicking it does not open a popup, and right-clicking it copies a reference to the block, see <a class=\"reference-link\" href=\"#root/_help_klC3ilXa9kUE\">Block references</a>. Hovering the drag handle shades the blocks that it moves: inside a table, this is the paragraph in the current cell, and with several cells selected, the whole table. The drag handle is not shown in the mobile layout.</p><figure class=\"image\"><img style=\"aspect-ratio:1440/60;\" src=\"{{helpAssets}}/User%20Guide/Note%20Types/Text/Formatting%20toolbar_image.png\" width=\"1440\" height=\"60\"></figure><p>By default, if the toolbar does not fit on the screen, the rest of the options will be grouped into a single option at the end. It is possible to display items on multiple lines by checking the corresponding option in the <em>Formatting toolbar</em> section.</p>",diff --git a/docs/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.md b/docs/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.mdindex b1cb9b3572..288a7090c6 100644--- a/docs/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.md+++ b/docs/User Guide/User Guide/Note Types/Text/Developer-specific formatting/Code blocks.md@@ -89,7 +89,7 @@ An extended code block is a code block that is edited with the code editor of a* A new block is empty and editable, in a _Full_ box, and takes the focus. Its language is the one set last on an extended code block, or _Plain text_.* A click places the cursor in the code, as in a code block. Over a _Small_ or _Medium_ box, the mouse wheel scrolls the code.* The block shows in a box like the one of an included code note, with the corners and the shadow of a code block. While the block can be edited, its language shows in its top corner, as for a code block.-* Its toolbar has the controls of an embedded code file, described in _Editing an include in place_ in <a class="reference-link" href="../Include%20Note.md">Include Note</a>: the <span class="tn-icon bx bx-edit-alt"></span> _Editable_ button, the _Change language mode_ and _Change tab width_ menus, and the <span class="tn-icon bx bx-move-vertical"></span> _Box size_ menu, with _Small_, _Medium_ and _Full_. Its <span class="tn-icon cke cke-three-vertical-dots"></span> _More actions_ menu has the _Word wrap_ and _Line numbers_ submenus.+* Its toolbar has the controls of an embedded code file, described in _Editing an include in place_ in <a class="reference-link" href="../Include%20Note.md">Include Note</a>: the <span class="tn-icon bx bx-edit-alt"></span> _Editable_ button, the _Change language mode_ and _Change tab width_ menus, and the <span class="tn-icon bx bx-move-vertical"></span> _Box size_ menu, with _Small_, _Medium_ and _Full_. Before the _Box size_ menu, the <span class="tn-icon cke cke-trilium-copy"></span> _Copy to clipboard_ button copies the code, as the one of a code block does. Its <span class="tn-icon cke cke-three-vertical-dots"></span> _More actions_ menu has the _Word wrap_ and _Line numbers_ submenus.* The changes made in the block are part of the undo history of the text note.The code stays in the note as a code block of its language. The Markdown export, [shared pages](../../../Advanced%20Usage/Sharing.md) and printing show it as a code block, and copying it into another application pastes a code block. Pasted into a text note, it becomes an extended code block again, with its settings.diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.spec.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.spec.tsindex e72ebbfe64..06db6b353b 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.spec.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.spec.ts@@ -13,6 +13,7 @@ import ContentEmbed, {import ContentEmbedBoxSizeDropdown from "./content_embed_box_size_dropdown.js";import ContentEmbedToolbar from "./content_embed_toolbar.js";import ContentEmbedTools, { CONTENT_EMBED_TOOLS } from "./content_embed_tools.js";+import ExtendedCodeBlock, { EXTENDED_CODE_BLOCK_COPY_BUTTON } from "./extended_code_block.js";// ---------------------------------------------------------------------------// Minimal inline plugin that registers a widget without a class attribute.@@ -94,6 +95,7 @@ describe("ContentEmbedToolbar", () => {expect(requires).toContain(ContentEmbed);expect(requires).toContain(ContentEmbedBoxSizeDropdown);expect(requires).toContain(ContentEmbedTools);+ expect(requires).toContain(ExtendedCodeBlock);});it("offers Editable, the display modes, the content's tools, then the embed's items", () => {@@ -108,6 +110,7 @@ describe("ContentEmbedToolbar", () => {"contentEmbedDisplayMode:split","contentEmbedDisplayMode:preview",CONTENT_EMBED_TOOLS,+ EXTENDED_CODE_BLOCK_COPY_BUTTON,"contentEmbedBoxSizeDropdown",TOGGLE_TITLE_COMMAND_NAME,TOGGLE_CAPTION_COMMAND_NAME,diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.tsindex e9f8272659..8970d7dcba 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.ts@@ -13,12 +13,14 @@ import ContentEmbedBoxSizeDropdown, {CONTENT_EMBED_BOX_SIZE_DROPDOWN} from "./content_embed_box_size_dropdown.js";import ContentEmbedTools, { CONTENT_EMBED_TOOLS } from "./content_embed_tools.js";+import ExtendedCodeBlock, { EXTENDED_CODE_BLOCK_COPY_BUTTON } from "./extended_code_block.js";export default class ContentEmbedToolbar extends Plugin {static get requires() {return [- WidgetToolbarRepository, ContentEmbed, ContentEmbedBoxSizeDropdown, ContentEmbedTools+ WidgetToolbarRepository, ContentEmbed, ContentEmbedBoxSizeDropdown, ContentEmbedTools,+ ExtendedCodeBlock] as const;}@@ -32,6 +34,7 @@ export default class ContentEmbedToolbar extends Plugin {CONTENT_EMBED_DISPLAY_MODE_SEPARATOR,...DISPLAY_MODES.map((mode) => DISPLAY_MODE_BUTTONS[mode]),CONTENT_EMBED_TOOLS,+ EXTENDED_CODE_BLOCK_COPY_BUTTON,CONTENT_EMBED_BOX_SIZE_DROPDOWN,TOGGLE_TITLE_COMMAND_NAME,TOGGLE_CAPTION_COMMAND_NAME,diff --git a/packages/ckeditor5/src/plugins/content_embed/extended_code_block.spec.ts b/packages/ckeditor5/src/plugins/content_embed/extended_code_block.spec.tsindex 317b68ea33..823a3d81b4 100644--- a/packages/ckeditor5/src/plugins/content_embed/extended_code_block.spec.ts+++ b/packages/ckeditor5/src/plugins/content_embed/extended_code_block.spec.ts@@ -1,12 +1,14 @@import {- _setModelData as setModelData, ButtonView, ClassicEditor, Essentials, Paragraph, Widget+ _setModelData as setModelData, ButtonView, ClassicEditor, Essentials, type ModelElement,+ Paragraph, Widget} from "ckeditor5";import { describe, expect, it, vi } from "vitest";import { createTestEditor } from "../../../test/editor-kit.js";import { CODE_VIEW_COMMAND_NAME } from "./content_embed.js";import ExtendedCodeBlock, {- EXTENDED_CODE_BLOCK_BUTTON, INSERT_EXTENDED_CODE_BLOCK_COMMAND, insertExtendedCodeBlock+ EXTENDED_CODE_BLOCK_BUTTON, EXTENDED_CODE_BLOCK_COPY_BUTTON, INSERT_EXTENDED_CODE_BLOCK_COMMAND,+ insertExtendedCodeBlock} from "./extended_code_block.js";import { INSERT_INLINE_EMBED_COMMAND } from "./inline_source.js";@@ -17,8 +19,8 @@ describe("ExtendedCodeBlock", () => {});}- function createButton(editor: ClassicEditor) {- const button = editor.ui.componentFactory.create(EXTENDED_CODE_BLOCK_BUTTON);+ function createButton(editor: ClassicEditor, name = EXTENDED_CODE_BLOCK_BUTTON) {+ const button = editor.ui.componentFactory.create(name);if (!(button instanceof ButtonView)) throw new Error("Expected a button.");return button;}@@ -60,4 +62,51 @@ describe("ExtendedCodeBlock", () => {expect(editor.getData()).toContain(`<code class="language-text-x-python"></code>`);});++ it("copies the code of the selected block as the code block copy button does", async () => {+ const copy = vi.fn();+ let flushInline = () => {};+ const plugins = [ Essentials, Paragraph, Widget, ExtendedCodeBlock ];+ const editor = await createTestEditor(plugins, {+ clipboard: { copy },+ contentEmbed: {+ inlineLanguages: [ "mermaid" ],+ loadInline: vi.fn(),+ flushInline: () => flushInline()+ }+ });+ editor.setData(`<pre class="extended-code-block">`+ + `<code class="language-text-x-python">print(1)</code></pre>`+ + `<pre><code class="language-mermaid">graph LR;</code></pre>`);+ const root = editor.model.document.getRoot();+ const [ codeBlock, diagram ] = Array.from(root?.getChildren() ?? []);+ if (!codeBlock?.is("element") || !diagram?.is("element")) {+ throw new Error("Expected embeds.");+ }+ const select = (embed: ModelElement) => {+ editor.model.change((writer) => writer.setSelection(embed, "on"));+ editor.ui.update();+ };+ const button = createButton(editor, EXTENDED_CODE_BLOCK_COPY_BUTTON);+ const legacyButton = createButton(editor, "copyToClipboard");++ select(codeBlock);+ expect([ button.label, button.icon, button.isVisible ])+ .toEqual([ legacyButton.label, legacyButton.icon, true ]);+ // The host writes the code typed last when the copy asks for it.+ flushInline = () => editor.model.change((writer) => {+ writer.setAttribute("source", "print(2)", codeBlock);+ });+ button.fire("execute");+ expect(copy).toHaveBeenCalledExactlyOnceWith("print(2)");++ select(diagram);+ expect(button.isVisible).toBe(false);++ // An empty block copies nothing, as an empty code block does.+ flushInline = () => {};+ setModelData(editor.model, `[<contentEmbed language="text-x-python"></contentEmbed>]`);+ button.fire("execute");+ expect(copy).toHaveBeenCalledOnce();+ });});diff --git a/packages/ckeditor5/src/plugins/content_embed/extended_code_block.ts b/packages/ckeditor5/src/plugins/content_embed/extended_code_block.tsindex 29c120ade8..2d611ca5ac 100644--- a/packages/ckeditor5/src/plugins/content_embed/extended_code_block.ts+++ b/packages/ckeditor5/src/plugins/content_embed/extended_code_block.ts@@ -4,22 +4,31 @@ import {ButtonView, Command, type DowncastAttributeEvent, type Editor, type ModelElement, Plugin} from "ckeditor5";+import CopyToClipboardButton, { createCopyToClipboardButton } from "../copy_to_clipboard_button.js";import ContentEmbed from "./content_embed.js";-import { INSERT_INLINE_EMBED_COMMAND, isExtendedCodeBlock } from "./inline_source.js";+import {+ getSelectedExtendedCodeBlock, INSERT_INLINE_EMBED_COMMAND, isExtendedCodeBlock+} from "./inline_source.js";export const INSERT_EXTENDED_CODE_BLOCK_COMMAND = "insertExtendedCodeBlock";/** The toolbar button that inserts an extended code block. */export const EXTENDED_CODE_BLOCK_BUTTON = "extendedCodeBlock";+/** The button of the embed toolbar that copies the code of the selected extended code block. */+export const EXTENDED_CODE_BLOCK_COPY_BUTTON = "extendedCodeBlockCopy";+/*** Extended code blocks: code blocks that the host shows and edits in its code editor, inside an* embed. `ContentEmbedInlineSource` saves each one as a `<pre><code class="language-…">` code block* of the note. This plugin adds the command and the button that insert one.*/export default class ExtendedCodeBlock extends Plugin {+ /** Whether the selection is on an extended code block. */+ declare public isSelected: boolean;+static get requires() {- return [ ContentEmbed ] as const;+ return [ ContentEmbed, CopyToClipboardButton ] as const;}static get pluginName() {@@ -32,6 +41,12 @@ export default class ExtendedCodeBlock extends Plugin {const command = new InsertExtendedCodeBlockCommand(editor);editor.commands.add(INSERT_EXTENDED_CODE_BLOCK_COMMAND, command);+ this.set("isSelected", false);+ // Runs before `WidgetToolbarRepository` positions the toolbar from its width.+ this.listenTo(editor.ui, "update", () => {+ this.isSelected = !!getSelectedExtendedCodeBlock(editor);+ }, { priority: "high" });+// A new block takes the language that was set last on a code block.editor.editing.downcastDispatcher.on<DowncastAttributeEvent<ModelElement>>("attribute:language:contentEmbed",@@ -50,6 +65,12 @@ export default class ExtendedCodeBlock extends Plugin {this.listenTo(button, "execute", () => insertExtendedCodeBlock(editor));return button;});++ editor.ui.componentFactory.add(EXTENDED_CODE_BLOCK_COPY_BUTTON, (locale) => {+ const button = createCopyToClipboardButton(editor, locale);+ button.bind("isVisible").to(this, "isSelected");+ return button;+ });}}diff --git a/packages/ckeditor5/src/plugins/content_embed/inline_source.ts b/packages/ckeditor5/src/plugins/content_embed/inline_source.tsindex 1497bd045c..f15761f20a 100644--- a/packages/ckeditor5/src/plugins/content_embed/inline_source.ts+++ b/packages/ckeditor5/src/plugins/content_embed/inline_source.ts@@ -245,6 +245,15 @@ export function isExtendedCodeBlock( editor: Editor, embed: ModelElement ) {return typeof language === 'string' && !getInlineLanguages( editor ).includes( language );}+/** The extended code block that the selection is on, or `null`. */+export function getSelectedExtendedCodeBlock( editor: Editor ): ModelElement | null {+ const embed = editor.model.document.selection.getSelectedElement();+ if ( !embed?.is( 'element', 'contentEmbed' ) ) {+ return null;+ }+ return isExtendedCodeBlock( editor, embed ) ? embed : null;+}+/** Saves an embed of a source as `<pre><code class="language-…">`. */function downcastData( editor: Editor, embed: ModelElement, api: DowncastConversionApi ) {if ( !isInlineSourceEmbed( embed ) || !api.consumable.consume( embed, 'insert' ) ) {diff --git a/packages/ckeditor5/src/plugins/copy_to_clipboard_button.ts b/packages/ckeditor5/src/plugins/copy_to_clipboard_button.tsindex 6ca9fd5e9b..3c233f3971 100644--- a/packages/ckeditor5/src/plugins/copy_to_clipboard_button.ts+++ b/packages/ckeditor5/src/plugins/copy_to_clipboard_button.ts@@ -1,5 +1,6 @@-import { ButtonView, Command, Plugin } from "ckeditor5";+import { ButtonView, Command, type Editor, type Locale, Plugin } from "ckeditor5";import copyIcon from "../icons/copy.svg?raw";+import { getSelectedExtendedCodeBlock } from "./content_embed/inline_source.js";export default class CopyToClipboardButton extends Plugin {@@ -8,26 +9,31 @@ export default class CopyToClipboardButton extends Plugin {editor.commands.add("copyToClipboard", new CopyToClipboardCommand(this.editor));const componentFactory = editor.ui.componentFactory;- componentFactory.add("copyToClipboard", locale => {- const button = new ButtonView(locale);- // A `label` rather than a literal `tooltip` string: the tooltip reads the same, and the- // icon-only button gains the accessible name it had no way to expose before.- button.set({- label: editor.t("Copy to clipboard"),- tooltip: true,- icon: copyIcon- });-- this.listenTo(button, "execute", () => {- editor.execute("copyToClipboard");- });-- return button;+ componentFactory.add("copyToClipboard", (locale) => {+ return createCopyToClipboardButton(editor, locale);});}}+/** A button that copies the code at the selection through the `copyToClipboard` command. */+export function createCopyToClipboardButton(editor: Editor, locale: Locale) {+ const button = new ButtonView(locale);+ // A `label` rather than a literal `tooltip` string: the tooltip reads the same, and the+ // icon-only button gains the accessible name it had no way to expose before.+ button.set({+ label: editor.t("Copy to clipboard"),+ tooltip: true,+ icon: copyIcon+ });++ button.on("execute", () => {+ editor.execute("copyToClipboard");+ });++ return button;+}+export class CopyToClipboardCommand extends Command {private executeCallback?: (text: string) => void;@@ -41,6 +47,15 @@ export class CopyToClipboardCommand extends Command {this.executeCallback = this.editor.config.get("clipboard")?.copy;}+ const embed = getSelectedExtendedCodeBlock(editor);+ if (embed) {+ // The host writes the code typed last into the model after a delay.+ editor.config.get("contentEmbed")?.flushInline?.();+ const source = embed.getAttribute("source") as string | undefined;+ this.copyText(source ?? "", "extended code block");+ return;+ }+// Try code block firstconst codeBlockEl = selection.getFirstPosition()?.findAncestor("codeBlock");if (codeBlockEl) {