Skip to content

Commit 7ca1e6b

Browse files
committed
fix: make tagClasses work better
fix #1504
1 parent e202180 commit 7ca1e6b

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

index.html

+17-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@
8787
thead {
8888
background: #FFFFFF;
8989
}
90+
91+
/* Custom tag classes test */
92+
.container img.class-image {
93+
max-width: 50%;
94+
}
95+
.container [class^="class-"] {
96+
border-bottom: 3px solid #89ff4a;
97+
}
9098
</style>
9199
<link rel="stylesheet" href="/dist/ui/trumbowyg.css">
92100
<link rel="stylesheet" href="/dist/plugins/colors/ui/trumbowyg.colors.css">
@@ -163,7 +171,7 @@ <h2>All buttons of Trumbowyg are here.</h2>
163171
</div>
164172

165173
<div id="customized-buttonpane">
166-
<h2>This is a demo of Trumbowyg with a customized button pane</h2>
174+
<h2>This is a demo of Trumbowyg with a customized button pane & tagClasses</h2>
167175
<p>
168176
The text is derived from Cicero's De Finibus Bonorum et Malorum (On the Ends of Goods and Evils, or
169177
alternatively [About] The Purposes of Good and Evil ). The original passage began: Neque porro quisquam est
@@ -413,6 +421,14 @@ <h2 class="title">The dark side of Trumbowyg</h2>
413421
defaultLinkTarget: '_blank',
414422
plugins: {
415423
upload: uploadOptions
424+
},
425+
tagClasses: {
426+
img: 'class-image',
427+
p: 'class-paragraph',
428+
b: 'class-bold',
429+
i: 'class-italic',
430+
s: 'class-strikethrough',
431+
a: 'class-link',
416432
}
417433
};
418434
$('#customized-buttonpane')

src/trumbowyg.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
583583
}
584584

585585
t.semanticCode();
586+
t.applyTagClasses();
586587

587588
if (t.o.autogrowOnEnter) {
588589
t.$ed.addClass(prefix + 'autogrow-on-enter');
@@ -1475,6 +1476,13 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
14751476
t.$c.trigger('tbw' + (isFullscreen ? 'open' : 'close') + 'fullscreen');
14761477
},
14771478

1479+
applyTagClasses: function () {
1480+
var t = this;
1481+
for (const tag of Object.keys(t.o.tagClasses)) {
1482+
$(tag, t.$ed).addClass(t.o.tagClasses[tag]);
1483+
}
1484+
t.syncCode();
1485+
},
14781486

14791487
/*
14801488
* Call method of trumbowyg if exist
@@ -1514,26 +1522,18 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
15141522

15151523
t.syncCode();
15161524
t.semanticCode(false, true);
1517-
try {
1518-
var listId = window.getSelection().focusNode;
1519-
if (!$(window.getSelection().focusNode.parentNode).hasClass('trumbowyg-editor')) {
1520-
listId = window.getSelection().focusNode.parentNode;
1521-
}
1522-
var classes = t.o.tagClasses[param];
1523-
if (classes) {
1524-
$(listId).addClass(classes);
1525-
}
1526-
} catch (e) {
1527-
1528-
}
1529-
15301525
}
15311526

15321527
if (cmd !== 'dropdown') {
15331528
t.updateButtonPaneStatus();
15341529
t.$c.trigger('tbwchange');
15351530
}
15361531
}
1532+
1533+
try {
1534+
t.applyTagClasses();
1535+
} catch (e) {
1536+
}
15371537
},
15381538

15391539

0 commit comments

Comments
 (0)