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

feat: add the ability to add classes to tags #1082

Merged
merged 5 commits into from
Nov 25, 2020
Merged
Changes from 4 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
85 changes: 57 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 @@ -591,7 +598,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 @@ -601,7 +609,8 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
t.execCmd('indent', true, null);
}
return false;
} catch (c) {}
} catch (c) {
}
}
}
})
Expand Down Expand Up @@ -659,8 +668,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 @@ -872,8 +880,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 @@ -1207,7 +1215,7 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {

$(oldTag, this.$ed).each(function () {
var $oldTag = $(this);
if($oldTag.contents().length === 0) {
if ($oldTag.contents().length === 0) {
return false;
}

Expand Down Expand Up @@ -1433,6 +1441,27 @@ 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) {
$(listId).removeClass();
Alex-D marked this conversation as resolved.
Show resolved Hide resolved
for (var i = 0; i < arr.length; i++) {
$(listId).removeClass(arr[i]);
}
for (var i = 0; i < arr.length; i++) {
$(listId).addClass(arr[i]);
}


}
} catch (e) {

}

}

if (cmd !== 'dropdown') {
Expand Down Expand Up @@ -1485,19 +1514,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 @@ -1809,9 +1838,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 @@ -1849,12 +1878,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