plugin.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.3.0 (2020-05-21)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var global$1 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  13. var global$2 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  14. var enableWhenDirty = function (editor) {
  15. return editor.getParam('save_enablewhendirty', true);
  16. };
  17. var hasOnSaveCallback = function (editor) {
  18. return !!editor.getParam('save_onsavecallback');
  19. };
  20. var hasOnCancelCallback = function (editor) {
  21. return !!editor.getParam('save_oncancelcallback');
  22. };
  23. var displayErrorMessage = function (editor, message) {
  24. editor.notificationManager.open({
  25. text: message,
  26. type: 'error'
  27. });
  28. };
  29. var save = function (editor) {
  30. var formObj;
  31. formObj = global$1.DOM.getParent(editor.id, 'form');
  32. if (enableWhenDirty(editor) && !editor.isDirty()) {
  33. return;
  34. }
  35. editor.save();
  36. if (hasOnSaveCallback(editor)) {
  37. editor.execCallback('save_onsavecallback', editor);
  38. editor.nodeChanged();
  39. return;
  40. }
  41. if (formObj) {
  42. editor.setDirty(false);
  43. if (!formObj.onsubmit || formObj.onsubmit()) {
  44. if (typeof formObj.submit === 'function') {
  45. formObj.submit();
  46. } else {
  47. displayErrorMessage(editor, 'Error: Form submit field collision.');
  48. }
  49. }
  50. editor.nodeChanged();
  51. } else {
  52. displayErrorMessage(editor, 'Error: No form element found.');
  53. }
  54. };
  55. var cancel = function (editor) {
  56. var h = global$2.trim(editor.startContent);
  57. if (hasOnCancelCallback(editor)) {
  58. editor.execCallback('save_oncancelcallback', editor);
  59. return;
  60. }
  61. editor.resetContent(h);
  62. };
  63. var register = function (editor) {
  64. editor.addCommand('mceSave', function () {
  65. save(editor);
  66. });
  67. editor.addCommand('mceCancel', function () {
  68. cancel(editor);
  69. });
  70. };
  71. var stateToggle = function (editor) {
  72. return function (api) {
  73. var handler = function () {
  74. api.setDisabled(enableWhenDirty(editor) && !editor.isDirty());
  75. };
  76. editor.on('NodeChange dirty', handler);
  77. return function () {
  78. return editor.off('NodeChange dirty', handler);
  79. };
  80. };
  81. };
  82. var register$1 = function (editor) {
  83. editor.ui.registry.addButton('save', {
  84. icon: 'save',
  85. tooltip: 'Save',
  86. disabled: true,
  87. onAction: function () {
  88. return editor.execCommand('mceSave');
  89. },
  90. onSetup: stateToggle(editor)
  91. });
  92. editor.ui.registry.addButton('cancel', {
  93. icon: 'cancel',
  94. tooltip: 'Cancel',
  95. disabled: true,
  96. onAction: function () {
  97. return editor.execCommand('mceCancel');
  98. },
  99. onSetup: stateToggle(editor)
  100. });
  101. editor.addShortcut('Meta+S', '', 'mceSave');
  102. };
  103. function Plugin () {
  104. global.add('save', function (editor) {
  105. register$1(editor);
  106. register(editor);
  107. });
  108. }
  109. Plugin();
  110. }());