From c96026b85cb2119718bf83ad33d38103f8a0d91a Mon Sep 17 00:00:00 2001 From: zhangmeng <1334717033@qq.com> Date: Sun, 8 Oct 2023 14:35:42 +0800 Subject: [PATCH] =?UTF-8?q?2023=E5=B9=B410=E6=9C=888=E6=97=A514:35:33?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tools/controller/EditListController.java | 51 +-- .../tools/editors/monaco/MonacoEdit.java | 200 ----------- .../tools/editors/monaco/MonacoHelper.java | 156 --------- .../tools/editors/monaco/MonacoMode.java | 20 -- .../tools/editors/monaco/MonacoThem.java | 27 -- .../tools/editors/monaco/MonacoWebView.java | 188 ++++++++++ src/main/resources/static/editors/index2.html | 329 ++++-------------- 7 files changed, 290 insertions(+), 681 deletions(-) delete mode 100644 src/main/java/com/zhangmeng/tools/editors/monaco/MonacoEdit.java delete mode 100644 src/main/java/com/zhangmeng/tools/editors/monaco/MonacoHelper.java delete mode 100644 src/main/java/com/zhangmeng/tools/editors/monaco/MonacoMode.java delete mode 100644 src/main/java/com/zhangmeng/tools/editors/monaco/MonacoThem.java create mode 100644 src/main/java/com/zhangmeng/tools/editors/monaco/MonacoWebView.java diff --git a/src/main/java/com/zhangmeng/tools/controller/EditListController.java b/src/main/java/com/zhangmeng/tools/controller/EditListController.java index 63e7212..6fadc75 100644 --- a/src/main/java/com/zhangmeng/tools/controller/EditListController.java +++ b/src/main/java/com/zhangmeng/tools/controller/EditListController.java @@ -1,11 +1,16 @@ package com.zhangmeng.tools.controller; import com.zhangmeng.tools.editors.ace.AceEditor; -import com.zhangmeng.tools.editors.monaco.MonacoEdit; +import com.zhangmeng.tools.editors.monaco.*; +import com.zhangmeng.tools.utils.AlertUtils; +import com.zhangmeng.tools.utils.ClipboardUtils; import com.zhangmeng.tools.utils.ImagePath; import com.zhangmeng.tools.utils.ResourcesUtils; +import eu.mihosoft.monacofx.Editor; import eu.mihosoft.monacofx.MonacoFX; import javafx.beans.property.SimpleDoubleProperty; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; @@ -19,21 +24,33 @@ import javafx.scene.control.ListView; import javafx.scene.control.SplitPane; import javafx.scene.image.Image; import javafx.scene.image.ImageView; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.HBox; import javafx.scene.paint.Paint; import javafx.scene.text.Font; +import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; +import javafx.stage.FileChooser; import javafx.stage.Stage; import javafx.util.Callback; import lombok.extern.slf4j.Slf4j; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Arrays; @Slf4j public class EditListController { + @FXML + public AnchorPane parent_root; + private SimpleDoubleProperty width = new SimpleDoubleProperty(0.0); private SimpleDoubleProperty height = new SimpleDoubleProperty(0.0); private AnchorPane root; @@ -306,9 +323,13 @@ public class EditListController { if (!flag){ acJsEditor = new AceEditor(); root = acJsEditor; - WebView webView = acJsEditor.getWebView(); - webView.prefWidthProperty().bind(root.widthProperty()); - webView.prefHeightProperty().bind(root.heightProperty()); + WebView view = acJsEditor.getWebView(); + + AnchorPane.setTopAnchor(view,0.0); + AnchorPane.setBottomAnchor(view,0.0); + AnchorPane.setLeftAnchor(view,0.0); + AnchorPane.setRightAnchor(view,0.0); + }else { root = acJsEditor; } @@ -317,27 +338,10 @@ public class EditListController { private void monacoEdit(boolean flag) { - MonacoFX monacoFX = new MonacoFX(); - // set initial text - monacoFX.getEditor().getDocument().setText( - "#include \n" + - "int main() {\n" + - " // printf() displays the string inside quotation\n" + - " printf(\"Hello, World!\");\n" + - " return 0;\n" + - "}"); - - // use a predefined language like 'c' - monacoFX.getEditor().setCurrentLanguage("c"); - monacoFX.getEditor().setCurrentTheme("vs-dark"); - //默认选择第一个 listView.getSelectionModel().select(1); if (!flag){ - root = new AnchorPane(monacoFX); - WebView webView = monacoFX.getView(); - webView.prefWidthProperty().bind(root.widthProperty()); - webView.prefHeightProperty().bind(root.heightProperty()); + root = new MonacoWebView(); monacoEdit = root; }else { root = monacoEdit; @@ -349,6 +353,7 @@ public class EditListController { private void common_method() { splitPane.getItems().remove(1); splitPane.getItems().add(1, root); + root.widthProperty().addListener((observable, oldValue, newValue) -> { if (newValue != null) { double width = splitPane.getWidth(); @@ -366,10 +371,12 @@ public class EditListController { this.width.addListener((observable, oldValue, newValue) -> { EditListController.this.root.setPrefWidth(newValue.doubleValue() - listView.getWidth()); + log.info("root:=====================>width:" + (newValue.doubleValue() - listView.getWidth())); }); this.height.addListener((observable, oldValue, newValue) -> { EditListController.this.root.setPrefHeight(newValue.doubleValue() - listView.getHeight()); + log.info("root:=====================>:height" + (newValue.doubleValue() - listView.getWidth())); }); } diff --git a/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoEdit.java b/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoEdit.java deleted file mode 100644 index 711bf2e..0000000 --- a/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoEdit.java +++ /dev/null @@ -1,200 +0,0 @@ -package com.zhangmeng.tools.editors.monaco; - -import com.zhangmeng.tools.editors.ace.AceHelper; -import com.zhangmeng.tools.editors.ace.AceMode; -import com.zhangmeng.tools.editors.ace.AceTheme; -import com.zhangmeng.tools.utils.AlertUtils; -import com.zhangmeng.tools.utils.ClipboardUtils; -import javafx.beans.property.SimpleStringProperty; -import javafx.beans.property.StringProperty; -import javafx.concurrent.Worker; -import javafx.scene.input.KeyCode; -import javafx.scene.input.KeyEvent; -import javafx.scene.layout.AnchorPane; -import javafx.scene.web.WebEngine; -import javafx.scene.web.WebView; -import javafx.stage.FileChooser; -import javafx.stage.Stage; -import netscape.javascript.JSObject; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; - - -/** - * @author zhangmeng - * @date 2023年10月7日16:02:39 - * @version 1.0 - */ -public class MonacoEdit extends AnchorPane { - - private final WebView webView = new WebView(); - private WebEngine engine; - private MonacoMode mode = MonacoMode.JAVA; - private JSObject window; - private JSObject editor; - private File file; - - - public WebView getWebView() { - return webView; - } - - public MonacoEdit() { - initialize(); - } - - public MonacoEdit(File file) { - String file_content = null; - try { - file_content = Files.readString(file.toPath(), StandardCharsets.UTF_8); - } catch (IOException e) { - try { - file_content = Files.readString(file.toPath(), Charset.forName("GBK")); - } catch (IOException ex) { - ex.printStackTrace(); - } - } - this.file = file; - setText(file_content); - initialize(); - } - - public MonacoEdit(String text) { - setText(text); - initialize(); - } - - private final StringProperty text = new SimpleStringProperty(""); - - public StringProperty textProperty() { - return text; - } - - public String getText() { - return text.get(); - } - - private void setText(String text) { - this.text.set(text); - } - - /** - * 设置内容 - * - * @param value - */ - private void setMonacoText(String value) { - engine.executeScript("setValue()"); - } - - /** - * 设置模式 - */ - private void setMonacoMode(MonacoMode mode) { - engine.executeScript("setModelLanguage()"); - } - - /** - * 获取选定文本 - * - * @return - */ - public String selectedText() { - // 一下两个方法都可以获取到选中的文本 - // String selectedText = (String) engine.executeScript("window.getSelection().toString()"); // 有些问题 - return (String) editor.call("getSelectedText"); - } - - - /** - * 删除选中的内容 - */ - private void removeSelectText() { - window.call("removeSelectRange"); // 或者:editor.call("remove", "right"); - } - - public void initialize() { - getChildren().addAll(webView); - webView.setContextMenuEnabled(false); - engine = webView.getEngine(); - // 界面加载完成执行的方法 - engine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -> { - if (newState == Worker.State.SUCCEEDED) { - // - window = (JSObject) engine.executeScript("window"); - window.setMember("MonacoEdit", this); -// editor = (JSObject) engine.executeScript("monaco.editor"); - setMonacoText(getText()); - setMonacoMode(mode); - // WebView 整合 Ace.js 后 Ctrl+C 和 Ctrl+X 有问题,重新处理下。 - webView.addEventFilter(KeyEvent.KEY_PRESSED, event -> { - if (event.isControlDown() && event.getCode() == KeyCode.C) { - event.consume(); - ClipboardUtils.setString(selectedText()); - } - if (event.isControlDown() && event.getCode() == KeyCode.X) { - event.consume(); - ClipboardUtils.setString(selectedText()); - removeSelectText(); // 删除选中的内容 - } - - if (event.isControlDown() && event.getCode() == KeyCode.S){//保存 - event.consume(); - - if (file != null){ - try { - FileWriter writer = new FileWriter(file); - writer.write(getText()); - writer.flush(); - writer.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - }else { - // - - } - - AlertUtils.alert_warning("保存成功!"); - } - - if (event.isControlDown() && event.getCode() == KeyCode.O){//保存 - event.consume(); - //打开文件 - Stage stage = new Stage(); - FileChooser dc = new FileChooser(); - dc.setTitle("文件选择"); - dc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("文件类型", "*.java", "*.go", "*.txt", "*.json", "*.sql")); - File file = dc.showOpenDialog(stage); - String file_content = null; - try { - file_content = Files.readString(file.toPath(), StandardCharsets.UTF_8); - } catch (IOException e) { - try { - file_content = Files.readString(file.toPath(), Charset.forName("GBK")); - } catch (IOException ex) { - ex.printStackTrace(); - } - } - this.file = file; - setText(file_content); - setMonacoText(getText()); - } - - }); - } - }); - engine.setJavaScriptEnabled(true); - // message print - engine.getLoadWorker().messageProperty().addListener((observable, oldValue, newValue) -> System.out.printf("engine message : %s%n", newValue)); - // exception print - engine.getLoadWorker().exceptionProperty().addListener((observable, oldValue, newValue) -> newValue.printStackTrace()); - engine.loadContent(MonacoHelper.getHtml(this.mode, MonacoThem.VS)); // 加载html资源 - // engine.load(""); // 同一时间多个WebView访问同一个资源时engine.executeScript();会返回undefined。 - } -} diff --git a/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoHelper.java b/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoHelper.java deleted file mode 100644 index b5ef0e7..0000000 --- a/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoHelper.java +++ /dev/null @@ -1,156 +0,0 @@ -package com.zhangmeng.tools.editors.monaco; - -import com.zhangmeng.tools.editors.ace.AceHelper; -import com.zhangmeng.tools.editors.ace.AceMode; -import com.zhangmeng.tools.editors.ace.AceTheme; - -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.util.Objects; - -public class MonacoHelper { - - private static final String loader_js_path = "/static/editors/monaco-editor/min/vs/loader.js"; - - public static String getHtml(MonacoMode aceMode, MonacoThem aceTheme) { - - String model_js = aceMode.getName(); - String theme_js = aceTheme.getName(); - - try ( - InputStream loader_JS = AceHelper.class.getResourceAsStream(loader_js_path); - ) { - String html = """ - - - - - Monaco Editor Demo - - """; - html += " "; - html += """ - - - - - - - -
- - - - - -
-
- - - - - """; - - return html; - } catch (Exception e) { - throw new RuntimeException(e); - } - } - -} diff --git a/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoMode.java b/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoMode.java deleted file mode 100644 index e2f93e0..0000000 --- a/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoMode.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.zhangmeng.tools.editors.monaco; - -public enum MonacoMode { - - JAVA("java"), - JAVA_SCRIPT("javascript"); - private String name; - - MonacoMode(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoThem.java b/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoThem.java deleted file mode 100644 index e6f6556..0000000 --- a/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoThem.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.zhangmeng.tools.editors.monaco; - - -/** - * vs、vs-dark、hc-black - */ - -public enum MonacoThem { - - VS("vs"), - VS_DARK("va-dark"), - HC_BLACK("hc-black"); - - private String name; - - MonacoThem(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoWebView.java b/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoWebView.java new file mode 100644 index 0000000..77fb560 --- /dev/null +++ b/src/main/java/com/zhangmeng/tools/editors/monaco/MonacoWebView.java @@ -0,0 +1,188 @@ +package com.zhangmeng.tools.editors.monaco; + +import com.zhangmeng.tools.utils.AlertUtils; +import com.zhangmeng.tools.utils.ClipboardUtils; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; +import javafx.scene.layout.AnchorPane; +import javafx.scene.web.WebEngine; +import javafx.scene.web.WebView; +import javafx.stage.FileChooser; +import javafx.stage.Stage; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import netscape.javascript.JSObject; + +public class MonacoWebView extends AnchorPane { + + + public MonacoWebView() { + init(); + } + + private File file; + + public File getFile() { + return file; + } + + public void setFile(File file) { + this.file = file; + } + + public WebView getView() { + return view; + } + + public void setView(WebView view) { + this.view = view; + } + + private WebView view = new WebView(); + + public void init() { + + WebEngine engine = view.getEngine(); + + String url = this.getClass().getResource("/static/editors/index2.html").toExternalForm(); + engine.load(url); + AnchorPane.setTopAnchor(view, 0.0); + AnchorPane.setBottomAnchor(view, 0.0); + AnchorPane.setLeftAnchor(view, 0.0); + AnchorPane.setRightAnchor(view, 0.0); + getChildren().add(view); + + view.addEventFilter(KeyEvent.KEY_PRESSED, event -> { + + if (event.isControlDown() && event.getCode() == KeyCode.C) { + event.consume(); + JSObject editor = (JSObject) engine.executeScript("window"); + //获取所选文本 + String text= editor.call("getSelected").toString(); + ClipboardUtils.setString(text); + } + if (event.isControlDown() && event.getCode() == KeyCode.X) { + event.consume(); + JSObject editor = (JSObject) engine.executeScript("window"); + //获取所选文本 + String text= editor.call("getSelected").toString(); + ClipboardUtils.setString(text); + //删除所选文本 + editor.call("removeSelectText"); // 删除选中的内容 + } + + + if (event.isControlDown() && event.getCode() == KeyCode.S) {//保存 + event.consume(); + AlertUtils.alert_warning("保存成功!"); + } + + if (event.isControlDown() && event.getCode() == KeyCode.O) { + event.consume(); + //打开文件 + Stage stage = new Stage(); + FileChooser dc = new FileChooser(); + dc.setTitle("文件选择"); + dc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("文件类型", "*.java", "*.go", "*.txt", "*.json", "*.sql")); + File file = dc.showOpenDialog(stage); + String file_content = null; + try { + file_content = Files.readString(file.toPath(), StandardCharsets.UTF_8); + } catch (IOException e) { + try { + file_content = Files.readString(file.toPath(), Charset.forName("GBK")); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + this.file = file; + JSObject editor = (JSObject) engine.executeScript("window"); + editor.call("setValue",file_content); + } + + }); + } + + public static String getText() { + return """ + package com.zhangmeng.tools.editors.monaco; + + import com.zhangmeng.tools.utils.AlertUtils; + import javafx.scene.input.KeyCode; + import javafx.scene.input.KeyEvent; + import javafx.scene.layout.AnchorPane; + import javafx.scene.web.WebEngine; + import javafx.scene.web.WebView; + import javafx.stage.FileChooser; + import javafx.stage.Stage; + + import java.io.File; + import java.io.FileWriter; + import java.io.IOException; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.nio.file.Files; + + public class MonacoWebView extends AnchorPane { + \s + private File file; + + private WebView view = new WebView(); + + public void init(){ + \s + WebEngine engine = view.getEngine(); + + String url = this.getClass().getResource("/static/editors/index2.html").toExternalForm(); + engine.load(url); + AnchorPane.setTopAnchor(view,0.0); + AnchorPane.setBottomAnchor(view,0.0); + AnchorPane.setLeftAnchor(view,0.0); + AnchorPane.setRightAnchor(view,0.0); + getChildren().add(view); + + view.addEventFilter(KeyEvent.KEY_PRESSED, event -> { + if (event.isControlDown() && event.getCode() == KeyCode.S){//保存 + event.consume(); + AlertUtils.alert_warning("保存成功!"); + } + + if (event.isControlDown() && event.getCode() == KeyCode.O){ + event.consume(); + //打开文件 + Stage stage = new Stage(); + FileChooser dc = new FileChooser(); + dc.setTitle("文件选择"); + dc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("文件类型", "*.java", "*.go", "*.txt", "*.json", "*.sql")); + File file = dc.showOpenDialog(stage); + String file_content = null; + try { + file_content = Files.readString(file.toPath(), StandardCharsets.UTF_8); + } catch (IOException e) { + try { + file_content = Files.readString(file.toPath(), Charset.forName("GBK")); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + this.file = file; + engine.executeScript("setValue('" + getText() +"')"); + } + + }); + } + \s + public static String getText(){ + return ""\" + ""\"; + } + } + + """; + } +} diff --git a/src/main/resources/static/editors/index2.html b/src/main/resources/static/editors/index2.html index 9fd5bd0..89152d1 100644 --- a/src/main/resources/static/editors/index2.html +++ b/src/main/resources/static/editors/index2.html @@ -3,6 +3,7 @@ Monaco Editor Demo + -
+