11package com .mindolph .base .editor ;
22
3- import java .util .concurrent .atomic .AtomicLong ;
4-
5- import com .mindolph .base .event .EventBus ;
6- import org .slf4j .Logger ;
7- import org .slf4j .LoggerFactory ;
8-
93import com .mindolph .base .EditorContext ;
104import com .mindolph .base .container .FixedSplitPane ;
11-
5+ import com . mindolph . base . event . EventBus ;
126import javafx .application .Platform ;
137import javafx .beans .property .BooleanProperty ;
148import javafx .beans .property .ObjectProperty ;
1812import javafx .scene .control .ScrollPane ;
1913import javafx .scene .layout .AnchorPane ;
2014import javafx .util .Callback ;
15+ import org .reactfx .EventSource ;
16+ import org .slf4j .Logger ;
17+ import org .slf4j .LoggerFactory ;
18+
19+ import java .time .Duration ;
20+ import java .util .concurrent .atomic .AtomicLong ;
2121
2222/**
2323 * Editor with preview.
@@ -54,6 +54,8 @@ public abstract class BasePreviewEditor extends BaseCodeAreaEditor implements Ed
5454 private final AtomicLong scrollStartTime = new AtomicLong (0 );
5555 private final static double SCROLL_SPEED_THRESHOLD = 1.75 ; // the threshold of scroll speed between scroll and swipe.
5656
57+ private EventSource <String > refreshEventSource = new EventSource <>();
58+
5759 public BasePreviewEditor (String fxmlResourcePath , EditorContext editorContext , boolean acceptDraggingFiles ) {
5860 super (fxmlResourcePath , editorContext , acceptDraggingFiles );
5961 if (fixedSplitPane == null ) {
@@ -74,6 +76,17 @@ public BasePreviewEditor(String fxmlResourcePath, EditorContext editorContext, b
7476 }
7577 });
7678 codeArea .setParentPane (this );
79+
80+ // reduce preview refreshing for performance.
81+ refreshEventSource .reduceSuccessions ((t1 , t2 ) -> t2 , Duration .ofMillis (250 )).subscribe (text -> {
82+ refreshPreview (text , renderContent -> {
83+ Platform .runLater (() -> {
84+ render (renderContent );
85+ afterRender ();
86+ });
87+ return null ;
88+ });
89+ });
7790 }
7891
7992 protected void enablePageSwipe () {
@@ -191,13 +204,7 @@ public void export() {
191204 @ Override
192205 protected void refresh (String text ) {
193206 if (viewMode != ViewMode .TEXT_ONLY ) {
194- refreshPreview (text , renderContent -> {
195- Platform .runLater (() -> {
196- render (renderContent );
197- afterRender ();
198- });
199- return null ;
200- });
207+ refreshEventSource .push (text );
201208 }
202209 }
203210
0 commit comments