百度编辑器内,对选中的内容进行替换

<template>
  <el-card>
    <el-row>
      <el-col>
        <vue-ueditor-wrap v-model="content" @ready="ready" :config="config" ref="editor" style="width:100%;"></vue-ueditor-wrap>
        <button @click="handleReplace">替换</button>
      </el-col>
    </el-row>
  </el-card>
</template>

<script>
// 用来记录选中的内容
let selection = "";
import VueUeditorWrap from "vue-ueditor-wrap"; // ES6 Module

export default {
  name: "",
  components: {
    VueUeditorWrap,
  },
  data() {
    return {
      content: "",
      config: {
        initialStyle: "p{font-size:18px;}",
        elementPathEnabled: false,
        // removeEmptyline: true,
        // clearFontSize: true,
        // clearFontFamily: true,
        // 相对路径
        UEDITOR_HOME_URL: "/UEditor/",
        // 编辑器不自动被内容撑高
        autoHeightEnabled: true,
        // 初始容器高度// 初始容器宽度
        initialFrameHeight: "100%",
        initialFrameWidth: "100%",
        toolbars: [
          // [
          //   // "fullscreen",
          //   "source",
          //   "|",
          //   "undo",
          //   "redo",
          //   "|",
          //   "bold",
          //   "italic",
          //   "underline",
          //   "fontborder",
          //   "strikethrough",
          //   "superscript",
          //   "subscript",
          //   "removeformat",
          //   "formatmatch",
          //   "blockquote",
          //   "pasteplain",
          //   "|",
          //   "forecolor",
          //   "backcolor",
          //   "insertorderedlist",
          //   "insertunorderedlist",
          //   "selectall",
          //   "cleardoc",
          //   "|",
          //   "customstyle",
          //   "paragraph",
          //   "fontfamily",
          //   "fontsize",
          //   "|",
          //   "justifyleft",
          //   "justifycenter",
          //   "justifyright",
          //   "justifyjustify",
          //   "|",
          //   "imagenone",
          //   "imageleft",
          //   "imageright",
          //   "imagecenter",
          //   "|",
          //   "simpleupload",
          //   "insertimage",
          //   "|",
          //   "horizontal",
          //   "date",
          //   "time"
          // ]
        ],
        contextMenu: [],
        // 不显示字数
        wordCount: true,
        // 上传图片路径
        serverUrl: "http://35.201.165.105:8000/controller.php",
        // 自动保存
        enableAutoSave: false,
        // focus: true,
        zIndex: 99,
      },
      selection: "",
      selectionDom: "",
      editor: {},
    };
  },
  methods: {
    ready(instance) {
      this.editor = instance;
      this.$nextTick(() => {
        // 监听选取变化
        this.editor.addListener("selectionchange", this.handleSelectionChange);
      });
    },
    handleSelectionChange(type, e) {
      if (this.editor.selection.getText()) {
        this.selection = this.editor.selection.getText();
        selection = this.selection;

        this.selectionDom = this.editor.document.getSelection();
      } else {
        this.selection = "";
      }
    },
    handleReplace() {
      this.selectionDom.deleteFromDocument();
      this.editor.execCommand(
        "inserthtml",
        `<span style="color:#fe6f63;">这里是替换后的内容</span>`
      );
    },
  },
};
</script>
<style lang="scss">
</style>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值