thelounge/client/components/ChatInput.vue

139 lines
3 KiB
Vue
Raw Normal View History

2018-07-08 15:42:54 +02:00
<template>
<form
id="form"
method="post"
2018-07-08 17:17:20 +02:00
action=""
@submit.prevent="onSubmit">
2018-07-08 15:42:54 +02:00
<span id="nick">{{ network.nick }}</span>
<textarea
id="input"
2018-07-08 17:17:20 +02:00
ref="input"
2018-07-08 15:42:54 +02:00
v-model="channel.pendingMessage"
:placeholder="getInputPlaceholder(channel)"
:aria-label="getInputPlaceholder(channel)"
2018-07-08 19:53:23 +02:00
:disabled="!$root.connected"
2018-07-08 15:42:54 +02:00
class="mousetrap"
2018-07-10 09:45:54 +02:00
@keypress.enter.prevent="onSubmit"
2018-07-08 15:42:54 +02:00
/>
<span
2018-07-08 19:53:23 +02:00
v-if="$root.connected"
2018-07-08 15:42:54 +02:00
id="submit-tooltip"
class="tooltipped tooltipped-w tooltipped-no-touch"
aria-label="Send message">
<button
id="submit"
type="submit"
aria-label="Send message"/>
</span>
</form>
</template>
<script>
2018-07-08 17:17:20 +02:00
const $ = require("jquery");
const commands = require("../js/commands/index");
2018-07-08 17:17:20 +02:00
const socket = require("../js/socket");
2018-07-09 20:51:27 +02:00
const Mousetrap = require("mousetrap");
const {wrapCursor} = require("undate");
const colorsHotkeys = {
k: "\x03",
b: "\x02",
u: "\x1F",
i: "\x1D",
o: "\x0F",
s: "\x1e",
m: "\x11",
};
// Autocomplete bracket and quote characters like in a modern IDE
// For example, select `text`, press `[` key, and it becomes `[text]`
const bracketWraps = {
'"': '"',
"'": "'",
"(": ")",
"<": ">",
"[": "]",
"{": "}",
"*": "*",
"`": "`",
"~": "~",
"_": "_",
};
2018-07-08 17:17:20 +02:00
2018-07-08 15:42:54 +02:00
export default {
name: "ChatInput",
props: {
network: Object,
channel: Object,
},
2018-07-08 16:57:02 +02:00
mounted() {
if (this.$root.settings.autocomplete) {
2018-07-09 20:53:49 +02:00
require("../js/autocompletion").enable(this.$refs.input);
2018-07-08 16:57:02 +02:00
}
2018-07-09 20:51:27 +02:00
const inputTrap = Mousetrap(this.$refs.input);
for (const hotkey in colorsHotkeys) {
inputTrap.bind("mod+" + hotkey, function(e) {
// Key is lowercased because keybinds also get processed if caps lock is on
const modifier = colorsHotkeys[e.key.toLowerCase()];
wrapCursor(
e.target,
modifier,
e.target.selectionStart === e.target.selectionEnd ? "" : modifier
);
return false;
});
}
inputTrap.bind(Object.keys(bracketWraps), function(e) {
if (e.target.selectionStart !== e.target.selectionEnd) {
wrapCursor(e.target, e.key, bracketWraps[e.key]);
return false;
}
});
2018-07-08 16:57:02 +02:00
},
destroyed() {
require("../js/autocompletion").disable();
},
2018-07-08 15:42:54 +02:00
methods: {
getInputPlaceholder(channel) {
if (channel.type === "channel" || channel.type === "query") {
return `Write to ${channel.name}`;
}
return "";
},
2018-07-08 17:17:20 +02:00
onSubmit() {
// Triggering click event opens the virtual keyboard on mobile
// This can only be called from another interactive event (e.g. button click)
$(this.$refs.input).trigger("click").trigger("focus");
const target = this.channel.id;
2018-07-08 20:21:24 +02:00
const text = this.channel.pendingMessage;
2018-07-08 17:17:20 +02:00
if (text.length === 0) {
return false;
}
2018-07-08 20:21:24 +02:00
this.channel.pendingMessage = "";
2018-07-08 17:17:20 +02:00
// resetInputHeight(input.get(0));
if (text[0] === "/") {
2018-07-08 17:17:20 +02:00
const args = text.substr(1).split(" ");
const cmd = args.shift().toLowerCase();
if (commands[cmd] && commands[cmd].input(args)) {
2018-07-08 17:17:20 +02:00
return false;
}
}
socket.emit("input", {target, text});
},
2018-07-08 15:42:54 +02:00
},
};
</script>