LZT Froala AIT

Add Template Button to all froala instances

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name LZT Froala AIT
// @namespace lzt-froala-init-template
// @description Add Template Button to all froala instances
// @version 1.0.0
// @author Toil
// @grant GM_setValue
// @grant GM_getValue
// @homepageURL https://t.me/lzt_upgrade
// @icon https://cdn.lztup.net/brand/logo-mini.png
// @license MIT
// @match *://lolz.guru/*
// @match *://lolz.live/*
// @match *://zelenka.guru/*
// @match *://zelenka.market/*
// @match *://lzt.market/*
// @match *://lolz.market/*
// @supportURL https://t.me/lzt_upgrade
// ==/UserScript==
// Generated by LZT Upgrade extension bundler. Telegram: https://t.me/lzt_upgrade Github: https://github.com/lzt-upgrade

// lzt-froala-init-template/index.js
if (typeof FroalaEditor !== "function") {
  console.log("[lztup] FroalaEditor isn't loaded");
  return;
}
var XF_LANG = XenForo?.visitor?.language_id === 1 ? "en" : "ru";
var i18n = {
  ru: {
    templates: "Шаблоны",
    create_template: "Создать шаблон",
    no_templates: "Вы пока что не добавили ни одного шаблона"
  },
  en: {
    templates: "Templates",
    create_template: "Create a template",
    no_templates: "You have not added any templates"
  },
  get(phrase) {
    return this[XF_LANG]?.[phrase] ?? phrase;
  }
};

class GM_Template {
  static key = "conversation_templates";
  static remove(id) {
    if (Number.isNaN(id)) {
      return;
    }
    const templates = GM_Template.get();
    GM_setValue(GM_Template.key, templates.filter((template) => template.id !== id));
  }
  static get() {
    return GM_getValue(GM_Template.key, []);
  }
  static push(templateData) {
    const templates = GM_Template.get();
    templates.push(templateData);
    GM_setValue(GM_Template.key, templates);
  }
  static set(templatesData) {
    GM_setValue(GM_Template.key, templatesData);
  }
}
function getTemplateItemsContent() {
  const templates = GM_Template.get();
  return templates.map((template) => {
    const { id, title, content, contentHtml } = template;
    return `
      <div id="ctemplate-${id}" class="template">
        <div class="actions">
          <a class="templateIcon editIcon fas fa-pen OverlayTrigger" href="conversations/template?template_id=${id}"></a>
          <a class="templateIcon deleteIcon DeleteTemplate fas fa-trash-alt" href="conversations/template/delete?template_id=${id}"></a>
        </div>

        <div class="InsertTemplate">
          <div class="title bold">${title}</div>
          <div class="content">${content}</div>
          <div class="ContentHtml dnone">${contentHtml}</div>
        </div>
      </div>`;
  }).join(`
`);
}
function registerAlwaysInitTemplate(froala) {
  const $parent = $(froala.$box.parent());
  if ($parent.find(".js-lzt-fe-extraButtons").length) {
    console.log("[lztup] templates button already exists");
    return;
  }
  XenForo.scriptLoader.loadCss(["conversation"], `css.php?css=__sentinel__&style=${XenForo.visitor.style_id}&_v=${XenForo._jsVersion}`);
  const $lztTemplateBtn = $(`
    <div id="lztup-fe-lztTemplate" type="button" tabindex="-1" role="button" aria-haspopup="true" class="fr-command fr-btn" data-cmd="lztTemplate" title="${i18n.get("templates")}">
      <i class="fal fa-drafting-compass" aria-hidden="true"></i>
    </div>`);
  const templateItemsContent = getTemplateItemsContent();
  const $lztTemplateBox = $(`
    <div style="display: none">
			<div id="ConversationTemplates" class="ConversationTemplates conversationTemplatesBox">
				<div class="header">
					<span class="title bold">${i18n.get("templates")}</span>
					<div class="fl_r">
						<a href="conversations/template" class="OverlayTrigger">${i18n.get("create_template")}</a>
					</div>
				</div>
				<div id="ConversationTemplateList" class="templateList">${templateItemsContent}</div>
				<div class="NoTemplates noConversationTemplates muted">${i18n.get("no_templates")}</div>
      </div>
		</div>`);
  if (templateItemsContent.length) {
    $lztTemplateBox.find(".NoTemplates").hide();
  }
  $(froala.$tb.find(".fr-float-right").first()).append($lztTemplateBtn);
  $parent.append($lztTemplateBox);
  const $templatesBox = $("#ConversationTemplates").clone();
  if (!$templatesBox.length) {
    return;
  }
  $templatesBox.data("lzt-fe-ed", froala);
  $lztTemplateBtn.data("tippy-content", $templatesBox[0]);
  XenForo.tippy($lztTemplateBtn[0], {
    content: $templatesBox[0],
    onShown: function() {
      $templatesBox.xfActivate();
    },
    onShow() {
      froala.selection.save();
    },
    onHide() {
      froala.selection.restore();
    },
    boundary: "window"
  }, "popup");
}
FroalaEditor.MODULES.alwaysInitTemplate = function(e) {
  return {
    _init: registerAlwaysInitTemplate(e)
  };
};
var getJQText = ($el, selector) => {
  return $el.find(selector).text().trim();
};
var parseTemplateId = ($el) => parseInt($el.attr("id")?.replace("ctemplate-", ""));
function parseTemplateItem($el) {
  const id = parseTemplateId($el);
  if (Number.isNaN(id)) {
    return null;
  }
  return {
    id,
    title: getJQText($el, ".title.bold"),
    content: getJQText($el, ".content"),
    contentHtml: getJQText($el, ".ContentHtml")
  };
}
function saveTemplatesToGM(froala) {
  const $parent = $(froala.$box.parent());
  if (!$parent.find(".js-lzt-fe-extraButtons > #lzt-fe-eb-lztTemplate").length) {
    console.log("[lztup] extra template buttons not found");
    return;
  }
  const $templateListItems = $parent.find("#ConversationTemplates > #ConversationTemplateList > .template");
  if (!$templateListItems.length) {
    console.log("[lztup] templates list items not found");
    return;
  }
  const templateList = Array.from($templateListItems).map((el) => parseTemplateItem($(el))).filter(Boolean);
  GM_Template.set(templateList);
}
FroalaEditor.MODULES.saveTemplatesToGM = function(e) {
  return {
    _init: saveTemplatesToGM(e)
  };
};
XenForo.LZTUpgradeConversationTemplateDelete = function($button) {
  $button.on("click", (e) => {
    e.preventDefault();
    const $template = $button.closest(".template");
    const id = parseTemplateId($template);
    GM_Template.remove(id);
  });
};
XenForo.LZTUpgradeConversationTemplatesCreate = function($form) {
  $form.on("AutoValidationComplete", (e) => {
    e.preventDefault();
    const $template = $(e.ajaxData.templateHtml);
    const id = parseTemplateId($template);
    if (e.ajaxData.templateUpdated) {
      GM_Template.remove(id);
    }
    const templateData = parseTemplateItem($template);
    GM_Template.push(templateData);
  });
};
XenForo.register(".DeleteTemplate", "XenForo.LZTUpgradeConversationTemplateDelete");
XenForo.register("#ConversationTemplateCreateForm", "XenForo.LZTUpgradeConversationTemplatesCreate");