plugin.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * TinyMCE version 6.4.2 (2023-04-26)
  3. */
  4. (function () {
  5. 'use strict';
  6. var global$2 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  7. const isSimpleType = type => value => typeof value === type;
  8. const isFunction = isSimpleType('function');
  9. var global$1 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  10. var global = tinymce.util.Tools.resolve('tinymce.util.Tools');
  11. const option = name => editor => editor.options.get(name);
  12. const register$2 = editor => {
  13. const registerOption = editor.options.register;
  14. registerOption('save_enablewhendirty', {
  15. processor: 'boolean',
  16. default: true
  17. });
  18. registerOption('save_onsavecallback', { processor: 'function' });
  19. registerOption('save_oncancelcallback', { processor: 'function' });
  20. };
  21. const enableWhenDirty = option('save_enablewhendirty');
  22. const getOnSaveCallback = option('save_onsavecallback');
  23. const getOnCancelCallback = option('save_oncancelcallback');
  24. const displayErrorMessage = (editor, message) => {
  25. editor.notificationManager.open({
  26. text: message,
  27. type: 'error'
  28. });
  29. };
  30. const save = editor => {
  31. const formObj = global$1.DOM.getParent(editor.id, 'form');
  32. if (enableWhenDirty(editor) && !editor.isDirty()) {
  33. return;
  34. }
  35. editor.save();
  36. const onSaveCallback = getOnSaveCallback(editor);
  37. if (isFunction(onSaveCallback)) {
  38. onSaveCallback.call(editor, editor);
  39. editor.nodeChanged();
  40. return;
  41. }
  42. if (formObj) {
  43. editor.setDirty(false);
  44. if (!formObj.onsubmit || formObj.onsubmit()) {
  45. if (typeof formObj.submit === 'function') {
  46. formObj.submit();
  47. } else {
  48. displayErrorMessage(editor, 'Error: Form submit field collision.');
  49. }
  50. }
  51. editor.nodeChanged();
  52. } else {
  53. displayErrorMessage(editor, 'Error: No form element found.');
  54. }
  55. };
  56. const cancel = editor => {
  57. const h = global.trim(editor.startContent);
  58. const onCancelCallback = getOnCancelCallback(editor);
  59. if (isFunction(onCancelCallback)) {
  60. onCancelCallback.call(editor, editor);
  61. return;
  62. }
  63. editor.resetContent(h);
  64. };
  65. const register$1 = editor => {
  66. editor.addCommand('mceSave', () => {
  67. save(editor);
  68. });
  69. editor.addCommand('mceCancel', () => {
  70. cancel(editor);
  71. });
  72. };
  73. const stateToggle = editor => api => {
  74. const handler = () => {
  75. api.setEnabled(!enableWhenDirty(editor) || editor.isDirty());
  76. };
  77. handler();
  78. editor.on('NodeChange dirty', handler);
  79. return () => editor.off('NodeChange dirty', handler);
  80. };
  81. const register = editor => {
  82. editor.ui.registry.addButton('save', {
  83. icon: 'save',
  84. tooltip: 'Save',
  85. enabled: false,
  86. onAction: () => editor.execCommand('mceSave'),
  87. onSetup: stateToggle(editor)
  88. });
  89. editor.ui.registry.addButton('cancel', {
  90. icon: 'cancel',
  91. tooltip: 'Cancel',
  92. enabled: false,
  93. onAction: () => editor.execCommand('mceCancel'),
  94. onSetup: stateToggle(editor)
  95. });
  96. editor.addShortcut('Meta+S', '', 'mceSave');
  97. };
  98. var Plugin = () => {
  99. global$2.add('save', editor => {
  100. register$2(editor);
  101. register(editor);
  102. register$1(editor);
  103. });
  104. };
  105. Plugin();
  106. })();