question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

editor.ui.registry.addButton doesn't work in Angular app when changing pages

See original GitHub issue

What is the current behavior? Describe the bug If you have a TinyMCE editor on a page where you’ve added a button using editor.ui.registry.addButton then change to another page and then go back to the first page, the custom button doesn’t show up anymore. The button is there after page refresh but not after Angular page transition.

Please provide the steps to reproduce and if possible a minimal demo of the problem via fiddle.tiny.cloud or similar.

Link to repo (hard to reproduce in a cloud environment)

Steps to reproduce:

  1. Add a self-hosted TinyMCE editor on a page inside your angular app.
@NgModule({
    declarations: [
        AppComponent,
        Page1Component,
        Page2Component,
    ],
    imports: [
        EditorModule,
        BrowserModule,
        AppRoutingModule,
    ],
    providers: [
        {provide: TINYMCE_SCRIPT_SRC, useValue: 'tinymce/tinymce.min.js'}
    ],
    bootstrap: [AppComponent]
})
export class AppModule {
    constructor() {
        tinymce.defaultSettings = {
            menubar: false,
            height: 300,
            suffix: '.min',
            base_url: '/tinymce'
        }
    }}

  1. Register a custom button for the editor:
import { AfterViewInit, Component, ViewChild } from '@angular/core'
import { EditorComponent } from '@tinymce/tinymce-angular'

export const TinyMCEConfig = {
    content_css: '/assets/tinymce.css',
    height: '100%',
    toolbar: `
        undo redo preview fullscreen |
        test |
    `
}

@Component({
    selector: 'app-page1',
    templateUrl: './page1.component.html',
    styleUrls: ['./page1.component.scss']
})
export class Page1Component implements AfterViewInit {
    editorConfig = TinyMCEConfig

    @ViewChild('editor') editorComponent!: EditorComponent

    ngAfterViewInit(): void {
        this.editorComponent.editor.ui.registry.addButton('test', {
            text: 'My Test button',
            onAction: () => {}
        })
    }
}

  1. Set the config for your TinyMCE editor to include the custom button
<editor [init]="editorConfig" #editor style="height: 400px;"></editor>
  1. Use the Angular router to create a second page (the editor will be on the first page). The second page can be an empty component. The point is to change from Page 1 to Page 2. When doing that, the custom button will disappear.

What is the expected behavior? The expected behavior is to show the custom buttons even if you change pages in the Angular app.

Which versions of TinyMCE, and which browser / OS are affected by this issue? Did this work in previous versions of TinyMCE? This happens in every browser on version "tinymce": "^5.7.1" and "@tinymce/tinymce-angular": "^4.2.2". I didn’t check older versions.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jscascacommented, May 15, 2021

Hey @IonelLupu my best guess is that there is a race condition going on in there somewhere. Can you try adding the button during the setup phase of the editor?

editorConfig: any = {...TinyMCEConfig, setup: (e) => e.ui.registry.addButton('test', {text: 'button', onAction: () => {}})};

Just nicer. I was just running a quick test in your repo

1reaction
jscascacommented, Jun 2, 2021

@IonelLupu To the best of my knowledge the buttons have to be added once the editor is available and before it has finished initialising. The race condition happens because the editor is asynchronously initialising and the ngAfterViewInit of your code fires before the editor has finished but if you wait longer the setup phase finishes before adding the button and nothing happens since it doesn’t lallow buttons to be added dynamically in the toolbar. I’m not sure if this is actually documented somewhere but you can always ask in the original TinyMCE repo

I will close the ticket with the suggestion to add buttons during setup for best results 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

tinymce.editor.ui.Registry | Docs
TinyMCE 5 Ui registration API. ... addButton(), Registers a new toolbar button that executes a command when clicked or activated via keyboard navigation ......
Read more >
Various script errors when loading tinymce inside a dynamic ...
I tried the dev version and that just gives me other script errors. It doesn't matter how I make the editor but this...
Read more >
set angular-ui-tinymce editor content after navigation
On the first page load, content is loaded to the editor via ng-model. Then I navigate to another state and then navigating back...
Read more >
48 answers on StackOverflow to the most popular Angular ...
I've got the following error when launching my Angular app, even if the component is not displayed. I have to comment out the...
Read more >
Angular 8 + Spring Boot 2.2: Build a CRUD App Today!
Then along came AngularJS and everyone started moving their UI architectures to JavaScript. Angular 2 was announced at the same time that Spring ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found