| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Editor plugin to handle tab keys not in lists to add 4 spaces. |
| 17 |
* |
| 18 |
* @author [email protected] (Robby Walker) |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.editor.plugins.SpacesTabHandler'); |
| 22 |
|
| 23 |
goog.require('goog.dom.TagName'); |
| 24 |
goog.require('goog.editor.plugins.AbstractTabHandler'); |
| 25 |
goog.require('goog.editor.range'); |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Plugin to handle tab keys when not in lists to add 4 spaces. |
| 31 |
* @constructor |
| 32 |
* @extends {goog.editor.plugins.AbstractTabHandler} |
| 33 |
* @final |
| 34 |
*/ |
| 35 |
goog.editor.plugins.SpacesTabHandler = function() { |
| 36 |
goog.editor.plugins.AbstractTabHandler.call(this); |
| 37 |
}; |
| 38 |
goog.inherits( |
| 39 |
goog.editor.plugins.SpacesTabHandler, |
| 40 |
goog.editor.plugins.AbstractTabHandler); |
| 41 |
|
| 42 |
|
| 43 |
/** @override */ |
| 44 |
goog.editor.plugins.SpacesTabHandler.prototype.getTrogClassId = function() { |
| 45 |
return 'SpacesTabHandler'; |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
/** @override */ |
| 50 |
goog.editor.plugins.SpacesTabHandler.prototype.handleTabKey = function(e) { |
| 51 |
var dh = this.getFieldDomHelper(); |
| 52 |
var range = this.getFieldObject().getRange(); |
| 53 |
if (!goog.editor.range.intersectsTag(range, goog.dom.TagName.LI)) { |
| 54 |
// In the shift + tab case we don't want to insert spaces, but we don't |
| 55 |
// want focus to move either so skip the spacing logic and just prevent |
| 56 |
// default. |
| 57 |
if (!e.shiftKey) { |
| 58 |
// Not in a list but we want to insert 4 spaces. |
| 59 |
|
| 60 |
// Stop change events while we make multiple field changes. |
| 61 |
this.getFieldObject().stopChangeEvents(true, true); |
| 62 |
|
| 63 |
// Inserting nodes below completely messes up the selection, doing the |
| 64 |
// deletion here before it's messed up. Only delete if text is selected, |
| 65 |
// otherwise we would remove the character to the right of the cursor. |
| 66 |
if (!range.isCollapsed()) { |
| 67 |
dh.getDocument().execCommand('delete', false, null); |
| 68 |
// Safari 3 has some DOM exceptions if we don't reget the range here, |
| 69 |
// doing it all the time just to be safe. |
| 70 |
range = this.getFieldObject().getRange(); |
| 71 |
} |
| 72 |
|
| 73 |
// Emulate tab by removing selection and inserting 4 spaces |
| 74 |
// Two breaking spaces in a row can be collapsed by the browser into one |
| 75 |
// space. Inserting the string below because it is guaranteed to never |
| 76 |
// collapse to less than four spaces, regardless of what is adjacent to |
| 77 |
// the inserted spaces. This might make line wrapping slightly |
| 78 |
// sub-optimal around a grouping of non-breaking spaces. |
| 79 |
var elem = |
| 80 |
dh.createDom(goog.dom.TagName.SPAN, null, '\u00a0\u00a0 \u00a0'); |
| 81 |
elem = range.insertNode(elem, false); |
| 82 |
|
| 83 |
this.getFieldObject().dispatchChange(); |
| 84 |
goog.editor.range.placeCursorNextTo(elem, false); |
| 85 |
this.getFieldObject().dispatchSelectionChangeEvent(); |
| 86 |
} |
| 87 |
|
| 88 |
e.preventDefault(); |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
return false; |
| 93 |
}; |