ace.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. /**
  31. * The main class required to set up an Ace instance in the browser.
  32. *
  33. * @class Ace
  34. **/
  35. define(function(require, exports, module) {
  36. "use strict";
  37. require("./lib/fixoldbrowsers");
  38. var dom = require("./lib/dom");
  39. var event = require("./lib/event");
  40. var Editor = require("./editor").Editor;
  41. var EditSession = require("./edit_session").EditSession;
  42. var UndoManager = require("./undomanager").UndoManager;
  43. var Renderer = require("./virtual_renderer").VirtualRenderer;
  44. // The following require()s are for inclusion in the built ace file
  45. require("./worker/worker_client");
  46. require("./keyboard/hash_handler");
  47. require("./placeholder");
  48. require("./multi_select");
  49. require("./mode/folding/fold_mode");
  50. require("./theme/textmate");
  51. require("./ext/error_marker");
  52. exports.config = require("./config");
  53. /**
  54. * Provides access to require in packed noconflict mode
  55. * @param {String} moduleName
  56. * @returns {Object}
  57. *
  58. **/
  59. exports.require = require;
  60. /**
  61. * Embeds the Ace editor into the DOM, at the element provided by `el`.
  62. * @param {String | DOMElement} el Either the id of an element, or the element itself
  63. *
  64. **/
  65. exports.edit = function(el) {
  66. if (typeof(el) == "string") {
  67. var _id = el;
  68. el = document.getElementById(_id);
  69. if (!el)
  70. throw new Error("ace.edit can't find div #" + _id);
  71. }
  72. if (el && el.env && el.env.editor instanceof Editor)
  73. return el.env.editor;
  74. var value = "";
  75. if (el && /input|textarea/i.test(el.tagName)) {
  76. var oldNode = el;
  77. value = oldNode.value;
  78. el = dom.createElement("pre");
  79. oldNode.parentNode.replaceChild(el, oldNode);
  80. } else if (el) {
  81. value = dom.getInnerText(el);
  82. el.innerHTML = '';
  83. }
  84. var doc = exports.createEditSession(value);
  85. var editor = new Editor(new Renderer(el));
  86. editor.setSession(doc);
  87. var env = {
  88. document: doc,
  89. editor: editor,
  90. onResize: editor.resize.bind(editor, null)
  91. };
  92. if (oldNode) env.textarea = oldNode;
  93. event.addListener(window, "resize", env.onResize);
  94. editor.on("destroy", function() {
  95. event.removeListener(window, "resize", env.onResize);
  96. env.editor.container.env = null; // prevent memory leak on old ie
  97. });
  98. editor.container.env = editor.env = env;
  99. return editor;
  100. };
  101. /**
  102. * Creates a new [[EditSession]], and returns the associated [[Document]].
  103. * @param {Document | String} text {:textParam}
  104. * @param {TextMode} mode {:modeParam}
  105. *
  106. **/
  107. exports.createEditSession = function(text, mode) {
  108. var doc = new EditSession(text, mode);
  109. doc.setUndoManager(new UndoManager());
  110. return doc;
  111. }
  112. exports.EditSession = EditSession;
  113. exports.UndoManager = UndoManager;
  114. exports.version = "1.2.2";
  115. });