MarkdownLinkUtil.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import Linker from '../models/Linker';
  2. /**
  3. * Utility for markdown link
  4. */
  5. class MarkdownLinkUtil {
  6. constructor() {
  7. this.getMarkdownLinkOrSelectedText = this.getMarkdownLinkOrSelectedText.bind(this);
  8. this.isInLink = this.isInLink.bind(this);
  9. this.getBeginningAndEndOfTheClosestLinkToCursor = this.getBeginningAndEndOfTheClosestLinkToCursor.bind(this);
  10. this.replaceFocusedMarkdownLinkWithEditor = this.replaceFocusedMarkdownLinkWithEditor.bind(this);
  11. }
  12. // return text as markdown link if the cursor on markdown link else return text as default label of new link.
  13. getMarkdownLinkOrSelectedText(editor) {
  14. if (!this.isInLink(editor)) {
  15. return editor.getDoc().getSelection();
  16. }
  17. const curPos = editor.getCursor();
  18. return Linker.fromLineAndPos(editor.getDoc().getLine(curPos.line), curPos.ch)
  19. }
  20. isInLink(editor) {
  21. const curPos = editor.getCursor();
  22. const { beginningOfLink, endOfLink } = this.getBeginningAndEndOfTheClosestLinkToCursor(editor);
  23. return beginningOfLink >= 0 && endOfLink >= 0 && beginningOfLink <= curPos.ch && curPos.ch <= endOfLink;
  24. }
  25. replaceFocusedMarkdownLinkWithEditor(editor) {
  26. // GW-3023
  27. }
  28. }
  29. // singleton pattern
  30. const instance = new MarkdownLinkUtil();
  31. Object.freeze(instance);
  32. export default instance;