コード例 #1
0
ファイル: index.ts プロジェクト: retrofox/wrap-command
  execute(range?: Range, value?: any): void {
    var hasRange: boolean = !!(range && range instanceof Range);
    var backward: boolean;
    var selection: Selection;

    if (!hasRange) {
      selection = currentSelection(this.document);
      backward = isBackward(selection);
      range = currentRange(selection);
    }

    if (this.queryState(range)) {
      unwrapRange(range, this.nodeName, this.document);
    } else {
      wrapRange(range, this.nodeName, this.document);
    }

    if (!hasRange) {
      // when no Range was passed in then we must reset the document's Selection
      setRange(selection, range, backward);
    }
  }
コード例 #2
0
ファイル: index.ts プロジェクト: webmodules/link-command
  execute(range?: Range, value?: any): void {
    var isSel: boolean = false;
    var backward: boolean;
    var selection: Selection;

    var sel: Selection = currentSelection(this.document);
    if (null != range && !(range instanceof Range)) {
      value = range;
      range = null;
    }
    if (!range) {
      backward = isBackward(sel);
      range = currentRange(this.document);
      isSel = true;
    }
    if (!range) return;
    var command: Command;

    if (this.queryState(range)) {
      command = this.unlink;
      value = null;

      if (range.collapsed) {
        // no selection, so manually traverse up the DOM and find the A node
        var a: Node = closest(range.commonAncestorContainer, 'a', true);
        if (a) {
          debug('selecting A node contents %o', a);
          range.selectNodeContents(a);
        }
      }

    } else {
      command = this.createLink;
      if (!value) value = this.href;

      if (range.collapsed) {
        debug('finding surrounding word at caret for collapsed Range');
        // upon a collapsed Range, we want to find the surrounding "word" that
        // the cursor is touching, and then augment the Range to surround the word
        var wordRange: Range = wordAtCaret(range.endContainer, range.endOffset);
        if (wordRange) {
          debug('found surrounding word: %o', wordRange.toString());
          copyRange(range, wordRange);
        } else {
          debug('no surrounding word, inserting text "Link"');
          var text = this.document.createTextNode('Link');
          insertNode(range, text);
          range.setStart(text, 0);
          range.setEnd(text, 4);
        }
      }

    }

    if (isSel) {
      // if no Range was explicitly passed in, then augment the current Selection
      // in the case that we modified the range (collapsd), so that native
      // browser selection works out properly after the command is executed.
      setRange(sel, range, backward);
      range = null;
    }

    command.execute(range, value);
  }