在线代码编辑器

在线代码编辑器

    • 文章说明
    • 前台核心代码
    • 后台核心代码
    • 效果展示
    • 源码下载

文章说明

采用Java结合vue3设计实现的在线代码编辑功能,支持在线编辑代码、运行代码,同时支持导入文件,支持图片识别,支持复制代码,可将代码导出为图片

暂时还未添加图片代码识别导入的功能,然后目前Linux版本下安装C#的编译器我还没找到好的方案,这个C#代码运行在Linux环境下暂时有点问题

目前对于代码安全方面还未添加校验,只作为简单的学习使用,存在着不小的风险,后续会考虑新开坑,书写更完善的在线编辑器

目前的这个结果展示的编码方面还有一些问题,主要是控制台输入、命令行以及文件本身的编码会有不一致的情况,有些难整,后续考虑采用新方案给这个问题解决一下

前台核心代码

前台主要采用 md-editor-v3 编辑器作为代码编辑区域,虽然没有提示功能,但是展示效果上会好一些,然后运行的时候调用后台接口,采用Java调用cmd命令行来实现代码的运行

其中代码下载为图片采用的是对dom对象进行下载,采用html2canvas来实现,会有一些小bug,不过效果感觉还可以

前台核心代码

<script setup>
import {MdEditor, MdPreview} from 'md-editor-v3';
import 'md-editor-v3/lib/style.css';
import 'md-editor-v3/lib/preview.css';
import {computed, onBeforeMount, reactive, ref, watch} from "vue";
import html2canvas from "html2canvas";
import {confirm,downloadFileToLocalByUrl,exportCommon,getRequest,loading,message,postRequest,prompt
} from "@/util";const data = reactive({language: "",codeText: "",inputText: "",displayText: "",exportDialogVisible: false,exportImgUrl: "",uploadExist: true,
});const languageList = ["Java","Python","Javascript","C","C++","C#",
];const codeText = computed(() => {return "```" + data.language + "\n" + data.codeText + "\n```"
});function exportCodeToImg() {const displayContent = document.getElementsByClassName("display-content")[0];data.exportDialogVisible = true;html2canvas(displayContent, {}).then((canvas) => {data.exportImgUrl = canvas.toDataURL('image/png');});
}function exportCode() {prompt("导出代码文件", "导出代码文件名称", data.language + "_" + new Date().toLocaleString() + ".txt", (value) => {exportCommon(data.codeText, value);});
}function exportCodeToImgConfirm() {prompt("导出代码图片", "导出代码图片名称", data.language + "_" + new Date().toLocaleString() + ".png", (value) => {downloadFileToLocalByUrl(data.exportImgUrl, value);data.exportDialogVisible = false;});
}function reset() {confirm("确认清空吗,包括代码区域、控制台文件输入以及结果展示区域?", () => {data.codeText = "";data.inputText = "";data.displayText = "";});
}function run() {if (!data.language) {message("请先选择编程语言", "warning");return;}const loadingInstance = loading("运行中...");postRequest("/codeRunner/run", null, {language: data.language,codeText: data.codeText,inputText: data.inputText,}).then((res) => {loadingInstance.close();if (res.data.code === 200) {data.displayText = res.data.data;message("代码运行成功", "success");} else if (res.data.code === 500) {message("代码运行失败", "error");}})
}const languageText = {};onBeforeMount(async () => {for (let i = 0; i < languageList.length; i++) {languageText[languageList[i]] = await getLanguageDefaultText(languageList[i]);}
});async function getLanguageDefaultText(language) {const res = await getRequest("/codeRunner/getDefaultText", {language});if (res.data.code === 200) {return res.data.data;} else if (res.data.code === 500) {message(language + "语言默认代码获取失败", "error");}
}watch(() => data.language, (newValue, oldValue) => {if (data.codeText) {languageText[oldValue] = data.codeText;}data.codeText = languageText[data.language];
});const uploadFileRef = ref();function uploadFile(event) {const loadingInstance = loading("导入中...");const file = event.target.files[0];const reader = new FileReader();reader.readAsText(file, 'utf8');data.uploadExist = false;reader.onload = () => {loadingInstance.close();const codeText = reader.result.toString();data.uploadExist = true;if (!codeText) {message("导出文件内容为空", "warning");return;}data.codeText = codeText;}
}function singleImport() {if (!data.language) {message("请先选择编程语言", "warning");return;}if (!data.uploadExist) {return;}uploadFileRef.value.click();
}
</script><template><div class="container"><div class="tool-bar"><div class="row"><el-select v-model="data.language" placeholder="请选择编程语言" style="width: 80%"><template v-for="item in languageList" :key="item"><el-option :label="item" :value="item"/></template></el-select></div><div class="row" style="margin-top: -0.5rem"><el-button type="primary" @click="run">运行</el-button><el-button type="danger" @click="reset">清空</el-button><el-button type="danger" @click="singleImport">导入代码</el-button><input v-if="data.uploadExist" ref="uploadFileRef" accept=".txt,.java,.py,.js,.c,.cpp,.cs" style="display: none"type="file" @change="uploadFile($event)"></div><div class="row" style="margin-top: -0.5rem"><el-button type="primary" @click="exportCode">导出代码</el-button><el-button type="danger" @click="exportCodeToImg">下载为图片</el-button></div></div><div class="content-container"><div class="editor-content"><MdEditor v-model="data.codeText" :preview="false" :toolbars="[]" placeholder="代码输入区域"style="height: 100%; width: 100%" theme="dark"/></div><div class="display-content"><MdPreview v-model="codeText" :show-code-row-number="false" style="height: 100%; width: 100%"/></div><div class="input-content"><MdEditor v-model="data.inputText" :preview="false" :toolbars="[]" placeholder="控制台文件输入区域"style="height: 100%; width: 100%" theme="dark"/></div><div class="result-content"><MdEditor v-model="data.displayText" :preview="false" :toolbars="[]" placeholder="结果展示区域" read-onlystyle="height: 100%; width: 100%" theme="dark"/></div></div></div><el-dialog v-model="data.exportDialogVisible" title="代码下载为图片" width="90%"><img :src="data.exportImgUrl" alt="" style="width: 100%; height: auto"/><template #footer><el-button type="primary" @click="data.exportDialogVisible = false">取消</el-button><el-button type="danger" @click="exportCodeToImgConfirm">确认</el-button></template></el-dialog>
</template><style lang="scss">
* {padding: 0;margin: 0;box-sizing: border-box;
}.container {width: 100vw;height: 100vh;display: flex;flex-direction: column;.tool-bar {height: 9rem;background-color: #282c34;border-bottom: 0.1rem solid gray;display: flex;flex-direction: column;align-items: center;justify-content: center;user-select: none;.row {width: 100%;flex: 1;display: flex;align-items: center;justify-content: center;}}.content-container {flex: 1;overflow: auto;display: flex;flex-direction: column;&::-webkit-scrollbar {width: 0;height: 0;}.md-editor-footer {display: none;}.cm-scroller {background-color: #282c34;}.editor-content {height: fit-content;border-bottom: 0.1rem solid gray;}.display-content {height: fit-content;border-bottom: 0.1rem solid gray;.md-editor-preview-wrapper {padding: 0;}.md-editor-code {margin: 0;}.md-editor-code-head {border-radius: 0 !important;}code {border-radius: 0 !important;white-space: pre-wrap;}}.input-content {flex: 1;background-color: #282c34;border-bottom: 0.1rem solid gray;}.result-content {flex: 1;background-color: #282c34;}}
}
</style>

后台核心代码

后台采用命令行运行代码,在Java、C、C++、C#,代码运行时会需要多步骤进行,目前采用的都是将代码写入到指定文件,然后采用指定命令运行绝对路径的文件,限制较大,后续考虑运行项目的形式,可以更加方便的解决文件编码问题以及代码运行问题

Java调用cmd运行代码工具类

package com.boot.util;import lombok.extern.slf4j.Slf4j;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;import static com.boot.util.DefaultConfig.*;
import static com.boot.util.LanguageType.*;/*** @author bbyh* @date 2023/2/27 0027 15:10*/
@Slf4j
public class ExecUtil {public static String exec(String language, String codeText, String inputText) throws Exception {File codeTextInputFile = new File(LANGUAGE_INPUT_FILE_MAP.get(language));if (codeTextInputFile.exists()) {try (FileOutputStream outputStream = new FileOutputStream(codeTextInputFile)) {outputStream.write(codeText.getBytes(StandardCharsets.UTF_8));}}File inputTextInputFile = new File(INPUT_TEXT_PUT_FILE);if (inputTextInputFile.exists()) {try (FileOutputStream outputStream = new FileOutputStream(inputTextInputFile)) {outputStream.write(inputText.getBytes(StandardCharsets.UTF_8));}}if (judgeLinux()) {int exeCode = execLinux(language);logCodeTextRunLog(language, codeText, inputText, exeCode);} else if (judgeWindows()) {int exeCode = execWindows(language);logCodeTextRunLog(language, codeText, inputText, exeCode);}String[] outputFileNames = {ERROR_OUTPUT_FILE, OUTPUT_FILE};for (String outputFileName : outputFileNames) {File outputFile = new File(outputFileName);if (outputFile.exists()) {try (FileInputStream inputStream = new FileInputStream(outputFile)) {byte[] buf = new byte[1024 * 1024];int read = inputStream.read(buf);if (read >= 0) {String result = new String(buf, 0, read);String encoding = getEncoding(result);if (!"".equals(encoding)) {return new String(buf, 0, read, encoding);}}}}}return "";}private static void logCodeTextRunLog(String language, String codeText, String inputText, int exeCode) {log.info("运行时间:{}", new Date());log.info("编程语言:{}", language);log.info("运行代码:{}", codeText);log.info("运行输入:{}", inputText);log.info("运行结果:{}", exeCode);}private static int execLinux(String language) throws Exception {switch (language) {case JAVA:if (execLinuxCommand("javac " + JAVA_INPUT_FILE + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE) == 0) {int start = JAVA_INPUT_FILE.lastIndexOf("/") + 1;int end = JAVA_INPUT_FILE.lastIndexOf(".");String javaClassFileName = JAVA_INPUT_FILE.substring(start, end);return execLinuxCommand("java -classpath " + LINUX_PREFIX + " " + javaClassFileName + " < " + INPUT_TEXT_PUT_FILE + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE);}return 0;case PYTHON:return execLinuxCommand("python " + PYTHON_INPUT_FILE + " < " + INPUT_TEXT_PUT_FILE + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE);case JAVASCRIPT:return execLinuxCommand("node " + JAVASCRIPT_INPUT_FILE + " < " + INPUT_TEXT_PUT_FILE + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE);case C:if (execLinuxCommand("gcc " + C_INPUT_FILE + " -o " + LINUX_PREFIX + "a.exe > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE) == 0) {return execLinuxCommand(LINUX_PREFIX + "a.exe < " + INPUT_TEXT_PUT_FILE + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE);}return 0;case C_PLUS_PLUS:if (execLinuxCommand("g++ " + C_PLUS_PLUS_INPUT_FILE + " -o " + LINUX_PREFIX + "a.exe > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE) == 0) {return execLinuxCommand(LINUX_PREFIX + "a.exe < " + INPUT_TEXT_PUT_FILE + "> " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE);}return 0;case C_SHARP:if (execLinuxCommand("csc /out:" + LINUX_PREFIX.replaceAll("/", "\\\\") + "a.exe "+ C_SHARP_INPUT_FILE.replaceAll("/", "\\\\") + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE) == 0) {return execLinuxCommand(LINUX_PREFIX + "a.exe < " + INPUT_TEXT_PUT_FILE + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE);}return 0;}return 0;}private static int execLinuxCommand(String command) throws Exception {ArrayList<String> commandList = new ArrayList<>();commandList.add("/bin/sh");commandList.add("-c");commandList.add(command);Process exec = Runtime.getRuntime().exec(commandList.toArray(new String[0]));return exec.waitFor();}private static int execWindows(String language) throws Exception {switch (language) {case JAVA:if (execWindowsCommand("javac " + JAVA_INPUT_FILE + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE) == 0) {int start = JAVA_INPUT_FILE.lastIndexOf("/") + 1;int end = JAVA_INPUT_FILE.lastIndexOf(".");String javaClassFileName = JAVA_INPUT_FILE.substring(start, end);return execWindowsCommand("java -classpath " + WINDOWS_PREFIX + " " + javaClassFileName + " < " + INPUT_TEXT_PUT_FILE + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE);}break;case PYTHON:return execWindowsCommand("python " + PYTHON_INPUT_FILE + " < " + INPUT_TEXT_PUT_FILE + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE);case JAVASCRIPT:return execWindowsCommand("node " + JAVASCRIPT_INPUT_FILE + " < " + INPUT_TEXT_PUT_FILE + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE);case C:if (execWindowsCommand("gcc " + C_INPUT_FILE + " -o " + WINDOWS_PREFIX + "a.exe > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE) == 0) {return execWindowsCommand(WINDOWS_PREFIX + "a.exe < " + INPUT_TEXT_PUT_FILE + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE);}return 0;case C_PLUS_PLUS:if (execWindowsCommand("g++ " + C_PLUS_PLUS_INPUT_FILE + " -o " + WINDOWS_PREFIX + "a.exe > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE) == 0) {return execWindowsCommand(WINDOWS_PREFIX + "a.exe < " + INPUT_TEXT_PUT_FILE + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE);}return 0;case C_SHARP:if (execWindowsCommand("csc /out:" + WINDOWS_PREFIX.replaceAll("/", "\\\\") + "a.exe "+ C_SHARP_INPUT_FILE.replaceAll("/", "\\\\") + " > " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE) == 0) {return execWindowsCommand(WINDOWS_PREFIX + "a.exe < " + INPUT_TEXT_PUT_FILE + "> " + OUTPUT_FILE + " 2> " + ERROR_OUTPUT_FILE);}return 0;}return 0;}private static int execWindowsCommand(String command) throws Exception {Process exec = Runtime.getRuntime().exec("cmd /c " + command);return exec.waitFor();}public static Boolean judgeWindows() {return System.getProperty("os.name").toLowerCase().contains("windows");}public static Boolean judgeLinux() {return System.getProperty("os.name").toLowerCase().contains("linux");}public static String getEncoding(String str) throws Exception {String[] encodes = {"UTF-8", "GBK"};for (String encode : encodes) {if (str.equals(new String(str.getBytes(encode), encode))) {return encode;}}return "";}}

效果展示

运行Java代码
在这里插入图片描述

运行Python代码
在这里插入图片描述

运行JavaScript代码
在这里插入图片描述

运行C语言代码
在这里插入图片描述

运行C++代码
在这里插入图片描述

运行C#代码
在这里插入图片描述

将代码下载为图片
在这里插入图片描述

源码下载

在线代码编辑器

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/55410.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【mod分享】侠盗猎魔2冬日mod,贴图高清化,增加下雪场景,支持光追,并且增加红色霓虹灯

今天小编为大家带来一个新的游戏mod&#xff0c;这次mod主要是修改了游戏《侠盗猎魔2》&#xff0c;我给游戏增加了下雪的场景&#xff0c;并且增加了红色的霓虹灯&#xff0c;整体让游戏沉浸在一种诡异的圣诞气氛中。并且我还提高了游戏材质的分辨率。更多细节需要玩家自己探索…

【STM32开发笔记】移植AI框架TensorFlow到STM32单片机【下篇】

【STM32开发笔记】移植AI框架TensorFlow到STM32单片机【下篇】 一、上篇回顾二、项目准备2.1 准备模板项目2.2 支持计时功能2.3 配置UART4引脚2.4 支持printf重定向到UART42.5 支持printf输出浮点数2.6 支持printf不带\r的换行2.7 支持ccache编译缓存 三、TFLM集成3.1 添加tfli…

设计模式-策略模式-200

优点&#xff1a;用来消除 if-else、switch 等多重判断的代码&#xff0c;消除 if-else、switch 多重判断 可以有效应对代码的复杂性。 缺点&#xff1a;会增加类的数量&#xff0c;有的时候没必要为了消除几个if-else而增加很多类&#xff0c;尤其是那些类型又长又臭的 原始代…

小鱼ROS2 g++编译报错

把humble换成jazzy &#xff0c;起初报错 /opt/ros/jazzy/include/rcl_interfaces/rcl_interfaces/srv/detail/list_parameters__struct.hpp:267:10: fatal error: service_msgs/msg/detail/service_event_info__struct.hpp: 没有那个文件或目录 267 | #include "servi…

基于单片机远程家电控制系统设计

本设计基于单片机的远程家电控制系统&#xff0c;以STC89C52单片机为核心&#xff0c;通过液晶LCD1602实时显示并控制&#xff0c;利用ESP8266WiFi模块实现本地与云平台的连接&#xff0c;最终实现远程对于灯光&#xff0c;热水器等家电的开关控制。同时&#xff0c;系统设有防…

HTB:Oopsie[WriteUP]

目录 连接至HTB服务器并开启靶机 1.With what kind of tool can intercept web traffic? 2.What is the path to the directory on the webserver that returns a login page? 3.What can be modified in Firefox to get access to the upload page? 4.What is the acc…

html+css+js实现step进度条效果

实现效果 代码实现 HTML部分 <div class"box"><ul class"step"><li class"circle actives ">1</li><li class"circle">2</li><li class"circle">3</li><li class&quo…

【设计模式-模板】

定义 模板方法模式是一种行为设计模式&#xff0c;它在一个方法中定义了一个算法的骨架&#xff0c;并将一些步骤延迟到子类中实现。通过这种方式&#xff0c;模板方法允许子类在不改变算法结构的情况下重新定义算法中的某些特定步骤。 UML图 组成角色 AbstractClass&#x…

python编程开发“人机猜拳”游戏

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;开发者-曼亿点 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 曼亿点 原创 &#x1f468;‍&#x1f4bb; 收录于专栏&#xff1a…

回归预测 | Matlab基于SABO-SVR减法平均算法优化支持向量机的数据多输入单输出回归预测

回归预测 | Matlab基于SABO-SVR减法平均算法优化支持向量机的数据多输入单输出回归预测 目录 回归预测 | Matlab基于SABO-SVR减法平均算法优化支持向量机的数据多输入单输出回归预测预测效果基本描述程序设计参考资料 预测效果 基本描述 1.Matlab基于SABO-SVR减法平均算法优化…

如何创建一个docker,给它命名,且下次重新打开它

1.创建一个新的docker并同时命名 docker run -it --name one ubuntu:18.04 /bin/bash 这时候我们已经创建了一个docker,并且命名为"one" 2.关闭当前docker exit 3.这时docker已经终止了&#xff0c;我们需要使用它要重新启动 docker start one 4.现在可以重新打…

哈希表(HashMap、HashSet)

文章目录 一、 什么是哈希表二、 哈希冲突2.1 为什么会出现冲突2.2 如何避免出现冲突2.3 出现冲突如何解决 三、模拟实现哈希桶/开散列&#xff08;整型数据&#xff09;3.1 结构3.2 插入元素3.3 获取元素 四、模拟实现哈希桶/开散列&#xff08;泛型&#xff09;4.1 结构4.2 插…

QSqlDatabase在多线程中的使用

Qt中多线程使用数据库_qt数据库管理类支持多数据库,多线程-CSDN博客 1. 代码&#xff1a; #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QPushButton> #include <QSqlDatabase> #include <QSqlQuery> #include <QSqlError>…

Python编码系列—Python备忘录模式:掌握对象状态保存与恢复技术

&#x1f31f;&#x1f31f; 欢迎来到我的技术小筑&#xff0c;一个专为技术探索者打造的交流空间。在这里&#xff0c;我们不仅分享代码的智慧&#xff0c;还探讨技术的深度与广度。无论您是资深开发者还是技术新手&#xff0c;这里都有一片属于您的天空。让我们在知识的海洋中…

【JavaSE】抽象类

1. 抽象类概念 在面向对象的概念中&#xff0c;所有的对象都是通过类来描绘的&#xff0c;但是反过来&#xff0c;并不是所有的类都是用来描绘对象的&#xff0c;如果一个类中没有包含足够的信息来描绘一个具体的对象&#xff0c;这样的类就是抽象类。 在打印图形例子中, 我们…

婚恋交友小程序的设计思路与用户体验优化

在数字化时代&#xff0c;婚恋小程序作为一种新兴的婚恋交友平台&#xff0c;正逐渐成为单身人士寻找伴侣的重要工具。一个优秀的婚恋小程序不仅要有创新的设计思路&#xff0c;还要注重用户体验的优化。编辑h17711347205以下是婚恋小程序的设计思路与用户体验优化的详细阐述&a…

服务器数据恢复—存储映射到服务器上的卷无法挂载的数据恢复案例

服务器存储数据恢复环境&故障&#xff1a; 一台存储上有一组由16块FC硬盘组建了一组raid。存储前面板上的对应10号和13号硬盘的故障灯亮起&#xff0c;存储映射到redhat linux操作系统服务器上的卷挂载不上&#xff0c;业务中断。 服务器存储数据恢复过程&#xff1a; 1、…

【AIGC】ChatGPT提示词解析:如何打造个人IP、CSDN爆款技术文案与高效教案设计

博客主页&#xff1a; [小ᶻZ࿆] 本文专栏: AIGC | ChatGPT 文章目录 &#x1f4af;前言&#x1f4af;打造个人IP爆款文案提示词使用方法 &#x1f4af;CSDN爆款技术文案提示词使用方法 &#x1f4af;高效教案设计提示词使用方法 &#x1f4af;小结 &#x1f4af;前言 在这…

迎国庆,开源完全免费工作流引擎AntFlow 0.9最强版本发布,支持tidb,提升易用性and more...

AntFlow是一款前端仿钉钉的企业级工作流引擎。后端既可嵌入到现有业务系统&#xff0c;也可以做为独立的流程引擎中台部署&#xff08;SAAS模式&#xff09;。嵌入业务系统模式已经在笔者所在企业使用多年&#xff0c;功能丰富&#xff0c;能适多种国产办公场景&#xff1b;简单…

从密码学看盲拍合约:智能合约的隐私与安全新革命!

文章目录 前言一、什么是盲拍合约&#xff1f;二、盲拍合约的优势1.时间压力的缓解2.绑定与秘密的挑战 三、盲拍合约的工作原理1.提交盲出价2.披露出价3.结束拍卖4.退款机制 四、代码示例总结 前言 随着区块链技术的发展&#xff0c;智能合约在各种场景中的应用越来越广泛。盲…