ckeditor5 icon indicating copy to clipboard operation
ckeditor5 copied to clipboard

Apply heading to selection only

Open josuemarlique opened this issue 7 years ago • 7 comments

Is this a bug report or feature request?

Other = > question

💻 Version of CKEditor

Custom CK5 based on classic build

❓ Question

How can I change the default behavior of the heading plugin when applying it to a selection.

Example:

Before selecting the text "test" and applying a heading

<p> this is a test sentence</p>

After selecting the text "test" and applying a heading

<p> this is a</p>
<h2>test</h2> 
<p>sentence</p>

Steps I've taken to achieve this feature

Replace the execute function in headingcommand.js with:

execute( options ) {
		const model = this.editor.model;
		const document = model.document;
		const modelElement = options.value; // the heading name

        model.change(writer => {
            const heading = writer.createElement(modelElement);

            //TODO: get selected text, and pass to insert text
            writer.insertText('example text', heading);

            model.insertContent(heading);
		});
	}

Sadly this only replaces the text and doesn't split the node

josuemarlique avatar Sep 16 '19 16:09 josuemarlique

@jodator, can you take a look at it?

Mgsy avatar Sep 23 '19 12:09 Mgsy

Is this doable in the current version of ckeditor5 or would this require a lot of workaround?

josuemarlique avatar Sep 27 '19 17:09 josuemarlique

Hi @josueexhume there's no easy API way to do it. I've came with something like this but this is a bit hacky (uses "enter" command to split blocks - as block splitting isn't easiest thing to do).

const model = editor.model;
const doc = model.document;

model.change( writer => {
	// Cache the selected text.
	const frag = model.getSelectedContent( doc.selection );

	// Remove current selection.
	model.deleteContent( doc.selection );

	// Split current block (selection after deleteContent is collapsed).
	editor.execute( 'enter' );

	// Create a heading element and insert a cached text from frag.
	const heading = writer.createElement( 'heading2' );
	writer.append( frag.getChild( 0 ), heading );

	// Insert content before current selection focus.
	model.insertContent( heading, writer.createPositionBefore( doc.selection.focus.parent ) );
} );

showcase: Peek 2019-10-01 16-27

jodator avatar Oct 01 '19 14:10 jodator

uses "enter" command to split blocks - as block splitting isn't easiest thing to do

Until now we could just use model.insertContent(), because splitting always was connected with inserting a new object. Maybe we need a new utility method in the writer. AFAIR, in this case, splitting "with" a non-object element (heading) will not work as intended.

scofalik avatar Oct 01 '19 14:10 scofalik

Thanks, @jodator for now this somewhat works. The other use case that it didn't work for, was if I used it on multiple text blocks.

josuemarlique avatar Oct 18 '19 14:10 josuemarlique

There has been no activity on this issue for the past two years. We've marked it as stale and will close it in 30 days. We understand it may be relevant, so if you're interested in the solution, leave a comment or reaction under this issue.

CKEditorBot avatar Aug 31 '23 08:08 CKEditorBot

We've closed your issue due to inactivity over the last year. We understand that the issue may still be relevant. If so, feel free to open a new one (and link this issue to it).

CKEditorBot avatar Oct 05 '23 05:10 CKEditorBot