Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/ui/InplaceEditor.js

Issue 2372663004: DevTools: Replace multiline InplaceEditor with CodeMirrorTextEditor (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @constructor 6 * @constructor
7 */ 7 */
8 WebInspector.InplaceEditor = function() 8 WebInspector.InplaceEditor = function()
9 { 9 {
10 } 10 }
11 11
12 /** 12 /**
13 * @typedef {{cancel: function(), commit: function(), setWidth: function(number) }} 13 * @typedef {{cancel: function(), commit: function()}}
14 */ 14 */
15 WebInspector.InplaceEditor.Controller; 15 WebInspector.InplaceEditor.Controller;
16 16
17 /** 17 /**
18 * @param {!Element} element 18 * @param {!Element} element
19 * @param {!WebInspector.InplaceEditor.Config=} config 19 * @param {!WebInspector.InplaceEditor.Config=} config
20 * @return {?WebInspector.InplaceEditor.Controller} 20 * @return {?WebInspector.InplaceEditor.Controller}
21 */ 21 */
22 WebInspector.InplaceEditor.startEditing = function(element, config) 22 WebInspector.InplaceEditor.startEditing = function(element, config)
23 { 23 {
24 if (!WebInspector.InplaceEditor._defaultInstance) 24 if (!WebInspector.InplaceEditor._defaultInstance)
25 WebInspector.InplaceEditor._defaultInstance = new WebInspector.InplaceEd itor(); 25 WebInspector.InplaceEditor._defaultInstance = new WebInspector.InplaceEd itor();
26 return WebInspector.InplaceEditor._defaultInstance.startEditing(element, con fig); 26 return WebInspector.InplaceEditor._defaultInstance.startEditing(element, con fig);
27 } 27 }
28 28
29 /**
30 * @param {!Element} element
31 * @param {!WebInspector.InplaceEditor.Config=} config
32 * @return {!Promise.<!WebInspector.InplaceEditor.Controller>}
33 */
34 WebInspector.InplaceEditor.startMultilineEditing = function(element, config)
35 {
36 return self.runtime.extension(WebInspector.InplaceEditor).instance().then(st artEditing);
37
38 /**
39 * @param {!Object} inplaceEditor
40 * @return {!WebInspector.InplaceEditor.Controller|!Promise.<!WebInspector.I nplaceEditor.Controller>}
41 */
42 function startEditing(inplaceEditor)
43 {
44 var controller = /** @type {!WebInspector.InplaceEditor} */ (inplaceEdit or).startEditing(element, config);
45 if (!controller)
46 return Promise.reject(new Error("Editing is already in progress"));
47 return controller;
48 }
49 }
50
51 WebInspector.InplaceEditor.prototype = { 29 WebInspector.InplaceEditor.prototype = {
52 /** 30 /**
53 * @return {string} 31 * @return {string}
54 */ 32 */
55 editorContent: function(editingContext) { 33 editorContent: function(editingContext) {
56 var element = editingContext.element; 34 var element = editingContext.element;
57 if (element.tagName === "INPUT" && element.type === "text") 35 if (element.tagName === "INPUT" && element.type === "text")
58 return element.value; 36 return element.value;
59 37
60 return element.textContent; 38 return element.textContent;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 { 85 {
108 if (!WebInspector.markBeingEdited(element, true)) 86 if (!WebInspector.markBeingEdited(element, true))
109 return null; 87 return null;
110 88
111 config = config || new WebInspector.InplaceEditor.Config(function() {}, function() {}); 89 config = config || new WebInspector.InplaceEditor.Config(function() {}, function() {});
112 var editingContext = { element: element, config: config }; 90 var editingContext = { element: element, config: config };
113 var committedCallback = config.commitHandler; 91 var committedCallback = config.commitHandler;
114 var cancelledCallback = config.cancelHandler; 92 var cancelledCallback = config.cancelHandler;
115 var pasteCallback = config.pasteHandler; 93 var pasteCallback = config.pasteHandler;
116 var context = config.context; 94 var context = config.context;
117 var isMultiline = config.multiline || false;
118 var moveDirection = ""; 95 var moveDirection = "";
119 var self = this; 96 var self = this;
120 97
121 /** 98 /**
122 * @param {!Event} e 99 * @param {!Event} e
123 */ 100 */
124 function consumeCopy(e) 101 function consumeCopy(e)
125 { 102 {
126 e.consume(); 103 e.consume();
127 } 104 }
128 105
129 this.setUpEditor(editingContext); 106 this.setUpEditor(editingContext);
130 107
131 editingContext.oldText = isMultiline ? config.initialValue : this.editor Content(editingContext); 108 editingContext.oldText = this.editorContent(editingContext);
132 109
133 /** 110 /**
134 * @param {!Event=} e 111 * @param {!Event=} e
135 */ 112 */
136 function blurEventListener(e) { 113 function blurEventListener(e) {
137 if (config.blurHandler && !config.blurHandler(element, e)) 114 if (config.blurHandler && !config.blurHandler(element, e))
138 return; 115 return;
139 if (!isMultiline || !e || !e.relatedTarget || !e.relatedTarget.isSel fOrDescendant(element)) 116 editingCommitted.call(element);
140 editingCommitted.call(element);
141 } 117 }
142 118
143 function cleanUpAfterEditing() 119 function cleanUpAfterEditing()
144 { 120 {
145 WebInspector.markBeingEdited(element, false); 121 WebInspector.markBeingEdited(element, false);
146 122
147 element.removeEventListener("blur", blurEventListener, isMultiline); 123 element.removeEventListener("blur", blurEventListener, false);
148 element.removeEventListener("keydown", keyDownEventListener, true); 124 element.removeEventListener("keydown", keyDownEventListener, true);
149 if (pasteCallback) 125 if (pasteCallback)
150 element.removeEventListener("paste", pasteEventListener, true); 126 element.removeEventListener("paste", pasteEventListener, true);
151 127
152 WebInspector.restoreFocusFromElement(element); 128 WebInspector.restoreFocusFromElement(element);
153 self.closeEditor(editingContext); 129 self.closeEditor(editingContext);
154 } 130 }
155 131
156 /** @this {Element} */ 132 /** @this {Element} */
157 function editingCancelled() 133 function editingCancelled()
(...skipping 13 matching lines...) Expand all
171 147
172 /** 148 /**
173 * @param {!Event} event 149 * @param {!Event} event
174 * @return {string} 150 * @return {string}
175 */ 151 */
176 function defaultFinishHandler(event) 152 function defaultFinishHandler(event)
177 { 153 {
178 var isMetaOrCtrl = WebInspector.isMac() ? 154 var isMetaOrCtrl = WebInspector.isMac() ?
179 event.metaKey && !event.shiftKey && !event.ctrlKey && !event.alt Key : 155 event.metaKey && !event.shiftKey && !event.ctrlKey && !event.alt Key :
180 event.ctrlKey && !event.shiftKey && !event.metaKey && !event.alt Key; 156 event.ctrlKey && !event.shiftKey && !event.metaKey && !event.alt Key;
181 if (isEnterKey(event) && (event.isMetaOrCtrlForTest || !isMultiline || isMetaOrCtrl)) 157 if (isEnterKey(event))
182 return "commit"; 158 return "commit";
183 else if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Esc.co de || event.key === "Escape") 159 else if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Esc.co de || event.key === "Escape")
184 return "cancel"; 160 return "cancel";
185 else if (!isMultiline && event.key === "Tab") 161 else if (event.key === "Tab")
186 return "move-" + (event.shiftKey ? "backward" : "forward"); 162 return "move-" + (event.shiftKey ? "backward" : "forward");
187 return ""; 163 return "";
188 } 164 }
189 165
190 function handleEditingResult(result, event) 166 function handleEditingResult(result, event)
191 { 167 {
192 if (result === "commit") { 168 if (result === "commit") {
193 editingCommitted.call(element); 169 editingCommitted.call(element);
194 event.consume(true); 170 event.consume(true);
195 } else if (result === "cancel") { 171 } else if (result === "cancel") {
(...skipping 19 matching lines...) Expand all
215 * @param {!Event} event 191 * @param {!Event} event
216 */ 192 */
217 function keyDownEventListener(event) 193 function keyDownEventListener(event)
218 { 194 {
219 var result = defaultFinishHandler(event); 195 var result = defaultFinishHandler(event);
220 if (!result && config.postKeydownFinishHandler) 196 if (!result && config.postKeydownFinishHandler)
221 result = config.postKeydownFinishHandler(event); 197 result = config.postKeydownFinishHandler(event);
222 handleEditingResult(result, event); 198 handleEditingResult(result, event);
223 } 199 }
224 200
225 element.addEventListener("blur", blurEventListener, isMultiline); 201 element.addEventListener("blur", blurEventListener, false);
226 element.addEventListener("keydown", keyDownEventListener, true); 202 element.addEventListener("keydown", keyDownEventListener, true);
227 if (pasteCallback) 203 if (pasteCallback)
228 element.addEventListener("paste", pasteEventListener, true); 204 element.addEventListener("paste", pasteEventListener, true);
229 205
230 var handle = { 206 var handle = {
231 cancel: editingCancelled.bind(element), 207 cancel: editingCancelled.bind(element),
232 commit: editingCommitted.bind(element), 208 commit: editingCommitted.bind(element)
233 setWidth: function() {}
234 }; 209 };
235 this.augmentEditingHandle(editingContext, handle); 210 this.augmentEditingHandle(editingContext, handle);
236 return handle; 211 return handle;
237 } 212 }
238 } 213 }
239 214
240 /** 215 /**
241 * @constructor 216 * @constructor
242 * @param {function(!Element,string,string,T,string)} commitHandler 217 * @param {function(!Element,string,string,T,string)} commitHandler
243 * @param {function(!Element,T)} cancelHandler 218 * @param {function(!Element,T)} cancelHandler
244 * @param {T=} context 219 * @param {T=} context
245 * @param {function(!Element,!Event):boolean=} blurHandler 220 * @param {function(!Element,!Event):boolean=} blurHandler
246 * @template T 221 * @template T
247 */ 222 */
248 WebInspector.InplaceEditor.Config = function(commitHandler, cancelHandler, conte xt, blurHandler) 223 WebInspector.InplaceEditor.Config = function(commitHandler, cancelHandler, conte xt, blurHandler)
249 { 224 {
250 this.commitHandler = commitHandler; 225 this.commitHandler = commitHandler;
251 this.cancelHandler = cancelHandler; 226 this.cancelHandler = cancelHandler;
252 this.context = context; 227 this.context = context;
253 this.blurHandler = blurHandler; 228 this.blurHandler = blurHandler;
254 229
255 /** 230 /**
256 * @type {function(!Event):string|undefined} 231 * @type {function(!Event):string|undefined}
257 */ 232 */
258 this.pasteHandler; 233 this.pasteHandler;
259 234
260 /** 235 /**
261 * @type {boolean|undefined}
262 */
263 this.multiline;
264
265 /**
266 * @type {function(!Event):string|undefined} 236 * @type {function(!Event):string|undefined}
267 */ 237 */
268 this.postKeydownFinishHandler; 238 this.postKeydownFinishHandler;
269 } 239 }
270 240
271 WebInspector.InplaceEditor.Config.prototype = { 241 WebInspector.InplaceEditor.Config.prototype = {
272 setPasteHandler: function(pasteHandler) 242 setPasteHandler: function(pasteHandler)
273 { 243 {
274 this.pasteHandler = pasteHandler; 244 this.pasteHandler = pasteHandler;
275 }, 245 },
276 246
277 /** 247 /**
278 * @param {string} initialValue
279 * @param {!Object} mode
280 * @param {string} theme
281 * @param {boolean=} lineWrapping
282 * @param {boolean=} smartIndent
283 */
284 setMultilineOptions: function(initialValue, mode, theme, lineWrapping, smart Indent)
285 {
286 this.multiline = true;
287 this.initialValue = initialValue;
288 this.mode = mode;
289 this.theme = theme;
290 this.lineWrapping = lineWrapping;
291 this.smartIndent = smartIndent;
292 },
293
294 /**
295 * @param {function(!Event):string} postKeydownFinishHandler 248 * @param {function(!Event):string} postKeydownFinishHandler
296 */ 249 */
297 setPostKeydownFinishHandler: function(postKeydownFinishHandler) 250 setPostKeydownFinishHandler: function(postKeydownFinishHandler)
298 { 251 {
299 this.postKeydownFinishHandler = postKeydownFinishHandler; 252 this.postKeydownFinishHandler = postKeydownFinishHandler;
300 } 253 }
301 } 254 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698