{"size": 1766, "ext": "js", "max_stars_repo_path": "lib/amd-dependency-edit.js", "max_stars_repo_name": "fengzilong/atom-amd-dependency-edit", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/amd-dependency-edit.js", "max_issues_repo_name": "fengzilong/atom-amd-dependency-edit", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/amd-dependency-edit.js", "max_forks_repo_name": "fengzilong/atom-amd-dependency-edit", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.2285714286, "max_line_length": 69, "alphanum_fraction": 0.637599094}
'use babel';

import { CompositeDisposable } from 'atom';
import extract from './extract';
import patch from './patch';
import accept from './accept';

export default {
	subscriptions: null,

	activate( state ) {
		this.subscriptions = new CompositeDisposable();
		this.subscriptions.add( atom.commands.add( 'atom-workspace', {
			'amd-dependency-edit:toggle': () => this.toggle()
		} ) );
	},

	deactivate() {
		this.subscriptions.dispose();
	},

	serialize() {
	},

	toggle() {
		const sub = new CompositeDisposable();
		const amdPane = atom.workspace.getActivePane();
		const amdEditor = amdPane.activeItem;

		amdPane.splitRight();
		atom.workspace.open()
			.then(cmdEditor => {
				const content = amdEditor.getText();
				const extracted = extract( content );
				cmdEditor.setText( extracted );

				sub.add( amdEditor.onDidStopChanging( () => {
					console.log( 'left did change called' );
					const content = amdEditor.getText();
					const extracted = extract( content );

					const activeTextEditor = atom.workspace.getActiveTextEditor();

					// 如果改动来自激活的非当前窗口
					if( activeTextEditor && activeTextEditor.id !== cmdEditor.id ) {
						cmdEditor.setText( extracted );
					}
				} ) );

				sub.add( cmdEditor.onDidStopChanging( () => {
					console.log( 'right did change called' );
					const content = cmdEditor.getText();
					const p = patch( content );

					const activeTextEditor = atom.workspace.getActiveTextEditor();
					if( activeTextEditor && activeTextEditor.id !== amdEditor.id ) {
						const accepted = accept( amdEditor.getText(), p );
						if( !( accepted instanceof Error ) ) {
							amdEditor.setText( accepted + '' );
						}
					}
				} ) );

				cmdEditor.onDidDestroy( () => {
					sub.dispose();
				} );
			});
	}
};
