math.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. KindEditor.plugin('math', function (K) {
  2. var self = this, name = 'math', lang = self.lang(name + '.');
  3. self.clickToolbar(name, function () {
  4. var img = self.plugin.getSelectedImage();
  5. var latex = $(img).data("latex") || '';
  6. var html = [
  7. '<div class="ke-dialog-content-inner">',
  8. '<div class="tabs"></div>',
  9. '<div class="ke-formula" style="width:510px;height:380px;"></div>',
  10. '</div>'
  11. ].join('');
  12. var iframe = K('<iframe class="" frameborder="0" src="' + self.pluginsPath + 'math/formula.html?latex=' + encodeURIComponent(latex) + '&previewUrl=' + encodeURIComponent(self.options.formulaPreviewUrl) + '" style="width:100%;height:300px;"></iframe>');
  13. var dialog = self.createDialog({
  14. name: name,
  15. width: Math.min(document.body.clientWidth, 500),
  16. height: 380,
  17. title: "插入公式",
  18. body: html,
  19. yesBtn: {
  20. name: '插入',
  21. click: function (e) {
  22. var win = iframe[0].contentWindow;
  23. var currentSVG = win.$("#preview-body")[0].querySelector("svg");
  24. if (!currentSVG) {
  25. alert("请输入正确的公式");
  26. }
  27. const blob = new Blob([currentSVG.outerHTML], { type: 'image/svg+xml' });
  28. const file = new File([blob], "latex.svg", { type: 'image/svg+xml' });
  29. require(['upload'], function(Upload){
  30. Upload.api.send(file, function (data, ret) {
  31. var url = Fast.api.cdnurl(ret.data.url);
  32. var latex = win.$("#latex-source").val();
  33. if (latex == '') {
  34. Layer.msg("请选择或输入公式");
  35. return false;
  36. }
  37. self.insertHtml("<img src='" + url + "' data-latex='" + latex + "'>");
  38. self.hideDialog().focus();
  39. });
  40. });
  41. return;
  42. }
  43. },
  44. noBtn: {
  45. name: self.lang('no'),
  46. click: function (e) {
  47. self.hideDialog().focus();
  48. }
  49. }
  50. }),
  51. div = dialog.div;
  52. K('.ke-formula', div).replaceWith(iframe);
  53. return;
  54. });
  55. });