mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-04-06 02:37:14 +00:00
feat: debounced on change
This commit is contained in:
parent
2b7343f831
commit
3d2392dc70
37
core/modules/utils/debounce.js
Normal file
37
core/modules/utils/debounce.js
Normal file
@ -0,0 +1,37 @@
|
||||
/*\
|
||||
title: $:/core/modules/utils/debounce.js
|
||||
type: application/javascript
|
||||
module-type: utils
|
||||
|
||||
Debounce function execution.
|
||||
|
||||
Usage:
|
||||
|
||||
- func: function to be debounced
|
||||
- wait: time to wait before executing the function
|
||||
- immediate: if true, the function is executed immediately
|
||||
|
||||
\*/
|
||||
|
||||
"use strict";
|
||||
|
||||
function debounce(func, wait, immediate) {
|
||||
var timeout;
|
||||
return function() {
|
||||
var context = this, args = arguments;
|
||||
var later = function() {
|
||||
timeout = null;
|
||||
if(!immediate) {
|
||||
func.apply(context, args);
|
||||
}
|
||||
};
|
||||
var callNow = immediate && !timeout;
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(later, wait);
|
||||
if(callNow) {
|
||||
func.apply(context, args);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
exports.debounce = debounce;
|
@ -10,6 +10,7 @@ Text node widget
|
||||
"use strict";
|
||||
|
||||
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
||||
var debounce = require("$:/core/modules/utils/debounce.js").debounce;
|
||||
|
||||
var EditorJSWidget = function(parseTreeNode,options) {
|
||||
this.initialise(parseTreeNode,options);
|
||||
@ -38,20 +39,34 @@ EditorJSWidget.prototype.render = function(parent,nextSibling) {
|
||||
tools: {
|
||||
list: List,
|
||||
header: Header
|
||||
}
|
||||
},
|
||||
onChange: this.debouncedSaveEditorContent.bind(this)
|
||||
});
|
||||
this.editor = editor;
|
||||
|
||||
editor.isReady
|
||||
.then(() => {
|
||||
console.log('Editor.js is ready to work!')
|
||||
console.log('Editor.js is ready to work!', editor.onChange);
|
||||
})
|
||||
.catch((reason) => {
|
||||
console.log('Editor.js initialization failed because of', reason)
|
||||
});
|
||||
|
||||
parent.insertBefore(container,nextSibling);
|
||||
this.domNodes.push(container);
|
||||
};
|
||||
|
||||
EditorJSWidget.prototype.saveEditorContent = function() {
|
||||
this.editor.save().then((outputData) => {
|
||||
console.log('Article data: ', outputData)
|
||||
}).catch((error) => {
|
||||
console.log('Saving failed: ', error)
|
||||
});
|
||||
}
|
||||
|
||||
// Debounced save function for performance
|
||||
EditorJSWidget.prototype.debouncedSaveEditorContent = debounce(EditorJSWidget.prototype.saveEditorContent, 300);
|
||||
|
||||
/*
|
||||
Compute the internal state of the widget
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user