-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Code blocks #436
Comments
We certainly plan to support code blocks. I added them to the roadmap but for now to the "after 1.0.0" section. With code blocks there's a question about the amount of work. A simple implementation where you can change the type of block to "code block" (which is kinda similar to applying a heading) would be pretty simple. Then, there's Enter support and white space handling (white spaces need to be preserved during the DOM<->view conversion). That would be step one. We might be able to squeeze this before 1.0.0 (if other things will go well). However, what I'd love to see is a real code editor (like CodeMirror) embedded directly inside CKEditor. This would provide syntax highlighting, better Tab and indentation handling and other features that you'd expect from a code editor. This, of course, means a lot more work since we haven't yet worked on such embedding in CKEditor 5 (so, we don't know what may go wrong). How would you see this feature? |
Hi Reinmar, thanks for clearing that up :) I discussed it and for us it would be most useful to simply be able to change something in the editor from plain text to a code block. A proper code editor would be awesome, but it is much more valuable for us to simply be able to display something as a code block for the immediate future, versus a whole editor. Usually the code is just copy pasted from elsewhere, and displaying it would be enough for now. Our 2 cents :) |
It's a good point that most often we're just pasting the code from a real code editor anyway. One more question – would a possibility to pick a language (JS, HTML, Python, ...) be critical? Some code highlighters can detect a language but I'm not sure how good they are in this. |
While certainly a nice feature, we don't consider it as critical currently. Just having the ability to have it displayed as a properly formatted block would help tons already :) |
I would think inline code within a paragraph and perhaps simple display of a block would be well-handled by CKEditor, but some way to embed a proper editor as it's own entity would be great. |
Hi, while a |
Hi,
|
any news on this it's really an important plugin to have when ckeditor5 reach final simple codesnippet like we have in version 4 is more than enough as starting point |
Thanks for the feedback :) We need to prioritise missing features and, TBH, in case of this one the priority is unclear. Theoretically, from all the editor users, only a really small percentage of them (mainly, developers) need to embed code blocks. At the same time, it's one of the most often requested features. My guess is that early adopters often look for solutions for their own projects which are often developer-oriented. Am I right? Or do we miss something? |
With the help of Szymon, I managed to get the up- and downcasting of I have created a separate plugin to track the progress while we're working on it. https://github.com/opf/ckeditor5-code-block, and would appreciate any input on the issues [1] and [2] |
This should be enough to handle the enter key: editor.editing.view.document.on( 'enter', ( evt, data ) => {
if ( insideAPre ) {
editor.execute( 'input', { text: '\n' } );
evt.stop(); // Prevent executing the default handler.
data.preventDefault();
editor.editing.view.scrollToTheSelection();
}
} ); However, I'm afraid that we are not handling |
Enter does work now, thanks to you both for your support. I also had to listen to backspace to detect an empty code block in order to remove both elements. What remains now is escaping the code block. I'm wondering whether it's possible to add some kind of placeholder before and after the |
I was wrong, fortunately. We're filtering out content of pre-like elements earlier in that function: Good for us :)
I replied in your ticket, but I don't have a good idea for how to approach that. |
I don't understand why that was needed. Last backspace should remove Model: View: Which should remove both blocks. I am curious why it wouldn't work 🤔 . |
@oliverguenther @Reinmar I think escaping the code block by hitting enter a couple of times could work well, here is an example of quilljs escaping |
any updates on when code block feature (for longer code snippets) will launch..!! |
I did not yet manage to address the open issues here since we diverted to a widget with a codemirror instance with proper highlighting (however, not editable within the widget). |
I wrote an ugly plugin for code blocks: https://github.com/Yeolar/ckeditor5-code-block <pre><p>def func(x):
return x</p></pre> and the page to be displayed can use highlight.js to do more rendering, add: <script src="/static/highlight/highlight.min.js"></script>
<link rel="stylesheet" type="text/css" href="/static/highlight/styles/default.css" />
<script>
$(document).ready(function() {
$('pre p').each(function(i, block) { // use <pre><p>
hljs.highlightBlock(block);
});
});
</script> I am not familiar with ckeditor, anyone would like to enhance this plugin is welcomed. |
I like that direction. Would prove helpful for downstream projects as well. Probably if you implement anything less feature-complete, people will ask for those missing features anyway. Syntax highlighted code blocks embedded in textual notes, possibly with an added copy button to copy the entire block's contents similar to Boostnote, would prove very helpful if you want to document your code snippets. See image for an example of what I mean. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
As temp solution. |
So, we've had a call to wrap up what we did already and problems/ideas/obstacles that appeared. We understood that there are two quite distinctive features and due to limitations and requirements, it's hard to implement just one feature which will support all of them. Anyway, we'll focus on on the first of them. Other notes from the meeting: Code blocks
Code snippets
|
One more requirement to keep in mind – migration from CKE4. There are two related features – "pre" format in the format dropdown and the codesnippet plugin. It'd be great if they were preserved when loaded in CKE5. |
Feature: Initial implementation of the code block feature. Closes ckeditor/ckeditor5#436. Closes ckeditor/ckeditor5#5664. Closes ckeditor/ckeditor5#5666.
Feature: Added styles for the code block feature UI (see ckeditor/ckeditor5#436).
@jodator could you open a new ticket for the Code snippets feature described above? |
Done: #5769 |
Please a syntax highlighting |
I successfully configured this plug-in CKEditor5-CodeBlock-With-Syntax-Highlight in ckeditor5. It is great in edit mode, but it does not display correctly in preview mode. |
Could you please let me refer to your code? I used vue3 to build online. I went to find out that there is no such plug-in at present, and I kept reporting error modules repeatedly imported when I referenced them |
Is there any support planned (or available) for code blocks? The example below doesn't seem to function, and the editor renders it as a
<p>
element.Example:
If you'd like to see this feature implemented, please add 👍to this comment.
The text was updated successfully, but these errors were encountered: