Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update index.js #1

Merged
merged 1 commit into from
Jun 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 24 additions & 42 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import grapesjs from 'grapesjs';
import grapesjs from 'grapesjs'

const stopPropagation = e => e.stopPropagation();

export default grapesjs.plugins.add('gjs-plugin-ckeditor', (editor, opts = {}) => {
grapesjs.plugins.add('gjs-plugin-ckeditor', (editor, opts = {}) => {
let c = opts;

let defaults = {
// CKEditor options
options: {},
options: {
// fix bug : https://github.com/artf/grapesjs-plugin-ckeditor/issues/49
enterMode: CKEDITOR.ENTER_BR,
shiftEnterMode: CKEDITOR.ENTER_P
},

// On which side of the element to position the toolbar
// Available options: 'left|center|right'
position: 'left',
position: 'center',
};

// Load defaults
Expand All @@ -26,19 +30,19 @@ export default grapesjs.plugins.add('gjs-plugin-ckeditor', (editor, opts = {}) =

editor.setCustomRte({
enable(el, rte) {
// If already exists I'll just focus on it
if(rte && rte.status != 'destroyed') {
this.focus(el, rte);
return rte;
}

// if(rte && rte.status != 'destroyed') {
// this.focus(el, rte);
// return rte;
// }

el.contentEditable = true;

// Seems like 'sharedspace' plugin doesn't work exactly as expected
// so will help hiding other toolbars already created
let rteToolbar = editor.RichTextEditor.getToolbarEl();
[].forEach.call(rteToolbar.children, (child) => {
child.style.display = 'none';
child.style.display = 'none';
});

// Check for the mandatory options
Expand Down Expand Up @@ -96,49 +100,27 @@ export default grapesjs.plugins.add('gjs-plugin-ckeditor', (editor, opts = {}) =
});

this.focus(el, rte);


return rte;
},

disable(el, rte) {
el.contentEditable = false;
if(rte && rte.focusManager)
if(rte && rte.focusManager){
rte.focusManager.blur(true);
}
// fix bug : https://github.com/artf/grapesjs-mjml/issues/193
// fix bug : https://github.com/artf/grapesjs-plugin-ckeditor/issues/37
rte.destroy(true);
},

// fix bug : https://github.com/artf/grapesjs-plugin-ckeditor/issues/31
focus(el, rte) {
// Do nothing if already focused
if (rte && rte.focusManager.hasFocus) {
return;
}
el.contentEditable = true;
rte && rte.focus();
editor.RichTextEditor.updatePosition()
setTimeout(function() {
rte && rte.focus();
}, 200);
},
});

// Update RTE toolbar position
editor.on('rteToolbarPosUpdate', (pos) => {
// Update by position
switch (c.position) {
case 'center':
let diff = (pos.elementWidth / 2) - (pos.targetWidth / 2);
pos.left = pos.elementLeft + diff;
break;
case 'right':
let width = pos.targetWidth;
pos.left = pos.elementLeft + pos.elementWidth - width;
break;
}

if (pos.top <= pos.canvasTop) {
pos.top = pos.elementTop + pos.elementHeight;
}

// Check if not outside of the canvas
if (pos.left < pos.canvasLeft) {
pos.left = pos.canvasLeft;
}
});

});