What is the process for updating the text of a toolbar menu button item in the latest version of TinyMCE, version 5?

After transitioning from tinymce 4 to tinymce 5, I am struggling to create a dropdown menu button and change its text dynamically based on the current selection.

Essentially, I want something similar to the default Font or title button.

This is how I currently create a dropdown button that wraps selected text with <span lang='xx' tag.

addLanguageBtn()
{
    let lngBtn = editor.ui.registry.addMenuButton('languageBtn', {
      text: 'Language',

      fetch: (callback) => {
        let items: any = [
          {text: 'French', value: 'fr', type: 'menuitem'},
          {text: 'German', value: 'de', type: 'menuitem'},
          {text: 'Spanish', value: 'es', type: 'menuitem'},
        ];
        
        items.forEach(item => {

          item.onAction = () => {

            let currentNode = editor.selection.getNode();

            if (currentNode.tagName.toLocaleLowerCase() !== 'body')
            {
              currentNode.setAttribute('lang', item.value);
            }
            else
            {
              let textToWrap = editor.selection.getContent();
              let wrappedContent = `<span lang='${item.value}'>${textToWrap}</span>`;
              editor.selection.setContent(wrappedContent);
            }
          }

        });

        callback(items);

      },

In the main setup method of tinymce, my goal is to update the button text to display the selected language whenever the cursor is positioned on a word within a <span lang='xx' tag.

  setup(ed)
  {
      
    let lngButton = this.addLanguageBtn(ed);


    ed.on('NodeChange', function (ev) {
      if (!lngButton)//No button
      {
        return;
      }
      let lang = ev.element.getAttribute('lang');
      lngButton.text = lang; // <==== This does not work, value is modified but UI is not updated

Note

For a codepen example, check out this link

Edit: Upon inspecting tinymce's source code, it appears they use Alloy components for their font family and font weight buttons. I'm aiming for a more straightforward solution.

Answer №1

It seems achieving this directly may not be possible, as there is already a similar issue with the same requirements in the project's repository. You can refer to the issue for more information.

One of the project contributors responded with:

Currently, there is no way to accomplish this, as the buttons that allow it utilize an internal API. We do have plans to make this functionality accessible to others, but it has not been scheduled yet. Therefore, I will mark this as a feature request.

If you are a commercial customer, we recommend submitting this feature request through our support channels for prioritized attention, rather than through GitHub requests.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The jquery function is not functioning properly within a javascript code block, however it performs perfectly when placed outside

In my php page, I have included jQuery and a script.js file. When I insert this function into my php file, it runs successfully: <script> $('#scoreboard-overview').load('getusers.php?q=<?php echo $NewString; ?>').fadeIn ...

Restrict Textfield input to only accept Integers and Floats

My textfield for Price only allows Integer values, not Float. I am currently struggling with allowing Float values like 3110.6 to be accepted. If you want to see my code in action, check out the DEMO. JS: $(document).ready(function () { $("#price") ...

Displaying the votes through an advanced system called Ajax voting system

I've encountered a challenge while using an ajax voting system in my project. The issue is that I'm utilizing a div with an id to showcase the votes, but whenever someone clicks on vote up or down in any of the posts (which are generated through ...

The issue persists with the Javascript AJAX POST Method as it fails to transmit values to the designated

I have been using Javascript AJAX to make a request to a php page and receive the output from that page. Interestingly, when I use the GET method in my AJAX call, everything works as expected. However, the issue arises when I try to use the POST method. B ...

Fixed position is used on elements that should always remain in the exact

I have a fixed menu on my website that is working perfectly fine. I also have a container (not using Bootstrap) that should stay fixed when it reaches the static menu. Everything is functioning as expected, except when I resize the window, the container se ...

Can variables be bound bidirectionally between nested isolated scopes?

Looking into the complexities of nested directives, I am facing a challenge in establishing two-way binding between isolate scopes. The main goal is to have the outerPower variable bind to the innerPower variable and update whenever the innerPower value i ...

Button-operated lightbox image slider

I am currently developing a website on WAMP where users can store images and view them in a lightbox effect. I have successfully implemented the lightbox effect, but I am facing challenges with moving between images within the lightbox. <html> <? ...

Invoke a JavaScript resource from VB code behind

I am attempting to pass a string and invoke a JavaScript function from the code-behind in VB.NET with the click of a button. The JavaScript function is located in a separate file. Here is the code for the button: /MyProject/myfile.aspx <HTML> ... ...

Using Cmd+Click in iTerm2 does not function properly when trying to navigate to TypeScript error messages

The appearance of the file name in the output is as follows: path/filename/ext(line,col) as opposed to the typical path/filename/ext:line This deviation causes iTerm2 to display an error message instead of opening the file: An application is not ass ...

Quickly select a stand-out team from a pool of candidates

In my current scenario, I am dealing with a database of 150 players including their names, IDs (UUIDs), and team names. Users have the ability to create teams consisting of 10 players, and there is no limit on how many teams they can create. Essentially ...

A guide on Implementing PastBack Functionality with AJAX Responses

When I make a GET request for an HTML page, I come across the following element: <a id="ctl00_cphRoblox_ClaimOwnershipButton" href="javascript:__doPostBack('ctl00$cphRoblox$ClaimOwnershipButton','')">Claim Ownership</a> My ...

Prevent Vertical Scrolling with Jquery on right-click functionality

When a user right-clicks in my application, I present them with some options. However, when the user clicks on an option such as "copy," the page always scrolls to the top. I am struggling to prevent this automatic scrolling. I attempted to use the anima ...

Tips on triggering a function whenever ngMessages detects an invalid field

I am in the process of gathering an array of error objects specific to front-end errors triggered by ngMessages detecting an invalid input field change. Previously, I have experience working with the event-driven formvalidation.io library. Is there a corr ...

Canvas not displaying image in JavaScript

I have a bird object with a draw function defined, but for some reason, the image is not showing up when called in the render function. Can someone please help me figure out what I'm doing wrong? Thanks in advance. EDIT: Upon further investigation, I ...

Error: The function bcrypt.checkPassword() is undefined and cannot be called

Upon logging in, I encountered an issue where the error message "TypeError: bcrypt.checkPassword is not a function" was displayed. Reviewing my code, I am puzzled as to why bcrypt.checkPassword is not recognized as a function. [bcrypt.js] var checkPassw ...

Node.js - Synchronize asynchronous calls to ensure coordinated execution in code

I am trying to figure out how to make a for loop with an async function wait until all the async functions called within it are finished before allowing the code to continue. In my scenario, I have a variable "bar" that contains a JSON array with other ne ...

Using TypeScript to structure and organize data in order to reduce the amount of overly complex code blocks

Within my TypeScript module, I have multiple array structures each intended to store distinct data sets. var monthlySheetP = [ ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', &apo ...

Distribute or scale a specific value to an array of numbers

I'm currently working on a project that involves distributing the values of an array in a way that the total sum equals 100. Here are some examples: [70, 34, 92] with a total value of 100. The output is [35.71, 17.35, 46.94] because 35.71 + 17.35 + ...

Display the device model using Google Cloud Monitoring API

I've been utilizing a Node.js library to fetch metrics from my Google Cloud Compute Engine instances. You can find the library here. When creating a time series, the resulting data looks like this: { "points": [...], "metric": { "lab ...

Could someone provide a detailed explanation of exhaustMap in the context of Angular using rxjs?

import { HttpHandler, HttpInterceptor, HttpParams, HttpRequest, } from '@angular/common/http'; import { Injectable } from '@core/services/auth.service'; import { exhaustMap, take } from 'rxjs/operators'; import { Authe ...