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.9.1 (2021-08-27)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global$2 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var global$1 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  13. var global = 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 = global$1.DOM.getParent(editor.id, 'form');
  31. if (enableWhenDirty(editor) && !editor.isDirty()) {
  32. return;
  33. }
  34. editor.save();
  35. if (hasOnSaveCallback(editor)) {
  36. editor.execCallback('save_onsavecallback', editor);
  37. editor.nodeChanged();
  38. return;
  39. }
  40. if (formObj) {
  41. editor.setDirty(false);
  42. if (!formObj.onsubmit || formObj.onsubmit()) {
  43. if (typeof formObj.submit === 'function') {
  44. formObj.submit();
  45. } else {
  46. displayErrorMessage(editor, 'Error: Form submit field collision.');
  47. }
  48. }
  49. editor.nodeChanged();
  50. } else {
  51. displayErrorMessage(editor, 'Error: No form element found.');
  52. }
  53. };
  54. var cancel = function (editor) {
  55. var h = global.trim(editor.startContent);
  56. if (hasOnCancelCallback(editor)) {
  57. editor.execCallback('save_oncancelcallback', editor);
  58. return;
  59. }
  60. editor.resetContent(h);
  61. };
  62. var register$1 = function (editor) {
  63. editor.addCommand('mceSave', function () {
  64. save(editor);
  65. });
  66. editor.addCommand('mceCancel', function () {
  67. cancel(editor);
  68. });
  69. };
  70. var stateToggle = function (editor) {
  71. return function (api) {
  72. var handler = function () {
  73. api.setDisabled(enableWhenDirty(editor) && !editor.isDirty());
  74. };
  75. handler();
  76. editor.on('NodeChange dirty', handler);
  77. return function () {
  78. return editor.off('NodeChange dirty', handler);
  79. };
  80. };
  81. };
  82. var register = 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$2.add('save', function (editor) {
  105. register(editor);
  106. register$1(editor);
  107. });
  108. }
  109. Plugin();
  110. }());