<commit_before>/* 
* Based on the tinyMCE core paragraph handling
* Released under LGPL License.
*
* License: http://tinymce.moxiecode.com/license
* Contributing: http://tinymce.moxiecode.com/contributing
*/

(function() {
	tinymce.create('tinymce.plugins.annoParagraphs', {		
		init : function(ed, url) {
			
			var t = this;
			t.editor = ed;

			ed.onKeyDown.addToTop(function(ed, e) {

				// If we're not hitting the shift key, we are hitting the return key, and we're not in a list (retain new list item functionality)
				if (!e.shiftKey && e.keyCode == 13) {
					if (!t.insertPara(e)) {
						e.preventDefault();

						ed.undoManager.add();
					}
					t._nodeChanged(ed);
					return false;
				}
				return true;
			});
			
			// Disable tab for everything except lists.
			ed.onKeyUp.addToTop(function(ed, e) {
				if (e.keyCode == 9 && ed.dom.getParent(ed.selection.getNode(), 'LIST-ITEM') == null) {
					e.preventDefault();
					return false;
				}
				
				return preventDefaultKey(ed, e);
			});
			
			function preventDefaultKey(ed, e) {
				var parent = ed.dom.getParent(ed.selection.getNode(), 'LIST, LIST-ITEM');
				if (!e.shiftKey && e.keyCode == 13 && !parent) {
					e.preventDefault();
					return false;
				}
				return true;
			}
			
			ed.onKeyPress.addToTop(function(ed, e) {
				return preventDefaultKey(ed, e);
			});
		},
		
		// Create a controlManager to hangle new nodes, the dispatchers for onKeyUp, onKeyPress etc.. does not get passed a CM
		createControl : function (ed, cm) {
			this.cm = cm;
		},
		
		// A new node is selected programtically, not be user. onNodeChange won't work, we need to add it to keypress.
		_nodeChanged : function (ed) {
			if (c = this.cm.get('annoformatselect')) {
				var parent = ed.dom.getParent(ed.selection.getNode(), 'HEADING, PARA, SEC'), selVal;
				if (parent) {
					selVal = parent.nodeName.toLowerCase();
				}
				else {
					selVal = 'format';
				}
				c.select(selVal);
			}
		},
		
		getParentBlock : function(n) {
			var t = this, ed = t.editor;
			return ed.dom.getParent(n, ed.dom.isBlock);
		},
		
		insertPara : function(e) {
			var t = this, ed = t.editor, dom = ed.dom, d = ed.getDoc(), se = ed.settings, s = ed.selection.getSel(), r = ed.selection.getRng(), b = d.body;
			var rb, ra, dir, sn, so, en, eo, sb, eb, bn, bef, aft, sc, ec, n, vp = dom.getViewPort(ed.getWin()), y, ch, car;
			var TRUE = true, FALSE = false, newElement, node = ed.selection.getNode();
			ed.undoManager.beforeChange();
			// Override default tinyMCE element.
			se.element = 'para';
			if (e.ctrlKey || /(BODY|HTML|HEADING|SEC)/.test(node.nodeName.toUpperCase())) {
				function insertNewBlock(node) {
					var newElement, parentNode;
					if (dom.getParent(node, 'PARA') !== null) {
						node = dom.getParent(node, 'PARA');
					}

					// If we're not in a section already, or this node is a section, insert a new section block
					if ((!(parentNode = dom.getParent(node, 'SEC')) || node.nodeName.toUpperCase() == 'SEC') && e.ctrlKey) {
						newElement = newSec();
					}
					else {
						newElement = dom.create('PARA');
					}
					// If we're not trying to insert a new section and we're in a section node, just return insert a paragraph at the cursor
					if (node.nodeName.toUpperCase() == 'SEC' && !e.ctrlKey) {
						// Inefficient mechanism to insert node at selection then select it, but tinyMCE offers no other method currently
						
						// Set an ID so it can be searched for later
						dom.setAttribs(newElement, { id: '_anno_inserted' });
						// Inser the node into at the current selection - note this does not return the dom node, just the node passed into it
						ed.selection.setNode(newElement);
						// Find the newly inserted node by ID
						newElement = dom.get('_anno_inserted');
						// Remove ID, so this process can run again
						dom.setAttribs(newElement, null);
						
						return newElement;
					}
					else {
						return dom.insertAfter(newElement, node);
					}
				}
				// Create a new sec element with a title
				function newSec() {
					var sec = dom.create('sec', null);
					dom.add(sec, 'heading', null, '&nbsp');
					dom.add(sec, 'para');
					return sec;
				}
				
				// Just insert a new paragraph if the ctrl key isn't held and the carat is in a para tag
				// Or, various tags should create paragraphs, not enter a br (when the ctrl key is held).
				if (/(DISP-FORMULA|TABLE-WRAP|FIG|DISP-QUOTE|HEADING)/.test(node.nodeName.toUpperCase())) {
					newElement = insertNewBlock(node);
				}
				else if (/(BODY|HTML)/.test(node.nodeName.toUpperCase())) {
					secElement = dom.add(node, 'sec');
					newElement = dom.add(secElement, 'heading', null, '&nbsp');
					dom.add(secElement, 'para');
				}
				else if (parentNode = dom.getParent(node, 'FIG')) {
					newElement = insertNewBlock(parentNode);
				}
				else if (parentNode = dom.getParent(node, 'TABLE-WRAP')) {					
					newElement = insertNewBlock(parentNode);
				}
				else if (parentNode = dom.getParent(node, 'SEC')) {
					newElement = insertNewBlock(parentNode);
				}
				else {
					newElement = insertNewBlock(node);
				}
				
				// Set new element as the first title tag, so we can select it
				if (newElement.nodeName.toLowerCase() == 'sec') {
					var eleArray = dom.select(' > heading', newElement);
					if (eleArray.length > 0) {
						newElement = eleArray[0];
					}
				}
				
				// Move caret to the freshly created item
				if (d.createRange) {     // all browsers, except IE before version 9
					r = d.createRange();
					r.selectNodeContents(newElement);
				}
				else { // IE < 9
					r = d.selection.createRange();
					r.moveToElementText(newElement);
				}

				r.collapse(1);
				ed.selection.setRng(r);
				ed.undoManager.add();
				return FALSE;
			}
			// Setup before range<commit_msg>beginning work on enter/ctrl-enter handling for textorum<commit_after>/* 
* Based on the tinyMCE core paragraph handling
* Released under LGPL License.
*
* License: http://tinymce.moxiecode.com/license
* Contributing: http://tinymce.moxiecode.com/contributing
*/

(function() {
	tinymce.create('tinymce.plugins.annoParagraphs', {		
		init : function(ed, url) {
			
			var t = this;
			t.editor = ed;
			t.helper = ed.plugins.textorum.helper;
			t.textorum = ed.plugins.textorum;
			
			ed.onKeyDown.addToTop(function(ed, e) {

				// If we're not hitting the shift key, we are hitting the return key, and we're not in a list (retain new list item functionality)
				if (!e.shiftKey && e.keyCode == 13) {
					if (!t.insertPara(e)) {
						e.preventDefault();

						ed.undoManager.add();
					}
					t._nodeChanged(ed);
					return false;
				}
				return true;
			});
			
			// Disable tab for everything except lists.
			ed.onKeyUp.addToTop(function(ed, e) {
				if (e.keyCode == 9 && ed.dom.getParent(ed.selection.getNode(), 'LIST-ITEM') == null) {
					e.preventDefault();
					return false;
				}
				
				return preventDefaultKey(ed, e);
			});
			
			function preventDefaultKey(ed, e) {
				var parent = ed.dom.getParent(ed.selection.getNode(), 'LIST, LIST-ITEM');
				if (!e.shiftKey && e.keyCode == 13 && !parent) {
					e.preventDefault();
					return false;
				}
				return true;
			}
			
			ed.onKeyPress.addToTop(function(ed, e) {
				return preventDefaultKey(ed, e);
			});
		},
		
		// Create a controlManager to hangle new nodes, the dispatchers for onKeyUp, onKeyPress etc.. does not get passed a CM
		createControl : function (ed, cm) {
			this.cm = cm;
		},
		
		// A new node is selected programtically, not be user. onNodeChange won't work, we need to add it to keypress.
		_nodeChanged : function (ed) {
			if (c = this.cm.get('annoformatselect')) {
				var parent = ed.dom.getParent(ed.selection.getNode(), 'HEADING, PARA, SEC'), selVal;
				if (parent) {
					selVal = parent.nodeName.toLowerCase();
				}
				else {
					selVal = 'format';
				}
				c.select(selVal);
			}
		},
		
		getParentBlock : function(n) {
			var t = this, ed = t.editor;
			return ed.dom.getParent(n, ed.dom.isBlock);
		},
		
		insertPara : function(e) {
			var t = this, ed = t.editor, dom = ed.dom, d = ed.getDoc(), se = ed.settings, s = ed.selection.getSel(), r = ed.selection.getRng(), b = d.body;
			var rb, ra, dir, sn, so, en, eo, sb, eb, bn, bef, aft, sc, ec, n, vp = dom.getViewPort(ed.getWin()), y, ch, car;
			var TRUE = true, FALSE = false, newElement, node = ed.selection.getNode();
			ed.undoManager.beforeChange();
			// Override default tinyMCE element.
			se.element = 'para';
			if (e.ctrlKey || /(BODY|HTML|HEADING|SEC)/.test(t.helper.getLocalName(node).toUpperCase())) {
				function insertNewBlock(node) {
					var newElement, parentNode;
					if (dom.getParent(node, t.helper.testNameIs('p')) !== null) {
						node = dom.getParent(node, t.helper.testNameIs('p'));
					}

					// If we're not in a section already, or this node is a section, insert a new section block
					if ((!(parentNode = dom.getParent(node, t.helper.testNameIs('sec'))) || t.helper.getLocalName(node).toUpperCase() == 'SEC') && e.ctrlKey) {
						newElement = newSec();
					}
					else {
						newElement = dom.create(t.textorum.translateElement('p'), {'class': 'p', 'data-xmlel': 'p'});
					}
					// If we're not trying to insert a new section and we're in a section node, just return insert a paragraph at the cursor
					if (t.helper.getLocalName(node).toUpperCase() == 'SEC' && !e.ctrlKey) {
						// Inefficient mechanism to insert node at selection then select it, but tinyMCE offers no other method currently
						
						// Set an ID so it can be searched for later
						dom.setAttribs(newElement, { id: '_anno_inserted' });
						// Inser the node into at the current selection - note this does not return the dom node, just the node passed into it
						ed.selection.setNode(newElement);
						// Find the newly inserted node by ID
						newElement = dom.get('_anno_inserted');
						// Remove ID, so this process can run again
						dom.setAttrib(newElement, 'id', null);
						
						return newElement;
					}
					else {
						return dom.insertAfter(newElement, node);
					}
				}
				// Create a new sec element with a title
				function newSec() {
					var sec = dom.create(t.textorum.translateElement('sec'), {'class': 'sec', 'data-xmlel': 'sec'});
					dom.add(sec, t.textorum.translateElement('title'), {'class': 'title', 'data-xmlel': 'title'}, '&nbsp');
					dom.add(sec, t.textorum.translateElement('p'), {'class': 'p', 'data-xmlel': 'p'}, '&nbsp');
					return sec;
				}
				
				// Just insert a new paragraph if the ctrl key isn't held and the carat is in a para tag
				// Or, various tags should create paragraphs, not enter a br (when the ctrl key is held).
				if (/(DISP-FORMULA|TABLE-WRAP|FIG|DISP-QUOTE|HEADING)/.test(t.helper.getLocalName(node).toUpperCase())) {
					newElement = insertNewBlock(node);
				}
				else if (/(BODY|HTML)/.test(t.helper.getLocalName(node).toUpperCase())) {
					secElement = dom.add(node, t.textorum.translateElement('sec'), {'class': 'sec', 'data-xmlel': 'sec'});
					newElement = dom.add(secElement, t.textorum.translateElement('title'), {'class': 'title', 'data-xmlel': 'title'}, '&nbsp');
					dom.add(secElement, t.textorum.translateElement('p'), {'class': 'p', 'data-xmlel': 'p'}, '&nbsp');
				}
				else if (parentNode = dom.getParent(node, 'FIG')) {
					newElement = insertNewBlock(parentNode);
				}
				else if (parentNode = dom.getParent(node, 'TABLE-WRAP')) {					
					newElement = insertNewBlock(parentNode);
				}
				else if (parentNode = dom.getParent(node, t.helper.testNameIs('sec'))) {
					newElement = insertNewBlock(parentNode);
				}
				else {
					newElement = insertNewBlock(node);
				}
				
				// Set new element as the first title tag, so we can select it
				if (newElement.nodeName.toLowerCase() == 'sec') {
					var eleArray = dom.select(' > heading', newElement);
					if (eleArray.length > 0) {
						newElement = eleArray[0];
					}
				}
				
				// Move caret to the freshly created item
				if (d.createRange) {     // all browsers, except IE before version 9
					r = d.createRange();
					r.selectNodeContents(newElement);
				}
				else { // IE < 9
					r = d.selection.createRange();
					r.moveToElementText(newElement);
				}

				r.collapse(1);
				ed.selection.setRng(r);
				ed.undoManager.add();
				return FALSE;
			}
			// Setup before range
{'fraction_non_alphanumeric': 0.14380298748486073, 'fraction_numerical': 0.0016148566814695195, 'mean_word_length': 2.8099046447247003, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2368870', 'n_tokens_mistral': 5128, 'n_tokens_neox': 4057, 'n_words': 1127}