| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- define([], function () {
- //判断系统深色模式变化,修改切换按钮
- var matchMedia = window.matchMedia(('(prefers-color-scheme: dark)'));
- matchMedia.addEventListener('change', function () {
- var mode = this.matches ? 'dark' : 'light';
- //只有当cookie中无手动定义值时才进行操作
- if (document.cookie.indexOf("thememode=") === -1 && Config.darktheme.mode === 'auto') {
- $("body").toggleClass("darktheme", mode === "dark");
- }
- });
- if (typeof Config.darktheme !== 'undefined' && Config.darktheme.switchbtn) {
- // 切换模式
- var switchMode = function (mode) {
- // 获取当前深色模式
- if (mode === 'auto') {
- var isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
- mode = isDarkMode ? 'dark' : 'light';
- }
- if (mode === 'auto') {
- } else if (mode === 'dark') {
- $("body").addClass("darktheme");
- $(".darktheme-link").removeAttr("media");
- } else {
- $("body").removeClass("darktheme");
- $(".darktheme-link").attr("media", "(prefers-color-scheme: dark)");
- }
- };
- // 创建Cookie
- var createCookie = function (name, value) {
- var date = new Date();
- date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
- var url = Config.moduleurl.replace(location.origin, "");
- var path = url ? url.substring(url.lastIndexOf("/")) : "/";
- document.cookie = encodeURIComponent(Config.cookie.prefix + name) + "=" + encodeURIComponent(value) + "; path=" + path + "; expires=" + date.toGMTString();
- };
- if (Config.controllername === 'index' && Config.actionname === 'index') {
- var mode = Config.darktheme.mode;
- if (mode === 'auto') {
- var isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
- mode = isDarkMode ? 'dark' : 'light';
- }
- var html = '<li class="theme-li">' +
- '<button type="button" title="切换' + (mode === 'dark' ? '浅色' : '深色') + '模式" data-mode="' + (mode === 'dark' ? 'light' : 'dark') + '" class="theme-toggle">' +
- '<svg class="sun-and-moon" aria-hidden="true" width="24" height="24" viewBox="0 0 24 24">\n' +
- ' <circle class="sun" cx="12" cy="12" r="6" mask="url(#moon-mask)" fill="currentColor" />\n' +
- ' <g class="sun-beams" stroke="currentColor">\n' +
- ' <line x1="12" y1="1" x2="12" y2="3" />\n' +
- ' <line x1="12" y1="21" x2="12" y2="23" />\n' +
- ' <line x1="4.22" y1="4.22" x2="5.64" y2="5.64" />\n' +
- ' <line x1="18.36" y1="18.36" x2="19.78" y2="19.78" />\n' +
- ' <line x1="1" y1="12" x2="3" y2="12" />\n' +
- ' <line x1="21" y1="12" x2="23" y2="12" />\n' +
- ' <line x1="4.22" y1="19.78" x2="5.64" y2="18.36" />\n' +
- ' <line x1="18.36" y1="5.64" x2="19.78" y2="4.22" />\n' +
- ' </g>\n' +
- ' <mask class="moon" id="moon-mask">\n' +
- ' <rect x="0" y="0" width="100%" height="100%" fill="white" />\n' +
- ' <circle cx="24" cy="10" r="6" fill="black" />\n' +
- ' </mask>\n' +
- ' </svg>' +
- '</button>' +
- '</li>';
- $(html).prependTo("#firstnav > div > ul");
- //点击切换按钮
- $(document).on("click", ".theme-toggle", function () {
- var mode = $(this).attr("data-mode");
- switchMode(mode);
- createCookie("thememode", mode);
- $("iframe").each(function () {
- try {
- $(this)[0].contentWindow.$("body").trigger("swithmode", [mode]);
- } catch (e) {
- }
- });
- $(this).attr("data-mode", mode === 'dark' ? 'light' : 'dark').attr("title", '切换' + (mode === 'dark' ? '浅色' : '深色') + '模式');
- });
- //判断系统深色模式变化,修改切换按钮
- var matchMedia = window.matchMedia(('(prefers-color-scheme: dark)'));
- matchMedia.addEventListener('change', function () {
- var mode = this.matches ? 'dark' : 'light';
- //只有当cookie中无手动定义值时才切换
- if (document.cookie.indexOf("thememode=") === -1 && Config.darktheme.mode === 'auto') {
- $(".theme-toggle").attr("data-mode", mode === 'dark' ? 'light' : 'dark').attr("title", '切换' + (mode === 'dark' ? '浅色' : '深色') + '模式');
- }
- });
- } else {
- //添加事件
- $("body").on("swithmode", function (e, mode) {
- switchMode(mode);
- $("iframe").each(function () {
- try {
- $(this)[0].contentWindow.$("body").trigger("swithmode", [mode]);
- } catch (e) {
- }
- });
- });
- }
- }
- require.config({
- paths: {
- 'bootstrap-markdown': '../addons/markdown/js/bootstrap-markdown.min',
- 'hyperdown': '../addons/markdown/js/hyperdown.min',
- 'turndown': '../addons/markdown/js/turndown',
- },
- shim: {
- 'bootstrap-markdown': {
- deps: [
- 'jquery',
- 'css!../addons/markdown/css/bootstrap-markdown.css'
- ],
- exports: '$.fn.markdown'
- }
- }
- });
- require(['form', 'upload'], function (Form, Upload) {
- var _bindevent = Form.events.bindevent;
- Form.events.bindevent = function (form) {
- _bindevent.apply(this, [form]);
- var insert = function (e, url, type) {
- var urlArr = url.split(/\,/);
- $.each(urlArr, function () {
- var url = Fast.api.cdnurl(this, true);
- if (type && type == 'image') {
- e.replaceSelection("\n" + '');
- } else {
- e.replaceSelection("\n" + '[输入链接说明](' + url + ')');
- }
- });
- e.change(e);
- // e.$element.blur();
- // e.$element.focus();
- };
- try {
- if ($(Config.markdown.classname || '.editor', form).length > 0) {
- require(['bootstrap-markdown', 'hyperdown', 'turndown'], function (undefined, undefined, Turndown) {
- $.fn.markdown.messages.zh = {
- Bold: "粗体",
- Italic: "斜体",
- Heading: "标题",
- "URL/Link": "链接",
- Image: "图片",
- List: "列表",
- "Unordered List": "无序列表",
- "Ordered List": "有序列表",
- Code: "代码",
- Quote: "引用",
- Preview: "预览",
- "strong text": "粗体",
- "emphasized text": "强调",
- "heading text": "标题",
- "enter link description here": "输入链接说明",
- "Insert Hyperlink": "URL地址",
- "enter image description here": "输入图片说明",
- "Insert Image Hyperlink": "图片URL地址",
- "enter image title here": "在这里输入图片标题",
- "list text here": "这里是列表文本",
- "code text here": "这里输入代码",
- "quote here": "这里输入引用文本"
- };
- var parser = new HyperDown();
- window.marked = function (text) {
- return parser.makeHtml(text);
- };
- var uploadFiles;
- uploadFiles = async function (files) {
- var self = this;
- for (var i = 0; i < files.length; i++) {
- try {
- await new Promise((resolve) => {
- var url, html, file;
- file = files[i];
- Upload.api.send(file, function (data) {
- url = Fast.api.cdnurl(data.url, true);
- if (file.type.indexOf("image") !== -1) {
- insert(self, url, 'image');
- } else {
- insert(self, url, 'file');
- }
- resolve();
- }, function () {
- resolve();
- });
- });
- } catch (e) {
- }
- }
- };
- $(Config.markdown.classname || '.editor', form).each(function () {
- var options = $(this).data("markdown-options") || {};
- var editor = $(this);
- var format = typeof options.format !== 'undefined' ? options.format : Config.markdown.format;
- if (format === 'html') {
- var origin = editor;
- var turndownService = new TurndownService();
- turndownService.use(turndownPluginGfm.gfm);
- var content = turndownService.turndown(origin.val());
- editor = origin.clone().removeAttr("name").removeAttr("id").val(content);
- origin.css("display", "none");
- editor.data("markdown-origin", origin);
- editor.insertAfter(origin);
- }
- (function (editor) {
- editor.markdown($.extend(true, {
- resize: 'vertical',
- language: 'zh',
- iconlibrary: 'fa',
- autofocus: false,
- savable: false,
- additionalButtons: [
- [{
- name: "groupCustom",
- data: [{
- name: "cmdUploadImage",
- toggle: false,
- title: "Upload image",
- icon: "fa fa-upload",
- }, {
- name: "cmdUploadFile",
- toggle: false,
- title: "Upload file",
- icon: "fa fa-cloud-upload",
- }, {
- name: "cmdSelectImage",
- toggle: false,
- title: "Select image",
- icon: "fa fa-file-image-o",
- callback: function (e) {
- parent.Fast.api.open("general/attachment/select?element_id=&multiple=true&mimetype=image/*", __('Choose'), {
- callback: function (data) {
- var urlArr = data.url.split(/\,/);
- insert(e, data.url, 'image');
- }
- });
- return false;
- }
- }, {
- name: "cmdSelectAttachment",
- toggle: false,
- title: "Select file",
- icon: "fa fa-file",
- callback: function (e) {
- parent.Fast.api.open("general/attachment/select?element_id=&multiple=true&mimetype=*", __('Choose'), {
- callback: function (data) {
- insert(e, data.url, 'file');
- }
- });
- return false;
- }
- }]
- }]
- ],
- onShow: function (e) {
- //添加上传图片按钮和上传附件按钮
- var imgBtn = $("button[data-handler='bootstrap-markdown-cmdUploadImage']", e.$editor);
- var fileBtn = $("button[data-handler='bootstrap-markdown-cmdUploadFile']", e.$editor);
- var btnParent = imgBtn.parent();
- btnParent.addClass("md-relative");
- var upImgBtn = $('<button type="button" class="uploadimage faupload" data-button="image" title="点击上传图片" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp,image/webp" data-multiple="true">点击上传图片</button>');
- upImgBtn.css(imgBtn.position()).appendTo(btnParent);
- var upFileBtn = $('<button type="button" class="uploadfile faupload" data-button="file" title="点击上传附件" data-multiple="true">点击上传附件</button>');
- upFileBtn.css(fileBtn.position()).appendTo(btnParent);
- upImgBtn.data("upload-success", function (data, ret) {
- insert(e, data.url, 'image');
- });
- upFileBtn.data("upload-success", function (data, ret) {
- insert(e, data.url, 'file');
- });
- Form.events.faupload(e.$editor);
- $(".uploadimage,.uploadfile", e.$editor).on("mouseenter", function () {
- ($(this).data("button") === 'image' ? imgBtn : fileBtn).addClass("active");
- }).on("mouseleave", function () {
- ($(this).data("button") === 'image' ? imgBtn : fileBtn).removeClass("active");
- });
- //粘贴上传
- $(e.$textarea).bind('paste', function (event) {
- var originalEvent;
- originalEvent = event.originalEvent;
- if (originalEvent.clipboardData && originalEvent.clipboardData.files.length > 0) {
- uploadFiles.call(e, originalEvent.clipboardData.files);
- return false;
- }
- });
- //拖拽上传
- $(e.$textarea).bind('drop', function (event) {
- var originalEvent;
- originalEvent = event.originalEvent;
- if (originalEvent.dataTransfer && originalEvent.dataTransfer.files.length > 0) {
- uploadFiles.call(e, originalEvent.dataTransfer.files);
- return false;
- }
- });
- },
- onChange: function (e) {
- var origin = $(e.$textarea).data("markdown-origin");
- if (origin) {
- origin.val(marked(e.$textarea.val()));
- }
- }
- }, editor.data("markdown-options") || {}));
- })(editor)
- });
- });
- }
- } catch (e) {
- console.log(e);
- }
- };
- });
- });
|