vue项目引入marvinJS

安装

官方没有提供NPM,只能通过iframe的方式引入,id、style、class根据实际情况自行修改

拷贝官方源文件到public目录

优点:调试方便,无需配置proxy

缺点:多系统版本升级易出错

ini 复制代码
 <iframe
    :id="id"
    src="/mjs/editorws.html"
    :style="getStyle"
    class="iframe-editor"
  ></iframe>

引用远程服务(推荐)

优点:多系统版本升级方便

缺点:无法本地调试

引用远程服务需要配置proxy(涉及到跨域问题)

javascript 复制代码
proxy: {
    // 将 /mjs开头的请求代理到后端
    "/mjs": {
      target: "http://xxx.com", // 后端地址
      changeOrigin: true, // 修改 Origin 头为目标地址(针对跨域)
      rewrite: (path) => path.replace(/^/mjs/, ""), // 路径重写(可选)
    },

    "/rest-v1": {
      target: "http://xxx.com", // 后端地址
      changeOrigin: true, // 修改 Origin 头为目标地址(针对跨域)
    },
  },

使用

修改根目录index.html

引入脚本:

xml 复制代码
<script src="/mjs/gui/lib/promise-1.0.0.min.js"></script>
<script src="/mjs/js/marvinjslauncher.js"></script>

获取编辑器对象

javascript 复制代码
let marvinSketcherInstance;
window.MarvinJSUtil.getEditor(`#${props.id}`).then(
    (sketcherInstance) => {
      marvinSketcherInstance = sketcherInstance;
      //设置编辑器模式
      marvinSketcherInstance.setDisplaySettings({
        carbonLabelVisible: false,
        toolbars: props.toolbars, // standard || search
        implicitHydrogen: "HETERO",
        // 'atomIndicesVisible' : true
      });
    },
    function (error) {
      console.log(error);
    }
  );

获取Structure

javascript 复制代码
const exportStructure = () => {
  marvinSketcherInstance.exportStructure("smiles").then(
    (source) => {
      emits("exportStructure", source);
    },
    ({ message }) => {
      ElMessage.error(t("editor.error.msg"));
    }
  );
};

导入Structure

javascript 复制代码
const importStructure = (structure) => {
  marvinSketcherInstance
    .importStructure("smiles", structure)
    .catch(({ message }) => {
      ElMessage.error(t("editor.error.msg"));
    });
};

导出图片(mrv、mol)

php 复制代码
const getImageByMrv = () => {
  imageSrc.value = marvinSketcherInstance.ImageExporter.mrvToDataUrl(
    mrvFileContent.value,
    "Image/PNG",
    {
      carbonLabelVisible: false,
      atomMapsVisible: true,
      chiralFlagVisible: true,
      valenceErrorVisible: true,
      lonePairsVisible: false,
      implicitHydrogen: "TERMINAL_AND_HETERO",
      displayMode: "WIREFRAME",
      backgroundColor: "#FFF",
      zoomMode: "autoshrink",
      width: 600,
      height: 282,
    }
  );
};

监听change事件

scss 复制代码
marvinSketcherInstance.on("molchange", () => {
    exportStructure();
});

WebServices接口使用

mrv格式转芳香环

rest-v1/util/calculate/molExport

php 复制代码
/**
 * mrv格式转换芳香环
 * @param {*} structure
 * @returns
 */
export const mrvFormatAromatizeService = (structure) => {
  return axios.post(import.meta.env.VITE_MRV_FORMAT_URL, {
    structure,
    parameters: "mrv",
    filterChain: [
      {
        filter: "standardizer",
        parameters: { standardizerDefinition: "aromatize" },
      },
    ],
  });
};

mrv转smiles(格式之间的转换都可以使用)

javascript 复制代码
export const mrvFormatSmartsService = (structure) => {
  return axios.post(import.meta.env.VITE_MRV_FORMAT_URL, {
    structure,
    parameters: "smiles",
    inputFormat: "mrv",
  });
};

export、import支持的格式

exportFormats

css 复制代码
[    "mrv",    "cxon",    "mol",    "rxn",    "rgf",    "rdf",    "smiles",    "cxsmiles",    "smarts",    "cxsmarts",    "inchi",    "inchikey",    "name",    "cml",    "mol:V3",    "rxn:V3",    "rgf:V3",    "sdf",    "sdf:ctab",    "csmol",    "cssdf",    "pdb"]

importFormats

css 复制代码
[    "mrv",    "cxon",    "mol",    "rxn",    "rgf",    "rdf",    "smiles",    "cxsmiles",    "smarts",    "cxsmarts",    "inchi",    "d2s",    "name",    "cml",    "mol:V3",    "rxn:V3",    "rgf:V3",    "sdf",    "csmol",    "cssdf",    "cdxml",    "pdb"]
相关推荐
东风破_25 分钟前
Docker 基础:为什么需要容器?
前端·后端·nginx
东风破_29 分钟前
Docker 实战:使用 Nginx 作为容器入口代理 Node 服务
前端·后端·nginx
你别说话了1 小时前
Vue项目解决跨域
前端·javascript·vue.js
唐青枫2 小时前
http-server:别再双击 index.html,一条命令把目录变成网站
前端·javascript
网安蟹佬霸2 小时前
OSINT开源情报收集实战:从信息搜集到资产测绘(2026最新万字保姆级指南)
前端·网络·安全·web安全·网络安全·开源
a1117763 小时前
3D历史建筑展览馆 THreeJS 开源
前端·开源
单线程_013 小时前
【源码阅读】Vue3 Tokenizer 词法分析器完整状态机流转深度梳理(3.4+ 新版架构)
前端
PBitW3 小时前
PM 丢来 3 个 Excel、12 个功能、4 种情形?用 TRAE Work 30 分钟整理成前端开发文档
前端·trae
学习星球5 小时前
Solid.js 实战:拆解官方 RealWorld 项目
开发语言·javascript·vue.js
风骏时光牛马5 小时前
AIAgent高可用架构:弹性容错与故障自愈的落地实践
前端