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

fix: advanced-text-editor custom color issue #10

Merged
merged 1 commit into from
May 25, 2017
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
83 changes: 52 additions & 31 deletions plugins/colorbutton/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,29 @@ CKEDITOR.plugins.add( 'colorbutton', {
onOpen: function() {
var doc = this._.panel._.iframe.getFrameDocument()

// highlight current color
var path = editor.elementPath();
var firstBlock = path.block || path.blockLimit;
var activeItem = doc.find('.cke_coloricon_active').getItem(0)
if (activeItem) activeItem.removeClass('cke_coloricon_active')
var defaultTr = doc.getById('cke_coloricon_default')
defaultTr.hide()
var colorNames = config.colorButton_colors.map(function(color) {return color[0]})
colorNames.push('custom1', 'custom2')
colorNames.some(function(colorName) {
var colorClass = config.colorButton_colorClassNamePattern.replace('%s', colorName)
if (firstBlock.hasClass(colorClass)) {
defaultTr.show()
doc.find('.cke_coloricon_' + colorName).getItem(0).addClass('cke_coloricon_active')
return true
}
})
if (editor.config.advancedEditor) {
// on advanced-text-editor
var activeItem = doc.find('.cke_coloricon_active').getItem(0)
if (activeItem) activeItem.removeClass('cke_coloricon_active')
} else {
// highlight current color
var path = editor.elementPath();
var firstBlock = path.block || path.blockLimit;
var activeItem = doc.find('.cke_coloricon_active').getItem(0)
if (activeItem) activeItem.removeClass('cke_coloricon_active')
var defaultTr = doc.getById('cke_coloricon_default')
defaultTr.hide()
var colorNames = config.colorButton_colors.map(function(color) {return color[0]})
colorNames.push('custom1', 'custom2')
colorNames.some(function(colorName) {
var colorClass = config.colorButton_colorClassNamePattern.replace('%s', colorName)
if (firstBlock.hasClass(colorClass)) {
defaultTr.show()
doc.find('.cke_coloricon_' + colorName).getItem(0).addClass('cke_coloricon_active')
return true
}
})
}

// enable custom color
var customColors = config.colorButton_getCustomColors()
Expand Down Expand Up @@ -193,21 +199,36 @@ CKEDITOR.plugins.add( 'colorbutton', {
editor.fire( 'saveSnapshot' );

if (editor.config.advancedEditor) {
var range = editor.createRange();
var selection = editor.getSelection()

var colorStyle = config[ 'colorButton_' + type + 'Style' ];
// Clean up any conflicting style within the range.
classNames.map(function(className) {
editor.removeStyle( new CKEDITOR.style( colorStyle, {className: className}))
});

if ( color ) {
// get classname
var colorClassName = config.colorButton_colorClassNamePattern.replace('%s', color);

colorStyle.childRule = function( element ) {
// Fore color style must be applied inside links instead of around it. (#4772,#6908)
return !( element.is( 'a' ) || element.getElementsByTag( 'a' ).count() ) || isUnstylable( element );
};
editor.applyStyle( new CKEDITOR.style( colorStyle, { className: colorClassName } ) );
if (color === 'default' && !selection.getSelectedText()) {
range.selectNodeContents( editor.editable() );
selection.selectRanges( [ range ] );

// Clean up any conflicting style within the range.
classNames.map(function(className) {
editor.removeStyle( new CKEDITOR.style( colorStyle, {className: className}))
});

selection.removeAllRanges()
} else {
// Clean up any conflicting style within the range.
classNames.map(function(className) {
editor.removeStyle( new CKEDITOR.style( colorStyle, {className: className}))
});

if ( color ) {
// get classname
var colorClassName = config.colorButton_colorClassNamePattern.replace('%s', color);

colorStyle.childRule = function( element ) {
// Fore color style must be applied inside links instead of around it. (#4772,#6908)
return !( element.is( 'a' ) || element.getElementsByTag( 'a' ).count() ) || isUnstylable( element );
};
editor.applyStyle( new CKEDITOR.style( colorStyle, { className: colorClassName } ) );
}
}
} else {
var range = editor.createRange();
Expand Down