Skip to content

Commit

Permalink
feat: add the ability to add classes to tags (#1082)
Browse files Browse the repository at this point in the history
  • Loading branch information
holema authored Nov 25, 2020
1 parent 276efec commit b1adae0
Showing 1 changed file with 51 additions and 28 deletions.
79 changes: 51 additions & 28 deletions src/trumbowyg.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
imageWidthModalEdit: false,

prefix: 'trumbowyg-',

// classes for inputs
tagClasses:{
'h1': null,
'h2': null,
'h3': null,
'h4': null,
'p': null,
},
semantic: true,
semanticKeepAttributes: false,
resetCss: false,
Expand Down Expand Up @@ -496,7 +503,7 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
return;
}

if (typeof(protocol) !== 'string') {
if (typeof (protocol) !== 'string') {
return 'https://';
}
return protocol.replace('://', '') + '://';
Expand Down Expand Up @@ -596,7 +603,8 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
try {
t.execCmd(key.fn, key.param);
return false;
} catch (c) {}
} catch (c) {
}
} else {
if (t.o.tabToIndent && e.key === 'Tab') {
try {
Expand All @@ -606,7 +614,8 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
t.execCmd('indent', true, null);
}
return false;
} catch (c) {}
} catch (c) {
}
}
}
})
Expand Down Expand Up @@ -669,8 +678,7 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
if (e.type === 'focus') {
t.autogrowOnEnterWasFocused = true;
t.autogrowEditorOnEnter();
}
else if (!t.o.autogrow) {
} else if (!t.o.autogrow) {
t.$ed.css({height: t.$ed.css('min-height')});
t.$c.trigger('tbwresize');
}
Expand Down Expand Up @@ -882,8 +890,8 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
type: 'button',
class: prefix + btnName + '-dropdown-button ' + (btn.class || '') + (btn.ico ? ' ' + prefix + btn.ico + '-button' : ''),
html: t.hasSvg && hasIcon ?
'<svg><use xlink:href="' + t.svgPath + '#' + prefix + (btn.ico || btnName).replace(/([A-Z]+)/g, '-$1').toLowerCase() + '"/></svg>' + (btn.text || btn.title || t.lang[btnName] || btnName) :
(btn.text || btn.title || t.lang[btnName] || btnName),
'<svg><use xlink:href="' + t.svgPath + '#' + prefix + (btn.ico || btnName).replace(/([A-Z]+)/g, '-$1').toLowerCase() + '"/></svg>' + (btn.text || btn.title || t.lang[btnName] || btnName) :
(btn.text || btn.title || t.lang[btnName] || btnName),
title: (btn.key ? '(' + (t.isMac ? 'Cmd' : 'Ctrl') + ' + ' + btn.key + ')' : null),
style: btn.style || null,
mousedown: function () {
Expand Down Expand Up @@ -1241,7 +1249,7 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
$(oldTag, this.$ed).each(function () {
var resetRange = false;
var $oldTag = $(this);
if($oldTag.contents().length === 0) {
if ($oldTag.contents().length === 0) {
return false;
}

Expand Down Expand Up @@ -1480,6 +1488,21 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {

t.syncCode();
t.semanticCode(false, true);
try {
var listId = window.getSelection().focusNode;
if(!$(window.getSelection().focusNode.parentNode).hasClass('trumbowyg-editor')){
listId = window.getSelection().focusNode.parentNode;
}
var arr = t.o.tagClasses[param];
if (arr) {
for (var i = 0; i < arr.length; i++) {
$(listId).addClass(arr[i]);
}
}
} catch (e) {

}

}

if (cmd !== 'dropdown') {
Expand Down Expand Up @@ -1532,19 +1555,19 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
action: '',
html: content
})
.on('submit', function () {
$modal.trigger(CONFIRM_EVENT);
return false;
})
.on('reset', function () {
$modal.trigger(CANCEL_EVENT);
return false;
})
.on('submit reset', function () {
if (t.o.autogrowOnEnter) {
t.autogrowOnEnterDontClose = false;
}
});
.on('submit', function () {
$modal.trigger(CONFIRM_EVENT);
return false;
})
.on('reset', function () {
$modal.trigger(CANCEL_EVENT);
return false;
})
.on('submit reset', function () {
if (t.o.autogrowOnEnter) {
t.autogrowOnEnterDontClose = false;
}
});
} else {
formOrContent = content;
}
Expand Down Expand Up @@ -1856,9 +1879,9 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {

clearButtonPaneStatus: function () {
var t = this,
prefix = t.o.prefix,
activeClasses = prefix + 'active-button ' + prefix + 'active',
originalIconClass = prefix + 'original-icon';
prefix = t.o.prefix,
activeClasses = prefix + 'active-button ' + prefix + 'active',
originalIconClass = prefix + 'original-icon';

// Reset all buttons and dropdown state
$('.' + prefix + 'active-button', t.$btnPane).removeClass(activeClasses);
Expand Down Expand Up @@ -1896,12 +1919,12 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
if (t.o.changeActiveDropdownIcon && $btnSvgUse.length > 0) {
// Save original icon
$dropdownBtn
.addClass(originalIconClass)
.data(originalIconClass, $dropdownBtnSvgUse.attr('xlink:href'));
.addClass(originalIconClass)
.data(originalIconClass, $dropdownBtnSvgUse.attr('xlink:href'));

// Put the active sub-button's icon
$dropdownBtnSvgUse
.attr('xlink:href', $btnSvgUse.attr('xlink:href'));
.attr('xlink:href', $btnSvgUse.attr('xlink:href'));
}
} catch (e) {
}
Expand Down

0 comments on commit b1adae0

Please sign in to comment.