Refactor new tab opening mechanism and update link options in TinyMCE v6 [SCI-8737] (#5866)

Refactor new tab opening mechanism and update link options in TinyMCE v6 [SCI-8737]
This commit is contained in:
ivanscinote 2023-08-22 14:59:34 +02:00 committed by GitHub
parent 9de4d455a5
commit c8bc4bef01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -247,12 +247,12 @@ window.TinyMCE = (() => {
object_resizing: true,
elementpath: false,
quickbars_insert_toolbar: false,
default_link_target: '_blank',
link_default_target: 'external',
mobile: {
menubar: 'file edit view insert format table'
},
target_list: [
{ title: 'New page', value: '_blank' },
link_target_list: [
{ title: 'New page', value: 'external' },
{ title: 'Same page', value: '_self' }
],
style_formats: [
@ -432,6 +432,19 @@ window.TinyMCE = (() => {
editor.on('init', () => {
restoreDraftNotification(selector, editor);
});
editor.on('BeforeSetContent GetContent', function(e) {
if (e.content && e.content.includes('target="external"')) {
const div = document.createElement('div');
div.innerHTML = e.content;
const links = div.querySelectorAll('a[target="external"]');
links.forEach(link => {
link.removeAttribute('target');
link.setAttribute('rel', 'external');
});
e.content = div.innerHTML;
}
});
},
save_onsavecallback: (editor) => { saveAction(editor); }
});
@ -487,3 +500,9 @@ $(document).on('turbolinks:before-visit', (e) => {
}
return true;
});
// Open rel="external" links in new tabs
$('a[rel*=external]').on('click', function(e) {
e.preventDefault();
window.open(this.href, '_blank', 'noopener');
});