plugin.min.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. (function() {
  2. const qiniuOption = {
  3. watermark: 'aHR0cHM6Ly93ZGwud2FsbHN0cmVldGNuLmNvbS8yM2Y0ZWE2Ny1hZjI1LTQ3ZTItYTFlYy1iNzJjNzkzYWNlOGE=',
  4. weexWatermark: 'aHR0cHM6Ly93cGltZy53YWxsc3Rjbi5jb20vMGQxMmMwN2ItNjViMS00NjA4LTllMTctODUyMDRhOTc3YzY5',
  5. dissolve: 30,
  6. dx: 20,
  7. dy: 20,
  8. watermarkScale: 0
  9. };
  10. tinymce.create('tinymce.plugins.WaterMarkPlugin', {
  11. init(ed) {
  12. ed.addCommand('mceWaterMark', () => {
  13. const target = ed.selection.getNode();
  14. const src = $(target).attr('src') + '?watermark/1/image/' + qiniuOption.watermark + '/dissolve/' + qiniuOption.dissolve + '/gravity/Center/dx/' + qiniuOption.dx + '/dy/' + qiniuOption.dy + '/ws/' + qiniuOption.watermarkScale
  15. ed.windowManager.open({
  16. title: '七牛水印',
  17. width: 600,
  18. height: 500,
  19. body: [{
  20. type: 'container',
  21. html: `<div class="mce-container" hidefocus="1" tabindex="-1" >
  22. <div class="mce-container-body mce-container-watermark-body">
  23. <div style="margin-bottom:10px;">
  24. <label><input checked='true' style="margin-left: 20px;margin-right: 5px;" name="image-watermarkType" data-type="watermark" class="note-image-watermarkType" type="radio" />见闻</label>
  25. <label><input style="margin-left: 20px;margin-right: 5px;" name="image-watermarkType" data-type="weexWatermark" class="note-image-watermarkType" type="radio" />WEEX</label>
  26. </div>
  27. <div style='min-height:600px;'>
  28. <label><input style="margin-left: 20px;margin-right: 5px;" name="image-watermark" data-position="NorthWest" class="note-image-watermark" type="radio" />左上</label>
  29. <label><input style="margin-left: 20px;margin-right: 5px;" name="image-watermark" data-position="SouthWest" class="note-image-watermark" type="radio" />左下</label>
  30. <label><input style="margin-left: 20px;margin-right: 5px;" name="image-watermark" data-position="NorthEast" class="note-image-watermark" type="radio" /><span>右上</span></label>
  31. <label><input style="margin-left: 20px;margin-right: 5px;" name="image-watermark" data-position="SouthEast" class="note-image-watermark" type="radio" /><span>右下</span></label>
  32. <label><input checked='true' style="margin-left: 20px;margin-right: 5px;" name="image-watermark" data-position="Center" class="note-image-watermark" type="radio" /><span>正中央</span></label>
  33. <img src=${src} style="display: block;margin: 20px auto;max-height: 420px;max-width: 100%;" class="watermark-preview">
  34. </div>
  35. </div>
  36. </div>`
  37. }],
  38. onSubmit() {
  39. const src = $('.mce-container-watermark-body .watermark-preview').attr('src');
  40. $(target).attr('src', src);
  41. ed.undoManager.add()
  42. // setTimeout(() => {
  43. // ed.insertContent('a')
  44. // }, 100);
  45. }
  46. });
  47. $('.mce-container-watermark-body input[type="radio"]').on('click', () => {
  48. const $watermarkImg = $('.mce-container-watermark-body .watermark-preview')
  49. const baseSrc = $watermarkImg.attr('src').split('?')[0];
  50. const position = $('.mce-container-watermark-body input[name="image-watermark"]:checked').attr('data-position');
  51. const type = $('.mce-container-watermark-body input[name="image-watermarkType"]:checked').attr('data-type');
  52. const query = setQuery(position, type);
  53. $watermarkImg.attr('src', baseSrc + query);
  54. })
  55. });
  56. function setQuery(postion, type) {
  57. return '?watermark/1/image/' + qiniuOption[type] + '/dissolve/' + qiniuOption.dissolve + '/gravity/' + postion + '/dx/' + qiniuOption.dx + '/dy/' + qiniuOption.dy + '/ws/' + qiniuOption.watermarkScale
  58. }
  59. // Register buttons
  60. ed.addButton('watermark', {
  61. title: 'watermark',
  62. text: '水印',
  63. cmd: 'mceWaterMark'
  64. });
  65. }
  66. });
  67. tinymce.PluginManager.add('watermark', tinymce.plugins.WaterMarkPlugin);
  68. }());