Skip to content

Commit

Permalink
LPS-139334 Manage table toolbar in its own plugin. CSS selection in `…
Browse files Browse the repository at this point in the history
…balloonToolbars.create` API is not a sufficient trigger.
  • Loading branch information
markocikos committed Jan 13, 2022
1 parent b3517c1 commit 5b1df32
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -912,9 +912,72 @@
}

CKEDITOR.plugins.add(pluginName, {
getSelectedCells,
icons: 'add-row, add-column, add-cell',
_createToolbar({editor}) {
const toolbar = new CKEDITOR.ui.balloonToolbar(editor);

const buttons = editor.config.toolbarTable.split(',');

buttons.forEach((button) => {
toolbar.addItem(button, editor.ui.create(button));
});

return toolbar;
},

_getToolbar({editor}) {
const instance = this;

let toolbar = editor.liferayToolbars.tableToolbar;

if (toolbar) {
return toolbar;
}

toolbar = instance._createToolbar({editor});

editor.liferayToolbars.tableToolbar = toolbar;

return toolbar;
},

init(editor) {
editor.liferayToolbars = editor.liferayToolbars ?? {};

const eventListeners = [];

eventListeners.push(
editor.on('destroy', () => {
eventListeners.forEach((listener) => {
listener.removeListener();
});
}),

editor.on('hideToolbars', () => {
editor.liferayToolbars.tableToolbar?.hide();
}),

editor.on('selectionChange', () => {
const selection = editor.getSelection();

const type = selection.getType();

const startElement = selection.getStartElement();

if (
type === CKEDITOR.SELECTION_TEXT &&
!selection.getSelectedText().match(/\w/) &&
startElement.hasAscendant('table')
) {
const toolbar = this._getToolbar({editor});

toolbar.attach(startElement);
}
else {
editor.liferayToolbars.tableToolbar?.hide();
}
})
);

function createDef(def) {
return CKEDITOR.tools.extend(def || {}, {
contextSensitive: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ const BalloonEditor = ({config = {}, contents, name, ...otherProps}) => {
});
}

if (editorConfig.toolbarTable) {
balloonToolbars.create({
buttons: editorConfig.toolbarTable,
cssSelector: 'td',
priority:
window.CKEDITOR.plugins.balloontoolbar.PRIORITY
.HIGH,
});
}

if (editorConfig.toolbarVideo) {
balloonToolbars.create({
buttons: editorConfig.toolbarVideo,
Expand Down

0 comments on commit 5b1df32

Please sign in to comment.