Skip to content

Commit

Permalink
Merge pull request #812 from DE-Danloc/onSave-callback
Browse files Browse the repository at this point in the history
feature request: #811 onSave callback method
  • Loading branch information
JiHong88 authored Jul 16, 2021
2 parents 5012b83 + cd95949 commit 2e67d7f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,14 @@ editor.onCut = function (e, clipboardData, core) { console.log('onCut', e) }
*/
editor.onDrop = function (e, cleanData, maxCharCount, core) { console.log('onDrop', e) }

// Save event
// Called just after the save was executed.
/**
* contents Editor content
* core: Core object
*/
editor.onSave = function (contents, core) {console.log(contents) };

// Called before the image is uploaded
// If true is returned, the internal upload process runs normally.
// If false is returned, no image upload is performed.
Expand Down
4 changes: 2 additions & 2 deletions dist/suneditor.min.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/lib/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,13 @@ export default class SunEditor {
onCopy: (e: Event, clipboardData: any, core: Core) => boolean;
onCut: (e: Event, clipboardData: any, core: Core) => boolean;

/**
* @description Called just after the save was executed.
* @param contents Editor content
* @param core Core object
*/
onSave: (contents: string, core: Core) => void;

/**
* @description Called just before the inline toolbar is positioned and displayed on the screen.
* @param toolbar Toolbar Element
Expand Down
21 changes: 19 additions & 2 deletions src/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -6987,6 +6987,14 @@ export default function (context, pluginCallButtons, plugins, lang, options, _re
}
},

onSave_wysiwyg: function (content) {
// user event
if (typeof functions.onSave === 'function') {
functions.onSave(content, core);
return;
}
},

onCut_wysiwyg: function (e) {
const clipboardData = util.isIE ? _w.clipboardData : e.clipboardData;

Expand Down Expand Up @@ -7351,6 +7359,13 @@ export default function (context, pluginCallButtons, plugins, lang, options, _re
*/
onChange: null,

/**
* @description Event functions
* @param {String} contents Current contents
* @param {Object} core Core object
*/
onSave: null,

/**
* @description Event functions (drop, paste)
* When false is returned, the default behavior is stopped.
Expand Down Expand Up @@ -7726,10 +7741,12 @@ export default function (context, pluginCallButtons, plugins, lang, options, _re
},

/**
* @description Copying the contents of the editor to the original textarea
* @description Copying the contents of the editor to the original textarea and execute onSave callback
*/
save: function () {
context.element.originElement.value = core.getContents(false);
const contents = core.getContents(false);
context.element.originElement.value = contents
event.onSave_wysiwyg(contents, core);
},

/**
Expand Down

0 comments on commit 2e67d7f

Please sign in to comment.