snippets.js 3.01 KB
Newer Older
1 2 3 4 5
/**
 * Scripting for handling custom code snippets
 */

/**
6
 * Shows the requested snippet, and stores the current state in visibleSnippet.
7
 */
8
function showSnippet(name, visibleSnippet) {
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  if (visibleSnippet == name) return;
  if (visibleSnippet != null) {
    var shown = document.getElementById(visibleSnippet);
    var attribute = document.createAttribute('hidden');
    if (shown != null) {
      shown.setAttributeNode(attribute);
    }
    var button = document.getElementById(visibleSnippet + 'Button');
    if (button != null) {
      button.removeAttribute('selected');
    }
  }
  if (name == null || name == '') {
    visibleSnippet = null;
    return;
  }
  var newlyVisible = document.getElementById(name);
  if (newlyVisible != null) {
    visibleSnippet = name;
    newlyVisible.removeAttribute('hidden');
  } else {
    visibleSnippet = null;
  }
  var button = document.getElementById(name + 'Button');
  var selectedAttribute = document.createAttribute('selected');
  if (button != null) {
    button.setAttributeNode(selectedAttribute);
  }
37
  return visibleSnippet;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
}

// Finds a sibling to given element with the given id.
function findSiblingWithId(element, id) {
  var siblings = element.parentNode.children;
  var siblingWithId = null;
  for (var i = siblings.length; i--;) {
    if (siblings[i] == element) continue;
    if (siblings[i].id == id) {
      siblingWithId = siblings[i];
      break;
    }
  }
  return siblingWithId;
};

// Returns true if the browser supports the "copy" command.
function supportsCopying() {
  return !!document.queryCommandSupported &&
      !!document.queryCommandSupported('copy');
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
// Copies the given string to the clipboard.
function copyStringToClipboard(string) {
  var textArea = document.createElement("textarea");
  textArea.value = string;
  document.body.appendChild(textArea);
  textArea.focus();
  textArea.select();

  if (!supportsCopying()) {
    alert('Unable to copy to clipboard (not supported by browser)');
    return;
  }

  try {
    document.execCommand('copy');
  } finally {
    document.body.removeChild(textArea);
  }
}

function fixHref(anchor, id) {
  anchor.href = window.location.href.replace(/#.*$/, '') + '#' + id;
}

84 85 86
// Copies the text inside the currently visible snippet to the clipboard, or the
// given element, if any.
function copyTextToClipboard(element) {
87 88
  if (typeof element === 'string') {
    var elementSelector = '#' + element + ' .language-dart';
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    element = document.querySelector(elementSelector);
    if (element == null) {
      console.log(
          'copyTextToClipboard: Unable to find element for "' +
          elementSelector + '"');
      return;
    }
  }
  if (!supportsCopying()) {
    alert('Unable to copy to clipboard (not supported by browser)');
    return;
  }

  if (element.hasAttribute('contenteditable')) {
    element.focus();
  }

  var selection = window.getSelection();
  var range = document.createRange();

  range.selectNodeContents(element);
  selection.removeAllRanges();
  selection.addRange(range);
  document.execCommand('copy');
}