2021-03-11 05:43:45 +01:00
import $ from "jquery" ;
2021-06-23 18:06:10 +02:00
import render _keyboard _shortcut from "../templates/keyboard_shortcuts.hbs" ;
2021-03-11 03:09:13 +01:00
import render _markdown _help from "../templates/markdown_help.hbs" ;
2021-06-06 19:43:36 +02:00
import render _search _operator from "../templates/search_operators.hbs" ;
2021-03-11 03:09:13 +01:00
2021-03-22 16:09:12 +01:00
import * as browser _history from "./browser_history" ;
2021-02-28 00:59:34 +01:00
import * as common from "./common" ;
import * as components from "./components" ;
2021-04-13 06:51:54 +02:00
import { $t , $t _html } from "./i18n" ;
2021-02-28 00:59:34 +01:00
import * as keydown _util from "./keydown_util" ;
2021-03-11 03:09:13 +01:00
import * as markdown from "./markdown" ;
2021-02-28 01:03:46 +01:00
import * as overlays from "./overlays" ;
2021-03-11 03:09:13 +01:00
import * as rendered _markdown from "./rendered_markdown" ;
2021-02-28 21:33:10 +01:00
import * as ui from "./ui" ;
2021-10-25 19:17:19 +02:00
import { user _settings } from "./user_settings" ;
2021-03-11 03:09:13 +01:00
import * as util from "./util" ;
2021-02-10 16:46:49 +01:00
2018-04-13 22:48:20 +02:00
// Make it explicit that our toggler is undefined until
2018-06-04 14:51:21 +02:00
// set_up_toggler is called.
2021-02-28 00:59:34 +01:00
export let toggler ;
2018-04-13 22:48:20 +02:00
2021-03-11 03:09:13 +01:00
const markdown _help _rows = [
{
markdown : "*italic*" ,
usage _html : "(or <kbd>Ctrl</kbd>+<kbd>I</kbd>)" ,
} ,
{
markdown : "**bold**" ,
usage _html : "(or <kbd>Ctrl</kbd>+<kbd>B</kbd>)" ,
} ,
{
markdown : "~~strikethrough~~" ,
} ,
{
markdown : "[Zulip website](https://zulip.org)" ,
usage _html : "(or <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>L</kbd>)" ,
} ,
{
markdown : ` \
* Milk
* Tea
* Green tea
* Black tea
* Oolong tea
* Coffee ` ,
} ,
{
markdown : ` \
1. Milk
1. Tea
1. Coffee ` ,
} ,
{
markdown : ":heart:" ,
usage _html :
'(and <a href="https://www.webfx.com/tools/emoji-cheat-sheet/" target="_blank" rel="noopener noreferrer">many others</a>, from the <a href="https://code.google.com/p/noto/" target="_blank" rel="noopener noreferrer">Noto Project</a>)' ,
} ,
{
markdown : "@**Joe Smith**" ,
usage _html : "(autocompletes from @joe)" ,
output _html : '<p><span class="user-mention">@Joe Smith</span></p>' ,
effect _html : "(notifies Joe Smith)" ,
} ,
{
markdown : "@_**Joe Smith**" ,
usage _html : "(autocompletes from @_joe)" ,
output _html : '<p><span class="user-mention">Joe Smith</span></p>' ,
effect _html : "(links to profile but doesn't notify Joe Smith)" ,
} ,
{
markdown : "@**all**" ,
effect _html : "(notifies all recipients)" ,
} ,
{
markdown : "#**streamName**" ,
output _html : "<p><a>#streamName</a></p>" ,
effect _html : "(links to a stream)" ,
} ,
{
markdown : "/me is busy working" ,
output _html : '<p><span class="sender_name-in-status">Iago</span> is busy working</p>' ,
} ,
2022-07-07 12:08:06 +02:00
{
markdown : ` /poll What did you drink this morning?
Milk
Tea
Coffee ` ,
output _html : ` \
< div class = "poll-widget" >
< h4 class = "poll-question-header reduced-font-size" > What did you drink this morning ? < / h 4 >
< i class = "fa fa-pencil poll-edit-question" > < / i >
< ul class = "poll-widget" >
< li >
< button class = "poll-vote" >
0
< / b u t t o n >
< span > Milk < / s p a n >
< / l i >
< li >
< button class = "poll-vote" >
0
< / b u t t o n >
< span > Tea < / s p a n >
< / l i >
< li >
< button class = "poll-vote" >
0
< / b u t t o n >
< span > Coffee < / s p a n >
< / l i >
< / u l >
< / d i v >
` ,
} ,
2021-03-11 03:09:13 +01:00
{
markdown : "Some inline `code`" ,
} ,
{
markdown : ` \
\ ` \` \`
def zulip ( ) :
print "Zulip"
\ ` \` \` ` ,
} ,
{
markdown : ` \
\ ` \` \` python
def zulip ( ) :
print "Zulip"
\ ` \` \` ` ,
output _html : ` \
< div class = "codehilite" > < pre > < span class = "k" > def < / s p a n > < s p a n c l a s s = " n f " > z u l i p < / s p a n > < s p a n c l a s s = " p " > ( ) : < / s p a n >
< span class = "k" > print < / s p a n > < s p a n c l a s s = " s " > " Z u l i p " < / s p a n > < / p r e > < / d i v > ` ,
} ,
{
2021-04-13 01:58:09 +02:00
note _html : $t _html (
{
defaultMessage :
"To add syntax highlighting to a multi-line code block, add the language's <b>first</b> <z-link>Pygments short name</z-link> after the first set of back-ticks. You can also make a code block by indenting each line with 4 spaces." ,
} ,
{
"z-link" : ( content _html ) =>
` <a target="_blank" rel="noopener noreferrer" href="https://pygments.org/docs/lexers/"> ${ content _html } </a> ` ,
} ,
2021-03-11 03:09:13 +01:00
) ,
} ,
{
markdown : "> Quoted" ,
} ,
{
markdown : ` \
\ ` \` \` quote
Quoted block
\ ` \` \` ` ,
} ,
{
markdown : ` \
\ ` \` \` spoiler Always visible heading
This text won ' t be visible until the user clicks .
\ ` \` \` ` ,
} ,
{
markdown : "Some inline math $$ e^{i \\pi} + 1 = 0 $$" ,
} ,
{
markdown : ` \
\ ` \` \` math
\ \ int _ { 0 } ^ { 1 } f ( x ) dx
\ ` \` \` ` ,
} ,
{
2021-04-13 01:58:09 +02:00
note _html : $t _html (
{
defaultMessage :
"You can also make <z-link>tables</z-link> with this <z-link>Markdown-ish table syntax</z-link>." ,
} ,
{
"z-link" : ( content _html ) =>
` <a target="_blank" rel="noopener noreferrer" href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#wiki-tables"> ${ content _html } </a> ` ,
} ,
2021-03-11 03:09:13 +01:00
) ,
} ,
] ;
2021-02-28 00:59:34 +01:00
export function set _up _toggler ( ) {
2021-03-11 03:09:13 +01:00
for ( const row of markdown _help _rows ) {
if ( row . markdown && ! row . output _html ) {
const message = { raw _content : row . markdown } ;
markdown . apply _markdown ( message ) ;
row . output _html = util . clean _user _content _links ( message . content ) ;
}
}
const $markdown _help = $ ( render _markdown _help ( { markdown _help _rows } ) ) ;
$markdown _help . find ( ".rendered_markdown" ) . each ( function ( ) {
rendered _markdown . update _elements ( $ ( this ) ) ;
} ) ;
$ ( ".informational-overlays .overlay-body" ) . append ( $markdown _help ) ;
2021-06-06 19:43:36 +02:00
const $search _operators = $ ( render _search _operator ( ) ) ;
$ ( ".informational-overlays .overlay-body" ) . append ( $search _operators ) ;
2021-06-23 18:06:10 +02:00
const $keyboard _shortcuts = $ ( render _keyboard _shortcut ( ) ) ;
$ ( ".informational-overlays .overlay-body" ) . append ( $keyboard _shortcuts ) ;
2019-11-02 00:06:25 +01:00
const opts = {
2018-03-30 14:09:11 +02:00
selected : 0 ,
2018-04-04 17:01:34 +02:00
child _wants _focus : true ,
2018-03-30 14:09:11 +02:00
values : [
2021-04-13 06:51:54 +02:00
{ label : $t ( { defaultMessage : "Keyboard shortcuts" } ) , key : "keyboard-shortcuts" } ,
{ label : $t ( { defaultMessage : "Message formatting" } ) , key : "message-formatting" } ,
{ label : $t ( { defaultMessage : "Search operators" } ) , key : "search-operators" } ,
2018-03-30 14:09:11 +02:00
] ,
2020-07-20 22:18:43 +02:00
callback ( name , key ) {
2018-03-30 14:09:11 +02:00
$ ( ".overlay-modal" ) . hide ( ) ;
2021-02-03 23:23:32 +01:00
$ ( ` # ${ CSS . escape ( key ) } ` ) . show ( ) ;
ui . get _scroll _element ( $ ( ` # ${ CSS . escape ( key ) } ` ) . find ( ".modal-body" ) ) . trigger ( "focus" ) ;
2018-03-30 14:09:11 +02:00
} ,
2018-04-04 17:01:34 +02:00
} ;
2018-03-30 14:09:11 +02:00
2021-02-28 00:59:34 +01:00
toggler = components . toggle ( opts ) ;
2022-01-25 11:36:19 +01:00
const $elem = toggler . get ( ) ;
$elem . addClass ( "large allow-overflow" ) ;
2018-04-04 17:01:34 +02:00
2020-07-02 01:39:34 +02:00
const modals = opts . values . map ( ( item ) => {
2019-11-02 00:06:25 +01:00
const key = item . key ; // e.g. message-formatting
2022-01-25 11:36:19 +01:00
const $modal = $ ( ` # ${ CSS . escape ( key ) } ` ) . find ( ".modal-body" ) ;
return $modal ;
2018-04-04 17:01:34 +02:00
} ) ;
2022-01-25 11:36:19 +01:00
for ( const $modal of modals ) {
ui . get _scroll _element ( $modal ) . prop ( "tabindex" , 0 ) ;
2018-04-04 17:01:34 +02:00
keydown _util . handle ( {
2022-01-25 11:36:19 +01:00
$elem : $modal ,
2018-04-04 17:01:34 +02:00
handlers : {
2021-05-26 19:51:07 +02:00
ArrowLeft : toggler . maybe _go _left ,
ArrowRight : toggler . maybe _go _right ,
2018-04-04 17:01:34 +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
}
2018-04-04 17:01:34 +02:00
2022-01-25 11:36:19 +01:00
$ ( ".informational-overlays .overlay-tabs" ) . append ( $elem ) ;
2018-03-30 14:09:11 +02:00
2021-10-25 19:17:19 +02:00
$ ( "#go-to-default-view-hotkey-help" ) . toggleClass (
"notdisplayed" ,
! user _settings . escape _navigates _to _default _view ,
) ;
2019-06-07 11:03:13 +02:00
common . adjust _mac _shortcuts ( ".hotkeys_table .hotkey kbd" ) ;
2019-05-07 09:26:25 +02:00
common . adjust _mac _shortcuts ( "#markdown-instructions kbd" ) ;
2021-02-28 00:59:34 +01:00
}
2018-03-30 14:09:11 +02:00
2021-02-28 00:59:34 +01:00
export function show ( target ) {
if ( ! toggler ) {
set _up _toggler ( ) ;
2018-06-04 14:51:21 +02:00
}
2022-01-25 11:36:19 +01:00
const $overlay = $ ( ".informational-overlays" ) ;
2018-03-30 14:09:11 +02:00
2022-01-25 11:36:19 +01:00
if ( ! $overlay . hasClass ( "show" ) ) {
2018-03-30 14:09:11 +02:00
overlays . open _overlay ( {
2020-07-15 01:29:15 +02:00
name : "informationalOverlays" ,
2022-01-25 11:36:19 +01:00
$overlay ,
2020-07-20 22:18:43 +02:00
on _close ( ) {
2021-03-22 16:09:12 +01:00
browser _history . exit _overlay ( ) ;
2018-03-30 14:09:11 +02:00
} ,
} ) ;
}
if ( target ) {
2021-02-28 00:59:34 +01:00
toggler . goto ( target ) ;
2018-03-30 14:09:11 +02:00
}
2021-02-28 00:59:34 +01:00
}