File tree Expand file tree Collapse file tree 2 files changed +40
-16
lines changed
Expand file tree Collapse file tree 2 files changed +40
-16
lines changed Original file line number Diff line number Diff line change @@ -27278,16 +27278,28 @@
2727827278
2727927279 // TODO: the next two functions could probably become a petite-vue component
2728027280 function editorListener(docName) {
27281+ let changes = []; // keep the changes as a list; then pick the last one
27282+ let timer; // we only want one timer, once the last one is done, use the result
27283+ function debounce(fn, delay) {
27284+ return (...args) => {
27285+ clearTimeout(timer);
27286+ timer = setTimeout(() => fn(...args), delay);
27287+ };
27288+ }
27289+
2728127290 return EditorView.updateListener.of((update) => {
2728227291 if (update.docChanged) {
27283- // set the global `doc` to the latest string from the editor
27284- try {
27285- const parsed = JSON.parse(update.state.sliceDoc(0, update.state.doc.length));
27286- this[docName] = parsed;
27287- this.parseError = '';
27288- } catch (err) {
27289- this.parseError = err.message;
27290- } }
27292+ changes.push(update.state.doc.toString());
27293+ debounce((docName) => {
27294+ // set the global `doc` to the latest string from the editor
27295+ try {
27296+ const parsed = JSON.parse(changes[changes.length-1]);
27297+ this[docName] = parsed;
27298+ this.parseError = '';
27299+ } catch (err) {
27300+ this.parseError = err.message;
27301+ } }, 1000).call(this, docName);
27302+ }
2729127303 });
2729227304 }
2729327305 function initEditor(id, content, varName) {
Original file line number Diff line number Diff line change @@ -60,16 +60,28 @@ const jsonLdAtTerms = [
6060
6161// TODO: the next two functions could probably become a petite-vue component
6262function editorListener ( docName ) {
63+ let changes = [ ] ; // keep the changes as a list; then pick the last one
64+ let timer ; // we only want one timer, once the last one is done, use the result
65+ function debounce ( fn , delay ) {
66+ return ( ...args ) => {
67+ clearTimeout ( timer ) ;
68+ timer = setTimeout ( ( ) => fn ( ...args ) , delay ) ;
69+ } ;
70+ }
71+
6372 return EditorView . updateListener . of ( ( update ) => {
6473 if ( update . docChanged ) {
65- // set the global `doc` to the latest string from the editor
66- try {
67- const parsed = JSON . parse ( update . state . sliceDoc ( 0 , update . state . doc . length ) ) ;
68- this [ docName ] = parsed ;
69- this . parseError = '' ;
70- } catch ( err ) {
71- this . parseError = err . message ;
72- } ;
74+ changes . push ( update . state . doc . toString ( ) ) ;
75+ debounce ( ( docName ) => {
76+ // set the global `doc` to the latest string from the editor
77+ try {
78+ const parsed = JSON . parse ( changes [ changes . length - 1 ] ) ;
79+ this [ docName ] = parsed ;
80+ this . parseError = '' ;
81+ } catch ( err ) {
82+ this . parseError = err . message ;
83+ } ;
84+ } , 1000 ) . call ( this , docName ) ;
7385 }
7486 } ) ;
7587}
You can’t perform that action at this time.
0 commit comments