addons.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. define([], function () {
  2. //判断系统深色模式变化,修改切换按钮
  3. var matchMedia = window.matchMedia(('(prefers-color-scheme: dark)'));
  4. matchMedia.addEventListener('change', function () {
  5. var mode = this.matches ? 'dark' : 'light';
  6. //只有当cookie中无手动定义值时才进行操作
  7. if (document.cookie.indexOf("thememode=") === -1 && Config.darktheme.mode === 'auto') {
  8. $("body").toggleClass("darktheme", mode === "dark");
  9. }
  10. });
  11. if (typeof Config.darktheme !== 'undefined' && Config.darktheme.switchbtn) {
  12. // 切换模式
  13. var switchMode = function (mode) {
  14. // 获取当前深色模式
  15. if (mode === 'auto') {
  16. var isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
  17. mode = isDarkMode ? 'dark' : 'light';
  18. }
  19. if (mode === 'auto') {
  20. } else if (mode === 'dark') {
  21. $("body").addClass("darktheme");
  22. $(".darktheme-link").removeAttr("media");
  23. } else {
  24. $("body").removeClass("darktheme");
  25. $(".darktheme-link").attr("media", "(prefers-color-scheme: dark)");
  26. }
  27. };
  28. // 创建Cookie
  29. var createCookie = function (name, value) {
  30. var date = new Date();
  31. date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
  32. var url = Config.moduleurl.replace(location.origin, "");
  33. var path = url ? url.substring(url.lastIndexOf("/")) : "/";
  34. document.cookie = encodeURIComponent(Config.cookie.prefix + name) + "=" + encodeURIComponent(value) + "; path=" + path + "; expires=" + date.toGMTString();
  35. };
  36. if (Config.controllername === 'index' && Config.actionname === 'index') {
  37. var mode = Config.darktheme.mode;
  38. if (mode === 'auto') {
  39. var isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
  40. mode = isDarkMode ? 'dark' : 'light';
  41. }
  42. var html = '<li class="theme-li">' +
  43. '<button type="button" title="切换' + (mode === 'dark' ? '浅色' : '深色') + '模式" data-mode="' + (mode === 'dark' ? 'light' : 'dark') + '" class="theme-toggle">' +
  44. '<svg class="sun-and-moon" aria-hidden="true" width="24" height="24" viewBox="0 0 24 24">\n' +
  45. ' <circle class="sun" cx="12" cy="12" r="6" mask="url(#moon-mask)" fill="currentColor" />\n' +
  46. ' <g class="sun-beams" stroke="currentColor">\n' +
  47. ' <line x1="12" y1="1" x2="12" y2="3" />\n' +
  48. ' <line x1="12" y1="21" x2="12" y2="23" />\n' +
  49. ' <line x1="4.22" y1="4.22" x2="5.64" y2="5.64" />\n' +
  50. ' <line x1="18.36" y1="18.36" x2="19.78" y2="19.78" />\n' +
  51. ' <line x1="1" y1="12" x2="3" y2="12" />\n' +
  52. ' <line x1="21" y1="12" x2="23" y2="12" />\n' +
  53. ' <line x1="4.22" y1="19.78" x2="5.64" y2="18.36" />\n' +
  54. ' <line x1="18.36" y1="5.64" x2="19.78" y2="4.22" />\n' +
  55. ' </g>\n' +
  56. ' <mask class="moon" id="moon-mask">\n' +
  57. ' <rect x="0" y="0" width="100%" height="100%" fill="white" />\n' +
  58. ' <circle cx="24" cy="10" r="6" fill="black" />\n' +
  59. ' </mask>\n' +
  60. ' </svg>' +
  61. '</button>' +
  62. '</li>';
  63. $(html).prependTo("#firstnav > div > ul");
  64. //点击切换按钮
  65. $(document).on("click", ".theme-toggle", function () {
  66. var mode = $(this).attr("data-mode");
  67. switchMode(mode);
  68. createCookie("thememode", mode);
  69. $("iframe").each(function () {
  70. try {
  71. $(this)[0].contentWindow.$("body").trigger("swithmode", [mode]);
  72. } catch (e) {
  73. }
  74. });
  75. $(this).attr("data-mode", mode === 'dark' ? 'light' : 'dark').attr("title", '切换' + (mode === 'dark' ? '浅色' : '深色') + '模式');
  76. });
  77. //判断系统深色模式变化,修改切换按钮
  78. var matchMedia = window.matchMedia(('(prefers-color-scheme: dark)'));
  79. matchMedia.addEventListener('change', function () {
  80. var mode = this.matches ? 'dark' : 'light';
  81. //只有当cookie中无手动定义值时才切换
  82. if (document.cookie.indexOf("thememode=") === -1 && Config.darktheme.mode === 'auto') {
  83. $(".theme-toggle").attr("data-mode", mode === 'dark' ? 'light' : 'dark').attr("title", '切换' + (mode === 'dark' ? '浅色' : '深色') + '模式');
  84. }
  85. });
  86. } else {
  87. //添加事件
  88. $("body").on("swithmode", function (e, mode) {
  89. switchMode(mode);
  90. $("iframe").each(function () {
  91. try {
  92. $(this)[0].contentWindow.$("body").trigger("swithmode", [mode]);
  93. } catch (e) {
  94. }
  95. });
  96. });
  97. }
  98. }
  99. require.config({
  100. paths: {
  101. 'bootstrap-markdown': '../addons/markdown/js/bootstrap-markdown.min',
  102. 'hyperdown': '../addons/markdown/js/hyperdown.min',
  103. 'turndown': '../addons/markdown/js/turndown',
  104. },
  105. shim: {
  106. 'bootstrap-markdown': {
  107. deps: [
  108. 'jquery',
  109. 'css!../addons/markdown/css/bootstrap-markdown.css'
  110. ],
  111. exports: '$.fn.markdown'
  112. }
  113. }
  114. });
  115. require(['form', 'upload'], function (Form, Upload) {
  116. var _bindevent = Form.events.bindevent;
  117. Form.events.bindevent = function (form) {
  118. _bindevent.apply(this, [form]);
  119. var insert = function (e, url, type) {
  120. var urlArr = url.split(/\,/);
  121. $.each(urlArr, function () {
  122. var url = Fast.api.cdnurl(this, true);
  123. if (type && type == 'image') {
  124. e.replaceSelection("\n" + '![输入图片说明](' + url + ')');
  125. } else {
  126. e.replaceSelection("\n" + '[输入链接说明](' + url + ')');
  127. }
  128. });
  129. e.change(e);
  130. // e.$element.blur();
  131. // e.$element.focus();
  132. };
  133. try {
  134. if ($(Config.markdown.classname || '.editor', form).length > 0) {
  135. require(['bootstrap-markdown', 'hyperdown', 'turndown'], function (undefined, undefined, Turndown) {
  136. $.fn.markdown.messages.zh = {
  137. Bold: "粗体",
  138. Italic: "斜体",
  139. Heading: "标题",
  140. "URL/Link": "链接",
  141. Image: "图片",
  142. List: "列表",
  143. "Unordered List": "无序列表",
  144. "Ordered List": "有序列表",
  145. Code: "代码",
  146. Quote: "引用",
  147. Preview: "预览",
  148. "strong text": "粗体",
  149. "emphasized text": "强调",
  150. "heading text": "标题",
  151. "enter link description here": "输入链接说明",
  152. "Insert Hyperlink": "URL地址",
  153. "enter image description here": "输入图片说明",
  154. "Insert Image Hyperlink": "图片URL地址",
  155. "enter image title here": "在这里输入图片标题",
  156. "list text here": "这里是列表文本",
  157. "code text here": "这里输入代码",
  158. "quote here": "这里输入引用文本"
  159. };
  160. var parser = new HyperDown();
  161. window.marked = function (text) {
  162. return parser.makeHtml(text);
  163. };
  164. var uploadFiles;
  165. uploadFiles = async function (files) {
  166. var self = this;
  167. for (var i = 0; i < files.length; i++) {
  168. try {
  169. await new Promise((resolve) => {
  170. var url, html, file;
  171. file = files[i];
  172. Upload.api.send(file, function (data) {
  173. url = Fast.api.cdnurl(data.url, true);
  174. if (file.type.indexOf("image") !== -1) {
  175. insert(self, url, 'image');
  176. } else {
  177. insert(self, url, 'file');
  178. }
  179. resolve();
  180. }, function () {
  181. resolve();
  182. });
  183. });
  184. } catch (e) {
  185. }
  186. }
  187. };
  188. $(Config.markdown.classname || '.editor', form).each(function () {
  189. var options = $(this).data("markdown-options") || {};
  190. var editor = $(this);
  191. var format = typeof options.format !== 'undefined' ? options.format : Config.markdown.format;
  192. if (format === 'html') {
  193. var origin = editor;
  194. var turndownService = new TurndownService();
  195. turndownService.use(turndownPluginGfm.gfm);
  196. var content = turndownService.turndown(origin.val());
  197. editor = origin.clone().removeAttr("name").removeAttr("id").val(content);
  198. origin.css("display", "none");
  199. editor.data("markdown-origin", origin);
  200. editor.insertAfter(origin);
  201. }
  202. (function (editor) {
  203. editor.markdown($.extend(true, {
  204. resize: 'vertical',
  205. language: 'zh',
  206. iconlibrary: 'fa',
  207. autofocus: false,
  208. savable: false,
  209. additionalButtons: [
  210. [{
  211. name: "groupCustom",
  212. data: [{
  213. name: "cmdUploadImage",
  214. toggle: false,
  215. title: "Upload image",
  216. icon: "fa fa-upload",
  217. }, {
  218. name: "cmdUploadFile",
  219. toggle: false,
  220. title: "Upload file",
  221. icon: "fa fa-cloud-upload",
  222. }, {
  223. name: "cmdSelectImage",
  224. toggle: false,
  225. title: "Select image",
  226. icon: "fa fa-file-image-o",
  227. callback: function (e) {
  228. parent.Fast.api.open("general/attachment/select?element_id=&multiple=true&mimetype=image/*", __('Choose'), {
  229. callback: function (data) {
  230. var urlArr = data.url.split(/\,/);
  231. insert(e, data.url, 'image');
  232. }
  233. });
  234. return false;
  235. }
  236. }, {
  237. name: "cmdSelectAttachment",
  238. toggle: false,
  239. title: "Select file",
  240. icon: "fa fa-file",
  241. callback: function (e) {
  242. parent.Fast.api.open("general/attachment/select?element_id=&multiple=true&mimetype=*", __('Choose'), {
  243. callback: function (data) {
  244. insert(e, data.url, 'file');
  245. }
  246. });
  247. return false;
  248. }
  249. }]
  250. }]
  251. ],
  252. onShow: function (e) {
  253. //添加上传图片按钮和上传附件按钮
  254. var imgBtn = $("button[data-handler='bootstrap-markdown-cmdUploadImage']", e.$editor);
  255. var fileBtn = $("button[data-handler='bootstrap-markdown-cmdUploadFile']", e.$editor);
  256. var btnParent = imgBtn.parent();
  257. btnParent.addClass("md-relative");
  258. 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>');
  259. upImgBtn.css(imgBtn.position()).appendTo(btnParent);
  260. var upFileBtn = $('<button type="button" class="uploadfile faupload" data-button="file" title="点击上传附件" data-multiple="true">点击上传附件</button>');
  261. upFileBtn.css(fileBtn.position()).appendTo(btnParent);
  262. upImgBtn.data("upload-success", function (data, ret) {
  263. insert(e, data.url, 'image');
  264. });
  265. upFileBtn.data("upload-success", function (data, ret) {
  266. insert(e, data.url, 'file');
  267. });
  268. Form.events.faupload(e.$editor);
  269. $(".uploadimage,.uploadfile", e.$editor).on("mouseenter", function () {
  270. ($(this).data("button") === 'image' ? imgBtn : fileBtn).addClass("active");
  271. }).on("mouseleave", function () {
  272. ($(this).data("button") === 'image' ? imgBtn : fileBtn).removeClass("active");
  273. });
  274. //粘贴上传
  275. $(e.$textarea).bind('paste', function (event) {
  276. var originalEvent;
  277. originalEvent = event.originalEvent;
  278. if (originalEvent.clipboardData && originalEvent.clipboardData.files.length > 0) {
  279. uploadFiles.call(e, originalEvent.clipboardData.files);
  280. return false;
  281. }
  282. });
  283. //拖拽上传
  284. $(e.$textarea).bind('drop', function (event) {
  285. var originalEvent;
  286. originalEvent = event.originalEvent;
  287. if (originalEvent.dataTransfer && originalEvent.dataTransfer.files.length > 0) {
  288. uploadFiles.call(e, originalEvent.dataTransfer.files);
  289. return false;
  290. }
  291. });
  292. },
  293. onChange: function (e) {
  294. var origin = $(e.$textarea).data("markdown-origin");
  295. if (origin) {
  296. origin.val(marked(e.$textarea.val()));
  297. }
  298. }
  299. }, editor.data("markdown-options") || {}));
  300. })(editor)
  301. });
  302. });
  303. }
  304. } catch (e) {
  305. console.log(e);
  306. }
  307. };
  308. });
  309. });