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"]
相关推荐
hongc931 分钟前
element-ui el-table 设置固定列fixed 高度不对
前端·vue.js·elementui
Forfun_tt13 分钟前
xss-labs pass-12
前端·xss
云枫晖29 分钟前
Webpack系列-编译过程
前端·webpack
AskHarries38 分钟前
Toolhub — 一个干净实用的在线工具集合
前端·后端
H尗1 小时前
Vue3响应式系统的精妙设计:深入理解 depsLength 与 trackId 的协同艺术
前端·vue.js
昔人'1 小时前
html`contenteditable`
前端·html
爱宇阳1 小时前
npm 常用标签与使用技巧新手教程
前端·npm·node.js
@大迁世界1 小时前
2025 年该选谁?npm vs yarn vs pnpm
前端·npm·node.js
crary,记忆1 小时前
简介NPM 和 NPX
前端·学习·npm·node.js
wocwin1 小时前
解决qiankun微前端Vue2+Element-ui主应用跳转到Vue3+Element-plus子应用样式冲突问题
vue.js