📄 htmlarea-gecko.js
字号:
/**************************************************************** Copyright notice** (c) 2002-2004, interactivetools.com, inc.* (c) 2003-2004 dynarch.com* (c) 2004-2007 Stanislas Rolland <stanislas.rolland(arobas)fructifor.ca>* All rights reserved** This script is part of the TYPO3 project. The TYPO3 project is* free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.** The GNU General Public License can be found at* http://www.gnu.org/copyleft/gpl.html.* A copy is found in the textfile GPL.txt and important notices to the license* from the author is found in LICENSE.txt distributed with these scripts.*** This script is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** This script is a modified version of a script published under the htmlArea License.* A copy of the htmlArea License may be found in the textfile HTMLAREA_LICENSE.txt.** This copyright notice MUST APPEAR in all copies of the script!***************************************************************//* * TYPO3 CVS ID: $Id: htmlarea-gecko.js 2648 2007-11-01 16:03:22Z stanrolland $ *//*************************************************** * GECKO-SPECIFIC FUNCTIONS ***************************************************//*************************************************** * MOZILLA/FIREFOX EDIT MODE INITILIZATION ***************************************************/HTMLArea.prototype._initEditMode = function () { // We can't set designMode when we are in a hidden TYPO3 tab // Then we will set it when the tab comes in the front. var isNested = false; var allDisplayed = true; if (this.nested.sorted && this.nested.sorted.length) { isNested = true; allDisplayed = HTMLArea.allElementsAreDisplayed(this.nested.sorted); } if (!HTMLArea.is_wamcom) { try { if (!isNested || allDisplayed) this._doc.designMode = "on"; } catch(e) { } } else { try { this._doc.designMode = "on"; } catch(e) { if (!isNested || allDisplayed) { this._doc.open(); this._doc.close(); this._initIframeTimer = window.setTimeout("HTMLArea.initIframe(" + this._editorNumber + ");", 500); return false; } } } // When the TYPO3 TCA feature div2tab is used, the editor iframe may become hidden with style.display = "none" // This breaks the editor in Mozilla/Firefox browsers: the designMode attribute needs to be resetted after the style.display of the containing div is resetted to "block" // Here we rely on TYPO3 naming conventions for the div id and class name if (this.nested.sorted && this.nested.sorted.length) { var nestedObj, listenerFunction; for (var i=0, length=this.nested.sorted.length; i < length; i++) { nestedObj = document.getElementById(this.nested.sorted[i]); listenerFunction = HTMLArea.NestedListener(this, nestedObj, false); HTMLArea._addEvent(nestedObj, 'DOMAttrModified', listenerFunction); } } return true;};/*************************************************** * SELECTIONS AND RANGES ***************************************************//* * Get the current selection object */HTMLArea.prototype._getSelection = function() { if (HTMLArea.is_safari) return window.getSelection(); return this._iframe.contentWindow.getSelection();};/* * Create a range for the current selection */HTMLArea.prototype._createRange = function(sel) { if (HTMLArea.is_safari) { var range = this._doc.createRange(); if (typeof(sel) == "undefined") return range; switch (sel.type) { case "Range": range.setStart(sel.baseNode,sel.baseOffset); range.setEnd(sel.extentNode,sel.extentOffset); break; case "Caret": range.setStart(sel.baseNode,sel.baseOffset); range.setEnd(sel.baseNode,sel.baseOffset); break; case "None": range.setStart(this._doc.body,0); range.setEnd(this._doc.body,0); } return range; } if (typeof(sel) == "undefined") return this._doc.createRange(); try { return sel.getRangeAt(0); } catch(e) { return this._doc.createRange(); }};/* * Select a node AND the contents inside the node */HTMLArea.prototype.selectNode = function(node,pos) { this.focusEditor(); var sel = this._getSelection(); var range = this._doc.createRange(); if (node.nodeType == 1 && node.tagName.toLowerCase() == "body") range.selectNodeContents(node); else range.selectNode(node); if ((typeof(pos) != "undefined")) range.collapse(pos); if (HTMLArea.is_safari) { sel.empty(); sel.setBaseAndExtent(range.startContainer,range.startOffset,range.endContainer,range.endOffset); } else { sel.removeAllRanges(); sel.addRange(range); }};/* * Select ONLY the contents inside the given node */HTMLArea.prototype.selectNodeContents = function(node,pos) { this.focusEditor(); var sel = this._getSelection(); var range = this._doc.createRange(); range.selectNodeContents(node); if ((typeof(pos) != "undefined")) range.collapse(pos); if (HTMLArea.is_safari) { sel.empty(); sel.setBaseAndExtent(range.startContainer,range.startOffset,range.endContainer,range.endOffset); } else { sel.removeAllRanges(); sel.addRange(range); }};/* * Retrieve the HTML contents of selected block */HTMLArea.prototype.getSelectedHTML = function() { var sel = this._getSelection(); var range = this._createRange(sel); var cloneContents = ""; try {cloneContents = range.cloneContents();} catch(e) { } return (cloneContents ? HTMLArea.getHTML(cloneContents,false,this) : "");};/* * Retrieve simply HTML contents of the selected block, IE ignoring control ranges */HTMLArea.prototype.getSelectedHTMLContents = function() { return this.getSelectedHTML();};/* * Get the deepest node that contains both endpoints of the current selection. */HTMLArea.prototype.getParentElement = function(sel,range) { if(!sel) var sel = this._getSelection(); if (typeof(range) == "undefined") var range = this._createRange(sel); try { var p = range.commonAncestorContainer; if(!range.collapsed && range.startContainer == range.endContainer && range.startOffset - range.endOffset <= 1 && range.startContainer.hasChildNodes()) p = range.startContainer.childNodes[range.startOffset]; while (p.nodeType == 3) {p = p.parentNode;} return p; } catch (e) { return this._doc.body; }};/* * Get the selected element, if any. That is, the element that you have last selected in the "path" * at the bottom of the editor, or a "control" (eg image) * * @returns null | element * Borrowed from Xinha (is not htmlArea) - http://xinha.gogo.co.nz/ */HTMLArea.prototype._activeElement = function(sel) { if(sel == null) return null; if(this._selectionEmpty(sel)) return null; // Check if the selection is not collapsed (something is selected) and if the anchor (start of selection) is an element. if(!sel.isCollapsed && sel.anchorNode.nodeType == 1) return sel.anchorNode; else return null;};/* * Determine if the current selection is empty or not. */HTMLArea.prototype._selectionEmpty = function(sel) { if (!sel) return true; if (typeof(sel.isCollapsed) != 'undefined') { if (HTMLArea.is_opera) this._createRange(sel).collapsed; else sel.isCollapsed; } else { return true; }};/*************************************************** * DOM TREE MANIPULATION ***************************************************/ /* * Insert a node at the current position. * Delete the current selection, if any. * Split the text node, if needed. */HTMLArea.prototype.insertNodeAtSelection = function(toBeInserted) { this.focusEditor(); var sel = this._getSelection(), range = this._createRange(sel), node = range.startContainer, pos = range.startOffset, selnode = toBeInserted; if (HTMLArea.is_safari) sel.empty(); else sel.removeAllRanges(); range.deleteContents(); switch (node.nodeType) { case 3: // Node.TEXT_NODE: we have to split it at the caret position. if(toBeInserted.nodeType == 3) { node.insertData(pos,toBeInserted.data); range = this._createRange(); range.setEnd(node, pos + toBeInserted.length); range.setStart(node, pos + toBeInserted.length); if (HTMLArea.is_safari) sel.setBaseAndExtent(range.startContainer, range.startOffset, range.endContainer, range.endOffset); else sel.addRange(range); } else { node = node.splitText(pos); if (toBeInserted.nodeType == 11) selnode = selnode.lastChild; node = node.parentNode.insertBefore(toBeInserted, node); this.selectNode(selnode, false); this.updateToolbar(); } break; case 1: if (toBeInserted.nodeType == 11) selnode = selnode.lastChild; node = node.insertBefore(toBeInserted, node.childNodes[pos]); this.selectNode(selnode, false); this.updateToolbar(); break; }};/* * Insert HTML source code at the current position. * Delete the current selection, if any. */HTMLArea.prototype.insertHTML = function(html) { this.focusEditor(); var fragment = this._doc.createDocumentFragment(); var div = this._doc.createElement("div"); div.innerHTML = html; while (div.firstChild) {fragment.appendChild(div.firstChild);} this.insertNodeAtSelection(fragment);};/*************************************************** * EVENTS HANDLERS ***************************************************//* * TYPO3 hidden tab and inline event listener (gets event calls) */HTMLArea.NestedListener = function (editor,nestedObj,noOpenCloseAction) { return (function(ev) { if(!ev) var ev = window.event; HTMLArea.NestedHandler(ev,editor,nestedObj,noOpenCloseAction); });};/* * TYPO3 hidden tab and inline event handler (performs actions on event calls) */HTMLArea.NestedHandler = function(ev,editor,nestedObj,noOpenCloseAction) { window.setTimeout(function() { var target = (ev.target) ? ev.target : ev.srcElement; if(target == nestedObj && editor._editMode == "wysiwyg" && ev.attrName=='style' && (target.style.display == '' || target.style.display == 'block')) { // Check if all affected nested elements are displayed (style.display!='none'): if (HTMLArea.allElementsAreDisplayed(editor.nested.sorted)) { window.setTimeout(function() { try { editor._doc.designMode = "on"; if (editor.config.sizeIncludesToolbar && editor._initialToolbarOffsetHeight != editor._toolbar.offsetHeight) { editor.sizeIframe(-2); } } catch(e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -