123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- /**
- * This file is part of the ForkBB <https://github.com/forkbb>.
- *
- * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
- * @license The MIT License (MIT)
- */
- if (typeof ForkBB === "undefined" || !ForkBB) {
- var ForkBB = {};
- }
- ForkBB.editor = (function (doc, win) {
- 'use strict';
- var instance,
- nameSelector = ".f-username",
- dataName = "data-SCEditorConfig",
- emotName = "data-smiliesEnabled",
- linkName = "data-linkEnabled",
- selector = "textarea[" + dataName + "]",
- textarea,
- elForScroll,
- options = {
- format: 'bbcode',
- icons: 'monocons',
- style: '',
- emoticonsCompat: true,
- emoticonsEnabled: false,
- resizeWidth: false,
- width: '100%',
- toolbar: 'bold,italic,underline,strike,subscript,superscript|' +
- 'left,center,right,justify|font,size,color,removeformat|' +
- 'bulletlist,orderedlist,indent,outdent|' +
- 'table|code,mono,quote|horizontalrule,image,email,link,unlink|' +
- 'emoticon,date,time|maximize,source'
- };
- function initEditor()
- {
- var conf, smiliesEnabled, linkEnabled;
- if (
- !Object.assign
- || !sceditor
- || !(textarea = doc.querySelector(selector))
- || !(conf = JSON.parse(textarea.getAttribute(dataName)))
- ) {
- return;
- }
- options = Object.assign(options, conf);
- smiliesEnabled = '1' == textarea.getAttribute(emotName);
- linkEnabled = '1' == textarea.getAttribute(linkName);
- var forDelete = ['youtube', 'rtl', 'ltr'];
- if (!smiliesEnabled) {
- forDelete = forDelete.concat('emoticon');
- }
- if (!linkEnabled) {
- forDelete = forDelete.concat('url', 'link', 'image', 'img', 'email');
- }
- for (var bbcodeForDelete of forDelete) {
- sceditor.command.remove(bbcodeForDelete);
- sceditor.formats.bbcode.remove(bbcodeForDelete);
- options.toolbar = options.toolbar.replace(new RegExp("\\b" + bbcodeForDelete + "\\b", "gi"), '');
- }
- options.toolbar = options.toolbar.replace(/[^\w]*\|[^\w]*/g, '|').replace(/,{2,}/g, ',');
- sceditor.create(textarea, options);
- instance = sceditor.instance(textarea);
- if (smiliesEnabled) {
- var checkbox = doc.querySelector('input[name="hide_smilies"]');
- if (checkbox) {
- checkbox.addEventListener('change', function (e) {
- instance.emoticons(!e.target.checked);
- });
- instance.emoticons(!checkbox.checked);
- } else {
- instance.emoticons(true);
- }
- }
- elForScroll = textarea.parentNode;
- var users = doc.querySelectorAll(nameSelector);
- for (var node of users) {
- var a = doc.createElement("a");
- a.textContent = "@";
- a.addEventListener('click', function (e) {
- instance.insert("[b]" + e.target.parentNode.textContent + "[/b], ");
- elForScroll.scrollIntoView({behavior: "smooth", block: "end"});
- });
- node.insertBefore(a, node.firstChild);
- }
- }
- return {
- init : function () {
- initEditor();
- },
- getInstance : function () {
- return instance;
- }
- };
- }(document, window));
- document.addEventListener("DOMContentLoaded", ForkBB.editor.init, false);
|