Browse Source

Make the RTE understand active commands

pull/415/head
Artur Arseniev 9 years ago
parent
commit
76af3011f1
  1. 2
      dist/css/grapes.min.css
  2. 1
      src/rich_text_editor/index.js
  3. 28
      src/rich_text_editor/model/RichTextEditor.js
  4. 8
      src/styles/scss/_gjs_rte.scss
  5. 33
      src/styles/scss/main.scss

2
dist/css/grapes.min.css

File diff suppressed because one or more lines are too long

1
src/rich_text_editor/index.js

@ -161,6 +161,7 @@ module.exports = () => {
const classes = { const classes = {
actionbar: `${pfx}actionbar`, actionbar: `${pfx}actionbar`,
button: `${pfx}action`, button: `${pfx}action`,
active: `${pfx}active`,
}; };
toolbar.style.display = ''; toolbar.style.display = '';

28
src/rich_text_editor/model/RichTextEditor.js

@ -1,25 +1,31 @@
// The initial version of this RTE was borrowed from https://github.com/jaredreich/pell // The initial version of this RTE was borrowed from https://github.com/jaredreich/pell
// and adapted to the GrapesJS's need // and adapted to the GrapesJS's need
import {on, off} from 'utils/mixins'
const RTE_KEY = '_rte'; const RTE_KEY = '_rte';
const actions = { const actions = {
bold: { bold: {
name: 'bold',
icon: '<b>B</b>', icon: '<b>B</b>',
attributes: {title: 'Bold'}, attributes: {title: 'Bold'},
result: (rte) => rte.exec('bold') result: (rte) => rte.exec('bold')
}, },
italic: { italic: {
name: 'italic',
icon: '<i>I</i>', icon: '<i>I</i>',
attributes: {title: 'Italic'}, attributes: {title: 'Italic'},
result: (rte) => rte.exec('italic') result: (rte) => rte.exec('italic')
}, },
underline: { underline: {
name: 'underline',
icon: '<u>U</u>', icon: '<u>U</u>',
attributes: {title: 'Underline'}, attributes: {title: 'Underline'},
result: (rte) => rte.exec('underline') result: (rte) => rte.exec('underline')
}, },
strikethrough: { strikethrough: {
name: 'strikethrough',
icon: '<strike>S</strike>', icon: '<strike>S</strike>',
attributes: {title: 'Strike-through'}, attributes: {title: 'Strike-through'},
result: (rte) => rte.exec('strikeThrough') result: (rte) => rte.exec('strikeThrough')
@ -48,6 +54,7 @@ export default class RichTextEditor {
el[RTE_KEY] = this; el[RTE_KEY] = this;
this.el = el; this.el = el;
this.doc = el.ownerDocument; this.doc = el.ownerDocument;
this.updateActiveActions = this.updateActiveActions.bind(this);
settings.actions = settings.actions settings.actions = settings.actions
? settings.actions.map(action => { ? settings.actions.map(action => {
@ -62,6 +69,7 @@ export default class RichTextEditor {
settings.classes = { ...{ settings.classes = { ...{
actionbar: 'actionbar', actionbar: 'actionbar',
button: 'action', button: 'action',
active: 'active',
}, ...settings.classes}; }, ...settings.classes};
const classes = settings.classes; const classes = settings.classes;
@ -85,16 +93,31 @@ export default class RichTextEditor {
return this; return this;
} }
updateActiveActions() {
this.actions().forEach(action => {
const btn = action.btn;
const active = this.classes.active;
btn.className = btn.className.replace(active, '').trim();
if (this.doc.queryCommandState(action.name)) {
btn.className += ` ${active}`;
}
})
}
enable() { enable() {
this.actionbarEl().style.display = ''; this.actionbarEl().style.display = '';
this.el.contentEditable = true; this.el.contentEditable = true;
on(this.el, 'mouseup keyup', this.updateActiveActions)
this.syncActions(); this.syncActions();
this.el.focus();
return this; return this;
} }
disable() { disable() {
this.actionbarEl().style.display = 'none'; this.actionbarEl().style.display = 'none';
this.el.contentEditable = false; this.el.contentEditable = false;
off(this.el, 'mouseup keyup', this.updateActiveActions)
return this; return this;
} }
@ -104,7 +127,10 @@ export default class RichTextEditor {
syncActions() { syncActions() {
this.actions().forEach(action => { this.actions().forEach(action => {
const event = action.event || 'click'; const event = action.event || 'click';
action.btn[`on${event}`] = e => action.result(this); action.btn[`on${event}`] = e => {
action.result(this);
this.updateActiveActions();
};
}) })
} }

8
src/styles/scss/_gjs_rte.scss

@ -30,12 +30,12 @@
border-right: none; border-right: none;
} }
&.btn-info {
background-color: $mainDkColor;
}
&:hover { &:hover {
background-color: $mainLhColor; background-color: $mainLhColor;
} }
} }
&active {
background-color: $mainDkColor;
}
} }

33
src/styles/scss/main.scss

@ -236,41 +236,10 @@
@import "gjs_canvas"; @import "gjs_canvas";
/************* RTE ****************/ /********* COMMANDS **********/
#commands.panel {
min-width: 35px;
height: 100%;
z-index:3;
}
#options.panel{ z-index:4; bottom: 0;}
#views.panel {
width: 16.5%;
font-weight: lighter;
color: $fontColor;
right:0; top:0;
z-index: 3;
height: 100%;
padding:0;
}
#views.panel .c{height:100%}
#commands.panel, #options.panel {width: 3.5%; left:0;}
#options .c { display: table; margin: 0 auto; }
/*********TEST**********/
body.dragging, body.dragging * { cursor: move !important;}
.dragged {
position: absolute;
@include opacity(0.50);
z-index: 2000;}
ol.example li.placeholder {position: relative;}
ol.example li.placeholder:before {position: absolute;}
/*********END-TEST**********/
.no-dots, .ui-resizable-handle{ border: none !important; margin:0 !important; outline: none !important; } .no-dots, .ui-resizable-handle{ border: none !important; margin:0 !important; outline: none !important; }
/********* COMMANDS **********/
.#{$com-prefix}dashed *{ .#{$com-prefix}dashed *{
outline: 1px dashed #888; outline: 1px dashed #888;
outline-offset: -2px; outline-offset: -2px;

Loading…
Cancel
Save