Path: blob/trunk/third_party/closure/goog/editor/plugins/spacestabhandler.js
2868 views
// Copyright 2008 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Editor plugin to handle tab keys not in lists to add 4 spaces.16*17* @author [email protected] (Robby Walker)18*/1920goog.provide('goog.editor.plugins.SpacesTabHandler');2122goog.require('goog.dom.TagName');23goog.require('goog.editor.plugins.AbstractTabHandler');24goog.require('goog.editor.range');25262728/**29* Plugin to handle tab keys when not in lists to add 4 spaces.30* @constructor31* @extends {goog.editor.plugins.AbstractTabHandler}32* @final33*/34goog.editor.plugins.SpacesTabHandler = function() {35goog.editor.plugins.AbstractTabHandler.call(this);36};37goog.inherits(38goog.editor.plugins.SpacesTabHandler,39goog.editor.plugins.AbstractTabHandler);404142/** @override */43goog.editor.plugins.SpacesTabHandler.prototype.getTrogClassId = function() {44return 'SpacesTabHandler';45};464748/** @override */49goog.editor.plugins.SpacesTabHandler.prototype.handleTabKey = function(e) {50var dh = this.getFieldDomHelper();51var range = this.getFieldObject().getRange();52if (!goog.editor.range.intersectsTag(range, goog.dom.TagName.LI)) {53// In the shift + tab case we don't want to insert spaces, but we don't54// want focus to move either so skip the spacing logic and just prevent55// default.56if (!e.shiftKey) {57// Not in a list but we want to insert 4 spaces.5859// Stop change events while we make multiple field changes.60this.getFieldObject().stopChangeEvents(true, true);6162// Inserting nodes below completely messes up the selection, doing the63// deletion here before it's messed up. Only delete if text is selected,64// otherwise we would remove the character to the right of the cursor.65if (!range.isCollapsed()) {66dh.getDocument().execCommand('delete', false, null);67// Safari 3 has some DOM exceptions if we don't reget the range here,68// doing it all the time just to be safe.69range = this.getFieldObject().getRange();70}7172// Emulate tab by removing selection and inserting 4 spaces73// Two breaking spaces in a row can be collapsed by the browser into one74// space. Inserting the string below because it is guaranteed to never75// collapse to less than four spaces, regardless of what is adjacent to76// the inserted spaces. This might make line wrapping slightly77// sub-optimal around a grouping of non-breaking spaces.78var elem =79dh.createDom(goog.dom.TagName.SPAN, null, '\u00a0\u00a0 \u00a0');80elem = range.insertNode(elem, false);8182this.getFieldObject().dispatchChange();83goog.editor.range.placeCursorNextTo(elem, false);84this.getFieldObject().dispatchSelectionChangeEvent();85}8687e.preventDefault();88return true;89}9091return false;92};939495