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

Event-based API doesn't upcast HTML tag #16349

Open
sociopart opened this issue May 14, 2024 · 0 comments
Open

Event-based API doesn't upcast HTML tag #16349

sociopart opened this issue May 14, 2024 · 0 comments
Labels
type:question This issue asks a question (how to...).

Comments

@sociopart
Copy link

sociopart commented May 14, 2024

Hello.
I have this HTML block which is correctly exporting from CKEditor mentions plugin.

<pt_mention class="mention" data-type="mention" data-mention="@Sample User" data-user-id="000" href="/users/000">@=Sample User</pt_mention>

I want to convert it back to CKEditor, so I tried to write a dispatcher using event-based API.

  editor.data.upcastDispatcher.on('element:pt_mention', (evt, data, conversionApi) => {
    console.log('Upcast started');

    const {
        consumable,
        writer,
        safeInsert,
        updateConversionResult
    } = conversionApi;

    const { viewItem } = data;


    const mentionClass = { name: true, classes: 'mention' };

    if (!consumable.test(viewItem, mentionClass)) {
        return;
    }

    const modelElement = writer.createElement('span', {
        'class': 'mention',
        'data-mention': viewItem.getAttribute('data-mention')
    });


    if (!safeInsert(modelElement, data.modelCursor)) {
        console.log("error")
        return;
    }

    modelElement.textContent = viewItem.getTextContent();

    consumable.consume(viewItem, mentionClass);

    updateConversionResult(modelElement, data);
});

But it keeps logging "error" to console, doesn't handle any attributes and creates <p> tag which only contains text.

The result should look like <span class="mention" .... >

So, what I'm doing wrong and how it should be implemented?

Thanks in advance!

@sociopart sociopart added the type:question This issue asks a question (how to...). label May 14, 2024
@sociopart sociopart changed the title Try to understand why event-based API doesn't work Event-based API doesn't upcast HTML tag May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question This issue asks a question (how to...).
Projects
None yet
Development

No branches or pull requests

1 participant