⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 enter-paragraphs.js

📁 Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是供中,大型企业来管理其发布在互连网
💻 JS
📖 第 1 页 / 共 3 页
字号:

    // roam = editor._doc.createElement('p');
    // roam.appendChild(editor._doc.createElement('br'));

		// for these cases, if we are processing the left hand side we want it to halt
		// processing instead of doing the right hand side. (Avoids adding another <p>&nbsp</p>
		// after the list etc.

    if ((search_direction == "left" ) && pify.previousSibling)
			{

			//this.ddt._ddt( "enter-paragraphs.js", "682", "processRng(): returning created roam AfterEnd" );

			return new Array(pify.previousSibling, 'AfterEnd', roam);
			}
    else if (( search_direction == "right") && pify.nextSibling)
			{

			//this.ddt._ddt( "enter-paragraphs.js", "682", "processRng(): returning created roam BeforeBegin" );

			return new Array(pify.nextSibling, 'BeforeBegin', roam);
			}
    else
			{

			//this.ddt._ddt( "enter-paragraphs.js", "682", "processRng(): returning created roam for direction '" + search_direction + "'" );

			return new Array(pify.parentNode, (search_direction == "left"?'AfterBegin':'BeforeEnd'), roam);
			}

	  }

  // If our cloned contents are 'content'-less, shove a break in them

  if ( fill )
		{

		// Ill-concieved?
		//
		// 3 is a TEXT node and it should be empty.
		//

		if ( fill.nodeType == 3 )
			{
			// fill = fill.parentNode;

			fill = editor._doc.createDocumentFragment();

			//this.ddt._ddtDumpNode( "enter-paragraphs.js", "575", "processRng(): fill.nodeType is 3. Moving up to parent:", fill );
			}

    if ( (fill.nodeType == 1 && !this._elemSolid.test()) || fill.nodeType == 11 )
			{

			// FIXME:/CHECKME: When Xinha is switched from WYSIWYG to text mode
			// HTMLArea.getHTMLWrapper() will strip out the trailing br. Not sure why.

			// fill.appendChild(editor._doc.createElement('br'));

			var pterminator = editor._doc.createElement( 'p' );
			pterminator.innerHTML = "&nbsp;";

			fill.appendChild( pterminator );

			//this.ddt._ddtDumpNode( "enter-paragraphs.js", "583", "processRng(): fill type is 1 and !elemsolid or it's type 11. Appending an nbsp tag:", fill );

			}
    else
			{

			//this.ddt._ddt( "enter-paragraphs.js", "583", "processRng(): inserting a br tag before." );

			// fill.parentNode.insertBefore(editor._doc.createElement('br'),fill);

			var pterminator = editor._doc.createElement( 'p' );
			pterminator.innerHTML = "&nbsp;";

			fill.parentNode.insertBefore(parentNode,fill);

			}
	  }

	// YmL: If there was no content replace with fill
	// (previous code did not use fill and we ended up with the
	// <p>test</p><p></p> because Gecko was finding two empty text nodes
	// when traversing on the right hand side of an empty document.

	if ( fill )
		{

		//this.ddt._ddtDumpNode( "enter-paragraphs.js", "606", "processRng(): no content. Using fill.", fill );

		roam = fill;
		}
	else
		{
	  // And stuff a shiny new object with whatever contents we have

		//this.ddt._ddt( "enter-paragraphs.js", "606", "processRng(): creating p tag or document fragment - pWrap is '" + pWrap + "' " );

	  roam = (pWrap || (cnt.nodeType == 11 && !cnt.firstChild)) ? editor._doc.createElement('p') : editor._doc.createDocumentFragment();
	  roam.appendChild(cnt);
		}

  if (preBr)
		{
		//this.ddt._ddt( "enter-paragraphs.js", "767", "processRng(): appending a br based on preBr flag" );

		roam.appendChild(editor._doc.createElement('br'));
		}

	//this.ddt._ddtDumpNode( "enter-paragraphs.js", "606", "processRng(): bottom with roam:", roam );
	//this.ddt._ddtDumpNode( "enter-paragraphs.js", "606", "processRng(): bottom with neighbour:", neighbour );

  // Return the nearest relative, relative insertion point and fragment to insert

  return new Array(neighbour, insertion, roam);

	};	// end of processRng()

// ----------------------------------------------------------------------------------

/**
* are we an <li> that should be handled by the browser?
*
* there is no good way to "get out of" ordered or unordered lists from Javascript.
* We have to pass the onKeyPress 13 event to the browser so it can take care of
* getting us "out of" the list.
*
* The Gecko engine does a good job of handling all the normal <li> cases except the "press
* enter at the first position" where we want a <p>&nbsp</p> inserted before the list. The
* built-in behavior is to open up a <li> before the current entry (not good).
*
* @param rng Range range.
*/

EnterParagraphs.prototype.isNormalListItem = function(rng)
	{

	//this.ddt._ddtDumpNode( "enter-paragraphs.js", "863", "isNormaListItem(): checking rng for list end:", rng );

	var node, listNode;

	node = rng.startContainer;

	if (( typeof node.nodeName != 'undefined') &&
		( node.nodeName.toLowerCase() == 'li' ))
		{

		//this.ddt._ddt( "enter-paragraphs.js", "863", "isNormaListItem(): node is a list item");

		// are we a list item?

		listNode = node;
		}
	else if (( typeof node.parentNode != 'undefined' ) &&
				( typeof node.parentNode.nodeName != 'undefined' ) &&
				( node.parentNode.nodeName.toLowerCase() == 'li' ))
		{

		//this.ddt._ddt( "enter-paragraphs.js", "863", "isNormaListItem(): parent is a list item");

		// our parent is a list item.

		listNode = node.parentNode;

		}
	else
		{
		//this.ddt._ddt( "enter-paragraphs.js", "863", "isNormaListItem(): not list item");

		// neither we nor our parent are a list item. this is not a normal
		// li case.

		return false;
		}

	// at this point we have a listNode. Is it the first list item?

	if ( ! listNode.previousSibling )
		{
		//this.ddt._ddt( "enter-paragraphs.js", "839", "isNormaListItem(): we are the first li." );

		// are we on the first character of the first li?

		if ( rng.startOffset == 0 )
			{
			//this.ddt._ddt( "enter-paragraphs.js", "839", "isNormaListItem(): we are on the first character." );

			return false;
			}
		}

	//this.ddt._ddt( "enter-paragraphs.js", "839", "isNormaListItem(): this is a normal list item case." );
	return true;

	};	// end of isNormalListItem()

// ----------------------------------------------------------------------------------
/**
* Called when a key is pressed in the editor
*/

EnterParagraphs.prototype.__onKeyPress = function(ev)
	{

	//this.ddt._ddt( "enter-paragraphs.js", "517", "__onKeyPress(): top with keyCode '" + ev.keyCode + "'" );

  // If they've hit enter and shift is not pressed, handle it

  if (ev.keyCode == 13 && !ev.shiftKey && this.editor._iframe.contentWindow.getSelection)
		{
		//this.ddt._ddt( "enter-paragraphs.js", "517", "__onKeyPress(): calling handleEnter" );

    return this.handleEnter(ev);
		}

	//this.ddt._ddt( "enter-paragraphs.js", "517", "__onKeyPress(): bottom" );

	};	// end of _onKeyPress()

// -----------------------------------------------------------------------------------

/**
* Handles the pressing of an unshifted enter for Gecko
*/

EnterParagraphs.prototype.handleEnter = function(ev)
	{

	//this.ddt._ddt( "enter-paragraphs.js", "537", "handleEnter(): top" );

	var cursorNode;

  // Grab the selection and associated range

  var sel = this.editor._getSelection();
  var rng = this.editor._createRange(sel);

	//this.ddt._ddtDumpNode( "enter-paragraphs.js", "757", "handleEnter(): initial range is: ", rng );

	// if we are at the end of a list and the node is empty let the browser handle
	// it to get us out of the list.

	if ( this.isNormalListItem(rng) )
		{
		//this.ddt._ddt( "enter-paragraphs.js", "757", "handleEnter(): we are at the end of a list with a blank item. Letting the browser handle it." );
		return true;
		}

	// as far as I can tell this isn't actually used.

  this.takenIds = new Object();

  // Grab ranges for document re-stuffing, if appropriate
	//
	// pStart and pEnd are arrays consisting of
	// [0] neighbor node
	// [1] insertion type
	// [2] roam

	//this.ddt._ddt( "enter-paragraphs.js", "537", "handleEnter(): calling processSide on left side." );

  var pStart = this.processSide(rng, "left");

	//this.ddt._ddtDumpNode( "enter-paragraphs.js", "757", "handleEnter(): after processing left side range is: ", rng );

	//this.ddt._ddt( "enter-paragraphs.js", "537", "handleEnter(): calling processSide on right side." );

 	var pEnd = this.processSide(rng, "right");

	// used to position the cursor after insertion.

	cursorNode = pEnd[2];

  // Get rid of everything local to the selection

  sel.removeAllRanges();
  rng.deleteContents();

	// Grab a node we'll have after insertion, since fragments will be lost
	//
	// we'll use this to position the cursor.

	//this.ddt._ddt( "enter-paragraphs.js", "712", "handleEnter(): looking for cursor position" );

  var holdEnd = this.forEachNodeUnder( cursorNode, "find_cursorpoint", "ltr", false, true);

	if ( ! holdEnd )
		{
		alert( "INTERNAL ERROR - could not find place to put cursor after ENTER" );
		}

  // Insert our carefully chosen document fragments

  if ( pStart )
		{

		//this.ddt._ddt( "enter-paragraphs.js", "712", "handleEnter(): inserting pEnd" );

		this.insertAdjacentElement(pStart[0], pStart[1], pStart[2]);
		}

  if ( pEnd && pEnd.nodeType != 1)
		{

		//this.ddt._ddt( "enter-paragraphs.js", "712", "handleEnter(): inserting pEnd" );

		this.insertAdjacentElement(pEnd[0], pEnd[1], pEnd[2]);
		}

  // Move the caret in front of the first good text element

  if ((holdEnd) && (this._permEmpty.test(holdEnd.nodeName) ))
		{

		//this.ddt._ddt( "enter-paragraphs.js", "712", "handleEnter(): looping to find cursor element." );

    var prodigal = 0;
    while ( holdEnd.parentNode.childNodes.item(prodigal) != holdEnd )
			{
			prodigal++;
			}

    sel.collapse( holdEnd.parentNode, prodigal);
	  }
  else
		{

		// holdEnd might be false.

		try
			{
			sel.collapse(holdEnd, 0);

			//this.ddt._ddtDumpNode( "enter-paragraphs.js", "1057", "handleEnter(): scrolling to element:", holdEnd );

			// interestingly, scrollToElement() scroll so the top if holdEnd is a text node.

			if ( holdEnd.nodeType == 3 )
				{
				holdEnd = holdEnd.parentNode;
				}

		  this.editor.scrollToElement(holdEnd);
			}
		catch (e)
			{
			// we could try to place the cursor at the end of the document.
			}
		}

  this.editor.updateToolbar();

	HTMLArea._stopEvent(ev);

	return true;

	};	// end of handleEnter()

// END

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -