Skip to content

Commit 74ab0e1

Browse files
committed
added: fallback icons, fixed bugs, option:debug
1 parent 7baa445 commit 74ab0e1

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

src/plugin.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@
135135
background: var(--supercode-modal-secondary);
136136
color: var(--supercode-modal-primary);
137137
}
138-
`
138+
`;
139+
140+
const CLOSE_ICON_FALLBACK = `<svg width="24" height="24"><path d="M17.3 8.2 13.4 12l3.9 3.8a1 1 0 0 1-1.5 1.5L12 13.4l-3.8 3.9a1 1 0 0 1-1.5-1.5l3.9-3.8-3.9-3.8a1 1 0 0 1 1.5-1.5l3.8 3.9 3.8-3.9a1 1 0 0 1 1.5 1.5Z" fill-rule="evenodd"></path></svg>`
141+
const CODE_ICON_FALLBACK = `<svg width="24" height="24" focusable="false"><g fill-rule="nonzero"><path d="M9.8 15.7c.3.3.3.8 0 1-.3.4-.9.4-1.2 0l-4.4-4.1a.8.8 0 0 1 0-1.2l4.4-4.2c.3-.3.9-.3 1.2 0 .3.3.3.8 0 1.1L6 12l3.8 3.7ZM14.2 15.7c-.3.3-.3.8 0 1 .4.4.9.4 1.2 0l4.4-4.1c.3-.3.3-.9 0-1.2l-4.4-4.2a.8.8 0 0 0-1.2 0c-.3.3-.3.8 0 1.1L18 12l-3.8 3.7Z"></path></g></svg>`
139142

140143
let modal = null;
141144

@@ -197,7 +200,14 @@
197200
fallbackModal: false, // enabled in cases like inline, or versions where `CustomView` is not supported.
198201
modalPrimaryColor: '#ffffff',
199202
modalSecondaryColor: '#222f3e',
200-
dark: false
203+
dark: false,
204+
debug: true
205+
}
206+
207+
const debug = (msg) => {
208+
if(Config.debug){
209+
console.warn(`${msg} \n\nUse debug:false option to disable this warning`);
210+
}
201211
}
202212

203213
// Get Configurations
@@ -231,6 +241,7 @@
231241
case 'shortcut':
232242
case 'fallbackModal':
233243
case 'dark':
244+
case 'debug':
234245
if (typeof value === "boolean") {
235246
Config[key] = value;
236247
}
@@ -250,9 +261,10 @@
250261
}
251262

252263
// Set plugin icon
253-
Config.icon = editor.ui.registry.getAll().icons[Config.iconName];
264+
Config.icon = editor.ui.registry.getAll?.().icons?.[Config.iconName];
254265
if (!Config.icon) {
255-
throw new Error("Supercode Icon name is invalid");
266+
Config.icon = CODE_ICON_FALLBACK;
267+
debug("Supercode Icon name is invalid or you are using older versions of tinyMCE. The icon is set to default fallback code icon.");
256268
}
257269

258270
// Detect and set fallback model if its required
@@ -310,44 +322,34 @@
310322
// If menu-bar exists utilize the space to show Title "Source Code Editor"
311323
const menubar = newHeader.querySelector('.tox-menubar');
312324
if(menubar){
313-
menubar.innerHTML = `<b style='font-size: 14px; font-weight: bold; padding: 9px;'>Source Code Editor</b>`
325+
menubar.innerHTML = `<b style='font-size: 14px; font-weight: bold; padding: 11px 9px;'>Source Code Editor</b>`
314326
}
315327

316-
// hide all the buttons except supercode button, attach event listener
317-
let isOverflow = true;
328+
// disable all the buttons except supercode button, attach event listener
329+
let overflowButton = null, isPluginButton = false;
318330
newHeader.querySelectorAll('.tox-tbtn, .tox-split-button').forEach((btn) => {
319331
if(btn.getAttribute('data-mce-name') != 'supercode'){
320332
// remove overflow button to make space for code button
321333
if(btn.getAttribute('data-mce-name') === 'overflow-button'){
322-
btn.style.display = 'none';
323-
btn.removeAttribute('data-mce-name')
324-
return;
325-
}
334+
overflowButton = btn;
335+
}
326336
btn.classList.remove('tox-tbtn--enabled');
327337
btn.classList.add('tox-tbtn--disabled');
328338
btn.removeAttribute('data-mce-name');
329339
}
330340
else{
331-
isOverflow = false;
341+
isPluginButton = true;
332342
btn.setAttribute('data-mce-name', 'supercode-toggle')
333343
btn.classList.add('tox-tbtn--enabled');
334344
btn.onclick = onSaveHandler;
335345
}
336346
});
337347

338-
// in case of overflow, button is inside a floating toolbar
339-
if(isOverflow){
340-
const div = document.createElement('div')
341-
div.classList = 'tox-toolbar__group';
342-
div.style.position = 'absolute';
343-
div.style.right = 0;
344-
div.style.height = '100%';
345-
const button = document.createElement('button');
346-
button.classList = 'tox-tbtn tox-tbtn--enabled';
347-
button.innerHTML = `<span class="tox-icon tox-tbtn__icon-wrap">${Config.icon}</span>`;
348-
button.onclick = onSaveHandler;
349-
div.append(button);
350-
newHeader.append(div);
348+
// in case of overflow, replace the overflow button with code button
349+
if(!isPluginButton && overflowButton){
350+
overflowButton.classList = 'tox-tbtn tox-tbtn--enabled';
351+
overflowButton.innerHTML = `<span class="tox-icon tox-tbtn__icon-wrap">${Config.icon}</span>`;
352+
overflowButton.onclick = onSaveHandler;
351353
}
352354
view.innerHTML = ''; // delete any existing header
353355
view.append(newHeader);
@@ -410,7 +412,8 @@
410412
if(Config.dark){
411413
document.body.style.setProperty('--supercode-modal-border', 'rgba(255, 255, 255, 0.1)');
412414
}
413-
document.querySelector('#supercode-close-btn').innerHTML = editor.ui.registry.getAll().icons['close']
415+
416+
modal.element.querySelector('#supercode-close-btn').innerHTML = editor.ui.registry.getAll?.().icons?.['close'] ?? CLOSE_ICON_FALLBACK;
414417

415418
modal.element.style.display = 'flex';
416419
setTimeout(() => {
@@ -429,7 +432,7 @@
429432
setTimeout(() => {
430433
modal.element.style.display = 'none';
431434
}, 10)
432-
}
435+
};
433436

434437
const onSaveHandler = () => {
435438
editor.focus();
@@ -500,6 +503,7 @@
500503
if(isScreenSizeChanged){
501504
setHeader(codeView.querySelector('.supercode-header'), originalHeader);
502505
codeView.querySelector('.supercode-body ').style.width = editorWidth+'px';
506+
aceEditor.resize();
503507
}
504508

505509
// Only on First time plugin opened => mount view

0 commit comments

Comments
 (0)