<commit_before>var TextOptions = require('./text-options.jsx');

var styles = {
	composeArea: {
		padding: '0% 20%'
	},
	labelPadding: {
		padding: '5px 0px'
	}
}

function getParentNodes(element) {
	var nodeNames = {};
	while (element.parentNode) {
		nodeNames[element.nodeName] = true;
		element = element.parentNode;
	}
	return nodeNames;
}

function hasNode(nodeList, name) {
	return nodeList[name];
}

module.exports = React.createClass({
	getInitialState: function() {
		return {
			text: '',
			url: chrome.extension.getBackgroundPage().url,
			textOptions: {bold: false, italic: false, posLeft: -999, posTop: -999}
		}
	},

	/*
	Check if the currently active URL in the main chrome window has notes in storage
	*/
	componentDidMount: function() {
		var self = this;
		chrome.storage.sync.get(this.state.url, function(val) {
			if (Object.keys(val).length === 0) {
				// not in storage --> show blank textarea
				console.dir('not in storage');
			} else {
				// in storage --> show saved note
				console.dir('in storage!');
				console.dir(val);
				// self.setState({text: val[self.state.url]});
				var articleEl = React.findDOMNode(self.refs.content);
				articleEl.innerHTML = val[self.state.url];
			}
		});

		this.setupEventBindings();
	},

	setupEventBindings: function() {
		document.onmouseup = this.checkTextHighlighting;
		document.onkeyup = this.saveContent;
	},

	checkTextHighlighting: function(e) {
		console.log('text highlighted');
		var selection = window.getSelection();
		if (selection.isCollapsed) {
			this.setState({textOptions: {
				bold: false,
				italic: false,
				posLeft: -999,
				posTop: -999
			}});
		} else {
			nodeList = getParentNodes(selection.focusNode);
			if (hasNode(nodeList, 'ARTICLE')) {
				// Update state which is passed as props to text-options component
				var b = selection.getRangeAt(0).getBoundingClientRect();
				this.setState({textOptions: {
					bold: hasNode(nodeList, 'B'),
					italic: hasNode(nodeList, 'I'),
					posLeft: (b.left + b.right) / 2,
					posTop: b.top - 5 + window.pageYOffset
				}});<commit_msg>handling onmouseclick on next tick allows old text selection to clear<commit_after>var TextOptions = require('./text-options.jsx');

var styles = {
	composeArea: {
		padding: '0% 20%'
	},
	labelPadding: {
		padding: '5px 0px'
	}
}

function getParentNodes(element) {
	var nodeNames = {};
	while (element.parentNode) {
		nodeNames[element.nodeName] = true;
		element = element.parentNode;
	}
	return nodeNames;
}

function hasNode(nodeList, name) {
	return nodeList[name];
}

module.exports = React.createClass({
	getInitialState: function() {
		return {
			text: '',
			url: chrome.extension.getBackgroundPage().url,
			textOptions: {bold: false, italic: false, posLeft: -999, posTop: -999}
		}
	},

	/*
	Check if the currently active URL in the main chrome window has notes in storage
	*/
	componentDidMount: function() {
		var self = this;
		chrome.storage.sync.get(this.state.url, function(val) {
			if (Object.keys(val).length === 0) {
				// not in storage --> show blank textarea
				console.dir('not in storage');
			} else {
				// in storage --> show saved note
				console.dir('in storage!');
				console.dir(val);
				// self.setState({text: val[self.state.url]});
				var articleEl = React.findDOMNode(self.refs.content);
				articleEl.innerHTML = val[self.state.url];
			}
		});

		this.setupEventBindings();
	},

	setupEventBindings: function() {
		var self = this;
		document.onmouseup = function(e) {
			setTimeout(function(e) {
				self.checkTextHighlighting(e);
			}, 1);
		}
		document.onkeyup = this.saveContent;
	},

	checkTextHighlighting: function(e) {
		console.log('text highlighted');
		var selection = window.getSelection();
		if (selection.isCollapsed) {
			this.setState({textOptions: {
				bold: false,
				italic: false,
				posLeft: -999,
				posTop: -999
			}});
		} else {
			nodeList = getParentNodes(selection.focusNode);
			if (hasNode(nodeList, 'ARTICLE')) {
				// Update state which is passed as props to text-options component
				var b = selection.getRangeAt(0).getBoundingClientRect();
				this.setState({textOptions: {
					bold: hasNode(nodeList, 'B'),
					italic: hasNode(nodeList, 'I'),
					posLeft: (b.left + b.right) / 2,
					posTop: b.top - 5 + window.pageYOffset
				}});
{'fraction_non_alphanumeric': 0.1456400742115028, 'fraction_numerical': 0.009972170686456401, 'mean_word_length': 3.502087682672234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2845894', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1434, 'n_words': 328}