<commit_before>'use strict'

var SettingActions = require('../actions/SettingActions');
var SettingStore = require('../stores/SettingStore');
var PostActions = require('../actions/PostActions');
var DraftActions = require('../actions/DraftActions');
var EditActions = require('../actions/EditActions');
var EditStore = require('../stores/EditStore');
var NavBar = require('./NavBar');
var PostButton = require('./PostButton');
var CancelButton = require('./CancelButton');

var React = require('react-native');
var KeyboardEvents = require('react-native-keyboardevents');
var KeyboardEventEmitter = KeyboardEvents.Emitter;

var {
  StyleSheet,
  Text,
  TextInput,
  ScrollView,
  View,
} = React;

class EditPage extends React.Component {
  constructor(props, context) {
    super(props, context);
    if (!this.props.route.post) {
      this.initialData = '';
      this.state = {post: {data: ''}};
      this.isEditing = false;
    } else {
      this.initialData = this.props.route.post.data;
      this.state = {post: this.props.route.post};
      this.isEditing = true;
    }
    this.saveDraft = this.saveDraft.bind(this);
    this.cancelButton = (<CancelButton onPress={this.saveDraft} />);
    this.savePost = this.savePost.bind(this);
    this.postButton = (<PostButton edit={this.isEditing} isDraft={this.state.post.isDraft} onPress={this.savePost}/>);
    this.updateKeyboardSpace = (frames) => {
      var change;
      if (frames.endCoordinates) change = frames.endCoordinates.height;
      else change = frames.end.height;
      this.setState({height: this.state.height - change});
    };
    this.resetKeyboardSpace = () => this.setState({height: this.state.fullHeight});
    this.updateWordCount = (text) => this.setState({wordCount: text.split(/\s+/).filter(w => !!w).length});
  }

  componentDidMount() {
    this.updateWordCount(this.state.post.data);
    KeyboardEventEmitter.on(KeyboardEvents.KeyboardDidShowEvent, this.updateKeyboardSpace);
    KeyboardEventEmitter.on(KeyboardEvents.KeyboardWillHideEvent, this.resetKeyboardSpace);
  }

  componentWillUnmount() {
    KeyboardEventEmitter.off(KeyboardEvents.KeyboardDidShowEvent, this.updateKeyboardSpace);
    KeyboardEventEmitter.off(KeyboardEvents.KeyboardWillHideEvent, this.resetKeyboardSpace);
  }

  savePost() {
    if (this.isEditing && this.state.post.isDraft) {
      this.state.post.isDraft = false;
      DraftActions.del(this.state.post);
      delete this.state.post.id;
      PostActions.create(this.state.post);
    } else if (this.isEditing && !this.state.post.isDraft) {
      if (this.state.post.hasUnpublishedEdits) {
        this.state.post.hasUnpublishedEdits = false;
        EditActions.del(this.state.post);
      }
      PostActions.edit(this.state.post);
    } else {
      PostActions.create(this.state.post);
    }
    this.props.navigator.pop();
  }

  saveDraft() {
    if (this.state.post.data !== this.initialData) {
      if (this.state.post.isDraft) {
        DraftActions.edit(this.state.post);
      } else {
        if (this.isEditing) {
          this.state.post.hasUnpublishedEdits = true;
          console.log(this.state.post);
          EditActions.save(this.state.post);
        } else {
          this.state.post.isDraft = true;
          DraftActions.create(this.state.post);
        }
      }
    }
    this.props.navigator.pop();
  }

  render() {
    var title = <Text style={styles.title}>{this.state.wordCount} words</Text>;
    return (
      <View style={styles.page} onLayout={(ev) => {
        // 80 is for the navbar on top
        var fullHeight = ev.nativeEvent.layout.height - 80;
        this.setState({height: fullHeight, fullHeight: fullHeight});
      }}>
        <NavBar leftButton={this.cancelButton} title={title} rightButton={this.postButton}/>
        <ScrollView keyboardDismissMode='interactive'>
          <TextInput
            multiline={true}
            onChangeText={(text) => {
              this.state.post.data = text;
              this.updateWordCount(text);
            }}
            defaultValue={this.state.post.data}
            autoFocus={true}
            style={[styles.input, {height:this.state.height}]}
          />
        </ScrollView>
      </View>
    );
  }
}

var styles = StyleSheet.create({
  page: {
    flex: 1,
    alignItems: 'stretch',
  },
  input: {
    padding: 20,
    paddingTop: 0,
    fontSize: 18,
    fontFamily: 'System',
  },
  title: {
    paddingTop: 4,
    fontSize: 18,
    fontFamily: 'System',<commit_msg>show/hide word count based on whether keyboard is up or not<commit_after>'use strict'

var SettingActions = require('../actions/SettingActions');
var SettingStore = require('../stores/SettingStore');
var PostActions = require('../actions/PostActions');
var DraftActions = require('../actions/DraftActions');
var EditActions = require('../actions/EditActions');
var EditStore = require('../stores/EditStore');
var NavBar = require('./NavBar');
var PostButton = require('./PostButton');
var CancelButton = require('./CancelButton');

var React = require('react-native');
var KeyboardEvents = require('react-native-keyboardevents');
var KeyboardEventEmitter = KeyboardEvents.Emitter;

var {
  StyleSheet,
  Text,
  TextInput,
  ScrollView,
  View,
} = React;

class EditPage extends React.Component {
  constructor(props, context) {
    super(props, context);
    if (!this.props.route.post) {
      this.initialData = '';
      this.state = {post: {data: ''}};
      this.isEditing = false;
    } else {
      this.initialData = this.props.route.post.data;
      this.state = {post: this.props.route.post};
      this.isEditing = true;
    }
    this.saveDraft = this.saveDraft.bind(this);
    this.cancelButton = (<CancelButton onPress={this.saveDraft} />);
    this.savePost = this.savePost.bind(this);
    this.postButton = (<PostButton edit={this.isEditing} isDraft={this.state.post.isDraft} onPress={this.savePost}/>);
    this.updateKeyboardSpace = (frames) => {
      var change;
      if (frames.endCoordinates) change = frames.endCoordinates.height;
      else change = frames.end.height;
      this.setState({height: this.state.height - change});
      this.setState({isKeyboardUp: true})
    };
    this.resetKeyboardSpace = () => this.setState({height: this.state.fullHeight, isKeyboardUp: false});
    this.updateWordCount = (text) => this.setState({wordCount: text.split(/\s+/).filter(w => !!w).length});
  }

  componentDidMount() {
    this.updateWordCount(this.state.post.data);
    KeyboardEventEmitter.on(KeyboardEvents.KeyboardDidShowEvent, this.updateKeyboardSpace);
    KeyboardEventEmitter.on(KeyboardEvents.KeyboardWillHideEvent, this.resetKeyboardSpace);
  }

  componentWillUnmount() {
    KeyboardEventEmitter.off(KeyboardEvents.KeyboardDidShowEvent, this.updateKeyboardSpace);
    KeyboardEventEmitter.off(KeyboardEvents.KeyboardWillHideEvent, this.resetKeyboardSpace);
  }

  savePost() {
    if (this.isEditing && this.state.post.isDraft) {
      this.state.post.isDraft = false;
      DraftActions.del(this.state.post);
      delete this.state.post.id;
      PostActions.create(this.state.post);
    } else if (this.isEditing && !this.state.post.isDraft) {
      if (this.state.post.hasUnpublishedEdits) {
        this.state.post.hasUnpublishedEdits = false;
        EditActions.del(this.state.post);
      }
      PostActions.edit(this.state.post);
    } else {
      PostActions.create(this.state.post);
    }
    this.props.navigator.pop();
  }

  saveDraft() {
    if (this.state.post.data !== this.initialData) {
      if (this.state.post.isDraft) {
        DraftActions.edit(this.state.post);
      } else {
        if (this.isEditing) {
          this.state.post.hasUnpublishedEdits = true;
          console.log(this.state.post);
          EditActions.save(this.state.post);
        } else {
          this.state.post.isDraft = true;
          DraftActions.create(this.state.post);
        }
      }
    }
    this.props.navigator.pop();
  }

  render() {
    if (!this.state.isKeyboardUp) var title = <Text></Text>;
    else var title = <Text style={styles.title}>{this.state.wordCount} words</Text>;
    return (
      <View style={styles.page} onLayout={(ev) => {
        // 80 is for the navbar on top
        var fullHeight = ev.nativeEvent.layout.height - 80;
        this.setState({height: fullHeight, fullHeight: fullHeight});
      }}>
        <NavBar leftButton={this.cancelButton} title={title} rightButton={this.postButton}/>
        <ScrollView keyboardDismissMode='interactive'>
          <TextInput
            multiline={true}
            onChangeText={(text) => {
              this.state.post.data = text;
              this.updateWordCount(text);
            }}
            defaultValue={this.state.post.data}
            autoFocus={true}
            style={[styles.input, {height:this.state.height}]}
          />
        </ScrollView>
      </View>
    );
  }
}

var styles = StyleSheet.create({
  page: {
    flex: 1,
    alignItems: 'stretch',
  },
  input: {
    padding: 20,
    paddingTop: 0,
    fontSize: 18,
    fontFamily: 'System',
  },
  title: {
    paddingTop: 6,
    fontSize: 16,
    fontFamily: 'System',
{'fraction_non_alphanumeric': 0.14562683803507243, 'fraction_numerical': 0.0028319355190066444, 'mean_word_length': 2.966306695464363, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 37, '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': '78443', 'n_tokens_mistral': 2880, 'n_tokens_neox': 2877, 'n_words': 465}