ckeditor5 icon indicating copy to clipboard operation
ckeditor5 copied to clipboard

Editor crashed when deleting content of span. Error : move-operation-node-into-itself: Trying to move a range of nodes into one of nodes from that range.

Open josuemarlique opened this issue 7 years ago • 8 comments

Is this a bug report or feature request?

🐞 Bug report

I enabled span for my build of ckeditor, but when i press the backspace button, it crashes ck editor and throws the error

move-operation-node-into-itself: Trying to move a range of nodes into one of nodes from that range.

Another error i notice is that when I press enter twice it doesn't escape the span, but it creates a new span.

Here is my class ( i need to allow any type to be placed in the span, ie, $block, $text)

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import { toWidget, toWidgetEditable } from '@ckeditor/ckeditor5-widget/src/utils';
import Widget from '@ckeditor/ckeditor5-widget/src/widget';

export default class SpanEditing extends Plugin {
    static get requires() {                                   
        return [Widget];
    }

    init() {
        this._defineSchema();   
        this._defineConverters();
    }

    _defineSchema() {    
        const schema = this.editor.model.schema;

        schema.register('span', {
            allowIn: ['$block'],
            allowWhere: ['$block', 'div', '$text'],
            allowContentOf: ['$root', '$block', '$text'],
        });
    }

    _defineConverters() {                                               
        const conversion = this.editor.conversion;

        conversion.for('upcast').elementToElement({
            model: 'span',
            view: {
                name: 'span'
            }
        });

        conversion.for('dataDowncast').elementToElement({
            model: 'span',
            view: {
                name: 'span'
            }
        });

        conversion.for('editingDowncast').elementToElement({
            model: 'span',
            view: (modelElement, viewWriter) => {
                const span = viewWriter.createEditableElement('span');

                return toWidgetEditable(span, viewWriter);
            }
        });
    }
}

josuemarlique avatar Jul 29 '19 21:07 josuemarlique

@jodator, can you check it?

Mgsy avatar Jul 30 '19 07:07 Mgsy

It looks like you're trying to define <span> (generally an inline element) as a block. The thing with the CKEditor is that we do not operate on generic span on div elements. We tend to define features that are represented in a model as meaningful elements or attributes. For instance, the font feature uses <span style="font-family:foo"> in the view to display model's $text attribute fontFamily. The bold feature might be configured do display $text's bold attribute as a <span style="font-weight:bold">. We do merge similar elements int the view, so if both above attributes will be applied on a text then the CKEditor will render <span style="font-family:foo;font-weight:bold;"> on such text.

Such a general answer to this question is because I don't know what kind of a feature you're trying to create.

jodator avatar Jul 30 '19 10:07 jodator

Oh okay thanks for the quick follow up. Basically my issue is i tried setting it up as inline, but it doesn't seems to work, it somewhat works when it was set to block.

Here is the code i tried to enable it as inline

schema.register('span', {
            allowIn: '$block',
            inheritAllFrom: '$text',
        });

If i use the code above and try to add this code

<p><span style="margin: 4px;">example </span></p>

the editor removes it.

I'm also wondering does span need to be a widget? I've been following this guide (https://ckeditor.com/docs/ckeditor5/latest/framework/guides/tutorials/implementing-an-inline-widget.html)

josuemarlique avatar Jul 30 '19 15:07 josuemarlique

Is there anyway to get the span working? here is my updated SpanEditing class

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';

export default class SpanEditing extends Plugin {

    init() {
        this._defineSchema();   
        this._defineConverters();
    }

    _defineSchema() {    
        const schema = this.editor.model.schema;

        schema.register('span', {
            inheritAllFrom: '$text',
            isInline: true
        });
    }

    _defineConverters() {                                               
        const conversion = this.editor.conversion;

        conversion.for('upcast').elementToElement({
            model: 'span',
            view: {
                name: 'span'
            }
        });

        conversion.for('downcast').elementToElement({
            model: 'span',
            view: {
                name: 'span'
            }
        });

    }
}

all that i want to do is to be able to add this code:

<div>
    <p>
        <span>Testing span</span>
    </p>
</div>

but it gets converted to

<div>
    <p>
        <span>&nbsp;</span>
    </p>
</div>

josuemarlique avatar Aug 09 '19 21:08 josuemarlique

I'd go with:

schema.$extend( '$text', {
    allowAttributes: [ 'span' ]
} );

editor.conversion.for( 'upcast', {
    model: 'span',
    view: 'span'
} );

editor.conversion.for( 'downcast', {
    model: 'span',
    view: 'span'
} );

to get selection properly work with inline HTML elements, like <span>. but generally speaking, such general element will probably get into the way of other features that also uses <span> in the data like Font feature.

So in order to propose something that will work properly, it would be better to know what kind of feature you're trying to implement? Do you want to edit existing span in the data with all their attributes or does your feature changes the text in some distinctive way:

  • like bold - the text is bold or not
  • font-family - the text can have a font attribute with a value
  • font color - the text can have a color value

based from that the converter might be as simple as in bold feature: https://github.com/ckeditor/ckeditor5-basic-styles/blob/master/src/bold/boldediting.js#L30-L49 or can have a more complex definition that allows some or all values as in font: https://github.com/ckeditor/ckeditor5-font/blob/master/src/fontcolor/fontcolorediting.js#L98-L124.

Note that all text formatting features, including bold, can be configured to render as <span> elements in the data.

jodator avatar Aug 12 '19 08:08 jodator

Thanks for getting back to me. I want to edit existing span in the data with all their attributes. The text will not change.

josuemarlique avatar Aug 12 '19 14:08 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 11 '23 05:10 CKEditorBot