2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @extends {WebInspector.InplaceEditor}
35 WebInspector.CodeMirrorUtils = function()
37 WebInspector.InplaceEditor.call(this);
40 WebInspector.CodeMirrorUtils.prototype = {
44 editorContent: function(editingContext) {
45 return editingContext.codeMirror.getValue();
51 _consumeCopy: function(e)
56 setUpEditor: function(editingContext)
58 var element = editingContext.element;
59 var config = editingContext.config;
60 loadScript("CodeMirrorTextEditor.js");
61 editingContext.cssLoadView = new WebInspector.CodeMirrorCSSLoadView();
62 editingContext.cssLoadView.show(element);
63 WebInspector.setCurrentFocusElement(element);
64 element.addEventListener("copy", this._consumeCopy, false);
65 var codeMirror = window.CodeMirror(element, {
67 lineWrapping: config.lineWrapping,
68 smartIndent: config.smartIndent,
71 value: config.initialValue
73 codeMirror.getWrapperElement().classList.add("source-code");
74 codeMirror.on("cursorActivity", function(cm) {
75 cm.display.cursor.scrollIntoViewIfNeeded(false);
77 editingContext.codeMirror = codeMirror;
80 closeEditor: function(editingContext)
82 editingContext.element.removeEventListener("copy", this._consumeCopy, false);
83 editingContext.cssLoadView.detach();
86 cancelEditing: function(editingContext)
88 editingContext.codeMirror.setValue(editingContext.oldText);
91 augmentEditingHandle: function(editingContext, handle)
93 function setWidth(editingContext, width)
96 var codeMirror = editingContext.codeMirror;
97 codeMirror.getWrapperElement().style.width = (width - codeMirror.getWrapperElement().offsetLeft - padding) + "px";
101 handle.codeMirror = editingContext.codeMirror;
102 handle.setWidth = setWidth.bind(null, editingContext);
105 __proto__: WebInspector.InplaceEditor.prototype
110 * @implements {WebInspector.TokenizerFactory}
112 WebInspector.CodeMirrorUtils.TokenizerFactory = function() { }
114 WebInspector.CodeMirrorUtils.TokenizerFactory.prototype = {
116 * @param {string} mimeType
117 * @return {function(string, function(string, ?string, number, number))}
119 createTokenizer: function(mimeType)
121 var mode = CodeMirror.getMode({indentUnit: 2}, mimeType);
122 var state = CodeMirror.startState(mode);
123 function tokenize(line, callback)
125 var stream = new CodeMirror.StringStream(line);
126 while (!stream.eol()) {
127 var style = mode.token(stream, state);
128 var value = stream.current();
129 callback(value, style, stream.start, stream.start + value.length);
130 stream.start = stream.pos;
138 * This bogus view is needed to load/unload CodeMirror-related CSS on demand.
141 * @extends {WebInspector.View}
143 WebInspector.CodeMirrorCSSLoadView = function()
145 WebInspector.View.call(this);
146 this.element.classList.add("hidden");
147 this.registerRequiredCSS("cm/codemirror.css");
148 this.registerRequiredCSS("cm/cmdevtools.css");
151 WebInspector.CodeMirrorCSSLoadView.prototype = {
152 __proto__: WebInspector.View.prototype