2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 01:26:16 +01:00
|
|
|
import emoji_codes from "../generated/emoji/emoji_codes.json";
|
|
|
|
import * as typeahead from "../shared/js/typeahead";
|
|
|
|
import render_emoji_popover from "../templates/emoji_popover.hbs";
|
|
|
|
import render_emoji_popover_content from "../templates/emoji_popover_content.hbs";
|
|
|
|
import render_emoji_popover_search_results from "../templates/emoji_popover_search_results.hbs";
|
|
|
|
import render_emoji_showcase from "../templates/emoji_showcase.hbs";
|
|
|
|
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 01:26:48 +01:00
|
|
|
import * as compose_ui from "./compose_ui";
|
2022-03-25 14:47:51 +01:00
|
|
|
import * as emoji from "./emoji";
|
2021-03-30 02:21:21 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2021-02-28 01:26:16 +01:00
|
|
|
import * as message_store from "./message_store";
|
2022-04-29 21:39:12 +02:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 01:26:16 +01:00
|
|
|
import * as popovers from "./popovers";
|
|
|
|
import * as reactions from "./reactions";
|
|
|
|
import * as rows from "./rows";
|
2022-04-29 21:39:12 +02:00
|
|
|
import * as spectators from "./spectators";
|
2021-02-28 21:33:10 +01:00
|
|
|
import * as ui from "./ui";
|
2021-07-28 16:00:58 +02:00
|
|
|
import {user_settings} from "./user_settings";
|
2021-06-27 21:24:11 +02:00
|
|
|
import * as user_status_ui from "./user_status_ui";
|
2021-02-28 00:42:30 +01:00
|
|
|
|
2017-08-25 19:59:23 +02:00
|
|
|
// Emoji picker is of fixed width and height. Update these
|
|
|
|
// whenever these values are changed in `reactions.css`.
|
2019-11-02 00:06:25 +01:00
|
|
|
const APPROX_HEIGHT = 375;
|
|
|
|
const APPROX_WIDTH = 255;
|
2017-08-25 19:59:23 +02:00
|
|
|
|
2017-04-28 22:26:22 +02:00
|
|
|
// The functionalities for reacting to a message with an emoji
|
|
|
|
// and composing a message with an emoji share a single widget,
|
|
|
|
// implemented as the emoji_popover.
|
2021-02-28 01:26:16 +01:00
|
|
|
export let complete_emoji_catalog = [];
|
2018-08-04 14:44:16 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
let $current_message_emoji_popover_elem;
|
2019-11-02 00:06:25 +01:00
|
|
|
let emoji_catalog_last_coordinates = {
|
2017-07-19 00:28:56 +02:00
|
|
|
section: 0,
|
|
|
|
index: 0,
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
let current_section = 0;
|
|
|
|
let current_index = 0;
|
|
|
|
let search_is_active = false;
|
|
|
|
const search_results = [];
|
|
|
|
let section_head_offsets = [];
|
|
|
|
let edit_message_id = null;
|
2017-07-19 00:28:56 +02:00
|
|
|
|
2020-07-08 09:14:41 +02:00
|
|
|
const EMOJI_CATEGORIES = [
|
2020-07-16 22:40:18 +02:00
|
|
|
{name: "Popular", icon: "fa-star-o"},
|
|
|
|
{name: "Smileys & Emotion", icon: "fa-smile-o"},
|
|
|
|
{name: "People & Body", icon: "fa-thumbs-o-up"},
|
|
|
|
{name: "Animals & Nature", icon: "fa-leaf"},
|
|
|
|
{name: "Food & Drink", icon: "fa-cutlery"},
|
|
|
|
{name: "Activities", icon: "fa-soccer-ball-o"},
|
|
|
|
{name: "Travel & Places", icon: "fa-car"},
|
|
|
|
{name: "Objects", icon: "fa-lightbulb-o"},
|
|
|
|
{name: "Symbols", icon: "fa-hashtag"},
|
|
|
|
{name: "Flags", icon: "fa-flag"},
|
|
|
|
{name: "Custom", icon: "fa-cog"},
|
2020-07-08 09:14:41 +02:00
|
|
|
];
|
2017-07-19 00:28:56 +02:00
|
|
|
|
|
|
|
function get_total_sections() {
|
|
|
|
if (search_is_active) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-02-28 01:26:16 +01:00
|
|
|
return complete_emoji_catalog.length;
|
2017-07-19 00:28:56 +02:00
|
|
|
}
|
2017-04-27 07:27:25 +02:00
|
|
|
|
2017-07-19 00:28:56 +02:00
|
|
|
function get_max_index(section) {
|
|
|
|
if (search_is_active) {
|
|
|
|
return search_results.length;
|
|
|
|
} else if (section >= 0 && section < get_total_sections()) {
|
2021-02-28 01:26:16 +01:00
|
|
|
return complete_emoji_catalog[section].emojis.length;
|
2017-04-27 07:27:25 +02:00
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-07-19 00:28:56 +02:00
|
|
|
}
|
2017-04-27 07:27:25 +02:00
|
|
|
|
2017-10-05 15:47:49 +02:00
|
|
|
function get_emoji_id(section, index) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let type = "emoji_picker_emoji";
|
2017-10-05 15:47:49 +02:00
|
|
|
if (search_is_active) {
|
|
|
|
type = "emoji_search_result";
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const emoji_id = [type, section, index].join(",");
|
2017-10-05 15:47:49 +02:00
|
|
|
return emoji_id;
|
|
|
|
}
|
|
|
|
|
2017-10-05 16:22:16 +02:00
|
|
|
function get_emoji_coordinates(emoji_id) {
|
2017-12-14 19:51:17 +01:00
|
|
|
// Emoji id is of the following form:
|
|
|
|
// <emoji_type>_<section_number>_<index>.
|
|
|
|
// See `get_emoji_id()`.
|
2019-11-02 00:06:25 +01:00
|
|
|
const emoji_info = emoji_id.split(",");
|
2017-10-05 16:22:16 +02:00
|
|
|
return {
|
2020-10-07 09:17:30 +02:00
|
|
|
section: Number.parseInt(emoji_info[1], 10),
|
|
|
|
index: Number.parseInt(emoji_info[2], 10),
|
2017-10-05 16:22:16 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:28:56 +02:00
|
|
|
function show_search_results() {
|
|
|
|
$(".emoji-popover-emoji-map").hide();
|
|
|
|
$(".emoji-popover-category-tabs").hide();
|
|
|
|
$(".emoji-search-results-container").show();
|
|
|
|
emoji_catalog_last_coordinates = {
|
|
|
|
section: current_section,
|
|
|
|
index: current_index,
|
|
|
|
};
|
|
|
|
current_section = 0;
|
|
|
|
current_index = 0;
|
|
|
|
search_is_active = true;
|
2017-04-27 07:27:25 +02:00
|
|
|
}
|
|
|
|
|
2017-07-19 00:28:56 +02:00
|
|
|
function show_emoji_catalog() {
|
|
|
|
$(".emoji-popover-emoji-map").show();
|
|
|
|
$(".emoji-popover-category-tabs").show();
|
|
|
|
$(".emoji-search-results-container").hide();
|
|
|
|
current_section = emoji_catalog_last_coordinates.section;
|
|
|
|
current_index = emoji_catalog_last_coordinates.index;
|
|
|
|
search_is_active = false;
|
|
|
|
}
|
2017-04-29 09:46:31 +02:00
|
|
|
|
2021-02-28 01:26:16 +01:00
|
|
|
export function rebuild_catalog() {
|
2020-07-25 16:11:28 +02:00
|
|
|
const realm_emojis = emoji.active_realm_emojis;
|
|
|
|
|
2020-02-12 06:22:45 +01:00
|
|
|
const catalog = new Map();
|
2020-07-15 00:34:28 +02:00
|
|
|
catalog.set(
|
|
|
|
"Custom",
|
|
|
|
Array.from(realm_emojis.keys(), (realm_emoji_name) =>
|
|
|
|
emoji.emojis_by_name.get(realm_emoji_name),
|
|
|
|
),
|
|
|
|
);
|
2017-05-01 00:04:51 +02:00
|
|
|
|
2020-02-06 02:16:33 +01:00
|
|
|
for (const [category, codepoints] of Object.entries(emoji_codes.emoji_catalog)) {
|
2020-02-12 06:22:45 +01:00
|
|
|
const emojis = [];
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const codepoint of codepoints) {
|
2020-05-27 04:17:29 +02:00
|
|
|
const name = emoji.get_emoji_name(codepoint);
|
|
|
|
if (name !== undefined) {
|
|
|
|
const emoji_dict = emoji.emojis_by_name.get(name);
|
2020-02-06 00:04:34 +01:00
|
|
|
if (emoji_dict !== undefined && emoji_dict.is_realm_emoji !== true) {
|
2020-02-12 06:22:45 +01:00
|
|
|
emojis.push(emoji_dict);
|
2017-07-19 00:28:56 +02:00
|
|
|
}
|
2017-05-23 15:54:01 +02:00
|
|
|
}
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2020-02-12 06:22:45 +01:00
|
|
|
catalog.set(category, emojis);
|
2020-02-06 02:16:33 +01:00
|
|
|
}
|
2017-04-29 09:46:31 +02:00
|
|
|
|
2020-02-12 06:22:45 +01:00
|
|
|
const popular = [];
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const codepoint of typeahead.popular_emojis) {
|
2020-05-27 04:17:29 +02:00
|
|
|
const name = emoji.get_emoji_name(codepoint);
|
|
|
|
if (name !== undefined) {
|
|
|
|
const emoji_dict = emoji.emojis_by_name.get(name);
|
2020-02-06 00:04:34 +01:00
|
|
|
if (emoji_dict !== undefined) {
|
2020-02-12 06:22:45 +01:00
|
|
|
popular.push(emoji_dict);
|
2017-07-19 00:28:56 +02:00
|
|
|
}
|
2017-04-29 09:46:31 +02:00
|
|
|
}
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2020-02-12 06:22:45 +01:00
|
|
|
catalog.set("Popular", popular);
|
|
|
|
|
2020-07-08 09:14:41 +02:00
|
|
|
const categories = EMOJI_CATEGORIES.filter((category) => catalog.has(category.name));
|
2021-02-28 01:26:16 +01:00
|
|
|
complete_emoji_catalog = categories.map((category) => ({
|
2020-02-12 06:22:45 +01:00
|
|
|
name: category.name,
|
|
|
|
icon: category.icon,
|
|
|
|
emojis: catalog.get(category.name),
|
|
|
|
}));
|
2021-02-28 01:26:16 +01:00
|
|
|
}
|
2017-04-29 09:46:31 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const generate_emoji_picker_content = function (id) {
|
|
|
|
let emojis_used = [];
|
2017-04-29 09:46:31 +02:00
|
|
|
|
2017-07-19 00:28:56 +02:00
|
|
|
if (id !== undefined) {
|
|
|
|
emojis_used = reactions.get_emojis_used_by_user_for_message_id(id);
|
2017-06-02 17:31:06 +02:00
|
|
|
}
|
2020-02-06 00:04:34 +01:00
|
|
|
for (const emoji_dict of emoji.emojis_by_name.values()) {
|
2020-07-02 01:39:34 +02:00
|
|
|
emoji_dict.has_reacted = emoji_dict.aliases.some((alias) => emojis_used.includes(alias));
|
2020-02-06 00:04:34 +01:00
|
|
|
}
|
2017-06-02 17:31:06 +02:00
|
|
|
|
2019-07-09 21:24:00 +02:00
|
|
|
return render_emoji_popover_content({
|
2017-07-19 00:28:56 +02:00
|
|
|
message_id: id,
|
2021-02-28 01:26:16 +01:00
|
|
|
emoji_categories: complete_emoji_catalog,
|
2021-06-27 21:24:11 +02:00
|
|
|
is_status_emoji_popover: user_status_ui.user_status_picker_open(),
|
2017-07-19 00:28:56 +02:00
|
|
|
});
|
|
|
|
};
|
2017-04-29 09:46:31 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
function refill_section_head_offsets($popover) {
|
2017-08-20 12:24:00 +02:00
|
|
|
section_head_offsets = [];
|
2022-01-25 11:36:19 +01:00
|
|
|
$popover.find(".emoji-popover-subheading").each(function () {
|
2017-08-20 12:24:00 +02:00
|
|
|
section_head_offsets.push({
|
2020-07-15 01:29:15 +02:00
|
|
|
section: $(this).attr("data-section"),
|
2017-08-20 12:24:00 +02:00
|
|
|
position_y: $(this).position().top,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:26:16 +01:00
|
|
|
export function reactions_popped() {
|
2022-01-25 11:36:19 +01:00
|
|
|
return $current_message_emoji_popover_elem !== undefined;
|
2021-02-28 01:26:16 +01:00
|
|
|
}
|
2017-04-27 07:27:25 +02:00
|
|
|
|
2021-02-28 01:26:16 +01:00
|
|
|
export function hide_emoji_popover() {
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".has_popover").removeClass("has_popover has_emoji_popover");
|
2021-06-28 10:02:45 +02:00
|
|
|
if (user_status_ui.user_status_picker_open()) {
|
|
|
|
// Re-enable clicking events for other elements after closing
|
|
|
|
// the popover. This is the inverse of the hack of in the
|
|
|
|
// handler that opens the "user status modal" emoji picker.
|
2021-07-07 09:16:19 +02:00
|
|
|
$(".app, .header, .modal__overlay, #set_user_status_modal").css("pointer-events", "all");
|
2021-06-28 10:02:45 +02:00
|
|
|
}
|
2021-02-28 01:26:16 +01:00
|
|
|
if (reactions_popped()) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$current_message_emoji_popover_elem.popover("destroy");
|
|
|
|
$current_message_emoji_popover_elem.removeClass("reaction_button_visible");
|
|
|
|
$current_message_emoji_popover_elem = undefined;
|
2017-04-27 07:27:25 +02:00
|
|
|
}
|
2021-02-28 01:26:16 +01:00
|
|
|
}
|
2017-04-27 07:27:25 +02:00
|
|
|
|
2017-05-30 00:50:45 +02:00
|
|
|
function get_selected_emoji() {
|
2020-10-07 11:38:36 +02:00
|
|
|
return $(".emoji-popover-emoji:focus")[0];
|
2017-05-30 00:50:45 +02:00
|
|
|
}
|
|
|
|
|
2017-07-19 00:28:56 +02:00
|
|
|
function get_rendered_emoji(section, index) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const emoji_id = get_emoji_id(section, index);
|
2022-01-25 11:36:19 +01:00
|
|
|
const $emoji = $(`.emoji-popover-emoji[data-emoji-id='${CSS.escape(emoji_id)}']`);
|
|
|
|
if ($emoji.length > 0) {
|
|
|
|
return $emoji;
|
2017-06-02 17:31:06 +02:00
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-06-02 17:31:06 +02:00
|
|
|
}
|
|
|
|
|
2022-04-30 19:52:53 +02:00
|
|
|
export function is_emoji_present_in_text(text, emoji_dict) {
|
|
|
|
// fetching emoji details to ensure emoji_code and reaction_type are present
|
|
|
|
const emoji_info = emoji.get_emoji_details_by_name(emoji_dict.name);
|
|
|
|
if (typeahead.is_unicode_emoji(emoji_info)) {
|
|
|
|
// convert emoji_dict to an actual emoji character
|
|
|
|
const parsed_emoji_code = typeahead.parse_unicode_emoji_code(emoji_info.emoji_code);
|
|
|
|
|
|
|
|
return text.includes(parsed_emoji_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-30 00:50:45 +02:00
|
|
|
function filter_emojis() {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elt = $(".emoji-popover-filter").expectOne();
|
|
|
|
const query = $elt.val().trim().toLowerCase();
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = $(".emoji-search-results-container").data("message-id");
|
|
|
|
const search_results_visible = $(".emoji-search-results-container").is(":visible");
|
2017-07-19 00:28:56 +02:00
|
|
|
if (query !== "") {
|
2021-02-28 01:26:16 +01:00
|
|
|
const categories = complete_emoji_catalog;
|
2019-11-02 00:06:25 +01:00
|
|
|
const search_terms = query.split(" ");
|
2019-10-26 00:28:42 +02:00
|
|
|
search_results.length = 0;
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
|
|
|
|
for (const category of categories) {
|
2017-07-19 00:28:56 +02:00
|
|
|
if (category.name === "Popular") {
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
continue;
|
2017-05-30 00:50:45 +02:00
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const emojis = category.emojis;
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const emoji_dict of emojis) {
|
2020-02-08 05:13:43 +01:00
|
|
|
for (const alias of emoji_dict.aliases) {
|
2020-07-02 01:39:34 +02:00
|
|
|
const match = search_terms.every((search_term) => alias.includes(search_term));
|
2017-08-24 23:04:24 +02:00
|
|
|
if (match) {
|
2020-07-16 22:40:18 +02:00
|
|
|
search_results.push({...emoji_dict, emoji_name: alias});
|
2020-07-16 23:29:01 +02:00
|
|
|
break; // We only need the first matching alias per emoji.
|
2017-08-24 23:04:24 +02:00
|
|
|
}
|
2020-02-08 05:13:43 +01:00
|
|
|
}
|
2022-04-30 19:52:53 +02:00
|
|
|
|
|
|
|
// using query instead of search_terms because it's possible multiple emojis were input
|
|
|
|
// without being separated by spaces
|
|
|
|
if (is_emoji_present_in_text(query, emoji_dict)) {
|
|
|
|
search_results.push({...emoji_dict, emoji_name: emoji_dict.name});
|
|
|
|
}
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 15:03:33 +02:00
|
|
|
const sorted_search_results = typeahead.sort_emojis(search_results, query);
|
2019-11-02 00:06:25 +01:00
|
|
|
const rendered_search_results = render_emoji_popover_search_results({
|
2020-07-08 15:03:33 +02:00
|
|
|
search_results: sorted_search_results,
|
2021-06-27 21:24:11 +02:00
|
|
|
is_status_emoji_popover: user_status_ui.user_status_picker_open(),
|
2020-07-20 22:18:43 +02:00
|
|
|
message_id,
|
2017-07-19 00:28:56 +02:00
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".emoji-search-results").html(rendered_search_results);
|
2019-01-09 14:30:35 +01:00
|
|
|
ui.reset_scrollbar($(".emoji-search-results-container"));
|
2017-07-19 00:28:56 +02:00
|
|
|
if (!search_results_visible) {
|
|
|
|
show_search_results();
|
2017-06-02 17:31:06 +02:00
|
|
|
}
|
2017-05-30 00:50:45 +02:00
|
|
|
} else {
|
2017-07-19 00:28:56 +02:00
|
|
|
show_emoji_catalog();
|
2017-05-30 00:50:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 22:55:45 +02:00
|
|
|
function toggle_reaction(emoji_name, event) {
|
2021-03-30 02:21:21 +02:00
|
|
|
const message_id = message_lists.current.selected_id();
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = message_store.get(message_id);
|
2017-08-27 11:45:40 +02:00
|
|
|
if (!message) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("reactions: Bad message id: " + message_id);
|
2017-08-27 11:45:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-06 22:55:45 +02:00
|
|
|
reactions.toggle_emoji_reaction(message_id, emoji_name, event);
|
|
|
|
|
2021-04-28 08:19:15 +02:00
|
|
|
if (!event.shiftKey) {
|
2021-02-28 01:26:16 +01:00
|
|
|
hide_emoji_popover();
|
2020-07-06 22:55:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$(event.target).closest(".reaction").toggleClass("reacted");
|
2017-08-27 11:45:40 +02:00
|
|
|
}
|
|
|
|
|
2020-07-08 08:02:16 +02:00
|
|
|
function is_composition(emoji) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return $(emoji).hasClass("composition");
|
2020-07-08 08:02:16 +02:00
|
|
|
}
|
|
|
|
|
2021-06-27 21:24:11 +02:00
|
|
|
function is_status_emoji(emoji) {
|
|
|
|
return $(emoji).hasClass("status_emoji");
|
|
|
|
}
|
|
|
|
|
2020-07-08 09:15:55 +02:00
|
|
|
function process_enter_while_filtering(e) {
|
2021-05-29 21:55:20 +02:00
|
|
|
if (e.key === "Enter") {
|
2017-05-30 00:50:45 +02:00
|
|
|
e.preventDefault();
|
2022-01-25 11:36:19 +01:00
|
|
|
const $first_emoji = get_rendered_emoji(0, 0);
|
|
|
|
if ($first_emoji) {
|
|
|
|
if (is_composition($first_emoji)) {
|
|
|
|
$first_emoji.trigger("click");
|
2017-05-30 00:50:45 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
toggle_reaction($first_emoji.attr("data-emoji-name"), e);
|
2017-05-30 00:50:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 22:55:45 +02:00
|
|
|
function toggle_selected_emoji(e) {
|
2017-05-30 00:50:45 +02:00
|
|
|
// Toggle the currently selected emoji.
|
2019-11-02 00:06:25 +01:00
|
|
|
const selected_emoji = get_selected_emoji();
|
2017-05-30 00:50:45 +02:00
|
|
|
|
|
|
|
if (selected_emoji === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:13:06 +02:00
|
|
|
const emoji_name = $(selected_emoji).attr("data-emoji-name");
|
2017-05-30 00:50:45 +02:00
|
|
|
|
2020-07-06 22:55:45 +02:00
|
|
|
toggle_reaction(emoji_name, e);
|
2020-07-08 08:02:16 +02:00
|
|
|
}
|
2017-05-30 00:50:45 +02:00
|
|
|
|
2017-07-19 00:28:56 +02:00
|
|
|
function round_off_to_previous_multiple(number_to_round, multiple) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return number_to_round - (number_to_round % multiple);
|
2017-07-19 00:28:56 +02:00
|
|
|
}
|
|
|
|
|
2017-08-27 22:15:02 +02:00
|
|
|
function reset_emoji_showcase() {
|
2022-08-18 00:30:47 +02:00
|
|
|
$(".emoji-showcase-container").empty();
|
2017-08-27 22:15:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function update_emoji_showcase($focused_emoji) {
|
|
|
|
// Don't use jQuery's data() function here. It has the side-effect
|
|
|
|
// of converting emoji names like :100:, :1234: etc to number.
|
2019-11-02 00:06:25 +01:00
|
|
|
const focused_emoji_name = $focused_emoji.attr("data-emoji-name");
|
|
|
|
const canonical_name = emoji.get_canonical_name(focused_emoji_name);
|
2020-07-24 23:30:57 +02:00
|
|
|
|
|
|
|
if (!canonical_name) {
|
|
|
|
blueslip.error("Invalid focused_emoji_name: " + focused_emoji_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-06 00:04:34 +01:00
|
|
|
const focused_emoji_dict = emoji.emojis_by_name.get(canonical_name);
|
2017-08-27 22:15:02 +02:00
|
|
|
|
2020-02-09 04:15:38 +01:00
|
|
|
const emoji_dict = {
|
|
|
|
...focused_emoji_dict,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: focused_emoji_name.replace(/_/g, " "),
|
2020-02-09 04:15:38 +01:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const rendered_showcase = render_emoji_showcase({
|
2020-07-20 22:18:43 +02:00
|
|
|
emoji_dict,
|
2017-08-27 22:15:02 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$(".emoji-showcase-container").html(rendered_showcase);
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:23:44 +02:00
|
|
|
function maybe_change_focused_emoji($emoji_map, next_section, next_index, preserve_scroll) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $next_emoji = get_rendered_emoji(next_section, next_index);
|
|
|
|
if ($next_emoji) {
|
2017-07-19 00:28:56 +02:00
|
|
|
current_section = next_section;
|
|
|
|
current_index = next_index;
|
2017-10-05 21:20:24 +02:00
|
|
|
if (!preserve_scroll) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$next_emoji.trigger("focus");
|
2017-10-05 21:20:24 +02:00
|
|
|
} else {
|
2019-11-02 00:06:25 +01:00
|
|
|
const start = ui.get_scroll_element($emoji_map).scrollTop();
|
2022-01-25 11:36:19 +01:00
|
|
|
$next_emoji.trigger("focus");
|
2019-03-01 01:40:05 +01:00
|
|
|
if (ui.get_scroll_element($emoji_map).scrollTop() !== start) {
|
|
|
|
ui.get_scroll_element($emoji_map).scrollTop(start);
|
2017-10-05 21:20:24 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
update_emoji_showcase($next_emoji);
|
2017-07-19 00:28:56 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-18 17:23:16 +02:00
|
|
|
function maybe_change_active_section(next_section) {
|
2020-06-18 18:23:44 +02:00
|
|
|
const $emoji_map = $(".emoji-popover-emoji-map");
|
|
|
|
|
2017-07-19 00:28:56 +02:00
|
|
|
if (next_section >= 0 && next_section < get_total_sections()) {
|
|
|
|
current_section = next_section;
|
|
|
|
current_index = 0;
|
2019-11-02 00:06:25 +01:00
|
|
|
const offset = section_head_offsets[current_section];
|
2017-07-19 00:28:56 +02:00
|
|
|
if (offset) {
|
2020-06-18 18:23:44 +02:00
|
|
|
ui.get_scroll_element($emoji_map).scrollTop(offset.position_y);
|
|
|
|
maybe_change_focused_emoji($emoji_map, current_section, current_index);
|
2017-07-19 00:28:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_next_emoji_coordinates(move_by) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let next_section = current_section;
|
|
|
|
let next_index = current_index + move_by;
|
|
|
|
let max_len;
|
2017-07-19 00:28:56 +02:00
|
|
|
if (next_index < 0) {
|
|
|
|
next_section = next_section - 1;
|
|
|
|
if (next_section >= 0) {
|
|
|
|
next_index = get_max_index(next_section) - 1;
|
|
|
|
if (move_by === -6) {
|
|
|
|
max_len = get_max_index(next_section);
|
2019-11-02 00:06:25 +01:00
|
|
|
const prev_multiple = round_off_to_previous_multiple(max_len, 6);
|
2020-07-16 23:29:01 +02:00
|
|
|
next_index = prev_multiple + current_index;
|
2020-07-15 00:34:28 +02:00
|
|
|
next_index = next_index >= max_len ? prev_multiple + current_index - 6 : next_index;
|
2017-07-19 00:28:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (next_index >= get_max_index(next_section)) {
|
|
|
|
next_section = next_section + 1;
|
|
|
|
if (next_section < get_total_sections()) {
|
|
|
|
next_index = 0;
|
|
|
|
if (move_by === 6) {
|
|
|
|
max_len = get_max_index(next_index);
|
|
|
|
next_index = current_index % 6;
|
|
|
|
next_index = next_index >= max_len ? max_len - 1 : next_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
section: next_section,
|
|
|
|
index: next_index,
|
|
|
|
};
|
|
|
|
}
|
2017-05-30 00:50:45 +02:00
|
|
|
|
2017-08-17 14:25:40 +02:00
|
|
|
function change_focus_to_filter() {
|
2020-07-20 21:24:26 +02:00
|
|
|
$(".emoji-popover-filter").trigger("focus");
|
2017-08-17 14:25:40 +02:00
|
|
|
// If search is active reset current selected emoji to first emoji.
|
|
|
|
if (search_is_active) {
|
|
|
|
current_section = 0;
|
|
|
|
current_index = 0;
|
|
|
|
}
|
2017-08-27 22:15:02 +02:00
|
|
|
reset_emoji_showcase();
|
2017-08-17 14:25:40 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:26:16 +01:00
|
|
|
export function navigate(event_name, e) {
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
event_name === "toggle_reactions_popover" &&
|
2021-02-28 01:26:16 +01:00
|
|
|
reactions_popped() &&
|
2020-07-15 00:34:28 +02:00
|
|
|
(search_is_active === false || search_results.length === 0)
|
|
|
|
) {
|
2021-02-28 01:26:16 +01:00
|
|
|
hide_emoji_popover();
|
2017-10-02 23:10:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-17 14:25:40 +02:00
|
|
|
// If search is active and results are empty then return immediately.
|
|
|
|
if (search_is_active === true && search_results.length === 0) {
|
2020-08-11 02:09:14 +02:00
|
|
|
// We don't want to prevent default for keys like Backspace and space.
|
2020-07-09 14:00:51 +02:00
|
|
|
return false;
|
2020-07-08 08:02:16 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (event_name === "enter") {
|
2021-06-27 21:24:11 +02:00
|
|
|
if (is_composition(e.target) || is_status_emoji(e.target)) {
|
2020-07-22 04:21:43 +02:00
|
|
|
e.target.click();
|
2020-07-08 08:02:16 +02:00
|
|
|
} else {
|
2020-07-06 22:55:45 +02:00
|
|
|
toggle_selected_emoji(e);
|
2020-07-08 08:02:16 +02:00
|
|
|
}
|
|
|
|
return true;
|
2017-08-17 14:25:40 +02:00
|
|
|
}
|
|
|
|
|
2020-06-18 20:52:25 +02:00
|
|
|
const $popover = $(".emoji-popover").expectOne();
|
|
|
|
const $emoji_map = $popover.find(".emoji-popover-emoji-map").expectOne();
|
2020-06-18 18:35:43 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $selected_emoji = get_rendered_emoji(current_section, current_index);
|
2020-07-15 01:29:15 +02:00
|
|
|
const is_filter_focused = $(".emoji-popover-filter").is(":focus");
|
2017-05-30 00:50:45 +02:00
|
|
|
// special cases
|
2017-08-25 16:32:28 +02:00
|
|
|
if (is_filter_focused) {
|
|
|
|
// Move down into emoji map.
|
2019-11-02 00:06:25 +01:00
|
|
|
const filter_text = $(".emoji-popover-filter").val();
|
|
|
|
const is_cursor_at_end = $(".emoji-popover-filter").caret() === filter_text.length;
|
2020-07-15 00:34:28 +02:00
|
|
|
if (event_name === "down_arrow" || (is_cursor_at_end && event_name === "right_arrow")) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$selected_emoji.trigger("focus");
|
2017-08-25 16:32:28 +02:00
|
|
|
if (current_section === 0 && current_index < 6) {
|
2020-06-18 18:35:43 +02:00
|
|
|
ui.get_scroll_element($emoji_map).scrollTop(0);
|
2017-08-25 16:32:28 +02:00
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
update_emoji_showcase($selected_emoji);
|
2017-08-25 16:32:28 +02:00
|
|
|
return true;
|
2017-05-30 00:50:45 +02:00
|
|
|
}
|
2017-08-25 16:32:28 +02:00
|
|
|
if (event_name === "tab") {
|
2022-01-25 11:36:19 +01:00
|
|
|
$selected_emoji.trigger("focus");
|
|
|
|
update_emoji_showcase($selected_emoji);
|
2017-08-25 16:32:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-07-15 00:34:28 +02:00
|
|
|
} else if (
|
|
|
|
(current_section === 0 && current_index < 6 && event_name === "up_arrow") ||
|
|
|
|
(current_section === 0 && current_index === 0 && event_name === "left_arrow")
|
|
|
|
) {
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($selected_emoji) {
|
2017-07-19 00:28:56 +02:00
|
|
|
// In this case, we're move up into the reaction
|
|
|
|
// filter. Here, we override the default browser
|
|
|
|
// behavior, which in Firefox is good (preserving
|
|
|
|
// the cursor position) and in Chrome is bad (cursor
|
|
|
|
// goes to beginning) with something reasonable and
|
|
|
|
// consistent (cursor goes to the end of the filter
|
|
|
|
// string).
|
2020-12-22 11:56:36 +01:00
|
|
|
$(".emoji-popover-filter").trigger("focus").caret(Number.POSITIVE_INFINITY);
|
2020-06-18 18:35:43 +02:00
|
|
|
ui.get_scroll_element($emoji_map).scrollTop(0);
|
2019-03-01 01:40:05 +01:00
|
|
|
ui.get_scroll_element($(".emoji-search-results-container")).scrollTop(0);
|
2017-07-19 00:28:56 +02:00
|
|
|
current_section = 0;
|
|
|
|
current_index = 0;
|
2017-08-27 22:15:02 +02:00
|
|
|
reset_emoji_showcase();
|
2017-05-30 00:50:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-05-09 23:36:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (event_name) {
|
|
|
|
case "tab":
|
|
|
|
case "shift_tab":
|
2017-08-17 14:25:40 +02:00
|
|
|
change_focus_to_filter();
|
2021-05-09 23:36:58 +02:00
|
|
|
return true;
|
|
|
|
case "page_up":
|
|
|
|
maybe_change_active_section(current_section - 1);
|
|
|
|
return true;
|
|
|
|
case "page_down":
|
|
|
|
maybe_change_active_section(current_section + 1);
|
|
|
|
return true;
|
|
|
|
case "down_arrow":
|
|
|
|
case "up_arrow":
|
|
|
|
case "left_arrow":
|
|
|
|
case "right_arrow": {
|
|
|
|
const next_coord = get_next_emoji_coordinates(
|
|
|
|
{down_arrow: 6, up_arrow: -6, left_arrow: -1, right_arrow: 1}[event_name],
|
|
|
|
);
|
|
|
|
return maybe_change_focused_emoji($emoji_map, next_coord.section, next_coord.index);
|
2017-07-19 00:28:56 +02:00
|
|
|
}
|
2021-05-09 23:36:58 +02:00
|
|
|
default:
|
|
|
|
return false;
|
2017-05-30 00:50:45 +02:00
|
|
|
}
|
2021-02-28 01:26:16 +01:00
|
|
|
}
|
2017-05-30 00:50:45 +02:00
|
|
|
|
2017-08-29 23:01:18 +02:00
|
|
|
function process_keypress(e) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const is_filter_focused = $(".emoji-popover-filter").is(":focus");
|
2019-11-02 00:06:25 +01:00
|
|
|
const pressed_key = e.which;
|
2020-12-22 11:26:39 +01:00
|
|
|
if (
|
|
|
|
!is_filter_focused &&
|
2017-10-02 23:10:55 +02:00
|
|
|
// ':' => 58, is a hotkey for toggling reactions popover.
|
2020-12-22 11:26:39 +01:00
|
|
|
pressed_key !== 58 &&
|
|
|
|
((pressed_key >= 32 && pressed_key <= 126) || pressed_key === 8)
|
|
|
|
) {
|
|
|
|
// Handle only printable characters or Backspace.
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2017-08-29 23:01:18 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $emoji_filter = $(".emoji-popover-filter");
|
|
|
|
const old_query = $emoji_filter.val();
|
2020-12-22 11:26:39 +01:00
|
|
|
let new_query = "";
|
|
|
|
|
|
|
|
if (pressed_key === 8) {
|
|
|
|
// Handles Backspace.
|
|
|
|
new_query = old_query.slice(0, -1);
|
|
|
|
} else {
|
|
|
|
// Handles any printable character.
|
2021-12-03 21:54:23 +01:00
|
|
|
const key_str = String.fromCodePoint(e.which);
|
2020-12-22 11:26:39 +01:00
|
|
|
new_query = old_query + key_str;
|
2017-08-29 23:01:18 +02:00
|
|
|
}
|
2020-12-22 11:26:39 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$emoji_filter.val(new_query);
|
2020-12-22 11:26:39 +01:00
|
|
|
change_focus_to_filter();
|
|
|
|
filter_emojis();
|
2017-08-29 23:01:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function emoji_select_tab($elt) {
|
|
|
|
const scrolltop = $elt.scrollTop();
|
|
|
|
const scrollheight = $elt.prop("scrollHeight");
|
|
|
|
const elt_height = $elt.height();
|
2019-11-02 00:06:25 +01:00
|
|
|
let currently_selected = "";
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const o of section_head_offsets) {
|
2018-06-04 21:13:07 +02:00
|
|
|
if (scrolltop + elt_height / 2 >= o.position_y) {
|
2017-07-19 00:28:56 +02:00
|
|
|
currently_selected = o.section;
|
|
|
|
}
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2017-07-19 00:28:56 +02:00
|
|
|
// Handles the corner case of the last category being
|
|
|
|
// smaller than half of the emoji picker height.
|
|
|
|
if (elt_height + scrolltop === scrollheight) {
|
2022-01-24 09:05:06 +01:00
|
|
|
currently_selected = section_head_offsets.at(-1).section;
|
2017-05-30 00:50:45 +02:00
|
|
|
}
|
2017-07-19 00:28:56 +02:00
|
|
|
// Handles the corner case of the scrolling back to top.
|
|
|
|
if (scrolltop === 0) {
|
|
|
|
currently_selected = section_head_offsets[0].section;
|
2017-05-30 00:50:45 +02:00
|
|
|
}
|
2017-07-19 00:28:56 +02:00
|
|
|
if (currently_selected) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".emoji-popover-tab-item.active").removeClass("active");
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`.emoji-popover-tab-item[data-tab-name="${CSS.escape(currently_selected)}"]`).addClass(
|
|
|
|
"active",
|
|
|
|
);
|
2017-05-30 00:50:45 +02:00
|
|
|
}
|
2021-02-28 01:26:16 +01:00
|
|
|
}
|
2017-05-30 00:50:45 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
function register_popover_events($popover) {
|
|
|
|
const $emoji_map = $popover.find(".emoji-popover-emoji-map");
|
2017-08-29 22:59:25 +02:00
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
ui.get_scroll_element($emoji_map).on("scroll", () => {
|
2021-02-28 01:26:16 +01:00
|
|
|
emoji_select_tab(ui.get_scroll_element($emoji_map));
|
2017-08-29 22:59:25 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".emoji-popover-filter").on("input", filter_emojis);
|
2020-07-20 21:26:58 +02:00
|
|
|
$(".emoji-popover-filter").on("keydown", process_enter_while_filtering);
|
|
|
|
$(".emoji-popover").on("keypress", process_keypress);
|
|
|
|
$(".emoji-popover").on("keydown", (e) => {
|
2020-08-11 02:09:14 +02:00
|
|
|
// Because of cross-browser issues we need to handle Backspace
|
|
|
|
// key separately. Firefox fires `keypress` event for Backspace
|
2017-08-29 23:01:18 +02:00
|
|
|
// key but chrome doesn't so we need to trigger the logic for
|
2020-08-11 02:09:14 +02:00
|
|
|
// handling Backspace in `keydown` event which is fired by both.
|
2017-08-29 23:01:18 +02:00
|
|
|
if (e.which === 8) {
|
|
|
|
process_keypress(e);
|
|
|
|
}
|
|
|
|
});
|
2017-08-29 22:59:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function build_emoji_popover($elt, id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const template_args = {
|
2017-08-29 22:42:54 +02:00
|
|
|
class: "emoji-info-popover",
|
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
let placement = popovers.compute_placement($elt, APPROX_HEIGHT, APPROX_WIDTH, true);
|
2017-09-26 21:54:34 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (placement === "viewport_center") {
|
2019-02-15 19:15:03 +01:00
|
|
|
// For legacy reasons `compute_placement` actually can
|
|
|
|
// return `viewport_center`, but bootstrap doesn't actually
|
|
|
|
// support that.
|
2020-07-15 01:29:15 +02:00
|
|
|
placement = "left";
|
2019-02-15 19:15:03 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let template = render_emoji_popover(template_args);
|
2017-09-26 21:54:34 +02:00
|
|
|
|
|
|
|
// if the window is mobile sized, add the `.popover-flex` wrapper to the emoji
|
|
|
|
// popover so that it will be wrapped in flex and centered in the screen.
|
|
|
|
if (window.innerWidth <= 768) {
|
|
|
|
template = "<div class='popover-flex'>" + template + "</div>";
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$elt.popover({
|
2017-08-29 22:42:54 +02:00
|
|
|
// temporary patch for handling popover placement of `viewport_center`
|
2020-07-20 22:18:43 +02:00
|
|
|
placement,
|
2019-02-15 19:15:20 +01:00
|
|
|
fix_positions: true,
|
2020-07-20 22:18:43 +02:00
|
|
|
template,
|
2018-12-18 19:34:45 +01:00
|
|
|
title: "",
|
|
|
|
content: generate_emoji_picker_content(id),
|
2019-09-17 23:54:52 +02:00
|
|
|
html: true,
|
2018-12-18 19:34:45 +01:00
|
|
|
trigger: "manual",
|
2017-08-29 22:42:54 +02:00
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
$elt.popover("show");
|
2019-02-14 13:58:44 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $popover = $elt.data("popover").$tip;
|
|
|
|
$popover.find(".emoji-popover-filter").trigger("focus");
|
|
|
|
$current_message_emoji_popover_elem = $elt;
|
2017-08-29 22:42:54 +02:00
|
|
|
|
|
|
|
emoji_catalog_last_coordinates = {
|
|
|
|
section: 0,
|
|
|
|
index: 0,
|
|
|
|
};
|
|
|
|
show_emoji_catalog();
|
|
|
|
|
2022-03-16 22:19:29 +01:00
|
|
|
$(() => refill_section_head_offsets($popover));
|
2022-01-25 11:36:19 +01:00
|
|
|
register_popover_events($popover);
|
2021-02-28 01:26:16 +01:00
|
|
|
}
|
2017-08-29 22:42:54 +02:00
|
|
|
|
2021-02-28 01:26:16 +01:00
|
|
|
export function toggle_emoji_popover(element, id) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $last_popover_elem = $current_message_emoji_popover_elem;
|
2017-08-29 22:42:54 +02:00
|
|
|
popovers.hide_all();
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($last_popover_elem !== undefined && $last_popover_elem.get()[0] === element) {
|
2017-08-29 22:42:54 +02:00
|
|
|
// We want it to be the case that a user can dismiss a popover
|
|
|
|
// by clicking on the same element that caused the popover.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$(element).closest(".message_row").toggleClass("has_popover has_emoji_popover");
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elt = $(element);
|
2017-08-29 22:42:54 +02:00
|
|
|
if (id !== undefined) {
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.current.select_id(id);
|
2017-08-29 22:42:54 +02:00
|
|
|
}
|
|
|
|
|
2021-06-27 21:24:11 +02:00
|
|
|
if (user_status_ui.user_status_picker_open()) {
|
2022-01-25 11:36:19 +01:00
|
|
|
build_emoji_popover($elt, id, true);
|
|
|
|
} else if ($elt.data("popover") === undefined) {
|
2017-09-07 18:03:17 +02:00
|
|
|
// Keep the element over which the popover is based off visible.
|
2022-01-25 11:36:19 +01:00
|
|
|
$elt.addClass("reaction_button_visible");
|
|
|
|
build_emoji_popover($elt, id);
|
2017-08-29 22:42:54 +02:00
|
|
|
}
|
2017-08-27 22:15:02 +02:00
|
|
|
reset_emoji_showcase();
|
2021-02-28 01:26:16 +01:00
|
|
|
}
|
2017-08-29 22:42:54 +02:00
|
|
|
|
2021-02-28 01:26:16 +01:00
|
|
|
export function register_click_handlers() {
|
2020-07-15 01:29:15 +02:00
|
|
|
$(document).on("click", ".emoji-popover-emoji.reaction", function (e) {
|
2017-08-16 12:14:34 +02:00
|
|
|
// When an emoji is clicked in the popover,
|
|
|
|
// if the user has reacted to this message with this emoji
|
|
|
|
// the reaction is removed
|
|
|
|
// otherwise, the reaction is added
|
2020-04-06 20:13:06 +02:00
|
|
|
const emoji_name = $(this).attr("data-emoji-name");
|
2020-07-06 22:55:45 +02:00
|
|
|
toggle_reaction(emoji_name, e);
|
2017-08-16 12:14:34 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$(document).on("click", ".emoji-popover-emoji.composition", function (e) {
|
2020-04-06 20:13:06 +02:00
|
|
|
const emoji_name = $(this).attr("data-emoji-name");
|
2020-07-15 01:29:15 +02:00
|
|
|
const emoji_text = ":" + emoji_name + ":";
|
2019-01-15 09:16:59 +01:00
|
|
|
// The following check will return false if emoji was not selected in
|
|
|
|
// message edit form.
|
|
|
|
if (edit_message_id !== null) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $edit_message_textarea = $(
|
2021-04-20 19:50:01 +02:00
|
|
|
`#edit_form_${CSS.escape(edit_message_id)} .message_edit_content`,
|
|
|
|
);
|
2019-01-15 09:16:59 +01:00
|
|
|
// Assign null to edit_message_id so that the selection of emoji in new
|
|
|
|
// message composition form works correctly.
|
|
|
|
edit_message_id = null;
|
2022-01-25 11:36:19 +01:00
|
|
|
compose_ui.insert_syntax_and_focus(emoji_text, $edit_message_textarea);
|
2019-01-15 09:16:59 +01:00
|
|
|
} else {
|
|
|
|
compose_ui.insert_syntax_and_focus(emoji_text);
|
|
|
|
}
|
2017-04-27 07:27:25 +02:00
|
|
|
e.stopPropagation();
|
2021-02-28 01:26:16 +01:00
|
|
|
hide_emoji_popover();
|
2017-04-27 07:27:25 +02:00
|
|
|
});
|
|
|
|
|
2021-12-03 07:19:24 +01:00
|
|
|
$("body").on("click", ".emoji_map", (e) => {
|
2017-04-27 07:27:25 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2021-11-23 19:01:36 +01:00
|
|
|
|
2021-12-03 07:19:24 +01:00
|
|
|
const compose_click_target = compose_ui.get_compose_click_target(e);
|
|
|
|
if ($(compose_click_target).parents(".message_edit_form").length === 1) {
|
2021-04-20 23:16:14 +02:00
|
|
|
// Store message id in global variable edit_message_id so that
|
2020-03-28 01:25:56 +01:00
|
|
|
// its value can be further used to correctly find the message textarea element.
|
2021-12-03 07:19:24 +01:00
|
|
|
edit_message_id = rows.get_message_id(compose_click_target);
|
2019-01-15 09:16:59 +01:00
|
|
|
} else {
|
|
|
|
edit_message_id = null;
|
|
|
|
}
|
2021-12-03 07:19:24 +01:00
|
|
|
toggle_emoji_popover(compose_click_target);
|
2017-04-27 07:27:25 +02:00
|
|
|
});
|
|
|
|
|
2017-09-07 11:14:59 +02:00
|
|
|
$("#main_div").on("click", ".reaction_button", function (e) {
|
2017-04-27 07:27:25 +02:00
|
|
|
e.stopPropagation();
|
2017-07-19 14:01:06 +02:00
|
|
|
|
2022-04-29 21:39:12 +02:00
|
|
|
if (page_params.is_spectator) {
|
|
|
|
spectators.login_to_access();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = rows.get_message_id(this);
|
2021-02-28 01:26:16 +01:00
|
|
|
toggle_emoji_popover(this, message_id);
|
2017-04-27 07:27:25 +02:00
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$("body").on("click", ".actions_popover .reaction_button", (e) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const message_id = $(e.currentTarget).data("message-id");
|
2017-04-27 07:27:25 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
// HACK: Because we need the popover to be based off an
|
|
|
|
// element that definitely exists in the page even if the
|
|
|
|
// message wasn't sent by us and thus the .reaction_hover
|
|
|
|
// element is not present, we use the message's
|
2018-10-15 16:25:18 +02:00
|
|
|
// .fa-chevron-down element as the base for the popover.
|
2019-11-02 00:06:25 +01:00
|
|
|
const elem = $(".selected_message .actions_hover")[0];
|
2021-02-28 01:26:16 +01:00
|
|
|
toggle_emoji_popover(elem, message_id);
|
2017-04-27 07:27:25 +02:00
|
|
|
});
|
2017-05-30 00:50:45 +02:00
|
|
|
|
2017-07-19 00:28:56 +02:00
|
|
|
$("body").on("click", ".emoji-popover-tab-item", function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2020-06-18 18:41:58 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const $popover = $(e.currentTarget).closest(".emoji-info-popover").expectOne();
|
2020-06-18 18:41:58 +02:00
|
|
|
const $emoji_map = $popover.find(".emoji-popover-emoji-map");
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const offset = section_head_offsets.find(
|
|
|
|
(o) => o.section === $(this).attr("data-tab-name"),
|
|
|
|
);
|
2017-07-19 00:28:56 +02:00
|
|
|
|
|
|
|
if (offset) {
|
2020-06-18 18:41:58 +02:00
|
|
|
ui.get_scroll_element($emoji_map).scrollTop(offset.position_y);
|
2017-07-19 00:28:56 +02:00
|
|
|
}
|
|
|
|
});
|
2017-08-27 22:15:02 +02:00
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$("body").on("click", ".emoji-popover-filter", () => {
|
2017-08-27 22:15:02 +02:00
|
|
|
reset_emoji_showcase();
|
|
|
|
});
|
2017-09-29 22:14:57 +02:00
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$("body").on("mouseenter", ".emoji-popover-emoji", (e) => {
|
2020-06-18 18:30:26 +02:00
|
|
|
const emoji_id = $(e.currentTarget).data("emoji-id");
|
2019-11-02 00:06:25 +01:00
|
|
|
const emoji_coordinates = get_emoji_coordinates(emoji_id);
|
2017-10-05 16:38:01 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const $emoji_map = $(e.currentTarget)
|
|
|
|
.closest(".emoji-popover")
|
|
|
|
.expectOne()
|
|
|
|
.find(".emoji-popover-emoji-map");
|
2020-06-18 18:23:44 +02:00
|
|
|
maybe_change_focused_emoji(
|
|
|
|
$emoji_map,
|
|
|
|
emoji_coordinates.section,
|
|
|
|
emoji_coordinates.index,
|
2020-07-02 02:16:03 +02:00
|
|
|
true,
|
2020-06-18 18:23:44 +02:00
|
|
|
);
|
2017-09-29 22:14:57 +02:00
|
|
|
});
|
2021-06-28 10:02:45 +02:00
|
|
|
|
|
|
|
$("body").on("click", "#set_user_status_modal #selected_emoji", (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
toggle_emoji_popover(e.target);
|
|
|
|
// Because the emoji picker gets drawn on top of the user
|
|
|
|
// status modal, we need this hack to make clicking outside
|
|
|
|
// the emoji picker only close the emoji picker, and not the
|
|
|
|
// whole user status modal.
|
2021-07-07 09:16:19 +02:00
|
|
|
$(".app, .header, .modal__overlay, #set_user_status_modal").css("pointer-events", "none");
|
2021-06-28 10:02:45 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on("click", ".emoji-popover-emoji.status_emoji", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
hide_emoji_popover();
|
|
|
|
const emoji_name = $(this).attr("data-emoji-name");
|
2021-07-28 16:00:58 +02:00
|
|
|
let emoji_info = {
|
|
|
|
emoji_name,
|
|
|
|
emoji_alt_code: user_settings.emojiset === "text",
|
|
|
|
};
|
2021-06-28 10:02:45 +02:00
|
|
|
if (!emoji_info.emoji_alt_code) {
|
|
|
|
emoji_info = {...emoji_info, ...emoji.get_emoji_details_by_name(emoji_name)};
|
|
|
|
}
|
|
|
|
user_status_ui.set_selected_emoji_info(emoji_info);
|
|
|
|
user_status_ui.update_button();
|
|
|
|
user_status_ui.toggle_clear_message_button();
|
|
|
|
});
|
2021-02-28 01:26:16 +01:00
|
|
|
}
|
2017-07-19 00:28:56 +02:00
|
|
|
|
2021-02-28 01:26:16 +01:00
|
|
|
export function initialize() {
|
|
|
|
rebuild_catalog();
|
|
|
|
}
|