select-link-type-mixins.js 3.0 KB
Newer Older
1
import { getPosition, formatText, linkTypeSpiltStr } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
2 3 4 5 6 7 8 9 10
export default {
  methods: {
    handleSelectLinkType(index) {
      const originalText = this.textContent;
      const queryInfo = this.queryInfo;
      const cursorPosition = getPosition(this.id);
      let typeStr = "";
      switch (index) {
        case 0:
郭维嘉 已提交
11
          typeStr = linkTypeSpiltStr + "card";
璃白.'s avatar
璃白. 已提交
12 13
          break;
        case 1:
郭维嘉 已提交
14
          typeStr = linkTypeSpiltStr + "title";
璃白.'s avatar
璃白. 已提交
15 16
          break;
        case 2:
郭维嘉 已提交
17
          typeStr = linkTypeSpiltStr + "link";
璃白.'s avatar
璃白. 已提交
18
          break;
璃白.'s avatar
璃白. 已提交
19 20 21
        case 3:
          typeStr = linkTypeSpiltStr + "3";
          break;
璃白.'s avatar
璃白. 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        default:
          break;
      }
      const newText =
        originalText.slice(0, cursorPosition) +
        typeStr +
        originalText.slice(cursorPosition);
      this.textContent = newText;
      this.emitText();
      this.showSelectLinkType = false;
      this.$nextTick(() => {
        const textEl = document.getElementById(this.id);
        textEl.setSelectionRange(
          cursorPosition + typeStr.length,
          cursorPosition + typeStr.length
        );
        textEl.focus();
      });
    },
    createSelectLinkTypeDialog(type) {
      const textEl = document.getElementById(this.id);
      if (!textEl) return;
      const height = getComputedStyle(textEl).getPropertyValue("height");
      const width = getComputedStyle(textEl).getPropertyValue("width");
      const scrollTop = textEl.scrollTop;
      const originalText = this.textContent;
      const cursorPoint = getPosition(this.id);
      const selectionInfo = {
        selectionStart: cursorPoint,
        selectionEnd: cursorPoint
      };
      const newText = formatText(
        originalText,
        selectionInfo,
        "<span id='call_position'>",
        "</span>"
      );
      const hideEl = this.createHideEl("clac_position_El_");
      hideEl.style.position = "absolute";
      hideEl.style.width = width;
      hideEl.style.height = height;
      hideEl.style.overflowY = "auto";
      hideEl.style.wordBreak = "break-all";
      hideEl.style.top = "14px";
      hideEl.style.left = 0;
      hideEl.style.whiteSpace = "pre-wrap";
      hideEl.innerHTML = newText;
      this.$nextTick(() => {
        hideEl.scrollTop = scrollTop;
        const pEl = document.getElementById("call_position");
        const frameWidth = textEl.parentNode.offsetWidth;
        this.selectLinkTypePosition = {
          left:
            pEl.offsetLeft < frameWidth * (2 / 3)
              ? pEl.offsetLeft
璃白.'s avatar
璃白. 已提交
77
              : pEl.offsetLeft - 120,
璃白.'s avatar
璃白. 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
          top: pEl.offsetTop - textEl.scrollTop
        };
        textEl.parentNode.removeChild(hideEl);
        this.$nextTick(() => {
          this.showSelectLinkType = true;
          this.$nextTick(() => {
            const list = textEl.parentNode.querySelector(".md_select_user");
            if (list) {
              this.activeLinkTypeIndex = 0;
              list.scrollTo(0, 0);
            }
          });
        });
      });
    }
  }
};