gosora/public/trumbowyg/plugins/preformatted/trumbowyg.preformatted.js
Azareal 14a14b7e80 More work on Cosora, we have a screenshot of it up now, although it's super experimental at the moment.
This commit might be a little broken, another is coming to fix things up!

The topics list is now paginated.
Refactored the error handling system.
Added the Trumboyg WYSIWYG editor for Cosora.
Moved Delete() out of the TopicStore and into *Topic
You can now bulk delete and bulk lock topics on Cosora.
h1s are now formatted properly on Firefox.
Added more ARIA Labels.
SuperModOnly is now a piece of middleware for the Control Panel routes.
Refactored and extended the router generator.
Improved the SEO for the paginators.
Added bits of Microdata to improve SEO further.
Wrote benchmarks for users.Get() and users.BypassGet()
More errors are caught now.
You can now attach pcss files to posts.
Improved the error logging for JavaScript.
Topic list avatars now link to the associated profiles.
Added last poster avatars to the forum list.
2017-10-30 09:57:08 +00:00

124 lines
3.9 KiB
JavaScript

/* ===========================================================
* trumbowyg.preformatted.js v1.0
* Preformatted plugin for Trumbowyg
* http://alex-d.github.com/Trumbowyg
* ===========================================================
* Author : Casella Edoardo (Civile)
*/
(function ($) {
'use strict';
$.extend(true, $.trumbowyg, {
langs: {
// jshint camelcase:false
en: {
preformatted: 'Code sample <pre>'
},
fr: {
preformatted: 'Exemple de code'
},
it: {
preformatted: 'Codice <pre>'
},
zh_cn: {
preformatted: '代码示例 <pre>'
},
ru: {
preformatted: 'Пример кода <pre>'
},
ja: {
preformatted: 'コードサンプル <pre>'
}
},
// jshint camelcase:true
plugins: {
preformatted: {
init: function (trumbowyg) {
var btnDef = {
fn: function () {
trumbowyg.saveRange();
var text = trumbowyg.getRangeText();
if (text.replace(/\s/g, '') !== '') {
try {
var curtag = getSelectionParentElement().tagName.toLowerCase();
if (curtag === 'code' || curtag === 'pre') {
return unwrapCode();
}
else {
trumbowyg.execCmd('insertHTML', '<pre><code>' + strip(text) + '</code></pre>');
}
} catch (e) {
}
}
},
tag: 'pre'
};
trumbowyg.addBtnDef('preformatted', btnDef);
}
}
}
});
/*
* GetSelectionParentElement
*/
function getSelectionParentElement() {
var parentEl = null,
selection;
if (window.getSelection) {
selection = window.getSelection();
if (selection.rangeCount) {
parentEl = selection.getRangeAt(0).commonAncestorContainer;
if (parentEl.nodeType !== 1) {
parentEl = parentEl.parentNode;
}
}
} else if ((selection = document.selection) && selection.type !== 'Control') {
parentEl = selection.createRange().parentElement();
}
return parentEl;
}
/*
* Strip
* returns a text without HTML tags
*/
function strip(html) {
var tmp = document.createElement('DIV');
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || '';
}
/*
* UnwrapCode
* ADD/FIX: to improve, works but can be better
* "paranoic" solution
*/
function unwrapCode() {
var container = null;
if (document.selection) { //for IE
container = document.selection.createRange().parentElement();
} else {
var select = window.getSelection();
if (select.rangeCount > 0) {
container = select.getRangeAt(0).startContainer.parentNode;
}
}
//'paranoic' unwrap
var ispre = $(container).contents().closest('pre').length;
var iscode = $(container).contents().closest('code').length;
if (ispre && iscode) {
$(container).contents().unwrap('code').unwrap('pre');
} else if (ispre) {
$(container).contents().unwrap('pre');
} else if (iscode) {
$(container).contents().unwrap('code');
}
}
})(jQuery);