<commit_before>// @flow

import ms from 'ms';
import socket from './socket';
import messageSchema from '../schemas/message';
import state from './state';

const Message = {
  props: ['message'],
  data() {
    return {title: ''};
  },
  template: `
    <div v-on:mouseover="fromNow" v-bind:title="title">
      <div v-if="message.raw">
        <span>{{message.text}}</span>
      </div>
      <div v-else-if="message.html">
        <span v-html="message.text"></span>
      </div>
      <div v-else>
        <span class="nav-link font-weight-bold" v-bind:style="'color: ' + message.hashColor">{{message.username}}: </span>
        <span v-html="message.text"></span>
      </div>
    </div>
  `,
  methods: {
    fromNow() {
      this.title = ms(Date.now() - this.message.date, { long: true }) + ' ago';
    }
  }
};

const MessageList = {
  props: ['messages'],
  components: {
    Message
  },
  template: `
    <div class="message-list">
      <div v-for="message in messages">
        <Message v-bind:message="message" />
      </div>
    </div>
  `,
  updated() {
    this.$el.scrollTop = this.$el.scrollHeight;
  }
};

const Chat = {
  props: ['messageList'],
  data() {
    return {
      message: ''
    };
  },
  components: {
    MessageList
  },
  template: `
    <div class="col-sm-10">
      <MessageList v-bind:messages="messageList" />
      <input
        type="text"
        class="form-control"
        v-model="message"
        v-on:keyup.enter="createMessage"
      />
    </div>
  `,
  methods: {
    createMessage() {
      const message = this.message.trim();
      const roomName = this.$route.params.id || this.$route.name;
      if (!state.username) {
        state.rooms.lobby.log.push({raw: true, text: 'Choose a username to chat.'});
        this.message = '';
        return console.error('Must have username to chat.');
      }
      if (!message) return console.error('Message cannot be blank.');
      if (message.length > 300) return;
      socket.emit('chat message', messageSchema.encode({
        username: state.username,
        room: roomName,
        text: message
      }));
      this.message = '';
    }
  }
};

module.exports = Chat;<commit_msg>Add chat history with keyup and down<commit_after>// @flow

import ms from 'ms';
import socket from './socket';
import messageSchema from '../schemas/message';
import state from './state';

const Message = {
  props: ['message'],
  data() {
    return {title: ''};
  },
  template: `
    <div v-on:mouseover="fromNow" v-bind:title="title">
      <div v-if="message.raw">
        <span>{{message.text}}</span>
      </div>
      <div v-else-if="message.html">
        <span v-html="message.text"></span>
      </div>
      <div v-else>
        <span class="nav-link font-weight-bold" v-bind:style="'color: ' + message.hashColor">{{message.username}}: </span>
        <span v-html="message.text"></span>
      </div>
    </div>
  `,
  methods: {
    fromNow() {
      this.title = ms(Date.now() - this.message.date, { long: true }) + ' ago';
    }
  }
};

const MessageList = {
  props: ['messages'],
  components: {
    Message
  },
  template: `
    <div class="message-list">
      <div v-for="message in messages">
        <Message v-bind:message="message" />
      </div>
    </div>
  `,
  updated() {
    this.$el.scrollTop = this.$el.scrollHeight;
  }
};

const Chat = {
  props: ['messageList'],
  data() {
    return {
      message: '',
      negativeIndex: 1
    };
  },
  components: {
    MessageList
  },
  template: `
    <div class="col-sm-10">
      <MessageList v-bind:messages="messageList" />
      <input
        type="text"
        class="form-control"
        v-model="message"
        v-on:keyup.enter="createMessage"
        v-on:keyup.up="pastMessage(1)"
        v-on:keyup.down="pastMessage(-1)"
      />
    </div>
  `,
  methods: {
    createMessage() {
      const message = this.message.trim();
      const roomName = this.$route.params.id || this.$route.name;
      if (!state.username) {
        state.rooms.lobby.log.push({raw: true, text: 'Choose a username to chat.'});
        this.message = '';
        return console.error('Must have username to chat.');
      }
      if (!message) return console.error('Message cannot be blank.');
      if (message.length > 300) return;
      socket.emit('chat message', messageSchema.encode({
        username: state.username,
        room: roomName,
        text: message
      }));
      this.message = '';
      this.negativeIndex = 1;
    },
    pastMessage(num /*: number */) {
      const filtered = this.messageList.filter(m => m.username === state.username);
      const index = filtered.length - this.negativeIndex;
      const message = filtered[index];
      if (!message) return;
      this.message = message.text;
      this.negativeIndex += num;
    }
  }
};

module.exports = Chat;
{'fraction_non_alphanumeric': 0.1765068915860934, 'fraction_numerical': 0.002880065830076116, 'mean_word_length': 2.336993822923816, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 56, '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': '2794587', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1552, 'n_words': 328}