From c078f31fd54da8811cfe8f497cd053bd242a184f Mon Sep 17 00:00:00 2001 From: zhangmeng <1334717033@qq.com> Date: Mon, 9 Oct 2023 10:20:20 +0800 Subject: [PATCH] =?UTF-8?q?2023=E5=B9=B410=E6=9C=889=E6=97=A510:19:52?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tools/controller/EditListController.java | 27 ++ .../tools/controller/HomeController.java | 4 + .../editors/codemirr/CodeMirrWebView.java | 234 ++++++++++++++++++ .../zhangmeng/tools/utils/ResourcesUtils.java | 2 + src/main/resources/fxml/editor-list.fxml | 1 + src/main/resources/fxml/home.fxml | 1 + .../resources/static/editors/code-mirror.html | 137 +++++----- 7 files changed, 337 insertions(+), 69 deletions(-) create mode 100644 src/main/java/com/zhangmeng/tools/editors/codemirr/CodeMirrWebView.java diff --git a/src/main/java/com/zhangmeng/tools/controller/EditListController.java b/src/main/java/com/zhangmeng/tools/controller/EditListController.java index 92166d5..b996dd3 100644 --- a/src/main/java/com/zhangmeng/tools/controller/EditListController.java +++ b/src/main/java/com/zhangmeng/tools/controller/EditListController.java @@ -1,6 +1,7 @@ package com.zhangmeng.tools.controller; import com.zhangmeng.tools.editors.ace.AceEditor; +import com.zhangmeng.tools.editors.codemirr.CodeMirrWebView; import com.zhangmeng.tools.editors.monaco.*; import com.zhangmeng.tools.utils.AlertUtils; import com.zhangmeng.tools.utils.ClipboardUtils; @@ -57,6 +58,7 @@ public class EditListController { private AceEditor acJsEditor; private AnchorPane monacoEdit; + private AnchorPane codemirror; public static final String color_cell = "#f4f4f4"; @@ -251,6 +253,13 @@ public class EditListController { } monacoEdit(flag); } + + if (newValue.getIndex() == 2) { + if (codemirror != null){ + flag = true; + } + codemirror(flag); + } } }); } @@ -259,6 +268,7 @@ public class EditListController { return switch (player){ case Ace_JS -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5)); case Monaco_JS -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5)); + case CodeMirror_JS -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5)); }; } @@ -349,6 +359,19 @@ public class EditListController { common_method(); } + private void codemirror(boolean flag) { + + //默认选择第一个 + listView.getSelectionModel().select(2); + if (!flag){ + root = new CodeMirrWebView(); + codemirror = root; + }else { + root = codemirror; + } + common_method(); + } + private void common_method() { splitPane.getItems().remove(1); @@ -618,4 +641,8 @@ public class EditListController { } monacoEdit(flag); } + + public void codemirror_js_menu_item(ActionEvent actionEvent) { + + } } diff --git a/src/main/java/com/zhangmeng/tools/controller/HomeController.java b/src/main/java/com/zhangmeng/tools/controller/HomeController.java index 2c52e99..4ee194a 100644 --- a/src/main/java/com/zhangmeng/tools/controller/HomeController.java +++ b/src/main/java/com/zhangmeng/tools/controller/HomeController.java @@ -641,4 +641,8 @@ public class HomeController implements Serializable { public void monaco_js_menu_item(ActionEvent actionEvent) { js_edit_list(1); } + + public void codemirror_js_menu_item(ActionEvent actionEvent) { + js_edit_list(2); + } } \ No newline at end of file diff --git a/src/main/java/com/zhangmeng/tools/editors/codemirr/CodeMirrWebView.java b/src/main/java/com/zhangmeng/tools/editors/codemirr/CodeMirrWebView.java new file mode 100644 index 0000000..264d306 --- /dev/null +++ b/src/main/java/com/zhangmeng/tools/editors/codemirr/CodeMirrWebView.java @@ -0,0 +1,234 @@ +package com.zhangmeng.tools.editors.codemirr; + +import com.zhangmeng.tools.utils.AlertUtils; +import com.zhangmeng.tools.utils.ClipboardUtils; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.value.ObservableValue; +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 lombok.extern.slf4j.Slf4j; +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月9日09:06:29 + * @version 1.0 + * + */ +@Slf4j +public class CodeMirrWebView extends AnchorPane { + + public SimpleStringProperty text_code = new SimpleStringProperty(""); + + public void setText(String text){ + log.info("text:" + text); + text_code.setValue(text); + } + + private JSObject window; + + public CodeMirrWebView() { + 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/code-mirror.html").toExternalForm(); + + engine.getLoadWorker().stateProperty().addListener( + (ObservableValue ov, Worker.State oldState, Worker.State newState) -> { + if (newState == Worker.State.SUCCEEDED) { + window = (JSObject) engine.executeScript("window"); + window.setMember("text_code",text_code); + } + } + ); + + 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 -> { + log.info("event.getCode:" + event.getCode()); + 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(); + if (file != null){ + try { + FileWriter writer = new FileWriter(file); + writer.write(text_code.getValue()); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + }else { + // + } + AlertUtils.alert_warning("保存成功!"); + } + + if (event.isControlDown() && event.getCode() == KeyCode.DIVIDE) {//注释 + event.consume(); + window.call("commentLine"); + } + + 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/java/com/zhangmeng/tools/utils/ResourcesUtils.java b/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java index edbef4d..6486627 100644 --- a/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java +++ b/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java @@ -363,6 +363,8 @@ public class ResourcesUtils { Ace_JS("ace.js 编辑器", 0), Monaco_JS("monaco.js 编辑器", 1), + CodeMirror_JS("codemirror.js 编辑器", 2), + ; EditorList(String title, int index) { diff --git a/src/main/resources/fxml/editor-list.fxml b/src/main/resources/fxml/editor-list.fxml index 5cf0827..00a21a4 100644 --- a/src/main/resources/fxml/editor-list.fxml +++ b/src/main/resources/fxml/editor-list.fxml @@ -107,6 +107,7 @@ + diff --git a/src/main/resources/fxml/home.fxml b/src/main/resources/fxml/home.fxml index 4f40422..f6ce65d 100644 --- a/src/main/resources/fxml/home.fxml +++ b/src/main/resources/fxml/home.fxml @@ -107,6 +107,7 @@ + diff --git a/src/main/resources/static/editors/code-mirror.html b/src/main/resources/static/editors/code-mirror.html index 10d1cc3..188b1ff 100644 --- a/src/main/resources/static/editors/code-mirror.html +++ b/src/main/resources/static/editors/code-mirror.html @@ -105,7 +105,7 @@