plugin.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.2.0 (2020-02-13)
  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 Settings = {
  24. enableWhenDirty: enableWhenDirty,
  25. hasOnSaveCallback: hasOnSaveCallback,
  26. hasOnCancelCallback: hasOnCancelCallback
  27. };
  28. var displayErrorMessage = function (editor, message) {
  29. editor.notificationManager.open({
  30. text: message,
  31. type: 'error'
  32. });
  33. };
  34. var save = function (editor) {
  35. var formObj;
  36. formObj = global$1.DOM.getParent(editor.id, 'form');
  37. if (Settings.enableWhenDirty(editor) && !editor.isDirty()) {
  38. return;
  39. }
  40. editor.save();
  41. if (Settings.hasOnSaveCallback(editor)) {
  42. editor.execCallback('save_onsavecallback', editor);
  43. editor.nodeChanged();
  44. return;
  45. }
  46. if (formObj) {
  47. editor.setDirty(false);
  48. if (!formObj.onsubmit || formObj.onsubmit()) {
  49. if (typeof formObj.submit === 'function') {
  50. formObj.submit();
  51. } else {
  52. displayErrorMessage(editor, 'Error: Form submit field collision.');
  53. }
  54. }
  55. editor.nodeChanged();
  56. } else {
  57. displayErrorMessage(editor, 'Error: No form element found.');
  58. }
  59. };
  60. var cancel = function (editor) {
  61. var h = global$2.trim(editor.startContent);
  62. if (Settings.hasOnCancelCallback(editor)) {
  63. editor.execCallback('save_oncancelcallback', editor);
  64. return;
  65. }
  66. editor.resetContent(h);
  67. };
  68. var Actions = {
  69. save: save,
  70. cancel: cancel
  71. };
  72. var register = function (editor) {
  73. editor.addCommand('mceSave', function () {
  74. Actions.save(editor);
  75. });
  76. editor.addCommand('mceCancel', function () {
  77. Actions.cancel(editor);
  78. });
  79. };
  80. var Commands = { register: register };
  81. var stateToggle = function (editor) {
  82. return function (api) {
  83. var handler = function () {
  84. api.setDisabled(Settings.enableWhenDirty(editor) && !editor.isDirty());
  85. };
  86. editor.on('NodeChange dirty', handler);
  87. return function () {
  88. return editor.off('NodeChange dirty', handler);
  89. };
  90. };
  91. };
  92. var register$1 = function (editor) {
  93. editor.ui.registry.addButton('save', {
  94. icon: 'save',
  95. tooltip: 'Save',
  96. disabled: true,
  97. onAction: function () {
  98. return editor.execCommand('mceSave');
  99. },
  100. onSetup: stateToggle(editor)
  101. });
  102. editor.ui.registry.addButton('cancel', {
  103. icon: 'cancel',
  104. tooltip: 'Cancel',
  105. disabled: true,
  106. onAction: function () {
  107. return editor.execCommand('mceCancel');
  108. },
  109. onSetup: stateToggle(editor)
  110. });
  111. editor.addShortcut('Meta+S', '', 'mceSave');
  112. };
  113. var Buttons = { register: register$1 };
  114. function Plugin () {
  115. global.add('save', function (editor) {
  116. Buttons.register(editor);
  117. Commands.register(editor);
  118. });
  119. }
  120. Plugin();
  121. }());