Skip to content

Commit ac23783

Browse files
committed
improvement: refreshing performance for preview mode of Markdown and PlantUML.
1 parent a3a1ea7 commit ac23783

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

code/mindolph-base/src/main/java/com/mindolph/base/editor/BasePreviewEditor.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
package 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-
93
import com.mindolph.base.EditorContext;
104
import com.mindolph.base.container.FixedSplitPane;
11-
5+
import com.mindolph.base.event.EventBus;
126
import javafx.application.Platform;
137
import javafx.beans.property.BooleanProperty;
148
import javafx.beans.property.ObjectProperty;
@@ -18,6 +12,12 @@
1812
import javafx.scene.control.ScrollPane;
1913
import javafx.scene.layout.AnchorPane;
2014
import 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

Comments
 (0)