whitecube / nova-flexible-content

Flexible Content & Repeater Fields for Laravel Nova
MIT License
788 stars 228 forks source link

Field Keys sometimes start with a number, breaking plugins that use querySelector to access the field #377

Closed ellukesmith closed 1 year ago

ellukesmith commented 2 years ago

The unique IDs that get generated for new fields in the Group.js file will sometimes start with a number.

While this is completely valid in the HTML5 spec (the HTML4 spec required IDs to start with a letter) it causes issues if you try to select the element by ID using a document.querySelector. I'm currently using a TinyMCE plugin, which will not render on any fields that have an ID that starts with a number due to the above issue.

Is it possible to modify the randomString method to always return an ID that starts with a letter?

I'd be happy with something like the following:

function randomString(len, charSet) {
    charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var safeCharSet = charSet.match("[a-zA-Z]+") ? charSet.replace(/[0-9]/g, '') : charSet;
    var randomString = '';
    for (var i = 0; i < len; i++) {
    var currentCharSet = i == 0 ? safeCharSet : charSet;
        var randomPoz = Math.floor(Math.random() * currentCharSet.length);
        randomString += currentCharSet.substring(randomPoz,randomPoz+1);
    }
    return randomString;
}

Thanks.

chrispage1 commented 2 years ago

Ah-ha, I've also run into this issue. I'll try and work up a fix now as my client has just run into this problem

chrispage1 commented 2 years ago

I've created a pull request, for a quick fix if you want to for the time being, change your require statement in composer to "emilianotisato/nova-tinymce": "dev-master",, and add the repositories attribute as below:

"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/motomedialab/nova-flexible-content.git"
        }
    ],
voidgraphics commented 1 year ago

Closed in #380